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.

Differences Between: [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  namespace gradereport_singleview\external;
  18  
  19  use context_course;
  20  use core_course_external;
  21  use core_external\external_function_parameters;
  22  use core_external\external_multiple_structure;
  23  use core_external\external_single_structure;
  24  use core_external\external_value;
  25  use core_external\external_warnings;
  26  use grade_tree;
  27  
  28  defined('MOODLE_INTERNAL') || die;
  29  
  30  require_once($CFG->dirroot.'/course/externallib.php');
  31  require_once($CFG->dirroot.'/grade/lib.php');
  32  
  33  /**
  34   * External grade report singleview API
  35   *
  36   * @package    gradereport_singleview
  37   * @copyright  2022 Mathew May <mathew.solutions>
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class singleview extends core_course_external {
  41      /**
  42       * Describes the parameters for get_users_for_course.
  43       *
  44       * @return external_function_parameters
  45       */
  46      public static function get_grade_items_for_search_widget_parameters(): external_function_parameters {
  47          return new external_function_parameters (
  48              [
  49                  'courseid' => new external_value(PARAM_INT, 'Course Id', VALUE_REQUIRED)
  50              ]
  51          );
  52      }
  53  
  54      /**
  55       * Given a course ID find the
  56       *
  57       * @param int $courseid
  58       * @return array Users and warnings to pass back to the calling widget.
  59       * @throws coding_exception
  60       * @throws invalid_parameter_exception
  61       * @throws moodle_exception
  62       * @throws restricted_context_exception
  63       */
  64      protected static function get_grade_items_for_search_widget(int $courseid): array {
  65          global $PAGE, $USER, $CFG;
  66  
  67          $params = self::validate_parameters(
  68              self::get_grade_items_for_search_widget_parameters(),
  69              [
  70                  'courseid' => $courseid,
  71              ]
  72          );
  73  
  74          $warnings = [];
  75          $coursecontext = context_course::instance($params['courseid']);
  76          parent::validate_context($coursecontext);
  77  
  78          $gtree = new grade_tree($params['courseid'], false, true, null, !$CFG->enableoutcomes);
  79          $gradeableitems = $gtree->get_items();
  80  
  81          $gradeitems = array_map(function ($gradeitem) use ($PAGE, $USER, $params) {
  82              $item = new \stdClass();
  83              $item->id = $gradeitem->id;
  84              $item->name = $gradeitem->get_name(true);
  85  
  86              return $item;
  87          }, $gradeableitems);
  88  
  89          return [
  90              'gradeitems' => $gradeitems,
  91              'warnings' => $warnings,
  92          ];
  93      }
  94  
  95      /**
  96       * Returns description of what the user search for the widget should return.
  97       *
  98       * @return external_single_structure
  99       */
 100      public static function get_grade_items_for_search_widget_returns(): external_single_structure {
 101          return new external_single_structure([
 102              'gradeitems' => new external_multiple_structure(
 103                  new external_single_structure([
 104                      'id'    => new external_value(PARAM_INT, 'ID of the grade item', VALUE_OPTIONAL),
 105                      'name' => new external_value(PARAM_TEXT, 'The full name of the grade item', VALUE_OPTIONAL)
 106                  ])
 107              ),
 108              'warnings' => new external_warnings(),
 109          ]);
 110      }
 111  }