MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / data / templates.php
blob2bd4ac48c6c92be7e3d68571eb0bb0eea51bf17a
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 $navlinks = array();
98 $navlinks[] = array('name' => $strdata, 'link' => "index.php?id=$course->id", 'type' => 'activity');
99 $navlinks[] = array('name' => format_string($data->name), 'link' => '', 'type' => 'activityinstance');
100 $navigation = build_navigation($navlinks);
102 print_header_simple($data->name, '', $navigation,
103 '', $meta, true, update_module_button($cm->id, $course->id, get_string('modulename', 'data')),
104 navmenu($course, $cm), '', $bodytag);
106 print_heading(format_string($data->name));
109 /// Groups needed for Add entry tab
110 $currentgroup = groups_get_activity_group($cm);
111 $groupmode = groups_get_activity_groupmode($cm);
113 /// Print the tabs.
114 $currenttab = 'templates';
115 include('tabs.php');
117 /// Processing submitted data, i.e updating form.
118 $resettemplate = false;
120 /// html editor is by default disabled
121 $editor = isset($SESSION->data_use_editor) ? $SESSION->data_use_editor : (can_use_html_editor() ? 1 : 0);
123 if (($mytemplate = data_submitted($CFG->wwwroot.'/mod/data/templates.php')) && confirm_sesskey()) {
124 $newtemplate->id = $data->id;
125 $newtemplate->{$mode} = $mytemplate->template;
127 if (!empty($mytemplate->switcheditor)) {
128 $editor = $editor ? 0 : 1;
129 $SESSION->data_use_editor = $editor;
130 } else if (!empty($mytemplate->defaultform)) {
131 // Reset the template to default, but don't save yet.
132 $resettemplate = true;
133 $data->{$mode} = data_generate_default_template($data, $mode, 0, false, false);
134 if ($mode == 'listtemplate') {
135 $data->listtemplateheader = '';
136 $data->listtemplatefooter = '';
138 } else {
139 if (isset($mytemplate->listtemplateheader)){
140 $newtemplate->listtemplateheader = $mytemplate->listtemplateheader;
142 if (isset($mytemplate->listtemplatefooter)){
143 $newtemplate->listtemplatefooter = $mytemplate->listtemplatefooter;
145 if (isset($mytemplate->rsstitletemplate)){
146 $newtemplate->rsstitletemplate = $mytemplate->rsstitletemplate;
149 // Check for multiple tags, only need to check for add template.
150 if ($mode != 'addtemplate' or data_tags_check($data->id, $newtemplate->{$mode})) {
151 if (update_record('data', $newtemplate)) {
152 notify(get_string('templatesaved', 'data'), 'notifysuccess');
155 add_to_log($course->id, 'data', 'templates saved', "templates.php?id=$cm->id&amp;d=$data->id", $data->id, $cm->id);
157 } else {
158 echo '<div class="littleintro" style="text-align:center">'.get_string('header'.$mode,'data').'</div>';
161 /// If everything is empty then generate some defaults
162 if (empty($data->addtemplate) and empty($data->singletemplate) and
163 empty($data->listtemplate) and empty($data->rsstemplate)) {
164 data_generate_default_template($data, 'singletemplate');
165 data_generate_default_template($data, 'listtemplate');
166 data_generate_default_template($data, 'addtemplate');
167 data_generate_default_template($data, 'asearchtemplate'); //Template for advanced searches.
168 data_generate_default_template($data, 'rsstemplate');
172 echo '<form id="tempform" action="templates.php?d='.$data->id.'&amp;mode='.$mode.'" method="post">';
173 echo '<div>';
174 echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
175 // Print button to autogen all forms, if all templates are empty
177 if (!$resettemplate) {
178 // Only reload if we are not resetting the template to default.
179 $data = get_record('data', 'id', $d);
181 print_simple_box_start('center','80%');
182 echo '<table cellpadding="4" cellspacing="0" border="0">';
184 /// Add the HTML editor(s).
185 $usehtmleditor = $editor && can_use_html_editor() && ($mode != 'csstemplate') && ($mode != 'jstemplate');
186 if ($mode == 'listtemplate'){
187 // Print the list template header.
188 echo '<tr>';
189 echo '<td>&nbsp;</td>';
190 echo '<td>';
191 echo '<div style="text-align:center"><label for="edit-listtemplateheader">'.get_string('header','data').'</label></div>';
192 print_textarea($usehtmleditor, 10, 72, 0, 0, 'listtemplateheader', $data->listtemplateheader);
193 echo '</td>';
194 echo '</tr>';
197 // Print the main template.
199 echo '<tr><td valign="top">';
200 if ($mode != 'csstemplate' and $mode != 'jstemplate') {
201 // Add all the available fields for this data.
202 echo '<label for="availabletags">'.get_string('availabletags','data').'</label>';
203 helpbutton('tags', get_string('tags','data'), 'data');
204 echo '<br />';
207 echo '<select name="fields1[]" id="availabletags" size="10" onclick="insert_field_tags(this)">';
209 $fields = get_records('data_fields', 'dataid', $data->id);
210 echo '<optgroup label="'.get_string('fields', 'data').'">';
211 foreach ($fields as $field) {
212 echo '<option value="[['.$field->name.']]" title="'.$field->description.'">'.$field->name.' - [['.$field->name.']]</option>';
214 echo '</optgroup>';
216 if ($mode == 'addtemplate') {
217 echo '<optgroup label="'.get_string('fieldids', 'data').'">';
218 foreach ($fields as $field) {
219 if (in_array($field->type, array('picture', 'checkbox', 'date', 'latlong', 'radiobutton'))) {
220 continue; //ids are not usable for these composed items
222 echo '<option value="[['.$field->name.'#id]]" title="'.$field->description.' id">'.$field->name.' id - [['.$field->name.'#id]]</option>';
224 echo '</optgroup>';
227 // Print special tags. fix for MDL-7031
228 if ($mode != 'addtemplate' && $mode != 'asearchtemplate') { //Don't print special tags when viewing the advanced search template and add template.
229 echo '<optgroup label="'.get_string('buttons', 'data').'">';
230 echo '<option value="##edit##">' .get_string('edit', 'data'). ' - ##edit##</option>';
231 echo '<option value="##delete##">' .get_string('delete', 'data'). ' - ##delete##</option>';
232 echo '<option value="##approve##">' .get_string('approve', 'data'). ' - ##approve##</option>';
233 echo '<option value="##user##">' .get_string('user'). ' - ##user##</option>';
234 if ($mode != 'singletemplate') {
235 // more points to single template - not useable there
236 echo '<option value="##more##">' .get_string('more', 'data'). ' - ##more##</option>';
237 echo '<option value="##moreurl##">' .get_string('moreurl', 'data'). ' - ##moreurl##</option>';
238 echo '<option value="##comments##">' .get_string('comments', 'data'). ' - ##comments##</option>';
240 echo '</optgroup>';
242 echo '</select>';
243 echo '<br /><br /><br /><br /><input type="submit" name="defaultform" value="'.get_string('resettemplate','data').'" />';
244 if (can_use_html_editor()) {
245 echo '<br /><br />';
246 if ($editor) {
247 $switcheditor = get_string('editordisable', 'data');
248 } else {
249 $switcheditor = get_string('editorenable', 'data');
251 echo '<input type="submit" name="switcheditor" value="'.s($switcheditor).'" />';
253 } else {
254 echo '<br /><br /><br /><br /><input type="submit" name="defaultform" value="'.get_string('resettemplate','data').'" />';
256 echo '</td>';
258 echo '<td>';
259 if ($mode == 'listtemplate'){
260 echo '<div style="text-align:center"><label for="edit-template">'.get_string('multientry','data').'</label></div>';
261 } else {
262 echo '<div style="text-align:center"><label for="edit-template">'.get_string($mode,'data').'</label></div>';
265 print_textarea($usehtmleditor, 20, 72, 0, 0, 'template', $data->{$mode});
266 echo '</td>';
267 echo '</tr>';
269 if ($mode == 'listtemplate'){
270 echo '<tr>';
271 echo '<td>&nbsp;</td>';
272 echo '<td>';
273 echo '<div style="text-align:center"><label for="edit-listtemplatefooter">'.get_string('footer','data').'</label></div>';
274 print_textarea($usehtmleditor, 10, 72, 0, 0, 'listtemplatefooter', $data->listtemplatefooter);
275 echo '</td>';
276 echo '</tr>';
277 } else if ($mode == 'rsstemplate') {
278 echo '<tr>';
279 echo '<td>&nbsp;</td>';
280 echo '<td>';
281 echo '<div style="text-align:center"><label for="edit-rsstitletemplate">'.get_string('rsstitletemplate','data').'</label></div>';
282 print_textarea($usehtmleditor, 10, 72, 0, 0, 'rsstitletemplate', $data->rsstitletemplate);
283 echo '</td>';
284 echo '</tr>';
287 echo '<tr><td style="text-align:center" colspan="2">';
288 echo '<input type="submit" value="'.get_string('savetemplate','data').'" />&nbsp;';
290 echo '</td></tr></table>';
293 print_simple_box_end();
294 echo '</div>';
295 echo '</form>';
296 if ($usehtmleditor) {
297 use_html_editor('template');
298 if ($mode == 'listtemplate'){
299 use_html_editor('listtemplateheader');
300 use_html_editor('listtemplatefooter');
301 } else if ($mode == 'rsstemplate'){
302 use_html_editor('rsstitletemplate');
306 /// Finish the page
307 print_footer($course);