Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * This file is part of the Database module for Moodle
  20   *
  21   * @copyright 2005 Martin Dougiamas  http://dougiamas.com
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   * @package mod_data
  24   */
  25  
  26  require_once('../../config.php');
  27  require_once ('lib.php');
  28  
  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);
  32  $useeditor = optional_param('useeditor', null, PARAM_BOOL);
  33  
  34  $url = new moodle_url('/mod/data/templates.php');
  35  if ($mode !== 'singletemplate') {
  36      $url->param('mode', $mode);
  37  }
  38  
  39  if ($id) {
  40      $url->param('id', $id);
  41      $PAGE->set_url($url);
  42      if (! $cm = get_coursemodule_from_id('data', $id)) {
  43          print_error('invalidcoursemodule');
  44      }
  45      if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
  46          print_error('coursemisconf');
  47      }
  48      if (! $data = $DB->get_record('data', array('id'=>$cm->instance))) {
  49          print_error('invalidcoursemodule');
  50      }
  51  
  52  } else {
  53      $url->param('d', $d);
  54      $PAGE->set_url($url);
  55      if (! $data = $DB->get_record('data', array('id'=>$d))) {
  56          print_error('invalidid', 'data');
  57      }
  58      if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
  59          print_error('coursemisconf');
  60      }
  61      if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
  62          print_error('invalidcoursemodule');
  63      }
  64  }
  65  
  66  require_login($course, false, $cm);
  67  
  68  $context = context_module::instance($cm->id);
  69  require_capability('mod/data:managetemplates', $context);
  70  
  71  if ($useeditor !== null) {
  72      // The useeditor param was set. Update the value for this template.
  73      data_set_config($data, "editor_{$mode}", !!$useeditor);
  74  }
  75  
  76  if (!$DB->count_records('data_fields', array('dataid'=>$data->id))) {      // Brand new database!
  77      redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id);  // Redirect to field entry
  78  }
  79  
  80  // Trigger an event for viewing templates.
  81  $event = \mod_data\event\template_viewed::create(array(
  82      'context' => $context,
  83      'courseid' => $course->id,
  84      'other' => array(
  85          'dataid' => $data->id
  86      )
  87  ));
  88  $event->add_record_snapshot('data', $data);
  89  $event->trigger();
  90  
  91  /// Print the page header
  92  
  93  $strdata = get_string('modulenameplural','data');
  94  
  95  // For the javascript for inserting template tags: initialise the default textarea to
  96  // 'edit_template' - it is always present in all different possible views.
  97  
  98  if ($mode == 'singletemplate') {
  99      $PAGE->navbar->add(get_string($mode,'data'));
 100  }
 101  
 102  $PAGE->requires->js('/mod/data/data.js');
 103  $PAGE->set_title($data->name);
 104  $PAGE->set_heading($course->fullname);
 105  $PAGE->set_pagelayout('admin');
 106  $PAGE->force_settings_menu(true);
 107  echo $OUTPUT->header();
 108  echo $OUTPUT->heading(format_string($data->name), 2);
 109  echo $OUTPUT->box(format_module_intro('data', $data, $cm->id), 'generalbox', 'intro');
 110  
 111  /// Groups needed for Add entry tab
 112  $currentgroup = groups_get_activity_group($cm);
 113  $groupmode = groups_get_activity_groupmode($cm);
 114  
 115  /// Print the tabs.
 116  $currenttab = 'templates';
 117  include ('tabs.php');
 118  
 119  /// Processing submitted data, i.e updating form.
 120  $resettemplate = false;
 121  
 122  if (($mytemplate = data_submitted()) && confirm_sesskey()) {
 123      $newtemplate = new stdClass();
 124      $newtemplate->id = $data->id;
 125      $newtemplate->{$mode} = $mytemplate->template;
 126  
 127      if (!empty($mytemplate->defaultform)) {
 128          // Reset the template to default, but don't save yet.
 129          $resettemplate = true;
 130          $data->{$mode} = data_generate_default_template($data, $mode, 0, false, false);
 131          if ($mode == 'listtemplate') {
 132              $data->listtemplateheader = '';
 133              $data->listtemplatefooter = '';
 134          }
 135      } else {
 136          if (isset($mytemplate->listtemplateheader)){
 137              $newtemplate->listtemplateheader = $mytemplate->listtemplateheader;
 138          }
 139          if (isset($mytemplate->listtemplatefooter)){
 140              $newtemplate->listtemplatefooter = $mytemplate->listtemplatefooter;
 141          }
 142          if (isset($mytemplate->rsstitletemplate)){
 143              $newtemplate->rsstitletemplate = $mytemplate->rsstitletemplate;
 144          }
 145  
 146          // Check for multiple tags, only need to check for add template.
 147          if ($mode != 'addtemplate' or data_tags_check($data->id, $newtemplate->{$mode})) {
 148              $DB->update_record('data', $newtemplate);
 149              echo $OUTPUT->notification(get_string('templatesaved', 'data'), 'notifysuccess');
 150  
 151              // Trigger an event for saving the templates.
 152              $event = \mod_data\event\template_updated::create(array(
 153                  'context' => $context,
 154                  'courseid' => $course->id,
 155                  'other' => array(
 156                      'dataid' => $data->id,
 157                  )
 158              ));
 159              $event->trigger();
 160          }
 161      }
 162  } else {
 163      echo '<div class="template_heading">'.get_string('header'.$mode,'data').'</div>';
 164  }
 165  
 166  /// If everything is empty then generate some defaults
 167  if (empty($data->addtemplate) and empty($data->singletemplate) and
 168      empty($data->listtemplate) and empty($data->rsstemplate)) {
 169      data_generate_default_template($data, 'singletemplate');
 170      data_generate_default_template($data, 'listtemplate');
 171      data_generate_default_template($data, 'addtemplate');
 172      data_generate_default_template($data, 'asearchtemplate');           //Template for advanced searches.
 173      data_generate_default_template($data, 'rsstemplate');
 174  }
 175  
 176  editors_head_setup();
 177  
 178  // Determine whether to use HTML editors.
 179  if (($mode === 'csstemplate') || ($mode === 'jstemplate')) {
 180      // The CSS and JS templates aren't HTML.
 181      $usehtmleditor = false;
 182  } else {
 183      $usehtmleditor = data_get_config($data, "editor_{$mode}", true);
 184  }
 185  
 186  if ($usehtmleditor) {
 187      $format = FORMAT_HTML;
 188  } else {
 189      $format = FORMAT_PLAIN;
 190  }
 191  
 192  $editor = editors_get_preferred_editor($format);
 193  $strformats = format_text_menu();
 194  $formats =  $editor->get_supported_formats();
 195  foreach ($formats as $fid) {
 196      $formats[$fid] = $strformats[$fid];
 197  }
 198  $options = array();
 199  $options['trusttext'] = false;
 200  $options['forcehttps'] = false;
 201  $options['subdirs'] = false;
 202  $options['maxfiles'] = 0;
 203  $options['maxbytes'] = 0;
 204  $options['changeformat'] = 0;
 205  $options['noclean'] = false;
 206  
 207  echo '<form id="tempform" action="templates.php?d='.$data->id.'&amp;mode='.$mode.'" method="post">';
 208  echo '<div>';
 209  echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
 210  // Print button to autogen all forms, if all templates are empty
 211  
 212  if (!$resettemplate) {
 213      // Only reload if we are not resetting the template to default.
 214      $data = $DB->get_record('data', array('id'=>$d));
 215  }
 216  echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
 217  echo '<table cellpadding="4" cellspacing="0" border="0">';
 218  
 219  if ($mode == 'listtemplate'){
 220      // Print the list template header.
 221      echo '<tr>';
 222      echo '<td>&nbsp;</td>';
 223      echo '<td>';
 224      echo '<div class="template_heading"><label for="edit-listtemplateheader">'.get_string('header','data').'</label></div>';
 225  
 226      $field = 'listtemplateheader';
 227      $editor->set_text($data->listtemplateheader);
 228      $editor->use_editor($field, $options);
 229      echo '<div><textarea id="'.$field.'" name="'.$field.'" class="form-control" rows="15" cols="80">' .
 230          s($data->listtemplateheader) . '</textarea></div>';
 231  
 232      echo '</td>';
 233      echo '</tr>';
 234  }
 235  
 236  // Print the main template.
 237  
 238  echo '<tr><td valign="top">';
 239  if ($mode != 'csstemplate' and $mode != 'jstemplate') {
 240      // Add all the available fields for this data.
 241      echo '<label for="availabletags">'.get_string('availabletags','data').'</label>';
 242      echo $OUTPUT->help_icon('availabletags', 'data');
 243      echo '<br />';
 244  
 245      echo '<div class="no-overflow" id="availabletags_wrapper">';
 246      echo '<select name="fields1[]" id="availabletags" size="12" onclick="insert_field_tags(this)" class="form-control">';
 247  
 248      $fields = $DB->get_records('data_fields', array('dataid'=>$data->id));
 249      echo '<optgroup label="'.get_string('fields', 'data').'">';
 250      foreach ($fields as $field) {
 251          echo '<option value="[['.$field->name.']]" title="'.$field->description.'">'.$field->name.' - [['.$field->name.']]</option>';
 252      }
 253      echo '</optgroup>';
 254  
 255      if ($mode == 'addtemplate') {
 256          echo '<optgroup label="'.get_string('fieldids', 'data').'">';
 257          foreach ($fields as $field) {
 258              if (in_array($field->type, array('picture', 'checkbox', 'date', 'latlong', 'radiobutton'))) {
 259                  continue; //ids are not usable for these composed items
 260              }
 261              echo '<option value="[['.$field->name.'#id]]" title="'.$field->description.' id">'.$field->name.' id - [['.$field->name.'#id]]</option>';
 262          }
 263          echo '</optgroup>';
 264          if (core_tag_tag::is_enabled('mod_data', 'data_records')) {
 265              echo '<optgroup label="'.get_string('other', 'data').'">';
 266              echo '<option value="##tags##">' . get_string('tags') . ' - ##tags##</option>';
 267              echo '</optgroup>';
 268          }
 269      }
 270  
 271      // Print special tags. fix for MDL-7031
 272      if ($mode != 'addtemplate' && $mode != 'asearchtemplate') {             //Don't print special tags when viewing the advanced search template and add template.
 273          echo '<optgroup label="'.get_string('buttons', 'data').'">';
 274          echo '<option value="##edit##">' .get_string('edit', 'data'). ' - ##edit##</option>';
 275          echo '<option value="##delete##">' .get_string('delete', 'data'). ' - ##delete##</option>';
 276          echo '<option value="##approve##">' .get_string('approve', 'data'). ' - ##approve##</option>';
 277          echo '<option value="##disapprove##">' .get_string('disapprove', 'data'). ' - ##disapprove##</option>';
 278          if ($mode != 'rsstemplate') {
 279              echo '<option value="##export##">' .get_string('export', 'data'). ' - ##export##</option>';
 280          }
 281          if ($mode != 'singletemplate') {
 282              // more points to single template - not useable there
 283              echo '<option value="##more##">' .get_string('more', 'data'). ' - ##more##</option>';
 284              echo '<option value="##moreurl##">' .get_string('moreurl', 'data'). ' - ##moreurl##</option>';
 285              echo '<option value="##delcheck##">' .get_string('delcheck', 'data'). ' - ##delcheck##</option>';
 286          }
 287          echo '</optgroup>';
 288          echo '<optgroup label="'.get_string('other', 'data').'">';
 289          echo '<option value="##timeadded##">'.get_string('timeadded', 'data'). ' - ##timeadded##</option>';
 290          echo '<option value="##timemodified##">'.get_string('timemodified', 'data'). ' - ##timemodified##</option>';
 291          echo '<option value="##user##">' .get_string('user'). ' - ##user##</option>';
 292          echo '<option value="##userpicture##">' . get_string('userpic') . ' - ##userpicture##</option>';
 293          echo '<option value="##approvalstatus##">' .get_string('approvalstatus', 'data'). ' - ##approvalstatus##</option>';
 294  
 295          if (core_tag_tag::is_enabled('mod_data', 'data_records')) {
 296              echo '<option value="##tags##">' . get_string('tags') . ' - ##tags##</option>';
 297          }
 298  
 299          if ($mode != 'singletemplate') {
 300              // more points to single template - not useable there
 301              echo '<option value="##comments##">' .get_string('comments', 'data'). ' - ##comments##</option>';
 302          }
 303          echo '</optgroup>';
 304      }
 305  
 306      if ($mode == 'asearchtemplate') {
 307          echo '<optgroup label="'.get_string('other', 'data').'">';
 308          echo '<option value="##firstname##">' .get_string('authorfirstname', 'data'). ' - ##firstname##</option>';
 309          echo '<option value="##lastname##">' .get_string('authorlastname', 'data'). ' - ##lastname##</option>';
 310          echo '</optgroup>';
 311      }
 312  
 313      echo '</select>';
 314      echo '</div>';
 315      echo '<br /><br /><br /><br />';
 316      echo '<input type="submit" class="btn btn-secondary" name="defaultform" value="'.get_string('resettemplate', 'data').'" />';
 317      echo '<br /><br />';
 318      if ($usehtmleditor) {
 319          $switchlink = new moodle_url($PAGE->url, ['useeditor' => false]);
 320          echo html_writer::link($switchlink, get_string('editordisable', 'data'));
 321      } else {
 322          $switchlink = new moodle_url($PAGE->url, ['useeditor' => true]);
 323          echo html_writer::link($switchlink, get_string('editorenable', 'data'), [
 324                  'id' => 'enabletemplateeditor',
 325              ]);
 326          $PAGE->requires->event_handler('#enabletemplateeditor', 'click', 'M.util.show_confirm_dialog', [
 327                  'message' => get_string('enabletemplateeditorcheck', 'data'),
 328              ]);
 329      }
 330  } else {
 331      echo '<br /><br /><br /><br />';
 332      echo '<input type="submit" class="btn btn-primary" name="defaultform" value="' . get_string('resettemplate', 'data') . '" />';
 333  }
 334  echo '</td>';
 335  
 336  echo '<td valign="top">';
 337  if ($mode == 'listtemplate'){
 338      echo '<div class="template_heading"><label for="edit-template">'.get_string('multientry','data').'</label></div>';
 339  } else {
 340      echo '<div class="template_heading"><label for="edit-template">'.get_string($mode,'data').'</label></div>';
 341  }
 342  
 343  $field = 'template';
 344  $editor->set_text($data->{$mode});
 345  $editor->use_editor($field, $options);
 346  echo '<div>';
 347  echo '<textarea class="form-control" id="' . $field . '" ' .
 348       'name="' . $field . '" rows="15" cols="80">' . s($data->{$mode}) . '</textarea>';
 349  echo '</div>';
 350  echo '</td>';
 351  echo '</tr>';
 352  
 353  if ($mode == 'listtemplate'){
 354      echo '<tr>';
 355      echo '<td>&nbsp;</td>';
 356      echo '<td>';
 357      echo '<div class="template_heading"><label for="edit-listtemplatefooter">'.get_string('footer','data').'</label></div>';
 358  
 359      $field = 'listtemplatefooter';
 360      $editor->set_text($data->listtemplatefooter);
 361      $editor->use_editor($field, $options);
 362      echo '<div>';
 363      echo '<textarea id="' . $field . '" class="form-control" ' .
 364           'name="' . $field . '" rows="15" cols="80">' . s($data->listtemplatefooter) . '</textarea>';
 365      echo '</div>';
 366      echo '</td>';
 367      echo '</tr>';
 368  } else if ($mode == 'rsstemplate') {
 369      echo '<tr>';
 370      echo '<td>&nbsp;</td>';
 371      echo '<td>';
 372      echo '<div class="template_heading">';
 373      echo '<label for="edit-rsstitletemplate">' . get_string('rsstitletemplate', 'data') . '</label>';
 374      echo '</div>';
 375  
 376      $field = 'rsstitletemplate';
 377      $editor->set_text($data->rsstitletemplate);
 378      $editor->use_editor($field, $options);
 379      echo '<div>';
 380      echo '<textarea id="' . $field . '" name="' . $field . '" ' .
 381           'class="form-control" rows="15" cols="80">' . s($data->rsstitletemplate) . '</textarea>';
 382      echo '</div>';
 383      echo '</td>';
 384      echo '</tr>';
 385  }
 386  
 387  echo '<tr><td class="save_template" colspan="2">';
 388  echo '<input type="submit" class="btn btn-primary" value="'.get_string('savetemplate','data').'" />&nbsp;';
 389  
 390  echo '</td></tr></table>';
 391  
 392  
 393  echo $OUTPUT->box_end();
 394  echo '</div>';
 395  echo '</form>';
 396  
 397  /// Finish the page
 398  echo $OUTPUT->footer();