MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / data / preset_class.php
blob5d8884f378952e994fb6f5bc0529141c526ee84d
1 <?php
2 // $Id$
4 /**
5 * This object is a representation of a file-based preset for database activities
6 */
8 class Data_Preset
11 /**
12 * Required files in a preset directory
13 * @var array $required_files
15 var $required_files = array('singletemplate.html',
16 'listtemplate.html',
17 'listtemplateheader.html',
18 'listtemplatefooter.html',
19 'addtemplate.html',
20 'rsstemplate.html',
21 'csstemplate.css',
22 'jstemplate.js',
23 'preset.xml');
25 /**
26 * The preset's shortname.
27 * TODO document
28 * @var string $shortname
30 var $shortname;
32 /**
33 * A database activity object
34 * @var object $data
36 var $data;
38 /**
39 * Directory mapped to this Preset object.
40 * @var string $directory
42 var $directory;
44 /**
45 * This Preset's singletemplate
46 * @var string $singletemplate
48 var $singletemplate;
50 /**
51 * This Preset's listtemplate
52 * @var string $listtemplate
54 var $listtemplate;
56 /**
57 * This Preset's listtemplateheader
58 * @var string $listtemplateheader
60 var $listtemplateheader;
62 /**
63 * This Preset's listtemplatefooter
64 * @var string $listtemplatefooter
66 var $listtemplatefooter;
68 /**
69 * This Preset's addtemplate
70 * @var string $addtemplate
72 var $addtemplate;
74 /**
75 * This Preset's rsstemplate
76 * @var string $rsstemplate
78 var $rsstemplate;
80 /**
81 * This Preset's csstemplate
82 * @var string $csstemplate
84 var $csstemplate;
86 /**
87 * This Preset's jstemplate
88 * @var string $jstemplate
90 var $jstemplate;
92 /**
93 * This Preset's xml
94 * @var string $xml
96 var $xml;
98 var $user_id;
101 * Constructor
103 function Data_Preset($shortname = null, $data_id = null, $directory = null, $user_id = null)
105 $this->shortname = $shortname;
106 $this->user_id = $user_id;
108 if (empty($directory)) {
109 $this->directory = $this->get_path();
110 } else {
111 $this->directory = $directory;
114 if (!empty($data_id)) {
115 if (!$this->data = get_record('data', 'id', $data_id)) {
116 print_error('wrongdataid','data');
117 } else {
118 $this->listtemplate = $this->data->listtemplate;
119 $this->singletemplate = $this->data->singletemplate;
120 $this->listtemplateheader = $this->data->listtemplateheader;
121 $this->listtemplatefooter = $this->data->listtemplatefooter;
122 $this->addtemplate = $this->data->addtemplate;
123 $this->rsstemplate = $this->data->rsstemplate;
124 $this->csstemplate = $this->data->csstemplate;
125 $this->jstemplate = $this->data->jstemplate;
131 * Returns the best name to show for a preset
132 * If the shortname has spaces in it, replace them with underscores.
133 * Convert the name to lower case.
135 function best_name($shortname = null) {
136 if (empty($shortname)) {
137 $shortname = $this->shortname;
140 /// We are looking inside the preset itself as a first choice, but also in normal data directory
141 $string = get_string('presetname'.$shortname, 'data', NULL, $this->directory.'/lang/');
143 if (substr($string, 0, 1) == '[') {
144 return strtolower(str_replace(' ', '_', $shortname));
145 } else {
146 return $string;
151 * TODO figure out what's going on here with the user id. This method doesn't look quite right to me.
153 function get_path() {
154 global $USER, $CFG, $COURSE;
156 $context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
158 if ($this->user_id > 0 && ($this->user_id == $USER->id || has_capability('mod/data:viewalluserpresets', $context))) {
159 return $CFG->dataroot.'/data/preset/'.$this->user_id.'/'.$this->shortname;
160 } else if ($this->user_id == 0) {
161 return $CFG->dirroot.'/mod/data/preset/'.$this->shortname;
162 } else if ($this->user_id < 0) {
163 return $CFG->dataroot.'/temp/data/'.-$this->user_id.'/'.$this->shortname;
166 return 'Does it disturb you that this code will never run?';
171 * A preset is a directory with a number of required files.
172 * This function verifies that the given directory contains
173 * all these files, thus validating as a preset directory.
175 * @param string $directory An optional directory to check. Will use this Preset's directory if not provided.
176 * @return mixed True if the directory contains all the files required to qualify as Preset object;
177 * an array of the missing filename is returned otherwise
179 function has_all_required_files($directory = null)
181 if (empty($directory)) {
182 $directory = $this->directory;
183 } else {
184 $directory = rtrim($directory, '/\\') . '/';
187 $missing_files = array();
189 foreach ($this->required_files as $file) {
190 if(!file_exists($directory . '/' . $file)) {
191 $missing_files[] = $file;
195 if (!empty($missing_files)) {
196 return $missing_files;
199 return true;
203 * Deletes all the files in the directory mapped by this Preset object.
205 * @return boolean False if an error occured while trying to delete one of the files, true otherwise.
207 function clean_files()
209 foreach ($this->required_files as $file) {
210 if (!unlink($this->directory . '/' . $file)) {
211 return $file;
215 return true;
218 function get_template_files()
220 $template_files = array();
222 foreach ($this->required_files as $file) {
223 if (preg_match('/^([a-z]+template[a-z]?)\.[a-z]{2,4}$/', $file, $matches)) {
224 $template_files[$matches[1]] = $file;
228 return $template_files;
232 * Exports this Preset object as a series of files in the Preset's directory.
233 * @return string The path/name of the resulting zip file if successful.
235 function export() {
236 global $CFG;
237 $this->directory = $CFG->dataroot.'/temp';
238 // write all templates, but not the xml yet
240 $template_files = $this->get_template_files();
241 foreach ($template_files as $var => $file) {
242 $handle = fopen($this->directory . '/' . $file, 'w');
243 fwrite($handle, $this->$var);
244 fclose($handle);
247 /* All the display data is now done. Now assemble preset.xml */
248 $fields = get_records('data_fields', 'dataid', $this->data->id);
249 $presetfile = fopen($this->directory.'/preset.xml', 'w');
250 $presetxml = "<preset>\n\n";
252 /* Database settings first. Name not included? */
253 $settingssaved = array('intro',
254 'comments',
255 'requiredentries',
256 'requiredentriestoview',
257 'maxentries',
258 'rssarticles',
259 'approval',
260 'scale',
261 'assessed',
262 'defaultsort',
263 'defaultsortdir',
264 'editany');
266 $presetxml .= "<settings>\n";
267 foreach ($settingssaved as $setting) {
268 $presetxml .= "<$setting>{$this->data->$setting}</$setting>\n";
270 $presetxml .= "</settings>\n\n";
272 /* Now for the fields. Grabs all settings that are non-empty */
273 if (!empty($fields)) {
274 foreach ($fields as $field) {
275 $presetxml .= "<field>\n";
276 foreach ($field as $key => $value) {
277 if ($value != '' && $key != 'id' && $key != 'dataid') {
278 $presetxml .= "<$key>$value</$key>\n";
281 $presetxml .= "</field>\n\n";
285 $presetxml .= "</preset>";
286 fwrite($presetfile, $presetxml);
287 fclose($presetfile);
289 /* Check all is well */
290 if (is_array($missing_files = $this->has_all_required_files())) {
291 $missing_files = implode(', ', $missing_files);
292 print_error('filesnotgenerated', 'data', null, $missing_files);
295 // Remove export.zip
296 @unlink($this->directory.'/export.zip');
298 $filelist = array();
299 foreach ($this->required_files as $file) {
300 $filelist[$file] = $this->directory . '/' . $file;
303 // zip_files is part of moodlelib
304 $status = zip_files($filelist, $this->directory.'/export.zip');
306 /* made the zip... now return the filename for storage.*/
307 return $this->directory.'/export.zip';
312 * Loads the contents of the preset folder to initialise this Preset object.
313 * TODO document
315 function load_from_file($directory = null) {
316 global $CFG;
317 if (empty($directory) && empty($this->directory)) {
318 $this->directory = $this->get_path();
321 if (is_array($missing_files = $this->has_all_required_files())) {
322 $a = new StdClass();
323 $a->missing_files = implode(', ', $missing_files);
324 $a->directory = $this->directory;
325 print_error('directorynotapreset','data', null, $a);
328 /* Grab XML */
329 $presetxml = file_get_contents($this->directory.'/preset.xml');
330 $parsedxml = xmlize($presetxml);
332 /* First, do settings. Put in user friendly array. */
333 $settingsarray = $parsedxml['preset']['#']['settings'][0]['#'];
334 $settings = new StdClass();
336 foreach ($settingsarray as $setting => $value) {
337 $settings->$setting = $value[0]['#'];
340 /* Now work out fields to user friendly array */
341 $fieldsarray = $parsedxml['preset']['#']['field'];
342 $fields = array();
343 foreach ($fieldsarray as $field) {
344 $f = new StdClass();
345 foreach ($field['#'] as $param => $value) {
346 $f->$param = $value[0]['#'];
348 $f->dataid = $this->data->id;
349 $f->type = clean_param($f->type, PARAM_ALPHA);
350 $fields[] = $f;
354 /* Now add the HTML templates to the settings array so we can update d */
355 $template_files = $this->get_template_files();
357 foreach ($template_files as $var => $file) {
358 $settings->$var = file_get_contents($this->directory . '/' . $file);
361 $settings->instance = $this->data->id;
363 /* Now we look at the current structure (if any) to work out whether we need to clear db
364 or save the data */
365 $currentfields = array();
366 $currentfields = get_records('data_fields', 'dataid', $this->data->id);
367 $currentfields = array_merge($currentfields);
368 return array($settings, $fields, $currentfields);
373 * Import options
374 * TODO document
375 * TODO replace all output by a return value
377 function get_import_html() {
378 if (!confirm_sesskey()) {
379 print_error("confirmsesskeybad");
382 $strblank = get_string('blank', 'data');
383 $strnofields = get_string('nofields', 'data');
384 $strcontinue = get_string('continue');
385 $strwarning = get_string('mappingwarning', 'data');
386 $strfieldmappings = get_string('fieldmappings', 'data');
387 $strnew = get_string('new');
388 $strold = get_string('old');
390 $sesskey = sesskey();
392 list($settings, $newfields, $currentfields) = $this->load_from_file();
394 $html = '';
396 $html .= '<div style="text-align:center"><form action="preset.php" method="post">';
397 $html .= '<fieldset class="invisiblefieldset">';
398 $html .= '<input type="hidden" name="action" value="finishimport" />';
399 $html .= '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
400 $html .= '<input type="hidden" name="d" value="'.$this->data->id.'" />';
401 $html .= '<input type="hidden" name="fullname" value="'.$this->user_id.'/'.$this->shortname.'" />';
403 if (!empty($currentfields) && !empty($newfields)) {
404 $html .= "<h3>$strfieldmappings ";
405 helpbutton('fieldmappings', '', 'data');
406 $html .= '</h3><table>';
408 foreach ($newfields as $nid => $newfield) {
409 $html .= "<tr><td><label for=\"id_$newfield->name\">$newfield->name</label></td>";
410 $html .= '<td><select name="field_'.$nid.'" id="id_'.$newfield->name.'">';
412 $selected = false;
413 foreach ($currentfields as $cid => $currentfield) {
414 if ($currentfield->type == $newfield->type) {
415 if ($currentfield->name == $newfield->name) {
416 $html .= '<option value="'.$cid.'" selected="selected">'.$currentfield->name.'</option>';
417 $selected=true;
419 else {
420 $html .= '<option value="$cid">'.$currentfield->name.'</option>';
425 if ($selected)
426 $html .= '<option value="-1">-</option>';
427 else
428 $html .= '<option value="-1" selected="selected">-</option>';
429 $html .= '</select></td></tr>';
431 $html .= '</table>';
432 $html .= "<p>$strwarning</p>";
434 else if (empty($newfields)) {
435 print_error('nodefinedfields', 'data');
437 $html .= '<input type="submit" value="'.$strcontinue.'" /></fieldset></form></div>';
438 return $html;
443 * import()
444 * TODO document
446 function import() {
447 global $CFG;
449 list($settings, $newfields, $currentfields) = $this->load_from_file();
450 $preservedfields = array();
452 /* Maps fields and makes new ones */
453 if (!empty($newfields)) {
454 /* We require an injective mapping, and need to know what to protect */
455 foreach ($newfields as $nid => $newfield) {
456 $cid = optional_param("field_$nid", -1, PARAM_INT);
457 if ($cid == -1) continue;
459 if (array_key_exists($cid, $preservedfields)) {
460 print_error('notinjectivemap', 'data');
461 } else {
462 $preservedfields[$cid] = true;
466 foreach ($newfields as $nid => $newfield) {
467 $cid = optional_param("field_$nid", -1, PARAM_INT);
468 /* A mapping. Just need to change field params. Data kept. */
469 if ($cid != -1 and isset($currentfelds[$cid])) {
470 $fieldobject = data_get_field_from_id($currentfields[$cid]->id, $this->data);
471 foreach ($newfield as $param => $value) {
472 if ($param != "id") {
473 $fieldobject->field->$param = $value;
476 unset($fieldobject->field->similarfield);
477 $fieldobject->update_field();
478 unset($fieldobject);
480 /* Make a new field */
481 else {
482 include_once("field/$newfield->type/field.class.php");
484 if (!isset($newfield->description)) {
485 $newfield->description = '';
487 $classname = 'data_field_'.$newfield->type;
488 $fieldclass = new $classname($newfield, $this->data);
489 $fieldclass->insert_field();
490 unset($fieldclass);
495 /* Get rid of all old unused data */
496 if (!empty($preservedfields)) {
497 foreach ($currentfields as $cid => $currentfield) {
498 if (!array_key_exists($cid, $preservedfields)) {
499 /* Data not used anymore so wipe! */
500 print "Deleting field $currentfield->name<br />";
501 $id = $currentfield->id;
502 // Why delete existing data records and related comments/ratings ??
504 if ($content = get_records('data_content', 'fieldid', $id)) {
505 foreach ($content as $item) {
506 delete_records('data_ratings', 'recordid', $item->recordid);
507 delete_records('data_comments', 'recordid', $item->recordid);
508 delete_records('data_records', 'id', $item->recordid);
512 delete_records('data_content', 'fieldid', $id);
513 delete_records('data_fields', 'id', $id);
518 data_update_instance(addslashes_object($settings));
520 if (strstr($this->directory, '/temp/')) clean_preset($this->directory); /* Removes the temporary files */
521 return true;
525 * Runs the Preset action method that matches the given action string.
526 * @param string $action
527 * @return string html
529 function process_action($action, $params)
531 echo $action;
532 if (in_array("action_$action", get_class_methods(get_class($this)))) {
533 return $this->{"action_$action"}($params);
534 } else {
535 print_error('undefinedprocessactionmethod', 'data', null, $action);
539 ////////////////////
540 // ACTION METHODS //
541 ////////////////////
543 function action_base($params)
545 return null;
548 function action_confirmdelete($params)
550 global $CFG, $USER;
551 $html = '';
552 $course = $params['course'];
553 $shortname = $params['shortname'];
555 if (!confirm_sesskey()) { // GET request ok here
556 print_error('confirmsesskeybad');
559 $this->user_id = $params['userid'];
561 if ($this->user_id > 0 and ($this->user_id == $USER->id || has_capability('mod/data:manageuserpresets', $context))) {
562 //ok can delete
563 } else {
564 print_error('invalidrequest');
567 $path = $this->get_path();
569 $strwarning = get_string('deletewarning', 'data').'<br />'.
570 data_preset_name($shortname, $path);
572 $options = new object();
573 $options->fullname = $this->user_id.'/'.$shortname;
574 $options->action = 'delete';
575 $options->d = $this->data->id;
576 $options->sesskey = sesskey();
578 $optionsno = new object();
579 $optionsno->d = $this->data->id;
580 notice_yesno($strwarning, 'preset.php', 'preset.php', $options, $optionsno, 'post', 'get');
581 $html .= print_footer($course, null, true);
582 echo $html;
583 exit();
586 function action_delete($params)
588 global $CFG, $USER;
589 $shortname = $params['shortname'];
591 if (!data_submitted() and !confirm_sesskey()) {
592 print_error('invalidrequest');
595 if ($this->user_id > 0 and ($this->user_id == $USER->id || has_capability('mod/data:manageuserpresets', $context))) {
596 //ok can delete
597 } else {
598 print_error('invalidrequest');
601 $this->shortname = $this->best_name($shortname);
603 $this->path = $this->get_path();
604 $this->directory = $this->path;
606 if (!$this->clean_files()) {
607 print_error('failedpresetdelete', 'data');
609 rmdir($this->path);
611 $strdeleted = get_string('deleted', 'data');
612 notify("$shortname $strdeleted", 'notifysuccess');
615 function action_importpreset($params)
617 $course = $params['course'];
618 if (!data_submitted() or !confirm_sesskey()) {
619 print_error('invalidrequest');
622 $this->shortname = $params['shortname'];
623 $this->data = $params['data'];
624 $this->user_id = $params['userid'];
625 $html = '';
626 $html .= $this->get_import_html();
628 $html .= print_footer($course, null, true);
629 echo $html;
630 exit();
633 function action_importzip($params)
635 global $CFG, $USER;
636 $course = $params['course'];
637 if (!data_submitted() or !confirm_sesskey()) {
638 print_error('invalid_request');
641 if (!make_upload_directory('temp/data/'.$USER->id)) {
642 print_error('errorcreatingdirectory', null, null, 'temp/data/' . $USER->id);
645 $this->file = $CFG->dataroot.'/temp/data/'.$USER->id;
646 $this->directory = $this->file;
647 $this->user_id = $USER->id;
648 $this->clean_files($this->file);
650 if (!unzip_file($CFG->dataroot."/$USER->id/$file", $this->file, false)) {
653 $html .= $this->get_import_html();
654 $html .= print_footer($course, null, true);
655 echo $html;
656 exit();
659 function action_finishimport($params)
661 if (!data_submitted() or !confirm_sesskey()) {
662 print_error('invalidrequest');
664 $this->shortname = $this->best_name($this->data->name);
665 $this->directory = $this->get_path();
666 $this->import();
668 $strimportsuccess = get_string('importsuccess', 'data');
669 $straddentries = get_string('addentries', 'data');
670 $strtodatabase = get_string('todatabase', 'data');
671 if (!get_records('data_records', 'dataid', $this->data->id)) {
672 notify('$strimportsuccess <a href="edit.php?d=' . $this->data->id . "\">$straddentries</a> $strtodatabase", 'notifysuccess');
673 } else {
674 notify("$strimportsuccess", 'notifysuccess');
678 function action_export($params)
680 global $CFG, $USER;
681 $course = $params['course'];
682 $html = '';
684 if (!data_submitted() or !confirm_sesskey()) {
685 print_error('invalid_request');
688 $this->shortname = $params['shortname'];
689 $this->data = $params['data'];
691 $html .= '<div style="text-align:center">';
692 $file = $this->export();
693 $html .= get_string('exportedtozip', 'data')."<br />";
694 $permanentfile = $CFG->dataroot.'/' . $course->id . '/moddata/data/' . $this->data->id . '/preset.zip';
695 @unlink($permanentfile);
696 /* is this created elsewhere? sometimes its not present... */
697 make_upload_directory($course->id . '/moddata/data/' . $this->data->id);
699 /* now just move the zip into this folder to allow a nice download */
700 if (!rename($file, $permanentfile)) {
701 print_error('movezipfailed', 'data');
704 $html .= '<a href="' . $CFG->wwwroot . '/file.php/' . $course->id . '/moddata/data/' . $this->data->id . '/preset.zip">'.get_string('download', 'data')."</a>";
705 $html .= '</div>';
706 return $html;
710 * First stage of saving a Preset: ask for a name
712 function action_save1($params)
714 $html = '';
715 $sesskey = $params['sesskey'];
716 $course = $params['course'];
717 if (!data_submitted() or !confirm_sesskey()) {
718 print_error('invalid_request');
721 $strcontinue = get_string('continue');
722 $strwarning = get_string('presetinfo', 'data');
723 $strname = get_string('shortname');
725 $html .= '<div style="text-align:center">';
726 $html .= '<p>'.$strwarning.'</p>';
727 $html .= '<form action="preset.php" method="post">';
728 $html .= '<fieldset class="invisiblefieldset">';
729 $html .= '<label for="shortname">'.$strname.'</label> <input type="text" id="shortname" name="name" value="'.$this->best_name($this->data->name).'" />';
730 $html .= '<input type="hidden" name="action" value="save2" />';
731 $html .= '<input type="hidden" name="d" value="'.$this->data->id.'" />';
732 $html .= '<input type="hidden" name="sesskey" value="'.$sesskey.'" />';
733 $html .= '<input type="submit" value="'.$strcontinue.'" /></fieldset></form></div>';
734 $html .= print_footer($course, null, true);
735 echo $html;
736 exit();
740 * Second stage of saving a preset: If the given name already exists,
741 * suggest to use a different name or offer to overwrite the existing preset.
743 function action_save2($params)
745 $course = $params['course'];
746 $this->data = $params['data'];
748 global $CFG, $USER;
749 if (!data_submitted() or !confirm_sesskey()) {
750 print_error('invalid_request');
753 $strcontinue = get_string('continue');
754 $stroverwrite = get_string('overwrite', 'data');
755 $strname = get_string('shortname');
757 $name = $this->best_name(optional_param('name', $this->data->name, PARAM_FILE));
758 $this->shortname = $name;
760 if (!is_array($this->has_all_required_files("$CFG->dataroot/data/preset/$USER->id/$name"))) {
761 notify("Preset already exists: Pick another name or overwrite ($CFG->dataroot/data/preset/$USER->id/$name)");
763 $html .= '<div style="text-align:center">';
764 $html .= '<form action="preset.php" method="post">';
765 $html .= '<fieldset class="invisiblefieldset">';
766 $html .= '<label for="shortname">'.$strname.'</label> <input id="shortname" type="text" name="name" value="'.$name.'" />';
767 $html .= '<input type="hidden" name="action" value="save2" />';
768 $html .= '<input type="hidden" name="d" value="'.$this->data->id.'" />';
769 $html .= '<input type="hidden" name="sesskey" value="'.$sesskey.'" />';
770 $html .= '<input type="submit" value="'.$strcontinue.'" /></fieldset></form>';
772 $html .= '<form action="preset.php" method="post">';
773 $html .= '<div>';
774 $html .= '<input type="hidden" name="name" value="'.$name.'" />';
775 $html .= '<input type="hidden" name="action" value="save3" />';
776 $html .= '<input type="hidden" name="d" value="'.$this->data->id.'" />';
777 $html .= '<input type="hidden" name="sesskey" value="'.$sesskey.'" />';
778 $html .= '<input type="submit" value="'.$stroverwrite.'" /></div></form>';
779 $html .= '</div>';
780 $html .= print_footer($course, null, true);
781 echo $html;
782 exit();
787 * Third stage of saving a preset, overwrites an existing preset with the new one.
789 function action_save3($params)
791 global $CFG, $USER;
792 if (!data_submitted() or !confirm_sesskey()) {
793 print_error('invalidrequest');
796 $name = $this->best_name(optional_param('name', $this->data->name, PARAM_FILE));
797 $this->directory = "/data/preset/$USER->id/$name";
798 $this->shortname = $name;
799 $this->user_id = $USER->id;
801 make_upload_directory($this->directory);
802 $this->clean_files($CFG->dataroot.$this->directory);
804 $file = $this->export();
805 if (!unzip_file($file, $CFG->dataroot.$this->directory, false)) {
806 print_error('cannotunzipfile');
808 notify(get_string('savesuccess', 'data'), 'notifysuccess');