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 core_group\external;
18
 
19
use context_course;
1441 ariadna 20
use context_module;
1 efrain 21
use core_external\external_api;
22
use core_external\external_description;
23
use core_external\external_function_parameters;
24
use core_external\external_multiple_structure;
25
use core_external\external_single_structure;
26
use core_external\external_value;
27
use core_external\external_warnings;
28
use core_grades\external\coding_exception;
29
use core_grades\external\invalid_parameter_exception;
30
use core_grades\external\moodle_exception;
31
use core_grades\external\restricted_context_exception;
32
use moodle_url;
33
 
34
/**
35
 * External group name and image API implementation
36
 *
37
 * @package    core_group
38
 * @copyright  2022 Mathew May <mathew.solutions>
39
 * @category   external
40
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41
 */
42
class get_groups_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),
1441 ariadna 53
                'cmid' => new external_value(PARAM_INT, 'Course module Id', VALUE_DEFAULT, 0),
1 efrain 54
            ]
55
        );
56
    }
57
 
58
    /**
59
     * Given a course ID find the existing user groups and map some fields to the returned array of group objects.
60
     *
1441 ariadna 61
     * If a course module ID is provided, this function will return only the available groups within the given course
62
     * module, adhering to the set group mode for that context. All validation checks will be performed within this
63
     * specific context.
64
     *
1 efrain 65
     * @param int $courseid
1441 ariadna 66
     * @param int|null $cmid The course module ID (optional).
1 efrain 67
     * @return array Groups and warnings to pass back to the calling widget.
68
     */
1441 ariadna 69
    public static function execute(int $courseid, ?int $cmid = null): array {
1 efrain 70
        global $DB, $USER, $OUTPUT;
71
 
72
        $params = self::validate_parameters(
73
            self::execute_parameters(),
74
            [
75
                'courseid' => $courseid,
1441 ariadna 76
                'cmid' => $cmid,
1 efrain 77
            ]
78
        );
79
 
80
        $warnings = [];
1441 ariadna 81
        $course = $DB->get_record('course', ['id' => $params['courseid']]);
82
 
83
        if ($params['cmid']) {
84
            $context = context_module::instance($params['cmid']);
85
            $cm = get_coursemodule_from_id('', $params['cmid']);
86
            $groupmode = groups_get_activity_groupmode($cm, $course);
87
            $groupingid = $cm->groupingid;
88
            $participationonly = true;
89
        } else {
90
            $context = context_course::instance($params['courseid']);
91
            $groupmode = $course->groupmode;
92
            $groupingid = $course->defaultgroupingid;
93
            $participationonly = false;
94
        }
1 efrain 95
        parent::validate_context($context);
96
 
97
        $mappedgroups = [];
98
        // Initialise the grade tracking object.
1441 ariadna 99
        if ($groupmode) {
1 efrain 100
            $aag = has_capability('moodle/site:accessallgroups', $context);
101
 
102
            $usergroups = [];
103
            if ($groupmode == VISIBLEGROUPS || $aag) {
1441 ariadna 104
                $groupuserid = 0;
1 efrain 105
                // Get user's own groups and put to the top.
1441 ariadna 106
                $usergroups = groups_get_all_groups(
107
                    courseid: $course->id,
108
                    userid: $USER->id,
109
                    groupingid: $groupingid,
110
                    participationonly: $participationonly
111
                );
1 efrain 112
            } else {
113
                $groupuserid = $USER->id;
114
            }
1441 ariadna 115
            $allowedgroups = groups_get_all_groups(
116
                courseid: $course->id,
117
                userid: $groupuserid,
118
                groupingid: $groupingid,
119
                participationonly: $participationonly
120
            );
1 efrain 121
 
122
            $allgroups = array_merge($allowedgroups, $usergroups);
123
            // Filter out any duplicate groups.
124
            $groupsmenu = array_intersect_key($allgroups, array_unique(array_column($allgroups, 'name')));
125
 
126
            if (!$allowedgroups || $groupmode == VISIBLEGROUPS || $aag) {
127
                array_unshift($groupsmenu, (object) [
128
                    'id' => 0,
129
                    'name' => get_string('allparticipants'),
130
                ]);
131
            }
132
 
133
            $mappedgroups = array_map(function($group) use ($context, $OUTPUT) {
134
                if ($group->id) { // Particular group. Get the group picture if it exists, otherwise return a generic image.
135
                    $picture = get_group_picture_url($group, $group->courseid, true) ??
1441 ariadna 136
                        moodle_url::make_pluginfile_url($context->get_course_context()->id, 'group', 'generated', $group->id,
137
                            '/', 'group.svg');
1 efrain 138
                } else { // All participants.
139
                    $picture = $OUTPUT->image_url('g/g1');
140
                }
141
 
142
                return (object) [
143
                    'id' => $group->id,
144
                    'name' => format_string($group->name, true, ['context' => $context]),
145
                    'groupimageurl' => $picture->out(false),
146
                ];
147
            }, $groupsmenu);
148
        }
149
 
150
        return [
151
            'groups' => $mappedgroups,
152
            'warnings' => $warnings,
153
        ];
154
    }
155
 
156
    /**
157
     * Returns description of what the group search for the widget should return.
158
     *
159
     * @return external_single_structure
160
     */
161
    public static function execute_returns(): external_single_structure {
162
        return new external_single_structure([
163
            'groups' => new external_multiple_structure(self::group_description()),
164
            'warnings' => new external_warnings(),
165
        ]);
166
    }
167
 
168
    /**
169
     * Create group return value description.
170
     *
171
     * @return external_description
172
     */
173
    public static function group_description(): external_description {
174
        $groupfields = [
175
            'id' => new external_value(PARAM_ALPHANUM, 'An ID for the group', VALUE_REQUIRED),
176
            'name' => new external_value(PARAM_TEXT, 'The full name of the group', VALUE_REQUIRED),
177
            'groupimageurl' => new external_value(PARAM_URL, 'Group image URL', VALUE_OPTIONAL),
178
        ];
179
        return new external_single_structure($groupfields);
180
    }
181
}