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 400 and 401]

   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 moodle_url;
  23  use core_form\dynamic_form;
  24  use core_reportbuilder\manager;
  25  use core_reportbuilder\permission;
  26  use core_reportbuilder\local\report\base;
  27  use core_reportbuilder\local\models\report;
  28  
  29  /**
  30   * Dynamic condition form
  31   *
  32   * @package     core_reportbuilder
  33   * @copyright   2021 Paul Holden <paulh@moodle.com>
  34   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class condition extends dynamic_form {
  37  
  38      /**
  39       * Return instance of the report using the condition form
  40       *
  41       * @return base
  42       */
  43      private function get_report(): base {
  44          $report = new report($this->optional_param('reportid', 0, PARAM_INT));
  45          $parameters = (array) json_decode($this->optional_param('parameters', '', PARAM_RAW));
  46  
  47          return manager::get_report_from_persistent($report, $parameters);
  48      }
  49  
  50      /**
  51       * Return the context for the form, it should be that of the system report itself
  52       *
  53       * @return context
  54       */
  55      protected function get_context_for_dynamic_submission(): context {
  56          return $this->get_report()->get_context();
  57      }
  58  
  59      /**
  60       * Ensure current user is able to use this form
  61       *
  62       * A {@see \core_reportbuilder\report_access_exception} will be thrown if they can't
  63       */
  64      protected function check_access_for_dynamic_submission(): void {
  65          permission::require_can_edit_report($this->get_report()->get_report_persistent());
  66      }
  67  
  68      /**
  69       * Process the form submission
  70       *
  71       * @return bool
  72       */
  73      public function process_dynamic_submission() {
  74          $values = $this->get_data();
  75  
  76          // Remove some unneeded fields.
  77          unset($values->reportid, $values->parameters);
  78  
  79          return $this->get_report()->set_condition_values((array) $values);
  80      }
  81  
  82      /**
  83       * Load in existing data as form defaults
  84       */
  85      public function set_data_for_dynamic_submission(): void {
  86          $defaults = [
  87              'reportid' => $this->optional_param('reportid', 0, PARAM_INT),
  88              'parameters' => $this->optional_param('parameters', 0, PARAM_RAW),
  89          ];
  90  
  91          $this->set_data(array_merge($defaults, $this->get_report()->get_condition_values()));
  92      }
  93  
  94      /**
  95       * URL of the page using this form
  96       *
  97       * @return moodle_url
  98       */
  99      protected function get_page_url_for_dynamic_submission(): moodle_url {
 100          return new moodle_url('/');
 101      }
 102  
 103      /**
 104       * Condition form definition
 105       */
 106      protected function definition() {
 107          global $OUTPUT;
 108  
 109          $mform = $this->_form;
 110  
 111          $mform->addElement('hidden', 'reportid');
 112          $mform->setType('reportid', PARAM_INT);
 113  
 114          $mform->addElement('hidden', 'parameters');
 115          $mform->setType('parameters', PARAM_RAW);
 116  
 117          // Wrap the form elements inside an outer container, as drag/drop requires draggable elements to be immediate
 118          // descendants of said container. Note this is identified by it's data-region property in the editor module.
 119          $mform->addElement('html', '<div class="list-group mt-2" data-region="active-conditions">');
 120  
 121          // Allow each condition instance to add itself to this form, wrapping each inside custom header/footer template.
 122          $conditioninstances = $this->get_report()->get_condition_instances();
 123          foreach ($conditioninstances as $conditioninstance) {
 124              $persistent = $conditioninstance->get_filter_persistent();
 125  
 126              $entityname = $conditioninstance->get_entity_name();
 127              $displayvalue = $conditioninstance->get_header();
 128  
 129              $mform->addElement('html', $OUTPUT->render_from_template('core_reportbuilder/local/conditions/header', [
 130                  'id' => $persistent->get('id'),
 131                  'entityname' => $this->get_report()->get_entity_title($entityname),
 132                  'heading' => $displayvalue,
 133                  'sortorder' => $persistent->get('filterorder'),
 134                  'movetitle' => get_string('movecondition', 'core_reportbuilder', $displayvalue),
 135              ]));
 136  
 137              $conditioninstance->setup_form($mform);
 138              $mform->addElement('html', $OUTPUT->render_from_template('core_reportbuilder/local/conditions/footer', []));
 139          }
 140          $mform->addElement('html', '</div>');
 141          $this->set_display_vertical();
 142  
 143          // We'll add a second submit button to the form that will be used to reset current report conditions.
 144          $mform->registerNoSubmitButton('resetconditions');
 145  
 146          $buttons = [];
 147          $buttons[] = $mform->createElement('submit', 'submitbutton', get_string('apply', 'core_reportbuilder'));
 148          $buttons[] = $mform->createElement('submit', 'resetconditions',  get_string('resetall', 'core_reportbuilder'),
 149              null, null, ['customclassoverride' => 'btn-link']);
 150  
 151          $mform->addGroup($buttons, 'buttonar', '', [' '], false);
 152          $mform->closeHeaderBefore('buttonar');
 153  
 154          $mform->disable_form_change_checker();
 155      }
 156  }