Fix a possible race condition in the PaintWeb DML code.
[moodle/mihaisucan.git] / mod / data / templates.php
blob2ec039c7661962c8402c5eadbebe3985a8b28ad7
1 <?php // $Id$
2 ///////////////////////////////////////////////////////////////////////////
3 // //
4 // NOTICE OF COPYRIGHT //
5 // //
6 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
7 // http://moodle.org //
8 // //
9 // Copyright (C) 2005 Martin Dougiamas http://dougiamas.com //
10 // //
11 // This program is free software; you can redistribute it and/or modify //
12 // it under the terms of the GNU General Public License as published by //
13 // the Free Software Foundation; either version 2 of the License, or //
14 // (at your option) any later version. //
15 // //
16 // This program is distributed in the hope that it will be useful, //
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
19 // GNU General Public License for more details: //
20 // //
21 // http://www.gnu.org/copyleft/gpl.html //
22 // //
23 ///////////////////////////////////////////////////////////////////////////
25 require_once('../../config.php');
26 require_once('lib.php');
27 require_once($CFG->libdir.'/blocklib.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);
33 if ($id) {
34 if (! $cm = get_coursemodule_from_id('data', $id)) {
35 error('Course Module ID was incorrect');
37 if (! $course = get_record('course', 'id', $cm->course)) {
38 error('Course is misconfigured');
40 if (! $data = get_record('data', 'id', $cm->instance)) {
41 error('Course module is incorrect');
44 } else {
45 if (! $data = get_record('data', 'id', $d)) {
46 error('Data ID is incorrect');
48 if (! $course = get_record('course', 'id', $data->course)) {
49 error('Course is misconfigured');
51 if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
52 error('Course Module ID was incorrect');
56 require_login($course->id, false, $cm);
58 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
59 require_capability('mod/data:managetemplates', $context);
61 if (!count_records('data_fields','dataid',$data->id)) { // Brand new database!
62 redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id); // Redirect to field entry
65 add_to_log($course->id, 'data', 'templates view', "templates.php?id=$cm->id&amp;d=$data->id", $data->id, $cm->id);
68 /// Print the page header
70 $strdata = get_string('modulenameplural','data');
72 // For the javascript for inserting template tags: initialise the default textarea to
73 // 'edit_template' - it is always present in all different possible views.
75 $editorobj = 'editor_'.md5('template');
77 $bodytag = 'onload="';
78 $bodytag .= 'if (typeof('.$editorobj.') != \'undefined\') { currEditor = '.$editorobj.'; } ';
79 $bodytag .= 'currTextarea = document.getElementById(\'tempform\').template;';
80 $bodytag .= '" ';
82 // Javascript to insert the field tags into the textarea.
83 $meta = '<script type="text/javascript">'."\n";
84 $meta .= '//<![CDATA['."\n";
85 $meta .= 'function insert_field_tags(selectlist) {';
86 $meta .= ' if (typeof(currEditor) != \'undefined\' && currEditor._editMode == \'wysiwyg\') {';
87 // HTMLArea-specific
88 $meta .= ' currEditor.insertHTML(selectlist.options[selectlist.selectedIndex].value); ';
89 $meta .= ' } else {';
90 // For inserting when in HTMLArea code view or for normal textareas
91 $meta .= ' insertAtCursor(currTextarea, selectlist.options[selectlist.selectedIndex].value);';
92 $meta .= ' }'."\n";
93 $meta .= '}'."\n";
94 $meta .= '//]]>'."\n";
95 $meta .= '</script>'."\n";
97 $navigation = build_navigation('', $cm);
98 print_header_simple($data->name, '', $navigation,
99 '', $meta, true, update_module_button($cm->id, $course->id, get_string('modulename', 'data')),
100 navmenu($course, $cm), '', $bodytag);
102 print_heading(format_string($data->name));
105 /// Groups needed for Add entry tab
106 $currentgroup = groups_get_activity_group($cm);
107 $groupmode = groups_get_activity_groupmode($cm);
109 /// Print the tabs.
110 $currenttab = 'templates';
111 include('tabs.php');
113 /// Processing submitted data, i.e updating form.
114 $resettemplate = false;
116 /// html editor is by default disabled
117 $editor = isset($SESSION->data_use_editor) ? $SESSION->data_use_editor : (can_use_html_editor() ? 1 : 0);
119 if (($mytemplate = data_submitted($CFG->wwwroot.'/mod/data/templates.php')) && confirm_sesskey()) {
120 $newtemplate->id = $data->id;
121 $newtemplate->{$mode} = $mytemplate->template;
123 if (!empty($mytemplate->switcheditor)) {
124 $editor = $editor ? 0 : 1;
125 $SESSION->data_use_editor = $editor;
126 } else if (!empty($mytemplate->defaultform)) {
127 // Reset the template to default, but don't save yet.
128 $resettemplate = true;
129 $data->{$mode} = data_generate_default_template($data, $mode, 0, false, false);
130 if ($mode == 'listtemplate') {
131 $data->listtemplateheader = '';
132 $data->listtemplatefooter = '';
134 } else {
135 if (isset($mytemplate->listtemplateheader)){
136 $newtemplate->listtemplateheader = $mytemplate->listtemplateheader;
138 if (isset($mytemplate->listtemplatefooter)){
139 $newtemplate->listtemplatefooter = $mytemplate->listtemplatefooter;
141 if (isset($mytemplate->rsstitletemplate)){
142 $newtemplate->rsstitletemplate = $mytemplate->rsstitletemplate;
145 // Check for multiple tags, only need to check for add template.
146 if ($mode != 'addtemplate' or data_tags_check($data->id, $newtemplate->{$mode})) {
147 if (update_record('data', $newtemplate)) {
148 notify(get_string('templatesaved', 'data'), 'notifysuccess');
151 add_to_log($course->id, 'data', 'templates saved', "templates.php?id=$cm->id&amp;d=$data->id", $data->id, $cm->id);
153 } else {
154 echo '<div class="littleintro" style="text-align:center">'.get_string('header'.$mode,'data').'</div>';
157 /// If everything is empty then generate some defaults
158 if (empty($data->addtemplate) and empty($data->singletemplate) and
159 empty($data->listtemplate) and empty($data->rsstemplate)) {
160 data_generate_default_template($data, 'singletemplate');
161 data_generate_default_template($data, 'listtemplate');
162 data_generate_default_template($data, 'addtemplate');
163 data_generate_default_template($data, 'asearchtemplate'); //Template for advanced searches.
164 data_generate_default_template($data, 'rsstemplate');
168 echo '<form id="tempform" action="templates.php?d='.$data->id.'&amp;mode='.$mode.'" method="post">';
169 echo '<div>';
170 echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
171 // Print button to autogen all forms, if all templates are empty
173 if (!$resettemplate) {
174 // Only reload if we are not resetting the template to default.
175 $data = get_record('data', 'id', $d);
177 print_simple_box_start('center','80%');
178 echo '<table cellpadding="4" cellspacing="0" border="0">';
180 /// Add the HTML editor(s).
181 $usehtmleditor = $editor && can_use_html_editor() && ($mode != 'csstemplate') && ($mode != 'jstemplate');
182 if ($mode == 'listtemplate'){
183 // Print the list template header.
184 echo '<tr>';
185 echo '<td>&nbsp;</td>';
186 echo '<td>';
187 echo '<div style="text-align:center"><label for="edit-listtemplateheader">'.get_string('header','data').'</label></div>';
188 print_textarea($usehtmleditor, 10, 72, 0, 0, 'listtemplateheader', $data->listtemplateheader);
189 echo '</td>';
190 echo '</tr>';
193 // Print the main template.
195 echo '<tr><td valign="top">';
196 if ($mode != 'csstemplate' and $mode != 'jstemplate') {
197 // Add all the available fields for this data.
198 echo '<label for="availabletags">'.get_string('availabletags','data').'</label>';
199 helpbutton('tags', get_string('tags'), 'data');
200 echo '<br />';
203 echo '<select name="fields1[]" id="availabletags" size="12" onclick="insert_field_tags(this)">';
205 $fields = get_records('data_fields', 'dataid', $data->id);
206 echo '<optgroup label="'.get_string('fields', 'data').'">';
207 foreach ($fields as $field) {
208 echo '<option value="[['.$field->name.']]" title="'.$field->description.'">'.$field->name.' - [['.$field->name.']]</option>';
210 echo '</optgroup>';
212 if ($mode == 'addtemplate') {
213 echo '<optgroup label="'.get_string('fieldids', 'data').'">';
214 foreach ($fields as $field) {
215 if (in_array($field->type, array('picture', 'checkbox', 'date', 'latlong', 'radiobutton'))) {
216 continue; //ids are not usable for these composed items
218 echo '<option value="[['.$field->name.'#id]]" title="'.$field->description.' id">'.$field->name.' id - [['.$field->name.'#id]]</option>';
220 echo '</optgroup>';
223 // Print special tags. fix for MDL-7031
224 if ($mode != 'addtemplate' && $mode != 'asearchtemplate') { //Don't print special tags when viewing the advanced search template and add template.
225 echo '<optgroup label="'.get_string('buttons', 'data').'">';
226 echo '<option value="##edit##">' .get_string('edit', 'data'). ' - ##edit##</option>';
227 echo '<option value="##delete##">' .get_string('delete', 'data'). ' - ##delete##</option>';
228 echo '<option value="##approve##">' .get_string('approve', 'data'). ' - ##approve##</option>';
229 if ($mode != 'singletemplate') {
230 // more points to single template - not useable there
231 echo '<option value="##more##">' .get_string('more', 'data'). ' - ##more##</option>';
232 echo '<option value="##moreurl##">' .get_string('moreurl', 'data'). ' - ##moreurl##</option>';
234 echo '</optgroup>';
235 echo '<optgroup label="'.get_string('other', 'data').'">';
236 echo '<option value="##timeadded##">'.get_string('timeadded', 'data'). ' - ##timeadded##</option>';
237 echo '<option value="##timemodified##">'.get_string('timemodified', 'data'). ' - ##timemodified##</option>';
238 echo '<option value="##user##">' .get_string('user'). ' - ##user##</option>';
239 if ($mode != 'singletemplate') {
240 // more points to single template - not useable there
241 echo '<option value="##comments##">' .get_string('comments', 'data'). ' - ##comments##</option>';
243 echo '</optgroup>';
246 if ($mode == 'asearchtemplate') {
247 echo '<optgroup label="'.get_string('other', 'data').'">';
248 echo '<option value="##firstname##">' .get_string('authorfirstname', 'data'). ' - ##firstname##</option>';
249 echo '<option value="##lastname##">' .get_string('authorlastname', 'data'). ' - ##lastname##</option>';
250 echo '</optgroup>';
253 echo '</select>';
254 echo '<br /><br /><br /><br /><input type="submit" name="defaultform" value="'.get_string('resettemplate','data').'" />';
255 if (can_use_html_editor()) {
256 echo '<br /><br />';
257 if ($editor) {
258 $switcheditor = get_string('editordisable', 'data');
259 } else {
260 $switcheditor = get_string('editorenable', 'data');
262 echo '<input type="submit" name="switcheditor" value="'.s($switcheditor).'" />';
264 } else {
265 echo '<br /><br /><br /><br /><input type="submit" name="defaultform" value="'.get_string('resettemplate','data').'" />';
267 echo '</td>';
269 echo '<td>';
270 if ($mode == 'listtemplate'){
271 echo '<div style="text-align:center"><label for="edit-template">'.get_string('multientry','data').'</label></div>';
272 } else {
273 echo '<div style="text-align:center"><label for="edit-template">'.get_string($mode,'data').'</label></div>';
276 print_textarea($usehtmleditor, 20, 72, 0, 0, 'template', $data->{$mode});
277 echo '</td>';
278 echo '</tr>';
280 if ($mode == 'listtemplate'){
281 echo '<tr>';
282 echo '<td>&nbsp;</td>';
283 echo '<td>';
284 echo '<div style="text-align:center"><label for="edit-listtemplatefooter">'.get_string('footer','data').'</label></div>';
285 print_textarea($usehtmleditor, 10, 72, 0, 0, 'listtemplatefooter', $data->listtemplatefooter);
286 echo '</td>';
287 echo '</tr>';
288 } else if ($mode == 'rsstemplate') {
289 echo '<tr>';
290 echo '<td>&nbsp;</td>';
291 echo '<td>';
292 echo '<div style="text-align:center"><label for="edit-rsstitletemplate">'.get_string('rsstitletemplate','data').'</label></div>';
293 print_textarea($usehtmleditor, 10, 72, 0, 0, 'rsstitletemplate', $data->rsstitletemplate);
294 echo '</td>';
295 echo '</tr>';
298 echo '<tr><td style="text-align:center" colspan="2">';
299 echo '<input type="submit" value="'.get_string('savetemplate','data').'" />&nbsp;';
301 echo '</td></tr></table>';
304 print_simple_box_end();
305 echo '</div>';
306 echo '</form>';
307 if ($usehtmleditor) {
308 use_html_editor('template');
309 if ($mode == 'listtemplate'){
310 use_html_editor('listtemplateheader');
311 use_html_editor('listtemplatefooter');
312 } else if ($mode == 'rsstemplate'){
313 use_html_editor('rsstitletemplate');
317 /// Finish the page
318 print_footer($course);