2 ///////////////////////////////////////////////////////////////////////////
4 // NOTICE OF COPYRIGHT //
6 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
7 // http://moodle.org //
9 // Copyright (C) 2005 Martin Dougiamas http://dougiamas.com //
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. //
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: //
21 // http://www.gnu.org/copyleft/gpl.html //
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
);
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');
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&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;';
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\') {';
88 $meta .= ' currEditor.insertHTML(selectlist.options[selectlist.selectedIndex].value); ';
90 // For inserting when in HTMLArea code view or for normal textareas
91 $meta .= ' insertAtCursor(currTextarea, selectlist.options[selectlist.selectedIndex].value);';
94 $meta .= '//]]>'."\n";
95 $meta .= '</script>'."\n";
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);
114 $currenttab = 'templates';
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
= '';
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&d=$data->id", $data->id
, $cm->id
);
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
.'&mode='.$mode.'" method="post">';
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.
189 echo '<td> </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
);
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');
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>';
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>';
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>';
243 echo '<br /><br /><br /><br /><input type="submit" name="defaultform" value="'.get_string('resettemplate','data').'" />';
244 if (can_use_html_editor()) {
247 $switcheditor = get_string('editordisable', 'data');
249 $switcheditor = get_string('editorenable', 'data');
251 echo '<input type="submit" name="switcheditor" value="'.s($switcheditor).'" />';
254 echo '<br /><br /><br /><br /><input type="submit" name="defaultform" value="'.get_string('resettemplate','data').'" />';
259 if ($mode == 'listtemplate'){
260 echo '<div style="text-align:center"><label for="edit-template">'.get_string('multientry','data').'</label></div>';
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});
269 if ($mode == 'listtemplate'){
271 echo '<td> </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
);
277 } else if ($mode == 'rsstemplate') {
279 echo '<td> </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
);
287 echo '<tr><td style="text-align:center" colspan="2">';
288 echo '<input type="submit" value="'.get_string('savetemplate','data').'" /> ';
290 echo '</td></tr></table>';
293 print_simple_box_end();
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');
307 print_footer($course);