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 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\external\filters;
  20  
  21  use external_api;
  22  use external_function_parameters;
  23  use external_value;
  24  use core_reportbuilder\manager;
  25  use core_reportbuilder\permission;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  global $CFG;
  30  require_once("{$CFG->libdir}/externallib.php");
  31  
  32  /**
  33   * External method for setting report filter values
  34   *
  35   * @package     core_reportbuilder
  36   * @copyright   2022 Paul Holden <paulh@moodle.com>
  37   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class set extends external_api {
  40  
  41      /**
  42       * External method parameters
  43       *
  44       * @return external_function_parameters
  45       */
  46      public static function execute_parameters(): external_function_parameters {
  47          return new external_function_parameters([
  48              'reportid' => new external_value(PARAM_INT, 'Report ID'),
  49              'parameters' => new external_value(PARAM_RAW, 'JSON encoded report parameters', VALUE_DEFAULT, ''),
  50              'values' => new external_value(PARAM_RAW, 'JSON encoded filter values'),
  51          ]);
  52      }
  53  
  54      /**
  55       * External method execution
  56       *
  57       * @param int $reportid
  58       * @param string $parameters
  59       * @param string $values
  60       * @return bool
  61       */
  62      public static function execute(int $reportid, string $parameters, string $values): bool {
  63          [
  64              'reportid' => $reportid,
  65              'parameters' => $parameters,
  66              'values' => $values,
  67          ] = self::validate_parameters(self::execute_parameters(), [
  68              'reportid' => $reportid,
  69              'parameters' => $parameters,
  70              'values' => $values,
  71          ]);
  72  
  73          $report = manager::get_report_from_id($reportid, (array) json_decode($parameters));
  74          self::validate_context($report->get_context());
  75  
  76          // System report permission is implicitly handled, we need to make sure custom report can be viewed.
  77          $persistent = $report->get_report_persistent();
  78          if ($persistent->get('type') === $report::TYPE_CUSTOM_REPORT) {
  79              permission::require_can_view_report($persistent);
  80          }
  81  
  82          return $report->set_filter_values((array) json_decode($values));
  83      }
  84  
  85      /**
  86       * External method return value
  87       *
  88       * @return external_value
  89       */
  90      public static function execute_returns(): external_value {
  91          return new external_value(PARAM_BOOL, 'Success');
  92      }
  93  }