MDL-61391 forum: Improve semantics for permalinks
[moodle.git] / mod / data / templates.php
blobce5af6c782aa0e80f035f691a05332023ce4dbdb
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 $useeditor = optional_param('useeditor', null, PARAM_BOOL);
34 $url = new moodle_url('/mod/data/templates.php');
35 if ($mode !== 'singletemplate') {
36 $url->param('mode', $mode);
39 if ($id) {
40 $url->param('id', $id);
41 $PAGE->set_url($url);
42 if (! $cm = get_coursemodule_from_id('data', $id)) {
43 print_error('invalidcoursemodule');
45 if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
46 print_error('coursemisconf');
48 if (! $data = $DB->get_record('data', array('id'=>$cm->instance))) {
49 print_error('invalidcoursemodule');
52 } else {
53 $url->param('d', $d);
54 $PAGE->set_url($url);
55 if (! $data = $DB->get_record('data', array('id'=>$d))) {
56 print_error('invalidid', 'data');
58 if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
59 print_error('coursemisconf');
61 if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
62 print_error('invalidcoursemodule');
66 require_login($course, false, $cm);
68 $context = context_module::instance($cm->id);
69 require_capability('mod/data:managetemplates', $context);
71 if ($useeditor !== null) {
72 // The useeditor param was set. Update the value for this template.
73 data_set_config($data, "editor_{$mode}", !!$useeditor);
76 if (!$DB->count_records('data_fields', array('dataid'=>$data->id))) { // Brand new database!
77 redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id); // Redirect to field entry
80 // Trigger an event for viewing templates.
81 $event = \mod_data\event\template_viewed::create(array(
82 'context' => $context,
83 'courseid' => $course->id,
84 'other' => array(
85 'dataid' => $data->id
87 ));
88 $event->add_record_snapshot('data', $data);
89 $event->trigger();
91 /// Print the page header
93 $strdata = get_string('modulenameplural','data');
95 // For the javascript for inserting template tags: initialise the default textarea to
96 // 'edit_template' - it is always present in all different possible views.
98 if ($mode == 'singletemplate') {
99 $PAGE->navbar->add(get_string($mode,'data'));
102 $PAGE->requires->js('/mod/data/data.js');
103 $PAGE->set_title($data->name);
104 $PAGE->set_heading($course->fullname);
105 $PAGE->set_pagelayout('admin');
106 $PAGE->force_settings_menu(true);
107 echo $OUTPUT->header();
108 echo $OUTPUT->heading(format_string($data->name), 2);
109 echo $OUTPUT->box(format_module_intro('data', $data, $cm->id), 'generalbox', 'intro');
111 /// Groups needed for Add entry tab
112 $currentgroup = groups_get_activity_group($cm);
113 $groupmode = groups_get_activity_groupmode($cm);
115 /// Print the tabs.
116 $currenttab = 'templates';
117 include('tabs.php');
119 /// Processing submitted data, i.e updating form.
120 $resettemplate = false;
122 if (($mytemplate = data_submitted()) && confirm_sesskey()) {
123 $newtemplate = new stdClass();
124 $newtemplate->id = $data->id;
125 $newtemplate->{$mode} = $mytemplate->template;
127 if (!empty($mytemplate->defaultform)) {
128 // Reset the template to default, but don't save yet.
129 $resettemplate = true;
130 $data->{$mode} = data_generate_default_template($data, $mode, 0, false, false);
131 if ($mode == 'listtemplate') {
132 $data->listtemplateheader = '';
133 $data->listtemplatefooter = '';
135 } else {
136 if (isset($mytemplate->listtemplateheader)){
137 $newtemplate->listtemplateheader = $mytemplate->listtemplateheader;
139 if (isset($mytemplate->listtemplatefooter)){
140 $newtemplate->listtemplatefooter = $mytemplate->listtemplatefooter;
142 if (isset($mytemplate->rsstitletemplate)){
143 $newtemplate->rsstitletemplate = $mytemplate->rsstitletemplate;
146 // Check for multiple tags, only need to check for add template.
147 if ($mode != 'addtemplate' or data_tags_check($data->id, $newtemplate->{$mode})) {
148 $DB->update_record('data', $newtemplate);
149 echo $OUTPUT->notification(get_string('templatesaved', 'data'), 'notifysuccess');
151 // Trigger an event for saving the templates.
152 $event = \mod_data\event\template_updated::create(array(
153 'context' => $context,
154 'courseid' => $course->id,
155 'other' => array(
156 'dataid' => $data->id,
159 $event->trigger();
162 } else {
163 echo '<div class="template_heading">'.get_string('header'.$mode,'data').'</div>';
166 /// If everything is empty then generate some defaults
167 if (empty($data->addtemplate) and empty($data->singletemplate) and
168 empty($data->listtemplate) and empty($data->rsstemplate)) {
169 data_generate_default_template($data, 'singletemplate');
170 data_generate_default_template($data, 'listtemplate');
171 data_generate_default_template($data, 'addtemplate');
172 data_generate_default_template($data, 'asearchtemplate'); //Template for advanced searches.
173 data_generate_default_template($data, 'rsstemplate');
176 editors_head_setup();
178 // Determine whether to use HTML editors.
179 if (($mode === 'csstemplate') || ($mode === 'jstemplate')) {
180 // The CSS and JS templates aren't HTML.
181 $usehtmleditor = false;
182 } else {
183 $usehtmleditor = data_get_config($data, "editor_{$mode}", true);
186 if ($usehtmleditor) {
187 $format = FORMAT_HTML;
188 } else {
189 $format = FORMAT_PLAIN;
192 $editor = editors_get_preferred_editor($format);
193 $strformats = format_text_menu();
194 $formats = $editor->get_supported_formats();
195 foreach ($formats as $fid) {
196 $formats[$fid] = $strformats[$fid];
198 $options = array();
199 $options['trusttext'] = false;
200 $options['forcehttps'] = false;
201 $options['subdirs'] = false;
202 $options['maxfiles'] = 0;
203 $options['maxbytes'] = 0;
204 $options['changeformat'] = 0;
205 $options['noclean'] = false;
207 echo '<form id="tempform" action="templates.php?d='.$data->id.'&amp;mode='.$mode.'" method="post">';
208 echo '<div>';
209 echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
210 // Print button to autogen all forms, if all templates are empty
212 if (!$resettemplate) {
213 // Only reload if we are not resetting the template to default.
214 $data = $DB->get_record('data', array('id'=>$d));
216 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
217 echo '<table cellpadding="4" cellspacing="0" border="0">';
219 if ($mode == 'listtemplate'){
220 // Print the list template header.
221 echo '<tr>';
222 echo '<td>&nbsp;</td>';
223 echo '<td>';
224 echo '<div class="template_heading"><label for="edit-listtemplateheader">'.get_string('header','data').'</label></div>';
226 $field = 'listtemplateheader';
227 $editor->set_text($data->listtemplateheader);
228 $editor->use_editor($field, $options);
229 echo '<div><textarea id="'.$field.'" name="'.$field.'" rows="15" cols="80">'.s($data->listtemplateheader).'</textarea></div>';
231 echo '</td>';
232 echo '</tr>';
235 // Print the main template.
237 echo '<tr><td valign="top">';
238 if ($mode != 'csstemplate' and $mode != 'jstemplate') {
239 // Add all the available fields for this data.
240 echo '<label for="availabletags">'.get_string('availabletags','data').'</label>';
241 echo $OUTPUT->help_icon('availabletags', 'data');
242 echo '<br />';
244 echo '<div class="no-overflow" id="availabletags_wrapper">';
245 echo '<select name="fields1[]" id="availabletags" size="12" onclick="insert_field_tags(this)" class="form-control">';
247 $fields = $DB->get_records('data_fields', array('dataid'=>$data->id));
248 echo '<optgroup label="'.get_string('fields', 'data').'">';
249 foreach ($fields as $field) {
250 echo '<option value="[['.$field->name.']]" title="'.$field->description.'">'.$field->name.' - [['.$field->name.']]</option>';
252 echo '</optgroup>';
254 if ($mode == 'addtemplate') {
255 echo '<optgroup label="'.get_string('fieldids', 'data').'">';
256 foreach ($fields as $field) {
257 if (in_array($field->type, array('picture', 'checkbox', 'date', 'latlong', 'radiobutton'))) {
258 continue; //ids are not usable for these composed items
260 echo '<option value="[['.$field->name.'#id]]" title="'.$field->description.' id">'.$field->name.' id - [['.$field->name.'#id]]</option>';
262 echo '</optgroup>';
263 if (core_tag_tag::is_enabled('mod_data', 'data_records')) {
264 echo '<optgroup label="'.get_string('other', 'data').'">';
265 echo '<option value="##tags##">' . get_string('tags') . ' - ##tags##</option>';
266 echo '</optgroup>';
270 // Print special tags. fix for MDL-7031
271 if ($mode != 'addtemplate' && $mode != 'asearchtemplate') { //Don't print special tags when viewing the advanced search template and add template.
272 echo '<optgroup label="'.get_string('buttons', 'data').'">';
273 echo '<option value="##edit##">' .get_string('edit', 'data'). ' - ##edit##</option>';
274 echo '<option value="##delete##">' .get_string('delete', 'data'). ' - ##delete##</option>';
275 echo '<option value="##approve##">' .get_string('approve', 'data'). ' - ##approve##</option>';
276 echo '<option value="##disapprove##">' .get_string('disapprove', 'data'). ' - ##disapprove##</option>';
277 if ($mode != 'rsstemplate') {
278 echo '<option value="##export##">' .get_string('export', 'data'). ' - ##export##</option>';
280 if ($mode != 'singletemplate') {
281 // more points to single template - not useable there
282 echo '<option value="##more##">' .get_string('more', 'data'). ' - ##more##</option>';
283 echo '<option value="##moreurl##">' .get_string('moreurl', 'data'). ' - ##moreurl##</option>';
284 echo '<option value="##delcheck##">' .get_string('delcheck', 'data'). ' - ##delcheck##</option>';
286 echo '</optgroup>';
287 echo '<optgroup label="'.get_string('other', 'data').'">';
288 echo '<option value="##timeadded##">'.get_string('timeadded', 'data'). ' - ##timeadded##</option>';
289 echo '<option value="##timemodified##">'.get_string('timemodified', 'data'). ' - ##timemodified##</option>';
290 echo '<option value="##user##">' .get_string('user'). ' - ##user##</option>';
291 echo '<option value="##userpicture##">' . get_string('userpic') . ' - ##userpicture##</option>';
292 echo '<option value="##approvalstatus##">' .get_string('approvalstatus', 'data'). ' - ##approvalstatus##</option>';
294 if (core_tag_tag::is_enabled('mod_data', 'data_records')) {
295 echo '<option value="##tags##">' . get_string('tags') . ' - ##tags##</option>';
298 if ($mode != 'singletemplate') {
299 // more points to single template - not useable there
300 echo '<option value="##comments##">' .get_string('comments', 'data'). ' - ##comments##</option>';
302 echo '</optgroup>';
305 if ($mode == 'asearchtemplate') {
306 echo '<optgroup label="'.get_string('other', 'data').'">';
307 echo '<option value="##firstname##">' .get_string('authorfirstname', 'data'). ' - ##firstname##</option>';
308 echo '<option value="##lastname##">' .get_string('authorlastname', 'data'). ' - ##lastname##</option>';
309 echo '</optgroup>';
312 echo '</select>';
313 echo '</div>';
314 echo '<br /><br /><br /><br />';
315 echo '<input type="submit" class="btn btn-secondary" name="defaultform" value="'.get_string('resettemplate', 'data').'" />';
316 echo '<br /><br />';
317 if ($usehtmleditor) {
318 $switchlink = new moodle_url($PAGE->url, ['useeditor' => false]);
319 echo html_writer::link($switchlink, get_string('editordisable', 'data'));
320 } else {
321 $switchlink = new moodle_url($PAGE->url, ['useeditor' => true]);
322 echo html_writer::link($switchlink, get_string('editorenable', 'data'), [
323 'id' => 'enabletemplateeditor',
325 $PAGE->requires->event_handler('#enabletemplateeditor', 'click', 'M.util.show_confirm_dialog', [
326 'message' => get_string('enabletemplateeditorcheck', 'data'),
329 } else {
330 echo '<br /><br /><br /><br />';
331 echo '<input type="submit" class="btn btn-primary" name="defaultform" value="' . get_string('resettemplate', 'data') . '" />';
333 echo '</td>';
335 echo '<td valign="top">';
336 if ($mode == 'listtemplate'){
337 echo '<div class="template_heading"><label for="edit-template">'.get_string('multientry','data').'</label></div>';
338 } else {
339 echo '<div class="template_heading"><label for="edit-template">'.get_string($mode,'data').'</label></div>';
342 $field = 'template';
343 $editor->set_text($data->{$mode});
344 $editor->use_editor($field, $options);
345 echo '<div>';
346 echo '<textarea class="form-control" id="' . $field . '" ' .
347 'name="' . $field . '" rows="15" cols="80">' . s($data->{$mode}) . '</textarea>';
348 echo '</div>';
349 echo '</td>';
350 echo '</tr>';
352 if ($mode == 'listtemplate'){
353 echo '<tr>';
354 echo '<td>&nbsp;</td>';
355 echo '<td>';
356 echo '<div class="template_heading"><label for="edit-listtemplatefooter">'.get_string('footer','data').'</label></div>';
358 $field = 'listtemplatefooter';
359 $editor->set_text($data->listtemplatefooter);
360 $editor->use_editor($field, $options);
361 echo '<div>';
362 echo '<textarea id="' . $field . '" class="form-control" ' .
363 'name="' . $field . '" rows="15" cols="80">' . s($data->listtemplatefooter) . '</textarea>';
364 echo '</div>';
365 echo '</td>';
366 echo '</tr>';
367 } else if ($mode == 'rsstemplate') {
368 echo '<tr>';
369 echo '<td>&nbsp;</td>';
370 echo '<td>';
371 echo '<div class="template_heading">';
372 echo '<label for="edit-rsstitletemplate">' . get_string('rsstitletemplate', 'data') . '</label>';
373 echo '</div>';
375 $field = 'rsstitletemplate';
376 $editor->set_text($data->rsstitletemplate);
377 $editor->use_editor($field, $options);
378 echo '<div>';
379 echo '<textarea id="' . $field . '" name="' . $field . '" ' .
380 'class="form-control" rows="15" cols="80">' . s($data->rsstitletemplate) . '</textarea>';
381 echo '</div>';
382 echo '</td>';
383 echo '</tr>';
386 echo '<tr><td class="save_template" colspan="2">';
387 echo '<input type="submit" class="btn btn-primary" value="'.get_string('savetemplate','data').'" />&nbsp;';
389 echo '</td></tr></table>';
392 echo $OUTPUT->box_end();
393 echo '</div>';
394 echo '</form>';
396 /// Finish the page
397 echo $OUTPUT->footer();