From: moodler Date: Mon, 2 Oct 2006 17:24:54 +0000 (+0000) Subject: Fixes related to presets and MDL-6755. X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=8303eb8447554de6514d95091b61beee736b7b65;p=moodle.git Fixes related to presets and MDL-6755. The original code was shocking once I started reviewing it, and even after heavy editing I'm still not happy with it, but at least it works more like it was supposed to. Needs more cleaning up to make it clearer and probably safer. Also, presets can now contain langage packs and they'll be used. The image gallery is an example. --- diff --git a/lang/en_utf8/data.php b/lang/en_utf8/data.php index 8a11ad5a78..f2e4842719 100644 --- a/lang/en_utf8/data.php +++ b/lang/en_utf8/data.php @@ -198,7 +198,7 @@ $string['fieldmappings'] = 'Field Mappings'; $string['mappingwarning'] = 'All old fields not mapped to a new field will be lost and all data in that field will be removed.'; $string['blank'] = 'Blank'; -$string['presetwarning'] = 'Saving as a preset publishes this template across the entire site. All users will be able to use it in their databases.'; +$string['presetinfo'] = 'Saving as a preset will publish this template. Other users may be able to use it in their databases.'; $string['deletewarning'] = 'Are you sure you want to delete this preset?'; $string['importsuccess'] = 'The preset has been successfully applied.'; $string['addentries'] = 'Add entries'; diff --git a/mod/data/db/access.php b/mod/data/db/access.php index aee820c1c6..dfed91c8b4 100644 --- a/mod/data/db/access.php +++ b/mod/data/db/access.php @@ -163,8 +163,36 @@ $mod_data_capabilities = array( 'coursecreator' => CAP_ALLOW, 'admin' => CAP_ALLOW ) + ), + + 'mod/data:viewalluserpresets' => array( + + 'captype' => 'read', + 'contextlevel' => CONTEXT_MODULE, + 'legacy' => array( + 'guest' => CAP_PREVENT, + 'student' => CAP_PREVENT, + 'teacher' => CAP_ALLOW, + 'editingteacher' => CAP_ALLOW, + 'coursecreator' => CAP_ALLOW, + 'admin' => CAP_ALLOW + ) + ), + + 'mod/data:manageuserpresets' => array( + + 'captype' => 'write', + 'contextlevel' => CONTEXT_MODULE, + 'legacy' => array( + 'guest' => CAP_PREVENT, + 'student' => CAP_PREVENT, + 'teacher' => CAP_PREVENT, + 'editingteacher' => CAP_PREVENT, + 'coursecreator' => CAP_PREVENT, + 'admin' => CAP_ALLOW + ) ) ); -?> \ No newline at end of file +?> diff --git a/mod/data/edit.php b/mod/data/edit.php index d000acb797..e1bc855bc9 100755 --- a/mod/data/edit.php +++ b/mod/data/edit.php @@ -95,7 +95,8 @@ $strdata = get_string('modulenameplural','data'); print_header_simple($data->name, '', "$strdata -> $data->name", - '', '', true, '', navmenu($course, $cm), '', ''); + '', '', true, update_module_button($cm->id, $course->id, get_string('modulename', 'data')), + navmenu($course, $cm), '', ''); print_heading(format_string($data->name)); diff --git a/mod/data/field.php b/mod/data/field.php index e747ce4290..e53de75966 100755 --- a/mod/data/field.php +++ b/mod/data/field.php @@ -181,7 +181,7 @@ } else { - data_fields_print_header($course,$cm,$data, false); + data_print_header($course,$cm,$data, false); // Print confirmation message. $field = data_get_field_from_id($fid, $data); @@ -232,14 +232,14 @@ if (($mode == 'new') && (!empty($newtype)) && confirm_sesskey()) { /// Adding a new field $CFG->pagepath='mod/data/field/'.$newtype; - data_fields_print_header($course,$cm,$data); + data_print_header($course,$cm,$data,'fields'); $field = data_get_field_new($newtype, $data); $field->display_edit_field(); } else if ($mode == 'display' && confirm_sesskey()) { /// Display/edit existing field $CFG->pagepath='mod/data/field/'.$newtype; - data_fields_print_header($course,$cm,$data); + data_print_header($course,$cm,$data,'fields'); $field = data_get_field_from_id($fid, $data); $field->display_edit_field(); @@ -247,7 +247,7 @@ } else { /// Display the main listing of all fields $CFG->pagepath='mod/data/field/'.$newtype; - data_fields_print_header($course,$cm,$data); + data_print_header($course,$cm,$data,'fields'); if (!record_exists('data_fields','dataid',$data->id)) { diff --git a/mod/data/lib.php b/mod/data/lib.php index 9d494070ae..e502c6178b 100755 --- a/mod/data/lib.php +++ b/mod/data/lib.php @@ -1260,34 +1260,6 @@ function data_get_cm($data) { } -function data_fields_print_header($course,$cm,$data,$showtabs=true) { - - global $CFG, $displaynoticegood, $displaynoticebad; - - $strdata = get_string('modulenameplural','data'); - - print_header_simple($data->name, '', "$strdata -> $data->name", - '', '', true, '', navmenu($course, $cm)); - - print_heading(format_string($data->name)); - - /// Print the tabs - - if ($showtabs) { - $currenttab = 'fields'; - include_once('tabs.php'); - } - - /// Print any notices - - if (!empty($displaynoticegood)) { - notify($displaynoticegood, 'notifysuccess'); // good (usually green) - } else if (!empty($displaynoticebad)) { - notify($displaynoticebad); // bad (usuually red) - } -} - - /** * Converts a database (module instance) to use the Roles System * @param $data - a data object with the same attributes as a record @@ -1429,5 +1401,111 @@ function data_convert_to_roles($data, $teacherroles=array(), $studentroles=array return true; } +/* + * Returns the best name to show for a preset + */ +function data_preset_name($shortname, $path) { + + /// We are looking inside the preset itself as a first choice, but also in normal data directory + $string = get_string('presetname'.$shortname, 'data', NULL, $path.'/lang/'); + + if (substr($string, 0, 1) == '[') { + return $shortname; + } else { + return $string; + } +} + +/* + * Returns an array of all the available presets + */ +function data_get_available_presets($context) { + global $CFG, $USER; + + $presets = array(); + + if ($dirs = get_list_of_plugins('mod/data/preset')) { + foreach ($dirs as $dir) { + $fulldir = $CFG->dirroot.'/mod/data/preset/'.$dir; + + if (is_directory_a_preset($fulldir)) { + $preset = new object; + $preset->path = $fulldir; + $preset->userid = 0; + $preset->shortname = $dir; + $preset->name = data_preset_name($dir, $fulldir); + if (file_exists($fulldir.'/screenshot.jpg')) { + $preset->screenshot = $CFG->wwwroot.'/mod/data/preset/'.$dir.'/screenshot.jpg'; + } else if (file_exists($fulldir.'/screenshot.png')) { + $preset->screenshot = $CFG->wwwroot.'/mod/data/preset/'.$dir.'/screenshot.png'; + } else if (file_exists($fulldir.'/screenshot.gif')) { + $preset->screenshot = $CFG->wwwroot.'/mod/data/preset/'.$dir.'/screenshot.gif'; + } + $presets[] = $preset; + } + } + } + + if ($userids = get_list_of_plugins('data/preset', '', $CFG->dataroot)) { + foreach ($userids as $userid) { + $fulldir = $CFG->dataroot.'/data/preset/'.$userid; + + if ($userid == 0 || $USER->id == $userid || has_capability('mod/data:viewalluserpresets', $context)) { + + if ($dirs = get_list_of_plugins('data/preset/'.$userid, '', $CFG->dataroot)) { + foreach ($dirs as $dir) { + $fulldir = $CFG->dataroot.'/data/preset/'.$userid.'/'.$dir; + + if (is_directory_a_preset($fulldir)) { + $preset = new object; + $preset->path = $fulldir; + $preset->userid = $userid; + $preset->shortname = $dir; + $preset->name = data_preset_name($dir, $fulldir); + if (file_exists($fulldir.'/screenshot.jpg')) { + $preset->screenshot = $CFG->wwwroot.'/mod/data/preset/'.$dir.'/screenshot.jpg'; + } else if (file_exists($fulldir.'/screenshot.png')) { + $preset->screenshot = $CFG->wwwroot.'/mod/data/preset/'.$dir.'/screenshot.png'; + } else if (file_exists($fulldir.'/screenshot.gif')) { + $preset->screenshot = $CFG->wwwroot.'/mod/data/preset/'.$dir.'/screenshot.gif'; + } + $presets[] = $preset; + } + } + } + } + } + } + + return $presets; +} + + +function data_print_header($course, $cm, $data, $currenttab='') { + + global $CFG, $displaynoticegood, $displaynoticebad; + + $strdata = get_string('modulenameplural','data'); + + print_header_simple($data->name, '', "$strdata -> $data->name", + '', '', true, update_module_button($cm->id, $course->id, get_string('modulename', 'data')), + navmenu($course, $cm)); + + print_heading(format_string($data->name)); + + /// Print the tabs + + if ($currenttab) { + include_once('tabs.php'); + } + + /// Print any notices + + if (!empty($displaynoticegood)) { + notify($displaynoticegood, 'notifysuccess'); // good (usually green) + } else if (!empty($displaynoticebad)) { + notify($displaynoticebad); // bad (usuually red) + } +} -?> \ No newline at end of file +?> diff --git a/mod/data/preset.php b/mod/data/preset.php index 9d4fbad389..223fd13278 100644 --- a/mod/data/preset.php +++ b/mod/data/preset.php @@ -11,10 +11,12 @@ require_once($CFG->libdir.'/uploadlib.php'); require_once($CFG->libdir.'/xmlize.php'); -$id = optional_param('id', 0, PARAM_INT); // course module id -$d = optional_param('d', 0, PARAM_INT); // database activity id -$action = optional_param('action', 'base', PARAM_RAW); // current action -$file = optional_param('file', false, PARAM_PATH); // path of file to upload +$id = optional_param('id', 0, PARAM_INT); // course module id +$d = optional_param('d', 0, PARAM_INT); // database activity id +$action = optional_param('action', 'base', PARAM_RAW); // current action +$userid = optional_param('userid', 0, PARAM_INT); // owner of the preset +$shortname = optional_param('shortname', '', PARAM_FILE); // directory the preset is in +$file = optional_param('file', '', PARAM_FILE); // uploaded file if ($id) { if (! $cm = get_record('course_modules', 'id', $id)) { @@ -40,390 +42,353 @@ if ($id) { error('Parameter missing'); } +if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) { + error('Could not find context'); +} + require_login($course->id); -require_capability('mod/data:managetemplates', get_context_instance(CONTEXT_MODULE, $cm->id)); +require_capability('mod/data:managetemplates', $context); -/* get the list of standard presets found in /mod/data/preset */ -$presets = array(); +if ($userid && ($userid != $USER->id) && !has_capability('mod/data:viewalluserpresets', $context)) { + error('You are not allowed to access presets from other users'); +} +/* Need sesskey security check here for import instruction */ +$sesskey = sesskey(); + +/********************************************************************/ +/* Output */ +data_print_header($course, $cm, $data, 'presets'); -if ($presetdir = opendir($CFG->dirroot.'/mod/data/preset')) { +echo '
'; +switch ($action) { - while ($userdir = readdir($presetdir)) { + /***************** Deleting *****************/ + case 'confirmdelete' : + if (!confirm_sesskey()) { + error("Sesskey Invalid"); + } + $path = data_preset_path($course, $userid, $shortname); + + $strwarning = get_string('deletewarning', 'data').'
'. + data_preset_name($shortname, $path); + + $options = new object; + $options->shortname = $shortname; + $options->userid = $userid; + $options->action = 'delete'; + $options->d = $data->id; + $options->sesskey = sesskey(); + + $optionsno = new object; + $optionsno->d = $data->id; + notice_yesno($strwarning, 'preset.php', 'preset.php', $options, $optionsno, 'post', 'get'); + print_footer($course); + exit; + break; + + case 'delete' : + if (!confirm_sesskey()) { + error('Sesskey Invalid'); + } - $fulluserdir = '/mod/data/preset/'.$userdir; + $presetpath = data_preset_path($course, $userid, $shortname); - if ($userdir == '.' || $userdir == '..') { - continue; + if (!clean_preset($presetpath)) { + error("Error deleting a preset!"); } + @rmdir($presetpath); - /* Global Standard Presets */ - if (is_directory_a_preset($CFG->dirroot.$fulluserdir)) { - $preset = new StdClass; - $preset->path = $fulluserdir; - $preset->name = $userdir; - if (file_exists($fulluserdir.'/screenshot.jpg')) { - $preset->screenshot = $CFG->wwwroot.'/mod/data/preset/'.$userdir.'/screenshot.jpg'; - } - $presets[] = $preset; - unset($preset); + $strdeleted = get_string('deleted', 'data'); + notify("$shortname $strdeleted"); + + break; + + + /***************** Importing *****************/ + case 'importpreset' : + if (!confirm_sesskey()) { + error("Sesskey Invalid"); } - /* User made presets stored in user folders */ - else if (get_record('user', 'id', $userdir)) { - $userdirh = opendir($CFG->dirroot.$fulluserdir); - while ($userpresetdir = readdir($userdirh)) { - $fulluserpresetdir = $fulluserdir.'/'.$userpresetdir; - if ($userpresetdir != '.' && $userpresetdir != '..' && is_directory_a_preset($CFG->dirroot.$fulluserpresetdir)) { - $preset = new StdClass; - $preset->path = $fulluserpresetdir; - $preset->name = $userpresetdir; - $preset->user = $userdir; - if (file_exists($fulluserpresetdir.'/screenshot.jpg')) { - $preset->screenshot = $CFG->wwwroot.'/mod/data/preset/'.$userdir.'/'.$userpresetdir.'/screenshot.jpg'; - } - $presets[] = $preset; - unset($preset); - } - } + $pimporter = new PresetImporter($course, $cm, $data, $userid, $shortname); + $pimporter->import_options(); + + print_footer($course); + exit; + break; + + /* Imports a zip file. */ + case 'importzip' : + if (!confirm_sesskey()) { + error("Sesskey Invalid"); } - } - closedir($presetdir); -} -/* Need sesskey security check here for import instruction */ -$sesskey = sesskey(); + if (!make_upload_directory('temp/data/'.$USER->id)) { + error("Can't Create Directory"); + } -/********************************************************************/ -/* Output */ -data_presets_print_header($course, $cm, $data); + $presetfile = $CFG->dataroot.'/temp/data/'.$USER->id; + clean_preset($presetfile); -echo "
"; -switch ($action) { - /* Main selection menu - default mode also. */ - default: - case 'base': - $strimport = get_string('import'); - $strfromfile = get_string('fromfile', 'data'); - $strchooseorupload = get_string('chooseorupload', 'data'); - $strok = get_string('ok'); - $strusestandard = get_string('usestandard', 'data'); - $strchoose = get_string('choose'); - $strexport = get_string('export', 'data'); - $strexportaszip = get_string('exportaszip', 'data'); - $strsaveaspreset = get_string('saveaspreset', 'data'); - $strdelete = get_string('delete'); - - echo ""; - echo ""; - echo ""; - - echo ""; - - echo ""; - echo "

$strimport

"; - helpbutton('importfromfile', '', 'data'); - echo " $strfromfile:
"; - echo ""; - echo "
"; - helpbutton('usepreset', '', 'data'); - echo " $strusestandard: "; - echo ""; - foreach ($presets as $id => $preset) { - echo ""; - echo "path\">"; - echo ""; - echo ""; - echo ""; - echo ""; - echo ""; - } - echo "
"; - if ($preset->screenshot) { - echo "$preset->screenshot"; - } - echo "$preset->name"; - if ($preset->user) { - $user = get_record('user', 'id', $preset->user); - echo " by $user->firstname $user->lastname"; - } - echo ""; - if ($preset->user == $USER->id || has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM, SITEID))) { - echo "
"; - echo ""; - echo ""; - echo ""; - echo ""; - echo "name\" />"; - echo "
"; - } - echo "

$strexport

"; - helpbutton('exportzip', '', 'data'); - echo " "; - echo ""; - echo ""; - echo "
"; - - echo "
"; - helpbutton('savepreset', '', 'data'); - echo " "; - echo ""; - echo ""; - echo ""; - echo "
"; - - echo "
"; - break; - - - - /***************** Deleting *****************/ - case 'confirmdelete' : - if (!confirm_sesskey()) { - error("Sesskey Invalid"); - } - - $deletename = required_param('deletename', PARAM_RAW); - $deleteid = required_param('deleteid', PARAM_INT); - - $strwarning = get_string('deletewarning', 'data'); - $strdelete = get_string('delete'); - notify($strwarning); - echo "
"; - echo ""; - echo ""; - echo ""; - echo ""; - echo ""; - echo "
"; - break; - - case 'delete' : - if (!confirm_sesskey()) { - error('Sesskey Invalid'); - } - - $deletename = required_param('deletename', PARAM_RAW); - $deleteid = required_param('deleteid', PARAM_INT); - - if (!empty($presets[$deleteid])) { - if ($presets[$deleteid]->name == $deletename) { - if (!clean_preset($CFG->dirroot.$presets[$deleteid]->path)) error("Error deleting"); - } - rmdir($CFG->dirroot.$presets[$deleteid]->path); - } - else { - error('Invalid delete'); - } - - $strdelete = get_string('deleted', 'data'); - notify("$deletename $strdeleted"); - - break; - - - - /***************** Importing *****************/ - case 'importpreset' : - if (!confirm_sesskey()) { - error("Sesskey Invalid"); - } - - $pimporter = new PresetImporter($course, $cm, $data, $CFG->dirroot.$file); - $pimporter->import_options(); - break; - - /* Imports a zip file. */ - case 'importzip' : - if (!confirm_sesskey()) { - error("Sesskey Invalid"); - } - - if (!make_upload_directory('temp/data/'.$USER->id)) { - error("Can't Create Directory"); - } - - $presetfile = $CFG->dataroot."/temp/data/".$USER->id; - clean_preset($presetfile); - - if (!unzip_file($CFG->dataroot."/$course->id/$file", - $presetfile, false)) - error("Can't unzip file"); - - $pimporter = new PresetImporter($course, $cm, $data, $presetfile); - $pimporter->import_options(); - break; - - case 'finishimport': - if (!confirm_sesskey()) { - error('Sesskey Invalid'); - } - - $pimporter = new PresetImporter($course, $cm, $data, $file); - $pimporter->import(); - - $strimportsuccess = get_string('importsuccess', 'data'); - $straddentries = get_string('addentries', 'data'); - $strtodatabase = get_string('todatabase', 'data'); - if (!get_records('data_records', 'dataid', $data->id)) { - notify("$strimportsuccess $straddentries $strtodatabase", 'notifysuccess'); - } - else { - notify("$strimportsuccess", 'notifysuccess'); - } - break; - - /* Exports as a zip file ready for download. */ - case 'export': - $file = data_presets_export($course, $cm, $data); - echo get_string('exportedtozip', 'data')."
"; - $perminantfile = $CFG->dataroot."/$course->id/moddata/data/$data->id/preset.zip"; - @unlink($perminantfile); - /* is this created elsewhere? sometimes its not present... */ - make_upload_directory("$course->id/moddata/data/$data->id"); - - /* now just move the zip into this folder to allow a nice download */ - if (!rename($file, $perminantfile)) error("Can't move zip"); - echo "".get_string('download', 'data').""; - break; - - - - /***************** Exporting *****************/ - case 'save1': - if (!confirm_sesskey()) { - error("Sesskey Invalid"); - } - - $strcontinue = get_string('continue'); - $strwarning = get_string('presetwarning', 'data'); - - echo "
"; - echo "

$strwarning

"; - echo "
"; - echo "Name: name\" />"; - echo ""; - echo ""; - echo ""; - echo "
"; - break; - - case 'save2': - if (!confirm_sesskey()) { - error("Sesskey Invalid"); - } - - $strcontinue = get_string('continue'); - $stroverwrite = get_string('overwrite'); - - $name = optional_param('name', $data->name, PARAM_FILE); - - if (is_directory_a_preset("$CFG->dirroot/mod/data/preset/$USER->id/$name")) { - notify("Preset already exists: Pick another name or overwrite"); - - echo "
"; - echo "
"; - echo "New name: "; - echo ""; - echo ""; - echo ""; - echo "
"; - - echo "
"; - echo ""; - echo ""; - echo ""; - echo ""; - echo "
"; - echo "
"; - break; - } - - case 'save3': - if (!confirm_sesskey()) { - error("Sesskey Invalid"); - } - - $name = optional_param('name', $data->name, PARAM_FILE); - $presetdirectory = "$CFG->dirroot/mod/data/preset/$USER->id/$name"; - - if (!is_dir($presetdirectory)) { - @mkdir("$CFG->dirroot/mod/data/preset/$USER->id"); - mkdir($presetdirectory); - } - else { - clean_preset($presetdirectory); - } - - $file = data_presets_export($course, $cm, $data); - if (!unzip_file($file, $presetdirectory, false)) error("Can't unzip to the preset directory"); - notify(get_string('savesuccess', 'data'), 'notifysuccess'); - break; + if (!unzip_file($CFG->dataroot."/$course->id/$file", $presetfile, false)) { + error("Can't unzip file"); + } -} -echo "
"; -print_footer($course); + $pimporter = new PresetImporter($course, $cm, $data, -$USER->id, $shortname); + $pimporter->import_options(); + print_footer($course); + exit; + break; -function is_directory_a_preset($directory) { - $directory = rtrim($directory, '/\\') . '/'; - if (file_exists($directory.'singletemplate.html') && - file_exists($directory.'listtemplate.html') && - file_exists($directory.'listtemplateheader.html') && - file_exists($directory.'listtemplatefooter.html') && - file_exists($directory.'addtemplate.html') && - file_exists($directory.'rsstemplate.html') && - file_exists($directory.'rsstitletemplate.html') && - file_exists($directory.'csstemplate.css') && - file_exists($directory.'jstemplate.js') && - file_exists($directory.'preset.xml')) return true; - else return false; -} + case 'finishimport': + if (!confirm_sesskey()) { + error('Sesskey Invalid'); + } -function data_presets_print_header($course, $cm, $data, $showtabs=true) { + $pimporter = new PresetImporter($course, $cm, $data, $userid, $shortname); + $pimporter->import(); - global $CFG, $displaynoticegood, $displaynoticebad; + $strimportsuccess = get_string('importsuccess', 'data'); + $straddentries = get_string('addentries', 'data'); + $strtodatabase = get_string('todatabase', 'data'); + if (!get_records('data_records', 'dataid', $data->id)) { + notify("$strimportsuccess $straddentries $strtodatabase", 'notifysuccess'); + } else { + notify("$strimportsuccess", 'notifysuccess'); + } + break; - $strdata = get_string('modulenameplural','data'); + /* Exports as a zip file ready for download. */ + case 'export': + $file = data_presets_export($course, $cm, $data); + echo get_string('exportedtozip', 'data')."
"; + $perminantfile = $CFG->dataroot."/$course->id/moddata/data/$data->id/preset.zip"; + @unlink($perminantfile); + /* is this created elsewhere? sometimes its not present... */ + make_upload_directory("$course->id/moddata/data/$data->id"); - print_header_simple($data->name, '', "$strdata -> $data->name", - '', '', true, '', navmenu($course, $cm)); + /* now just move the zip into this folder to allow a nice download */ + if (!rename($file, $perminantfile)) error("Can't move zip"); + echo "".get_string('download', 'data').""; + break; - print_heading(format_string($data->name)); - /// Print the tabs - if ($showtabs) { - $currenttab = 'presets'; - include_once('tabs.php'); - } + /***************** Exporting *****************/ + case 'save1': + if (!confirm_sesskey()) { + error("Sesskey Invalid"); + } + + $strcontinue = get_string('continue'); + $strwarning = get_string('presetinfo', 'data'); - /// Print any notices + echo "
"; + echo "

$strwarning

"; + echo "
"; + echo "Name: name\" />"; + echo ""; + echo ""; + echo ""; + echo "
"; + print_footer($course); + exit; + break; - if (!empty($displaynoticegood)) { - notify($displaynoticegood, 'notifysuccess'); // good (usually green) - } else if (!empty($displaynoticebad)) { - notify($displaynoticebad); // bad (usuually red) + case 'save2': + if (!confirm_sesskey()) { + error("Sesskey Invalid"); + } + + $strcontinue = get_string('continue'); + $stroverwrite = get_string('overwrite'); + + $name = optional_param('name', $data->name, PARAM_FILE); + + if (is_directory_a_preset("$CFG->dataroot/data/preset/$USER->id/$name")) { + notify("Preset already exists: Pick another name or overwrite"); + + echo "
"; + echo "
"; + echo "New name: "; + echo ""; + echo ""; + echo ""; + echo "
"; + + echo "
"; + echo ""; + echo ""; + echo ""; + echo ""; + echo "
"; + echo "
"; + print_footer($course); + exit; + break; + } + + case 'save3': + if (!confirm_sesskey()) { + error("Sesskey Invalid"); + } + + $name = optional_param('name', $data->name, PARAM_FILE); + $presetdirectory = "/data/preset/$USER->id/$name"; + + make_upload_directory($presetdirectory); + clean_preset($CFG->dataroot.$presetdirectory); + + $file = data_presets_export($course, $cm, $data); + if (!unzip_file($file, $CFG->dataroot.$presetdirectory, false)) { + error("Can't unzip to the preset directory"); + } + notify(get_string('savesuccess', 'data'), 'notifysuccess'); + break; +} + +$presets = data_get_available_presets($context); + +$strimport = get_string('import'); +$strfromfile = get_string('fromfile', 'data'); +$strchooseorupload = get_string('chooseorupload', 'data'); +$strok = get_string('ok'); +$strusestandard = get_string('usestandard', 'data'); +$strchoose = get_string('choose'); +$strexport = get_string('export', 'data'); +$strexportaszip = get_string('exportaszip', 'data'); +$strsaveaspreset = get_string('saveaspreset', 'data'); +$strdelete = get_string('delete'); + +echo ''; +echo ''; + +echo ''; + +echo ''; +echo '
'; +echo '

'.$strexport.'

'; +echo '
'; + +echo ''; +echo '
'; +$options = new object; +$options->action = 'export'; +$options->d = $data->id; +$options->sesskey = sesskey(); +helpbutton('exportzip', '', 'data'); +echo ''; +print_single_button('preset.php', $options, $strexportaszip, 'post'); + +echo '
'; +$options = new object; +$options->action = 'save1'; +$options->d = $data->id; +$options->sesskey = sesskey(); +helpbutton('savepreset', '', 'data'); +echo ''; +print_single_button('preset.php', $options, $strsaveaspreset, 'post'); +echo '
'; + +echo '
'; +echo '

'.$strimport.'

'; +echo '
'; +helpbutton('importfromfile', '', 'data'); +echo $strfromfile.':'; +echo ''; +echo '
'; +echo ''; +echo ''; +echo ''; +echo ''; +echo ''; +echo '
'; +echo '
'; +echo ''; +helpbutton('usepreset', '', 'data'); +echo $strusestandard.':'; +echo ''; + +echo ''; +foreach ($presets as $id => $preset) { + echo ''; + echo ''; } +echo '
'; + if (!empty($preset->screenshot)) { + echo ''; } + echo ''.$preset->name; + if (!empty($preset->userid)) { + $user = get_record('user', 'id', $preset->userid); + echo ' ('.fullname($user, has_capability('moodle/site:viewfullnames', $context)).')'; + } + echo ''; + $options = new object; + $options->shortname = $preset->shortname; + $options->userid = $preset->userid; + $options->action = 'importpreset'; + $options->d = $data->id; + $options->sesskey = sesskey(); + print_single_button('preset.php', $options, $strchoose, 'post'); + echo ''; + if ($preset->userid > 0 && + ($preset->userid == $USER->id || has_capability('mod/data:manageuserpresets', $context))) { + $options = new object; + $options->shortname = $preset->shortname; + $options->userid = $preset->userid; + $options->action = 'confirmdelete'; + $options->d = $data->id; + $options->sesskey = sesskey(); + print_single_button('preset.php', $options, $strdelete, 'post'); + } + echo '
'; +echo '
'; -function clean_preset($folder) { - if (unlink($folder.'/singletemplate.html') && - unlink($folder.'/listtemplate.html') && - unlink($folder.'/listtemplateheader.html') && - unlink($folder.'/listtemplatefooter.html') && - unlink($folder.'/addtemplate.html') && - unlink($folder.'/rsstemplate.html') && - unlink($folder.'/rsstitletemplate.html') && - unlink($folder.'/csstemplate.css') && - unlink($folder.'/jstemplate.js') && - unlink($folder.'/preset.xml')) return true; +print_footer($course); + + + + + +function is_directory_a_preset($directory) { + $directory = rtrim($directory, '/\\') . '/'; + if (file_exists($directory.'singletemplate.html') && + file_exists($directory.'listtemplate.html') && + file_exists($directory.'listtemplateheader.html') && + file_exists($directory.'listtemplatefooter.html') && + file_exists($directory.'addtemplate.html') && + file_exists($directory.'rsstemplate.html') && + file_exists($directory.'rsstitletemplate.html') && + file_exists($directory.'csstemplate.css') && + file_exists($directory.'jstemplate.js') && + file_exists($directory.'preset.xml')) return true; else return false; } + +function clean_preset($folder) { + if (@unlink($folder.'/singletemplate.html') && + @unlink($folder.'/listtemplate.html') && + @unlink($folder.'/listtemplateheader.html') && + @unlink($folder.'/listtemplatefooter.html') && + @unlink($folder.'/addtemplate.html') && + @unlink($folder.'/rsstemplate.html') && + @unlink($folder.'/rsstitletemplate.html') && + @unlink($folder.'/csstemplate.css') && + @unlink($folder.'/jstemplate.js') && + @unlink($folder.'/preset.xml')) { + return true; + } + return false; +} + + function data_presets_export($course, $cm, $data) { global $CFG; /* Info Collected. Now need to make files in moodledata/temp */ @@ -464,28 +429,28 @@ function data_presets_export($course, $cm, $data) { $presetxml = "\n\n"; /* Database settings first. Name not included? */ - $settingssaved = array('intro', 'comments', 'ratings', 'participants', - 'requiredentries', 'requiredentriestoview', 'maxentries', - 'rssarticles', 'approval', 'scale', 'assessed', 'assessedpublic', - 'defaultsort', 'defaultsortdir', 'editany'); + $settingssaved = array('intro', 'comments', + 'requiredentries', 'requiredentriestoview', 'maxentries', + 'rssarticles', 'approval', 'scale', 'assessed', + 'defaultsort', 'defaultsortdir', 'editany'); $presetxml .= "\n"; foreach ($settingssaved as $setting) { - $presetxml .= "<$setting>{$data->$setting}\n"; + $presetxml .= "<$setting>{$data->$setting}\n"; } $presetxml .= "\n\n"; /* Now for the fields. Grabs all settings that are non-empty */ if (!empty($fields)) { - foreach ($fields as $field) { - $presetxml .= "\n"; - foreach ($field as $key => $value) { - if ($value != '' && $key != 'id' && $key != 'dataid') { - $presetxml .= "<$key>$value\n"; - } + foreach ($fields as $field) { + $presetxml .= "\n"; + foreach ($field as $key => $value) { + if ($value != '' && $key != 'id' && $key != 'dataid') { + $presetxml .= "<$key>$value\n"; + } + } + $presetxml .= "\n\n"; } - $presetxml .= "\n\n"; - } } $presetxml .= ""; @@ -498,16 +463,16 @@ function data_presets_export($course, $cm, $data) { } $filelist = array( - "singletemplate.html", - "listtemplate.html", - "listtemplateheader.html", - "listtemplatefooter.html", - "addtemplate.html", - "rsstemplate.html", - "rsstitletemplate.html", - "csstemplate.css", - "jstemplate.js", - "preset.xml"); + 'singletemplate.html', + 'listtemplate.html', + 'listtemplateheader.html', + 'listtemplatefooter.html', + 'addtemplate.html', + 'rsstemplate.html', + 'rsstitletemplate.html', + 'csstemplate.css', + 'jstemplate.js', + 'preset.xml'); foreach ($filelist as $key => $file) { $filelist[$key] = $tempfolder.'/'.$filelist[$key]; @@ -523,16 +488,16 @@ function data_presets_export($course, $cm, $data) { class PresetImporter { - function PresetImporter($course, $cm, $data, $folder) { + function PresetImporter($course, $cm, $data, $userid, $shortname) { global $CFG; $this->course = $course; $this->cm = $cm; $this->data = $data; - $this->folder = $folder; - $this->postfolder = $folder; + $this->userid = $userid; + $this->shortname = $shortname; + $this->folder = data_preset_path($course, $userid, $shortname); } - function get_settings() { global $CFG; @@ -601,11 +566,12 @@ class PresetImporter { list($settings, $newfields, $currentfields) = $this->get_settings(); - echo "
"; - echo ""; - echo ""; - echo ""; - echo "postfolder\" />"; + echo '
'; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; if ($currentfields != array() && $newfields != array()) { echo "

$strfieldmappings "; @@ -623,9 +589,9 @@ class PresetImporter { $selected=true; } else { - echo ""; - } - } + echo ""; + } + } } if ($selected) @@ -659,7 +625,6 @@ class PresetImporter { if (array_key_exists($cid, $preservedfields)) error("Not an injective map"); else $preservedfields[$cid] = true; - } foreach ($newfields as $nid => $newfield) { @@ -706,16 +671,33 @@ class PresetImporter { } delete_records('data_content', 'fieldid', $id); delete_records('data_fields', 'id', $id); - } } } data_update_instance(addslashes_object($settings)); - + if (strstr($this->folder, "/temp/")) clean_preset($this->folder); /* Removes the temporary files */ return true; } } +function data_preset_path($course, $userid, $shortname) { + global $USER, $CFG; + + $context = get_context_instance(CONTEXT_COURSE, $course->id); + + $userid = (int)$userid; + + if ($userid > 0 && ($userid == $USER->id || has_capability('mod/data:viewalluserpresets', $context))) { + return $CFG->dataroot.'/data/preset/'.$userid.'/'.$shortname; + } else if ($userid == 0) { + return $CFG->dirroot.'/mod/data/preset/'.$shortname; + } else if ($userid < 0) { + return $CFG->dataroot.'/temp/data/'.-$userid.'/'.$shortname; + } + + return 'Does it disturb you that this code will never run?'; +} + ?> diff --git a/mod/data/styles.php b/mod/data/styles.php index 3eeaa1d7e8..88997fd356 100644 --- a/mod/data/styles.php +++ b/mod/data/styles.php @@ -13,3 +13,7 @@ .mod-data-field .fielddescription { width:300px; } + +.presetcontrols form { + display: inline; +} diff --git a/mod/data/templates.php b/mod/data/templates.php index f15e0b8e44..71f4e1854f 100755 --- a/mod/data/templates.php +++ b/mod/data/templates.php @@ -78,7 +78,8 @@ $bodytag .= '" '; print_header_simple($data->name, '', "$strdata -> $data->name", - '', '', true, '', navmenu($course, $cm), '', $bodytag); + '', '', true, update_module_button($cm->id, $course->id, get_string('modulename', 'data')), + navmenu($course, $cm), '', $bodytag); print_heading(format_string($data->name)); diff --git a/mod/data/version.php b/mod/data/version.php index b045c58a23..0fecce89b5 100644 --- a/mod/data/version.php +++ b/mod/data/version.php @@ -5,7 +5,7 @@ // This fragment is called by /admin/index.php //////////////////////////////////////////////////////////////////////////////// -$module->version = 2006092302; +$module->version = 2006100200; $module->requires = 2006080900; // Requires this Moodle version $module->cron = 60;