Permalink
Browse files

MDL-9585 - mod/data: moved functions and classes out of preset.php in…

…to lib.php
  • Loading branch information...
1 parent 146bc3b commit 1bb8b3818f33c5b361507c8f95ef3c5db75e6ccb mjollnir_ committed Apr 26, 2007
Showing with 352 additions and 351 deletions.
  1. +352 −0 mod/data/lib.php
  2. +0 −351 mod/data/preset.php
View
@@ -1519,5 +1519,357 @@ function data_user_can_add_entry($data, $currentgroup=false, $groupmode='') {
}
}
+// pulled directly out of preset.php Penny 20070426
+
+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 */
+ $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');
+ $jstemplate = fopen($tempfolder.'/jstemplate.js', '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);
+ fwrite($jstemplate, $data->jstemplate);
+
+ fclose($singletemplate);
+ fclose($listtemplate);
+ fclose($listtemplateheader);
+ fclose($listtemplatefooter);
+ fclose($addtemplate);
+ fclose($rsstemplate);
+ fclose($rsstitletemplate);
+ fclose($csstemplate);
+ fclose($jstemplate);
+
+ /* 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 = "<preset>\n\n";
+
+ /* Database settings first. Name not included? */
+ $settingssaved = array('intro', 'comments',
+ 'requiredentries', 'requiredentriestoview', 'maxentries',
+ 'rssarticles', 'approval', 'scale', 'assessed',
+ 'defaultsort', 'defaultsortdir', 'editany');
+
+ $presetxml .= "<settings>\n";
+ foreach ($settingssaved as $setting) {
+ $presetxml .= "<$setting>{$data->$setting}</$setting>\n";
+ }
+ $presetxml .= "</settings>\n\n";
+
+ /* Now for the fields. Grabs all settings that are non-empty */
+ if (!empty($fields)) {
+ foreach ($fields as $field) {
+ $presetxml .= "<field>\n";
+ foreach ($field as $key => $value) {
+ if ($value != '' && $key != 'id' && $key != 'dataid') {
+ $presetxml .= "<$key>$value</$key>\n";
+ }
+ }
+ $presetxml .= "</field>\n\n";
+ }
+ }
+
+ $presetxml .= "</preset>";
+ 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',
+ 'jstemplate.js',
+ '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';
+}
+
+
+
+class PresetImporter {
+ function PresetImporter($course, $cm, $data, $userid, $shortname) {
+ global $CFG;
+ $this->course = $course;
+ $this->cm = $cm;
+ $this->data = $data;
+ $this->userid = $userid;
+ $this->shortname = $shortname;
+ $this->folder = data_preset_path($course, $userid, $shortname);
+ }
+
+ function get_settings() {
+ global $CFG;
+
+ if (!is_directory_a_preset($this->folder)) {
+ error("$this->userid/$this->shortname 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;
+ $f->type = clean_param($f->type, PARAM_ALPHA);
+ $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->jstemplate = file_get_contents($this->folder."/jstemplate.js");
+
+ $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');
+ $strwarning = get_string('mappingwarning', 'data');
+ $strfieldmappings = get_string('fieldmappings', 'data');
+ $strnew = get_string('new');
+ $strold = get_string('old');
+
+ $sesskey = sesskey();
+
+ list($settings, $newfields, $currentfields) = $this->get_settings();
+
+ echo '<div style="text-align:center"><form action="preset.php" method="post">';
+ echo '<fieldset class="invisiblefieldset">';
+ echo '<input type="hidden" name="action" value="finishimport" />';
+ echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
+ echo '<input type="hidden" name="d" value="'.$this->data->id.'" />';
+ echo '<input type="hidden" name="fullname" value="'.$this->userid.'/'.$this->shortname.'" />';
+
+ if (!empty($currentfields) && !empty($newfields)) {
+ echo "<h3>$strfieldmappings ";
+ helpbutton('fieldmappings', '', 'data');
+ echo '</h3><table>';
+
+ foreach ($newfields as $nid => $newfield) {
+ echo "<tr><td><label for=\"id_$newfield->name\">$newfield->name</label></td>";
+ echo '<td><select name="field_'.$nid.'" id="id_'.$newfield->name.'">';
+
+ $selected = false;
+ foreach ($currentfields as $cid => $currentfield) {
+ if ($currentfield->type == $newfield->type) {
+ if ($currentfield->name == $newfield->name) {
+ echo '<option value="'.$cid.'" selected="selected">'.$currentfield->name.'</option>';
+ $selected=true;
+ }
+ else {
+ echo '<option value="$cid">'.$currentfield->name.'</option>';
+ }
+ }
+ }
+
+ if ($selected)
+ echo '<option value="-1">-</option>';
+ else
+ echo '<option value="-1" selected="selected">-</option>';
+ echo '</select></td></tr>';
+ }
+ echo '</table>';
+ echo "<p>$strwarning</p>";
+ }
+ else if (empty($newfields)) {
+ error("New preset has no defined fields!");
+ }
+ echo '<input type="submit" value="'.$strcontinue.'" /></fieldset></form></div>';
+
+ }
+
+ function import() {
+ global $CFG;
+
+ list($settings, $newfields, $currentfields) = $this->get_settings();
+ $preservedfields = array();
+
+ /* Maps fields and makes new ones */
+ if (!empty($newfields)) {
+ /* 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 and isset($currentfields[$cid])) {
+ $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");
+
+ if (!isset($newfield->description)) {
+ $newfield->description = '';
+ }
+ $classname = 'data_field_'.$newfield->type;
+ $fieldclass = new $classname($newfield, $this->data);
+ $fieldclass->insert_field();
+ unset($fieldclass);
+ }
+ }
+ }
+
+ /* Get rid of all old unused data */
+ if (!empty($preservedfields)) {
+ foreach ($currentfields as $cid => $currentfield) {
+ if (!array_key_exists($cid, $preservedfields)) {
+ /* Data not used anymore so wipe! */
+ print "Deleting field $currentfield->name<br />";
+
+ $id = $currentfield->id;
+ //Why delete existing data records and related comments/ratings??
+/*
+ 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(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?';
+}
?>
Oops, something went wrong.

0 comments on commit 1bb8b38

Please sign in to comment.