Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 15... Línea 15...
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
Línea 16... Línea 16...
16
 
16
 
Línea 17... Línea 17...
17
namespace core_group\external;
17
namespace core_group\external;
-
 
18
 
18
 
19
use context_course;
19
use context_course;
20
use context_module;
20
use core_external\external_api;
21
use core_external\external_api;
21
use core_external\external_description;
22
use core_external\external_description;
22
use core_external\external_function_parameters;
23
use core_external\external_function_parameters;
Línea 47... Línea 48...
47
     */
48
     */
48
    public static function execute_parameters(): external_function_parameters {
49
    public static function execute_parameters(): external_function_parameters {
49
        return new external_function_parameters (
50
        return new external_function_parameters (
50
            [
51
            [
51
                'courseid' => new external_value(PARAM_INT, 'Course Id', VALUE_REQUIRED),
52
                'courseid' => new external_value(PARAM_INT, 'Course Id', VALUE_REQUIRED),
-
 
53
                'cmid' => new external_value(PARAM_INT, 'Course module Id', VALUE_DEFAULT, 0),
52
            ]
54
            ]
53
        );
55
        );
54
    }
56
    }
Línea 55... Línea 57...
55
 
57
 
56
    /**
58
    /**
57
     * Given a course ID find the existing user groups and map some fields to the returned array of group objects.
59
     * Given a course ID find the existing user groups and map some fields to the returned array of group objects.
-
 
60
     *
-
 
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.
58
     *
64
     *
-
 
65
     * @param int $courseid
59
     * @param int $courseid
66
     * @param int|null $cmid The course module ID (optional).
60
     * @return array Groups and warnings to pass back to the calling widget.
67
     * @return array Groups and warnings to pass back to the calling widget.
61
     */
68
     */
62
    public static function execute(int $courseid): array {
69
    public static function execute(int $courseid, ?int $cmid = null): array {
Línea 63... Línea 70...
63
        global $DB, $USER, $OUTPUT;
70
        global $DB, $USER, $OUTPUT;
64
 
71
 
65
        $params = self::validate_parameters(
72
        $params = self::validate_parameters(
66
            self::execute_parameters(),
73
            self::execute_parameters(),
-
 
74
            [
67
            [
75
                'courseid' => $courseid,
68
                'courseid' => $courseid,
76
                'cmid' => $cmid,
Línea 69... Línea 77...
69
            ]
77
            ]
-
 
78
        );
-
 
79
 
-
 
80
        $warnings = [];
-
 
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);
70
        );
87
            $groupingid = $cm->groupingid;
-
 
88
            $participationonly = true;
-
 
89
        } else {
-
 
90
            $context = context_course::instance($params['courseid']);
-
 
91
            $groupmode = $course->groupmode;
71
 
92
            $groupingid = $course->defaultgroupingid;
Línea 72... Línea 93...
72
        $warnings = [];
93
            $participationonly = false;
73
        $context = context_course::instance($params['courseid']);
-
 
74
        parent::validate_context($context);
94
        }
75
 
95
        parent::validate_context($context);
76
        $mappedgroups = [];
96
 
Línea 77... Línea 97...
77
        $course = $DB->get_record('course', ['id' => $params['courseid']]);
97
        $mappedgroups = [];
78
        // Initialise the grade tracking object.
-
 
79
        if ($groupmode = $course->groupmode) {
98
        // Initialise the grade tracking object.
-
 
99
        if ($groupmode) {
80
            $aag = has_capability('moodle/site:accessallgroups', $context);
100
            $aag = has_capability('moodle/site:accessallgroups', $context);
81
 
101
 
-
 
102
            $usergroups = [];
-
 
103
            if ($groupmode == VISIBLEGROUPS || $aag) {
-
 
104
                $groupuserid = 0;
-
 
105
                // Get user's own groups and put to the top.
-
 
106
                $usergroups = groups_get_all_groups(
82
            $usergroups = [];
107
                    courseid: $course->id,
83
            $groupuserid = 0;
108
                    userid: $USER->id,
84
            if ($groupmode == VISIBLEGROUPS || $aag) {
109
                    groupingid: $groupingid,
85
                // Get user's own groups and put to the top.
110
                    participationonly: $participationonly
-
 
111
                );
-
 
112
            } else {
-
 
113
                $groupuserid = $USER->id;
-
 
114
            }
-
 
115
            $allowedgroups = groups_get_all_groups(
Línea 86... Línea 116...
86
                $usergroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid);
116
                courseid: $course->id,
87
            } else {
117
                userid: $groupuserid,
88
                $groupuserid = $USER->id;
118
                groupingid: $groupingid,
Línea 101... Línea 131...
101
            }
131
            }
Línea 102... Línea 132...
102
 
132
 
103
            $mappedgroups = array_map(function($group) use ($context, $OUTPUT) {
133
            $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.
134
                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) ??
135
                    $picture = get_group_picture_url($group, $group->courseid, true) ??
-
 
136
                        moodle_url::make_pluginfile_url($context->get_course_context()->id, 'group', 'generated', $group->id,
106
                        moodle_url::make_pluginfile_url($context->id, 'group', 'generated', $group->id, '/', 'group.svg');
137
                            '/', 'group.svg');
107
                } else { // All participants.
138
                } else { // All participants.
108
                    $picture = $OUTPUT->image_url('g/g1');
139
                    $picture = $OUTPUT->image_url('g/g1');
Línea 109... Línea 140...
109
                }
140
                }