Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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  /**
  18   * Web service functions relating to point grades and grading.
  19   *
  20   * @package    core_grades
  21   * @copyright  2019 Andrew Nicols <andrew@nicols.co.uk>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  declare(strict_types = 1);
  26  
  27  namespace core_grades\grades\grader\gradingpanel\point\external;
  28  
  29  use coding_exception;
  30  use context;
  31  use core_user;
  32  use core_grades\component_gradeitem as gradeitem;
  33  use core_grades\component_gradeitems;
  34  use external_api;
  35  use external_function_parameters;
  36  use external_multiple_structure;
  37  use external_single_structure;
  38  use external_value;
  39  use external_warnings;
  40  use moodle_exception;
  41  use required_capability_exception;
  42  use stdClass;
  43  
  44  /**
  45   * External grading panel point API
  46   *
  47   * @package    core_grades
  48   * @copyright  2019 Andrew Nicols <andrew@nicols.co.uk>
  49   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  50   */
  51  class fetch extends external_api {
  52  
  53      /**
  54       * Describes the parameters for fetching the grading panel for a simple grade.
  55       *
  56       * @return external_function_parameters
  57       * @since Moodle 3.8
  58       */
  59      public static function execute_parameters(): external_function_parameters {
  60          return new external_function_parameters ([
  61              'component' => new external_value(
  62                  PARAM_ALPHANUMEXT,
  63                  'The name of the component',
  64                  VALUE_REQUIRED
  65              ),
  66              'contextid' => new external_value(
  67                  PARAM_INT,
  68                  'The ID of the context being graded',
  69                  VALUE_REQUIRED
  70              ),
  71              'itemname' => new external_value(
  72                  PARAM_ALPHANUM,
  73                  'The grade item itemname being graded',
  74                  VALUE_REQUIRED
  75              ),
  76              'gradeduserid' => new external_value(
  77                  PARAM_INT,
  78                  'The ID of the user show',
  79                  VALUE_REQUIRED
  80              ),
  81          ]);
  82      }
  83  
  84      /**
  85       * Fetch the data required to build a grading panel for a simple grade.
  86       *
  87       * @param string $component
  88       * @param int $contextid
  89       * @param string $itemname
  90       * @param int $gradeduserid
  91       * @return array
  92       * @throws \dml_exception
  93       * @throws \invalid_parameter_exception
  94       * @throws \restricted_context_exception
  95       * @throws coding_exception
  96       * @throws moodle_exception
  97       * @since Moodle 3.8
  98       */
  99      public static function execute(string $component, int $contextid, string $itemname, int $gradeduserid): array {
 100          global $USER, $CFG;
 101          require_once("{$CFG->libdir}/gradelib.php");
 102          [
 103              'component' => $component,
 104              'contextid' => $contextid,
 105              'itemname' => $itemname,
 106              'gradeduserid' => $gradeduserid,
 107          ] = self::validate_parameters(self::execute_parameters(), [
 108              'component' => $component,
 109              'contextid' => $contextid,
 110              'itemname' => $itemname,
 111              'gradeduserid' => $gradeduserid,
 112          ]);
 113  
 114          // Validate the context.
 115          $context = context::instance_by_id($contextid);
 116          self::validate_context($context);
 117  
 118          // Validate that the supplied itemname is a gradable item.
 119          if (!component_gradeitems::is_valid_itemname($component, $itemname)) {
 120              throw new coding_exception("The '{$itemname}' item is not valid for the '{$component}' component");
 121          }
 122  
 123          // Fetch the gradeitem instance.
 124          $gradeitem = gradeitem::instance($component, $context, $itemname);
 125  
 126          if (!$gradeitem->is_using_direct_grading()) {
 127              throw new moodle_exception("The {$itemname} item in {$component}/{$contextid} is not configured for direct grading");
 128          }
 129  
 130          // Fetch the actual data.
 131          $gradeduser = \core_user::get_user($gradeduserid, '*', MUST_EXIST);
 132  
 133          // One can access its own grades. Others just if they're graders.
 134          if ($gradeduserid != $USER->id) {
 135              $gradeitem->require_user_can_grade($gradeduser, $USER);
 136          }
 137  
 138          $hasgrade = $gradeitem->user_has_grade($gradeduser);
 139          $grade = $gradeitem->get_grade_for_user($gradeduser, $USER);
 140  
 141          // Set up some items we need to return on other interfaces.
 142          $gradegrade = \grade_grade::fetch(['itemid' => $gradeitem->get_grade_item()->id, 'userid' => $gradeduser->id]);
 143          $gradername = $gradegrade ? fullname(\core_user::get_user($gradegrade->usermodified)) : null;
 144          $maxgrade = (int) $gradeitem->get_grade_item()->grademax;
 145  
 146          return self::get_fetch_data($grade, $hasgrade, $maxgrade, $gradername);
 147      }
 148  
 149      /**
 150       * Get the data to be fetched.
 151       *
 152       * @param stdClass $grade
 153       * @param bool $hasgrade
 154       * @param int $maxgrade
 155       * @param string|null $gradername
 156       * @return array
 157       */
 158      public static function get_fetch_data(stdClass $grade, bool $hasgrade, int $maxgrade, ?string $gradername): array {
 159          return [
 160              'templatename' => 'core_grades/grades/grader/gradingpanel/point',
 161              'hasgrade' => $hasgrade,
 162              'grade' => [
 163                  'grade' => $grade->grade,
 164                  'usergrade' => $grade->grade,
 165                  'maxgrade' => $maxgrade,
 166                  'gradedby' => $gradername,
 167                  'timecreated' => $grade->timecreated,
 168                  'timemodified' => $grade->timemodified,
 169              ],
 170              'warnings' => [],
 171          ];
 172      }
 173  
 174      /**
 175       * Describes the data returned from the external function.
 176       *
 177       * @return external_single_structure
 178       * @since Moodle 3.8
 179       */
 180      public static function execute_returns(): external_single_structure {
 181          return new external_single_structure([
 182              'templatename' => new external_value(PARAM_SAFEPATH, 'The template to use when rendering this data'),
 183              'hasgrade' => new external_value(PARAM_BOOL, 'Does the user have a grade?'),
 184              'grade' => new external_single_structure([
 185                  'grade' => new external_value(PARAM_FLOAT, 'The numeric grade'),
 186                  'usergrade' => new external_value(PARAM_RAW, 'Current user grade'),
 187                  'maxgrade' => new external_value(PARAM_RAW, 'Max possible grade'),
 188                  'gradedby' => new external_value(PARAM_RAW, 'The assumed grader of this grading instance'),
 189                  'timecreated' => new external_value(PARAM_INT, 'The time that the grade was created'),
 190                  'timemodified' => new external_value(PARAM_INT, 'The time that the grade was last updated'),
 191              ]),
 192              'warnings' => new external_warnings(),
 193          ]);
 194      }
 195  }