Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/data/templates.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00026 require_once('../../config.php');
00027 require_once('lib.php');
00028 
00029 $id    = optional_param('id', 0, PARAM_INT);  // course module id
00030 $d     = optional_param('d', 0, PARAM_INT);   // database id
00031 $mode  = optional_param('mode', 'singletemplate', PARAM_ALPHA);
00032 $disableeditor = optional_param('switcheditor', false, PARAM_RAW);
00033 $enableeditor = optional_param('useeditor', false, PARAM_RAW);
00034 
00035 $url = new moodle_url('/mod/data/templates.php');
00036 if ($mode !== 'singletemplate') {
00037     $url->param('mode', $mode);
00038 }
00039 
00040 if ($id) {
00041     $url->param('id', $id);
00042     $PAGE->set_url($url);
00043     if (! $cm = get_coursemodule_from_id('data', $id)) {
00044         print_error('invalidcoursemodule');
00045     }
00046     if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
00047         print_error('coursemisconf');
00048     }
00049     if (! $data = $DB->get_record('data', array('id'=>$cm->instance))) {
00050         print_error('invalidcoursemodule');
00051     }
00052 
00053 } else {
00054     $url->param('d', $d);
00055     $PAGE->set_url($url);
00056     if (! $data = $DB->get_record('data', array('id'=>$d))) {
00057         print_error('invalidid', 'data');
00058     }
00059     if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
00060         print_error('coursemisconf');
00061     }
00062     if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
00063         print_error('invalidcoursemodule');
00064     }
00065 }
00066 
00067 require_login($course->id, false, $cm);
00068 
00069 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
00070 require_capability('mod/data:managetemplates', $context);
00071 
00072 if (!$DB->count_records('data_fields', array('dataid'=>$data->id))) {      // Brand new database!
00073     redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id);  // Redirect to field entry
00074 }
00075 
00076 add_to_log($course->id, 'data', 'templates view', "templates.php?id=$cm->id&amp;d=$data->id", $data->id, $cm->id);
00077 
00078 
00080 
00081 $strdata = get_string('modulenameplural','data');
00082 
00083 // For the javascript for inserting template tags: initialise the default textarea to
00084 // 'edit_template' - it is always present in all different possible views.
00085 
00086 if ($mode == 'singletemplate') {
00087     $PAGE->navbar->add(get_string($mode,'data'));
00088 }
00089 
00090 $PAGE->requires->js('/mod/data/data.js');
00091 $PAGE->set_title($data->name);
00092 $PAGE->set_heading($course->fullname);
00093 $PAGE->set_pagelayout('report');
00094 echo $OUTPUT->header();
00095 echo $OUTPUT->heading(format_string($data->name));
00096 
00097 
00099 $currentgroup = groups_get_activity_group($cm);
00100 $groupmode = groups_get_activity_groupmode($cm);
00101 
00103 $currenttab = 'templates';
00104 include('tabs.php');
00105 
00107 $resettemplate = false;
00108 
00109 if (($mytemplate = data_submitted()) && confirm_sesskey()) {
00110     $newtemplate = new stdClass();
00111     $newtemplate->id = $data->id;
00112     $newtemplate->{$mode} = $mytemplate->template;
00113 
00114     if (!empty($mytemplate->defaultform)) {
00115         // Reset the template to default, but don't save yet.
00116         $resettemplate = true;
00117         $data->{$mode} = data_generate_default_template($data, $mode, 0, false, false);
00118         if ($mode == 'listtemplate') {
00119             $data->listtemplateheader = '';
00120             $data->listtemplatefooter = '';
00121         }
00122     } else {
00123         if (isset($mytemplate->listtemplateheader)){
00124             $newtemplate->listtemplateheader = $mytemplate->listtemplateheader;
00125         }
00126         if (isset($mytemplate->listtemplatefooter)){
00127             $newtemplate->listtemplatefooter = $mytemplate->listtemplatefooter;
00128         }
00129         if (isset($mytemplate->rsstitletemplate)){
00130             $newtemplate->rsstitletemplate = $mytemplate->rsstitletemplate;
00131         }
00132 
00133         // Check for multiple tags, only need to check for add template.
00134         if ($mode != 'addtemplate' or data_tags_check($data->id, $newtemplate->{$mode})) {
00135             // if disableeditor or enableeditor buttons click, don't save instance
00136             if (empty($disableeditor) && empty($enableeditor)) {
00137                 $DB->update_record('data', $newtemplate);
00138                 echo $OUTPUT->notification(get_string('templatesaved', 'data'), 'notifysuccess');
00139                 add_to_log($course->id, 'data', 'templates saved', "templates.php?id=$cm->id&amp;d=$data->id", $data->id, $cm->id);
00140             }
00141         }
00142     }
00143 } else {
00144     echo '<div class="littleintro" style="text-align:center">'.get_string('header'.$mode,'data').'</div>';
00145 }
00146 
00148 if (empty($data->addtemplate) and empty($data->singletemplate) and
00149     empty($data->listtemplate) and empty($data->rsstemplate)) {
00150     data_generate_default_template($data, 'singletemplate');
00151     data_generate_default_template($data, 'listtemplate');
00152     data_generate_default_template($data, 'addtemplate');
00153     data_generate_default_template($data, 'asearchtemplate');           //Template for advanced searches.
00154     data_generate_default_template($data, 'rsstemplate');
00155 }
00156 
00157 editors_head_setup();
00158 $format = FORMAT_HTML;
00159 
00160 if ($mode === 'csstemplate' or $mode === 'jstemplate') {
00161     $disableeditor = true;
00162 }
00163 
00164 if ($disableeditor) {
00165     $format = FORMAT_PLAIN;
00166 }
00167 $editor = editors_get_preferred_editor($format);
00168 $strformats = format_text_menu();
00169 $formats =  $editor->get_supported_formats();
00170 foreach ($formats as $fid) {
00171     $formats[$fid] = $strformats[$fid];
00172 }
00173 $options = array();
00174 $options['trusttext'] = false;
00175 $options['forcehttps'] = false;
00176 $options['subdirs'] = false;
00177 $options['maxfiles'] = 0;
00178 $options['maxbytes'] = 0;
00179 $options['changeformat'] = 0;
00180 $options['noclean'] = false;
00181 
00182 echo '<form id="tempform" action="templates.php?d='.$data->id.'&amp;mode='.$mode.'" method="post">';
00183 echo '<div>';
00184 echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
00185 // Print button to autogen all forms, if all templates are empty
00186 
00187 if (!$resettemplate) {
00188     // Only reload if we are not resetting the template to default.
00189     $data = $DB->get_record('data', array('id'=>$d));
00190 }
00191 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
00192 echo '<table cellpadding="4" cellspacing="0" border="0">';
00193 
00195 $usehtmleditor = can_use_html_editor() && ($mode != 'csstemplate') && ($mode != 'jstemplate') && !$disableeditor;
00196 if ($mode == 'listtemplate'){
00197     // Print the list template header.
00198     echo '<tr>';
00199     echo '<td>&nbsp;</td>';
00200     echo '<td>';
00201     echo '<div style="text-align:center"><label for="edit-listtemplateheader">'.get_string('header','data').'</label></div>';
00202 
00203     $field = 'listtemplateheader';
00204     $editor->use_editor($field, $options);
00205     echo '<div><textarea id="'.$field.'" name="'.$field.'" rows="15" cols="80">'.s($data->listtemplateheader).'</textarea></div>';
00206 
00207     echo '</td>';
00208     echo '</tr>';
00209 }
00210 
00211 // Print the main template.
00212 
00213 echo '<tr><td valign="top">';
00214 if ($mode != 'csstemplate' and $mode != 'jstemplate') {
00215     // Add all the available fields for this data.
00216     echo '<label for="availabletags">'.get_string('availabletags','data').'</label>';
00217     echo $OUTPUT->help_icon('availabletags', 'data');
00218     echo '<br />';
00219 
00220 
00221     echo '<select name="fields1[]" id="availabletags" size="12" onclick="insert_field_tags(this)">';
00222 
00223     $fields = $DB->get_records('data_fields', array('dataid'=>$data->id));
00224     echo '<optgroup label="'.get_string('fields', 'data').'">';
00225     foreach ($fields as $field) {
00226         echo '<option value="[['.$field->name.']]" title="'.$field->description.'">'.$field->name.' - [['.$field->name.']]</option>';
00227     }
00228     echo '</optgroup>';
00229 
00230     if ($mode == 'addtemplate') {
00231         echo '<optgroup label="'.get_string('fieldids', 'data').'">';
00232         foreach ($fields as $field) {
00233             if (in_array($field->type, array('picture', 'checkbox', 'date', 'latlong', 'radiobutton'))) {
00234                 continue; //ids are not usable for these composed items
00235             }
00236             echo '<option value="[['.$field->name.'#id]]" title="'.$field->description.' id">'.$field->name.' id - [['.$field->name.'#id]]</option>';
00237         }
00238         echo '</optgroup>';
00239     }
00240 
00241     // Print special tags. fix for MDL-7031
00242     if ($mode != 'addtemplate' && $mode != 'asearchtemplate') {             //Don't print special tags when viewing the advanced search template and add template.
00243         echo '<optgroup label="'.get_string('buttons', 'data').'">';
00244         echo '<option value="##edit##">' .get_string('edit', 'data'). ' - ##edit##</option>';
00245         echo '<option value="##delete##">' .get_string('delete', 'data'). ' - ##delete##</option>';
00246         echo '<option value="##approve##">' .get_string('approve', 'data'). ' - ##approve##</option>';
00247         if ($mode != 'rsstemplate') {
00248             echo '<option value="##export##">' .get_string('export', 'data'). ' - ##export##</option>';
00249         }
00250         if ($mode != 'singletemplate') {
00251             // more points to single template - not useable there
00252             echo '<option value="##more##">' .get_string('more', 'data'). ' - ##more##</option>';
00253             echo '<option value="##moreurl##">' .get_string('moreurl', 'data'). ' - ##moreurl##</option>';
00254         }
00255         echo '</optgroup>';
00256         echo '<optgroup label="'.get_string('other', 'data').'">';
00257         echo '<option value="##timeadded##">'.get_string('timeadded', 'data'). ' - ##timeadded##</option>';
00258         echo '<option value="##timemodified##">'.get_string('timemodified', 'data'). ' - ##timemodified##</option>';
00259         echo '<option value="##user##">' .get_string('user'). ' - ##user##</option>';
00260         if ($mode != 'singletemplate') {
00261             // more points to single template - not useable there
00262             echo '<option value="##comments##">' .get_string('comments', 'data'). ' - ##comments##</option>';
00263         }
00264         echo '</optgroup>';
00265     }
00266 
00267     if ($mode == 'asearchtemplate') {
00268         echo '<optgroup label="'.get_string('other', 'data').'">';
00269         echo '<option value="##firstname##">' .get_string('authorfirstname', 'data'). ' - ##firstname##</option>';
00270         echo '<option value="##lastname##">' .get_string('authorlastname', 'data'). ' - ##lastname##</option>';
00271         echo '</optgroup>';
00272     }
00273 
00274     echo '</select>';
00275     echo '<br /><br /><br /><br /><input type="submit" name="defaultform" value="'.get_string('resettemplate','data').'" />';
00276     if (can_use_html_editor()) {
00277         echo '<br /><br />';
00278         if ($usehtmleditor) {
00279             $switcheditor = get_string('editordisable', 'data');
00280             echo '<input type="submit" name="switcheditor" value="'.s($switcheditor).'" />';
00281         } else {
00282             $switcheditor = get_string('editorenable', 'data');
00283             echo '<input type="submit" name="useeditor" value="'.s($switcheditor).'" />';
00284         }
00285     }
00286 } else {
00287     echo '<br /><br /><br /><br /><input type="submit" name="defaultform" value="'.get_string('resettemplate','data').'" />';
00288 }
00289 echo '</td>';
00290 
00291 echo '<td valign="top">';
00292 if ($mode == 'listtemplate'){
00293     echo '<div style="text-align:center"><label for="edit-template">'.get_string('multientry','data').'</label></div>';
00294 } else {
00295     echo '<div style="text-align:center"><label for="edit-template">'.get_string($mode,'data').'</label></div>';
00296 }
00297 
00298 $field = 'template';
00299 $editor->use_editor($field, $options);
00300 echo '<div><textarea id="'.$field.'" name="'.$field.'" rows="15" cols="80">'.s($data->{$mode}).'</textarea></div>';
00301 echo '</td>';
00302 echo '</tr>';
00303 
00304 if ($mode == 'listtemplate'){
00305     echo '<tr>';
00306     echo '<td>&nbsp;</td>';
00307     echo '<td>';
00308     echo '<div style="text-align:center"><label for="edit-listtemplatefooter">'.get_string('footer','data').'</label></div>';
00309 
00310     $field = 'listtemplatefooter';
00311     $editor->use_editor($field, $options);
00312     echo '<div><textarea id="'.$field.'" name="'.$field.'" rows="15" cols="80">'.s($data->listtemplatefooter).'</textarea></div>';
00313     echo '</td>';
00314     echo '</tr>';
00315 } else if ($mode == 'rsstemplate') {
00316     echo '<tr>';
00317     echo '<td>&nbsp;</td>';
00318     echo '<td>';
00319     echo '<div style="text-align:center"><label for="edit-rsstitletemplate">'.get_string('rsstitletemplate','data').'</label></div>';
00320 
00321     $field = 'rsstitletemplate';
00322     $editor->use_editor($field, $options);
00323     echo '<div><textarea id="'.$field.'" name="'.$field.'" rows="15" cols="80">'.s($data->rsstitletemplate).'</textarea></div>';
00324     echo '</td>';
00325     echo '</tr>';
00326 }
00327 
00328 echo '<tr><td style="text-align:center" colspan="2">';
00329 echo '<input type="submit" value="'.get_string('savetemplate','data').'" />&nbsp;';
00330 
00331 echo '</td></tr></table>';
00332 
00333 
00334 echo $OUTPUT->box_end();
00335 echo '</div>';
00336 echo '</form>';
00337 
00339 echo $OUTPUT->footer();
 All Data Structures Namespaces Files Functions Variables Enumerations