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 use mod_data\local\importer\preset_importer
;
32 use mod_data\local\importer\preset_upload_importer
;
35 use mod_data\output\action_bar
;
36 use mod_data\output\preset_preview
;
38 require_once('../../config.php');
39 require_once($CFG->dirroot
.'/mod/data/lib.php');
40 require_once($CFG->dirroot
.'/mod/data/preset_form.php');
42 // The course module id.
43 $id = optional_param('id', 0, PARAM_INT
);
47 list($course, $cm) = get_course_and_cm_from_cmid($id, manager
::MODULE
);
48 $manager = manager
::create_from_coursemodule($cm);
49 $data = $manager->get_instance();
51 // We must have the database activity id.
52 $d = required_param('d', PARAM_INT
);
53 $data = $DB->get_record('data', ['id' => $d], '*', MUST_EXIST
);
54 $manager = manager
::create_from_instance($data);
55 $cm = $manager->get_coursemodule();
56 $course = get_course($cm->course
);
59 $action = optional_param('action', 'view', PARAM_ALPHA
); // The page action.
60 $allowedactions = ['view', 'importzip', 'finishimport',
62 if (!in_array($action, $allowedactions)) {
63 throw new moodle_exception('invalidaccess');
66 $context = $manager->get_context();
68 require_login($course, false, $cm);
69 require_capability('mod/data:managetemplates', $context);
71 $url = new moodle_url('/mod/data/preset.php', array('d' => $data->id
));
73 $PAGE->add_body_class('mediumwidth');
76 get_string('presets', 'data'),
77 format_string($cm->name
),
78 format_string($course->fullname
),
80 $PAGE->set_title(implode(moodle_page
::TITLE_SEPARATOR
, $titleparts));
81 $PAGE->set_heading($course->fullname
);
82 $PAGE->force_settings_menu(true);
83 $PAGE->activityheader
->disable();
84 $PAGE->requires
->js_call_amd('mod_data/deletepreset', 'init');
86 // fill in missing properties needed for updating of instance
87 $data->course
= $cm->course
;
88 $data->cmidnumber
= $cm->idnumber
;
89 $data->instance
= $cm->instance
;
91 $renderer = $manager->get_renderer();
92 $presets = $manager->get_available_presets();
94 if ($action === 'export') {
96 throw new \
moodle_exception('headersent');
99 // Check if we should export a given preset or the current one.
100 $presetname = optional_param('presetname', $data->name
, PARAM_FILE
);
102 $preset = preset
::create_from_instance($manager, $presetname);
103 $exportfile = $preset->export();
104 $exportfilename = basename($exportfile);
105 header("Content-Type: application/download\n");
106 header("Content-Disposition: attachment; filename=\"$exportfilename\"");
107 header('Expires: 0');
108 header('Cache-Control: must-revalidate,post-check=0,pre-check=0');
109 header('Pragma: public');
111 // If this file was requested from a form, then mark download as complete.
112 \core_form\util
::form_download_complete();
114 $exportfilehandler = fopen($exportfile, 'rb');
115 print fread($exportfilehandler, filesize($exportfile));
116 fclose($exportfilehandler);
122 if ($action == 'importzip') {
123 $filepath = optional_param('filepath', '', PARAM_PATH
);
124 $importer = new preset_upload_importer($manager, $CFG->tempdir
. $filepath);
125 if ($importer->needs_mapping()) {
126 echo $OUTPUT->header();
127 echo $OUTPUT->heading(get_string('fieldmappings', 'data'), 2, 'mb-4');
128 echo $renderer->importing_preset($data, $importer);
129 echo $OUTPUT->footer();
132 $importer->import(false);
133 core\notification
::success(get_string('importsuccess', 'mod_data'));
134 redirect(new moodle_url('/mod/data/field.php', ['id' => $cm->id
]));
138 // Preset preview injects CSS and JS to the page and should be done before the page header.
139 if ($action === 'preview') {
140 $fullname = optional_param('fullname', '', PARAM_PATH
); // The directory the preset is in.
141 $templatename = optional_param('template', 'listtemplate', PARAM_ALPHA
);
142 // Find out preset owner userid and shortname.
143 $preset = preset
::create_from_fullname($manager, $fullname);
144 // Validate if the user can view this preset.
145 if (!$manager->can_view_preset($preset)) {
146 throw new \
moodle_exception('cannotaccesspresentsother', manager
::PLUGINNAME
);
148 $preview = new preset_preview($manager, $preset, $templatename);
149 $preview->prepare_page($PAGE);
151 'fullname' => $fullname,
152 'template' => $templatename,
154 $PAGE->set_url($url);
156 get_string('preview', 'data', $preset->name
),
157 format_string($cm->name
),
158 format_string($course->fullname
),
160 $PAGE->set_title(implode(moodle_page
::TITLE_SEPARATOR
, $titleparts));
161 // Print the preview screen.
162 echo $OUTPUT->header();
163 $actionbar = new action_bar($data->id
, $url);
164 echo $actionbar->get_presets_preview_action_bar($manager, $fullname, $templatename);
165 echo $renderer->render($preview);
166 echo $OUTPUT->footer();
170 if ($action === 'finishimport') {
171 if (!confirm_sesskey()) {
172 throw new moodle_exception('invalidsesskey');
174 $overwritesettings = optional_param('overwritesettings', false, PARAM_BOOL
);
175 $importer = preset_importer
::create_from_parameters($manager);
176 $importer->finish_import_process($overwritesettings, $data);
179 echo $OUTPUT->header();
181 $actionbar = new \mod_data\output\action_bar
($data->id
, $url);
182 echo $actionbar->get_presets_action_bar();
183 $presets = new \mod_data\output\
presets($manager, $presets, new \
moodle_url('/mod/data/field.php'), true);
184 echo $renderer->render_presets($presets);
186 echo $OUTPUT->footer();