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   * Card view dynamic form
  31   *
  32   * @package     core_reportbuilder
  33   * @copyright   2021 Mikel Martín <mikel@moodle.com>
  34   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class card_view extends dynamic_form {
  37  
  38      /**
  39       * Return instance of the report using the card view 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       * Returns context where this form is used
  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       * Check if current user has access to this form, otherwise throw exception
  61       */
  62      public function check_access_for_dynamic_submission(): void {
  63          permission::require_can_edit_report($this->get_report()->get_report_persistent());
  64      }
  65  
  66      /**
  67       * Store the conditions values and operators
  68       *
  69       * @return bool
  70       */
  71      public function process_dynamic_submission(): bool {
  72          $values = $this->get_data();
  73  
  74          $settings = [
  75              'cardview_showfirsttitle' => (int)$values->showfirsttitle,
  76              // Minimum value for 'cardview_visiblecolumns' should be 1.
  77              'cardview_visiblecolumns' => max((int)$values->visiblecolumns, 1)
  78          ];
  79          return $this->get_report()->set_settings_values($settings);
  80      }
  81  
  82      /**
  83       * Load in existing data as form defaults
  84       */
  85      public function set_data_for_dynamic_submission(): void {
  86          $report = $this->get_report();
  87          $settings = $report->get_settings_values();
  88  
  89          $defaults = [
  90              // Maximum value for 'cardview_visiblecolumns' should be the report total number of columns.
  91              'visiblecolumns' => min($settings['cardview_visiblecolumns'] ?? 1, count($report->get_active_columns())),
  92              'showfirsttitle' => $settings['cardview_showfirsttitle'] ?? 0,
  93          ];
  94          $this->set_data(array_merge($defaults, $this->_ajaxformdata));
  95      }
  96  
  97      /**
  98       * Returns url to set in $PAGE->set_url() when form is being rendered or submitted via AJAX
  99       *
 100       * @return moodle_url
 101       */
 102      protected function get_page_url_for_dynamic_submission(): moodle_url {
 103          return new moodle_url('/reportbuilder/edit.php');
 104      }
 105  
 106      /**
 107       * Card view form definition
 108       */
 109      public function definition(): void {
 110          $report = $this->get_report();
 111  
 112          $mform = $this->_form;
 113  
 114          $mform->addElement('hidden', 'reportid');
 115          $mform->setType('reportid', PARAM_INT);
 116  
 117          // Generate select options from 1 to report total number of columns.
 118          $visiblecolumns = range(1, max(count($report->get_active_columns()), 1));
 119          $mform->addElement('select', 'visiblecolumns', get_string('cardviewvisiblecolumns', 'core_reportbuilder'),
 120              array_combine($visiblecolumns, $visiblecolumns));
 121          $mform->setType('visiblecolumns', PARAM_INT);
 122  
 123          $mform->addElement('selectyesno', 'showfirsttitle', get_string('cardviewfirstcolumntitle', 'core_reportbuilder'));
 124          $mform->setType('showfirsttitle', PARAM_BOOL);
 125  
 126          $mform->disable_form_change_checker();
 127  
 128          $this->add_action_buttons(false);
 129      }
 130  }