MDL-27891 tag logs: minibump version
[moodle.git] / group / externallib.php
bloba37cf64da00e6f768c0020b6ec191569c55ba911
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * External groups API
21 * @package moodlecore
22 * @subpackage webservice
23 * @copyright 2009 Moodle Pty Ltd (http://moodle.com)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require_once("$CFG->libdir/externallib.php");
29 class moodle_group_external extends external_api {
31 /**
32 * Returns description of method parameters
33 * @return external_function_parameters
35 public static function create_groups_parameters() {
36 return new external_function_parameters(
37 array(
38 'groups' => new external_multiple_structure(
39 new external_single_structure(
40 array(
41 'courseid' => new external_value(PARAM_INT, 'id of course'),
42 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'),
43 'description' => new external_value(PARAM_RAW, 'group description text'),
44 'enrolmentkey' => new external_value(PARAM_RAW, 'group enrol secret phrase'),
46 ), 'List of group object. A group has a courseid, a name, a description and an enrolment key.'
52 /**
53 * Create groups
54 * @param array $groups array of group description arrays (with keys groupname and courseid)
55 * @return array of newly created groups
57 public static function create_groups($groups) {
58 global $CFG, $DB;
59 require_once("$CFG->dirroot/group/lib.php");
61 $params = self::validate_parameters(self::create_groups_parameters(), array('groups'=>$groups));
63 $transaction = $DB->start_delegated_transaction();
65 $groups = array();
67 foreach ($params['groups'] as $group) {
68 $group = (object)$group;
70 if (trim($group->name) == '') {
71 throw new invalid_parameter_exception('Invalid group name');
73 if ($DB->get_record('groups', array('courseid'=>$group->courseid, 'name'=>$group->name))) {
74 throw new invalid_parameter_exception('Group with the same name already exists in the course');
77 // now security checks
78 $context = get_context_instance(CONTEXT_COURSE, $group->courseid);
79 try {
80 self::validate_context($context);
81 } catch (Exception $e) {
82 $exceptionparam = new stdClass();
83 $exceptionparam->message = $e->getMessage();
84 $exceptionparam->courseid = $group->courseid;
85 throw new moodle_exception(
86 get_string('errorcoursecontextnotvalid' , 'webservice', $exceptionparam));
88 require_capability('moodle/course:managegroups', $context);
90 // finally create the group
91 $group->id = groups_create_group($group, false);
92 $groups[] = (array)$group;
95 $transaction->allow_commit();
97 return $groups;
101 * Returns description of method result value
102 * @return external_description
104 public static function create_groups_returns() {
105 return new external_multiple_structure(
106 new external_single_structure(
107 array(
108 'id' => new external_value(PARAM_INT, 'group record id'),
109 'courseid' => new external_value(PARAM_INT, 'id of course'),
110 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'),
111 'description' => new external_value(PARAM_RAW, 'group description text'),
112 'enrolmentkey' => new external_value(PARAM_RAW, 'group enrol secret phrase'),
114 ), 'List of group object. A group has an id, a courseid, a name, a description and an enrolment key.'
119 * Returns description of method parameters
120 * @return external_function_parameters
122 public static function get_groups_parameters() {
123 return new external_function_parameters(
124 array(
125 'groupids' => new external_multiple_structure(new external_value(PARAM_INT, 'Group ID')
126 ,'List of group id. A group id is an integer.'),
132 * Get groups definition specified by ids
133 * @param array $groupids arrays of group ids
134 * @return array of group objects (id, courseid, name, enrolmentkey)
136 public static function get_groups($groupids) {
137 $params = self::validate_parameters(self::get_groups_parameters(), array('groupids'=>$groupids));
139 $groups = array();
140 foreach ($params['groupids'] as $groupid) {
141 // validate params
142 $group = groups_get_group($groupid, 'id, courseid, name, description, enrolmentkey', MUST_EXIST);
144 // now security checks
145 $context = get_context_instance(CONTEXT_COURSE, $group->courseid);
146 try {
147 self::validate_context($context);
148 } catch (Exception $e) {
149 $exceptionparam = new stdClass();
150 $exceptionparam->message = $e->getMessage();
151 $exceptionparam->courseid = $group->courseid;
152 throw new moodle_exception(
153 get_string('errorcoursecontextnotvalid' , 'webservice', $exceptionparam));
155 require_capability('moodle/course:managegroups', $context);
157 $groups[] = (array)$group;
160 return $groups;
164 * Returns description of method result value
165 * @return external_description
167 public static function get_groups_returns() {
168 return new external_multiple_structure(
169 new external_single_structure(
170 array(
171 'id' => new external_value(PARAM_INT, 'group record id'),
172 'courseid' => new external_value(PARAM_INT, 'id of course'),
173 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'),
174 'description' => new external_value(PARAM_RAW, 'group description text'),
175 'enrolmentkey' => new external_value(PARAM_RAW, 'group enrol secret phrase'),
182 * Returns description of method parameters
183 * @return external_function_parameters
185 public static function get_course_groups_parameters() {
186 return new external_function_parameters(
187 array(
188 'courseid' => new external_value(PARAM_INT, 'id of course'),
194 * Get all groups in the specified course
195 * @param int $courseid id of course
196 * @return array of group objects (id, courseid, name, enrolmentkey)
198 public static function get_course_groups($courseid) {
199 $params = self::validate_parameters(self::get_course_groups_parameters(), array('courseid'=>$courseid));
201 // now security checks
202 $context = get_context_instance(CONTEXT_COURSE, $params['courseid']);
203 try {
204 self::validate_context($context);
205 } catch (Exception $e) {
206 $exceptionparam = new stdClass();
207 $exceptionparam->message = $e->getMessage();
208 $exceptionparam->courseid = $params['courseid'];
209 throw new moodle_exception(
210 get_string('errorcoursecontextnotvalid' , 'webservice', $exceptionparam));
212 require_capability('moodle/course:managegroups', $context);
214 $gs = groups_get_all_groups($params['courseid'], 0, 0, 'g.id, g.courseid, g.name, g.description, g.enrolmentkey');
216 $groups = array();
217 foreach ($gs as $group) {
218 $groups[] = (array)$group;
221 return $groups;
225 * Returns description of method result value
226 * @return external_description
228 public static function get_course_groups_returns() {
229 return new external_multiple_structure(
230 new external_single_structure(
231 array(
232 'id' => new external_value(PARAM_INT, 'group record id'),
233 'courseid' => new external_value(PARAM_INT, 'id of course'),
234 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'),
235 'description' => new external_value(PARAM_RAW, 'group description text'),
236 'enrolmentkey' => new external_value(PARAM_RAW, 'group enrol secret phrase'),
243 * Returns description of method parameters
244 * @return external_function_parameters
246 public static function delete_groups_parameters() {
247 return new external_function_parameters(
248 array(
249 'groupids' => new external_multiple_structure(new external_value(PARAM_INT, 'Group ID')),
255 * Delete groups
256 * @param array $groupids array of group ids
257 * @return void
259 public static function delete_groups($groupids) {
260 global $CFG, $DB;
261 require_once("$CFG->dirroot/group/lib.php");
263 $params = self::validate_parameters(self::delete_groups_parameters(), array('groupids'=>$groupids));
265 $transaction = $DB->start_delegated_transaction();
267 // TODO: this is problematic because the DB rollback does not handle deleting of group images!
268 foreach ($params['groupids'] as $groupid) {
269 // validate params
270 $groupid = validate_param($groupid, PARAM_INTEGER);
271 if (!$group = groups_get_group($groupid, 'id, courseid', IGNORE_MISSING)) {
272 // silently ignore attempts to delete nonexisting groups
273 continue;
276 // now security checks
277 $context = get_context_instance(CONTEXT_COURSE, $group->courseid);
278 try {
279 self::validate_context($context);
280 } catch (Exception $e) {
281 $exceptionparam = new stdClass();
282 $exceptionparam->message = $e->getMessage();
283 $exceptionparam->courseid = $group->courseid;
284 throw new moodle_exception(
285 get_string('errorcoursecontextnotvalid' , 'webservice', $exceptionparam));
287 require_capability('moodle/course:managegroups', $context);
289 groups_delete_group($group);
292 $transaction->allow_commit();
296 * Returns description of method result value
297 * @return external_description
299 public static function delete_groups_returns() {
300 return null;
305 * Returns description of method parameters
306 * @return external_function_parameters
308 public static function get_groupmembers_parameters() {
309 return new external_function_parameters(
310 array(
311 'groupids' => new external_multiple_structure(new external_value(PARAM_INT, 'Group ID')),
317 * Return all members for a group
318 * @param array $groupids array of group ids
319 * @return array with group id keys containing arrays of user ids
321 public static function get_groupmembers($groupids) {
322 $members = array();
324 $params = self::validate_parameters(self::get_groupmembers_parameters(), array('groupids'=>$groupids));
326 foreach ($params['groupids'] as $groupid) {
327 // validate params
328 $group = groups_get_group($groupid, 'id, courseid, name, enrolmentkey', MUST_EXIST);
329 // now security checks
330 $context = get_context_instance(CONTEXT_COURSE, $group->courseid);
331 try {
332 self::validate_context($context);
333 } catch (Exception $e) {
334 $exceptionparam = new stdClass();
335 $exceptionparam->message = $e->getMessage();
336 $exceptionparam->courseid = $group->courseid;
337 throw new moodle_exception(
338 get_string('errorcoursecontextnotvalid' , 'webservice', $exceptionparam));
340 require_capability('moodle/course:managegroups', $context);
342 $groupmembers = groups_get_members($group->id, 'u.id', 'lastname ASC, firstname ASC');
344 $members[] = array('groupid'=>$groupid, 'userids'=>array_keys($groupmembers));
347 return $members;
351 * Returns description of method result value
352 * @return external_description
354 public static function get_groupmembers_returns() {
355 return new external_multiple_structure(
356 new external_single_structure(
357 array(
358 'groupid' => new external_value(PARAM_INT, 'group record id'),
359 'userids' => new external_multiple_structure(new external_value(PARAM_INT, 'user id')),
367 * Returns description of method parameters
368 * @return external_function_parameters
370 public static function add_groupmembers_parameters() {
371 return new external_function_parameters(
372 array(
373 'members'=> new external_multiple_structure(
374 new external_single_structure(
375 array(
376 'groupid' => new external_value(PARAM_INT, 'group record id'),
377 'userid' => new external_value(PARAM_INT, 'user id'),
386 * Add group members
387 * @param array $members of arrays with keys userid, groupid
388 * @return void
390 public static function add_groupmembers($members) {
391 global $CFG, $DB;
392 require_once("$CFG->dirroot/group/lib.php");
394 $params = self::validate_parameters(self::add_groupmembers_parameters(), array('members'=>$members));
396 $transaction = $DB->start_delegated_transaction();
397 foreach ($params['members'] as $member) {
398 // validate params
399 $groupid = $member['groupid'];
400 $userid = $member['userid'];
402 $group = groups_get_group($groupid, 'id, courseid', MUST_EXIST);
403 $user = $DB->get_record('user', array('id'=>$userid, 'deleted'=>0, 'mnethostid'=>$CFG->mnet_localhost_id), '*', MUST_EXIST);
405 // now security checks
406 $context = get_context_instance(CONTEXT_COURSE, $group->courseid);
407 try {
408 self::validate_context($context);
409 } catch (Exception $e) {
410 $exceptionparam = new stdClass();
411 $exceptionparam->message = $e->getMessage();
412 $exceptionparam->courseid = $group->courseid;
413 throw new moodle_exception(
414 get_string('errorcoursecontextnotvalid' , 'webservice', $exceptionparam));
416 require_capability('moodle/course:managegroups', $context);
418 // now make sure user is enrolled in course - this is mandatory requirement,
419 // unfortunately this is slow
420 if (!is_enrolled($context, $userid)) {
421 throw new invalid_parameter_exception('Only enrolled users may be members of groups');
424 groups_add_member($group, $user);
427 $transaction->allow_commit();
431 * Returns description of method result value
432 * @return external_description
434 public static function add_groupmembers_returns() {
435 return null;
440 * Returns description of method parameters
441 * @return external_function_parameters
443 public static function delete_groupmembers_parameters() {
444 return new external_function_parameters(
445 array(
446 'members'=> new external_multiple_structure(
447 new external_single_structure(
448 array(
449 'groupid' => new external_value(PARAM_INT, 'group record id'),
450 'userid' => new external_value(PARAM_INT, 'user id'),
459 * Delete group members
460 * @param array $members of arrays with keys userid, groupid
461 * @return void
463 public static function delete_groupmembers($members) {
464 global $CFG, $DB;
465 require_once("$CFG->dirroot/group/lib.php");
467 $params = self::validate_parameters(self::delete_groupmembers_parameters(), array('members'=>$members));
469 $transaction = $DB->start_delegated_transaction();
471 foreach ($params['members'] as $member) {
472 // validate params
473 $groupid = $member['groupid'];
474 $userid = $member['userid'];
476 $group = groups_get_group($groupid, 'id, courseid', MUST_EXIST);
477 $user = $DB->get_record('user', array('id'=>$userid, 'deleted'=>0, 'mnethostid'=>$CFG->mnet_localhost_id), '*', MUST_EXIST);
479 // now security checks
480 $context = get_context_instance(CONTEXT_COURSE, $group->courseid);
481 try {
482 self::validate_context($context);
483 } catch (Exception $e) {
484 $exceptionparam = new stdClass();
485 $exceptionparam->message = $e->getMessage();
486 $exceptionparam->courseid = $group->courseid;
487 throw new moodle_exception(
488 get_string('errorcoursecontextnotvalid' , 'webservice', $exceptionparam));
490 require_capability('moodle/course:managegroups', $context);
492 groups_remove_member($group, $user);
495 $transaction->allow_commit();
499 * Returns description of method result value
500 * @return external_description
502 public static function delete_groupmembers_returns() {
503 return null;