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_group\external;
18
 
19
use context_course;
20
use core_external\external_api;
21
use core_external\external_description;
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 core_grades\external\coding_exception;
28
use core_grades\external\invalid_parameter_exception;
29
use core_grades\external\moodle_exception;
30
use core_grades\external\restricted_context_exception;
31
use moodle_url;
32
 
33
/**
34
 * External group name and image API implementation
35
 *
36
 * @package    core_group
37
 * @copyright  2022 Mathew May <mathew.solutions>
38
 * @category   external
39
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40
 */
41
class get_groups_for_selector extends external_api {
42
 
43
    /**
44
     * Returns description of method parameters.
45
     *
46
     * @return external_function_parameters
47
     */
48
    public static function execute_parameters(): external_function_parameters {
49
        return new external_function_parameters (
50
            [
51
                'courseid' => new external_value(PARAM_INT, 'Course Id', VALUE_REQUIRED),
52
            ]
53
        );
54
    }
55
 
56
    /**
57
     * Given a course ID find the existing user groups and map some fields to the returned array of group objects.
58
     *
59
     * @param int $courseid
60
     * @return array Groups and warnings to pass back to the calling widget.
61
     */
62
    public static function execute(int $courseid): array {
63
        global $DB, $USER, $OUTPUT;
64
 
65
        $params = self::validate_parameters(
66
            self::execute_parameters(),
67
            [
68
                'courseid' => $courseid,
69
            ]
70
        );
71
 
72
        $warnings = [];
73
        $context = context_course::instance($params['courseid']);
74
        parent::validate_context($context);
75
 
76
        $mappedgroups = [];
77
        $course = $DB->get_record('course', ['id' => $params['courseid']]);
78
        // Initialise the grade tracking object.
79
        if ($groupmode = $course->groupmode) {
80
            $aag = has_capability('moodle/site:accessallgroups', $context);
81
 
82
            $usergroups = [];
83
            $groupuserid = 0;
84
            if ($groupmode == VISIBLEGROUPS || $aag) {
85
                // Get user's own groups and put to the top.
86
                $usergroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid);
87
            } else {
88
                $groupuserid = $USER->id;
89
            }
90
            $allowedgroups = groups_get_all_groups($course->id, $groupuserid, $course->defaultgroupingid);
91
 
92
            $allgroups = array_merge($allowedgroups, $usergroups);
93
            // Filter out any duplicate groups.
94
            $groupsmenu = array_intersect_key($allgroups, array_unique(array_column($allgroups, 'name')));
95
 
96
            if (!$allowedgroups || $groupmode == VISIBLEGROUPS || $aag) {
97
                array_unshift($groupsmenu, (object) [
98
                    'id' => 0,
99
                    'name' => get_string('allparticipants'),
100
                ]);
101
            }
102
 
103
            $mappedgroups = array_map(function($group) use ($context, $OUTPUT) {
104
                if ($group->id) { // Particular group. Get the group picture if it exists, otherwise return a generic image.
105
                    $picture = get_group_picture_url($group, $group->courseid, true) ??
106
                        moodle_url::make_pluginfile_url($context->id, 'group', 'generated', $group->id, '/', 'group.svg');
107
                } else { // All participants.
108
                    $picture = $OUTPUT->image_url('g/g1');
109
                }
110
 
111
                return (object) [
112
                    'id' => $group->id,
113
                    'name' => format_string($group->name, true, ['context' => $context]),
114
                    'groupimageurl' => $picture->out(false),
115
                ];
116
            }, $groupsmenu);
117
        }
118
 
119
        return [
120
            'groups' => $mappedgroups,
121
            'warnings' => $warnings,
122
        ];
123
    }
124
 
125
    /**
126
     * Returns description of what the group search for the widget should return.
127
     *
128
     * @return external_single_structure
129
     */
130
    public static function execute_returns(): external_single_structure {
131
        return new external_single_structure([
132
            'groups' => new external_multiple_structure(self::group_description()),
133
            'warnings' => new external_warnings(),
134
        ]);
135
    }
136
 
137
    /**
138
     * Create group return value description.
139
     *
140
     * @return external_description
141
     */
142
    public static function group_description(): external_description {
143
        $groupfields = [
144
            'id' => new external_value(PARAM_ALPHANUM, 'An ID for the group', VALUE_REQUIRED),
145
            'name' => new external_value(PARAM_TEXT, 'The full name of the group', VALUE_REQUIRED),
146
            'groupimageurl' => new external_value(PARAM_URL, 'Group image URL', VALUE_OPTIONAL),
147
        ];
148
        return new external_single_structure($groupfields);
149
    }
150
}