MDL-36453 - mod_data: Presets now load in a Windows environment.
[moodle.git] / mod / data / preset.php
CommitLineData
6111b2b0 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Preset Menu
449e6414 20 *
901dd2fb 21 * This is the page that is the menu item in the config database
22 * pages.
6111b2b0 23 *
24 * This file is part of the Database module for Moodle
25 *
26 * @copyright 2005 Martin Dougiamas http://dougiamas.com
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 * @package mod-data
901dd2fb 29 */
30
31require_once('../../config.php');
cba87c36
SH
32require_once($CFG->dirroot.'/mod/data/lib.php');
33require_once($CFG->dirroot.'/mod/data/preset_form.php');
901dd2fb 34require_once($CFG->libdir.'/xmlize.php');
35
cba87c36 36$id = optional_param('id', 0, PARAM_INT); // course module id
901dd2fb 37if ($id) {
cba87c36
SH
38 $cm = get_coursemodule_from_id('data', $id, null, null, MUST_EXIST);
39 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
40 $data = $DB->get_record('data', array('id'=>$cm->instance), '*', MUST_EXIST);
901dd2fb 41} else {
cba87c36
SH
42 $d = required_param('d', PARAM_INT); // database activity id
43 $data = $DB->get_record('data', array('id'=>$d), '*', MUST_EXIST);
44 $course = $DB->get_record('course', array('id'=>$data->course), '*', MUST_EXIST);
45 $cm = get_coursemodule_from_instance('data', $data->id, $course->id, null, MUST_EXIST);
901dd2fb 46}
541685ec
AG
47
48$context = context_module::instance($cm->id, MUST_EXIST);
cdbea7ee 49require_login($course, false, $cm);
cba87c36
SH
50require_capability('mod/data:managetemplates', $context);
51$PAGE->set_url(new moodle_url('/mod/data/preset.php', array('d'=>$data->id)));
52$PAGE->set_title(get_string('course') . ': ' . $course->fullname);
53$PAGE->set_heading($course->fullname);
901dd2fb 54
13534ef7
ML
55// fill in missing properties needed for updating of instance
56$data->course = $cm->course;
57$data->cmidnumber = $cm->idnumber;
58$data->instance = $cm->instance;
59
cba87c36 60$presets = data_get_available_presets($context);
cba87c36
SH
61$strdelete = get_string('deleted', 'data');
62foreach ($presets as &$preset) {
63 if (!empty($preset->userid)) {
64 $presetuser = $DB->get_record('user', array('id'=>$preset->userid), 'id,firstname,lastname', MUST_EXIST);
65 $preset->description = $preset->name.' ('.fullname($presetuser, true).')';
66 } else {
67 $preset->userid = 0;
68 $preset->description = $preset->name;
541685ec
AG
69 if (data_user_can_delete_preset($context, $preset) && $preset->name != 'Image gallery') {
70 $delurl = new moodle_url('/mod/data/preset.php', array('d'=> $data->id, 'action'=>'confirmdelete', 'fullname'=>$preset->userid.'/'.$preset->shortname, 'sesskey'=>sesskey()));
71 $delicon = html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/delete'), 'class'=>'iconsmall', 'alt'=>$strdelete.' '.$preset->description));
72 $preset->description .= html_writer::link($delurl, $delicon);
73 }
cba87c36 74 }
541685ec 75 if ($preset->userid > 0 && data_user_can_delete_preset($context, $preset)) {
cba87c36
SH
76 $delurl = new moodle_url('/mod/data/preset.php', array('d'=> $data->id, 'action'=>'confirmdelete', 'fullname'=>$preset->userid.'/'.$preset->shortname, 'sesskey'=>sesskey()));
77 $delicon = html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/delete'), 'class'=>'iconsmall', 'alt'=>$strdelete.' '.$preset->description));
78 $preset->description .= html_writer::link($delurl, $delicon);
79 }
8303eb84 80}
c56d266a
SH
81// This is required because its currently bound to the last element in the array.
82// If someone were to inadvently use it again and this call were not here
83unset($preset);
8303eb84 84
cba87c36
SH
85$form_importexisting = new data_existing_preset_form(null, array('presets'=>$presets));
86$form_importexisting->set_data(array('d' => $data->id));
901dd2fb 87
cba87c36
SH
88$form_importzip = new data_import_preset_zip_form();
89$form_importzip->set_data(array('d' => $data->id));
901dd2fb 90
cba87c36
SH
91$form_export = new data_export_form();
92$form_export->set_data(array('d' => $data->id));
901dd2fb 93
cba87c36
SH
94$form_save = new data_save_preset_form();
95$form_save->set_data(array('d' => $data->id, 'name'=>$data->name));
29b64a22 96
8303eb84 97/* Output */
cba87c36
SH
98if (!$form_export->is_submitted()) {
99 echo $OUTPUT->header();
100 echo $OUTPUT->heading(format_string($data->name));
101
102 // Needed for tabs.php
103 $currenttab = 'presets';
104 $currentgroup = groups_get_activity_group($cm);
105 $groupmode = groups_get_activity_groupmode($cm);
106 include('tabs.php');
5d0f79e1 107}
901dd2fb 108
cba87c36 109if (optional_param('sesskey', false, PARAM_BOOL) && confirm_sesskey()) {
901dd2fb 110
cba87c36 111 $renderer = $PAGE->get_renderer('mod_data');
8303eb84 112
cba87c36
SH
113 if ($formdata = $form_importexisting->get_data()) {
114 $importer = new data_preset_existing_importer($course, $cm, $data, $formdata->fullname);
115 echo $renderer->import_setting_mappings($data, $importer);
7b30ba8b 116 echo $OUTPUT->footer();
39b77150 117 exit(0);
cba87c36
SH
118 } else if ($formdata = $form_importzip->get_data()) {
119 $file = new stdClass;
120 $file->name = $form_importzip->get_new_filename('importfile');
121 $file->path = $form_importzip->save_temp_file('importfile');
122 $importer = new data_preset_upload_importer($course, $cm, $data, $file->path);
123 echo $renderer->import_setting_mappings($data, $importer);
7b30ba8b 124 echo $OUTPUT->footer();
5d0f79e1 125 exit(0);
cba87c36 126 } else if ($formdata = $form_export->get_data()) {
901dd2fb 127
cba87c36
SH
128 if (headers_sent()) {
129 print_error('headersent');
8303eb84 130 }
901dd2fb 131
efe05692 132 $exportfile = data_presets_export($course, $cm, $data);
5d0f79e1 133 $exportfilename = basename($exportfile);
134 header("Content-Type: application/download\n");
0170b834 135 header("Content-Disposition: attachment; filename=\"$exportfilename\"");
5d0f79e1 136 header('Expires: 0');
137 header('Cache-Control: must-revalidate,post-check=0,pre-check=0');
138 header('Pragma: public');
139 $exportfilehandler = fopen($exportfile, 'rb');
140 print fread($exportfilehandler, filesize($exportfile));
141 fclose($exportfilehandler);
142 unlink($exportfile);
143 exit(0);
8303eb84 144
cba87c36 145 } else if ($formdata = $form_save->get_data()) {
8aff1574 146 if (!empty($formdata->overwrite)) {
541685ec
AG
147 $selectedpreset = new stdClass();
148 foreach ($presets as $preset) {
149 if ($preset->name == $formdata->name) {
150 $selectedpreset = $preset;
c56d266a 151 break;
541685ec
AG
152 }
153 }
c56d266a
SH
154 if (isset($selectedpreset->name)) {
155 if (data_user_can_delete_preset($context, $selectedpreset)) {
156 data_delete_site_preset($formdata->name);
157 } else {
158 print_error('cannotoverwritepreset', 'data');
159 }
541685ec 160 }
8aff1574 161 }
8303eb84 162
8aff1574
SH
163 // If the preset exists now then we need to throw an error.
164 $sitepresets = data_get_available_site_presets($context);
165 foreach ($sitepresets as $key=>$preset) {
166 if ($formdata->name == $preset->name) {
cba87c36 167 print_error('errorpresetexists', 'preset');
cba87c36 168 }
8303eb84 169 }
8303eb84 170
8aff1574
SH
171 // Save the preset now
172 data_presets_save($course, $cm, $data, $formdata->name);
173
4102b449 174 echo $OUTPUT->notification(get_string('savesuccess', 'data'), 'notifysuccess');
cba87c36
SH
175 echo $OUTPUT->continue_button($PAGE->url);
176 echo $OUTPUT->footer();
177 exit(0);
7c6a0735 178 } else {
cba87c36
SH
179 $action = optional_param('action', null, PARAM_ALPHA);
180 $fullname = optional_param('fullname', '', PARAM_PATH); // directory the preset is in
181 //
182 // find out preset owner userid and shortname
183 $parts = explode('/', $fullname, 2);
184 $userid = empty($parts[0]) ? 0 : (int)$parts[0];
185 $shortname = empty($parts[1]) ? '' : $parts[1];
186
187 if ($userid && ($userid != $USER->id) && !has_capability('mod/data:viewalluserpresets', $context)) {
188 print_error('cannotaccesspresentsother', 'data');
189 }
190
191 if ($action == 'confirmdelete') {
192 $path = data_preset_path($course, $userid, $shortname);
193 $strwarning = get_string('deletewarning', 'data').'<br />'.$shortname;
194 $optionsyes = array('fullname' => $userid.'/'.$shortname,
195 'action' => 'delete',
196 'd' => $data->id);
197 $optionsno = array('d' => $data->id);
198 echo $OUTPUT->confirm($strwarning, new moodle_url('preset.php', $optionsyes), new moodle_url('preset.php', $optionsno));
199 echo $OUTPUT->footer();
200 exit(0);
201 } else if ($action == 'delete') {
541685ec
AG
202 $selectedpreset = new stdClass();
203 foreach ($presets as $preset) {
204 if ($preset->shortname == $shortname) {
205 $selectedpreset = $preset;
206 }
207 }
c56d266a 208 if (!isset($selectedpreset->shortname) || !data_user_can_delete_preset($context, $selectedpreset)) {
cba87c36
SH
209 print_error('invalidrequest');
210 }
7c6a0735 211
901d58df 212 data_delete_site_preset($shortname);
cba87c36
SH
213
214 $strdeleted = get_string('deleted', 'data');
215 echo $OUTPUT->notification("$shortname $strdeleted", 'notifysuccess');
216 } else if ($action == 'finishimport') {
217 $overwritesettings = optional_param('overwritesettings', false, PARAM_BOOL);
218 if (!$fullname) {
f6cdef58 219 $presetdir = $CFG->tempdir.'/forms/'.required_param('directory', PARAM_FILE);
cba87c36
SH
220 if (!file_exists($presetdir) || !is_dir($presetdir)) {
221 print_error('cannotimport');
7c6a0735 222 }
cba87c36
SH
223 $importer = new data_preset_upload_importer($course, $cm, $data, $presetdir);
224 } else {
225 $importer = new data_preset_existing_importer($course, $cm, $data, $fullname);
226 }
227 $importer->import($overwritesettings);
228 $strimportsuccess = get_string('importsuccess', 'data');
229 $straddentries = get_string('addentries', 'data');
230 $strtodatabase = get_string('todatabase', 'data');
231 if (!$DB->get_records('data_records', array('dataid'=>$data->id))) {
232 echo $OUTPUT->notification("$strimportsuccess <a href='edit.php?d=$data->id'>$straddentries</a> $strtodatabase", 'notifysuccess');
233 } else {
234 echo $OUTPUT->notification("$strimportsuccess", 'notifysuccess');
7c6a0735 235 }
7c6a0735 236 }
cba87c36
SH
237 echo $OUTPUT->continue_button($PAGE->url);
238 echo $OUTPUT->footer();
239 exit(0);
7c6a0735 240 }
7c6a0735 241}
8303eb84 242
cba87c36
SH
243// Export forms
244echo $OUTPUT->heading(get_string('export', 'data'));
245$form_export->display();
246$form_save->display();
247
248// Import forms
249echo $OUTPUT->heading(get_string('import'));
250$form_importzip->display();
251$form_importexisting->display();
1adbd2c3 252
cba87c36 253echo $OUTPUT->footer();