Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / mod / data / templates.php
blobfc21c0a9e5084f7f880528f96bca45fc8f6b15b9
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 require_login();
31 $id = optional_param('id', 0, PARAM_INT); // course module id
32 $d = optional_param('d', 0, PARAM_INT); // database id
33 $mode = optional_param('mode', 'singletemplate', PARAM_ALPHA);
35 if ($id) {
36 if (! $cm = get_coursemodule_from_id('data', $id)) {
37 error('Course Module ID was incorrect');
39 if (! $course = get_record('course', 'id', $cm->course)) {
40 error('Course is misconfigured');
42 if (! $data = get_record('data', 'id', $cm->instance)) {
43 error('Course module is incorrect');
46 } else {
47 if (! $data = get_record('data', 'id', $d)) {
48 error('Data ID is incorrect');
50 if (! $course = get_record('course', 'id', $data->course)) {
51 error('Course is misconfigured');
53 if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
54 error('Course Module ID was incorrect');
58 require_login($course->id, false, $cm);
60 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
61 require_capability('mod/data:managetemplates', $context);
63 if (!count_records('data_fields','dataid',$data->id)) { // Brand new database!
64 redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id); // Redirect to field entry
67 add_to_log($course->id, 'data', 'templates view', "templates.php?id=$cm->id&amp;d=$data->id", $data->id, $cm->id);
70 /// Print the page header
72 $strdata = get_string('modulenameplural','data');
74 // For the javascript for inserting template tags: initialise the default textarea to
75 // 'edit_template' - it is always present in all different possible views.
77 $editorobj = 'editor_'.md5('template');
79 $bodytag = 'onload="';
80 $bodytag .= 'if (typeof('.$editorobj.') != \'undefined\') { currEditor = '.$editorobj.'; } ';
81 $bodytag .= 'currTextarea = document.getElementById(\'tempform\').template;';
82 $bodytag .= '" ';
84 // Javascript to insert the field tags into the textarea.
85 $meta = '<script type="text/javascript">'."\n";
86 $meta .= '//<![CDATA['."\n";
87 $meta .= 'function insert_field_tags(selectlist) {';
88 $meta .= ' if (typeof(currEditor) != \'undefined\' && currEditor._editMode == \'wysiwyg\') {';
89 // HTMLArea-specific
90 $meta .= ' currEditor.insertHTML(selectlist.options[selectlist.selectedIndex].value); ';
91 $meta .= ' } else {';
92 // For inserting when in HTMLArea code view or for normal textareas
93 $meta .= ' insertAtCursor(currTextarea, selectlist.options[selectlist.selectedIndex].value);';
94 $meta .= ' }'."\n";
95 $meta .= '}'."\n";
96 $meta .= '//]]>'."\n";
97 $meta .= '</script>'."\n";
99 $navlinks = array();
100 $navlinks[] = array('name' => $strdata, 'link' => "index.php?id=$course->id", 'type' => 'activity');
101 $navlinks[] = array('name' => format_string($data->name), 'link' => '', 'type' => 'activityinstance');
102 $navigation = build_navigation($navlinks);
104 print_header_simple($data->name, '', $navigation,
105 '', $meta, true, update_module_button($cm->id, $course->id, get_string('modulename', 'data')),
106 navmenu($course, $cm), '', $bodytag);
108 print_heading(format_string($data->name));
111 /// Groups needed for Add entry tab
112 $groupmode = groupmode($course, $cm);
113 $currentgroup = get_and_set_current_group($course, $groupmode);
115 /// Print the tabs.
116 $currenttab = 'templates';
117 include('tabs.php');
119 /// Processing submitted data, i.e updating form.
120 $resettemplate = false;
122 /// html editor is by default disabled
123 $editor = isset($SESSION->data_use_editor) ? $SESSION->data_use_editor : (can_use_html_editor() ? 1 : 0);
125 if (($mytemplate = data_submitted($CFG->wwwroot.'/mod/data/templates.php')) && confirm_sesskey()) {
126 $newtemplate->id = $data->id;
127 $newtemplate->{$mode} = $mytemplate->template;
129 if (!empty($mytemplate->switcheditor)) {
130 $editor = $editor ? 0 : 1;
131 $SESSION->data_use_editor = $editor;
132 } else if (!empty($mytemplate->defaultform)) {
133 // Reset the template to default, but don't save yet.
134 $resettemplate = true;
135 $data->{$mode} = data_generate_default_template($data, $mode, 0, false, false);
136 if ($mode == 'listtemplate') {
137 $data->listtemplateheader = '';
138 $data->listtemplatefooter = '';
140 } else {
141 if (isset($mytemplate->listtemplateheader)){
142 $newtemplate->listtemplateheader = $mytemplate->listtemplateheader;
144 if (isset($mytemplate->listtemplatefooter)){
145 $newtemplate->listtemplatefooter = $mytemplate->listtemplatefooter;
147 if (isset($mytemplate->rsstitletemplate)){
148 $newtemplate->rsstitletemplate = $mytemplate->rsstitletemplate;
151 // Check for multiple tags, only need to check for add template.
152 if ($mode != 'addtemplate' or data_tags_check($data->id, $newtemplate->{$mode})) {
153 if (update_record('data', $newtemplate)) {
154 notify(get_string('templatesaved', 'data'), 'notifysuccess');
157 add_to_log($course->id, 'data', 'templates saved', "templates.php?id=$cm->id&amp;d=$data->id", $data->id, $cm->id);
159 } else {
160 echo '<div class="littleintro" style="text-align:center">'.get_string('header'.$mode,'data').'</div>';
163 /// If everything is empty then generate some defaults
164 if (empty($data->addtemplate) and empty($data->singletemplate) and
165 empty($data->listtemplate) and empty($data->rsstemplate)) {
166 data_generate_default_template($data, 'singletemplate');
167 data_generate_default_template($data, 'listtemplate');
168 data_generate_default_template($data, 'addtemplate');
169 data_generate_default_template($data, 'asearchtemplate'); //Template for advanced searches.
170 data_generate_default_template($data, 'rsstemplate');
174 echo '<form id="tempform" action="templates.php?d='.$data->id.'&amp;mode='.$mode.'" method="post">';
175 echo '<div>';
176 echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
177 // Print button to autogen all forms, if all templates are empty
179 if (!$resettemplate) {
180 // Only reload if we are not resetting the template to default.
181 $data = get_record('data', 'id', $d);
183 print_simple_box_start('center','80%');
184 echo '<table cellpadding="4" cellspacing="0" border="0">';
186 /// Add the HTML editor(s).
187 $usehtmleditor = $editor && can_use_html_editor() && ($mode != 'csstemplate') && ($mode != 'jstemplate');
188 if ($mode == 'listtemplate'){
189 // Print the list template header.
190 echo '<tr>';
191 echo '<td>&nbsp;</td>';
192 echo '<td>';
193 echo '<div style="text-align:center"><label for="edit-listtemplateheader">'.get_string('header','data').'</label></div>';
194 print_textarea($usehtmleditor, 10, 72, 0, 0, 'listtemplateheader', $data->listtemplateheader);
195 echo '</td>';
196 echo '</tr>';
199 // Print the main template.
201 echo '<tr><td valign="top">';
202 if ($mode != 'csstemplate' and $mode != 'jstemplate') {
203 // Add all the available fields for this data.
204 echo '<label for="availabletags">'.get_string('availabletags','data').'</label>';
205 helpbutton('tags', get_string('tags','data'), 'data');
206 echo '<br />';
209 echo '<select name="fields1[]" id="availabletags" size="10" onclick="insert_field_tags(this)">';
211 $fields = get_records('data_fields', 'dataid', $data->id);
212 echo '<optgroup label="'.get_string('fields', 'data').'">';
213 foreach ($fields as $field) {
214 echo '<option value="[['.$field->name.']]" title="'.$field->description.'">'.$field->name.' - [['.$field->name.']]</option>';
216 echo '</optgroup>';
218 if ($mode == 'addtemplate') {
219 echo '<optgroup label="'.get_string('fieldids', 'data').'">';
220 foreach ($fields as $field) {
221 if (in_array($field->type, array('picture', 'checkbox', 'date', 'latlong', 'radiobutton'))) {
222 continue; //ids are not usable for these composed items
224 echo '<option value="[['.$field->name.'#id]]" title="'.$field->description.' id">'.$field->name.' id - [['.$field->name.'#id]]</option>';
226 echo '</optgroup>';
229 // Print special tags. fix for MDL-7031
230 if ($mode != 'addtemplate' && $mode != 'asearchtemplate') { //Don't print special tags when viewing the advanced search template and add template.
231 echo '<optgroup label="'.get_string('buttons', 'data').'">';
232 echo '<option value="##edit##">' .get_string('edit', 'data'). ' - ##edit##</option>';
233 echo '<option value="##delete##">' .get_string('delete', 'data'). ' - ##delete##</option>';
234 echo '<option value="##approve##">' .get_string('approve', 'data'). ' - ##approve##</option>';
235 echo '<option value="##user##">' .get_string('user'). ' - ##user##</option>';
236 if ($mode != 'singletemplate') {
237 // more points to single template - not useable there
238 echo '<option value="##more##">' .get_string('more', 'data'). ' - ##more##</option>';
239 echo '<option value="##moreurl##">' .get_string('moreurl', 'data'). ' - ##moreurl##</option>';
240 echo '<option value="##comments##">' .get_string('comments', 'data'). ' - ##comments##</option>';
242 echo '</optgroup>';
244 echo '</select>';
245 echo '<br /><br /><br /><br /><input type="submit" name="defaultform" value="'.get_string('resettemplate','data').'" />';
246 if (can_use_html_editor()) {
247 echo '<br /><br />';
248 if ($editor) {
249 $switcheditor = get_string('editordisable', 'data');
250 } else {
251 $switcheditor = get_string('editorenable', 'data');
253 echo '<input type="submit" name="switcheditor" value="'.s($switcheditor).'" />';
255 } else {
256 echo '<br /><br /><br /><br /><input type="submit" name="defaultform" value="'.get_string('resettemplate','data').'" />';
258 echo '</td>';
260 echo '<td>';
261 if ($mode == 'listtemplate'){
262 echo '<div style="text-align:center"><label for="edit-template">'.get_string('multientry','data').'</label></div>';
263 } else {
264 echo '<div style="text-align:center"><label for="edit-template">'.get_string($mode,'data').'</label></div>';
267 print_textarea($usehtmleditor, 20, 72, 0, 0, 'template', $data->{$mode});
268 echo '</td>';
269 echo '</tr>';
271 if ($mode == 'listtemplate'){
272 echo '<tr>';
273 echo '<td>&nbsp;</td>';
274 echo '<td>';
275 echo '<div style="text-align:center"><label for="edit-listtemplatefooter">'.get_string('footer','data').'</label></div>';
276 print_textarea($usehtmleditor, 10, 72, 0, 0, 'listtemplatefooter', $data->listtemplatefooter);
277 echo '</td>';
278 echo '</tr>';
279 } else if ($mode == 'rsstemplate') {
280 echo '<tr>';
281 echo '<td>&nbsp;</td>';
282 echo '<td>';
283 echo '<div style="text-align:center"><label for="edit-rsstitletemplate">'.get_string('rsstitletemplate','data').'</label></div>';
284 print_textarea($usehtmleditor, 10, 72, 0, 0, 'rsstitletemplate', $data->rsstitletemplate);
285 echo '</td>';
286 echo '</tr>';
289 echo '<tr><td style="text-align:center" colspan="2">';
290 echo '<input type="submit" value="'.get_string('savetemplate','data').'" />&nbsp;';
292 echo '</td></tr></table>';
295 print_simple_box_end();
296 echo '</div>';
297 echo '</form>';
298 if ($usehtmleditor) {
299 use_html_editor('template');
300 if ($mode == 'listtemplate'){
301 use_html_editor('listtemplateheader');
302 use_html_editor('listtemplatefooter');
303 } else if ($mode == 'rsstemplate'){
304 use_html_editor('rsstitletemplate');
308 /// Finish the page
309 print_footer($course);