Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   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  use mod_data\manager;
  27  
  28  require_once('../../config.php');
  29  require_once ('lib.php');
  30  
  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', 'addtemplate', PARAM_ALPHA);
  34  $action  = optional_param('action', '', PARAM_ALPHA);
  35  $useeditor = optional_param('useeditor', null, PARAM_BOOL);
  36  
  37  $url = new moodle_url('/mod/data/templates.php');
  38  
  39  if ($id) {
  40      list($course, $cm) = get_course_and_cm_from_cmid($id, manager::MODULE);
  41      $manager = manager::create_from_coursemodule($cm);
  42      $url->param('d', $cm->instance);
  43  } else {   // We must have $d.
  44      $instance = $DB->get_record('data', ['id' => $d], '*', MUST_EXIST);
  45      $manager = manager::create_from_instance($instance);
  46      $cm = $manager->get_coursemodule();
  47      $course = get_course($cm->course);
  48      $url->param('d', $d);
  49  }
  50  
  51  $instance = $manager->get_instance();
  52  $context = $manager->get_context();
  53  
  54  $url->param('mode', $mode);
  55  $PAGE->set_url($url);
  56  
  57  require_login($course, false, $cm);
  58  require_capability('mod/data:managetemplates', $context);
  59  
  60  if ($action == 'resetalltemplates') {
  61      require_sesskey();
  62      $manager->reset_all_templates();
  63      redirect($PAGE->url, get_string('templateresetall', 'mod_data'), null, \core\output\notification::NOTIFY_SUCCESS);
  64  }
  65  
  66  $manager->set_template_viewed();
  67  
  68  if ($useeditor !== null) {
  69      // The useeditor param was set. Update the value for this template.
  70      data_set_config($instance, "editor_{$mode}", !!$useeditor);
  71  }
  72  
  73  $PAGE->requires->js('/mod/data/data.js');
  74  switch ($mode) {
  75      case 'asearchtemplate':
  76          $title = get_string('asearchtemplate', 'data');
  77          break;
  78      case 'csstemplate':
  79          $title = get_string('csstemplate', 'data');
  80          break;
  81      case 'jstemplate':
  82          $title = get_string('jstemplate', 'data');
  83          break;
  84      case 'listtemplate':
  85          $title = get_string('listtemplate', 'data');
  86          break;
  87      case 'rsstemplate':
  88          $title = get_string('rsstemplate', 'data');
  89          break;
  90      case 'singletemplate':
  91          $title = get_string('singletemplate', 'data');
  92          break;
  93      default:
  94          if ($manager->has_fields()) {
  95              $title = get_string('addtemplate', 'data');
  96          } else {
  97              $title = get_string('data:managetemplates', 'data');
  98          }
  99          break;
 100  }
 101  $titleparts = [
 102      $title,
 103      format_string($instance->name),
 104      format_string($course->fullname),
 105  ];
 106  $PAGE->set_title(implode(moodle_page::TITLE_SEPARATOR, $titleparts));
 107  $PAGE->set_heading($course->fullname);
 108  $PAGE->set_pagelayout('admin');
 109  $PAGE->force_settings_menu(true);
 110  $PAGE->activityheader->disable();
 111  $PAGE->add_body_class('mediumwidth');
 112  
 113  echo $OUTPUT->header();
 114  
 115  $renderer = $manager->get_renderer();
 116  // Check if it is an empty database with no fields.
 117  if (!$manager->has_fields()) {
 118      echo $renderer->render_templates_zero_state($manager);
 119      echo $OUTPUT->footer();
 120      // Don't check the rest of the options. There is no field, there is nothing else to work with.
 121      exit;
 122  }
 123  
 124  $actionbar = new \mod_data\output\action_bar($instance->id, $url);
 125  echo $actionbar->get_templates_action_bar();
 126  
 127  if (($formdata = data_submitted()) && confirm_sesskey()) {
 128      if (!empty($formdata->defaultform)) {
 129          // Reset the template to default.
 130          if (!empty($formdata->resetall)) {
 131              $manager->reset_all_templates();
 132              $notificationstr = get_string('templateresetall', 'mod_data');
 133          } else {
 134              $manager->reset_template($mode);
 135              $notificationstr = get_string('templatereset', 'data');
 136          }
 137      } else {
 138          $manager->update_templates($formdata);
 139          $notificationstr = get_string('templatesaved', 'data');
 140      }
 141  }
 142  
 143  if (!empty($notificationstr)) {
 144      echo $OUTPUT->notification($notificationstr, 'notifysuccess');
 145  }
 146  
 147  $templateeditor = new \mod_data\output\template_editor($manager, $mode);
 148  echo $renderer->render($templateeditor);
 149  
 150  /// Finish the page
 151  echo $OUTPUT->footer();