Fixes to the PaintWeb cron task.
[moodle/mihaisucan.git] / mod / data / preset_class.php
blob63b25ed3f096401c3397d95cdd5ebbdb931209f9
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 $strcontinue = get_string('continue');
384 $strwarning = get_string('mappingwarning', 'data');
385 $strfieldmappings = get_string('fieldmappings', 'data');
386 $strnew = get_string('new');
388 $sesskey = sesskey();
390 list($settings, $newfields, $currentfields) = $this->load_from_file();
392 $html = '';
394 $html .= '<div style="text-align:center"><form action="preset.php" method="post">';
395 $html .= '<fieldset class="invisiblefieldset">';
396 $html .= '<input type="hidden" name="action" value="finishimport" />';
397 $html .= '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
398 $html .= '<input type="hidden" name="d" value="'.$this->data->id.'" />';
399 $html .= '<input type="hidden" name="fullname" value="'.$this->user_id.'/'.$this->shortname.'" />';
401 if (!empty($currentfields) && !empty($newfields)) {
402 $html .= "<h3>$strfieldmappings ";
403 helpbutton('fieldmappings', '', 'data');
404 $html .= '</h3><table>';
406 foreach ($newfields as $nid => $newfield) {
407 $html .= "<tr><td><label for=\"id_$newfield->name\">$newfield->name</label></td>";
408 $html .= '<td><select name="field_'.$nid.'" id="id_'.$newfield->name.'">';
410 $selected = false;
411 foreach ($currentfields as $cid => $currentfield) {
412 if ($currentfield->type == $newfield->type) {
413 if ($currentfield->name == $newfield->name) {
414 $html .= '<option value="'.$cid.'" selected="selected">'.$currentfield->name.'</option>';
415 $selected=true;
417 else {
418 $html .= '<option value="'.$cid.'">'.$currentfield->name.'</option>';
423 if ($selected)
424 $html .= '<option value="-1">-</option>';
425 else
426 $html .= '<option value="-1" selected="selected">-</option>';
427 $html .= '</select></td></tr>';
429 $html .= '</table>';
430 $html .= "<p>$strwarning</p>";
432 else if (empty($newfields)) {
433 print_error('nodefinedfields', 'data');
435 $html .= '<input type="submit" value="'.$strcontinue.'" /></fieldset></form></div>';
436 return $html;
441 * import()
442 * TODO document
444 function import() {
445 global $CFG;
447 list($settings, $newfields, $currentfields) = $this->load_from_file();
448 $preservedfields = array();
450 /* Maps fields and makes new ones */
451 if (!empty($newfields)) {
452 /* We require an injective mapping, and need to know what to protect */
453 foreach ($newfields as $nid => $newfield) {
454 $cid = optional_param("field_$nid", -1, PARAM_INT);
455 if ($cid == -1) continue;
457 if (array_key_exists($cid, $preservedfields)) {
458 print_error('notinjectivemap', 'data');
459 } else {
460 $preservedfields[$cid] = true;
464 foreach ($newfields as $nid => $newfield) {
465 $cid = optional_param("field_$nid", -1, PARAM_INT);
466 /* A mapping. Just need to change field params. Data kept. */
467 if ($cid != -1 and isset($currentfelds[$cid])) {
468 $fieldobject = data_get_field_from_id($currentfields[$cid]->id, $this->data);
469 foreach ($newfield as $param => $value) {
470 if ($param != "id") {
471 $fieldobject->field->$param = $value;
474 unset($fieldobject->field->similarfield);
475 $fieldobject->update_field();
476 unset($fieldobject);
478 /* Make a new field */
479 else {
480 include_once("field/$newfield->type/field.class.php");
482 if (!isset($newfield->description)) {
483 $newfield->description = '';
485 $classname = 'data_field_'.$newfield->type;
486 $fieldclass = new $classname($newfield, $this->data);
487 $fieldclass->insert_field();
488 unset($fieldclass);
493 /* Get rid of all old unused data */
494 if (!empty($preservedfields)) {
495 foreach ($currentfields as $cid => $currentfield) {
496 if (!array_key_exists($cid, $preservedfields)) {
497 /* Data not used anymore so wipe! */
498 print "Deleting field $currentfield->name<br />";
499 $id = $currentfield->id;
500 // Why delete existing data records and related comments/ratings ??
502 if ($content = get_records('data_content', 'fieldid', $id)) {
503 foreach ($content as $item) {
504 delete_records('data_ratings', 'recordid', $item->recordid);
505 delete_records('data_comments', 'recordid', $item->recordid);
506 delete_records('data_records', 'id', $item->recordid);
510 delete_records('data_content', 'fieldid', $id);
511 delete_records('data_fields', 'id', $id);
516 data_update_instance(addslashes_object($settings));
518 if (strstr($this->directory, '/temp/')) clean_preset($this->directory); /* Removes the temporary files */
519 return true;
523 * Runs the Preset action method that matches the given action string.
524 * @param string $action
525 * @return string html
527 function process_action($action, $params)
529 echo $action;
530 if (in_array("action_$action", get_class_methods(get_class($this)))) {
531 return $this->{"action_$action"}($params);
532 } else {
533 print_error('undefinedprocessactionmethod', 'data', null, $action);
537 ////////////////////
538 // ACTION METHODS //
539 ////////////////////
541 function action_base($params)
543 return null;
546 function action_confirmdelete($params)
548 global $CFG, $USER;
549 $html = '';
550 $course = $params['course'];
551 $shortname = $params['shortname'];
553 if (!confirm_sesskey()) { // GET request ok here
554 print_error('confirmsesskeybad');
557 $this->user_id = $params['userid'];
559 if (!$cm = get_coursemodule_from_instance('data', $this->data->id, $course->id)) {
560 print_error('invalidrequest');
563 $context = get_context_instance(COURSE_MODULE, $cm->id);
565 if ($this->user_id > 0 and ($this->user_id == $USER->id || has_capability('mod/data:manageuserpresets', $context))) {
566 //ok can delete
567 } else {
568 print_error('invalidrequest');
571 $path = $this->get_path();
573 $strwarning = get_string('deletewarning', 'data').'<br />'.
574 data_preset_name($shortname, $path);
576 $options = new object();
577 $options->fullname = $this->user_id.'/'.$shortname;
578 $options->action = 'delete';
579 $options->d = $this->data->id;
580 $options->sesskey = sesskey();
582 $optionsno = new object();
583 $optionsno->d = $this->data->id;
584 notice_yesno($strwarning, 'preset.php', 'preset.php', $options, $optionsno, 'post', 'get');
585 $html .= print_footer($course, null, true);
586 echo $html;
587 exit();
590 function action_delete($params)
592 global $CFG, $USER;
593 $course = $params['course'];
594 $shortname = $params['shortname'];
596 if (!data_submitted() and !confirm_sesskey()) {
597 print_error('invalidrequest');
600 if (!$cm = get_coursemodule_from_instance('data', $this->data->id, $course->id)) {
601 print_error('invalidrequest');
604 $context = get_context_instance(COURSE_MODULE, $cm->id);
606 if ($this->user_id > 0 and ($this->user_id == $USER->id || has_capability('mod/data:manageuserpresets', $context))) {
607 //ok can delete
608 } else {
609 print_error('invalidrequest');
612 $this->shortname = $this->best_name($shortname);
614 $this->path = $this->get_path();
615 $this->directory = $this->path;
617 if (!$this->clean_files()) {
618 print_error('failedpresetdelete', 'data');
620 rmdir($this->path);
622 $strdeleted = get_string('deleted', 'data');
623 notify("$shortname $strdeleted", 'notifysuccess');
626 function action_importpreset($params)
628 $course = $params['course'];
629 if (!data_submitted() or !confirm_sesskey()) {
630 print_error('invalidrequest');
633 $this->shortname = $params['shortname'];
634 $this->data = $params['data'];
635 $this->user_id = $params['userid'];
636 $html = '';
637 $html .= $this->get_import_html();
639 $html .= print_footer($course, null, true);
640 echo $html;
641 exit();
644 function action_importzip($params)
646 global $CFG, $USER;
647 $course = $params['course'];
648 if (!data_submitted() or !confirm_sesskey()) {
649 print_error('invalid_request');
652 if (!make_upload_directory('temp/data/'.$USER->id)) {
653 print_error('errorcreatingdirectory', null, null, 'temp/data/' . $USER->id);
656 $this->file = $CFG->dataroot.'/temp/data/'.$USER->id;
657 $this->directory = $this->file;
658 $this->user_id = $USER->id;
659 $this->clean_files($this->file);
661 if (!unzip_file($CFG->dataroot."/$USER->id/$file", $this->file, false)) {
664 $html .= $this->get_import_html();
665 $html .= print_footer($course, null, true);
666 echo $html;
667 exit();
670 function action_finishimport($params)
672 if (!data_submitted() or !confirm_sesskey()) {
673 print_error('invalidrequest');
675 $this->shortname = $this->best_name($this->data->name);
676 $this->directory = $this->get_path();
677 $this->import();
679 $strimportsuccess = get_string('importsuccess', 'data');
680 $straddentries = get_string('addentries', 'data');
681 $strtodatabase = get_string('todatabase', 'data');
682 if (!get_records('data_records', 'dataid', $this->data->id)) {
683 notify('$strimportsuccess <a href="edit.php?d=' . $this->data->id . "\">$straddentries</a> $strtodatabase", 'notifysuccess');
684 } else {
685 notify("$strimportsuccess", 'notifysuccess');
689 function action_export($params)
691 global $CFG, $USER;
692 $course = $params['course'];
693 $html = '';
695 if (!data_submitted() or !confirm_sesskey()) {
696 print_error('invalid_request');
699 $this->shortname = $params['shortname'];
700 $this->data = $params['data'];
702 $html .= '<div style="text-align:center">';
703 $file = $this->export();
704 $html .= get_string('exportedtozip', 'data')."<br />";
705 $permanentfile = $CFG->dataroot.'/' . $course->id . '/moddata/data/' . $this->data->id . '/preset.zip';
706 @unlink($permanentfile);
707 /* is this created elsewhere? sometimes its not present... */
708 make_upload_directory($course->id . '/moddata/data/' . $this->data->id);
710 /* now just move the zip into this folder to allow a nice download */
711 if (!rename($file, $permanentfile)) {
712 print_error('movezipfailed', 'data');
715 require_once($CFG->libdir.'/filelib.php');
716 $html .= '<a href="'. get_file_url($course->id .'/moddata/data/'. $this->data->id .'/preset.zip') .'">'. get_string('download', 'data') .'</a>';
717 $html .= '</div>';
718 return $html;
722 * First stage of saving a Preset: ask for a name
724 function action_save1($params)
726 $html = '';
727 $sesskey = sesskey();
728 $course = $params['course'];
729 if (!data_submitted() or !confirm_sesskey()) {
730 print_error('invalid_request');
733 $strcontinue = get_string('continue');
734 $strwarning = get_string('presetinfo', 'data');
735 $strname = get_string('shortname');
737 $html .= '<div style="text-align:center">';
738 $html .= '<p>'.$strwarning.'</p>';
739 $html .= '<form action="preset.php" method="post">';
740 $html .= '<fieldset class="invisiblefieldset">';
741 $html .= '<label for="shortname">'.$strname.'</label> <input type="text" id="shortname" name="name" value="'.$this->best_name($this->data->name).'" />';
742 $html .= '<input type="hidden" name="action" value="save2" />';
743 $html .= '<input type="hidden" name="d" value="'.$this->data->id.'" />';
744 $html .= '<input type="hidden" name="sesskey" value="'.$sesskey.'" />';
745 $html .= '<input type="submit" value="'.$strcontinue.'" /></fieldset></form></div>';
746 $html .= print_footer($course, null, true);
747 echo $html;
748 exit();
752 * Second stage of saving a preset: If the given name already exists,
753 * suggest to use a different name or offer to overwrite the existing preset.
755 function action_save2($params)
757 $course = $params['course'];
758 $this->data = $params['data'];
760 global $CFG, $USER;
761 if (!data_submitted() or !confirm_sesskey()) {
762 print_error('invalid_request');
765 $strcontinue = get_string('continue');
766 $stroverwrite = get_string('overwrite', 'data');
767 $strname = get_string('shortname');
769 $name = $this->best_name(optional_param('name', $this->data->name, PARAM_FILE));
770 $this->shortname = $name;
771 $sesskey = sesskey();
773 if (!is_array($this->has_all_required_files("$CFG->dataroot/data/preset/$USER->id/$name"))) {
774 notify("Preset already exists: Pick another name or overwrite ($CFG->dataroot/data/preset/$USER->id/$name)");
776 $html .= '<div style="text-align:center">';
777 $html .= '<form action="preset.php" method="post">';
778 $html .= '<fieldset class="invisiblefieldset">';
779 $html .= '<label for="shortname">'.$strname.'</label> <input id="shortname" type="text" name="name" value="'.$name.'" />';
780 $html .= '<input type="hidden" name="action" value="save2" />';
781 $html .= '<input type="hidden" name="d" value="'.$this->data->id.'" />';
782 $html .= '<input type="hidden" name="sesskey" value="'.$sesskey.'" />';
783 $html .= '<input type="submit" value="'.$strcontinue.'" /></fieldset></form>';
785 $html .= '<form action="preset.php" method="post">';
786 $html .= '<div>';
787 $html .= '<input type="hidden" name="name" value="'.$name.'" />';
788 $html .= '<input type="hidden" name="action" value="save3" />';
789 $html .= '<input type="hidden" name="d" value="'.$this->data->id.'" />';
790 $html .= '<input type="hidden" name="sesskey" value="'.$sesskey.'" />';
791 $html .= '<input type="submit" value="'.$stroverwrite.'" /></div></form>';
792 $html .= '</div>';
793 $html .= print_footer($course, null, true);
794 echo $html;
795 exit();
800 * Third stage of saving a preset, overwrites an existing preset with the new one.
802 function action_save3($params)
804 global $CFG, $USER;
805 if (!data_submitted() or !confirm_sesskey()) {
806 print_error('invalidrequest');
809 $name = $this->best_name(optional_param('name', $this->data->name, PARAM_FILE));
810 $this->directory = "/data/preset/$USER->id/$name";
811 $this->shortname = $name;
812 $this->user_id = $USER->id;
814 make_upload_directory($this->directory);
815 $this->clean_files($CFG->dataroot.$this->directory);
817 $file = $this->export();
818 if (!unzip_file($file, $CFG->dataroot.$this->directory, false)) {
819 print_error('cannotunzipfile');
821 notify(get_string('savesuccess', 'data'), 'notifysuccess');