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\permission;
  24  use core_reportbuilder\local\audiences\base;
  25  use core_reportbuilder\local\models\audience;
  26  
  27  /**
  28   * Audience heading editable component
  29   *
  30   * @package     core_reportbuilder
  31   * @copyright   2021 Paul Holden <paulh@moodle.com>
  32   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class audience_heading_editable extends inplace_editable {
  35  
  36      /**
  37       * Class constructor
  38       *
  39       * @param int $audienceid
  40       * @param audience|null $audience
  41       */
  42      public function __construct(int $audienceid, ?audience $audience = null) {
  43          if ($audience === null) {
  44              $audience = new audience($audienceid);
  45          }
  46  
  47          $report = $audience->get_report();
  48          $editable = permission::can_edit_report($report);
  49  
  50          $audienceinstance = base::instance(0, $audience->to_record());
  51  
  52          // Use audience defined title if custom heading not set.
  53          if ('' !== $value = (string) $audience->get('heading')) {
  54              $displayvalue = $audience->get_formatted_heading($report->get_context());
  55          } else {
  56              $displayvalue = $value = $audienceinstance->get_name();
  57          }
  58  
  59          parent::__construct('core_reportbuilder', 'audienceheading', $audience->get('id'), $editable, $displayvalue, $value,
  60              get_string('renameaudience', 'core_reportbuilder', $audienceinstance->get_name()));
  61      }
  62  
  63      /**
  64       * Update audience persistent and return self, called from inplace_editable callback
  65       *
  66       * @param int $audienceid
  67       * @param string $value
  68       * @return self
  69       */
  70      public static function update(int $audienceid, string $value): self {
  71          $audience = new audience($audienceid);
  72  
  73          $report = $audience->get_report();
  74  
  75          external_api::validate_context($report->get_context());
  76          permission::require_can_edit_report($report);
  77  
  78          $value = clean_param($value, PARAM_TEXT);
  79          $audience
  80              ->set('heading', $value)
  81              ->update();
  82  
  83          return new self(0, $audience);
  84      }
  85  }