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