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