MDL-57920 mod_data: Refactor search array creation
[moodle.git] / mod / data / templates.php
blob4d68d9fe12749a3ffd7b6887adff66353e2473e8
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 echo $OUTPUT->header();
107 echo $OUTPUT->heading(format_string($data->name), 2);
108 echo $OUTPUT->box(format_module_intro('data', $data, $cm->id), 'generalbox', 'intro');
110 /// Groups needed for Add entry tab
111 $currentgroup = groups_get_activity_group($cm);
112 $groupmode = groups_get_activity_groupmode($cm);
114 /// Print the tabs.
115 $currenttab = 'templates';
116 include('tabs.php');
118 /// Processing submitted data, i.e updating form.
119 $resettemplate = false;
121 if (($mytemplate = data_submitted()) && confirm_sesskey()) {
122 $newtemplate = new stdClass();
123 $newtemplate->id = $data->id;
124 $newtemplate->{$mode} = $mytemplate->template;
126 if (!empty($mytemplate->defaultform)) {
127 // Reset the template to default, but don't save yet.
128 $resettemplate = true;
129 $data->{$mode} = data_generate_default_template($data, $mode, 0, false, false);
130 if ($mode == 'listtemplate') {
131 $data->listtemplateheader = '';
132 $data->listtemplatefooter = '';
134 } else {
135 if (isset($mytemplate->listtemplateheader)){
136 $newtemplate->listtemplateheader = $mytemplate->listtemplateheader;
138 if (isset($mytemplate->listtemplatefooter)){
139 $newtemplate->listtemplatefooter = $mytemplate->listtemplatefooter;
141 if (isset($mytemplate->rsstitletemplate)){
142 $newtemplate->rsstitletemplate = $mytemplate->rsstitletemplate;
145 // Check for multiple tags, only need to check for add template.
146 if ($mode != 'addtemplate' or data_tags_check($data->id, $newtemplate->{$mode})) {
147 $DB->update_record('data', $newtemplate);
148 echo $OUTPUT->notification(get_string('templatesaved', 'data'), 'notifysuccess');
150 // Trigger an event for saving the templates.
151 $event = \mod_data\event\template_updated::create(array(
152 'context' => $context,
153 'courseid' => $course->id,
154 'other' => array(
155 'dataid' => $data->id,
158 $event->trigger();
161 } else {
162 echo '<div class="template_heading">'.get_string('header'.$mode,'data').'</div>';
165 /// If everything is empty then generate some defaults
166 if (empty($data->addtemplate) and empty($data->singletemplate) and
167 empty($data->listtemplate) and empty($data->rsstemplate)) {
168 data_generate_default_template($data, 'singletemplate');
169 data_generate_default_template($data, 'listtemplate');
170 data_generate_default_template($data, 'addtemplate');
171 data_generate_default_template($data, 'asearchtemplate'); //Template for advanced searches.
172 data_generate_default_template($data, 'rsstemplate');
175 editors_head_setup();
177 // Determine whether to use HTML editors.
178 if (($mode === 'csstemplate') || ($mode === 'jstemplate')) {
179 // The CSS and JS templates aren't HTML.
180 $usehtmleditor = false;
181 } else {
182 $usehtmleditor = data_get_config($data, "editor_{$mode}", true);
185 if ($usehtmleditor) {
186 $format = FORMAT_HTML;
187 } else {
188 $format = FORMAT_PLAIN;
191 $editor = editors_get_preferred_editor($format);
192 $strformats = format_text_menu();
193 $formats = $editor->get_supported_formats();
194 foreach ($formats as $fid) {
195 $formats[$fid] = $strformats[$fid];
197 $options = array();
198 $options['trusttext'] = false;
199 $options['forcehttps'] = false;
200 $options['subdirs'] = false;
201 $options['maxfiles'] = 0;
202 $options['maxbytes'] = 0;
203 $options['changeformat'] = 0;
204 $options['noclean'] = false;
206 echo '<form id="tempform" action="templates.php?d='.$data->id.'&amp;mode='.$mode.'" method="post">';
207 echo '<div>';
208 echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
209 // Print button to autogen all forms, if all templates are empty
211 if (!$resettemplate) {
212 // Only reload if we are not resetting the template to default.
213 $data = $DB->get_record('data', array('id'=>$d));
215 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
216 echo '<table cellpadding="4" cellspacing="0" border="0">';
218 if ($mode == 'listtemplate'){
219 // Print the list template header.
220 echo '<tr>';
221 echo '<td>&nbsp;</td>';
222 echo '<td>';
223 echo '<div class="template_heading"><label for="edit-listtemplateheader">'.get_string('header','data').'</label></div>';
225 $field = 'listtemplateheader';
226 $editor->set_text($data->listtemplateheader);
227 $editor->use_editor($field, $options);
228 echo '<div><textarea id="'.$field.'" name="'.$field.'" rows="15" cols="80">'.s($data->listtemplateheader).'</textarea></div>';
230 echo '</td>';
231 echo '</tr>';
234 // Print the main template.
236 echo '<tr><td valign="top">';
237 if ($mode != 'csstemplate' and $mode != 'jstemplate') {
238 // Add all the available fields for this data.
239 echo '<label for="availabletags">'.get_string('availabletags','data').'</label>';
240 echo $OUTPUT->help_icon('availabletags', 'data');
241 echo '<br />';
243 echo '<div class="no-overflow" id="availabletags_wrapper">';
244 echo '<select name="fields1[]" id="availabletags" size="12" onclick="insert_field_tags(this)" class="form-control">';
246 $fields = $DB->get_records('data_fields', array('dataid'=>$data->id));
247 echo '<optgroup label="'.get_string('fields', 'data').'">';
248 foreach ($fields as $field) {
249 echo '<option value="[['.$field->name.']]" title="'.$field->description.'">'.$field->name.' - [['.$field->name.']]</option>';
251 echo '</optgroup>';
253 if ($mode == 'addtemplate') {
254 echo '<optgroup label="'.get_string('fieldids', 'data').'">';
255 foreach ($fields as $field) {
256 if (in_array($field->type, array('picture', 'checkbox', 'date', 'latlong', 'radiobutton'))) {
257 continue; //ids are not usable for these composed items
259 echo '<option value="[['.$field->name.'#id]]" title="'.$field->description.' id">'.$field->name.' id - [['.$field->name.'#id]]</option>';
261 echo '</optgroup>';
264 // Print special tags. fix for MDL-7031
265 if ($mode != 'addtemplate' && $mode != 'asearchtemplate') { //Don't print special tags when viewing the advanced search template and add template.
266 echo '<optgroup label="'.get_string('buttons', 'data').'">';
267 echo '<option value="##edit##">' .get_string('edit', 'data'). ' - ##edit##</option>';
268 echo '<option value="##delete##">' .get_string('delete', 'data'). ' - ##delete##</option>';
269 echo '<option value="##approve##">' .get_string('approve', 'data'). ' - ##approve##</option>';
270 echo '<option value="##disapprove##">' .get_string('disapprove', 'data'). ' - ##disapprove##</option>';
271 if ($mode != 'rsstemplate') {
272 echo '<option value="##export##">' .get_string('export', 'data'). ' - ##export##</option>';
274 if ($mode != 'singletemplate') {
275 // more points to single template - not useable there
276 echo '<option value="##more##">' .get_string('more', 'data'). ' - ##more##</option>';
277 echo '<option value="##moreurl##">' .get_string('moreurl', 'data'). ' - ##moreurl##</option>';
278 echo '<option value="##delcheck##">' .get_string('delcheck', 'data'). ' - ##delcheck##</option>';
280 echo '</optgroup>';
281 echo '<optgroup label="'.get_string('other', 'data').'">';
282 echo '<option value="##timeadded##">'.get_string('timeadded', 'data'). ' - ##timeadded##</option>';
283 echo '<option value="##timemodified##">'.get_string('timemodified', 'data'). ' - ##timemodified##</option>';
284 echo '<option value="##user##">' .get_string('user'). ' - ##user##</option>';
285 echo '<option value="##userpicture##">' . get_string('userpic') . ' - ##userpicture##</option>';
286 echo '<option value="##approvalstatus##">' .get_string('approvalstatus', 'data'). ' - ##approvalstatus##</option>';
287 if ($mode != 'singletemplate') {
288 // more points to single template - not useable there
289 echo '<option value="##comments##">' .get_string('comments', 'data'). ' - ##comments##</option>';
291 echo '</optgroup>';
294 if ($mode == 'asearchtemplate') {
295 echo '<optgroup label="'.get_string('other', 'data').'">';
296 echo '<option value="##firstname##">' .get_string('authorfirstname', 'data'). ' - ##firstname##</option>';
297 echo '<option value="##lastname##">' .get_string('authorlastname', 'data'). ' - ##lastname##</option>';
298 echo '</optgroup>';
301 echo '</select>';
302 echo '</div>';
303 echo '<br /><br /><br /><br />';
304 echo '<input type="submit" class="btn btn-secondary" name="defaultform" value="'.get_string('resettemplate', 'data').'" />';
305 echo '<br /><br />';
306 if ($usehtmleditor) {
307 $switchlink = new moodle_url($PAGE->url, ['useeditor' => false]);
308 echo html_writer::link($switchlink, get_string('editordisable', 'data'));
309 } else {
310 $switchlink = new moodle_url($PAGE->url, ['useeditor' => true]);
311 echo html_writer::link($switchlink, get_string('editorenable', 'data'), [
312 'id' => 'enabletemplateeditor',
314 $PAGE->requires->event_handler('#enabletemplateeditor', 'click', 'M.util.show_confirm_dialog', [
315 'message' => get_string('enabletemplateeditorcheck', 'data'),
318 } else {
319 echo '<br /><br /><br /><br />';
320 echo '<input type="submit" class="btn btn-primary" name="defaultform" value="' . get_string('resettemplate', 'data') . '" />';
322 echo '</td>';
324 echo '<td valign="top">';
325 if ($mode == 'listtemplate'){
326 echo '<div class="template_heading"><label for="edit-template">'.get_string('multientry','data').'</label></div>';
327 } else {
328 echo '<div class="template_heading"><label for="edit-template">'.get_string($mode,'data').'</label></div>';
331 $field = 'template';
332 $editor->set_text($data->{$mode});
333 $editor->use_editor($field, $options);
334 echo '<div>';
335 echo '<textarea class="form-control" id="' . $field . '" ' .
336 'name="' . $field . '" rows="15" cols="80">' . s($data->{$mode}) . '</textarea>';
337 echo '</div>';
338 echo '</td>';
339 echo '</tr>';
341 if ($mode == 'listtemplate'){
342 echo '<tr>';
343 echo '<td>&nbsp;</td>';
344 echo '<td>';
345 echo '<div class="template_heading"><label for="edit-listtemplatefooter">'.get_string('footer','data').'</label></div>';
347 $field = 'listtemplatefooter';
348 $editor->set_text($data->listtemplatefooter);
349 $editor->use_editor($field, $options);
350 echo '<div>';
351 echo '<textarea id="' . $field . '" class="form-control" ' .
352 'name="' . $field . '" rows="15" cols="80">' . s($data->listtemplatefooter) . '</textarea>';
353 echo '</div>';
354 echo '</td>';
355 echo '</tr>';
356 } else if ($mode == 'rsstemplate') {
357 echo '<tr>';
358 echo '<td>&nbsp;</td>';
359 echo '<td>';
360 echo '<div class="template_heading">';
361 echo '<label for="edit-rsstitletemplate">' . get_string('rsstitletemplate', 'data') . '</label>';
362 echo '</div>';
364 $field = 'rsstitletemplate';
365 $editor->set_text($data->rsstitletemplate);
366 $editor->use_editor($field, $options);
367 echo '<div>';
368 echo '<textarea id="' . $field . '" name="' . $field . '" ' .
369 'class="form-control" rows="15" cols="80">' . s($data->rsstitletemplate) . '</textarea>';
370 echo '</div>';
371 echo '</td>';
372 echo '</tr>';
375 echo '<tr><td class="save_template" colspan="2">';
376 echo '<input type="submit" class="btn btn-primary" value="'.get_string('savetemplate','data').'" />&nbsp;';
378 echo '</td></tr></table>';
381 echo $OUTPUT->box_end();
382 echo '</div>';
383 echo '</form>';
385 /// Finish the page
386 echo $OUTPUT->footer();