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 core_form\dynamic_form;
  23  use core_reportbuilder\local\audiences\base;
  24  use core_reportbuilder\output\audience_heading_editable;
  25  use core_reportbuilder\permission;
  26  use moodle_url;
  27  use stdClass;
  28  
  29  /**
  30   * Dynamic audience form
  31   *
  32   * @package     core_reportbuilder
  33   * @copyright   2021 David Matamoros <davidmc@moodle.com>
  34   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class audience extends dynamic_form {
  37  
  38      /**
  39       * Audience we work with
  40       *
  41       * @return base
  42       */
  43      protected function get_audience(): base {
  44          $id = $this->optional_param('id', 0, PARAM_INT);
  45  
  46          $record = new stdClass();
  47          if (!$id) {
  48              // New instance, pre-define report id and classname.
  49              $record->reportid = $this->optional_param('reportid', null, PARAM_INT);
  50              $record->classname = $this->optional_param('classname', null, PARAM_RAW_TRIMMED);
  51          }
  52          return base::instance($id, $record);
  53      }
  54  
  55      /**
  56       * Form definition.
  57       */
  58      public function definition() {
  59          $mform = $this->_form;
  60  
  61          $mform->addElement('hidden', 'id');
  62          $mform->setType('id', PARAM_INT);
  63  
  64          $mform->addElement('hidden', 'reportid');
  65          $mform->setType('reportid', PARAM_INT);
  66  
  67          $mform->addElement('hidden', 'classname');
  68          $mform->setType('classname', PARAM_RAW_TRIMMED);
  69  
  70          // Embed form defined in audience class.
  71          $audience = $this->get_audience();
  72          $audience->get_config_form($mform);
  73  
  74          $this->add_action_buttons();
  75      }
  76  
  77      /**
  78       * Form validation.
  79       *
  80       * @param array $data array of ("fieldname"=>value) of submitted data
  81       * @param array $files array of uploaded files "element_name"=>tmp_file_path
  82       * @return array of "element_name"=>"error_description" if there are errors,
  83       *         or an empty array if everything is OK (true allowed for backwards compatibility too).
  84       */
  85      public function validation($data, $files) {
  86          $audience = $this->get_audience();
  87          return $audience->validate_config_form($data);
  88      }
  89  
  90      /**
  91       * Returns context where this form is used
  92       *
  93       * @return context
  94       */
  95      protected function get_context_for_dynamic_submission(): context {
  96          return $this->get_audience()->get_persistent()->get_report()->get_context();
  97      }
  98  
  99      /**
 100       * Ensure current user is able to use this form
 101       *
 102       * A {@see \core_reportbuilder\report_access_exception} will be thrown if they can't
 103       */
 104      protected function check_access_for_dynamic_submission(): void {
 105          $audience = $this->get_audience();
 106  
 107          $report = $audience->get_persistent()->get_report();
 108          permission::require_can_edit_report($report);
 109  
 110          // Check whether we are able to add/edit the current audience.
 111          $audience->get_persistent()->get('id') === 0
 112              ? $audience->require_user_can_add()
 113              : $audience->require_user_can_edit();
 114      }
 115  
 116      /**
 117       * Process the form submission, used if form was submitted via AJAX
 118       */
 119      public function process_dynamic_submission() {
 120          global $PAGE;
 121  
 122          $formdata = $this->get_data();
 123          $audience = $this->get_audience();
 124  
 125          $configdata = $audience::retrieve_configdata($formdata);
 126          if (!$formdata->id) {
 127              // New audience.
 128              $audience = $audience::create($formdata->reportid, $configdata);
 129          } else {
 130              // Editing audience.
 131              $audience->update_configdata($configdata);
 132          }
 133  
 134          $persistent = $audience->get_persistent();
 135          $editable = new audience_heading_editable(0, $persistent);
 136  
 137          return [
 138              'instanceid' => $persistent->get('id'),
 139              'heading' => $editable->render($PAGE->get_renderer('core')),
 140              'description' => $audience->get_description(),
 141          ];
 142      }
 143  
 144      /**
 145       * Load in existing data as form defaults
 146       */
 147      public function set_data_for_dynamic_submission(): void {
 148          $audience = $this->get_audience();
 149          $persistent = $audience->get_persistent();
 150  
 151          // Populate form data based on whether we are editing/creating an audience.
 152          if ($persistent->get('id') !== 0) {
 153              $formdata = [
 154                  'id' => $persistent->get('id'),
 155                  'reportid' => $persistent->get('reportid'),
 156                  'classname' => $persistent->get('classname'),
 157              ] + $audience->get_configdata();
 158          } else {
 159              $formdata = [
 160                  'reportid' => $this->optional_param('reportid', null, PARAM_INT),
 161                  'classname' => $this->optional_param('classname', null, PARAM_RAW_TRIMMED),
 162              ];
 163          }
 164  
 165          $this->set_data($formdata);
 166      }
 167  
 168      /**
 169       * Page url
 170       *
 171       * @return moodle_url
 172       */
 173      protected function get_page_url_for_dynamic_submission(): moodle_url {
 174          return new moodle_url('/reportbuilder/edit.php', ['id' => $this->optional_param('reportid', 0, PARAM_INT)]);
 175      }
 176  }