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