Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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 coding_exception;
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 core_user_external;
28
use invalid_parameter_exception;
29
use moodle_exception;
30
use user_picture;
31
 
32
defined('MOODLE_INTERNAL') || die;
33
 
34
require_once($CFG->dirroot . '/grade/lib.php');
35
require_once($CFG->dirroot . '/user/externallib.php');
36
 
37
/**
38
 * Get the gradable users in a course.
39
 *
40
 * @package    core_grades
41
 * @copyright  2023 Ilya Tregubov <ilya.a.tregubov@gmail.com>
42
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43
 */
44
class get_gradable_users extends external_api {
45
 
46
    /**
47
     * Returns description of method parameters.
48
     *
49
     * @return external_function_parameters
50
     */
51
    public static function execute_parameters(): external_function_parameters {
52
        return new external_function_parameters (
53
            [
54
                'courseid' => new external_value(PARAM_INT, 'Course Id', VALUE_REQUIRED),
55
                'groupid' => new external_value(PARAM_INT, 'Group Id', VALUE_DEFAULT, 0),
56
                'onlyactive' => new external_value(PARAM_BOOL, 'Only active enrolment', VALUE_DEFAULT, false),
57
            ]
58
        );
59
    }
60
 
61
    /**
62
     * Given a course ID find the gradable users within a group.
63
     *
64
     * @param int $courseid Course ID
65
     * @param int|null $groupid Group ID
66
     * @param bool $onlyactive Whether we should only return active enrolments.
67
     * @return array Users and warnings.
68
     * @throws coding_exception
69
     * @throws invalid_parameter_exception
70
     * @throws moodle_exception
71
     * @throws restricted_context_exception
72
     */
73
    public static function execute(int $courseid, ?int $groupid = 0, bool $onlyactive = false): array {
74
        global $DB, $PAGE;
75
 
76
        $params = self::validate_parameters(
77
            self::execute_parameters(),
78
            [
79
                'courseid' => $courseid,
80
                'groupid' => $groupid,
81
                'onlyactive' => $onlyactive,
82
            ]
83
        );
84
 
85
        $warnings = [];
86
        $coursecontext = \context_course::instance($params['courseid']);
87
        parent::validate_context($coursecontext);
88
 
89
        require_capability('moodle/course:viewparticipants', $coursecontext);
90
 
91
        $course = $DB->get_record('course', ['id' => $params['courseid']]);
92
        $onlyactive = $onlyactive || !has_capability('moodle/course:viewsuspendedusers', $coursecontext);
93
 
94
        $users = get_gradable_users($course->id, $params['groupid'], $onlyactive);
95
        $users = array_map(function ($user) use ($PAGE) {
96
            $user->fullname = fullname($user);
97
            $userpicture = new user_picture($user);
98
            $userpicture->size = 1;
99
            $user->profileimageurlsmall = $userpicture->get_url($PAGE)->out(false);
100
            $user->profileimageurl = $userpicture->get_url($PAGE)->out(false);
101
            return $user;
102
        }, $users);
103
        sort($users);
104
 
105
        return [
106
            'users' => $users,
107
            'warnings' => $warnings,
108
        ];
109
    }
110
 
111
    /**
112
     * Returns description of method result value.
113
     *
114
     * @return external_single_structure
115
     */
116
    public static function execute_returns(): external_single_structure {
117
        return new external_single_structure([
118
            'users' => new external_multiple_structure(core_user_external::user_description()),
119
            'warnings' => new external_warnings(),
120
        ]);
121
    }
122
}