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/>.
19 * This file is part of the Database module for Moodle
21 * @copyright 2005 Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 require_once('../../config.php');
29 require_once('lib.php');
31 $id = optional_param('id', 0, PARAM_INT
); // course module id
32 $d = optional_param('d', 0, PARAM_INT
); // database id
33 $mode = optional_param('mode', 'addtemplate', PARAM_ALPHA
);
34 $action = optional_param('action', '', PARAM_ALPHA
);
35 $useeditor = optional_param('useeditor', null, PARAM_BOOL
);
37 $url = new moodle_url('/mod/data/templates.php');
40 list($course, $cm) = get_course_and_cm_from_cmid($id, manager
::MODULE
);
41 $manager = manager
::create_from_coursemodule($cm);
42 $url->param('d', $cm->instance
);
43 } else { // We must have $d.
44 $instance = $DB->get_record('data', ['id' => $d], '*', MUST_EXIST
);
45 $manager = manager
::create_from_instance($instance);
46 $cm = $manager->get_coursemodule();
47 $course = get_course($cm->course
);
51 $instance = $manager->get_instance();
52 $context = $manager->get_context();
54 $url->param('mode', $mode);
57 require_login($course, false, $cm);
58 require_capability('mod/data:managetemplates', $context);
60 if ($action == 'resetalltemplates') {
62 $manager->reset_all_templates();
63 redirect($PAGE->url
, get_string('templateresetall', 'mod_data'), null, \core\output\notification
::NOTIFY_SUCCESS
);
66 $manager->set_template_viewed();
68 if ($useeditor !== null) {
69 // The useeditor param was set. Update the value for this template.
70 data_set_config($instance, "editor_{$mode}", !!$useeditor);
73 $PAGE->requires
->js('/mod/data/data.js');
75 case 'asearchtemplate':
76 $title = get_string('asearchtemplate', 'data');
79 $title = get_string('csstemplate', 'data');
82 $title = get_string('jstemplate', 'data');
85 $title = get_string('listtemplate', 'data');
88 $title = get_string('rsstemplate', 'data');
90 case 'singletemplate':
91 $title = get_string('singletemplate', 'data');
94 if ($manager->has_fields()) {
95 $title = get_string('addtemplate', 'data');
97 $title = get_string('data:managetemplates', 'data');
103 format_string($instance->name
),
104 format_string($course->fullname
),
106 $PAGE->set_title(implode(moodle_page
::TITLE_SEPARATOR
, $titleparts));
107 $PAGE->set_heading($course->fullname
);
108 $PAGE->set_pagelayout('admin');
109 $PAGE->force_settings_menu(true);
110 $PAGE->activityheader
->disable();
111 $PAGE->add_body_class('mediumwidth');
113 echo $OUTPUT->header();
115 $renderer = $manager->get_renderer();
116 // Check if it is an empty database with no fields.
117 if (!$manager->has_fields()) {
118 echo $renderer->render_templates_zero_state($manager);
119 echo $OUTPUT->footer();
120 // Don't check the rest of the options. There is no field, there is nothing else to work with.
124 $actionbar = new \mod_data\output\action_bar
($instance->id
, $url);
125 echo $actionbar->get_templates_action_bar();
127 if (($formdata = data_submitted()) && confirm_sesskey()) {
128 if (!empty($formdata->defaultform
)) {
129 // Reset the template to default.
130 if (!empty($formdata->resetall
)) {
131 $manager->reset_all_templates();
132 $notificationstr = get_string('templateresetall', 'mod_data');
134 $manager->reset_template($mode);
135 $notificationstr = get_string('templatereset', 'data');
138 $manager->update_templates($formdata);
139 $notificationstr = get_string('templatesaved', 'data');
143 if (!empty($notificationstr)) {
144 echo $OUTPUT->notification($notificationstr, 'notifysuccess');
147 $templateeditor = new \mod_data\output\template_editor
($manager, $mode);
148 echo $renderer->render($templateeditor);
151 echo $OUTPUT->footer();