Merge branch 'mdl-53133-m30' of git://github.com/deraadt/moodle into MOODLE_30_STABLE
[moodle.git] / mod / data / templates.php
blob06d1a7b2829c552643e1ec4c9ff21dbc9cd88d4f
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->set_text($data->listtemplateheader);
222 $editor->use_editor($field, $options);
223 echo '<div><textarea id="'.$field.'" name="'.$field.'" rows="15" cols="80">'.s($data->listtemplateheader).'</textarea></div>';
225 echo '</td>';
226 echo '</tr>';
229 // Print the main template.
231 echo '<tr><td valign="top">';
232 if ($mode != 'csstemplate' and $mode != 'jstemplate') {
233 // Add all the available fields for this data.
234 echo '<label for="availabletags">'.get_string('availabletags','data').'</label>';
235 echo $OUTPUT->help_icon('availabletags', 'data');
236 echo '<br />';
238 echo '<div class="no-overflow" id="availabletags_wrapper">';
239 echo '<select name="fields1[]" id="availabletags" size="12" onclick="insert_field_tags(this)">';
241 $fields = $DB->get_records('data_fields', array('dataid'=>$data->id));
242 echo '<optgroup label="'.get_string('fields', 'data').'">';
243 foreach ($fields as $field) {
244 echo '<option value="[['.$field->name.']]" title="'.$field->description.'">'.$field->name.' - [['.$field->name.']]</option>';
246 echo '</optgroup>';
248 if ($mode == 'addtemplate') {
249 echo '<optgroup label="'.get_string('fieldids', 'data').'">';
250 foreach ($fields as $field) {
251 if (in_array($field->type, array('picture', 'checkbox', 'date', 'latlong', 'radiobutton'))) {
252 continue; //ids are not usable for these composed items
254 echo '<option value="[['.$field->name.'#id]]" title="'.$field->description.' id">'.$field->name.' id - [['.$field->name.'#id]]</option>';
256 echo '</optgroup>';
259 // Print special tags. fix for MDL-7031
260 if ($mode != 'addtemplate' && $mode != 'asearchtemplate') { //Don't print special tags when viewing the advanced search template and add template.
261 echo '<optgroup label="'.get_string('buttons', 'data').'">';
262 echo '<option value="##edit##">' .get_string('edit', 'data'). ' - ##edit##</option>';
263 echo '<option value="##delete##">' .get_string('delete', 'data'). ' - ##delete##</option>';
264 echo '<option value="##approve##">' .get_string('approve', 'data'). ' - ##approve##</option>';
265 echo '<option value="##disapprove##">' .get_string('disapprove', 'data'). ' - ##disapprove##</option>';
266 if ($mode != 'rsstemplate') {
267 echo '<option value="##export##">' .get_string('export', 'data'). ' - ##export##</option>';
269 if ($mode != 'singletemplate') {
270 // more points to single template - not useable there
271 echo '<option value="##more##">' .get_string('more', 'data'). ' - ##more##</option>';
272 echo '<option value="##moreurl##">' .get_string('moreurl', 'data'). ' - ##moreurl##</option>';
273 echo '<option value="##delcheck##">' .get_string('delcheck', 'data'). ' - ##delcheck##</option>';
275 echo '</optgroup>';
276 echo '<optgroup label="'.get_string('other', 'data').'">';
277 echo '<option value="##timeadded##">'.get_string('timeadded', 'data'). ' - ##timeadded##</option>';
278 echo '<option value="##timemodified##">'.get_string('timemodified', 'data'). ' - ##timemodified##</option>';
279 echo '<option value="##user##">' .get_string('user'). ' - ##user##</option>';
280 echo '<option value="##userpicture##">' . get_string('userpic') . ' - ##userpicture##</option>';
281 echo '<option value="##approvalstatus##">' .get_string('approvalstatus', 'data'). ' - ##approvalstatus##</option>';
282 if ($mode != 'singletemplate') {
283 // more points to single template - not useable there
284 echo '<option value="##comments##">' .get_string('comments', 'data'). ' - ##comments##</option>';
286 echo '</optgroup>';
289 if ($mode == 'asearchtemplate') {
290 echo '<optgroup label="'.get_string('other', 'data').'">';
291 echo '<option value="##firstname##">' .get_string('authorfirstname', 'data'). ' - ##firstname##</option>';
292 echo '<option value="##lastname##">' .get_string('authorlastname', 'data'). ' - ##lastname##</option>';
293 echo '</optgroup>';
296 echo '</select>';
297 echo '</div>';
298 echo '<br /><br /><br /><br /><input type="submit" name="defaultform" value="'.get_string('resettemplate','data').'" />';
299 echo '<br /><br />';
300 if ($usehtmleditor) {
301 $switcheditor = get_string('editordisable', 'data');
302 echo '<input type="submit" name="switcheditor" value="'.s($switcheditor).'" />';
303 } else {
304 $switcheditor = get_string('editorenable', 'data');
305 echo '<input type="submit" name="useeditor" value="'.s($switcheditor).'" />';
307 } else {
308 echo '<br /><br /><br /><br /><input type="submit" name="defaultform" value="'.get_string('resettemplate','data').'" />';
310 echo '</td>';
312 echo '<td valign="top">';
313 if ($mode == 'listtemplate'){
314 echo '<div class="template_heading"><label for="edit-template">'.get_string('multientry','data').'</label></div>';
315 } else {
316 echo '<div class="template_heading"><label for="edit-template">'.get_string($mode,'data').'</label></div>';
319 $field = 'template';
320 $editor->set_text($data->{$mode});
321 $editor->use_editor($field, $options);
322 echo '<div><textarea id="'.$field.'" name="'.$field.'" rows="15" cols="80">'.s($data->{$mode}).'</textarea></div>';
323 echo '</td>';
324 echo '</tr>';
326 if ($mode == 'listtemplate'){
327 echo '<tr>';
328 echo '<td>&nbsp;</td>';
329 echo '<td>';
330 echo '<div class="template_heading"><label for="edit-listtemplatefooter">'.get_string('footer','data').'</label></div>';
332 $field = 'listtemplatefooter';
333 $editor->set_text($data->listtemplatefooter);
334 $editor->use_editor($field, $options);
335 echo '<div><textarea id="'.$field.'" name="'.$field.'" rows="15" cols="80">'.s($data->listtemplatefooter).'</textarea></div>';
336 echo '</td>';
337 echo '</tr>';
338 } else if ($mode == 'rsstemplate') {
339 echo '<tr>';
340 echo '<td>&nbsp;</td>';
341 echo '<td>';
342 echo '<div class="template_heading"><label for="edit-rsstitletemplate">'.get_string('rsstitletemplate','data').'</label></div>';
344 $field = 'rsstitletemplate';
345 $editor->set_text($data->rsstitletemplate);
346 $editor->use_editor($field, $options);
347 echo '<div><textarea id="'.$field.'" name="'.$field.'" rows="15" cols="80">'.s($data->rsstitletemplate).'</textarea></div>';
348 echo '</td>';
349 echo '</tr>';
352 echo '<tr><td class="save_template" colspan="2">';
353 echo '<input type="submit" value="'.get_string('savetemplate','data').'" />&nbsp;';
355 echo '</td></tr></table>';
358 echo $OUTPUT->box_end();
359 echo '</div>';
360 echo '</form>';
362 /// Finish the page
363 echo $OUTPUT->footer();