3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
21 * This is the page that is the menu item in the config database
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
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
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
);
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).')';
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
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
));
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');
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();
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();
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');
144 // If this file was requested from a form, then mark download as complete.
145 \core_form\util
::form_download_complete();
147 $exportfilehandler = fopen($exportfile, 'rb');
148 print fread($exportfilehandler, filesize($exportfile));
149 fclose($exportfilehandler);
153 } else if ($formdata = $form_save->get_data()) {
154 if (!empty($formdata->overwrite
)) {
155 $selectedpreset = new stdClass();
156 foreach ($presets as $preset) {
157 if ($preset->name
== $formdata->name
) {
158 $selectedpreset = $preset;
162 if (isset($selectedpreset->name
)) {
163 if (data_user_can_delete_preset($context, $selectedpreset)) {
164 data_delete_site_preset($formdata->name
);
166 print_error('cannotoverwritepreset', 'data');
171 // If the preset exists now then we need to throw an error.
172 $sitepresets = data_get_available_site_presets($context);
173 foreach ($sitepresets as $key=>$preset) {
174 if ($formdata->name
== $preset->name
) {
175 print_error('errorpresetexists', 'preset');
179 // Save the preset now
180 data_presets_save($course, $cm, $data, $formdata->name
);
182 echo $OUTPUT->notification(get_string('savesuccess', 'data'), 'notifysuccess');
183 echo $OUTPUT->continue_button($PAGE->url
);
184 echo $OUTPUT->footer();
187 $action = optional_param('action', null, PARAM_ALPHANUM
);
188 $fullname = optional_param('fullname', '', PARAM_PATH
); // directory the preset is in
190 // find out preset owner userid and shortname
191 $parts = explode('/', $fullname, 2);
192 $userid = empty($parts[0]) ?
0 : (int)$parts[0];
193 $shortname = empty($parts[1]) ?
'' : $parts[1];
195 if ($userid && ($userid != $USER->id
) && !has_capability('mod/data:viewalluserpresets', $context)) {
196 print_error('cannotaccesspresentsother', 'data');
199 if ($action == 'confirmdelete') {
200 $path = data_preset_path($course, $userid, $shortname);
201 $strwarning = get_string('deletewarning', 'data').'<br />'.$shortname;
202 $optionsyes = array('fullname' => $userid.'/'.$shortname,
203 'action' => 'delete',
205 $optionsno = array('d' => $data->id
);
206 echo $OUTPUT->confirm($strwarning, new moodle_url('preset.php', $optionsyes), new moodle_url('preset.php', $optionsno));
207 echo $OUTPUT->footer();
209 } else if ($action == 'delete') {
210 $selectedpreset = new stdClass();
211 foreach ($presets as $preset) {
212 if ($preset->shortname
== $shortname) {
213 $selectedpreset = $preset;
216 if (!isset($selectedpreset->shortname
) ||
!data_user_can_delete_preset($context, $selectedpreset)) {
217 print_error('invalidrequest');
220 data_delete_site_preset($shortname);
222 $strdeleted = get_string('deleted', 'data');
223 echo $OUTPUT->notification("$shortname $strdeleted", 'notifysuccess');
224 } else if ($action == 'finishimport') {
225 $overwritesettings = optional_param('overwritesettings', false, PARAM_BOOL
);
227 $presetdir = $CFG->tempdir
.'/forms/'.required_param('directory', PARAM_FILE
);
228 if (!file_exists($presetdir) ||
!is_dir($presetdir)) {
229 print_error('cannotimport');
231 $importer = new data_preset_upload_importer($course, $cm, $data, $presetdir);
233 $importer = new data_preset_existing_importer($course, $cm, $data, $fullname);
235 $importer->import($overwritesettings);
236 $strimportsuccess = get_string('importsuccess', 'data');
237 $straddentries = get_string('addentries', 'data');
238 $strtodatabase = get_string('todatabase', 'data');
239 if (!$DB->get_records('data_records', array('dataid'=>$data->id
))) {
240 echo $OUTPUT->notification("$strimportsuccess <a href='edit.php?d=$data->id'>$straddentries</a> $strtodatabase", 'notifysuccess');
242 echo $OUTPUT->notification("$strimportsuccess", 'notifysuccess');
245 echo $OUTPUT->continue_button($PAGE->url
);
246 echo $OUTPUT->footer();
252 echo $OUTPUT->heading(get_string('export', 'data'), 3);
253 $form_export->display();
254 $form_save->display();
257 echo $OUTPUT->heading(get_string('import'), 3);
258 $form_importzip->display();
259 $form_importexisting->display();
261 echo $OUTPUT->footer();