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 html_writer;
  22  use moodle_url;
  23  use core\output\inplace_editable;
  24  use core_external\external_api;
  25  use core_reportbuilder\permission;
  26  use core_reportbuilder\local\models\report;
  27  
  28  /**
  29   * Report name 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 report_name_editable extends inplace_editable {
  36  
  37      /**
  38       * Class constructor
  39       *
  40       * @param int $reportid
  41       * @param report|null $report The report persistent, note that in addition to id/name properties being present we also
  42       *      require the following to be correctly set in order to perform permission checks: contextid/type/usercreated
  43       */
  44      public function __construct(int $reportid, ?report $report = null) {
  45          if ($report === null) {
  46              $report = new report($reportid);
  47          }
  48  
  49          $editable = permission::can_edit_report($report);
  50  
  51          $url = $editable
  52              ? new moodle_url('/reportbuilder/edit.php', ['id' => $report->get('id')])
  53              : new moodle_url('/reportbuilder/view.php', ['id' => $report->get('id')]);
  54  
  55          $displayvalue = html_writer::link($url, $report->get_formatted_name());
  56  
  57          parent::__construct('core_reportbuilder', 'reportname', $report->get('id'), $editable, $displayvalue, $report->get('name'),
  58              get_string('editreportname', 'core_reportbuilder'));
  59      }
  60  
  61      /**
  62       * Update report persistent and return self, called from inplace_editable callback
  63       *
  64       * @param int $reportid
  65       * @param string $value
  66       * @return self
  67       */
  68      public static function update(int $reportid, string $value): self {
  69          $report = new report($reportid);
  70  
  71          external_api::validate_context($report->get_context());
  72          permission::require_can_edit_report($report);
  73  
  74          $value = trim(clean_param($value, PARAM_TEXT));
  75          if ($value !== '') {
  76              $report
  77                  ->set('name', $value)
  78                  ->update();
  79          }
  80  
  81          return new self(0, $report);
  82      }
  83  }