moodle/mod/data/field.php

333 lines
14 KiB
PHP
Raw Normal View History

<?php // $Id$
///////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.org //
// //
// Copyright (C) 2005 Martin Dougiamas http://dougiamas.com //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 2 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details: //
// //
// http://www.gnu.org/copyleft/gpl.html //
// //
///////////////////////////////////////////////////////////////////////////
require_once('../../config.php');
require_once('lib.php');
2006-03-27 08:37:17 +00:00
$id = optional_param('id', 0, PARAM_INT); // course module id
$d = optional_param('d', 0, PARAM_INT); // database id
$fid = optional_param('fid', 0 , PARAM_INT); // update field id
$newtype = optional_param('newtype','',PARAM_ALPHA); // type of the new field
$mode = optional_param('mode','',PARAM_ALPHA);
$defaultsort = optional_param('defaultsort', 0, PARAM_INT);
$defaultsortdir = optional_param('defaultsortdir', 0, PARAM_INT);
$cancel = optional_param('cancel', '');
2006-03-25 14:00:54 +00:00
if ($cancel) {
$mode = 'list';
}
if ($id) {
if (! $cm = get_coursemodule_from_id('data', $id)) {
error('Course Module ID was incorrect');
}
if (! $course = get_record('course', 'id', $cm->course)) {
error('Course is misconfigured');
}
if (! $data = get_record('data', 'id', $cm->instance)) {
error('Course module is incorrect');
}
} else {
if (! $data = get_record('data', 'id', $d)) {
error('Data ID is 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');
}
}
require_login($course->id, true, $cm);
2006-08-09 13:45:49 +00:00
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
require_capability('mod/data:managetemplates', $context);
/************************************
* Data Processing *
***********************************/
switch ($mode) {
case 'add': ///add a new field
2006-03-24 02:44:05 +00:00
if (confirm_sesskey() and $fieldinput = data_submitted($CFG->wwwroot.'/mod/data/field.php')){
<?php // $Id: preset.php,v 1.5 2006/08/30 09:17:40 skodak Exp $ /* Preset Menu * * This is the page that is the menu item in the config database * pages. */ require_once('../../config.php'); require_once('lib.php'); 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 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); require_capability('mod/data:managetemplates', get_context_instance(CONTEXT_MODULE, $cm->id)); /* 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 "<center>"; 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 "<table cellpadding=7>"; echo "<tr><td><h3>$strimport</h3></td>"; echo "<td><form name='form' method='POST' action='?d=$data->id&action=importzip&sesskey=$sesskey' enctype='multipart/form-data'>"; helpbutton('importfromfile', '', 'data'); echo " $strfromfile:</td><td><input name=\"file\" size=\"20\" value=\"\" alt=\"file\" type=\"text\"><input name=\"coursefiles\" title=\"Choose or upload a file\" value=\"$strchooseorupload\" onclick=\"return openpopup('/files/index.php?id=2&choose=form.file', 'coursefiles', 'menubar=0,location=0,scrollbars,resizable,width=750,height=500', 0);\" type=\"button\">"; echo "<input type=\"submit\" value=\"$strok\"/>"; echo "</form></td></tr>"; echo "<tr valign=top><td></td><td>"; helpbutton('usepreset', '', 'data'); echo " $strusestandard: </td><td>"; echo "<table width=100%>"; foreach ($presets as $id => $preset) { echo "<tr><form action='' method='POST'>"; echo "<input type='hidden' name='file' value=\"$preset->path\">"; echo "<input type='hidden' name='action' value='importpreset'>"; echo "<input type='hidden' name='d' value='$data->id'>"; echo "<input type='hidden' name='sesskey' value='$sesskey'>"; echo "<td>"; if ($preset->screenshot) { echo "<img src='$preset->screenshot' alt='$preset->screenshot' />"; } echo "</td><td>$preset->name"; if ($preset->user) { $user = get_record('user', 'id', $preset->user); echo " by $user->firstname $user->lastname"; } echo "</td><td><input type='submit' value='$strchoose'></td></form>"; echo "<td>"; if ($preset->user == $USER->id || isadmin()) { echo "<form action='' method='POST'>"; echo "<input type='hidden' name='d' value='$data->id' />"; echo "<input type='hidden' name='action' value='confirmdelete' />"; echo "<input type='hidden' name='sesskey' value='$sesskey' />"; echo "<input type='hidden' name='deleteid' value='$id' />"; echo "<input type='hidden' name='deletename' value=\"$preset->name\" />"; echo "<input type='submit' value='$strdelete' /></form>"; } echo "</td></tr>"; } echo "</table></td></tr>"; echo "<tr><td valign=top><h3>$strexport</h3></td>"; echo "<td><form action='' method='POST'>"; helpbutton('exportzip', '', 'data'); echo " <input type='hidden' name='action' value='export' />"; echo "<input type='hidden' name='d' value='$data->id' />"; echo "<input type='submit' value='$strexportaszip' />"; echo "</form>"; echo "<form action='' method='POST'>"; helpbutton('savepreset', '', 'data'); echo " <input type='hidden' name='action' value='save1' />"; echo "<input type='hidden' name='d' value='$data->id' />"; echo "<input type='hidden' name='sesskey' value='$sesskey' />"; echo "<input type='submit' value='$strsaveaspreset' />"; echo "</form>"; echo "</table>"; 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 "<form action='' method='POST'>"; echo "<input type='hidden' name='d' value='$data->id' />"; echo "<input type='hidden' name='action' value='delete' />"; echo "<input type='hidden' name='sesskey' value='$sesskey' />"; echo "<input type='hidden' name='deleteid' value='$deleteid' />"; echo "<input type='hidden' name='deletename' value=\"$deletename\" />"; echo "<input type='submit' value='$strdelete' /></form>"; 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 <a href='edit.php?d=$data->id'>$straddentries</a> $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')."<br>"; $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 "<a href='$CFG->wwwroot/file.php/$course->id/moddata/data/$data->id/preset.zip'>".get_string('download', 'data')."</a>"; break; /***************** Exporting *****************/ case 'save1': if (!confirm_sesskey()) { error("Sesskey Invalid"); } $strcontinue = get_string('continue'); $strwarning = get_string('presetwarning', 'data'); echo "<div align=center>"; echo "<p>$strwarning</p>"; echo "<form action='' method='POST'>"; echo "Name: <input type='textbox' name='name' value=\"$data->name\" />"; echo "<input type='hidden' name='action' value='save2' />"; echo "<input type='hidden' name='d' value='$data->id' />"; echo "<input type='hidden' name='sesskey' value='$sesskey' />"; echo "<input type='submit' value='$strcontinue' /></form></div>"; 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 "<div align=center>"; echo "<form action='' method='POST'>"; echo "New name: <input type='textbox' name='name' value=\"$name\" />"; echo "<input type='hidden' name='action' value='save2' />"; echo "<input type='hidden' name='d' value='$data->id' />"; echo "<input type='hidden' name='sesskey' value='$sesskey' />"; echo "<input type='submit' value='$strcontinue' /></form>"; echo "<form action='' method='POST'>"; echo "<input type='hidden' name='name' value=\"$name\" />"; echo "<input type='hidden' name='action' value='save3' />"; echo "<input type='hidden' name='d' value='$data->id' />"; echo "<input type='hidden' name='sesskey' value='$sesskey' />"; echo "<input type='submit' value='$stroverwrite' /></form>"; echo "</div>"; 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 "</center>"; 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 data_presets_print_header($course, $cm, $data, $showtabs=true) { global $CFG, $displaynoticegood, $displaynoticebad; $strdata = get_string('modulenameplural','data'); print_header_simple($data->name, '', "<a href='index.php?id=$course->id'>$strdata</a> -> $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.'/jstemplate.js') && 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'); $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', 'ratings', 'participants', 'requiredentries', 'requiredentriestoview', 'maxentries', 'rssarticles', 'approval', 'scale', 'assessed', 'assessedpublic', '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, $folder) { global $CFG; $this->course = $course; $this->cm = $cm; $this->data = $data; $this->folder = $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->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"); $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 "<div align='center'><form action='' method='POST'>"; echo "<input type='hidden' name='sesskey' value='$sesskey' />"; echo "<input type='hidden' name='d' value='{$this->data->id}' />"; echo "<input type='hidden' name='action' value='finishimport' />"; echo "<input type='hidden' name='file' value=\"$this->postfolder\" />"; if ($currentfields != array() && $newfields != array()) { echo "<h3>$strfieldmappings "; echo helpbutton('fieldmappings', '', 'data'); echo "</h3><table>"; foreach ($newfields as $nid => $newfield) { echo "<tr><td>$newfield->name </td>"; echo "<td><select name='field_$nid'>"; foreach ($currentfields as $cid => $currentfield) { if ($currentfield->type == $newfield->type) { if ($currentfield->name == $newfield->name) { echo "<option value='$cid' selected='true'>$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='true'>-</option>"; echo "</select></td></tr>"; } echo "</table>"; echo "<p>$strwarning</p>"; } else if ($newfields == array()) { error("New preset has no defined fields!"); } echo "<input type='submit' value='$strcontinue' /></form></div>"; } 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<br>"; $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(addslashes_object($settings)); if (strstr($this->folder, "/temp/")) clean_preset($this->folder); /* Removes the temporary files */ return true; } } ?>
2006-09-18 11:42:28 +00:00
//$fieldinput->name = data_clean_field_name($fieldinput->name);
/// Only store this new field if it doesn't already exist.
if (($fieldinput->name == '') or data_fieldname_exists($fieldinput->name, $data->id)) {
2006-03-22 09:49:29 +00:00
$displaynoticebad = get_string('invalidfieldname','data');
} else {
/// Check for arrays and convert to a comma-delimited string
data_convert_arrays_to_strings($fieldinput);
/// Create a field object to collect and store the data safely
$type = required_param('type', PARAM_FILE);
$field = data_get_field_new($type, $data);
$field->define_field($fieldinput);
$field->insert_field();
/// Update some templates
data_append_new_field_to_templates($data, $field->field->name);
add_to_log($course->id, 'data', 'fields add',
2006-03-24 02:44:05 +00:00
"field.php?d=$data->id&amp;mode=display&amp;fid=$fid", $fid, $cm->id);
2006-03-22 09:49:29 +00:00
$displaynoticegood = get_string('fieldadded','data');
}
}
break;
case 'update': ///update a field
2006-03-24 02:44:05 +00:00
if (confirm_sesskey() and $fieldinput = data_submitted($CFG->wwwroot.'/mod/data/field.php')){
<?php // $Id: preset.php,v 1.5 2006/08/30 09:17:40 skodak Exp $ /* Preset Menu * * This is the page that is the menu item in the config database * pages. */ require_once('../../config.php'); require_once('lib.php'); 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 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); require_capability('mod/data:managetemplates', get_context_instance(CONTEXT_MODULE, $cm->id)); /* 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 "<center>"; 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 "<table cellpadding=7>"; echo "<tr><td><h3>$strimport</h3></td>"; echo "<td><form name='form' method='POST' action='?d=$data->id&action=importzip&sesskey=$sesskey' enctype='multipart/form-data'>"; helpbutton('importfromfile', '', 'data'); echo " $strfromfile:</td><td><input name=\"file\" size=\"20\" value=\"\" alt=\"file\" type=\"text\"><input name=\"coursefiles\" title=\"Choose or upload a file\" value=\"$strchooseorupload\" onclick=\"return openpopup('/files/index.php?id=2&choose=form.file', 'coursefiles', 'menubar=0,location=0,scrollbars,resizable,width=750,height=500', 0);\" type=\"button\">"; echo "<input type=\"submit\" value=\"$strok\"/>"; echo "</form></td></tr>"; echo "<tr valign=top><td></td><td>"; helpbutton('usepreset', '', 'data'); echo " $strusestandard: </td><td>"; echo "<table width=100%>"; foreach ($presets as $id => $preset) { echo "<tr><form action='' method='POST'>"; echo "<input type='hidden' name='file' value=\"$preset->path\">"; echo "<input type='hidden' name='action' value='importpreset'>"; echo "<input type='hidden' name='d' value='$data->id'>"; echo "<input type='hidden' name='sesskey' value='$sesskey'>"; echo "<td>"; if ($preset->screenshot) { echo "<img src='$preset->screenshot' alt='$preset->screenshot' />"; } echo "</td><td>$preset->name"; if ($preset->user) { $user = get_record('user', 'id', $preset->user); echo " by $user->firstname $user->lastname"; } echo "</td><td><input type='submit' value='$strchoose'></td></form>"; echo "<td>"; if ($preset->user == $USER->id || isadmin()) { echo "<form action='' method='POST'>"; echo "<input type='hidden' name='d' value='$data->id' />"; echo "<input type='hidden' name='action' value='confirmdelete' />"; echo "<input type='hidden' name='sesskey' value='$sesskey' />"; echo "<input type='hidden' name='deleteid' value='$id' />"; echo "<input type='hidden' name='deletename' value=\"$preset->name\" />"; echo "<input type='submit' value='$strdelete' /></form>"; } echo "</td></tr>"; } echo "</table></td></tr>"; echo "<tr><td valign=top><h3>$strexport</h3></td>"; echo "<td><form action='' method='POST'>"; helpbutton('exportzip', '', 'data'); echo " <input type='hidden' name='action' value='export' />"; echo "<input type='hidden' name='d' value='$data->id' />"; echo "<input type='submit' value='$strexportaszip' />"; echo "</form>"; echo "<form action='' method='POST'>"; helpbutton('savepreset', '', 'data'); echo " <input type='hidden' name='action' value='save1' />"; echo "<input type='hidden' name='d' value='$data->id' />"; echo "<input type='hidden' name='sesskey' value='$sesskey' />"; echo "<input type='submit' value='$strsaveaspreset' />"; echo "</form>"; echo "</table>"; 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 "<form action='' method='POST'>"; echo "<input type='hidden' name='d' value='$data->id' />"; echo "<input type='hidden' name='action' value='delete' />"; echo "<input type='hidden' name='sesskey' value='$sesskey' />"; echo "<input type='hidden' name='deleteid' value='$deleteid' />"; echo "<input type='hidden' name='deletename' value=\"$deletename\" />"; echo "<input type='submit' value='$strdelete' /></form>"; 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 <a href='edit.php?d=$data->id'>$straddentries</a> $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')."<br>"; $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 "<a href='$CFG->wwwroot/file.php/$course->id/moddata/data/$data->id/preset.zip'>".get_string('download', 'data')."</a>"; break; /***************** Exporting *****************/ case 'save1': if (!confirm_sesskey()) { error("Sesskey Invalid"); } $strcontinue = get_string('continue'); $strwarning = get_string('presetwarning', 'data'); echo "<div align=center>"; echo "<p>$strwarning</p>"; echo "<form action='' method='POST'>"; echo "Name: <input type='textbox' name='name' value=\"$data->name\" />"; echo "<input type='hidden' name='action' value='save2' />"; echo "<input type='hidden' name='d' value='$data->id' />"; echo "<input type='hidden' name='sesskey' value='$sesskey' />"; echo "<input type='submit' value='$strcontinue' /></form></div>"; 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 "<div align=center>"; echo "<form action='' method='POST'>"; echo "New name: <input type='textbox' name='name' value=\"$name\" />"; echo "<input type='hidden' name='action' value='save2' />"; echo "<input type='hidden' name='d' value='$data->id' />"; echo "<input type='hidden' name='sesskey' value='$sesskey' />"; echo "<input type='submit' value='$strcontinue' /></form>"; echo "<form action='' method='POST'>"; echo "<input type='hidden' name='name' value=\"$name\" />"; echo "<input type='hidden' name='action' value='save3' />"; echo "<input type='hidden' name='d' value='$data->id' />"; echo "<input type='hidden' name='sesskey' value='$sesskey' />"; echo "<input type='submit' value='$stroverwrite' /></form>"; echo "</div>"; 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 "</center>"; 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 data_presets_print_header($course, $cm, $data, $showtabs=true) { global $CFG, $displaynoticegood, $displaynoticebad; $strdata = get_string('modulenameplural','data'); print_header_simple($data->name, '', "<a href='index.php?id=$course->id'>$strdata</a> -> $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.'/jstemplate.js') && 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'); $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', 'ratings', 'participants', 'requiredentries', 'requiredentriestoview', 'maxentries', 'rssarticles', 'approval', 'scale', 'assessed', 'assessedpublic', '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, $folder) { global $CFG; $this->course = $course; $this->cm = $cm; $this->data = $data; $this->folder = $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->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"); $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 "<div align='center'><form action='' method='POST'>"; echo "<input type='hidden' name='sesskey' value='$sesskey' />"; echo "<input type='hidden' name='d' value='{$this->data->id}' />"; echo "<input type='hidden' name='action' value='finishimport' />"; echo "<input type='hidden' name='file' value=\"$this->postfolder\" />"; if ($currentfields != array() && $newfields != array()) { echo "<h3>$strfieldmappings "; echo helpbutton('fieldmappings', '', 'data'); echo "</h3><table>"; foreach ($newfields as $nid => $newfield) { echo "<tr><td>$newfield->name </td>"; echo "<td><select name='field_$nid'>"; foreach ($currentfields as $cid => $currentfield) { if ($currentfield->type == $newfield->type) { if ($currentfield->name == $newfield->name) { echo "<option value='$cid' selected='true'>$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='true'>-</option>"; echo "</select></td></tr>"; } echo "</table>"; echo "<p>$strwarning</p>"; } else if ($newfields == array()) { error("New preset has no defined fields!"); } echo "<input type='submit' value='$strcontinue' /></form></div>"; } 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<br>"; $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(addslashes_object($settings)); if (strstr($this->folder, "/temp/")) clean_preset($this->folder); /* Removes the temporary files */ return true; } } ?>
2006-09-18 11:42:28 +00:00
//$fieldinput->name = data_clean_field_name($fieldinput->name);
if (($fieldinput->name == '') or data_fieldname_exists($fieldinput->name, $data->id, $fieldinput->fid)) {
2006-03-22 09:49:29 +00:00
$displaynoticebad = get_string('invalidfieldname','data');
} else {
/// Check for arrays and convert to a comma-delimited string
data_convert_arrays_to_strings($fieldinput);
/// Create a field object to collect and store the data safely
$field = data_get_field_from_id($fid, $data);
$oldfieldname = $field->field->name;
2006-03-24 14:34:00 +00:00
$field->field->name = $fieldinput->name;
$field->field->description = $fieldinput->description;
2006-03-24 14:34:00 +00:00
for ($i=1; $i<=10; $i++) {
if (isset($fieldinput->{'param'.$i})) {
$field->field->{'param'.$i} = $fieldinput->{'param'.$i};
} else {
$field->field->{'param'.$i} = '';
}
}
2006-03-24 14:34:00 +00:00
$field->update_field();
/// Update the templates.
data_replace_field_in_templates($data, $oldfieldname, $field->field->name);
add_to_log($course->id, 'data', 'fields update',
2006-03-24 02:44:05 +00:00
"field.php?d=$data->id&amp;mode=display&amp;fid=$fid", $fid, $cm->id);
2006-03-22 09:49:29 +00:00
$displaynoticegood = get_string('fieldupdated','data');
}
}
break;
case 'delete': // Delete a field
if (confirm_sesskey()){
if ($confirm = optional_param('confirm', 0, PARAM_INT)) {
// Delete the field completely
2006-03-22 09:53:13 +00:00
if ($field = data_get_field_from_id($fid, $data)) {
$field->delete_field();
// Update the templates.
data_replace_field_in_templates($data, $field->field->name, '');
2006-03-27 08:37:17 +00:00
// Update the default sort field
if ($fid == $data->defaultsort) {
unset($rec);
$rec->id = $data->id;
$rec->defaultsort = 0;
$rec->defaultsortdir = 0;
if (!update_record('data', $rec)) {
error('There was an error updating the database');
}
}
add_to_log($course->id, 'data', 'fields delete',
2006-03-24 02:44:05 +00:00
"field.php?d=$data->id", $field->field->name, $cm->id);
2006-03-22 09:53:13 +00:00
$displaynoticegood = get_string('fielddeleted', 'data');
}
} else {
data_print_header($course,$cm,$data, false);
// Print confirmation message.
$field = data_get_field_from_id($fid, $data);
notice_yesno('<strong>'.$field->name().': '.$field->field->name.'</strong><br /><br />'. get_string('confirmdeletefield','data'),
2006-03-24 02:44:05 +00:00
'field.php?d='.$data->id.'&amp;mode=delete&amp;fid='.$fid.'&amp;sesskey='.sesskey().'&amp;confirm=1',
'field.php?d='.$data->id);
print_footer($course);
exit;
}
}
break;
2006-03-27 08:37:17 +00:00
case 'sort': // Set the default sort parameters
if (confirm_sesskey()) {
$rec->id = $data->id;
$rec->defaultsort = $defaultsort;
$rec->defaultsortdir = $defaultsortdir;
if (update_record('data', $rec)) {
redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id, get_string('changessaved'), 2);
} else {
error('There was an error updating the database');
}
exit;
}
break;
default:
break;
}
/// Print the browsing interface
///get the list of possible fields (plugins)
$directories = get_list_of_plugins('mod/data/field/');
$menufield = array();
foreach ($directories as $directory){
$menufield[$directory] = get_string($directory,'data'); //get from language files
}
asort($menufield); //sort in alphabetical order
2006-03-27 08:37:17 +00:00
if (($mode == 'new') && (!empty($newtype)) && confirm_sesskey()) { /// Adding a new field
$CFG->pagepath='mod/data/field/'.$newtype;
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_print_header($course,$cm,$data,'fields');
$field = data_get_field_from_id($fid, $data);
$field->display_edit_field();
} else { /// Display the main listing of all fields
$CFG->pagepath='mod/data/field/'.$newtype;
data_print_header($course,$cm,$data,'fields');
if (!record_exists('data_fields','dataid',$data->id)) {
2006-03-22 09:53:13 +00:00
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
2006-03-22 09:33:29 +00:00
2006-03-26 06:00:28 +00:00
$table->head = array(get_string('fieldname','data'), get_string('type','data'), get_string('fielddescription', 'data'), get_string('action','data'));
$table->align = array('left','left','left', 'center');
2006-03-25 14:00:54 +00:00
$table->wrap = array(false,false,false,false);
2006-03-24 14:34:00 +00:00
if ($fff = get_records('data_fields','dataid',$data->id,'id')){
foreach ($fff as $ff) {
$field = data_get_field($ff, $data);
2006-03-22 09:33:29 +00:00
$table->data[] = array(
2006-03-24 02:44:05 +00:00
'<a href="field.php?mode=display&amp;d='.$data->id.
2006-04-05 07:37:48 +00:00
'&amp;fid='.$field->field->id.'&amp;sesskey='.sesskey().'">'.$field->field->name.'</a>',
2006-03-25 14:00:54 +00:00
$field->image().'&nbsp;'.get_string($field->type, 'data'),
2006-03-26 06:00:28 +00:00
shorten_text($field->field->description, 30),
'<a href="field.php?d='.$data->id.'&amp;mode=display&amp;fid='.$field->field->id.'&amp;sesskey='.sesskey().'">'.
'<img src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'.get_string('edit').'" title="'.get_string('edit').'" /></a>'.
2006-03-26 06:00:28 +00:00
'&nbsp;'.
'<a href="field.php?d='.$data->id.'&amp;mode=delete&amp;fid='.$field->field->id.'&amp;sesskey='.sesskey().'">'.
'<img src="'.$CFG->pixpath.'/t/delete.gif" class="iconsmall" alt="'.get_string('delete').'" title="'.get_string('delete').'" /></a>'
2006-03-25 14:00:54 +00:00
);
}
}
2006-03-22 09:33:29 +00:00
print_table($table);
}
2006-03-27 09:08:09 +00:00
2007-01-08 01:09:54 +00:00
echo '<div class="fieldadd">';
2006-03-22 09:53:13 +00:00
echo get_string('newfield','data').': ';
2006-03-27 09:08:09 +00:00
popup_form($CFG->wwwroot.'/mod/data/field.php?d='.$data->id.'&amp;mode=new&amp;sesskey='.
sesskey().'&amp;newtype=', $menufield, 'fieldform', '', 'choose');
2006-03-22 09:53:13 +00:00
helpbutton('fields', get_string('addafield','data'), 'data');
echo '</div>';
2006-03-27 08:37:17 +00:00
if ($fields = get_records('data_fields','dataid',$data->id)) {
2007-01-08 01:09:54 +00:00
echo '<div class="sortdefault">';
echo '<form id="sortdefault" action="'.$CFG->wwwroot.'/mod/data/field.php" method="get">';
2007-01-08 01:09:54 +00:00
echo '<fieldset class="invisiblefieldset">';
echo '<input type="hidden" name="d" value="'.$data->id.'" />';
echo '<input type="hidden" name="mode" value="sort" />';
echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
echo '&nbsp;'.get_string('defaultsortfield','data').':';
echo '<select name="defaultsort"><option value="0">'.get_string('dateentered','data').'</option>';
foreach ($fields as $field) {
if ($field->id == $data->defaultsort) {
echo '<option value="'.$field->id.'" selected="selected">'.$field->name.'</option>';
} else {
echo '<option value="'.$field->id.'">'.$field->name.'</option>';
}
2006-03-27 08:37:17 +00:00
}
echo '</select>';
echo '&nbsp;';
$options = array(0 => get_string('ascending', 'data'),
1 => get_string('descending', 'data'));
choose_from_menu($options, 'defaultsortdir', $data->defaultsortdir, '');
echo '<input type="submit" value="'.get_string('go').'" />';
2007-01-08 01:09:54 +00:00
echo '</fieldset>';
echo '</form>';
echo '</div>';
2006-03-27 08:37:17 +00:00
}
}
/// Finish the page
print_footer($course);
?>