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\models\filter;
  26  
  27  defined('MOODLE_INTERNAL') || die;
  28  
  29  global $CFG;
  30  require_once("{$CFG->libdir}/external/externallib.php");
  31  
  32  /**
  33   * Filter heading editable component
  34   *
  35   * @package     core_reportbuilder
  36   * @copyright   2021 David Matamoros <davidmc@moodle.com>
  37   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class filter_heading_editable extends inplace_editable {
  40  
  41      /**
  42       * Class constructor
  43       *
  44       * @param int $filterid
  45       * @param filter|null $filter
  46       */
  47      public function __construct(int $filterid, ?filter $filter = null) {
  48          if ($filter === null) {
  49              $filter = new filter($filterid);
  50          }
  51  
  52          $report = $filter->get_report();
  53          $editable = permission::can_edit_report($report);
  54  
  55          $filterinstance = manager::get_report_from_persistent($report)
  56              ->get_filter($filter->get('uniqueidentifier'));
  57  
  58          // Use filter defined header if custom heading not set.
  59          if ('' !== $value = (string) $filter->get('heading')) {
  60              $displayvalue = $filter->get_formatted_heading($report->get_context());
  61          } else {
  62              $displayvalue = $value = $filterinstance->get_header();
  63          }
  64  
  65          parent::__construct('core_reportbuilder', 'filterheading', $filter->get('id'), $editable, $displayvalue, $value,
  66              get_string('renamefilter', 'core_reportbuilder', $filterinstance->get_header()));
  67      }
  68  
  69      /**
  70       * Update filter persistent and return self, called from inplace_editable callback
  71       *
  72       * @param int $filterid
  73       * @param string $value
  74       * @return self
  75       */
  76      public static function update(int $filterid, string $value): self {
  77          $filter = new filter($filterid);
  78  
  79          $report = $filter->get_report();
  80          core_external::validate_context($report->get_context());
  81          permission::require_can_edit_report($report);
  82  
  83          $value = clean_param($value, PARAM_TEXT);
  84          $filter
  85              ->set('heading', trim($value))
  86              ->update();
  87  
  88          return new self(0, $filter);
  89      }
  90  }