Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 402 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  namespace core_grades\external;
  18  
  19  use core_external\external_api;
  20  use core_external\external_function_parameters;
  21  use core_external\external_single_structure;
  22  use core_external\external_value;
  23  use invalid_parameter_exception;
  24  
  25  defined('MOODLE_INTERNAL') || die;
  26  
  27  require_once($CFG->dirroot.'/grade/lib.php');
  28  
  29  /**
  30   * Web service to fetch students feedback for a grade item.
  31   *
  32   * @package    core_grades
  33   * @copyright  2023 Kevin Percy <kevin.percy@moodle.com>
  34   * @category   external
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class get_feedback extends external_api {
  38  
  39      /**
  40       * Returns description of method parameters.
  41       *
  42       * @return external_function_parameters
  43       */
  44      public static function execute_parameters(): external_function_parameters {
  45          return new external_function_parameters (
  46              [
  47                  'courseid' => new external_value(PARAM_INT, 'Course ID', VALUE_REQUIRED),
  48                  'userid' => new external_value(PARAM_INT, 'User ID', VALUE_REQUIRED),
  49                  'itemid' => new external_value(PARAM_INT, 'Grade Item ID', VALUE_REQUIRED)
  50              ]
  51          );
  52      }
  53  
  54      /**
  55       * Given a user ID and grade item ID, return feedback and user details.
  56       *
  57       * @param int $courseid The course ID.
  58       * @param int $userid
  59       * @param int $itemid
  60       * @return array Feedback and user details
  61       */
  62      public static function execute(int $courseid, int $userid, int $itemid): array {
  63          global $OUTPUT, $CFG;
  64  
  65          $params = self::validate_parameters(
  66              self::execute_parameters(),
  67              [
  68                  'courseid' => $courseid,
  69                  'userid' => $userid,
  70                  'itemid' => $itemid
  71              ]
  72          );
  73  
  74          $context = \context_course::instance($courseid);
  75          parent::validate_context($context);
  76  
  77          require_capability('gradereport/grader:view', $context);
  78  
  79          $gtree = new \grade_tree($params['courseid'], false, false, null, !$CFG->enableoutcomes);
  80          $gradeitem = $gtree->get_item($params['itemid']);
  81  
  82          // If Item ID is not part of Course ID, $gradeitem will be set to false.
  83          if ($gradeitem === false) {
  84              throw new invalid_parameter_exception('Course ID and item ID mismatch');
  85          }
  86  
  87          $grade = $gradeitem->get_grade($params['userid'], false);
  88          $user = \core_user::get_user($params['userid']);
  89          $extrafields = \core_user\fields::get_identity_fields($context);
  90  
  91          return [
  92              'feedbacktext' => $grade->feedback,
  93              'title' => $gradeitem->get_name(true),
  94              'fullname' => fullname($user),
  95              'picture' => $OUTPUT->user_picture($user, ['size' => 50, 'link' => false]),
  96              'additionalfield' => empty($extrafields) ? '' : $user->{$extrafields[0]},
  97          ];
  98      }
  99  
 100      /**
 101       * Describes the return structure.
 102       *
 103       * @return external_single_structure
 104       */
 105      public static function execute_returns(): external_single_structure {
 106          return new external_single_structure([
 107              'feedbacktext' => new external_value(PARAM_RAW, 'The full feedback text'),
 108              'title' => new external_value(PARAM_TEXT, 'Title of the grade item that the feedback is for'),
 109              'fullname' => new external_value(PARAM_TEXT, 'Students name'),
 110              'picture' => new external_value(PARAM_RAW, 'Students picture'),
 111              'additionalfield' => new external_value(PARAM_RAW, 'Additional field for the user (email or ID number, for example)'),
 112          ]);
 113      }
 114  }