Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

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