Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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  $datafieldtype = '';
 187  if ($usehtmleditor) {
 188      $format = FORMAT_HTML;
 189      $datafieldtype = ' data-fieldtype="editor" ';
 190  } else {
 191      $format = FORMAT_PLAIN;
 192  }
 193  
 194  $editor = editors_get_preferred_editor($format);
 195  $strformats = format_text_menu();
 196  $formats =  $editor->get_supported_formats();
 197  foreach ($formats as $fid) {
 198      $formats[$fid] = $strformats[$fid];
 199  }
 200  $options = array();
 201  $options['trusttext'] = false;
 202  $options['forcehttps'] = false;
 203  $options['subdirs'] = false;
 204  $options['maxfiles'] = 0;
 205  $options['maxbytes'] = 0;
 206  $options['changeformat'] = 0;
 207  $options['noclean'] = false;
 208  
 209  echo '<form id="tempform" action="templates.php?d='.$data->id.'&amp;mode='.$mode.'" method="post">';
 210  echo '<div>';
 211  echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
 212  // Print button to autogen all forms, if all templates are empty
 213  
 214  if (!$resettemplate) {
 215      // Only reload if we are not resetting the template to default.
 216      $data = $DB->get_record('data', array('id'=>$d));
 217  }
 218  echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
 219  echo '<table cellpadding="4" cellspacing="0" border="0">';
 220  
 221  if ($mode == 'listtemplate'){
 222      // Print the list template header.
 223      echo '<tr>';
 224      echo '<td>&nbsp;</td>';
 225      echo '<td>';
 226      echo '<div class="template_heading"><label for="edit-listtemplateheader">'.get_string('header','data').'</label></div>';
 227  
 228      $field = 'listtemplateheader';
 229      $editor->set_text($data->listtemplateheader);
 230      $editor->use_editor($field, $options);
 231      echo "<div><textarea id='{$field}' {$datafieldtype} name='{$field}' class='form-control' rows='15' cols='80'>" .
 232          s($data->listtemplateheader) . '</textarea></div>';
 233  
 234      echo '</td>';
 235      echo '</tr>';
 236  }
 237  
 238  // Print the main template.
 239  
 240  echo '<tr><td valign="top">';
 241  if ($mode != 'csstemplate' and $mode != 'jstemplate') {
 242      // Add all the available fields for this data.
 243      echo '<label for="availabletags">'.get_string('availabletags','data').'</label>';
 244      echo $OUTPUT->help_icon('availabletags', 'data');
 245      echo '<br />';
 246  
 247      echo '<div class="no-overflow" id="availabletags_wrapper">';
 248      echo '<select name="fields1[]" id="availabletags" size="12" onclick="insert_field_tags(this)" class="form-control">';
 249  
 250      $fields = $DB->get_records('data_fields', array('dataid'=>$data->id));
 251      echo '<optgroup label="'.get_string('fields', 'data').'">';
 252      foreach ($fields as $field) {
 253          echo '<option value="[['.$field->name.']]" title="'.$field->description.'">'.$field->name.' - [['.$field->name.']]</option>';
 254      }
 255      echo '</optgroup>';
 256  
 257      if ($mode == 'addtemplate') {
 258          echo '<optgroup label="'.get_string('fieldids', 'data').'">';
 259          foreach ($fields as $field) {
 260              if (in_array($field->type, array('picture', 'checkbox', 'date', 'latlong', 'radiobutton'))) {
 261                  continue; //ids are not usable for these composed items
 262              }
 263              echo '<option value="[['.$field->name.'#id]]" title="'.$field->description.' id">'.$field->name.' id - [['.$field->name.'#id]]</option>';
 264          }
 265          echo '</optgroup>';
 266          if (core_tag_tag::is_enabled('mod_data', 'data_records')) {
 267              echo '<optgroup label="'.get_string('other', 'data').'">';
 268              echo '<option value="##tags##">' . get_string('tags') . ' - ##tags##</option>';
 269              echo '</optgroup>';
 270          }
 271      }
 272  
 273      // Print special tags. fix for MDL-7031
 274      if ($mode != 'addtemplate' && $mode != 'asearchtemplate') {             //Don't print special tags when viewing the advanced search template and add template.
 275          echo '<optgroup label="'.get_string('buttons', 'data').'">';
 276          echo '<option value="##edit##">' .get_string('edit', 'data'). ' - ##edit##</option>';
 277          echo '<option value="##delete##">' .get_string('delete', 'data'). ' - ##delete##</option>';
 278          echo '<option value="##approve##">' .get_string('approve', 'data'). ' - ##approve##</option>';
 279          echo '<option value="##disapprove##">' .get_string('disapprove', 'data'). ' - ##disapprove##</option>';
 280          if ($mode != 'rsstemplate') {
 281              echo '<option value="##export##">' .get_string('export', 'data'). ' - ##export##</option>';
 282          }
 283          if ($mode != 'singletemplate') {
 284              // more points to single template - not useable there
 285              echo '<option value="##more##">' .get_string('more', 'data'). ' - ##more##</option>';
 286              echo '<option value="##moreurl##">' .get_string('moreurl', 'data'). ' - ##moreurl##</option>';
 287              echo '<option value="##delcheck##">' .get_string('delcheck', 'data'). ' - ##delcheck##</option>';
 288          }
 289          echo '</optgroup>';
 290          echo '<optgroup label="'.get_string('other', 'data').'">';
 291          echo '<option value="##timeadded##">'.get_string('timeadded', 'data'). ' - ##timeadded##</option>';
 292          echo '<option value="##timemodified##">'.get_string('timemodified', 'data'). ' - ##timemodified##</option>';
 293          echo '<option value="##user##">' .get_string('user'). ' - ##user##</option>';
 294          echo '<option value="##userpicture##">' . get_string('userpic') . ' - ##userpicture##</option>';
 295          echo '<option value="##approvalstatus##">' .get_string('approvalstatus', 'data'). ' - ##approvalstatus##</option>';
 296  
 297          if (core_tag_tag::is_enabled('mod_data', 'data_records')) {
 298              echo '<option value="##tags##">' . get_string('tags') . ' - ##tags##</option>';
 299          }
 300  
 301          if ($mode != 'singletemplate') {
 302              // more points to single template - not useable there
 303              echo '<option value="##comments##">' .get_string('comments', 'data'). ' - ##comments##</option>';
 304          }
 305          echo '</optgroup>';
 306      }
 307  
 308      if ($mode == 'asearchtemplate') {
 309          echo '<optgroup label="'.get_string('other', 'data').'">';
 310          echo '<option value="##firstname##">' .get_string('authorfirstname', 'data'). ' - ##firstname##</option>';
 311          echo '<option value="##lastname##">' .get_string('authorlastname', 'data'). ' - ##lastname##</option>';
 312          echo '</optgroup>';
 313      }
 314  
 315      echo '</select>';
 316      echo '</div>';
 317      echo '<br /><br /><br /><br />';
 318      echo '<input type="submit" class="btn btn-secondary" name="defaultform" value="'.get_string('resettemplate', 'data').'" />';
 319      echo '<br /><br />';
 320      if ($usehtmleditor) {
 321          $switchlink = new moodle_url($PAGE->url, ['useeditor' => false]);
 322          echo html_writer::link($switchlink, get_string('editordisable', 'data'));
 323      } else {
 324          $switchlink = new moodle_url($PAGE->url, ['useeditor' => true]);
 325          echo html_writer::link($switchlink, get_string('editorenable', 'data'), [
 326                  'id' => 'enabletemplateeditor',
 327              ]);
 328          $PAGE->requires->event_handler('#enabletemplateeditor', 'click', 'M.util.show_confirm_dialog', [
 329                  'message' => get_string('enabletemplateeditorcheck', 'data'),
 330              ]);
 331      }
 332  } else {
 333      echo '<br /><br /><br /><br />';
 334      echo '<input type="submit" class="btn btn-primary" name="defaultform" value="' . get_string('resettemplate', 'data') . '" />';
 335  }
 336  echo '</td>';
 337  
 338  echo '<td valign="top">';
 339  if ($mode == 'listtemplate'){
 340      echo '<div class="template_heading"><label for="edit-template">'.get_string('multientry','data').'</label></div>';
 341  } else {
 342      echo '<div class="template_heading"><label for="edit-template">'.get_string($mode,'data').'</label></div>';
 343  }
 344  
 345  $field = 'template';
 346  $editor->set_text($data->{$mode});
 347  $editor->use_editor($field, $options);
 348  echo '<div>';
 349  echo '<textarea class="form-control" id="' . $field . '" ' . $datafieldtype .
 350       'name="' . $field . '" rows="15" cols="80">' . s($data->{$mode}) . '</textarea>';
 351  echo '</div>';
 352  echo '</td>';
 353  echo '</tr>';
 354  
 355  if ($mode == 'listtemplate'){
 356      echo '<tr>';
 357      echo '<td>&nbsp;</td>';
 358      echo '<td>';
 359      echo '<div class="template_heading"><label for="edit-listtemplatefooter">'.get_string('footer','data').'</label></div>';
 360  
 361      $field = 'listtemplatefooter';
 362      $editor->set_text($data->listtemplatefooter);
 363      $editor->use_editor($field, $options);
 364      echo '<div>';
 365      echo '<textarea id="' . $field . '" class="form-control" ' . $datafieldtype .
 366           'name="' . $field . '" rows="15" cols="80">' . s($data->listtemplatefooter) . '</textarea>';
 367      echo '</div>';
 368      echo '</td>';
 369      echo '</tr>';
 370  } else if ($mode == 'rsstemplate') {
 371      echo '<tr>';
 372      echo '<td>&nbsp;</td>';
 373      echo '<td>';
 374      echo '<div class="template_heading">';
 375      echo '<label for="edit-rsstitletemplate">' . get_string('rsstitletemplate', 'data') . '</label>';
 376      echo '</div>';
 377  
 378      $field = 'rsstitletemplate';
 379      $editor->set_text($data->rsstitletemplate);
 380      $editor->use_editor($field, $options);
 381      echo '<div>';
 382      echo '<textarea id="' . $field . '" name="' . $field . '" ' . $datafieldtype .
 383           'class="form-control" rows="15" cols="80">' . s($data->rsstitletemplate) . '</textarea>';
 384      echo '</div>';
 385      echo '</td>';
 386      echo '</tr>';
 387  }
 388  
 389  echo '<tr><td class="save_template" colspan="2">';
 390  echo '<input type="submit" class="btn btn-primary" value="'.get_string('savetemplate','data').'" />&nbsp;';
 391  
 392  echo '</td></tr></table>';
 393  
 394  
 395  echo $OUTPUT->box_end();
 396  echo '</div>';
 397  echo '</form>';
 398  
 399  /// Finish the page
 400  echo $OUTPUT->footer();