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 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 scale 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\scale\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  
  43  /**
  44   * External grading panel scale API
  45   *
  46   * @package    core_grades
  47   * @copyright  2019 Andrew Nicols <andrew@nicols.co.uk>
  48   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  49   */
  50  class store extends external_api {
  51  
  52      /**
  53       * Describes the parameters for fetching the grading panel for a simple grade.
  54       *
  55       * @return external_function_parameters
  56       * @since Moodle 3.8
  57       */
  58      public static function execute_parameters(): external_function_parameters {
  59          return new external_function_parameters ([
  60              'component' => new external_value(
  61                  PARAM_ALPHANUMEXT,
  62                  'The name of the component',
  63                  VALUE_REQUIRED
  64              ),
  65              'contextid' => new external_value(
  66                  PARAM_INT,
  67                  'The ID of the context being graded',
  68                  VALUE_REQUIRED
  69              ),
  70              'itemname' => new external_value(
  71                  PARAM_ALPHANUM,
  72                  'The grade item itemname being graded',
  73                  VALUE_REQUIRED
  74              ),
  75              'gradeduserid' => new external_value(
  76                  PARAM_INT,
  77                  'The ID of the user show',
  78                  VALUE_REQUIRED
  79              ),
  80              'notifyuser' => new external_value(
  81                  PARAM_BOOL,
  82                  'Wheteher to notify the user or not',
  83                  VALUE_DEFAULT,
  84                  false
  85              ),
  86              'formdata' => new external_value(
  87                  PARAM_RAW,
  88                  'The serialised form data representing the grade',
  89                  VALUE_REQUIRED
  90              ),
  91          ]);
  92      }
  93  
  94      /**
  95       * Fetch the data required to build a grading panel for a simple grade.
  96       *
  97       * @param string $component
  98       * @param int $contextid
  99       * @param string $itemname
 100       * @param int $gradeduserid
 101       * @param string $formdata
 102       * @param bool $notifyuser
 103       * @return array
 104       * @throws coding_exception
 105       * @throws moodle_exception
 106       * @since Moodle 3.8
 107       */
 108      public static function execute(string $component, int $contextid, string $itemname, int $gradeduserid,
 109              bool $notifyuser, string $formdata): array {
 110          global $USER, $CFG;
 111          require_once("{$CFG->libdir}/gradelib.php");
 112          [
 113              'component' => $component,
 114              'contextid' => $contextid,
 115              'itemname' => $itemname,
 116              'gradeduserid' => $gradeduserid,
 117              'notifyuser' => $notifyuser,
 118              'formdata' => $formdata,
 119          ] = self::validate_parameters(self::execute_parameters(), [
 120              'component' => $component,
 121              'contextid' => $contextid,
 122              'itemname' => $itemname,
 123              'gradeduserid' => $gradeduserid,
 124              'notifyuser' => $notifyuser,
 125              'formdata' => $formdata,
 126          ]);
 127  
 128          // Validate the context.
 129          $context = context::instance_by_id($contextid);
 130          self::validate_context($context);
 131  
 132          // Validate that the supplied itemname is a gradable item.
 133          if (!component_gradeitems::is_valid_itemname($component, $itemname)) {
 134              throw new coding_exception("The '{$itemname}' item is not valid for the '{$component}' component");
 135          }
 136  
 137          // Fetch the gradeitem instance.
 138          $gradeitem = gradeitem::instance($component, $context, $itemname);
 139  
 140          // Validate that this gradeitem is actually enabled.
 141          if (!$gradeitem->is_grading_enabled()) {
 142              throw new moodle_exception("Grading is not enabled for {$itemname} in this context");
 143          }
 144  
 145          // Fetch the record for the graded user.
 146          $gradeduser = \core_user::get_user($gradeduserid);
 147  
 148          // Require that this user can save grades.
 149          $gradeitem->require_user_can_grade($gradeduser, $USER);
 150  
 151          if (!$gradeitem->is_using_scale()) {
 152              throw new moodle_exception("The {$itemname} item in {$component}/{$contextid} is not configured for grading with scales");
 153          }
 154  
 155          // Parse the serialised string into an object.
 156          $data = [];
 157          parse_str($formdata, $data);
 158  
 159          // Grade.
 160          $gradeitem->store_grade_from_formdata($gradeduser, $USER, (object) $data);
 161  
 162          // Notify.
 163          if ($notifyuser) {
 164              // Send notification.
 165              $gradeitem->send_student_notification($gradeduser, $USER);
 166          }
 167  
 168          $gradegrade = \grade_grade::fetch(['itemid' => $gradeitem->get_grade_item()->id, 'userid' => $gradeduser->id]);
 169          $gradername = $gradegrade ? fullname(\core_user::get_user($gradegrade->usermodified)) : null;
 170          $maxgrade = (int) $gradeitem->get_grade_item()->grademax;
 171  
 172          return fetch::get_fetch_data($gradeitem, $gradeduser, $maxgrade, $gradername);
 173      }
 174  
 175      /**
 176       * Describes the data returned from the external function.
 177       *
 178       * @return external_single_structure
 179       * @since Moodle 3.8
 180       */
 181      public static function execute_returns(): external_single_structure {
 182          return fetch::execute_returns();
 183      }
 184  }