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