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_user_external;
  20  use core_external\external_api;
  21  use core_external\external_function_parameters;
  22  use core_external\external_multiple_structure;
  23  use core_external\external_single_structure;
  24  use core_external\external_value;
  25  use core_external\external_warnings;
  26  use core_external\restricted_context_exception;
  27  use user_picture;
  28  
  29  defined('MOODLE_INTERNAL') || die;
  30  
  31  require_once($CFG->dirroot.'/grade/lib.php');
  32  require_once($CFG->dirroot .'/user/externallib.php');
  33  
  34  /**
  35   * Get the enrolled users within and map some fields to the returned array of user objects.
  36   *
  37   * @package    core_grades
  38   * @copyright  2022 Mihail Geshoski <mihail@moodle.com>
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   * @since      Moodle 4.2
  41   */
  42  class get_enrolled_users_for_selector extends external_api {
  43  
  44      /**
  45       * Returns description of method parameters.
  46       *
  47       * @return external_function_parameters
  48       */
  49      public static function execute_parameters(): external_function_parameters {
  50          return new external_function_parameters (
  51              [
  52                  'courseid' => new external_value(PARAM_INT, 'Course Id', VALUE_REQUIRED),
  53                  'groupid' => new external_value(PARAM_INT, 'Group Id', VALUE_DEFAULT, 0)
  54              ]
  55          );
  56      }
  57  
  58      /**
  59       * Given a course ID find the enrolled users within and map some fields to the returned array of user objects.
  60       *
  61       * @param int $courseid
  62       * @param int|null $groupid
  63       * @return array Users and warnings to pass back to the calling widget.
  64       * @throws coding_exception
  65       * @throws invalid_parameter_exception
  66       * @throws moodle_exception
  67       * @throws restricted_context_exception
  68       */
  69      public static function execute(int $courseid, ?int $groupid = 0): array {
  70          global $DB, $PAGE;
  71  
  72          $params = self::validate_parameters(
  73              self::execute_parameters(),
  74              [
  75                  'courseid' => $courseid,
  76                  'groupid' => $groupid
  77              ]
  78          );
  79  
  80          $warnings = [];
  81          $coursecontext = \context_course::instance($params['courseid']);
  82          parent::validate_context($coursecontext);
  83  
  84          require_capability('moodle/course:viewparticipants', $coursecontext);
  85  
  86          $course = $DB->get_record('course', ['id' => $params['courseid']]);
  87          // Create a graded_users_iterator because it will properly check the groups etc.
  88          $defaultgradeshowactiveenrol = !empty($CFG->grade_report_showonlyactiveenrol);
  89          $showonlyactiveenrol = get_user_preferences('grade_report_showonlyactiveenrol', $defaultgradeshowactiveenrol);
  90          $showonlyactiveenrol = $showonlyactiveenrol || !has_capability('moodle/course:viewsuspendedusers', $coursecontext);
  91  
  92          $gui = new \graded_users_iterator($course, null, $params['groupid']);
  93          $gui->require_active_enrolment($showonlyactiveenrol);
  94          $gui->init();
  95  
  96          $users = [];
  97  
  98          while ($userdata = $gui->next_user()) {
  99              $user = $userdata->user;
 100              $user->fullname = fullname($user);
 101              $userpicture = new user_picture($user);
 102              $userpicture->size = 1;
 103              $user->profileimageurl = $userpicture->get_url($PAGE)->out(false);
 104              $userpicture->size = 0; // Size f2.
 105              $user->profileimageurlsmall = $userpicture->get_url($PAGE)->out(false);
 106  
 107              $users[] = $user;
 108          }
 109          $gui->close();
 110  
 111          return [
 112              'users' => $users,
 113              'warnings' => $warnings,
 114          ];
 115      }
 116  
 117      /**
 118       * Returns description of method result value.
 119       *
 120       * @return external_single_structure
 121       */
 122      public static function execute_returns(): external_single_structure {
 123          return new external_single_structure([
 124              'users' => new external_multiple_structure(core_user_external::user_description()),
 125              'warnings' => new external_warnings(),
 126          ]);
 127      }
 128  }