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 core_grades\external;
  18  
  19  defined('MOODLE_INTERNAL') || die;
  20  
  21  use context_course;
  22  use core_external\external_api;
  23  use core_external\external_function_parameters;
  24  use core_external\external_multiple_structure;
  25  use core_external\external_single_structure;
  26  use core_external\external_value;
  27  use core_external\external_warnings;
  28  use core_external\restricted_context_exception;
  29  use grade_item;
  30  
  31  require_once($CFG->libdir . '/gradelib.php');
  32  
  33  /**
  34   * External grade get gradeitems API implementation
  35   *
  36   * @package    core_grades
  37   * @copyright  2023 Mathew May <mathew.solutions>
  38   * @category   external
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class get_gradeitems extends external_api {
  42  
  43      /**
  44       * Returns description of method parameters.
  45       *
  46       * @return external_function_parameters
  47       */
  48      public static function execute_parameters(): external_function_parameters {
  49          return new external_function_parameters (
  50              [
  51                  'courseid' => new external_value(PARAM_INT, 'Course ID', VALUE_REQUIRED)
  52              ]
  53          );
  54      }
  55  
  56      /**
  57       * Given a course ID find the grading objects and return their names & IDs.
  58       *
  59       * @param int $courseid
  60       * @return array
  61       * @throws restricted_context_exception
  62       * @throws \invalid_parameter_exception
  63       */
  64      public static function execute(int $courseid): array {
  65          $params = self::validate_parameters(
  66              self::execute_parameters(),
  67              [
  68                  'courseid' => $courseid
  69              ]
  70          );
  71  
  72          $warnings = [];
  73          $context = context_course::instance($params['courseid']);
  74          parent::validate_context($context);
  75  
  76          $allgradeitems = grade_item::fetch_all(['courseid' => $params['courseid']]);
  77          $gradeitems = array_filter($allgradeitems, function($item) {
  78              $item->itemname = $item->get_name();
  79              $item->category = $item->get_parent_category()->get_name();
  80              return $item->gradetype != GRADE_TYPE_NONE && !$item->is_category_item() && !$item->is_course_item();
  81          });
  82  
  83          return [
  84              'gradeItems' => $gradeitems,
  85              'warnings' => $warnings,
  86          ];
  87      }
  88  
  89      /**
  90       * Returns description of what gradeitems fetch should return.
  91       *
  92       * @return external_single_structure
  93       */
  94      public static function execute_returns(): external_single_structure {
  95          return new external_single_structure([
  96              'gradeItems' => new external_multiple_structure(
  97                  new external_single_structure([
  98                      'id' => new external_value(PARAM_ALPHANUM, 'An ID for the grade item', VALUE_REQUIRED),
  99                      'itemname' => new external_value(PARAM_CLEANHTML, 'The full name of the grade item', VALUE_REQUIRED),
 100                      'category' => new external_value(PARAM_TEXT, 'The grade category of the grade item', VALUE_OPTIONAL),
 101                  ])
 102              ),
 103              'warnings' => new external_warnings(),
 104          ]);
 105      }
 106  }