Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   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  
 110  // Render the activity information.
 111  $cminfo = cm_info::create($cm);
 112  $completiondetails = \core_completion\cm_completion_details::get_instance($cminfo, $USER->id);
 113  $activitydates = \core\activity_dates::get_dates_for_module($cminfo, $USER->id);
 114  echo $OUTPUT->activity_information($cminfo, $completiondetails, $activitydates);
 115  
 116  echo $OUTPUT->box(format_module_intro('data', $data, $cm->id), 'generalbox', 'intro');
 117  
 118  /// Groups needed for Add entry tab
 119  $currentgroup = groups_get_activity_group($cm);
 120  $groupmode = groups_get_activity_groupmode($cm);
 121  
 122  /// Print the tabs.
 123  $currenttab = 'templates';
 124  include ('tabs.php');
 125  
 126  /// Processing submitted data, i.e updating form.
 127  $resettemplate = false;
 128  
 129  if (($mytemplate = data_submitted()) && confirm_sesskey()) {
 130      $newtemplate = new stdClass();
 131      $newtemplate->id = $data->id;
 132      $newtemplate->{$mode} = $mytemplate->template;
 133  
 134      if (!empty($mytemplate->defaultform)) {
 135          // Reset the template to default, but don't save yet.
 136          $resettemplate = true;
 137          $data->{$mode} = data_generate_default_template($data, $mode, 0, false, false);
 138          if ($mode == 'listtemplate') {
 139              $data->listtemplateheader = '';
 140              $data->listtemplatefooter = '';
 141          }
 142      } else {
 143          if (isset($mytemplate->listtemplateheader)){
 144              $newtemplate->listtemplateheader = $mytemplate->listtemplateheader;
 145          }
 146          if (isset($mytemplate->listtemplatefooter)){
 147              $newtemplate->listtemplatefooter = $mytemplate->listtemplatefooter;
 148          }
 149          if (isset($mytemplate->rsstitletemplate)){
 150              $newtemplate->rsstitletemplate = $mytemplate->rsstitletemplate;
 151          }
 152  
 153          // Check for multiple tags, only need to check for add template.
 154          if ($mode != 'addtemplate' or data_tags_check($data->id, $newtemplate->{$mode})) {
 155              $DB->update_record('data', $newtemplate);
 156              echo $OUTPUT->notification(get_string('templatesaved', 'data'), 'notifysuccess');
 157  
 158              // Trigger an event for saving the templates.
 159              $event = \mod_data\event\template_updated::create(array(
 160                  'context' => $context,
 161                  'courseid' => $course->id,
 162                  'other' => array(
 163                      'dataid' => $data->id,
 164                  )
 165              ));
 166              $event->trigger();
 167          }
 168      }
 169  } else {
 170      echo '<div class="template_heading">'.get_string('header'.$mode,'data').'</div>';
 171  }
 172  
 173  /// If everything is empty then generate some defaults
 174  if (empty($data->addtemplate) and empty($data->singletemplate) and
 175      empty($data->listtemplate) and empty($data->rsstemplate)) {
 176      data_generate_default_template($data, 'singletemplate');
 177      data_generate_default_template($data, 'listtemplate');
 178      data_generate_default_template($data, 'addtemplate');
 179      data_generate_default_template($data, 'asearchtemplate');           //Template for advanced searches.
 180      data_generate_default_template($data, 'rsstemplate');
 181  }
 182  
 183  editors_head_setup();
 184  
 185  // Determine whether to use HTML editors.
 186  if (($mode === 'csstemplate') || ($mode === 'jstemplate')) {
 187      // The CSS and JS templates aren't HTML.
 188      $usehtmleditor = false;
 189  } else {
 190      $usehtmleditor = data_get_config($data, "editor_{$mode}", true);
 191  }
 192  
 193  $datafieldtype = '';
 194  if ($usehtmleditor) {
 195      $format = FORMAT_HTML;
 196      $datafieldtype = ' data-fieldtype="editor" ';
 197  } else {
 198      $format = FORMAT_PLAIN;
 199  }
 200  
 201  $editor = editors_get_preferred_editor($format);
 202  $strformats = format_text_menu();
 203  $formats =  $editor->get_supported_formats();
 204  foreach ($formats as $fid) {
 205      $formats[$fid] = $strformats[$fid];
 206  }
 207  $options = array();
 208  $options['trusttext'] = false;
 209  $options['forcehttps'] = false;
 210  $options['subdirs'] = false;
 211  $options['maxfiles'] = 0;
 212  $options['maxbytes'] = 0;
 213  $options['changeformat'] = 0;
 214  $options['noclean'] = false;
 215  
 216  echo '<form id="tempform" action="templates.php?d='.$data->id.'&amp;mode='.$mode.'" method="post">';
 217  echo '<div>';
 218  echo '<input name="sesskey" value="'.sesskey().'" type="hidden" />';
 219  // Print button to autogen all forms, if all templates are empty
 220  
 221  if (!$resettemplate) {
 222      // Only reload if we are not resetting the template to default.
 223      $data = $DB->get_record('data', array('id'=>$d));
 224  }
 225  echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
 226  echo '<table cellpadding="4" cellspacing="0" border="0">';
 227  
 228  if ($mode == 'listtemplate'){
 229      // Print the list template header.
 230      echo '<tr>';
 231      echo '<td>&nbsp;</td>';
 232      echo '<td>';
 233      echo '<div class="template_heading"><label for="edit-listtemplateheader">'.get_string('header','data').'</label></div>';
 234  
 235      $field = 'listtemplateheader';
 236      $editor->set_text($data->listtemplateheader);
 237      $editor->use_editor($field, $options);
 238      echo "<div><textarea id='{$field}' {$datafieldtype} name='{$field}' class='form-control' rows='15' cols='80'>" .
 239          s($data->listtemplateheader) . '</textarea></div>';
 240  
 241      echo '</td>';
 242      echo '</tr>';
 243  }
 244  
 245  // Print the main template.
 246  
 247  echo '<tr><td valign="top">';
 248  if ($mode != 'csstemplate' and $mode != 'jstemplate') {
 249      // Add all the available fields for this data.
 250      echo '<label for="availabletags">'.get_string('availabletags','data').'</label>';
 251      echo $OUTPUT->help_icon('availabletags', 'data');
 252      echo '<br />';
 253  
 254      echo '<div class="no-overflow" id="availabletags_wrapper">';
 255      echo '<select name="fields1[]" id="availabletags" size="12" onclick="insert_field_tags(this)" class="form-control">';
 256  
 257      $fields = $DB->get_records('data_fields', array('dataid'=>$data->id));
 258      echo '<optgroup label="'.get_string('fields', 'data').'">';
 259      foreach ($fields as $field) {
 260          echo '<option value="[['.$field->name.']]" title="'.$field->description.'">'.$field->name.' - [['.$field->name.']]</option>';
 261      }
 262      echo '</optgroup>';
 263  
 264      if ($mode == 'addtemplate') {
 265          echo '<optgroup label="'.get_string('fieldids', 'data').'">';
 266          foreach ($fields as $field) {
 267              if (in_array($field->type, array('picture', 'checkbox', 'date', 'latlong', 'radiobutton'))) {
 268                  continue; //ids are not usable for these composed items
 269              }
 270              echo '<option value="[['.$field->name.'#id]]" title="'.$field->description.' id">'.$field->name.' id - [['.$field->name.'#id]]</option>';
 271          }
 272          echo '</optgroup>';
 273          if (core_tag_tag::is_enabled('mod_data', 'data_records')) {
 274              echo '<optgroup label="'.get_string('other', 'data').'">';
 275              echo '<option value="##tags##">' . get_string('tags') . ' - ##tags##</option>';
 276              echo '</optgroup>';
 277          }
 278      }
 279  
 280      // Print special tags. fix for MDL-7031
 281      if ($mode != 'addtemplate' && $mode != 'asearchtemplate') {             //Don't print special tags when viewing the advanced search template and add template.
 282          echo '<optgroup label="'.get_string('buttons', 'data').'">';
 283          echo '<option value="##edit##">' .get_string('edit', 'data'). ' - ##edit##</option>';
 284          echo '<option value="##delete##">' .get_string('delete', 'data'). ' - ##delete##</option>';
 285          echo '<option value="##approve##">' .get_string('approve', 'data'). ' - ##approve##</option>';
 286          echo '<option value="##disapprove##">' .get_string('disapprove', 'data'). ' - ##disapprove##</option>';
 287          if ($mode != 'rsstemplate') {
 288              echo '<option value="##export##">' .get_string('export', 'data'). ' - ##export##</option>';
 289          }
 290          if ($mode != 'singletemplate') {
 291              // more points to single template - not useable there
 292              echo '<option value="##more##">' .get_string('more', 'data'). ' - ##more##</option>';
 293              echo '<option value="##moreurl##">' .get_string('moreurl', 'data'). ' - ##moreurl##</option>';
 294              echo '<option value="##delcheck##">' .get_string('delcheck', 'data'). ' - ##delcheck##</option>';
 295          }
 296          echo '</optgroup>';
 297          echo '<optgroup label="'.get_string('other', 'data').'">';
 298          echo '<option value="##timeadded##">'.get_string('timeadded', 'data'). ' - ##timeadded##</option>';
 299          echo '<option value="##timemodified##">'.get_string('timemodified', 'data'). ' - ##timemodified##</option>';
 300          echo '<option value="##user##">' .get_string('user'). ' - ##user##</option>';
 301          echo '<option value="##userpicture##">' . get_string('userpic') . ' - ##userpicture##</option>';
 302          echo '<option value="##approvalstatus##">' .get_string('approvalstatus', 'data'). ' - ##approvalstatus##</option>';
 303  
 304          if (core_tag_tag::is_enabled('mod_data', 'data_records')) {
 305              echo '<option value="##tags##">' . get_string('tags') . ' - ##tags##</option>';
 306          }
 307  
 308          if ($mode != 'singletemplate') {
 309              // more points to single template - not useable there
 310              echo '<option value="##comments##">' .get_string('comments', 'data'). ' - ##comments##</option>';
 311          }
 312          echo '</optgroup>';
 313      }
 314  
 315      if ($mode == 'asearchtemplate') {
 316          echo '<optgroup label="'.get_string('other', 'data').'">';
 317          echo '<option value="##firstname##">' .get_string('authorfirstname', 'data'). ' - ##firstname##</option>';
 318          echo '<option value="##lastname##">' .get_string('authorlastname', 'data'). ' - ##lastname##</option>';
 319          echo '</optgroup>';
 320      }
 321  
 322      echo '</select>';
 323      echo '</div>';
 324      echo '<br /><br /><br /><br />';
 325      echo '<input type="submit" class="btn btn-secondary" name="defaultform" value="'.get_string('resettemplate', 'data').'" />';
 326      echo '<br /><br />';
 327      if ($usehtmleditor) {
 328          $switchlink = new moodle_url($PAGE->url, ['useeditor' => false]);
 329          echo html_writer::link($switchlink, get_string('editordisable', 'data'));
 330      } else {
 331          $switchlink = new moodle_url($PAGE->url, ['useeditor' => true]);
 332          echo html_writer::link($switchlink, get_string('editorenable', 'data'), [
 333                  'id' => 'enabletemplateeditor',
 334              ]);
 335          $PAGE->requires->event_handler('#enabletemplateeditor', 'click', 'M.util.show_confirm_dialog', [
 336                  'message' => get_string('enabletemplateeditorcheck', 'data'),
 337              ]);
 338      }
 339  } else {
 340      echo '<br /><br /><br /><br />';
 341      echo '<input type="submit" class="btn btn-primary" name="defaultform" value="' . get_string('resettemplate', 'data') . '" />';
 342  }
 343  echo '</td>';
 344  
 345  echo '<td valign="top">';
 346  if ($mode == 'listtemplate'){
 347      echo '<div class="template_heading"><label for="edit-template">'.get_string('multientry','data').'</label></div>';
 348  } else {
 349      echo '<div class="template_heading"><label for="edit-template">'.get_string($mode,'data').'</label></div>';
 350  }
 351  
 352  $field = 'template';
 353  $editor->set_text($data->{$mode});
 354  $editor->use_editor($field, $options);
 355  echo '<div>';
 356  echo '<textarea class="form-control" id="' . $field . '" ' . $datafieldtype .
 357       'name="' . $field . '" rows="15" cols="80">' . s($data->{$mode}) . '</textarea>';
 358  echo '</div>';
 359  echo '</td>';
 360  echo '</tr>';
 361  
 362  if ($mode == 'listtemplate'){
 363      echo '<tr>';
 364      echo '<td>&nbsp;</td>';
 365      echo '<td>';
 366      echo '<div class="template_heading"><label for="edit-listtemplatefooter">'.get_string('footer','data').'</label></div>';
 367  
 368      $field = 'listtemplatefooter';
 369      $editor->set_text($data->listtemplatefooter);
 370      $editor->use_editor($field, $options);
 371      echo '<div>';
 372      echo '<textarea id="' . $field . '" class="form-control" ' . $datafieldtype .
 373           'name="' . $field . '" rows="15" cols="80">' . s($data->listtemplatefooter) . '</textarea>';
 374      echo '</div>';
 375      echo '</td>';
 376      echo '</tr>';
 377  } else if ($mode == 'rsstemplate') {
 378      echo '<tr>';
 379      echo '<td>&nbsp;</td>';
 380      echo '<td>';
 381      echo '<div class="template_heading">';
 382      echo '<label for="edit-rsstitletemplate">' . get_string('rsstitletemplate', 'data') . '</label>';
 383      echo '</div>';
 384  
 385      $field = 'rsstitletemplate';
 386      $editor->set_text($data->rsstitletemplate);
 387      $editor->use_editor($field, $options);
 388      echo '<div>';
 389      echo '<textarea id="' . $field . '" name="' . $field . '" ' . $datafieldtype .
 390           'class="form-control" rows="15" cols="80">' . s($data->rsstitletemplate) . '</textarea>';
 391      echo '</div>';
 392      echo '</td>';
 393      echo '</tr>';
 394  }
 395  
 396  echo '<tr><td class="save_template" colspan="2">';
 397  echo '<input type="submit" class="btn btn-primary" value="'.get_string('savetemplate','data').'" />&nbsp;';
 398  
 399  echo '</td></tr></table>';
 400  
 401  
 402  echo $OUTPUT->box_end();
 403  echo '</div>';
 404  echo '</form>';
 405  
 406  /// Finish the page
 407  echo $OUTPUT->footer();