Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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 310 and 402] [Versions 310 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 services relating to fetching of a rubric for the grading panel.
  19   *
  20   * @package    gradingform_rubric
  21   * @copyright  2019 Mathew May <mathew.solutions>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  declare(strict_types = 1);
  26  
  27  namespace gradingform_rubric\grades\grader\gradingpanel\external;
  28  
  29  global $CFG;
  30  
  31  use coding_exception;
  32  use context;
  33  use core_grades\component_gradeitem as gradeitem;
  34  use core_grades\component_gradeitems;
  35  use external_api;
  36  use external_function_parameters;
  37  use external_single_structure;
  38  use external_value;
  39  use moodle_exception;
  40  require_once($CFG->dirroot.'/grade/grading/form/rubric/lib.php');
  41  
  42  /**
  43   * Web services relating to storing of a rubric for the grading panel.
  44   *
  45   * @package    gradingform_rubric
  46   * @copyright  2019 Mathew May <mathew.solutions>
  47   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  48   */
  49  class store extends external_api {
  50  
  51      /**
  52       * Describes the parameters for storing the grading panel for a simple grade.
  53       *
  54       * @return external_function_parameters
  55       * @since Moodle 3.8
  56       */
  57      public static function execute_parameters(): external_function_parameters {
  58          return new external_function_parameters ([
  59              'component' => new external_value(
  60                  PARAM_ALPHANUMEXT,
  61                  'The name of the component',
  62                  VALUE_REQUIRED
  63              ),
  64              'contextid' => new external_value(
  65                  PARAM_INT,
  66                  'The ID of the context being graded',
  67                  VALUE_REQUIRED
  68              ),
  69              'itemname' => new external_value(
  70                  PARAM_ALPHANUM,
  71                  'The grade item itemname being graded',
  72                  VALUE_REQUIRED
  73              ),
  74              'gradeduserid' => new external_value(
  75                  PARAM_INT,
  76                  'The ID of the user show',
  77                  VALUE_REQUIRED
  78              ),
  79              'notifyuser' => new external_value(
  80                  PARAM_BOOL,
  81                  'Wheteher to notify the user or not',
  82                  VALUE_DEFAULT,
  83                  false
  84              ),
  85              'formdata' => new external_value(
  86                  PARAM_RAW,
  87                  'The serialised form data representing the grade',
  88                  VALUE_REQUIRED
  89              ),
  90          ]);
  91      }
  92  
  93      /**
  94       * Fetch the data required to build a grading panel for a simple grade.
  95       *
  96       * @param string $component
  97       * @param int $contextid
  98       * @param string $itemname
  99       * @param int $gradeduserid
 100       * @param string $formdata
 101       * @param bool $notifyuser
 102       * @return array
 103       * @throws coding_exception
 104       * @throws moodle_exception
 105       * @since Moodle 3.8
 106       */
 107      public static function execute(string $component, int $contextid, string $itemname, int $gradeduserid, bool $notifyuser,
 108              string $formdata): array {
 109          global $USER;
 110  
 111          [
 112              'component' => $component,
 113              'contextid' => $contextid,
 114              'itemname' => $itemname,
 115              'gradeduserid' => $gradeduserid,
 116              'notifyuser' => $notifyuser,
 117              'formdata' => $formdata,
 118          ] = self::validate_parameters(self::execute_parameters(), [
 119              'component' => $component,
 120              'contextid' => $contextid,
 121              'itemname' => $itemname,
 122              'gradeduserid' => $gradeduserid,
 123              'notifyuser' => $notifyuser,
 124              'formdata' => $formdata,
 125          ]);
 126  
 127          // Validate the context.
 128          $context = context::instance_by_id($contextid);
 129          self::validate_context($context);
 130  
 131          // Validate that the supplied itemname is a gradable item.
 132          if (!component_gradeitems::is_valid_itemname($component, $itemname)) {
 133              throw new coding_exception("The '{$itemname}' item is not valid for the '{$component}' component");
 134          }
 135  
 136          // Fetch the gradeitem instance.
 137          $gradeitem = gradeitem::instance($component, $context, $itemname);
 138  
 139          // Validate that this gradeitem is actually enabled.
 140          if (!$gradeitem->is_grading_enabled()) {
 141              throw new moodle_exception("Grading is not enabled for {$itemname} in this context");
 142          }
 143  
 144          // Fetch the record for the graded user.
 145          $gradeduser = \core_user::get_user($gradeduserid);
 146  
 147          // Require that this user can save grades.
 148          $gradeitem->require_user_can_grade($gradeduser, $USER);
 149  
 150          if (RUBRIC !== $gradeitem->get_advanced_grading_method()) {
 151              throw new moodle_exception(
 152                  "The {$itemname} item in {$component}/{$contextid} is not configured for advanced grading with a rubric"
 153              );
 154          }
 155  
 156          // Parse the serialised string into an object.
 157          $data = [];
 158          parse_str($formdata, $data);
 159  
 160          // Grade.
 161          $gradeitem->store_grade_from_formdata($gradeduser, $USER, (object) $data);
 162  
 163          // Notify.
 164          if ($notifyuser) {
 165              // Send notification.
 166              $gradeitem->send_student_notification($gradeduser, $USER);
 167          }
 168  
 169          // Fetch the updated grade back out.
 170          $grade = $gradeitem->get_grade_for_user($gradeduser, $USER);
 171  
 172          return fetch::get_fetch_data($gradeitem, $gradeduser);
 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  }