MDL-57920 mod_data: Refactor search array creation
[moodle.git] / mod / data / preset.php
blob5cb45e95090a0b683525856270d64a3fe6c8ca0a
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);
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;
60 $presets = data_get_available_presets($context);
61 $strdelete = get_string('deleted', 'data');
62 foreach ($presets as &$preset) {
63 if (!empty($preset->userid)) {
64 $namefields = get_all_user_name_fields(true);
65 $presetuser = $DB->get_record('user', array('id' => $preset->userid), 'id, ' . $namefields, MUST_EXIST);
66 $preset->description = $preset->name.' ('.fullname($presetuser, true).')';
67 } else {
68 $preset->userid = 0;
69 $preset->description = $preset->name;
70 if (data_user_can_delete_preset($context, $preset) && $preset->name != 'Image gallery') {
71 $delurl = new moodle_url('/mod/data/preset.php', array('d'=> $data->id, 'action'=>'confirmdelete', 'fullname'=>$preset->userid.'/'.$preset->shortname, 'sesskey'=>sesskey()));
72 $delicon = $OUTPUT->pix_icon('t/delete', $strdelete . ' ' . $preset->description);
73 $preset->description .= html_writer::link($delurl, $delicon);
76 if ($preset->userid > 0 && data_user_can_delete_preset($context, $preset)) {
77 $delurl = new moodle_url('/mod/data/preset.php', array('d'=> $data->id, 'action'=>'confirmdelete', 'fullname'=>$preset->userid.'/'.$preset->shortname, 'sesskey'=>sesskey()));
78 $delicon = $OUTPUT->pix_icon('t/delete', $strdelete . ' ' . $preset->description);
79 $preset->description .= html_writer::link($delurl, $delicon);
82 // This is required because its currently bound to the last element in the array.
83 // If someone were to inadvently use it again and this call were not here
84 unset($preset);
86 $form_importexisting = new data_existing_preset_form(null, array('presets'=>$presets));
87 $form_importexisting->set_data(array('d' => $data->id));
89 $form_importzip = new data_import_preset_zip_form();
90 $form_importzip->set_data(array('d' => $data->id));
92 $form_export = new data_export_form();
93 $form_export->set_data(array('d' => $data->id));
95 $form_save = new data_save_preset_form();
96 $form_save->set_data(array('d' => $data->id, 'name'=>$data->name));
98 /* Output */
99 if (!$form_export->is_submitted()) {
100 echo $OUTPUT->header();
101 echo $OUTPUT->heading(format_string($data->name), 2);
103 // Needed for tabs.php
104 $currenttab = 'presets';
105 $currentgroup = groups_get_activity_group($cm);
106 $groupmode = groups_get_activity_groupmode($cm);
107 echo $OUTPUT->box(format_module_intro('data', $data, $cm->id), 'generalbox', 'intro');
109 include('tabs.php');
112 if (optional_param('sesskey', false, PARAM_BOOL) && confirm_sesskey()) {
114 $renderer = $PAGE->get_renderer('mod_data');
116 if ($formdata = $form_importexisting->get_data()) {
117 $importer = new data_preset_existing_importer($course, $cm, $data, $formdata->fullname);
118 echo $renderer->import_setting_mappings($data, $importer);
119 echo $OUTPUT->footer();
120 exit(0);
121 } else if ($formdata = $form_importzip->get_data()) {
122 $file = new stdClass;
123 $file->name = $form_importzip->get_new_filename('importfile');
124 $file->path = $form_importzip->save_temp_file('importfile');
125 $importer = new data_preset_upload_importer($course, $cm, $data, $file->path);
126 echo $renderer->import_setting_mappings($data, $importer);
127 echo $OUTPUT->footer();
128 exit(0);
129 } else if ($formdata = $form_export->get_data()) {
131 if (headers_sent()) {
132 print_error('headersent');
135 $exportfile = data_presets_export($course, $cm, $data);
136 $exportfilename = basename($exportfile);
137 header("Content-Type: application/download\n");
138 header("Content-Disposition: attachment; filename=\"$exportfilename\"");
139 header('Expires: 0');
140 header('Cache-Control: must-revalidate,post-check=0,pre-check=0');
141 header('Pragma: public');
142 $exportfilehandler = fopen($exportfile, 'rb');
143 print fread($exportfilehandler, filesize($exportfile));
144 fclose($exportfilehandler);
145 unlink($exportfile);
146 exit(0);
148 } else if ($formdata = $form_save->get_data()) {
149 if (!empty($formdata->overwrite)) {
150 $selectedpreset = new stdClass();
151 foreach ($presets as $preset) {
152 if ($preset->name == $formdata->name) {
153 $selectedpreset = $preset;
154 break;
157 if (isset($selectedpreset->name)) {
158 if (data_user_can_delete_preset($context, $selectedpreset)) {
159 data_delete_site_preset($formdata->name);
160 } else {
161 print_error('cannotoverwritepreset', 'data');
166 // If the preset exists now then we need to throw an error.
167 $sitepresets = data_get_available_site_presets($context);
168 foreach ($sitepresets as $key=>$preset) {
169 if ($formdata->name == $preset->name) {
170 print_error('errorpresetexists', 'preset');
174 // Save the preset now
175 data_presets_save($course, $cm, $data, $formdata->name);
177 echo $OUTPUT->notification(get_string('savesuccess', 'data'), 'notifysuccess');
178 echo $OUTPUT->continue_button($PAGE->url);
179 echo $OUTPUT->footer();
180 exit(0);
181 } else {
182 $action = optional_param('action', null, PARAM_ALPHANUM);
183 $fullname = optional_param('fullname', '', PARAM_PATH); // directory the preset is in
185 // find out preset owner userid and shortname
186 $parts = explode('/', $fullname, 2);
187 $userid = empty($parts[0]) ? 0 : (int)$parts[0];
188 $shortname = empty($parts[1]) ? '' : $parts[1];
190 if ($userid && ($userid != $USER->id) && !has_capability('mod/data:viewalluserpresets', $context)) {
191 print_error('cannotaccesspresentsother', 'data');
194 if ($action == 'confirmdelete') {
195 $path = data_preset_path($course, $userid, $shortname);
196 $strwarning = get_string('deletewarning', 'data').'<br />'.$shortname;
197 $optionsyes = array('fullname' => $userid.'/'.$shortname,
198 'action' => 'delete',
199 'd' => $data->id);
200 $optionsno = array('d' => $data->id);
201 echo $OUTPUT->confirm($strwarning, new moodle_url('preset.php', $optionsyes), new moodle_url('preset.php', $optionsno));
202 echo $OUTPUT->footer();
203 exit(0);
204 } else if ($action == 'delete') {
205 $selectedpreset = new stdClass();
206 foreach ($presets as $preset) {
207 if ($preset->shortname == $shortname) {
208 $selectedpreset = $preset;
211 if (!isset($selectedpreset->shortname) || !data_user_can_delete_preset($context, $selectedpreset)) {
212 print_error('invalidrequest');
215 data_delete_site_preset($shortname);
217 $strdeleted = get_string('deleted', 'data');
218 echo $OUTPUT->notification("$shortname $strdeleted", 'notifysuccess');
219 } else if ($action == 'finishimport') {
220 $overwritesettings = optional_param('overwritesettings', false, PARAM_BOOL);
221 if (!$fullname) {
222 $presetdir = $CFG->tempdir.'/forms/'.required_param('directory', PARAM_FILE);
223 if (!file_exists($presetdir) || !is_dir($presetdir)) {
224 print_error('cannotimport');
226 $importer = new data_preset_upload_importer($course, $cm, $data, $presetdir);
227 } else {
228 $importer = new data_preset_existing_importer($course, $cm, $data, $fullname);
230 $importer->import($overwritesettings);
231 $strimportsuccess = get_string('importsuccess', 'data');
232 $straddentries = get_string('addentries', 'data');
233 $strtodatabase = get_string('todatabase', 'data');
234 if (!$DB->get_records('data_records', array('dataid'=>$data->id))) {
235 echo $OUTPUT->notification("$strimportsuccess <a href='edit.php?d=$data->id'>$straddentries</a> $strtodatabase", 'notifysuccess');
236 } else {
237 echo $OUTPUT->notification("$strimportsuccess", 'notifysuccess');
240 echo $OUTPUT->continue_button($PAGE->url);
241 echo $OUTPUT->footer();
242 exit(0);
246 // Export forms
247 echo $OUTPUT->heading(get_string('export', 'data'), 3);
248 $form_export->display();
249 $form_save->display();
251 // Import forms
252 echo $OUTPUT->heading(get_string('import'), 3);
253 $form_importzip->display();
254 $form_importexisting->display();
256 echo $OUTPUT->footer();