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.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  declare(strict_types=1);
  18  
  19  namespace core_reportbuilder\form;
  20  
  21  use context;
  22  use context_system;
  23  use core_reportbuilder\permission;
  24  use moodle_url;
  25  use core_form\dynamic_form;
  26  use core_reportbuilder\datasource;
  27  use core_reportbuilder\manager;
  28  use core_reportbuilder\local\helpers\report as reporthelper;
  29  
  30  defined('MOODLE_INTERNAL') || die();
  31  
  32  global $CFG;
  33  require_once("$CFG->libdir/formslib.php");
  34  
  35  /**
  36   * Report details form
  37   *
  38   * @package     core_reportbuilder
  39   * @copyright   2021 David Matamoros <davidmc@moodle.com>
  40   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class report extends dynamic_form {
  43  
  44      /**
  45       * Return instance of the custom report we are editing, or null when creating a new report
  46       *
  47       * @return datasource|null
  48       */
  49      protected function get_custom_report(): ?datasource {
  50          if ($reportid = $this->optional_param('id', 0, PARAM_INT)) {
  51              /** @var datasource $customreport */
  52              $customreport = manager::get_report_from_id($reportid);
  53  
  54              return $customreport;
  55          }
  56  
  57          return null;
  58      }
  59  
  60      /**
  61       * Return the context for the form, it should be that of the custom report itself, or system when creating a new report
  62       *
  63       * @return context
  64       */
  65      public function get_context_for_dynamic_submission(): context {
  66          if ($report = $this->get_custom_report()) {
  67              return $report->get_context();
  68          } else {
  69              return context_system::instance();
  70          }
  71      }
  72  
  73      /**
  74       * Ensure current user is able to use this form
  75       *
  76       * A {@see \core_reportbuilder\report_access_exception} will be thrown if they can't
  77       */
  78      protected function check_access_for_dynamic_submission(): void {
  79          $report = $this->get_custom_report();
  80  
  81          if ($report) {
  82              permission::require_can_edit_report($report->get_report_persistent());
  83          } else {
  84              permission::require_can_create_report();
  85          }
  86      }
  87  
  88      /**
  89       * Form definition
  90       */
  91      public function definition() {
  92          $mform = $this->_form;
  93  
  94          $mform->addElement('hidden', 'id');
  95          $mform->setType('id', PARAM_INT);
  96  
  97          $mform->addElement('text', 'name', get_string('name'));
  98          $mform->setType('name', PARAM_TEXT);
  99          $mform->addRule('name', null, 'required', null, 'client');
 100          $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255);
 101  
 102          // Allow user to select report source if creating a new report.
 103          if (!$this->get_custom_report()) {
 104              $default = ['' => ['' => get_string('selectareportsource', 'core_reportbuilder')]];
 105              $mform->addElement('selectgroups', 'source', get_string('reportsource', 'core_reportbuilder'),
 106                  array_merge($default, manager::get_report_datasources()));
 107              $mform->addRule('source', null, 'required', null, 'client');
 108              $mform->addHelpButton('source', 'reportsource', 'core_reportbuilder');
 109  
 110              $mform->addElement('advcheckbox', 'includedefaultsetup', get_string('includedefaultsetup', 'core_reportbuilder'));
 111              $mform->setDefault('includedefaultsetup', 1);
 112              $mform->addHelpButton('includedefaultsetup', 'includedefaultsetup', 'core_reportbuilder');
 113          }
 114  
 115          $mform->addElement('advcheckbox', 'uniquerows', get_string('uniquerows', 'core_reportbuilder'));
 116          $mform->addHelpButton('uniquerows', 'uniquerows', 'core_reportbuilder');
 117      }
 118  
 119      /**
 120       * Process the form submission
 121       *
 122       * @return string The URL to advance to upon completion
 123       */
 124      public function process_dynamic_submission() {
 125          $data = $this->get_data();
 126  
 127          if ($data->id) {
 128              $reportpersistent = reporthelper::update_report($data);
 129          } else {
 130              $reportpersistent = reporthelper::create_report($data, (bool)$data->includedefaultsetup);
 131          }
 132  
 133          return (new moodle_url('/reportbuilder/edit.php', ['id' => $reportpersistent->get('id')]))->out(false);
 134      }
 135  
 136      /**
 137       * Load in existing data as form defaults
 138       */
 139      public function set_data_for_dynamic_submission(): void {
 140          if ($report = $this->get_custom_report()) {
 141              $this->set_data($report->get_report_persistent()->to_record());
 142          }
 143      }
 144  
 145      /**
 146       * URL of the page using this form
 147       *
 148       * @return moodle_url
 149       */
 150      public function get_page_url_for_dynamic_submission(): moodle_url {
 151          return new moodle_url('/reportbuilder/index.php');
 152      }
 153  
 154      /**
 155       * Perform some extra moodle validation
 156       *
 157       * @param array $data
 158       * @param array $files
 159       * @return array
 160       */
 161      public function validation($data, $files): array {
 162          $errors = [];
 163  
 164          if (trim($data['name']) === '') {
 165              $errors['name'] = get_string('required');
 166          }
 167  
 168          return $errors;
 169      }
 170  }