2006-08-15 08:42:06 +00:00
|
|
|
<?php // $Id$
|
|
|
|
/* Preset Menu
|
2006-12-08 19:36:00 +00:00
|
|
|
*
|
2006-08-15 08:42:06 +00:00
|
|
|
* 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');
|
|
|
|
|
|
|
|
|
2006-12-08 19:36:00 +00:00
|
|
|
$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_ALPHANUM); // current action
|
|
|
|
$fullname = optional_param('fullname', '', PARAM_PATH); // directory the preset is in
|
|
|
|
$file = optional_param('file', '', PARAM_FILE); // uploaded file
|
|
|
|
|
|
|
|
// find out preset owner userid and shortname
|
|
|
|
$parts = explode('/', $fullname);
|
|
|
|
$userid = empty($parts[0]) ? 0 : (int)$parts[0];
|
|
|
|
$shortname = empty($parts[1]) ? '' : $parts[1];
|
|
|
|
unset($parts);
|
|
|
|
unset($fullname);
|
2006-08-15 08:42:06 +00:00
|
|
|
|
|
|
|
if ($id) {
|
2006-12-06 20:17:58 +00:00
|
|
|
if (! $cm = get_coursemodule_from_id('data', $id)) {
|
|
|
|
error('Course Module ID was incorrect');
|
2006-08-15 08:42:06 +00:00
|
|
|
}
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
2006-10-02 17:24:54 +00:00
|
|
|
if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
|
|
|
|
error('Could not find context');
|
|
|
|
}
|
|
|
|
|
2006-12-06 20:17:58 +00:00
|
|
|
require_login($course->id, false, $cm);
|
2006-08-15 08:42:06 +00:00
|
|
|
|
2006-10-02 17:24:54 +00:00
|
|
|
require_capability('mod/data:managetemplates', $context);
|
2006-08-15 08:42:06 +00:00
|
|
|
|
2006-10-02 17:24:54 +00:00
|
|
|
if ($userid && ($userid != $USER->id) && !has_capability('mod/data:viewalluserpresets', $context)) {
|
|
|
|
error('You are not allowed to access presets from other users');
|
|
|
|
}
|
2006-08-15 08:42:06 +00:00
|
|
|
|
2006-10-02 17:24:54 +00:00
|
|
|
/* Need sesskey security check here for import instruction */
|
|
|
|
$sesskey = sesskey();
|
|
|
|
|
|
|
|
/********************************************************************/
|
|
|
|
/* Output */
|
|
|
|
data_print_header($course, $cm, $data, 'presets');
|
2006-08-15 08:42:06 +00:00
|
|
|
|
2006-10-02 17:24:54 +00:00
|
|
|
switch ($action) {
|
2006-08-15 08:42:06 +00:00
|
|
|
|
2006-10-02 17:24:54 +00:00
|
|
|
/***************** Deleting *****************/
|
|
|
|
case 'confirmdelete' :
|
2006-12-08 19:36:00 +00:00
|
|
|
if (!confirm_sesskey()) { // GET request ok here
|
2006-10-02 17:24:54 +00:00
|
|
|
error("Sesskey Invalid");
|
|
|
|
}
|
2006-12-08 19:36:00 +00:00
|
|
|
|
|
|
|
if ($userid > 0 and ($userid == $USER->id || has_capability('mod/data:manageuserpresets', $context))) {
|
|
|
|
//ok can delete
|
|
|
|
} else {
|
|
|
|
error("Invalid request");
|
|
|
|
}
|
|
|
|
|
2006-10-02 17:24:54 +00:00
|
|
|
$path = data_preset_path($course, $userid, $shortname);
|
|
|
|
|
|
|
|
$strwarning = get_string('deletewarning', 'data').'<br />'.
|
|
|
|
data_preset_name($shortname, $path);
|
|
|
|
|
2006-12-08 19:36:00 +00:00
|
|
|
$options = new object();
|
|
|
|
$options->fullname = $userid.'/'.$shortname;
|
|
|
|
$options->action = 'delete';
|
|
|
|
$options->d = $data->id;
|
|
|
|
$options->sesskey = sesskey();
|
2006-10-02 17:24:54 +00:00
|
|
|
|
2006-12-08 19:36:00 +00:00
|
|
|
$optionsno = new object();
|
2006-10-02 17:24:54 +00:00
|
|
|
$optionsno->d = $data->id;
|
|
|
|
notice_yesno($strwarning, 'preset.php', 'preset.php', $options, $optionsno, 'post', 'get');
|
|
|
|
print_footer($course);
|
|
|
|
exit;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'delete' :
|
2006-12-08 19:36:00 +00:00
|
|
|
if (!data_submitted() and !confirm_sesskey()) {
|
|
|
|
error("Invalid request");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($userid > 0 and ($userid == $USER->id || has_capability('mod/data:manageuserpresets', $context))) {
|
|
|
|
//ok can delete
|
|
|
|
} else {
|
|
|
|
error("Invalid request");
|
2006-10-02 17:24:54 +00:00
|
|
|
}
|
2006-08-15 08:42:06 +00:00
|
|
|
|
2006-10-02 17:24:54 +00:00
|
|
|
$presetpath = data_preset_path($course, $userid, $shortname);
|
2006-08-15 08:42:06 +00:00
|
|
|
|
2006-10-02 17:24:54 +00:00
|
|
|
if (!clean_preset($presetpath)) {
|
|
|
|
error("Error deleting a preset!");
|
2006-08-15 08:42:06 +00:00
|
|
|
}
|
2006-10-02 17:24:54 +00:00
|
|
|
@rmdir($presetpath);
|
2006-08-15 08:42:06 +00:00
|
|
|
|
2006-10-02 17:24:54 +00:00
|
|
|
$strdeleted = get_string('deleted', 'data');
|
2006-12-08 19:36:00 +00:00
|
|
|
notify("$shortname $strdeleted", 'notifysuccess');
|
2006-10-02 17:24:54 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
/***************** Importing *****************/
|
|
|
|
case 'importpreset' :
|
2006-12-08 19:36:00 +00:00
|
|
|
if (!data_submitted() or !confirm_sesskey()) {
|
|
|
|
error("Invalid request");
|
2006-08-15 08:42:06 +00:00
|
|
|
}
|
|
|
|
|
2006-10-02 17:24:54 +00:00
|
|
|
$pimporter = new PresetImporter($course, $cm, $data, $userid, $shortname);
|
|
|
|
$pimporter->import_options();
|
|
|
|
|
|
|
|
print_footer($course);
|
|
|
|
exit;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Imports a zip file. */
|
|
|
|
case 'importzip' :
|
2006-12-08 19:36:00 +00:00
|
|
|
if (!data_submitted() or !confirm_sesskey()) {
|
|
|
|
error("Invalid request");
|
2006-08-15 08:42:06 +00:00
|
|
|
}
|
|
|
|
|
2006-10-02 17:24:54 +00:00
|
|
|
if (!make_upload_directory('temp/data/'.$USER->id)) {
|
|
|
|
error("Can't Create Directory");
|
|
|
|
}
|
2006-08-15 08:42:06 +00:00
|
|
|
|
2006-10-02 17:24:54 +00:00
|
|
|
$presetfile = $CFG->dataroot.'/temp/data/'.$USER->id;
|
|
|
|
clean_preset($presetfile);
|
2006-08-15 08:42:06 +00:00
|
|
|
|
2006-10-02 17:24:54 +00:00
|
|
|
if (!unzip_file($CFG->dataroot."/$course->id/$file", $presetfile, false)) {
|
|
|
|
error("Can't unzip file");
|
|
|
|
}
|
2006-08-15 08:42:06 +00:00
|
|
|
|
2006-10-02 17:24:54 +00:00
|
|
|
$pimporter = new PresetImporter($course, $cm, $data, -$USER->id, $shortname);
|
|
|
|
$pimporter->import_options();
|
2006-08-15 08:42:06 +00:00
|
|
|
|
2006-10-02 17:24:54 +00:00
|
|
|
print_footer($course);
|
|
|
|
exit;
|
|
|
|
break;
|
2006-08-15 08:42:06 +00:00
|
|
|
|
2006-10-02 17:24:54 +00:00
|
|
|
case 'finishimport':
|
2006-12-08 19:36:00 +00:00
|
|
|
if (!data_submitted() or !confirm_sesskey()) {
|
|
|
|
error("Invalid request");
|
2006-10-02 17:24:54 +00:00
|
|
|
}
|
2006-08-15 08:42:06 +00:00
|
|
|
|
2006-10-02 17:24:54 +00:00
|
|
|
$pimporter = new PresetImporter($course, $cm, $data, $userid, $shortname);
|
|
|
|
$pimporter->import();
|
2006-08-15 08:42:06 +00:00
|
|
|
|
2006-10-02 17:24:54 +00:00
|
|
|
$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;
|
2006-08-15 08:42:06 +00:00
|
|
|
|
2006-10-02 17:24:54 +00:00
|
|
|
/* Exports as a zip file ready for download. */
|
|
|
|
case 'export':
|
2006-12-08 19:36:00 +00:00
|
|
|
if (!data_submitted() or !confirm_sesskey()) {
|
|
|
|
error("Invalid request");
|
|
|
|
}
|
|
|
|
|
2007-01-05 08:01:33 +00:00
|
|
|
echo '<div style="text-align:center">';
|
2006-10-02 17:24:54 +00:00
|
|
|
$file = data_presets_export($course, $cm, $data);
|
2006-12-08 19:36:00 +00:00
|
|
|
echo get_string('exportedtozip', 'data')."<br />";
|
2006-10-02 17:24:54 +00:00
|
|
|
$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");
|
2006-08-15 08:42:06 +00:00
|
|
|
|
2006-10-02 17:24:54 +00:00
|
|
|
/* 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>";
|
2006-12-08 19:36:00 +00:00
|
|
|
echo '</div>';
|
2006-10-02 17:24:54 +00:00
|
|
|
break;
|
2006-08-15 08:42:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2006-10-02 17:24:54 +00:00
|
|
|
/***************** Exporting *****************/
|
|
|
|
case 'save1':
|
2006-12-08 19:36:00 +00:00
|
|
|
if (!data_submitted() or !confirm_sesskey()) {
|
|
|
|
error("Invalid request");
|
2006-10-02 17:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$strcontinue = get_string('continue');
|
|
|
|
$strwarning = get_string('presetinfo', 'data');
|
2006-12-08 19:36:00 +00:00
|
|
|
$strname = get_string('shortname');
|
|
|
|
|
2007-01-05 08:01:33 +00:00
|
|
|
echo '<div style="text-align:center">';
|
2006-12-08 19:36:00 +00:00
|
|
|
echo '<p>'.$strwarning.'</p>';
|
|
|
|
echo '<form action="preset.php" method="post">';
|
2007-01-05 08:01:33 +00:00
|
|
|
echo '<fieldset class="invisiblefieldset">';
|
2006-12-08 19:36:00 +00:00
|
|
|
echo '<label for="shorname">'.$strname.'</label> <input type="text" id="shorname" 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.'" />';
|
2007-01-05 08:01:33 +00:00
|
|
|
echo '<input type="submit" value="'.$strcontinue.'" /></fieldset></form></div>';
|
2006-10-02 17:24:54 +00:00
|
|
|
print_footer($course);
|
|
|
|
exit;
|
|
|
|
break;
|
2006-08-15 08:42:06 +00:00
|
|
|
|
2006-10-02 17:24:54 +00:00
|
|
|
case 'save2':
|
2006-12-08 19:36:00 +00:00
|
|
|
if (!data_submitted() or !confirm_sesskey()) {
|
|
|
|
error("Invalid request");
|
2006-10-02 17:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$strcontinue = get_string('continue');
|
2006-12-08 19:36:00 +00:00
|
|
|
$stroverwrite = get_string('overwrite', 'data');
|
|
|
|
$strname = get_string('shortname');
|
2006-10-02 17:24:54 +00:00
|
|
|
|
|
|
|
$name = optional_param('name', $data->name, PARAM_FILE);
|
|
|
|
|
|
|
|
if (is_directory_a_preset("$CFG->dataroot/data/preset/$USER->id/$name")) {
|
|
|
|
notify("Preset already exists: Pick another name or overwrite");
|
|
|
|
|
2007-01-05 08:01:33 +00:00
|
|
|
echo '<div style="text-align:center">';
|
2006-12-08 19:36:00 +00:00
|
|
|
echo '<form action="preset.php" method="post">';
|
2007-01-05 08:01:33 +00:00
|
|
|
echo '<fieldset class="invisiblefieldset">';
|
2006-12-08 19:36:00 +00:00
|
|
|
echo '<label for="shorname">'.$strname.'</label> <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.'" />';
|
2007-01-05 08:01:33 +00:00
|
|
|
echo '<input type="submit" value="'.$strcontinue.'" /></fieldset></form>';
|
2006-12-08 19:36:00 +00:00
|
|
|
|
|
|
|
echo '<form action="preset.php" method="post">';
|
2007-01-05 08:01:33 +00:00
|
|
|
echo '<fieldset class="invisiblefieldset">';
|
2006-12-08 19:36:00 +00:00
|
|
|
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.'" />';
|
2007-01-05 08:01:33 +00:00
|
|
|
echo '<input type="submit" value="'.$stroverwrite.'" /><fieldset></form>';
|
2006-12-08 19:36:00 +00:00
|
|
|
echo '</div>';
|
2006-10-02 17:24:54 +00:00
|
|
|
print_footer($course);
|
|
|
|
exit;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'save3':
|
2006-12-08 19:36:00 +00:00
|
|
|
if (!data_submitted() or !confirm_sesskey()) {
|
|
|
|
error("Invalid request");
|
2006-10-02 17:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$name = optional_param('name', $data->name, PARAM_FILE);
|
|
|
|
$presetdirectory = "/data/preset/$USER->id/$name";
|
|
|
|
|
|
|
|
make_upload_directory($presetdirectory);
|
|
|
|
clean_preset($CFG->dataroot.$presetdirectory);
|
|
|
|
|
|
|
|
$file = data_presets_export($course, $cm, $data);
|
|
|
|
if (!unzip_file($file, $CFG->dataroot.$presetdirectory, false)) {
|
|
|
|
error("Can't unzip to the preset directory");
|
|
|
|
}
|
|
|
|
notify(get_string('savesuccess', 'data'), 'notifysuccess');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$presets = data_get_available_presets($context);
|
|
|
|
|
2006-12-08 19:36:00 +00:00
|
|
|
$strimport = get_string('import');
|
|
|
|
$strfromfile = get_string('fromfile', 'data');
|
2006-10-02 17:24:54 +00:00
|
|
|
$strchooseorupload = get_string('chooseorupload', 'data');
|
2006-12-08 19:36:00 +00:00
|
|
|
$strusestandard = get_string('usestandard', 'data');
|
|
|
|
$strchoose = get_string('choose');
|
|
|
|
$strexport = get_string('export', 'data');
|
|
|
|
$strexportaszip = get_string('exportaszip', 'data');
|
|
|
|
$strsaveaspreset = get_string('saveaspreset', 'data');
|
|
|
|
$strsave = get_string('save', 'data');
|
|
|
|
$strdelete = get_string('delete');
|
|
|
|
|
2007-01-05 08:01:33 +00:00
|
|
|
echo '<div style="text-align:center">';
|
2006-12-08 19:36:00 +00:00
|
|
|
echo '<table class="presets" cellpadding="5">';
|
|
|
|
echo '<tr><td valign="top" colspan="2" align="center"><h3>'.$strexport.'</h3></td></tr>';
|
|
|
|
|
2006-12-12 23:34:55 +00:00
|
|
|
echo '<tr><td><label>'.$strexportaszip.'</label>';
|
2006-12-08 19:36:00 +00:00
|
|
|
helpbutton('exportzip', '', 'data');
|
|
|
|
echo '</td><td>';
|
|
|
|
$options = new object();
|
2006-10-02 17:24:54 +00:00
|
|
|
$options->action = 'export';
|
|
|
|
$options->d = $data->id;
|
|
|
|
$options->sesskey = sesskey();
|
2006-12-08 19:36:00 +00:00
|
|
|
print_single_button('preset.php', $options, $strexport, 'post');
|
|
|
|
echo '</td></tr>';
|
2006-10-02 17:24:54 +00:00
|
|
|
|
2006-12-12 23:34:55 +00:00
|
|
|
echo '<tr><td><label>'.$strsaveaspreset.'</label>';
|
2006-12-08 19:36:00 +00:00
|
|
|
helpbutton('savepreset', '', 'data');
|
|
|
|
echo '</td><td>';
|
|
|
|
$options = new object();
|
2006-10-02 17:24:54 +00:00
|
|
|
$options->action = 'save1';
|
|
|
|
$options->d = $data->id;
|
|
|
|
$options->sesskey = sesskey();
|
2006-12-08 19:36:00 +00:00
|
|
|
print_single_button('preset.php', $options, $strsave, 'post');
|
2006-10-02 17:24:54 +00:00
|
|
|
echo '</td></tr>';
|
|
|
|
|
2006-12-08 19:36:00 +00:00
|
|
|
|
|
|
|
echo '<tr><td valign="top" colspan="2" align="center"><h3>'.$strimport.'</h3></td></tr>';
|
|
|
|
|
|
|
|
echo '<tr><td><label for="fromfile">'.$strfromfile.'</label>';
|
2006-10-02 17:24:54 +00:00
|
|
|
helpbutton('importfromfile', '', 'data');
|
|
|
|
echo '</td><td>';
|
2006-12-08 19:36:00 +00:00
|
|
|
|
2007-01-04 21:32:36 +00:00
|
|
|
echo '<form id="uploadpreset" method="post" action="preset.php">';
|
2007-01-05 08:01:33 +00:00
|
|
|
echo '<fieldset class="invisiblefieldset">';
|
2006-10-02 17:24:54 +00:00
|
|
|
echo '<input type="hidden" name="d" value="'.$data->id.'" />';
|
|
|
|
echo '<input type="hidden" name="action" value="importzip" />';
|
|
|
|
echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
|
2006-12-18 04:47:58 +00:00
|
|
|
echo '<input name="file" size="20" value="" id="fromfile" type="text" /><input name="coursefiles" value="'.$strchooseorupload.'" onclick="return openpopup('."'/files/index.php?id=2&choose=uploadpreset.file', 'coursefiles', 'menubar=0,location=0,scrollbars,resizable,width=750,height=500', 0".');" type="button" />';
|
2006-12-08 19:36:00 +00:00
|
|
|
echo '<input type="submit" value="'.$strimport.'" />';
|
2007-01-05 08:01:33 +00:00
|
|
|
echo '</fieldset></form>';
|
2006-10-02 17:24:54 +00:00
|
|
|
echo '</td></tr>';
|
|
|
|
|
2006-12-08 19:36:00 +00:00
|
|
|
|
|
|
|
echo '<tr valign="top"><td><label>'.$strusestandard.'</label>';
|
2006-10-02 17:24:54 +00:00
|
|
|
helpbutton('usepreset', '', 'data');
|
|
|
|
echo '</td><td>';
|
|
|
|
|
2007-01-04 21:32:36 +00:00
|
|
|
echo '<form id="presets" method="post" action="preset.php" >';
|
2007-01-05 08:01:33 +00:00
|
|
|
echo '<fieldset class="invisiblefieldset">';
|
2006-12-08 19:36:00 +00:00
|
|
|
echo '<input type="hidden" name="d" value="'.$data->id.'" />';
|
|
|
|
echo '<input type="hidden" name="action" value="importpreset" />';
|
|
|
|
echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
|
|
|
|
|
|
|
|
$i = 0;
|
2006-10-02 17:24:54 +00:00
|
|
|
foreach ($presets as $id => $preset) {
|
2006-12-08 19:36:00 +00:00
|
|
|
$screenshot = '';
|
2006-10-02 17:24:54 +00:00
|
|
|
if (!empty($preset->userid)) {
|
|
|
|
$user = get_record('user', 'id', $preset->userid);
|
2006-12-08 19:36:00 +00:00
|
|
|
$desc = $preset->name.' ('.fullname($user, true).')';
|
|
|
|
} else {
|
|
|
|
$desc = $preset->name;
|
2006-10-02 17:24:54 +00:00
|
|
|
}
|
2006-12-08 19:36:00 +00:00
|
|
|
|
|
|
|
if (!empty($preset->screenshot)) {
|
|
|
|
$screenshot = '<img width="150" class="presetscreenshot" src="'.$preset->screenshot.'" alt="'.get_string('screenshot').' '.$desc.'" /> ';
|
2006-10-02 17:24:54 +00:00
|
|
|
}
|
2006-08-15 08:42:06 +00:00
|
|
|
|
2006-12-08 19:36:00 +00:00
|
|
|
$fullname = $preset->userid.'/'.$preset->shortname;
|
|
|
|
|
|
|
|
$dellink = '';
|
|
|
|
if ($preset->userid > 0 and ($preset->userid == $USER->id || has_capability('mod/data:manageuserpresets', $context))) {
|
|
|
|
$dellink = ' <a href="preset.php?d='.$data->id.'&action=confirmdelete&fullname='.$fullname.'&sesskey='.sesskey().'">'.
|
2007-01-08 09:14:05 +00:00
|
|
|
'<img src="'.$CFG->pixpath.'/t/delete.gif" class="iconsmall" alt="'.$strdelete.' '.$desc.'" /></a>';
|
2006-12-08 19:36:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
echo '<input type="radio" name="fullname" id="usepreset'.$i.'" value="'.$fullname.'" /><label for="usepreset'.$i++.'">'.$desc.'</label>'.$dellink.'<br />';
|
|
|
|
}
|
|
|
|
echo '<br />';
|
|
|
|
echo '<input type="submit" value="'.$strchoose.'" />';
|
2007-01-05 08:01:33 +00:00
|
|
|
echo '</fieldset></form>';
|
2006-10-02 17:24:54 +00:00
|
|
|
echo '</td></tr>';
|
|
|
|
echo '</table>';
|
2006-12-08 19:36:00 +00:00
|
|
|
echo '</div>';
|
2006-08-15 08:42:06 +00:00
|
|
|
|
2006-10-02 17:24:54 +00:00
|
|
|
print_footer($course);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function is_directory_a_preset($directory) {
|
2006-12-08 19:36:00 +00:00
|
|
|
$directory = rtrim($directory, '/\\') . '/';
|
2006-10-02 17:24:54 +00:00
|
|
|
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') &&
|
2006-12-08 19:36:00 +00:00
|
|
|
file_exists($directory.'csstemplate.css') &&
|
|
|
|
file_exists($directory.'jstemplate.js') &&
|
2006-10-02 17:24:54 +00:00
|
|
|
file_exists($directory.'preset.xml')) return true;
|
2006-08-15 08:42:06 +00:00
|
|
|
else return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-02 17:24:54 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-15 08:42:06 +00:00
|
|
|
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');
|
2006-08-17 13:19:28 +00:00
|
|
|
$jstemplate = fopen($tempfolder.'/jstemplate.js', 'w');
|
2006-08-15 08:42:06 +00:00
|
|
|
|
|
|
|
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);
|
2006-08-17 13:19:28 +00:00
|
|
|
fwrite($jstemplate, $data->jstemplate);
|
2006-08-15 08:42:06 +00:00
|
|
|
|
|
|
|
fclose($singletemplate);
|
|
|
|
fclose($listtemplate);
|
|
|
|
fclose($listtemplateheader);
|
|
|
|
fclose($listtemplatefooter);
|
|
|
|
fclose($addtemplate);
|
|
|
|
fclose($rsstemplate);
|
|
|
|
fclose($rsstitletemplate);
|
|
|
|
fclose($csstemplate);
|
2006-08-17 13:19:28 +00:00
|
|
|
fclose($jstemplate);
|
2006-08-15 08:42:06 +00:00
|
|
|
|
|
|
|
/* 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";
|
|
|
|
|
2006-08-15 11:14:07 +00:00
|
|
|
/* Database settings first. Name not included? */
|
2006-12-08 19:36:00 +00:00
|
|
|
$settingssaved = array('intro', 'comments',
|
2006-10-02 17:24:54 +00:00
|
|
|
'requiredentries', 'requiredentriestoview', 'maxentries',
|
|
|
|
'rssarticles', 'approval', 'scale', 'assessed',
|
|
|
|
'defaultsort', 'defaultsortdir', 'editany');
|
2006-08-15 08:42:06 +00:00
|
|
|
|
2006-08-15 11:14:07 +00:00
|
|
|
$presetxml .= "<settings>\n";
|
|
|
|
foreach ($settingssaved as $setting) {
|
2006-10-02 17:24:54 +00:00
|
|
|
$presetxml .= "<$setting>{$data->$setting}</$setting>\n";
|
2006-08-15 11:14:07 +00:00
|
|
|
}
|
|
|
|
$presetxml .= "</settings>\n\n";
|
2006-08-15 08:42:06 +00:00
|
|
|
|
|
|
|
/* Now for the fields. Grabs all settings that are non-empty */
|
2006-08-15 11:14:07 +00:00
|
|
|
if (!empty($fields)) {
|
2006-10-02 17:24:54 +00:00
|
|
|
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";
|
2006-09-20 20:31:09 +00:00
|
|
|
}
|
2006-08-15 08:42:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$presetxml .= "</preset>";
|
|
|
|
fwrite($presetfile, $presetxml);
|
|
|
|
fclose($presetfile);
|
|
|
|
|
|
|
|
/* Check all is well */
|
|
|
|
if (!is_directory_a_preset($tempfolder)) {
|
|
|
|
error("Not all files generated!");
|
|
|
|
}
|
|
|
|
|
|
|
|
$filelist = array(
|
2006-10-02 17:24:54 +00:00
|
|
|
'singletemplate.html',
|
|
|
|
'listtemplate.html',
|
|
|
|
'listtemplateheader.html',
|
|
|
|
'listtemplatefooter.html',
|
|
|
|
'addtemplate.html',
|
|
|
|
'rsstemplate.html',
|
|
|
|
'rsstitletemplate.html',
|
|
|
|
'csstemplate.css',
|
|
|
|
'jstemplate.js',
|
|
|
|
'preset.xml');
|
2006-08-15 08:42:06 +00:00
|
|
|
|
|
|
|
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';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-12-08 19:36:00 +00:00
|
|
|
class PresetImporter {
|
2006-10-02 17:24:54 +00:00
|
|
|
function PresetImporter($course, $cm, $data, $userid, $shortname) {
|
2006-08-15 08:42:06 +00:00
|
|
|
global $CFG;
|
|
|
|
$this->course = $course;
|
|
|
|
$this->cm = $cm;
|
|
|
|
$this->data = $data;
|
2006-10-02 17:24:54 +00:00
|
|
|
$this->userid = $userid;
|
|
|
|
$this->shortname = $shortname;
|
|
|
|
$this->folder = data_preset_path($course, $userid, $shortname);
|
2006-08-15 08:42:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function get_settings() {
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
if (!is_directory_a_preset($this->folder)) {
|
2006-12-08 19:36:00 +00:00
|
|
|
error("$this->userid/$this->shortname Not a preset");
|
2006-08-15 08:42:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
2006-12-10 17:31:00 +00:00
|
|
|
$f->type = clean_param($f->type, PARAM_ALPHA);
|
2006-08-15 08:42:06 +00:00
|
|
|
$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");
|
2006-08-17 13:19:28 +00:00
|
|
|
$settings->jstemplate = file_get_contents($this->folder."/jstemplate.js");
|
2006-08-15 08:42:06 +00:00
|
|
|
|
|
|
|
$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');
|
2006-12-08 19:36:00 +00:00
|
|
|
$strcontinue = get_string('continue');
|
2006-08-15 08:42:06 +00:00
|
|
|
$strwarning = get_string('mappingwarning', 'data');
|
|
|
|
$strfieldmappings = get_string('fieldmappings', 'data');
|
2006-12-08 19:36:00 +00:00
|
|
|
$strnew = get_string('new');
|
|
|
|
$strold = get_string('old');
|
|
|
|
|
|
|
|
$sesskey = sesskey();
|
2006-08-15 08:42:06 +00:00
|
|
|
|
|
|
|
list($settings, $newfields, $currentfields) = $this->get_settings();
|
|
|
|
|
2007-01-05 08:01:33 +00:00
|
|
|
echo '<div style="text-align:center"><form action="preset.php" method="post">';
|
|
|
|
echo '<fieldset class="invisiblefieldset">';
|
2006-10-02 17:24:54 +00:00
|
|
|
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.'" />';
|
2006-12-08 19:36:00 +00:00
|
|
|
echo '<input type="hidden" name="fullname" value="'.$this->userid.'/'.$this->shortname.'" />';
|
2006-08-15 08:42:06 +00:00
|
|
|
|
2006-12-13 20:32:13 +00:00
|
|
|
if (!empty($currentfields) && !empty($newfields)) {
|
2006-08-15 08:42:06 +00:00
|
|
|
echo "<h3>$strfieldmappings ";
|
2006-12-08 19:36:00 +00:00
|
|
|
helpbutton('fieldmappings', '', 'data');
|
|
|
|
echo '</h3><table>';
|
2006-08-15 08:42:06 +00:00
|
|
|
|
|
|
|
foreach ($newfields as $nid => $newfield) {
|
2006-12-08 19:36:00 +00:00
|
|
|
echo "<tr><td><label for=\"id_$newfield->name\">$newfield->name</label></td>";
|
|
|
|
echo '<td><select name="field_'.$nid.'" id="id_'.$newfield->name.'">';
|
2006-08-15 08:42:06 +00:00
|
|
|
|
2006-12-08 19:36:00 +00:00
|
|
|
$selected = false;
|
2006-08-15 08:42:06 +00:00
|
|
|
foreach ($currentfields as $cid => $currentfield) {
|
|
|
|
if ($currentfield->type == $newfield->type) {
|
|
|
|
if ($currentfield->name == $newfield->name) {
|
2006-12-08 19:36:00 +00:00
|
|
|
echo '<option value="'.$cid.'" selected="selected">'.$currentfield->name.'</option>';
|
2006-08-15 08:42:06 +00:00
|
|
|
$selected=true;
|
|
|
|
}
|
2006-08-15 11:14:07 +00:00
|
|
|
else {
|
2006-12-08 19:36:00 +00:00
|
|
|
echo '<option value="$cid">'.$currentfield->name.'</option>';
|
2006-10-02 17:24:54 +00:00
|
|
|
}
|
|
|
|
}
|
2006-08-15 08:42:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($selected)
|
2006-12-08 19:36:00 +00:00
|
|
|
echo '<option value="-1">-</option>';
|
2006-08-15 08:42:06 +00:00
|
|
|
else
|
2006-12-08 19:36:00 +00:00
|
|
|
echo '<option value="-1" selected="selected">-</option>';
|
|
|
|
echo '</select></td></tr>';
|
2006-08-15 08:42:06 +00:00
|
|
|
}
|
2006-12-08 19:36:00 +00:00
|
|
|
echo '</table>';
|
2006-08-15 08:42:06 +00:00
|
|
|
echo "<p>$strwarning</p>";
|
|
|
|
}
|
2006-12-13 20:32:13 +00:00
|
|
|
else if (empty($newfields)) {
|
2006-08-15 08:42:06 +00:00
|
|
|
error("New preset has no defined fields!");
|
|
|
|
}
|
2007-01-05 08:01:33 +00:00
|
|
|
echo '<input type="submit" value="'.$strcontinue.'" /></fieldset></form></div>';
|
2006-08-15 08:42:06 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function import() {
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
list($settings, $newfields, $currentfields) = $this->get_settings();
|
|
|
|
$preservedfields = array();
|
|
|
|
|
|
|
|
/* Maps fields and makes new ones */
|
2006-12-13 20:32:13 +00:00
|
|
|
if (!empty($newfields)) {
|
2006-08-15 08:42:06 +00:00
|
|
|
/* 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");
|
|
|
|
|
2006-12-08 22:23:45 +00:00
|
|
|
if (!isset($newfield->description)) {
|
|
|
|
$newfield->description = '';
|
|
|
|
}
|
2006-08-15 08:42:06 +00:00
|
|
|
$classname = 'data_field_'.$newfield->type;
|
|
|
|
$fieldclass = new $classname($newfield, $this->data);
|
|
|
|
$fieldclass->insert_field();
|
|
|
|
unset($fieldclass);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get rid of all old unused data */
|
2006-12-13 20:32:13 +00:00
|
|
|
if (!empty($preservedfields)) {
|
2006-08-15 08:42:06 +00:00
|
|
|
foreach ($currentfields as $cid => $currentfield) {
|
|
|
|
if (!array_key_exists($cid, $preservedfields)) {
|
|
|
|
/* Data not used anymore so wipe! */
|
2006-12-08 19:36:00 +00:00
|
|
|
print "Deleting field $currentfield->name<br />";
|
2006-08-15 08:42:06 +00:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-17 13:19:28 +00:00
|
|
|
data_update_instance(addslashes_object($settings));
|
2006-10-02 17:24:54 +00:00
|
|
|
|
2006-12-08 19:36:00 +00:00
|
|
|
if (strstr($this->folder, '/temp/')) clean_preset($this->folder); /* Removes the temporary files */
|
2006-08-15 08:42:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-02 17:24:54 +00:00
|
|
|
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?';
|
|
|
|
}
|
|
|
|
|
2006-08-15 08:42:06 +00:00
|
|
|
?>
|