.
/**
* Preset Menu
*
* This is the page that is the menu item in the config database
* pages.
*
* This file is part of the Database module for Moodle
*
* @copyright 2005 Martin Dougiamas http://dougiamas.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package mod-data
*/
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_ALPHANUM); // current action
$fullname = optional_param('fullname', '', PARAM_PATH); // directory the preset is in
$file = optional_param('file', '', PARAM_PATH); // uploaded file
$url = new moodle_url('/mod/data/preset.php');
if ($action !== 'base') {
$url->param('action', $action);
}
if ($fullname !== '') {
$url->param('fullname', $fullname);
}
if ($file !== '') {
$url->param('file', $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);
if ($id) {
$url->param('id', $id);
$PAGE->set_url($url);
if (! $cm = get_coursemodule_from_id('data', $id)) {
print_error('invalidcoursemodule');
}
if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
print_error('coursemisconf');
}
if (! $data = $DB->get_record('data', array('id'=>$cm->instance))) {
print_error('invalidid', 'data');
}
} else if ($d) {
$url->param('d', $d);
$PAGE->set_url($url);
if (! $data = $DB->get_record('data', array('id'=>$d))) {
print_error('invalidid', 'data');
}
if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
print_error('coursemisconf');
}
if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
print_error('invalidcoursemodule');
}
} else {
print_error('missingparameter');
}
// fill in missing properties needed for updating of instance
$data->course = $cm->course;
$data->cmidnumber = $cm->idnumber;
$data->instance = $cm->instance;
if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
print_error('cannotfindcontext');
}
require_login($course->id, false, $cm);
require_capability('mod/data:managetemplates', $context);
if ($userid && ($userid != $USER->id) && !has_capability('mod/data:viewalluserpresets', $context)) {
print_error('cannotaccesspresentsother', 'data');
}
/* Need sesskey security check here for import instruction */
$sesskey = sesskey();
$PAGE->navbar->add(get_string('presets', 'data'));
/********************************************************************/
/* Output */
if ($action !== 'export') {
data_print_header($course, $cm, $data, 'presets');
}
switch ($action) {
/***************** Deleting *****************/
case 'confirmdelete' :
if (!confirm_sesskey()) { // GET request ok here
print_error('invalidsesskey');
}
if ($userid > 0 and ($userid == $USER->id || has_capability('mod/data:manageuserpresets', $context))) {
//ok can delete
} else {
print_error('invalidrequest');
}
$path = data_preset_path($course, $userid, $shortname);
$strwarning = get_string('deletewarning', 'data').'
'.
data_preset_name($shortname, $path);
$optionsyes = array('fullname' => $userid.'/'.$shortname,
'action' => 'delete',
'd' => $data->id);
$optionsno = array('d' => $data->id);
echo $OUTPUT->confirm($strwarning, new moodle_url('preset.php', $optionsyes), new moodle_url('preset.php', $optionsno));
echo $OUTPUT->footer();
exit(0);
break;
case 'delete' :
if (!data_submitted() and !confirm_sesskey()) {
print_error('invalidrequest');
}
if ($userid > 0 and ($userid == $USER->id || has_capability('mod/data:manageuserpresets', $context))) {
//ok can delete
} else {
print_error('invalidrequest');
}
$presetpath = data_preset_path($course, $userid, $shortname);
if (!clean_preset($presetpath)) {
print_error('cannotdeletepreset', 'data');
}
@rmdir($presetpath);
$strdeleted = get_string('deleted', 'data');
echo $OUTPUT->notification("$shortname $strdeleted", 'notifysuccess');
break;
/***************** Importing *****************/
case 'importpreset' :
if (!data_submitted() or !confirm_sesskey()) {
print_error('invalidrequest');
}
$pimporter = new PresetImporter($course, $cm, $data, $userid, $shortname);
$pimporter->import_options();
echo $OUTPUT->footer();
exit(0);
break;
/* Imports a zip file. */
case 'importzip' :
if (!data_submitted() or !confirm_sesskey()) {
print_error('invalidrequest');
}
if (!make_upload_directory('temp/data/'.$USER->id)) {
print_error('nopermissiontomkdir');
}
$presetfile = $CFG->dataroot.'/temp/data/'.$USER->id;
clean_preset($presetfile);
if (!unzip_file($CFG->dataroot."/$course->id/$file", $presetfile, false)) {
print_error('cannotunzipfile');
}
$pimporter = new PresetImporter($course, $cm, $data, -$USER->id, $shortname);
$pimporter->import_options();
echo $OUTPUT->footer();
exit(0);
break;
case 'finishimport':
if (!data_submitted() or !confirm_sesskey()) {
print_error('invalidrequest');
}
$pimporter = new PresetImporter($course, $cm, $data, $userid, $shortname);
$pimporter->import();
$strimportsuccess = get_string('importsuccess', 'data');
$straddentries = get_string('addentries', 'data');
$strtodatabase = get_string('todatabase', 'data');
if (!$DB->get_records('data_records', array('dataid'=>$data->id))) {
echo $OUTPUT->notification("$strimportsuccess $straddentries $strtodatabase", 'notifysuccess');
} else {
echo $OUTPUT->notification("$strimportsuccess", 'notifysuccess');
}
break;
/* Exports as a zip file ready for download. */
case 'export':
if (!data_submitted() or !confirm_sesskey()) {
print_error('invalidrequest');
}
$exportfile = data_presets_export($course, $cm, $data);
$exportfilename = basename($exportfile);
header("Content-Type: application/download\n");
header("Content-Disposition: attachment; filename=$exportfilename");
header('Expires: 0');
header('Cache-Control: must-revalidate,post-check=0,pre-check=0');
header('Pragma: public');
$exportfilehandler = fopen($exportfile, 'rb');
print fread($exportfilehandler, filesize($exportfile));
fclose($exportfilehandler);
unlink($exportfile);
exit(0);
break;
/***************** Exporting *****************/
case 'save1':
if (!data_submitted() or !confirm_sesskey()) {
print_error('invalidrequest');
}
$strcontinue = get_string('continue');
$strwarning = get_string('presetinfo', 'data');
$strname = get_string('shortname');
echo '
'.$strwarning.'
'; echo ''.$strexport.' | |
'; echo $OUTPUT->help_icon('exportzip', get_string('help'), 'data', false); echo ' | '; $options = array(); $options['sesskey'] = sesskey(); $options['action'] = 'export'; $options['d'] = $data->id; echo $OUTPUT->single_button(new moodle_url('preset.php', $options), $strexport); echo ' |
'; echo $OUTPUT->help_icon('savepreset', get_string('help'), 'data', false); echo ' | '; $options = array(); $options['sesskey'] = sesskey(); $options['action'] = 'save1'; $options['d'] = $data->id; echo $OUTPUT->single_button(new moodle_url('preset.php', $options), $strsave); echo ' |
'.$strimport.' | |
'; echo $OUTPUT->help_icon('importfromfile', get_string('help'), 'data', true); echo ' | '; echo ''; echo ' |
'; echo $OUTPUT->help_icon('usepreset', get_string('help'), 'data', true); echo ' | '; echo ''; echo ' |