From 901dd2fb4cb7b82baa1a255b8910e38daf157218 Mon Sep 17 00:00:00 2001 From: moodler Date: Tue, 15 Aug 2006 08:42:06 +0000 Subject: [PATCH] Merged new "Database preset" support from Tom Flannaghan. Still needs some tidy-up work but it's working. --- lang/en_utf8/data.php | 23 + lang/en_utf8/help/data/exportzip.html | 4 + lang/en_utf8/help/data/fieldmappings.html | 11 + lang/en_utf8/help/data/importfromfile.html | 3 + lang/en_utf8/help/data/savepreset.html | 4 + lang/en_utf8/help/data/usepreset.html | 4 + mod/data/field.php | 28 +- mod/data/lib.php | 28 + mod/data/preset.php | 735 +++++++++++++++++++++ mod/data/tabs.php | 9 +- 10 files changed, 818 insertions(+), 31 deletions(-) create mode 100644 lang/en_utf8/help/data/exportzip.html create mode 100644 lang/en_utf8/help/data/fieldmappings.html create mode 100644 lang/en_utf8/help/data/importfromfile.html create mode 100644 lang/en_utf8/help/data/savepreset.html create mode 100644 lang/en_utf8/help/data/usepreset.html create mode 100644 mod/data/preset.php diff --git a/lang/en_utf8/data.php b/lang/en_utf8/data.php index 10f08d6969..9ebe302f04 100644 --- a/lang/en_utf8/data.php +++ b/lang/en_utf8/data.php @@ -137,6 +137,7 @@ $string['optionalfilename'] = 'Filename (optional)'; $string['pagesize'] = 'Entries per page'; $string['participants'] = 'Participants'; $string['picture'] = 'Picture'; +$string['pleaseaddsome'] = 'Please create some below or choose a predefined set to get started.'; $string['radiobutton'] = 'Radio buttons'; $string['rate'] = 'Rate'; $string['rating'] = 'Rating'; @@ -182,4 +183,26 @@ $string['url'] = 'Url'; $string['viewfromdate'] = 'Viewable from'; $string['viewtodate'] = 'Viewable to'; +$string['presets'] = 'Presets'; +$string['exportaszip'] = 'Export as zip'; +$string['exportedtozip'] = 'Exported to temporary zip...'; +$string['saveaspreset'] = 'Save as preset'; +$string['export'] = 'Export'; +$string['fromfile'] = 'from zip file'; +$string['usestandard'] = 'use a preset'; +$string['chooseorupload'] = 'Choose file'; +$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['deletewarning'] = 'Are you sure you want to delete this preset?'; +$string['importsuccess'] = 'The preset has been successfully applied.'; +$string['addentries'] = 'Add entries'; +$string['todatabase'] = 'to this database.'; +$string['savesuccess'] = 'Saved successfully. Your preset will now be available across the site.'; +$string['overwrite'] = 'Overwrite'; + +$string['download'] = 'Download'; + ?> diff --git a/lang/en_utf8/help/data/exportzip.html b/lang/en_utf8/help/data/exportzip.html new file mode 100644 index 0000000000..e85f31efc6 --- /dev/null +++ b/lang/en_utf8/help/data/exportzip.html @@ -0,0 +1,4 @@ +

Exporting as Zip

+

This allows you to download the templates to your own computer, +which can be uploaded later into a different database with the Import +from Zip feature.

diff --git a/lang/en_utf8/help/data/fieldmappings.html b/lang/en_utf8/help/data/fieldmappings.html new file mode 100644 index 0000000000..b7d489d37f --- /dev/null +++ b/lang/en_utf8/help/data/fieldmappings.html @@ -0,0 +1,11 @@ +

Field Mappings

+

This menu allows you to keep the data from the existing +database. To preserve the data in a field, you must map it to a new +field, where the data will appear. Any field can also be left blank, +with no infomation copied into it. Any old field not mapped to a new +one will be lost and all it's data removed.

+

+You can only map fields of the same type, so each dropdown will have +different fields in it. Also, you must be careful not to try and map +one old field to more than one new field. +

diff --git a/lang/en_utf8/help/data/importfromfile.html b/lang/en_utf8/help/data/importfromfile.html new file mode 100644 index 0000000000..2b7575005a --- /dev/null +++ b/lang/en_utf8/help/data/importfromfile.html @@ -0,0 +1,3 @@ +

Importing Presets from Zip

+

Use this to upload presets that have been saved to your computer +with the export feature.

diff --git a/lang/en_utf8/help/data/savepreset.html b/lang/en_utf8/help/data/savepreset.html new file mode 100644 index 0000000000..0163df5ea3 --- /dev/null +++ b/lang/en_utf8/help/data/savepreset.html @@ -0,0 +1,4 @@ +

Save as Preset

+

This publishes the current templates as a preset which anyone on +the site can view and use. It will appear in the preset list. You will +be able to remove it at any time.

diff --git a/lang/en_utf8/help/data/usepreset.html b/lang/en_utf8/help/data/usepreset.html new file mode 100644 index 0000000000..d7628f78d1 --- /dev/null +++ b/lang/en_utf8/help/data/usepreset.html @@ -0,0 +1,4 @@ +

Use Preset

+

Uses a template available to the whole site.

+

Also, if you've added the preset to the library using the 'Save as +Preset' feature then you can delete it.

diff --git a/mod/data/field.php b/mod/data/field.php index 7490b25f8f..a8b492da46 100755 --- a/mod/data/field.php +++ b/mod/data/field.php @@ -259,6 +259,7 @@ if (!record_exists('data_fields','dataid',$data->id)) { notify(get_string('nofieldindatabase','data')); // nothing in database + notify(get_string('pleaseaddsome','data', 'preset.php?id='.$cm->id)); // link to presets } else { //else print quiz style list of fields @@ -337,31 +338,4 @@ print_footer($course); - 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) - } - } - ?> diff --git a/mod/data/lib.php b/mod/data/lib.php index 49d8836936..3df1f8d3cc 100755 --- a/mod/data/lib.php +++ b/mod/data/lib.php @@ -1263,4 +1263,32 @@ function data_get_cm($data) { return get_record_sql($SQL); } + +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) + } +} + ?> diff --git a/mod/data/preset.php b/mod/data/preset.php new file mode 100644 index 0000000000..24902a3da4 --- /dev/null +++ b/mod/data/preset.php @@ -0,0 +1,735 @@ +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 + +if ($id) { + if (! $cm = get_record('course_modules', 'id', $id)) { + error('Course Module ID Incorrect'); + } + if (! $course = get_record('course', 'id', $cm->course)) { + error('Course is misconfigured'); + } + if (! $data = get_record('data', 'id', $cm->instance)) { + error('Module Incorrect'); + } +} else if ($d) { + if (! $data = get_record('data', 'id', $d)) { + error('Database ID Incorrect'); + } + if (! $course = get_record('course', 'id', $data->course)) { + error('Course is misconfigured'); + } + if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) { + error('Course Module ID was incorrect'); + } +} else { + error('Parameter missing'); +} + +require_login($course->id); + +if (!isteacher($course->id)) { + error('Must be a teacher to Import Database'); +} + + +/* get the list of standard presets found in /mod/data/preset */ +$presets = array(); + + +if ($presetdir = opendir($CFG->dirroot.'/mod/data/preset')) { + + while ($userdir = readdir($presetdir)) { + + $fulluserdir = '/mod/data/preset/'.$userdir; + + if ($userdir == '.' || $userdir == '..') { + continue; + } + + /* 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); + } + + /* 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); + } + } + } + } + closedir($presetdir); +} + +/* Need sesskey security check here for import instruction */ +$sesskey = sesskey(); + +/********************************************************************/ +/* Output */ +data_presets_print_header($course, $cm, $data); + +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 || isadmin()) { + 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 'beginimport' : + if (!confirm_sesskey()) { + error("Sesskey Invalid"); + } + + $pimporter = new PresetImporter($course, $cm, $data, $file); + $pimporter->import_options(); + break; + + /* Imports a zip file. */ + case 'importzip' : + if (!confirm_sesskey()) { + error("Sesskey Invalid"); + } + + if (!unzip_file($CFG->dataroot."/$course->id/$file", $CFG->dataroot."/temp/data/".$USER->id, false)) + error("Can't unzip file"); + $presetfile = $CFG->dataroot."/temp/data/".$USER->id; + + $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; + +} +echo "
"; +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.'preset.xml')) return true; + else return false; +} + +function data_presets_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 = 'presets'; + 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) + } +} + + +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.'/preset.xml')) return true; + else return false; +} + + +function data_presets_export($course, $cm, $data) { + global $CFG; + /* Info Collected. Now need to make files in moodledata/temp */ + $tempfolder = $CFG->dataroot.'/temp'; + $singletemplate = fopen($tempfolder.'/singletemplate.html', 'w'); + $listtemplate = fopen($tempfolder.'/listtemplate.html', 'w'); + $listtemplateheader = fopen($tempfolder.'/listtemplateheader.html', 'w'); + $listtemplatefooter = fopen($tempfolder.'/listtemplatefooter.html', 'w'); + $addtemplate = fopen($tempfolder.'/addtemplate.html', 'w'); + $rsstemplate = fopen($tempfolder.'/rsstemplate.html', 'w'); + $rsstitletemplate = fopen($tempfolder.'/rsstitletemplate.html', 'w'); + $csstemplate = fopen($tempfolder.'/csstemplate.css', 'w'); + + fwrite($singletemplate, $data->singletemplate); + fwrite($listtemplate, $data->listtemplate); + fwrite($listtemplateheader, $data->listtemplateheader); + fwrite($listtemplatefooter, $data->listtemplatefooter); + fwrite($addtemplate, $data->addtemplate); + fwrite($rsstemplate, $data->rsstemplate); + fwrite($rsstitletemplate, $data->rsstitletemplate); + fwrite($csstemplate, $data->csstemplate); + + fclose($singletemplate); + fclose($listtemplate); + fclose($listtemplateheader); + fclose($listtemplatefooter); + fclose($addtemplate); + fclose($rsstemplate); + fclose($rsstitletemplate); + fclose($csstemplate); + + /* All the display data is now done. Now assemble preset.xml */ + $fields = get_records('data_fields', 'dataid', $data->id); + $presetfile = fopen($tempfolder.'/preset.xml', 'w'); + $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'); + + $presetxml .= "\n"; + foreach ($settingssaved as $setting) { + $presetxml .= "<$setting>{$data->$setting}\n"; + } + $presetxml .= "\n\n"; + + /* Now for the fields. Grabs all settings that are non-empty */ + 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 .= ""; + fwrite($presetfile, $presetxml); + fclose($presetfile); + + /* Check all is well */ + if (!is_directory_a_preset($tempfolder)) { + error("Not all files generated!"); + } + + $filelist = array( + "singletemplate.html", + "listtemplate.html", + "listtemplateheader.html", + "listtemplatefooter.html", + "addtemplate.html", + "rsstemplate.html", + "rsstitletemplate.html", + "csstemplate.css", + "preset.xml"); + + foreach ($filelist as $key => $file) { + $filelist[$key] = $tempfolder.'/'.$filelist[$key]; + } + + @unlink($tempfolder.'/export.zip'); + $status = zip_files($filelist, $tempfolder.'/export.zip'); + + /* made the zip... now return the filename for storage.*/ + return $tempfolder.'/export.zip'; +} + + +function data_presets_import_zip($course, $cm, $data, $file) { + global $CFG; + + /* + * Now need to move file to temp directory for unzipping. + */ + $tempfolder = $CFG->dataroot.'/temp'; + + if (!file_exists($file)) { + error('No such file '.$file); + } + + if (file_exists($tempfolder.'/template.zip')) { + unlink($tempfolder.'/template.zip'); + } + + if (!copy($file, $tempfolder.'/template.zip')) { + error("Can't copy file"); + } + + /* Unzip. */ + clean_preset($tempfolder); + if (!unzip_file($tempfolder.'/template.zip', '', false)) { + error("Can't unzip file"); + } + + return data_presets_import_options($course, $cm, $data, $tempfolder); +} + +class PresetImporter { + function PresetImporter($course, $cm, $data, $folder) { + global $CFG; + $this->course = $course; + $this->cm = $cm; + $this->data = $data; + $this->folder = $CFG->dirroot.$folder; + $this->postfolder = $folder; + } + + + function get_settings() { + global $CFG; + + if (!is_directory_a_preset($this->folder)) { + error("$this->folder Not a preset"); + } + + /* Grab XML */ + $presetxml = file_get_contents($this->folder.'/preset.xml'); + $parsedxml = xmlize($presetxml); + + /* First, do settings. Put in user friendly array. */ + $settingsarray = $parsedxml['preset']['#']['settings'][0]['#']; + $settings = new StdClass(); + + foreach ($settingsarray as $setting => $value) { + $settings->$setting = $value[0]['#']; + } + + /* Now work out fields to user friendly array */ + $fieldsarray = $parsedxml['preset']['#']['field']; + $fields = array(); + foreach ($fieldsarray as $field) { + $f = new StdClass(); + foreach ($field['#'] as $param => $value) { + $f->$param = $value[0]['#']; + } + $f->dataid = $this->data->id; + $fields[] = $f; + } + + /* Now add the HTML templates to the settings array so we can update d */ + $settings->singletemplate = file_get_contents($this->folder."/singletemplate.html"); + $settings->listtemplate = file_get_contents($this->folder."/listtemplate.html"); + $settings->listtemplateheader = file_get_contents($this->folder."/listtemplateheader.html"); + $settings->listtemplatefooter = file_get_contents($this->folder."/listtemplatefooter.html"); + $settings->addtemplate = file_get_contents($this->folder."/addtemplate.html"); + $settings->rsstemplate = file_get_contents($this->folder."/rsstemplate.html"); + $settings->rsstitletemplate = file_get_contents($this->folder."/rsstitletemplate.html"); + $settings->csstemplate = file_get_contents($this->folder."/csstemplate.css"); + + $settings->instance = $this->data->id; + + /* Now we look at the current structure (if any) to work out whether we need to clear db + or save the data */ + $currentfields = array(); + $currentfields = get_records('data_fields', 'dataid', $this->data->id); + + return array($settings, $fields, $currentfields); + } + + function import_options() { + if (!confirm_sesskey()) { + error("Sesskey Invalid"); + } + + $strblank = get_string('blank', 'data'); + $strnofields = get_string('nofields', 'data'); + $strcontinue = get_string("continue"); + $sesskey = sesskey(); + $strwarning = get_string('mappingwarning', 'data'); + $strfieldmappings = get_string('fieldmappings', 'data'); + $strnew = get_string("new"); + $strold = get_string("old"); + + list($settings, $newfields, $currentfields) = $this->get_settings(); + + echo "
"; + echo ""; + echo ""; + echo ""; + echo "postfolder\" />"; + + if ($currentfields != array() && $newfields != array()) { + echo "

$strfieldmappings "; + echo helpbutton('fieldmappings', '', 'data'); + echo "

"; + + foreach ($newfields as $nid => $newfield) { + echo ""; + echo ""; + } + echo "
$newfield->name
"; + echo "

$strwarning

"; + } + else if ($newfields == array()) { + error("New preset has no defined fields!"); + } + echo "
"; + + } + + function import() { + global $CFG; + + list($settings, $newfields, $currentfields) = $this->get_settings(); + + $preservedfields = array(); + + /* Maps fields and makes new ones */ + if ($newfields != array()) { + /* We require an injective mapping, and need to know what to protect */ + foreach ($newfields as $nid => $newfield) { + $cid = optional_param("field_$nid", -1, PARAM_INT); + if ($cid == -1) continue; + + if (array_key_exists($cid, $preservedfields)) error("Not an injective map"); + else $preservedfields[$cid] = true; + + } + + foreach ($newfields as $nid => $newfield) { + $cid = optional_param("field_$nid", -1, PARAM_INT); + + /* A mapping. Just need to change field params. Data kept. */ + if ($cid != -1) { + $fieldobject = data_get_field_from_id($currentfields[$cid]->id, $this->data); + foreach ($newfield as $param => $value) { + if ($param != "id") { + $fieldobject->field->$param = $value; + } + } + unset($fieldobject->field->similarfield); + $fieldobject->update_field(); + unset($fieldobject); + } + /* Make a new field */ + else { + include_once("field/$newfield->type/field.class.php"); + + $classname = 'data_field_'.$newfield->type; + $fieldclass = new $classname($newfield, $this->data); + $fieldclass->insert_field(); + unset($fieldclass); + } + } + } + + /* Get rid of all old unused data */ + if ($preservedfields != array()) { + foreach ($currentfields as $cid => $currentfield) { + if (!array_key_exists($cid, $preservedfields)) { + /* Data not used anymore so wipe! */ + print "Deleting field $currentfield->name
"; + $id = $currentfield->id; + + if ($content = get_records('data_content', 'fieldid', $id)) { + foreach ($content as $item) { + delete_records('data_ratings', 'recordid', $item->recordid); + delete_records('data_comments', 'recordid', $item->recordid); + delete_records('data_records', 'id', $item->recordid); + } + } + delete_records('data_content', 'fieldid', $id); + delete_records('data_fields', 'id', $id); + + } + } + } + + data_update_instance($settings); + + if (strstr($this->folder, "/temp/")) clean_preset($this->folder); /* Removes the temporary files */ + return true; + } +} + +?> diff --git a/mod/data/tabs.php b/mod/data/tabs.php index ba57d4cb1c..9def9553c4 100755 --- a/mod/data/tabs.php +++ b/mod/data/tabs.php @@ -43,7 +43,6 @@ $row[] = new tabobject('single', $CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&mode=single', get_string('single','data'), '', true); } - //if (isloggedin() and !isguest()) { if (isloggedin()) { if (has_capability('mod/data:writeentry', $context)) { // took out participation list here! $addstring = empty($editentry) ? get_string('add', 'data') : get_string('editentry', 'data'); @@ -58,10 +57,12 @@ $defaultemplate = 'singletemplate'; } - $row[] = new tabobject('templates', $CFG->wwwroot.'/mod/data/templates.php?d='.$data->id. - '&mode='.$defaultemplate, get_string('templates','data')); + $row[] = new tabobject('templates', $CFG->wwwroot.'/mod/data/templates.php?d='.$data->id.'&mode='.$defaultemplate, + get_string('templates','data')); $row[] = new tabobject('fields', $CFG->wwwroot.'/mod/data/field.php?d='.$data->id, - get_string('fields','data'), '', true); + get_string('fields','data'), '', true); + $row[] = new tabobject('presets', $CFG->wwwroot.'/mod/data/preset.php?d='.$data->id, + get_string('presets', 'data'), '', true); } } -- 2.39.5