Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

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

   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\output;
  20  
  21  use core\output\inplace_editable;
  22  use core_external\external_api;
  23  use core_reportbuilder\manager;
  24  use core_reportbuilder\permission;
  25  use core_reportbuilder\local\helpers\aggregation;
  26  use core_reportbuilder\local\models\column;
  27  
  28  /**
  29   * Column aggregation editable component
  30   *
  31   * @package     core_reportbuilder
  32   * @copyright   2021 Paul Holden <paulh@moodle.com>
  33   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class column_aggregation_editable extends inplace_editable {
  36  
  37      /**
  38       * Class constructor
  39       *
  40       * @param int $columnid
  41       * @param column|null $column
  42       */
  43      public function __construct(int $columnid, ?column $column = null) {
  44          if ($column === null) {
  45              $column = new column($columnid);
  46          }
  47  
  48          $report = $column->get_report();
  49          $editable = permission::can_edit_report($report);
  50  
  51          $columninstance = manager::get_report_from_persistent($report)
  52              ->get_column($column->get('uniqueidentifier'));
  53  
  54          $currentvalue = (string) $column->get('aggregation');
  55  
  56          $editlabel = get_string('aggregatecolumn', 'core_reportbuilder', $columninstance->get_title());
  57          parent::__construct('core_reportbuilder', 'columnaggregation', $column->get('id'), $editable, null, $currentvalue,
  58              $editlabel, $editlabel);
  59  
  60          // List of available aggregation methods for the column type, minus any specifically disabled.
  61          $options = aggregation::get_column_aggregations($columninstance->get_type(),
  62              $columninstance->get_disabled_aggregation());
  63  
  64          $this->set_type_select(['' => get_string('aggregationnone', 'core_reportbuilder')] + $options);
  65      }
  66  
  67      /**
  68       * Update column persistent and return self, called from inplace_editable callback
  69       *
  70       * @param int $columnid
  71       * @param string $value
  72       * @return self
  73       */
  74      public static function update(int $columnid, string $value): self {
  75          $column = new column($columnid);
  76  
  77          $report = $column->get_report();
  78  
  79          external_api::validate_context($report->get_context());
  80          permission::require_can_edit_report($report);
  81  
  82          $value = clean_param($value, PARAM_TEXT);
  83          $column
  84              ->set('aggregation', $value)
  85              ->update();
  86  
  87          return new self(0, $column);
  88      }
  89  }