Merge branch 'MDL-62560-master'
[moodle.git] / mod / data / preset.php
blob9a166b2d37d6cc815b7ce050752655a77f2bbb7c
1 <?php
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.
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/>.
18 /**
19 * Preset Menu
21 * This is the page that is the menu item in the config database
22 * pages.
24 * This file is part of the Database module for Moodle
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
31 require_once('../../config.php');
32 require_once($CFG->dirroot.'/mod/data/lib.php');
33 require_once($CFG->dirroot.'/mod/data/preset_form.php');
34 require_once($CFG->libdir.'/xmlize.php');
36 $id = optional_param('id', 0, PARAM_INT); // course module id
37 if ($id) {
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);
41 } else {
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);
48 $context = context_module::instance($cm->id, MUST_EXIST);
49 require_login($course, false, $cm);
50 require_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);
54 $PAGE->force_settings_menu(true);
56 // fill in missing properties needed for updating of instance
57 $data->course = $cm->course;
58 $data->cmidnumber = $cm->idnumber;
59 $data->instance = $cm->instance;
61 $presets = data_get_available_presets($context);
62 $strdelete = get_string('deleted', 'data');
63 foreach ($presets as &$preset) {
64 if (!empty($preset->userid)) {
65 $namefields = get_all_user_name_fields(true);
66 $presetuser = $DB->get_record('user', array('id' => $preset->userid), 'id, ' . $namefields, MUST_EXIST);
67 $preset->description = $preset->name.' ('.fullname($presetuser, true).')';
68 } else {
69 $preset->userid = 0;
70 $preset->description = $preset->name;
71 if (data_user_can_delete_preset($context, $preset) && $preset->name != 'Image gallery') {
72 $delurl = new moodle_url('/mod/data/preset.php', array('d'=> $data->id, 'action'=>'confirmdelete', 'fullname'=>$preset->userid.'/'.$preset->shortname, 'sesskey'=>sesskey()));
73 $delicon = $OUTPUT->pix_icon('t/delete', $strdelete . ' ' . $preset->description);
74 $preset->description .= html_writer::link($delurl, $delicon);
77 if ($preset->userid > 0 && data_user_can_delete_preset($context, $preset)) {
78 $delurl = new moodle_url('/mod/data/preset.php', array('d'=> $data->id, 'action'=>'confirmdelete', 'fullname'=>$preset->userid.'/'.$preset->shortname, 'sesskey'=>sesskey()));
79 $delicon = $OUTPUT->pix_icon('t/delete', $strdelete . ' ' . $preset->description);
80 $preset->description .= html_writer::link($delurl, $delicon);
83 // This is required because its currently bound to the last element in the array.
84 // If someone were to inadvently use it again and this call were not here
85 unset($preset);
87 $form_importexisting = new data_existing_preset_form(null, array('presets'=>$presets));
88 $form_importexisting->set_data(array('d' => $data->id));
90 $form_importzip = new data_import_preset_zip_form();
91 $form_importzip->set_data(array('d' => $data->id));
93 $form_export = new data_export_form();
94 $form_export->set_data(array('d' => $data->id));
96 $form_save = new data_save_preset_form();
97 $form_save->set_data(array('d' => $data->id, 'name'=>$data->name));
99 /* Output */
100 if (!$form_export->is_submitted()) {
101 echo $OUTPUT->header();
102 echo $OUTPUT->heading(format_string($data->name), 2);
104 // Needed for tabs.php
105 $currenttab = 'presets';
106 $currentgroup = groups_get_activity_group($cm);
107 $groupmode = groups_get_activity_groupmode($cm);
108 echo $OUTPUT->box(format_module_intro('data', $data, $cm->id), 'generalbox', 'intro');
110 include('tabs.php');
113 if (optional_param('sesskey', false, PARAM_BOOL) && confirm_sesskey()) {
115 $renderer = $PAGE->get_renderer('mod_data');
117 if ($formdata = $form_importexisting->get_data()) {
118 $importer = new data_preset_existing_importer($course, $cm, $data, $formdata->fullname);
119 echo $renderer->import_setting_mappings($data, $importer);
120 echo $OUTPUT->footer();
121 exit(0);
122 } else if ($formdata = $form_importzip->get_data()) {
123 $file = new stdClass;
124 $file->name = $form_importzip->get_new_filename('importfile');
125 $file->path = $form_importzip->save_temp_file('importfile');
126 $importer = new data_preset_upload_importer($course, $cm, $data, $file->path);
127 echo $renderer->import_setting_mappings($data, $importer);
128 echo $OUTPUT->footer();
129 exit(0);
130 } else if ($formdata = $form_export->get_data()) {
132 if (headers_sent()) {
133 print_error('headersent');
136 $exportfile = data_presets_export($course, $cm, $data);
137 $exportfilename = basename($exportfile);
138 header("Content-Type: application/download\n");
139 header("Content-Disposition: attachment; filename=\"$exportfilename\"");
140 header('Expires: 0');
141 header('Cache-Control: must-revalidate,post-check=0,pre-check=0');
142 header('Pragma: public');
143 $exportfilehandler = fopen($exportfile, 'rb');
144 print fread($exportfilehandler, filesize($exportfile));
145 fclose($exportfilehandler);
146 unlink($exportfile);
147 exit(0);
149 } else if ($formdata = $form_save->get_data()) {
150 if (!empty($formdata->overwrite)) {
151 $selectedpreset = new stdClass();
152 foreach ($presets as $preset) {
153 if ($preset->name == $formdata->name) {
154 $selectedpreset = $preset;
155 break;
158 if (isset($selectedpreset->name)) {
159 if (data_user_can_delete_preset($context, $selectedpreset)) {
160 data_delete_site_preset($formdata->name);
161 } else {
162 print_error('cannotoverwritepreset', 'data');
167 // If the preset exists now then we need to throw an error.
168 $sitepresets = data_get_available_site_presets($context);
169 foreach ($sitepresets as $key=>$preset) {
170 if ($formdata->name == $preset->name) {
171 print_error('errorpresetexists', 'preset');
175 // Save the preset now
176 data_presets_save($course, $cm, $data, $formdata->name);
178 echo $OUTPUT->notification(get_string('savesuccess', 'data'), 'notifysuccess');
179 echo $OUTPUT->continue_button($PAGE->url);
180 echo $OUTPUT->footer();
181 exit(0);
182 } else {
183 $action = optional_param('action', null, PARAM_ALPHANUM);
184 $fullname = optional_param('fullname', '', PARAM_PATH); // directory the preset is in
186 // find out preset owner userid and shortname
187 $parts = explode('/', $fullname, 2);
188 $userid = empty($parts[0]) ? 0 : (int)$parts[0];
189 $shortname = empty($parts[1]) ? '' : $parts[1];
191 if ($userid && ($userid != $USER->id) && !has_capability('mod/data:viewalluserpresets', $context)) {
192 print_error('cannotaccesspresentsother', 'data');
195 if ($action == 'confirmdelete') {
196 $path = data_preset_path($course, $userid, $shortname);
197 $strwarning = get_string('deletewarning', 'data').'<br />'.$shortname;
198 $optionsyes = array('fullname' => $userid.'/'.$shortname,
199 'action' => 'delete',
200 'd' => $data->id);
201 $optionsno = array('d' => $data->id);
202 echo $OUTPUT->confirm($strwarning, new moodle_url('preset.php', $optionsyes), new moodle_url('preset.php', $optionsno));
203 echo $OUTPUT->footer();
204 exit(0);
205 } else if ($action == 'delete') {
206 $selectedpreset = new stdClass();
207 foreach ($presets as $preset) {
208 if ($preset->shortname == $shortname) {
209 $selectedpreset = $preset;
212 if (!isset($selectedpreset->shortname) || !data_user_can_delete_preset($context, $selectedpreset)) {
213 print_error('invalidrequest');
216 data_delete_site_preset($shortname);
218 $strdeleted = get_string('deleted', 'data');
219 echo $OUTPUT->notification("$shortname $strdeleted", 'notifysuccess');
220 } else if ($action == 'finishimport') {
221 $overwritesettings = optional_param('overwritesettings', false, PARAM_BOOL);
222 if (!$fullname) {
223 $presetdir = $CFG->tempdir.'/forms/'.required_param('directory', PARAM_FILE);
224 if (!file_exists($presetdir) || !is_dir($presetdir)) {
225 print_error('cannotimport');
227 $importer = new data_preset_upload_importer($course, $cm, $data, $presetdir);
228 } else {
229 $importer = new data_preset_existing_importer($course, $cm, $data, $fullname);
231 $importer->import($overwritesettings);
232 $strimportsuccess = get_string('importsuccess', 'data');
233 $straddentries = get_string('addentries', 'data');
234 $strtodatabase = get_string('todatabase', 'data');
235 if (!$DB->get_records('data_records', array('dataid'=>$data->id))) {
236 echo $OUTPUT->notification("$strimportsuccess <a href='edit.php?d=$data->id'>$straddentries</a> $strtodatabase", 'notifysuccess');
237 } else {
238 echo $OUTPUT->notification("$strimportsuccess", 'notifysuccess');
241 echo $OUTPUT->continue_button($PAGE->url);
242 echo $OUTPUT->footer();
243 exit(0);
247 // Export forms
248 echo $OUTPUT->heading(get_string('export', 'data'), 3);
249 $form_export->display();
250 $form_save->display();
252 // Import forms
253 echo $OUTPUT->heading(get_string('import'), 3);
254 $form_importzip->display();
255 $form_importexisting->display();
257 echo $OUTPUT->footer();