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