Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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\models\schedule;
  25  
  26  /**
  27   * Schedule name editable component
  28   *
  29   * @package     core_reportbuilder
  30   * @copyright   2021 Paul Holden <paulh@moodle.com>
  31   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class schedule_name_editable extends inplace_editable {
  34  
  35      /**
  36       * Class constructor
  37       *
  38       * @param int $scheduleid
  39       * @param schedule|null $schedule
  40       */
  41      public function __construct(int $scheduleid, ?schedule $schedule = null) {
  42          if ($schedule === null) {
  43              $schedule = new schedule($scheduleid);
  44          }
  45  
  46          $report = $schedule->get_report();
  47          $editable = permission::can_edit_report($report);
  48  
  49          parent::__construct('core_reportbuilder', 'schedulename', $schedule->get('id'), $editable, $schedule->get_formatted_name(),
  50              $schedule->get('name'), get_string('editschedulename', 'core_reportbuilder'));
  51      }
  52  
  53      /**
  54       * Update schedule persistent and return self, called from inplace_editable callback
  55       *
  56       * @param int $scheduleid
  57       * @param string $value
  58       * @return self
  59       */
  60      public static function update(int $scheduleid, string $value): self {
  61          $schedule = new schedule($scheduleid);
  62  
  63          $report = $schedule->get_report();
  64  
  65          core_external::validate_context($report->get_context());
  66          permission::require_can_edit_report($report);
  67  
  68          $value = trim(clean_param($value, PARAM_TEXT));
  69          if ($value !== '') {
  70              $schedule
  71                  ->set('name', $value)
  72                  ->update();
  73          }
  74  
  75          return new self(0, $schedule);
  76      }
  77  }