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
/**
18
 * Group widget external functions contains the add memebrs, get non group members list.
19
 *
20
 * @package    block_dash
21
 * @copyright  2022 bdecent gmbh <https://bdecent.de>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace block_dash\local\widget\groups;
26
 
27
defined('MOODLE_INTERNAL') || die('No direct access');
28
 
29
require_once($CFG->libdir . '/externallib.php');
30
use external_api;
31
 
32
require_once($CFG->dirroot . '/user/selector/lib.php');
33
require_once($CFG->dirroot . '/group/lib.php');
34
 
35
/**
36
 * Group widget external function definitions.
37
 */
38
class external extends external_api {
39
 
40
    /**
41
     * Get the users details if not assigned to the group.
42
     *
43
     * @return \external_function_parameters
44
     */
45
    public static function get_non_members_parameters() {
46
 
47
        return new \external_function_parameters([
48
            'query' => new \external_value(PARAM_RAW,
49
                'Query string (full or partial user full name or other details)'),
50
            'groupid' => new \external_value(PARAM_INT, 'group id (0 if none)'),
51
        ]);
52
    }
53
 
54
    /**
55
     * Returns result type for get_relevant_users function.
56
     *
57
     * @return \external_description Result type
58
     */
59
    public static function get_non_members_returns() {
60
        return new \external_multiple_structure(
61
                new \external_single_structure([
62
                    'id' => new \external_value(PARAM_INT, 'User id'),
63
                    'fullname' => new \external_value(PARAM_RAW, 'Full name as text'),
64
                ]));
65
    }
66
 
67
    /**
68
     * Searches for users given a query, taking into account the current user's permissions and
69
     * possibly a course to check within.
70
     *
71
     * @param string $query Query text
72
     * @param int $groupid Course id or 0 if no restriction
73
     * @return array Defined return structure
74
     */
75
    public static function get_non_members($query, $groupid) {
76
        global $CFG, $PAGE;
77
 
78
        // Validate parameter.
79
        [
80
            'query' => $query,
81
            'groupid' => $groupid,
82
        ] = self::validate_parameters(self::get_non_members_parameters(), [
83
            'query' => $query,
84
            'groupid' => $groupid,
85
        ]);
86
 
87
        // Validate the context (search page is always system context).
88
        $systemcontext = \context_system::instance();
89
        self::validate_context($systemcontext);
90
        $group = groups_get_group($groupid);
91
        $courseid = $group->courseid;
92
        if ($group) {
93
            $potentialmembersselector = new \group_non_members_selector('addselect', [
94
                'groupid' => $groupid, 'courseid' => $courseid,
95
            ]);
96
            $users = $potentialmembersselector->find_users($query);
97
            $list = [];
98
            foreach ($users as $role => $user) {
99
                $list = array_merge($list, $user);
100
            }
101
            array_walk($list, function(&$user) use ($potentialmembersselector) {
102
                $user = ['id' => $user->id, 'fullname' => fullname($user)];
103
            });
104
 
105
            return $list;
106
        }
107
        return ['id' => '0', 'fullname' => 'No source'];
108
    }
109
 
110
    /**
111
     * Add members.
112
     *
113
     * @return void
114
     */
115
    public static function add_members_parameters() {
116
 
117
        return new \external_function_parameters(
118
            [
119
                'formdata' => new \external_value(PARAM_RAW, 'The data from the user notes'),
120
            ]
121
        );
122
    }
123
    /**
124
     * Retuns the redirect course url and created pulse id for save method.
125
     *
126
     * @return void
127
     */
128
    public static function add_members_returns() {
129
        return new \external_value(PARAM_BOOL, 'Result of members added.');
130
    }
131
 
132
    /**
133
     * Add memebers to the selected group.
134
     *
135
     * @param \stdclass $formdata
136
     * @return bool
137
     */
138
    public static function add_members($formdata) {
139
        // Validate parameter.
140
        [
141
            'formdata' => $formdata,
142
        ] = self::validate_parameters(self::add_members_parameters(), [
143
            'formdata' => $formdata,
144
        ]);
145
        parse_str($formdata, $data);
146
        $groupid = $data['groupid'];
147
        $users = $data['users'];
148
        if (!empty($users) && is_array($users)) {
149
            foreach ($users as $user) {
150
                groups_add_member($groupid, $user);
151
            }
152
            return true;
153
        }
154
        return false;
155
    }
156
 
157
    /**
158
     * Prameters definition that helps to leave from the own group.
159
     *
160
     * @return \external_function_parameters
161
     */
162
    public static function leave_group_parameters() {
163
        return new \external_function_parameters([
164
            'groupid' => new \external_value(PARAM_INT, 'group id (0 if none)'),
165
        ]);
166
    }
167
 
168
    /**
169
     * Return data of leave group service.
170
     *
171
     * @return \external_value
172
     */
173
    public static function leave_group_returns() {
174
        return new \external_value(PARAM_BOOL, 'Result of members added.');
175
    }
176
 
177
    /**
178
     * Remove the user from the own group.
179
     *
180
     * @param int $groupid
181
     * @return void
182
     */
183
    public static function leave_group($groupid) {
184
        global $USER;
185
 
186
        ['groupid' => $groupid] = self::validate_parameters(self::leave_group_parameters(), [
187
            'groupid' => $groupid,
188
        ]);
189
 
190
        if ($groupid && isloggedin()) {
191
            return groups_remove_member($groupid, $USER->id);
192
        }
193
 
194
        return false;
195
    }
196
 
197
    /**
198
     * Prameters definition that helps to create groups in the selected course.
199
     *
200
     * @return \external_function_parameters
201
     */
202
    public static function create_group_parameters() {
203
        return new \external_function_parameters([
204
            'formdata' => new \external_value(PARAM_RAW, 'The data from the user notes'),
205
        ]);
206
    }
207
 
208
    /**
209
     * Returns the status of group creation in course.
210
     *
211
     * @return void
212
     */
213
    public static function create_group_returns() {
214
        return new \external_value(PARAM_BOOL, 'Result of members added.');
215
    }
216
 
217
    /**
218
     * Create the group in the selected course.
219
     *
220
     * @param stdclass $formdata
221
     * @return void
222
     */
223
    public static function create_group($formdata) {
224
        global $USER;
225
 
226
        [
227
            'formdata' => $formdata,
228
        ] = self::validate_parameters(self::add_members_parameters(), [
229
            'formdata' => $formdata,
230
        ]);
231
 
232
        // TODO: TEST Require capability.
233
        if ($formdata) {
234
            parse_str($formdata, $data);
235
            $data = (object) $data;
236
            if ($groupid = groups_create_group($data)) {
237
                groups_add_member($groupid, $USER->id);
238
                return true;
239
            }
240
        }
241
        return false;
242
    }
243
}