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 311 and 401] [Versions 39 and 401] [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  /**
  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_formatted_grade_for_user($gradeduser, $USER);
 140          $isgrading = $gradeitem->user_can_grade($gradeduser, $USER);
 141  
 142          // Set up some items we need to return on other interfaces.
 143          $gradegrade = \grade_grade::fetch(['itemid' => $gradeitem->get_grade_item()->id, 'userid' => $gradeduser->id]);
 144          $gradername = $gradegrade ? fullname(\core_user::get_user($gradegrade->usermodified)) : null;
 145  
 146          return self::get_fetch_data($grade, $hasgrade, $gradeitem, $gradername, $isgrading);
 147      }
 148  
 149      /**
 150       * Get the data to be fetched.
 151       *
 152       * @param stdClass $grade
 153       * @param bool $hasgrade
 154       * @param gradeitem $gradeitem
 155       * @param string|null $gradername
 156       * @param bool $isgrading
 157       * @return array
 158       */
 159      public static function get_fetch_data(
 160          stdClass $grade,
 161          bool $hasgrade,
 162          gradeitem $gradeitem,
 163          ?string $gradername,
 164          bool $isgrading = false
 165      ): array {
 166          $templatename = 'core_grades/grades/grader/gradingpanel/point';
 167  
 168          // We do not want to display anything if we are showing the grade as a letter. For example the 'Grade' might
 169          // read 'B-'. We do not want to show the user the actual point they were given. See MDL-71439.
 170          if (($gradeitem->get_grade_item()->get_displaytype() == GRADE_DISPLAY_TYPE_LETTER) && !$isgrading) {
 171              $templatename = 'core_grades/grades/grader/gradingpanel/point_blank';
 172          }
 173  
 174          return [
 175              'templatename' => $templatename,
 176              'hasgrade' => $hasgrade,
 177              'grade' => [
 178                  'grade' => $grade->grade,
 179                  'usergrade' => $grade->usergrade,
 180                  'maxgrade' => (int) $grade->maxgrade,
 181                  'gradedby' => $gradername,
 182                  'timecreated' => $grade->timecreated,
 183                  'timemodified' => $grade->timemodified,
 184              ],
 185              'warnings' => [],
 186          ];
 187      }
 188  
 189      /**
 190       * Describes the data returned from the external function.
 191       *
 192       * @return external_single_structure
 193       * @since Moodle 3.8
 194       */
 195      public static function execute_returns(): external_single_structure {
 196          return new external_single_structure([
 197              'templatename' => new external_value(PARAM_SAFEPATH, 'The template to use when rendering this data'),
 198              'hasgrade' => new external_value(PARAM_BOOL, 'Does the user have a grade?'),
 199              'grade' => new external_single_structure([
 200                  'grade' => new external_value(PARAM_FLOAT, 'The numeric grade'),
 201                  'usergrade' => new external_value(PARAM_RAW, 'Current user grade'),
 202                  'maxgrade' => new external_value(PARAM_RAW, 'Max possible grade'),
 203                  'gradedby' => new external_value(PARAM_RAW, 'The assumed grader of this grading instance'),
 204                  'timecreated' => new external_value(PARAM_INT, 'The time that the grade was created'),
 205                  'timemodified' => new external_value(PARAM_INT, 'The time that the grade was last updated'),
 206              ]),
 207              'warnings' => new external_warnings(),
 208          ]);
 209      }
 210  }