MDL-31496 mod_data Replacing inline styles for CSS classes in templates and browse...
[moodle.git] / mod / data / templates.php
CommitLineData
6111b2b0 1<?php
2
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.
14//
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/>.
17
18/**
19 * This file is part of the Database module for Moodle
20 *
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
24 */
25
26require_once('../../config.php');
27require_once('lib.php');
28
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);
08a7fbb0
DC
32$disableeditor = optional_param('switcheditor', false, PARAM_RAW);
33$enableeditor = optional_param('useeditor', false, PARAM_RAW);
6111b2b0 34
a6855934 35$url = new moodle_url('/mod/data/templates.php');
6111b2b0 36if ($mode !== 'singletemplate') {
37 $url->param('mode', $mode);
38}
39
40if ($id) {
41 $url->param('id', $id);
42 $PAGE->set_url($url);
43 if (! $cm = get_coursemodule_from_id('data', $id)) {
44 print_error('invalidcoursemodule');
45 }
46 if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
47 print_error('coursemisconf');
48 }
49 if (! $data = $DB->get_record('data', array('id'=>$cm->instance))) {
50 print_error('invalidcoursemodule');
51 }
3d4b223a 52
6111b2b0 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');
58 }
59 if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
60 print_error('coursemisconf');
61 }
62 if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
63 print_error('invalidcoursemodule');
3d4b223a 64 }
6111b2b0 65}
3d4b223a 66
cdbea7ee 67require_login($course, false, $cm);
c088d603 68
0b3b8c30 69$context = context_module::instance($cm->id);
6111b2b0 70require_capability('mod/data:managetemplates', $context);
0468976c 71
6111b2b0 72if (!$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
74}
0468976c 75
6111b2b0 76add_to_log($course->id, 'data', 'templates view', "templates.php?id=$cm->id&amp;d=$data->id", $data->id, $cm->id);
3d4b223a 77
3d4b223a 78
79/// Print the page header
80
6111b2b0 81$strdata = get_string('modulenameplural','data');
aab98aaf 82
6111b2b0 83// For the javascript for inserting template tags: initialise the default textarea to
84// 'edit_template' - it is always present in all different possible views.
986b73b8 85
91bc072a
RW
86if ($mode == 'singletemplate') {
87 $PAGE->navbar->add(get_string($mode,'data'));
88}
89
9dec75db 90$PAGE->requires->js('/mod/data/data.js');
6111b2b0 91$PAGE->set_title($data->name);
91bc072a 92$PAGE->set_heading($course->fullname);
16265f06 93$PAGE->set_pagelayout('report');
6111b2b0 94echo $OUTPUT->header();
95echo $OUTPUT->heading(format_string($data->name));
10aba627 96
704a26cb 97
cca1547e 98/// Groups needed for Add entry tab
6111b2b0 99$currentgroup = groups_get_activity_group($cm);
100$groupmode = groups_get_activity_groupmode($cm);
cca1547e 101
10aba627 102/// Print the tabs.
6111b2b0 103$currenttab = 'templates';
104include('tabs.php');
10aba627 105
106/// Processing submitted data, i.e updating form.
6111b2b0 107$resettemplate = false;
108
109if (($mytemplate = data_submitted()) && confirm_sesskey()) {
39790bd8 110 $newtemplate = new stdClass();
6111b2b0 111 $newtemplate->id = $data->id;
112 $newtemplate->{$mode} = $mytemplate->template;
113
114 if (!empty($mytemplate->defaultform)) {
115 // Reset the template to default, but don't save yet.
116 $resettemplate = true;
117 $data->{$mode} = data_generate_default_template($data, $mode, 0, false, false);
118 if ($mode == 'listtemplate') {
119 $data->listtemplateheader = '';
120 $data->listtemplatefooter = '';
121 }
122 } else {
123 if (isset($mytemplate->listtemplateheader)){
124 $newtemplate->listtemplateheader = $mytemplate->listtemplateheader;
125 }
126 if (isset($mytemplate->listtemplatefooter)){
127 $newtemplate->listtemplatefooter = $mytemplate->listtemplatefooter;
128 }
129 if (isset($mytemplate->rsstitletemplate)){
130 $newtemplate->rsstitletemplate = $mytemplate->rsstitletemplate;
131 }
ea6a8345 132
6111b2b0 133 // Check for multiple tags, only need to check for add template.
134 if ($mode != 'addtemplate' or data_tags_check($data->id, $newtemplate->{$mode})) {
08a7fbb0
DC
135 // if disableeditor or enableeditor buttons click, don't save instance
136 if (empty($disableeditor) && empty($enableeditor)) {
137 $DB->update_record('data', $newtemplate);
138 echo $OUTPUT->notification(get_string('templatesaved', 'data'), 'notifysuccess');
139 add_to_log($course->id, 'data', 'templates saved', "templates.php?id=$cm->id&amp;d=$data->id", $data->id, $cm->id);
140 }
3d4b223a 141 }
142 }
6111b2b0 143} else {
649ac85d 144 echo '<div class="template_heading">'.get_string('header'.$mode,'data').'</div>';
6111b2b0 145}
3d4b223a 146
d74a6d5e 147/// If everything is empty then generate some defaults
6111b2b0 148if (empty($data->addtemplate) and empty($data->singletemplate) and
149 empty($data->listtemplate) and empty($data->rsstemplate)) {
150 data_generate_default_template($data, 'singletemplate');
151 data_generate_default_template($data, 'listtemplate');
152 data_generate_default_template($data, 'addtemplate');
153 data_generate_default_template($data, 'asearchtemplate'); //Template for advanced searches.
154 data_generate_default_template($data, 'rsstemplate');
155}
156
4e033542
SH
157editors_head_setup();
158$format = FORMAT_HTML;
6d5b79c5
PS
159
160if ($mode === 'csstemplate' or $mode === 'jstemplate') {
161 $disableeditor = true;
162}
163
5d4ee8b5
AB
164if ($disableeditor) {
165 $format = FORMAT_PLAIN;
166}
20e5da7d 167$editor = editors_get_preferred_editor($format);
4e033542
SH
168$strformats = format_text_menu();
169$formats = $editor->get_supported_formats();
170foreach ($formats as $fid) {
171 $formats[$fid] = $strformats[$fid];
172}
173$options = array();
174$options['trusttext'] = false;
175$options['forcehttps'] = false;
176$options['subdirs'] = false;
177$options['maxfiles'] = 0;
178$options['maxbytes'] = 0;
179$options['changeformat'] = 0;
180$options['noclean'] = false;
6111b2b0 181
182echo '<form id="tempform" action="templates.php?d='.$data->id.'&amp;mode='.$mode.'" method="post">';
183echo '<div>';
184echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
185// Print button to autogen all forms, if all templates are empty
186
187if (!$resettemplate) {
188 // Only reload if we are not resetting the template to default.
189 $data = $DB->get_record('data', array('id'=>$d));
190}
191echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
192echo '<table cellpadding="4" cellspacing="0" border="0">';
c969c287 193
c969c287 194/// Add the HTML editor(s).
5d4ee8b5 195$usehtmleditor = can_use_html_editor() && ($mode != 'csstemplate') && ($mode != 'jstemplate') && !$disableeditor;
6111b2b0 196if ($mode == 'listtemplate'){
197 // Print the list template header.
198 echo '<tr>';
199 echo '<td>&nbsp;</td>';
200 echo '<td>';
649ac85d 201 echo '<div class="template_heading"><label for="edit-listtemplateheader">'.get_string('header','data').'</label></div>';
4e033542
SH
202
203 $field = 'listtemplateheader';
204 $editor->use_editor($field, $options);
205 echo '<div><textarea id="'.$field.'" name="'.$field.'" rows="15" cols="80">'.s($data->listtemplateheader).'</textarea></div>';
206
6111b2b0 207 echo '</td>';
208 echo '</tr>';
209}
210
211// Print the main template.
aab98aaf 212
6111b2b0 213echo '<tr><td valign="top">';
214if ($mode != 'csstemplate' and $mode != 'jstemplate') {
215 // Add all the available fields for this data.
216 echo '<label for="availabletags">'.get_string('availabletags','data').'</label>';
7719b4db 217 echo $OUTPUT->help_icon('availabletags', 'data');
6111b2b0 218 echo '<br />';
be6d38ea 219
aab98aaf 220
6111b2b0 221 echo '<select name="fields1[]" id="availabletags" size="12" onclick="insert_field_tags(this)">';
aab98aaf 222
6111b2b0 223 $fields = $DB->get_records('data_fields', array('dataid'=>$data->id));
224 echo '<optgroup label="'.get_string('fields', 'data').'">';
225 foreach ($fields as $field) {
226 echo '<option value="[['.$field->name.']]" title="'.$field->description.'">'.$field->name.' - [['.$field->name.']]</option>';
227 }
228 echo '</optgroup>';
aab98aaf 229
6111b2b0 230 if ($mode == 'addtemplate') {
231 echo '<optgroup label="'.get_string('fieldids', 'data').'">';
be6d38ea 232 foreach ($fields as $field) {
6111b2b0 233 if (in_array($field->type, array('picture', 'checkbox', 'date', 'latlong', 'radiobutton'))) {
234 continue; //ids are not usable for these composed items
235 }
236 echo '<option value="[['.$field->name.'#id]]" title="'.$field->description.' id">'.$field->name.' id - [['.$field->name.'#id]]</option>';
be6d38ea 237 }
6871e4e4 238 echo '</optgroup>';
6111b2b0 239 }
a5adbd0c 240
6111b2b0 241 // Print special tags. fix for MDL-7031
242 if ($mode != 'addtemplate' && $mode != 'asearchtemplate') { //Don't print special tags when viewing the advanced search template and add template.
243 echo '<optgroup label="'.get_string('buttons', 'data').'">';
244 echo '<option value="##edit##">' .get_string('edit', 'data'). ' - ##edit##</option>';
245 echo '<option value="##delete##">' .get_string('delete', 'data'). ' - ##delete##</option>';
246 echo '<option value="##approve##">' .get_string('approve', 'data'). ' - ##approve##</option>';
247 if ($mode != 'rsstemplate') {
248 echo '<option value="##export##">' .get_string('export', 'data'). ' - ##export##</option>';
a5adbd0c 249 }
6111b2b0 250 if ($mode != 'singletemplate') {
251 // more points to single template - not useable there
252 echo '<option value="##more##">' .get_string('more', 'data'). ' - ##more##</option>';
253 echo '<option value="##moreurl##">' .get_string('moreurl', 'data'). ' - ##moreurl##</option>';
be6d38ea 254 }
6111b2b0 255 echo '</optgroup>';
256 echo '<optgroup label="'.get_string('other', 'data').'">';
257 echo '<option value="##timeadded##">'.get_string('timeadded', 'data'). ' - ##timeadded##</option>';
258 echo '<option value="##timemodified##">'.get_string('timemodified', 'data'). ' - ##timemodified##</option>';
259 echo '<option value="##user##">' .get_string('user'). ' - ##user##</option>';
260 if ($mode != 'singletemplate') {
261 // more points to single template - not useable there
262 echo '<option value="##comments##">' .get_string('comments', 'data'). ' - ##comments##</option>';
714bec74 263 }
6111b2b0 264 echo '</optgroup>';
265 }
714bec74 266
6111b2b0 267 if ($mode == 'asearchtemplate') {
268 echo '<optgroup label="'.get_string('other', 'data').'">';
269 echo '<option value="##firstname##">' .get_string('authorfirstname', 'data'). ' - ##firstname##</option>';
270 echo '<option value="##lastname##">' .get_string('authorlastname', 'data'). ' - ##lastname##</option>';
271 echo '</optgroup>';
272 }
273
274 echo '</select>';
275 echo '<br /><br /><br /><br /><input type="submit" name="defaultform" value="'.get_string('resettemplate','data').'" />';
276 if (can_use_html_editor()) {
277 echo '<br /><br />';
278 if ($usehtmleditor) {
279 $switcheditor = get_string('editordisable', 'data');
5d4ee8b5 280 echo '<input type="submit" name="switcheditor" value="'.s($switcheditor).'" />';
6111b2b0 281 } else {
282 $switcheditor = get_string('editorenable', 'data');
5d4ee8b5 283 echo '<input type="submit" name="useeditor" value="'.s($switcheditor).'" />';
0c097262 284 }
0c097262 285 }
6111b2b0 286} else {
287 echo '<br /><br /><br /><br /><input type="submit" name="defaultform" value="'.get_string('resettemplate','data').'" />';
288}
289echo '</td>';
290
37f796ea 291echo '<td valign="top">';
6111b2b0 292if ($mode == 'listtemplate'){
649ac85d 293 echo '<div class="template_heading"><label for="edit-template">'.get_string('multientry','data').'</label></div>';
6111b2b0 294} else {
649ac85d 295 echo '<div class="template_heading"><label for="edit-template">'.get_string($mode,'data').'</label></div>';
6111b2b0 296}
297
4e033542
SH
298$field = 'template';
299$editor->use_editor($field, $options);
300echo '<div><textarea id="'.$field.'" name="'.$field.'" rows="15" cols="80">'.s($data->{$mode}).'</textarea></div>';
6111b2b0 301echo '</td>';
302echo '</tr>';
303
304if ($mode == 'listtemplate'){
305 echo '<tr>';
306 echo '<td>&nbsp;</td>';
307 echo '<td>';
649ac85d 308 echo '<div class="template_heading"><label for="edit-listtemplatefooter">'.get_string('footer','data').'</label></div>';
4e033542
SH
309
310 $field = 'listtemplatefooter';
311 $editor->use_editor($field, $options);
312 echo '<div><textarea id="'.$field.'" name="'.$field.'" rows="15" cols="80">'.s($data->listtemplatefooter).'</textarea></div>';
c969c287 313 echo '</td>';
6111b2b0 314 echo '</tr>';
315} else if ($mode == 'rsstemplate') {
316 echo '<tr>';
317 echo '<td>&nbsp;</td>';
c969c287 318 echo '<td>';
649ac85d 319 echo '<div class="template_heading"><label for="edit-rsstitletemplate">'.get_string('rsstitletemplate','data').'</label></div>';
4e033542
SH
320
321 $field = 'rsstitletemplate';
322 $editor->use_editor($field, $options);
323 echo '<div><textarea id="'.$field.'" name="'.$field.'" rows="15" cols="80">'.s($data->rsstitletemplate).'</textarea></div>';
c969c287 324 echo '</td>';
325 echo '</tr>';
6111b2b0 326}
aab98aaf 327
649ac85d 328echo '<tr><td class="save_template" colspan="2">';
6111b2b0 329echo '<input type="submit" value="'.get_string('savetemplate','data').'" />&nbsp;';
aab98aaf 330
6111b2b0 331echo '</td></tr></table>';
aab98aaf 332
333
6111b2b0 334echo $OUTPUT->box_end();
335echo '</div>';
336echo '</form>';
3d4b223a 337
338/// Finish the page
6111b2b0 339echo $OUTPUT->footer();