90cf012aa0e843de89ddcc48f748775e4ddd37d0
[moodle.git] / mod / data / templates.php
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.
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/>.
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  */
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_ALPHA);
34 $url = new moodle_url('/mod/data/templates.php');
35 if ($mode !== 'singletemplate') {
36     $url->param('mode', $mode);
37 }
39 if ($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     }
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');
63     }
64 }
66 require_login($course->id, false, $cm);
68 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
69 require_capability('mod/data:managetemplates', $context);
71 if (!$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 }
75 add_to_log($course->id, 'data', 'templates view', "templates.php?id=$cm->id&amp;d=$data->id", $data->id, $cm->id);
78 /// Print the page header
80 $strdata = get_string('modulenameplural','data');
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.
85 if ($mode == 'singletemplate') {
86     $PAGE->navbar->add(get_string($mode,'data'));
87 }
89 $PAGE->requires->js('/mod/data/data.js');
90 $PAGE->set_title($data->name);
91 $PAGE->set_heading($course->fullname);
92 echo $OUTPUT->header();
93 echo $OUTPUT->heading(format_string($data->name));
96 /// Groups needed for Add entry tab
97 $currentgroup = groups_get_activity_group($cm);
98 $groupmode = groups_get_activity_groupmode($cm);
100 /// Print the tabs.
101 $currenttab = 'templates';
102 include('tabs.php');
104 /// Processing submitted data, i.e updating form.
105 $resettemplate = false;
107 if (($mytemplate = data_submitted()) && confirm_sesskey()) {
108     $newtemplate = new stdClass();
109     $newtemplate->id = $data->id;
110     $newtemplate->{$mode} = $mytemplate->template;
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         }
131         // Check for multiple tags, only need to check for add template.
132         if ($mode != 'addtemplate' or data_tags_check($data->id, $newtemplate->{$mode})) {
133             $DB->update_record('data', $newtemplate);
134             echo $OUTPUT->notification(get_string('templatesaved', 'data'), 'notifysuccess');
135         }
136         add_to_log($course->id, 'data', 'templates saved', "templates.php?id=$cm->id&amp;d=$data->id", $data->id, $cm->id);
137     }
138 } else {
139     echo '<div class="littleintro" style="text-align:center">'.get_string('header'.$mode,'data').'</div>';
142 /// If everything is empty then generate some defaults
143 if (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');
152 editors_head_setup();
153 $format = FORMAT_HTML;
155 if ($mode === 'csstemplate' or $mode === 'jstemplate') {
156     $disableeditor = true;
159 if ($disableeditor) {
160     $format = FORMAT_PLAIN;
162 $editor = editors_get_preferred_editor($format);
163 $strformats = format_text_menu();
164 $formats =  $editor->get_supported_formats();
165 foreach ($formats as $fid) {
166     $formats[$fid] = $strformats[$fid];
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;
177 echo '<form id="tempform" action="templates.php?d='.$data->id.'&amp;mode='.$mode.'" method="post">';
178 echo '<div>';
179 echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
180 // Print button to autogen all forms, if all templates are empty
182 if (!$resettemplate) {
183     // Only reload if we are not resetting the template to default.
184     $data = $DB->get_record('data', array('id'=>$d));
186 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
187 echo '<table cellpadding="4" cellspacing="0" border="0">';
189 /// Add the HTML editor(s).
190 $usehtmleditor = can_use_html_editor() && ($mode != 'csstemplate') && ($mode != 'jstemplate') && !$disableeditor;
191 if ($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>';
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>';
202     echo '</td>';
203     echo '</tr>';
206 // Print the main template.
208 echo '<tr><td valign="top">';
209 if ($mode != 'csstemplate' and $mode != 'jstemplate') {
210     // Add all the available fields for this data.
211     echo '<label for="availabletags">'.get_string('availabletags','data').'</label>';
212     echo $OUTPUT->help_icon('availabletags', 'data');
213     echo '<br />';
216     echo '<select name="fields1[]" id="availabletags" size="12" onclick="insert_field_tags(this)">';
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>';
225     if ($mode == 'addtemplate') {
226         echo '<optgroup label="'.get_string('fieldids', 'data').'">';
227         foreach ($fields as $field) {
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>';
232         }
233         echo '</optgroup>';
234     }
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>';
244         }
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>';
249         }
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>';
258         }
259         echo '</optgroup>';
260     }
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     }
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');
275             echo '<input type="submit" name="switcheditor" value="'.s($switcheditor).'" />';
276         } else {
277             $switcheditor = get_string('editorenable', 'data');
278             echo '<input type="submit" name="useeditor" value="'.s($switcheditor).'" />';
279         }
280     }
281 } else {
282     echo '<br /><br /><br /><br /><input type="submit" name="defaultform" value="'.get_string('resettemplate','data').'" />';
284 echo '</td>';
286 echo '<td valign="top">';
287 if ($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>';
293 $field = 'template';
294 $editor->use_editor($field, $options);
295 echo '<div><textarea id="'.$field.'" name="'.$field.'" rows="15" cols="80">'.s($data->{$mode}).'</textarea></div>';
296 echo '</td>';
297 echo '</tr>';
299 if ($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>';
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>';
308     echo '</td>';
309     echo '</tr>';
310 } else if ($mode == 'rsstemplate') {
311     echo '<tr>';
312     echo '<td>&nbsp;</td>';
313     echo '<td>';
314     echo '<div style="text-align:center"><label for="edit-rsstitletemplate">'.get_string('rsstitletemplate','data').'</label></div>';
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>';
319     echo '</td>';
320     echo '</tr>';
323 echo '<tr><td style="text-align:center" colspan="2">';
324 echo '<input type="submit" value="'.get_string('savetemplate','data').'" />&nbsp;';
326 echo '</td></tr></table>';
329 echo $OUTPUT->box_end();
330 echo '</div>';
331 echo '</form>';
333 /// Finish the page
334 echo $OUTPUT->footer();