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