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\external\columns\sort;
  20  
  21  use core_external\external_api;
  22  use core_external\external_value;
  23  use core_external\external_single_structure;
  24  use core_external\external_function_parameters;
  25  use core_reportbuilder\manager;
  26  use core_reportbuilder\permission;
  27  use core_reportbuilder\local\helpers\report;
  28  use core_reportbuilder\external\custom_report_columns_sorting_exporter;
  29  
  30  /**
  31   * External method for toggling report column sorting
  32   *
  33   * @package     core_reportbuilder
  34   * @copyright   2021 Paul Holden <paulh@moodle.com>
  35   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class toggle extends external_api {
  38  
  39      /**
  40       * External method parameters
  41       *
  42       * @return external_function_parameters
  43       */
  44      public static function execute_parameters(): external_function_parameters {
  45          return new external_function_parameters([
  46              'reportid' => new external_value(PARAM_INT, 'Report ID'),
  47              'columnid' => new external_value(PARAM_INT, 'Column ID'),
  48              'enabled' => new external_value(PARAM_BOOL, 'Sort enabled'),
  49              'direction' => new external_value(PARAM_INT, 'Sort direction', VALUE_DEFAULT, SORT_ASC),
  50          ]);
  51      }
  52  
  53      /**
  54       * External method execution
  55       *
  56       * @param int $reportid
  57       * @param int $columnid
  58       * @param bool $enabled
  59       * @param int $direction
  60       * @return array
  61       */
  62      public static function execute(int $reportid, int $columnid, bool $enabled, int $direction = SORT_ASC): array {
  63          global $PAGE;
  64  
  65          [
  66              'reportid' => $reportid,
  67              'columnid' => $columnid,
  68              'enabled' => $enabled,
  69              'direction' => $direction,
  70          ] = self::validate_parameters(self::execute_parameters(), [
  71              'reportid' => $reportid,
  72              'columnid' => $columnid,
  73              'enabled' => $enabled,
  74              'direction' => $direction,
  75          ]);
  76  
  77          $report = manager::get_report_from_id($reportid);
  78  
  79          self::validate_context($report->get_context());
  80          permission::require_can_edit_report($report->get_report_persistent());
  81  
  82          report::toggle_report_column_sorting($reportid, $columnid, $enabled, $direction);
  83  
  84          $exporter = new custom_report_columns_sorting_exporter(null, [
  85              'report' => $report,
  86          ]);
  87  
  88          return (array) $exporter->export($PAGE->get_renderer('core'));
  89      }
  90  
  91      /**
  92       * External method return value
  93       *
  94       * @return external_single_structure
  95       */
  96      public static function execute_returns(): external_single_structure {
  97          return custom_report_columns_sorting_exporter::get_read_structure();
  98      }
  99  }