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 marking guide for the grading panel.
  19   *
  20   * @package    gradingform_guide
  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 gradingform_guide\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 core_user;
  36  use external_api;
  37  use external_function_parameters;
  38  use external_single_structure;
  39  use external_value;
  40  use moodle_exception;
  41  require_once($CFG->dirroot.'/grade/grading/form/guide/lib.php');
  42  
  43  /**
  44   * Web services relating to storing of a marking guide for the grading panel.
  45   *
  46   * @package    gradingform_guide
  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 storing 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 bool $notifyuser
 102       * @param string $formdata
 103       *
 104       * @return array
 105       * @throws \dml_exception
 106       * @throws \invalid_parameter_exception
 107       * @throws \restricted_context_exception
 108       * @throws coding_exception
 109       * @throws moodle_exception
 110       * @since Moodle 3.8
 111       */
 112      public static function execute(string $component, int $contextid, string $itemname, int $gradeduserid,
 113              bool $notifyuser, string $formdata): array {
 114          global $USER;
 115  
 116          [
 117              'component' => $component,
 118              'contextid' => $contextid,
 119              'itemname' => $itemname,
 120              'gradeduserid' => $gradeduserid,
 121              'notifyuser' => $notifyuser,
 122              'formdata' => $formdata,
 123          ] = self::validate_parameters(self::execute_parameters(), [
 124              'component' => $component,
 125              'contextid' => $contextid,
 126              'itemname' => $itemname,
 127              'gradeduserid' => $gradeduserid,
 128              'notifyuser' => $notifyuser,
 129              'formdata' => $formdata,
 130          ]);
 131  
 132          // Validate the context.
 133          $context = context::instance_by_id($contextid);
 134          self::validate_context($context);
 135  
 136          // Validate that the supplied itemname is a gradable item.
 137          if (!component_gradeitems::is_valid_itemname($component, $itemname)) {
 138              throw new coding_exception("The '{$itemname}' item is not valid for the '{$component}' component");
 139          }
 140  
 141          // Fetch the gradeitem instance.
 142          $gradeitem = gradeitem::instance($component, $context, $itemname);
 143  
 144          // Validate that this gradeitem is actually enabled.
 145          if (!$gradeitem->is_grading_enabled()) {
 146              throw new moodle_exception("Grading is not enabled for {$itemname} in this context");
 147          }
 148  
 149          // Fetch the record for the graded user.
 150          $gradeduser = core_user::get_user($gradeduserid);
 151  
 152          // Require that this user can save grades.
 153          $gradeitem->require_user_can_grade($gradeduser, $USER);
 154  
 155          if (MARKING_GUIDE !== $gradeitem->get_advanced_grading_method()) {
 156              throw new moodle_exception(
 157                  "The {$itemname} item in {$component}/{$contextid} is not configured for advanced grading with a marking guide"
 158              );
 159          }
 160  
 161          // Parse the serialised string into an object.
 162          $data = [];
 163          parse_str($formdata, $data);
 164  
 165          // Grade.
 166          $gradeitem->store_grade_from_formdata($gradeduser, $USER, (object) $data);
 167  
 168          // Notify.
 169          if ($notifyuser) {
 170              // Send notification.
 171              $gradeitem->send_student_notification($gradeduser, $USER);
 172          }
 173  
 174          return fetch::get_fetch_data($gradeitem, $gradeduser);
 175      }
 176  
 177      /**
 178       * Describes the data returned from the external function.
 179       *
 180       * @return external_single_structure
 181       * @since Moodle 3.8
 182       */
 183      public static function execute_returns(): external_single_structure {
 184          return fetch::execute_returns();
 185      }
 186  }