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.
   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 gradereport_grader\external;
  18  
  19  use context_course;
  20  use core_user_external;
  21  use core_external\external_api;
  22  use core_external\external_function_parameters;
  23  use core_external\external_multiple_structure;
  24  use core_external\external_single_structure;
  25  use core_external\external_value;
  26  use core_external\external_warnings;
  27  use grade_report_grader;
  28  use user_picture;
  29  
  30  defined('MOODLE_INTERNAL') || die;
  31  
  32  require_once($CFG->dirroot.'/course/externallib.php');
  33  require_once($CFG->dirroot .'/user/externallib.php');
  34  require_once($CFG->dirroot.'/grade/lib.php');
  35  require_once($CFG->dirroot.'/grade/report/grader/lib.php');
  36  
  37  /**
  38   * External grade report grader API
  39   *
  40   * @package    gradereport_grader
  41   * @copyright  2022 Mathew May <mathew.solutions>
  42   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   */
  44  class get_users_in_report extends external_api {
  45      /**
  46       * Describes the parameters for get_users_in_report
  47       *
  48       * @return external_function_parameters
  49       */
  50      public static function execute_parameters(): external_function_parameters {
  51          return new external_function_parameters (
  52              [
  53                  'courseid' => new external_value(PARAM_INT, 'Course ID', VALUE_REQUIRED)
  54              ]
  55          );
  56      }
  57  
  58      /**
  59       * Given a course ID find Fetch the grader report and add some fields to the returned users.
  60       *
  61       * @param int $courseid Course ID to fetch the grader report for.
  62       * @return array Users and warnings to pass back to the calling widget.
  63       */
  64      public static function execute(int $courseid): array {
  65          global $PAGE;
  66  
  67          self::validate_parameters(
  68              self::execute_parameters(),
  69              [
  70                  'courseid' => $courseid,
  71              ]
  72          );
  73  
  74          $warnings = [];
  75          $context = context_course::instance($courseid);
  76          self::validate_context($context);
  77  
  78          require_capability('gradereport/grader:view', $context);
  79  
  80          // Return tracking object.
  81          $gpr = new \grade_plugin_return(
  82              [
  83                  'type' => 'report',
  84                  'plugin' => 'grader',
  85                  'courseid' => $courseid
  86              ]
  87          );
  88          $report = new grade_report_grader($courseid, $gpr, $context);
  89  
  90          // For the returned users, Add a couple of extra fields that we need for the search module.
  91          $users = array_map(function ($user) use ($PAGE) {
  92              $user->fullname = fullname($user);
  93              $userpicture = new user_picture($user);
  94              $userpicture->size = 1;
  95              $user->profileimageurl = $userpicture->get_url($PAGE)->out(false);
  96              $userpicture->size = 0; // Size f2.
  97              $user->profileimageurlsmall = $userpicture->get_url($PAGE)->out(false);
  98              return $user;
  99          }, $report->load_users(true));
 100          sort($users);
 101  
 102          return [
 103              'users' => $users,
 104              'warnings' => $warnings,
 105          ];
 106      }
 107  
 108      /**
 109       * Returns description of what the users & warnings should return.
 110       *
 111       * @return external_single_structure
 112       */
 113      public static function execute_returns(): external_single_structure {
 114          return new external_single_structure([
 115              'users' => new external_multiple_structure(core_user_external::user_description()),
 116              'warnings' => new external_warnings(),
 117          ]);
 118      }
 119  }