mod-data MDL-25671 disable editor in data list template editor will not save the...
[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
6111b2b0 67require_login($course->id, false, $cm);
c088d603 68
6111b2b0 69$context = get_context_instance(CONTEXT_MODULE, $cm->id);
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);
6111b2b0 93echo $OUTPUT->header();
94echo $OUTPUT->heading(format_string($data->name));
10aba627 95
704a26cb 96
cca1547e 97/// Groups needed for Add entry tab
6111b2b0 98$currentgroup = groups_get_activity_group($cm);
99$groupmode = groups_get_activity_groupmode($cm);
cca1547e 100
10aba627 101/// Print the tabs.
6111b2b0 102$currenttab = 'templates';
103include('tabs.php');
10aba627 104
105/// Processing submitted data, i.e updating form.
6111b2b0 106$resettemplate = false;
107
108if (($mytemplate = data_submitted()) && confirm_sesskey()) {
39790bd8 109 $newtemplate = new stdClass();
6111b2b0 110 $newtemplate->id = $data->id;
111 $newtemplate->{$mode} = $mytemplate->template;
112
113 if (!empty($mytemplate->defaultform)) {
114 // Reset the template to default, but don't save yet.
115 $resettemplate = true;
116 $data->{$mode} = data_generate_default_template($data, $mode, 0, false, false);
117 if ($mode == 'listtemplate') {
118 $data->listtemplateheader = '';
119 $data->listtemplatefooter = '';
120 }
121 } else {
122 if (isset($mytemplate->listtemplateheader)){
123 $newtemplate->listtemplateheader = $mytemplate->listtemplateheader;
124 }
125 if (isset($mytemplate->listtemplatefooter)){
126 $newtemplate->listtemplatefooter = $mytemplate->listtemplatefooter;
127 }
128 if (isset($mytemplate->rsstitletemplate)){
129 $newtemplate->rsstitletemplate = $mytemplate->rsstitletemplate;
130 }
ea6a8345 131
6111b2b0 132 // Check for multiple tags, only need to check for add template.
133 if ($mode != 'addtemplate' or data_tags_check($data->id, $newtemplate->{$mode})) {
08a7fbb0
DC
134 // if disableeditor or enableeditor buttons click, don't save instance
135 if (empty($disableeditor) && empty($enableeditor)) {
136 $DB->update_record('data', $newtemplate);
137 echo $OUTPUT->notification(get_string('templatesaved', 'data'), 'notifysuccess');
138 add_to_log($course->id, 'data', 'templates saved', "templates.php?id=$cm->id&amp;d=$data->id", $data->id, $cm->id);
139 }
3d4b223a 140 }
141 }
6111b2b0 142} else {
143 echo '<div class="littleintro" style="text-align:center">'.get_string('header'.$mode,'data').'</div>';
144}
3d4b223a 145
d74a6d5e 146/// If everything is empty then generate some defaults
6111b2b0 147if (empty($data->addtemplate) and empty($data->singletemplate) and
148 empty($data->listtemplate) and empty($data->rsstemplate)) {
149 data_generate_default_template($data, 'singletemplate');
150 data_generate_default_template($data, 'listtemplate');
151 data_generate_default_template($data, 'addtemplate');
152 data_generate_default_template($data, 'asearchtemplate'); //Template for advanced searches.
153 data_generate_default_template($data, 'rsstemplate');
154}
155
4e033542
SH
156editors_head_setup();
157$format = FORMAT_HTML;
6d5b79c5
PS
158
159if ($mode === 'csstemplate' or $mode === 'jstemplate') {
160 $disableeditor = true;
161}
162
5d4ee8b5
AB
163if ($disableeditor) {
164 $format = FORMAT_PLAIN;
165}
20e5da7d 166$editor = editors_get_preferred_editor($format);
4e033542
SH
167$strformats = format_text_menu();
168$formats = $editor->get_supported_formats();
169foreach ($formats as $fid) {
170 $formats[$fid] = $strformats[$fid];
171}
172$options = array();
173$options['trusttext'] = false;
174$options['forcehttps'] = false;
175$options['subdirs'] = false;
176$options['maxfiles'] = 0;
177$options['maxbytes'] = 0;
178$options['changeformat'] = 0;
179$options['noclean'] = false;
6111b2b0 180
181echo '<form id="tempform" action="templates.php?d='.$data->id.'&amp;mode='.$mode.'" method="post">';
182echo '<div>';
183echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
184// Print button to autogen all forms, if all templates are empty
185
186if (!$resettemplate) {
187 // Only reload if we are not resetting the template to default.
188 $data = $DB->get_record('data', array('id'=>$d));
189}
190echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
191echo '<table cellpadding="4" cellspacing="0" border="0">';
c969c287 192
c969c287 193/// Add the HTML editor(s).
5d4ee8b5 194$usehtmleditor = can_use_html_editor() && ($mode != 'csstemplate') && ($mode != 'jstemplate') && !$disableeditor;
6111b2b0 195if ($mode == 'listtemplate'){
196 // Print the list template header.
197 echo '<tr>';
198 echo '<td>&nbsp;</td>';
199 echo '<td>';
200 echo '<div style="text-align:center"><label for="edit-listtemplateheader">'.get_string('header','data').'</label></div>';
4e033542
SH
201
202 $field = 'listtemplateheader';
203 $editor->use_editor($field, $options);
204 echo '<div><textarea id="'.$field.'" name="'.$field.'" rows="15" cols="80">'.s($data->listtemplateheader).'</textarea></div>';
205
6111b2b0 206 echo '</td>';
207 echo '</tr>';
208}
209
210// Print the main template.
aab98aaf 211
6111b2b0 212echo '<tr><td valign="top">';
213if ($mode != 'csstemplate' and $mode != 'jstemplate') {
214 // Add all the available fields for this data.
215 echo '<label for="availabletags">'.get_string('availabletags','data').'</label>';
7719b4db 216 echo $OUTPUT->help_icon('availabletags', 'data');
6111b2b0 217 echo '<br />';
be6d38ea 218
aab98aaf 219
6111b2b0 220 echo '<select name="fields1[]" id="availabletags" size="12" onclick="insert_field_tags(this)">';
aab98aaf 221
6111b2b0 222 $fields = $DB->get_records('data_fields', array('dataid'=>$data->id));
223 echo '<optgroup label="'.get_string('fields', 'data').'">';
224 foreach ($fields as $field) {
225 echo '<option value="[['.$field->name.']]" title="'.$field->description.'">'.$field->name.' - [['.$field->name.']]</option>';
226 }
227 echo '</optgroup>';
aab98aaf 228
6111b2b0 229 if ($mode == 'addtemplate') {
230 echo '<optgroup label="'.get_string('fieldids', 'data').'">';
be6d38ea 231 foreach ($fields as $field) {
6111b2b0 232 if (in_array($field->type, array('picture', 'checkbox', 'date', 'latlong', 'radiobutton'))) {
233 continue; //ids are not usable for these composed items
234 }
235 echo '<option value="[['.$field->name.'#id]]" title="'.$field->description.' id">'.$field->name.' id - [['.$field->name.'#id]]</option>';
be6d38ea 236 }
6871e4e4 237 echo '</optgroup>';
6111b2b0 238 }
a5adbd0c 239
6111b2b0 240 // Print special tags. fix for MDL-7031
241 if ($mode != 'addtemplate' && $mode != 'asearchtemplate') { //Don't print special tags when viewing the advanced search template and add template.
242 echo '<optgroup label="'.get_string('buttons', 'data').'">';
243 echo '<option value="##edit##">' .get_string('edit', 'data'). ' - ##edit##</option>';
244 echo '<option value="##delete##">' .get_string('delete', 'data'). ' - ##delete##</option>';
245 echo '<option value="##approve##">' .get_string('approve', 'data'). ' - ##approve##</option>';
246 if ($mode != 'rsstemplate') {
247 echo '<option value="##export##">' .get_string('export', 'data'). ' - ##export##</option>';
a5adbd0c 248 }
6111b2b0 249 if ($mode != 'singletemplate') {
250 // more points to single template - not useable there
251 echo '<option value="##more##">' .get_string('more', 'data'). ' - ##more##</option>';
252 echo '<option value="##moreurl##">' .get_string('moreurl', 'data'). ' - ##moreurl##</option>';
be6d38ea 253 }
6111b2b0 254 echo '</optgroup>';
255 echo '<optgroup label="'.get_string('other', 'data').'">';
256 echo '<option value="##timeadded##">'.get_string('timeadded', 'data'). ' - ##timeadded##</option>';
257 echo '<option value="##timemodified##">'.get_string('timemodified', 'data'). ' - ##timemodified##</option>';
258 echo '<option value="##user##">' .get_string('user'). ' - ##user##</option>';
259 if ($mode != 'singletemplate') {
260 // more points to single template - not useable there
261 echo '<option value="##comments##">' .get_string('comments', 'data'). ' - ##comments##</option>';
714bec74 262 }
6111b2b0 263 echo '</optgroup>';
264 }
714bec74 265
6111b2b0 266 if ($mode == 'asearchtemplate') {
267 echo '<optgroup label="'.get_string('other', 'data').'">';
268 echo '<option value="##firstname##">' .get_string('authorfirstname', 'data'). ' - ##firstname##</option>';
269 echo '<option value="##lastname##">' .get_string('authorlastname', 'data'). ' - ##lastname##</option>';
270 echo '</optgroup>';
271 }
272
273 echo '</select>';
274 echo '<br /><br /><br /><br /><input type="submit" name="defaultform" value="'.get_string('resettemplate','data').'" />';
275 if (can_use_html_editor()) {
276 echo '<br /><br />';
277 if ($usehtmleditor) {
278 $switcheditor = get_string('editordisable', 'data');
5d4ee8b5 279 echo '<input type="submit" name="switcheditor" value="'.s($switcheditor).'" />';
6111b2b0 280 } else {
281 $switcheditor = get_string('editorenable', 'data');
5d4ee8b5 282 echo '<input type="submit" name="useeditor" value="'.s($switcheditor).'" />';
0c097262 283 }
0c097262 284 }
6111b2b0 285} else {
286 echo '<br /><br /><br /><br /><input type="submit" name="defaultform" value="'.get_string('resettemplate','data').'" />';
287}
288echo '</td>';
289
37f796ea 290echo '<td valign="top">';
6111b2b0 291if ($mode == 'listtemplate'){
292 echo '<div style="text-align:center"><label for="edit-template">'.get_string('multientry','data').'</label></div>';
293} else {
294 echo '<div style="text-align:center"><label for="edit-template">'.get_string($mode,'data').'</label></div>';
295}
296
4e033542
SH
297$field = 'template';
298$editor->use_editor($field, $options);
299echo '<div><textarea id="'.$field.'" name="'.$field.'" rows="15" cols="80">'.s($data->{$mode}).'</textarea></div>';
6111b2b0 300echo '</td>';
301echo '</tr>';
302
303if ($mode == 'listtemplate'){
304 echo '<tr>';
305 echo '<td>&nbsp;</td>';
306 echo '<td>';
307 echo '<div style="text-align:center"><label for="edit-listtemplatefooter">'.get_string('footer','data').'</label></div>';
4e033542
SH
308
309 $field = 'listtemplatefooter';
310 $editor->use_editor($field, $options);
311 echo '<div><textarea id="'.$field.'" name="'.$field.'" rows="15" cols="80">'.s($data->listtemplatefooter).'</textarea></div>';
c969c287 312 echo '</td>';
6111b2b0 313 echo '</tr>';
314} else if ($mode == 'rsstemplate') {
315 echo '<tr>';
316 echo '<td>&nbsp;</td>';
c969c287 317 echo '<td>';
6111b2b0 318 echo '<div style="text-align:center"><label for="edit-rsstitletemplate">'.get_string('rsstitletemplate','data').'</label></div>';
4e033542
SH
319
320 $field = 'rsstitletemplate';
321 $editor->use_editor($field, $options);
322 echo '<div><textarea id="'.$field.'" name="'.$field.'" rows="15" cols="80">'.s($data->rsstitletemplate).'</textarea></div>';
c969c287 323 echo '</td>';
324 echo '</tr>';
6111b2b0 325}
aab98aaf 326
6111b2b0 327echo '<tr><td style="text-align:center" colspan="2">';
328echo '<input type="submit" value="'.get_string('savetemplate','data').'" />&nbsp;';
aab98aaf 329
6111b2b0 330echo '</td></tr></table>';
aab98aaf 331
332
6111b2b0 333echo $OUTPUT->box_end();
334echo '</div>';
335echo '</form>';
3d4b223a 336
337/// Finish the page
6111b2b0 338echo $OUTPUT->footer();