Merge branch 'MDL-44822' of git://github.com/aolley/moodle
[moodle.git] / mod / data / templates.php
blob236c23be17efacb12738f08d66ef4aca2af3e092
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 * 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
23 * @package mod_data
26 require_once('../../config.php');
27 require_once('lib.php');
29 $id = optional_param('id', 0, PARAM_INT); // course module id
30 $d = optional_param('d', 0, PARAM_INT); // database id
31 $mode = optional_param('mode', 'singletemplate', PARAM_ALPHA);
32 $disableeditor = optional_param('switcheditor', false, PARAM_RAW);
33 $enableeditor = optional_param('useeditor', false, PARAM_RAW);
35 $url = new moodle_url('/mod/data/templates.php');
36 if ($mode !== 'singletemplate') {
37 $url->param('mode', $mode);
40 if ($id) {
41 $url->param('id', $id);
42 $PAGE->set_url($url);
43 if (! $cm = get_coursemodule_from_id('data', $id)) {
44 print_error('invalidcoursemodule');
46 if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
47 print_error('coursemisconf');
49 if (! $data = $DB->get_record('data', array('id'=>$cm->instance))) {
50 print_error('invalidcoursemodule');
53 } else {
54 $url->param('d', $d);
55 $PAGE->set_url($url);
56 if (! $data = $DB->get_record('data', array('id'=>$d))) {
57 print_error('invalidid', 'data');
59 if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
60 print_error('coursemisconf');
62 if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
63 print_error('invalidcoursemodule');
67 require_login($course, false, $cm);
69 $context = context_module::instance($cm->id);
70 require_capability('mod/data:managetemplates', $context);
72 if (!$DB->count_records('data_fields', array('dataid'=>$data->id))) { // Brand new database!
73 redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id); // Redirect to field entry
76 // Trigger an event for viewing templates.
77 $event = \mod_data\event\template_viewed::create(array(
78 'context' => $context,
79 'courseid' => $course->id,
80 'other' => array(
81 'dataid' => $data->id
83 ));
84 $event->add_record_snapshot('data', $data);
85 $event->trigger();
87 /// Print the page header
89 $strdata = get_string('modulenameplural','data');
91 // For the javascript for inserting template tags: initialise the default textarea to
92 // 'edit_template' - it is always present in all different possible views.
94 if ($mode == 'singletemplate') {
95 $PAGE->navbar->add(get_string($mode,'data'));
98 $PAGE->requires->js('/mod/data/data.js');
99 $PAGE->set_title($data->name);
100 $PAGE->set_heading($course->fullname);
101 $PAGE->set_pagelayout('admin');
102 echo $OUTPUT->header();
103 echo $OUTPUT->heading(format_string($data->name), 2);
104 echo $OUTPUT->box(format_module_intro('data', $data, $cm->id), 'generalbox', 'intro');
106 /// Groups needed for Add entry tab
107 $currentgroup = groups_get_activity_group($cm);
108 $groupmode = groups_get_activity_groupmode($cm);
110 /// Print the tabs.
111 $currenttab = 'templates';
112 include('tabs.php');
114 /// Processing submitted data, i.e updating form.
115 $resettemplate = false;
117 if (($mytemplate = data_submitted()) && confirm_sesskey()) {
118 $newtemplate = new stdClass();
119 $newtemplate->id = $data->id;
120 $newtemplate->{$mode} = $mytemplate->template;
122 if (!empty($mytemplate->defaultform)) {
123 // Reset the template to default, but don't save yet.
124 $resettemplate = true;
125 $data->{$mode} = data_generate_default_template($data, $mode, 0, false, false);
126 if ($mode == 'listtemplate') {
127 $data->listtemplateheader = '';
128 $data->listtemplatefooter = '';
130 } else {
131 if (isset($mytemplate->listtemplateheader)){
132 $newtemplate->listtemplateheader = $mytemplate->listtemplateheader;
134 if (isset($mytemplate->listtemplatefooter)){
135 $newtemplate->listtemplatefooter = $mytemplate->listtemplatefooter;
137 if (isset($mytemplate->rsstitletemplate)){
138 $newtemplate->rsstitletemplate = $mytemplate->rsstitletemplate;
141 // Check for multiple tags, only need to check for add template.
142 if ($mode != 'addtemplate' or data_tags_check($data->id, $newtemplate->{$mode})) {
143 // if disableeditor or enableeditor buttons click, don't save instance
144 if (empty($disableeditor) && empty($enableeditor)) {
145 $DB->update_record('data', $newtemplate);
146 echo $OUTPUT->notification(get_string('templatesaved', 'data'), 'notifysuccess');
148 // Trigger an event for saving the templates.
149 $event = \mod_data\event\template_updated::create(array(
150 'context' => $context,
151 'courseid' => $course->id,
152 'other' => array(
153 'dataid' => $data->id,
156 $event->trigger();
160 } else {
161 echo '<div class="template_heading">'.get_string('header'.$mode,'data').'</div>';
164 /// If everything is empty then generate some defaults
165 if (empty($data->addtemplate) and empty($data->singletemplate) and
166 empty($data->listtemplate) and empty($data->rsstemplate)) {
167 data_generate_default_template($data, 'singletemplate');
168 data_generate_default_template($data, 'listtemplate');
169 data_generate_default_template($data, 'addtemplate');
170 data_generate_default_template($data, 'asearchtemplate'); //Template for advanced searches.
171 data_generate_default_template($data, 'rsstemplate');
174 editors_head_setup();
175 $format = FORMAT_HTML;
177 if ($mode === 'csstemplate' or $mode === 'jstemplate') {
178 $disableeditor = true;
181 if ($disableeditor) {
182 $format = FORMAT_PLAIN;
184 $editor = editors_get_preferred_editor($format);
185 $strformats = format_text_menu();
186 $formats = $editor->get_supported_formats();
187 foreach ($formats as $fid) {
188 $formats[$fid] = $strformats[$fid];
190 $options = array();
191 $options['trusttext'] = false;
192 $options['forcehttps'] = false;
193 $options['subdirs'] = false;
194 $options['maxfiles'] = 0;
195 $options['maxbytes'] = 0;
196 $options['changeformat'] = 0;
197 $options['noclean'] = false;
199 echo '<form id="tempform" action="templates.php?d='.$data->id.'&amp;mode='.$mode.'" method="post">';
200 echo '<div>';
201 echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
202 // Print button to autogen all forms, if all templates are empty
204 if (!$resettemplate) {
205 // Only reload if we are not resetting the template to default.
206 $data = $DB->get_record('data', array('id'=>$d));
208 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
209 echo '<table cellpadding="4" cellspacing="0" border="0">';
211 /// Add the HTML editor(s).
212 $usehtmleditor = ($mode != 'csstemplate') && ($mode != 'jstemplate') && !$disableeditor;
213 if ($mode == 'listtemplate'){
214 // Print the list template header.
215 echo '<tr>';
216 echo '<td>&nbsp;</td>';
217 echo '<td>';
218 echo '<div class="template_heading"><label for="edit-listtemplateheader">'.get_string('header','data').'</label></div>';
220 $field = 'listtemplateheader';
221 $editor->use_editor($field, $options);
222 echo '<div><textarea id="'.$field.'" name="'.$field.'" rows="15" cols="80">'.s($data->listtemplateheader).'</textarea></div>';
224 echo '</td>';
225 echo '</tr>';
228 // Print the main template.
230 echo '<tr><td valign="top">';
231 if ($mode != 'csstemplate' and $mode != 'jstemplate') {
232 // Add all the available fields for this data.
233 echo '<label for="availabletags">'.get_string('availabletags','data').'</label>';
234 echo $OUTPUT->help_icon('availabletags', 'data');
235 echo '<br />';
238 echo '<select name="fields1[]" id="availabletags" size="12" onclick="insert_field_tags(this)">';
240 $fields = $DB->get_records('data_fields', array('dataid'=>$data->id));
241 echo '<optgroup label="'.get_string('fields', 'data').'">';
242 foreach ($fields as $field) {
243 echo '<option value="[['.$field->name.']]" title="'.$field->description.'">'.$field->name.' - [['.$field->name.']]</option>';
245 echo '</optgroup>';
247 if ($mode == 'addtemplate') {
248 echo '<optgroup label="'.get_string('fieldids', 'data').'">';
249 foreach ($fields as $field) {
250 if (in_array($field->type, array('picture', 'checkbox', 'date', 'latlong', 'radiobutton'))) {
251 continue; //ids are not usable for these composed items
253 echo '<option value="[['.$field->name.'#id]]" title="'.$field->description.' id">'.$field->name.' id - [['.$field->name.'#id]]</option>';
255 echo '</optgroup>';
258 // Print special tags. fix for MDL-7031
259 if ($mode != 'addtemplate' && $mode != 'asearchtemplate') { //Don't print special tags when viewing the advanced search template and add template.
260 echo '<optgroup label="'.get_string('buttons', 'data').'">';
261 echo '<option value="##edit##">' .get_string('edit', 'data'). ' - ##edit##</option>';
262 echo '<option value="##delete##">' .get_string('delete', 'data'). ' - ##delete##</option>';
263 echo '<option value="##approve##">' .get_string('approve', 'data'). ' - ##approve##</option>';
264 echo '<option value="##disapprove##">' .get_string('disapprove', 'data'). ' - ##disapprove##</option>';
265 if ($mode != 'rsstemplate') {
266 echo '<option value="##export##">' .get_string('export', 'data'). ' - ##export##</option>';
268 if ($mode != 'singletemplate') {
269 // more points to single template - not useable there
270 echo '<option value="##more##">' .get_string('more', 'data'). ' - ##more##</option>';
271 echo '<option value="##moreurl##">' .get_string('moreurl', 'data'). ' - ##moreurl##</option>';
272 echo '<option value="##delcheck##">' .get_string('delcheck', 'data'). ' - ##delcheck##</option>';
274 echo '</optgroup>';
275 echo '<optgroup label="'.get_string('other', 'data').'">';
276 echo '<option value="##timeadded##">'.get_string('timeadded', 'data'). ' - ##timeadded##</option>';
277 echo '<option value="##timemodified##">'.get_string('timemodified', 'data'). ' - ##timemodified##</option>';
278 echo '<option value="##user##">' .get_string('user'). ' - ##user##</option>';
279 if ($mode != 'singletemplate') {
280 // more points to single template - not useable there
281 echo '<option value="##comments##">' .get_string('comments', 'data'). ' - ##comments##</option>';
283 echo '</optgroup>';
286 if ($mode == 'asearchtemplate') {
287 echo '<optgroup label="'.get_string('other', 'data').'">';
288 echo '<option value="##firstname##">' .get_string('authorfirstname', 'data'). ' - ##firstname##</option>';
289 echo '<option value="##lastname##">' .get_string('authorlastname', 'data'). ' - ##lastname##</option>';
290 echo '</optgroup>';
293 echo '</select>';
294 echo '<br /><br /><br /><br /><input type="submit" name="defaultform" value="'.get_string('resettemplate','data').'" />';
295 echo '<br /><br />';
296 if ($usehtmleditor) {
297 $switcheditor = get_string('editordisable', 'data');
298 echo '<input type="submit" name="switcheditor" value="'.s($switcheditor).'" />';
299 } else {
300 $switcheditor = get_string('editorenable', 'data');
301 echo '<input type="submit" name="useeditor" value="'.s($switcheditor).'" />';
303 } else {
304 echo '<br /><br /><br /><br /><input type="submit" name="defaultform" value="'.get_string('resettemplate','data').'" />';
306 echo '</td>';
308 echo '<td valign="top">';
309 if ($mode == 'listtemplate'){
310 echo '<div class="template_heading"><label for="edit-template">'.get_string('multientry','data').'</label></div>';
311 } else {
312 echo '<div class="template_heading"><label for="edit-template">'.get_string($mode,'data').'</label></div>';
315 $field = 'template';
316 $editor->use_editor($field, $options);
317 echo '<div><textarea id="'.$field.'" name="'.$field.'" rows="15" cols="80">'.s($data->{$mode}).'</textarea></div>';
318 echo '</td>';
319 echo '</tr>';
321 if ($mode == 'listtemplate'){
322 echo '<tr>';
323 echo '<td>&nbsp;</td>';
324 echo '<td>';
325 echo '<div class="template_heading"><label for="edit-listtemplatefooter">'.get_string('footer','data').'</label></div>';
327 $field = 'listtemplatefooter';
328 $editor->use_editor($field, $options);
329 echo '<div><textarea id="'.$field.'" name="'.$field.'" rows="15" cols="80">'.s($data->listtemplatefooter).'</textarea></div>';
330 echo '</td>';
331 echo '</tr>';
332 } else if ($mode == 'rsstemplate') {
333 echo '<tr>';
334 echo '<td>&nbsp;</td>';
335 echo '<td>';
336 echo '<div class="template_heading"><label for="edit-rsstitletemplate">'.get_string('rsstitletemplate','data').'</label></div>';
338 $field = 'rsstitletemplate';
339 $editor->use_editor($field, $options);
340 echo '<div><textarea id="'.$field.'" name="'.$field.'" rows="15" cols="80">'.s($data->rsstitletemplate).'</textarea></div>';
341 echo '</td>';
342 echo '</tr>';
345 echo '<tr><td class="save_template" colspan="2">';
346 echo '<input type="submit" value="'.get_string('savetemplate','data').'" />&nbsp;';
348 echo '</td></tr></table>';
351 echo $OUTPUT->box_end();
352 echo '</div>';
353 echo '</form>';
355 /// Finish the page
356 echo $OUTPUT->footer();