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\reports;
  20  
  21  use context_system;
  22  use external_api;
  23  use external_function_parameters;
  24  use external_multiple_structure;
  25  use external_single_structure;
  26  use external_value;
  27  use external_warnings;
  28  use stdClass;
  29  use core_reportbuilder\permission;
  30  use core_reportbuilder\external\custom_report_details_exporter;
  31  use core_reportbuilder\local\helpers\audience;
  32  use core_reportbuilder\local\models\report;
  33  
  34  defined('MOODLE_INTERNAL') || die();
  35  
  36  global $CFG;
  37  require_once("{$CFG->libdir}/externallib.php");
  38  
  39  /**
  40   * External method for listing users' custom reports
  41   *
  42   * @package     core_reportbuilder
  43   * @copyright   2022 Paul Holden <paulh@moodle.com>
  44   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  45   */
  46  class listing extends external_api {
  47  
  48      /**
  49       * External method parameters
  50       *
  51       * @return external_function_parameters
  52       */
  53      public static function execute_parameters(): external_function_parameters {
  54          return new external_function_parameters([
  55              'page' => new external_value(PARAM_INT, 'Page number', VALUE_DEFAULT, 0),
  56              'perpage' => new external_value(PARAM_INT, 'Reports per page', VALUE_DEFAULT, 10),
  57          ]);
  58      }
  59  
  60      /**
  61       * External method execution
  62       *
  63       * @param int $page
  64       * @param int $perpage
  65       * @return array
  66       */
  67      public static function execute(int $page = 0, int $perpage = 10): array {
  68          global $DB, $PAGE;
  69  
  70          [
  71              'page' => $page,
  72              'perpage' => $perpage,
  73          ] = self::validate_parameters(self::execute_parameters(), [
  74              'page' => $page,
  75              'perpage' => $perpage,
  76          ]);
  77  
  78          $context = context_system::instance();
  79          self::validate_context($context);
  80  
  81          permission::require_can_view_reports_list(null, $context);
  82  
  83          // Filter list of reports by those the user can access.
  84          [$where, $params] = audience::user_reports_list_access_sql('r');
  85          $reports = $DB->get_records_sql("
  86              SELECT r.*
  87                FROM {" . report::TABLE . "} r
  88               WHERE r.type = 0 AND {$where}
  89            ORDER BY r.name, r.id", $params, $page * $perpage, $perpage);
  90  
  91          $output = $PAGE->get_renderer('core');
  92  
  93          return [
  94              'reports' => array_map(static function(stdClass $report) use ($output): array {
  95                  $exporter = new custom_report_details_exporter(new report(0, $report));
  96                  return (array) $exporter->export($output);
  97              }, $reports),
  98              'warnings' => [],
  99          ];
 100      }
 101  
 102      /**
 103       * External method return value
 104       *
 105       * @return external_single_structure
 106       */
 107      public static function execute_returns(): external_single_structure {
 108          return new external_single_structure([
 109              'reports' => new external_multiple_structure(custom_report_details_exporter::get_read_structure()),
 110              'warnings' => new external_warnings(),
 111          ]);
 112      }
 113  }