Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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 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
 
11 efrain 90
        $userfieldsapi = \core_user\fields::for_identity($context, false)->with_userpic();
91
        $extrauserfields = $userfieldsapi->get_required_fields([\core_user\fields::PURPOSE_IDENTITY]);
92
 
1 efrain 93
        // For the returned users, Add a couple of extra fields that we need for the search module.
11 efrain 94
        $users = array_map(function ($user) use ($PAGE, $extrauserfields) {
95
            $userforselector = new \stdClass();
96
            $userforselector->id = $user->id;
97
            $userforselector->fullname = fullname($user);
98
            foreach (\core_user\fields::get_name_fields() as $field) {
99
                $userforselector->$field = $user->$field ?? null;
100
            }
1 efrain 101
            $userpicture = new user_picture($user);
102
            $userpicture->size = 1;
11 efrain 103
            $userforselector->profileimageurl = $userpicture->get_url($PAGE)->out(false);
1 efrain 104
            $userpicture->size = 0; // Size f2.
11 efrain 105
            $userforselector->profileimageurlsmall = $userpicture->get_url($PAGE)->out(false);
106
            foreach ($extrauserfields as $field) {
107
                $userforselector->$field = $user->$field ?? null;
108
            }
109
            return $userforselector;
1 efrain 110
        }, $report->load_users(true));
111
        sort($users);
112
 
113
        return [
114
            'users' => $users,
115
            'warnings' => $warnings,
116
        ];
117
    }
118
 
119
    /**
120
     * Returns description of what the users & warnings should return.
121
     *
122
     * @return external_single_structure
123
     */
124
    public static function execute_returns(): external_single_structure {
125
        return new external_single_structure([
126
            'users' => new external_multiple_structure(core_user_external::user_description()),
127
            'warnings' => new external_warnings(),
128
        ]);
129
    }
130
}