Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   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  namespace gradereport_user\external;
  18  
  19  use context_course;
  20  use core_external\external_api;
  21  use core_external\external_function_parameters;
  22  use core_external\external_single_structure;
  23  use core_external\external_value;
  24  use core_external\external_warnings;
  25  use stdClass;
  26  
  27  
  28  /**
  29   * External grade report API implementation
  30   *
  31   * @package    gradereport_user
  32   * @copyright  2023 Juan Leyva <juan@moodle.com>
  33   * @category   external
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class get_access_information extends external_api {
  37  
  38      /**
  39       * Returns description of method parameters.
  40       *
  41       * @return external_function_parameters.
  42       * @since  Moodle 4.2
  43       */
  44      public static function execute_parameters(): external_function_parameters {
  45          return new external_function_parameters(
  46              [
  47                  'courseid' => new external_value(PARAM_INT, 'Course to check.'),
  48              ]
  49          );
  50      }
  51  
  52      /**
  53       * Convenience function to retrieve some permissions information for the given course grade report.
  54       *
  55       * @param int $courseid Course to check, empty for site.
  56       * @return array The access information
  57       * @since  Moodle 4.2
  58       */
  59      public static function execute(int $courseid): array {
  60  
  61          $params = self::validate_parameters(self::execute_parameters(), ['courseid' => $courseid]);
  62  
  63          $context = context_course::instance($params['courseid']);
  64  
  65          self::validate_context($context);
  66  
  67          return [
  68              'canviewusergradereport' => has_capability('gradereport/user:view', $context),
  69              'canviewmygrades' => has_capability('moodle/grade:view', $context),
  70              'canviewallgrades' => has_capability('moodle/grade:viewall', $context),
  71              'warnings' => [],
  72          ];
  73      }
  74  
  75      /**
  76       * Returns description of method result value.
  77       *
  78       * @return external_single_structure.
  79       * @since  Moodle 4.2
  80       */
  81      public static function execute_returns(): external_single_structure {
  82  
  83          return new external_single_structure(
  84              [
  85                  'canviewusergradereport' => new external_value(PARAM_BOOL, 'Whether the user can view the user grade report.'),
  86                  'canviewmygrades' => new external_value(PARAM_BOOL, 'Whether the user can view his grades in the course.'),
  87                  'canviewallgrades' => new external_value(PARAM_BOOL, 'Whether the user can view all users grades in the course.'),
  88                  'warnings' => new external_warnings(),
  89              ]
  90          );
  91      }
  92  }