Merge branch 'MDL-28346-22' of git://github.com/FMCorz/moodle into MOODLE_22_STABLE
[moodle.git] / group / externallib.php
bloba91c245916098ebf36d6447f2b9b385390849473
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 /**
30 * Group functions
32 class core_group_external extends external_api {
34 /**
35 * Returns description of method parameters
36 * @return external_function_parameters
38 public static function create_groups_parameters() {
39 return new external_function_parameters(
40 array(
41 'groups' => new external_multiple_structure(
42 new external_single_structure(
43 array(
44 'courseid' => new external_value(PARAM_INT, 'id of course'),
45 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'),
46 'description' => new external_value(PARAM_RAW, 'group description text'),
47 'enrolmentkey' => new external_value(PARAM_RAW, 'group enrol secret phrase', VALUE_OPTIONAL),
49 ), 'List of group object. A group has a courseid, a name, a description and an enrolment key.'
55 /**
56 * Create groups
57 * @param array $groups array of group description arrays (with keys groupname and courseid)
58 * @return array of newly created groups
60 public static function create_groups($groups) {
61 global $CFG, $DB;
62 require_once("$CFG->dirroot/group/lib.php");
64 $params = self::validate_parameters(self::create_groups_parameters(), array('groups'=>$groups));
66 $transaction = $DB->start_delegated_transaction();
68 $groups = array();
70 foreach ($params['groups'] as $group) {
71 $group = (object)$group;
73 if (trim($group->name) == '') {
74 throw new invalid_parameter_exception('Invalid group name');
76 if ($DB->get_record('groups', array('courseid'=>$group->courseid, 'name'=>$group->name))) {
77 throw new invalid_parameter_exception('Group with the same name already exists in the course');
80 // now security checks
81 $context = get_context_instance(CONTEXT_COURSE, $group->courseid);
82 try {
83 self::validate_context($context);
84 } catch (Exception $e) {
85 $exceptionparam = new stdClass();
86 $exceptionparam->message = $e->getMessage();
87 $exceptionparam->courseid = $group->courseid;
88 throw new moodle_exception(
89 get_string('errorcoursecontextnotvalid' , 'webservice', $exceptionparam));
91 require_capability('moodle/course:managegroups', $context);
93 // finally create the group
94 $group->id = groups_create_group($group, false);
95 if (!isset($group->enrolmentkey)) {
96 $group->enrolmentkey = '';
98 $groups[] = (array)$group;
101 $transaction->allow_commit();
103 return $groups;
107 * Returns description of method result value
108 * @return external_description
110 public static function create_groups_returns() {
111 return new external_multiple_structure(
112 new external_single_structure(
113 array(
114 'id' => new external_value(PARAM_INT, 'group record id'),
115 'courseid' => new external_value(PARAM_INT, 'id of course'),
116 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'),
117 'description' => new external_value(PARAM_RAW, 'group description text'),
118 'enrolmentkey' => new external_value(PARAM_RAW, 'group enrol secret phrase'),
120 ), 'List of group object. A group has an id, a courseid, a name, a description and an enrolment key.'
125 * Returns description of method parameters
126 * @return external_function_parameters
128 public static function get_groups_parameters() {
129 return new external_function_parameters(
130 array(
131 'groupids' => new external_multiple_structure(new external_value(PARAM_INT, 'Group ID')
132 ,'List of group id. A group id is an integer.'),
138 * Get groups definition specified by ids
139 * @param array $groupids arrays of group ids
140 * @return array of group objects (id, courseid, name, enrolmentkey)
142 public static function get_groups($groupids) {
143 $params = self::validate_parameters(self::get_groups_parameters(), array('groupids'=>$groupids));
145 $groups = array();
146 foreach ($params['groupids'] as $groupid) {
147 // validate params
148 $group = groups_get_group($groupid, 'id, courseid, name, description, enrolmentkey', MUST_EXIST);
150 // now security checks
151 $context = get_context_instance(CONTEXT_COURSE, $group->courseid);
152 try {
153 self::validate_context($context);
154 } catch (Exception $e) {
155 $exceptionparam = new stdClass();
156 $exceptionparam->message = $e->getMessage();
157 $exceptionparam->courseid = $group->courseid;
158 throw new moodle_exception(
159 get_string('errorcoursecontextnotvalid' , 'webservice', $exceptionparam));
161 require_capability('moodle/course:managegroups', $context);
163 $groups[] = (array)$group;
166 return $groups;
170 * Returns description of method result value
171 * @return external_description
173 public static function get_groups_returns() {
174 return new external_multiple_structure(
175 new external_single_structure(
176 array(
177 'id' => new external_value(PARAM_INT, 'group record id'),
178 'courseid' => new external_value(PARAM_INT, 'id of course'),
179 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'),
180 'description' => new external_value(PARAM_RAW, 'group description text'),
181 'enrolmentkey' => new external_value(PARAM_RAW, 'group enrol secret phrase'),
188 * Returns description of method parameters
189 * @return external_function_parameters
191 public static function get_course_groups_parameters() {
192 return new external_function_parameters(
193 array(
194 'courseid' => new external_value(PARAM_INT, 'id of course'),
200 * Get all groups in the specified course
201 * @param int $courseid id of course
202 * @return array of group objects (id, courseid, name, enrolmentkey)
204 public static function get_course_groups($courseid) {
205 $params = self::validate_parameters(self::get_course_groups_parameters(), array('courseid'=>$courseid));
207 // now security checks
208 $context = get_context_instance(CONTEXT_COURSE, $params['courseid']);
209 try {
210 self::validate_context($context);
211 } catch (Exception $e) {
212 $exceptionparam = new stdClass();
213 $exceptionparam->message = $e->getMessage();
214 $exceptionparam->courseid = $params['courseid'];
215 throw new moodle_exception(
216 get_string('errorcoursecontextnotvalid' , 'webservice', $exceptionparam));
218 require_capability('moodle/course:managegroups', $context);
220 $gs = groups_get_all_groups($params['courseid'], 0, 0, 'g.id, g.courseid, g.name, g.description, g.enrolmentkey');
222 $groups = array();
223 foreach ($gs as $group) {
224 $groups[] = (array)$group;
227 return $groups;
231 * Returns description of method result value
232 * @return external_description
234 public static function get_course_groups_returns() {
235 return new external_multiple_structure(
236 new external_single_structure(
237 array(
238 'id' => new external_value(PARAM_INT, 'group record id'),
239 'courseid' => new external_value(PARAM_INT, 'id of course'),
240 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'),
241 'description' => new external_value(PARAM_RAW, 'group description text'),
242 'enrolmentkey' => new external_value(PARAM_RAW, 'group enrol secret phrase'),
249 * Returns description of method parameters
250 * @return external_function_parameters
252 public static function delete_groups_parameters() {
253 return new external_function_parameters(
254 array(
255 'groupids' => new external_multiple_structure(new external_value(PARAM_INT, 'Group ID')),
261 * Delete groups
262 * @param array $groupids array of group ids
263 * @return void
265 public static function delete_groups($groupids) {
266 global $CFG, $DB;
267 require_once("$CFG->dirroot/group/lib.php");
269 $params = self::validate_parameters(self::delete_groups_parameters(), array('groupids'=>$groupids));
271 $transaction = $DB->start_delegated_transaction();
273 foreach ($params['groupids'] as $groupid) {
274 // validate params
275 $groupid = validate_param($groupid, PARAM_INTEGER);
276 if (!$group = groups_get_group($groupid, 'id, courseid', IGNORE_MISSING)) {
277 // silently ignore attempts to delete nonexisting groups
278 continue;
281 // now security checks
282 $context = get_context_instance(CONTEXT_COURSE, $group->courseid);
283 try {
284 self::validate_context($context);
285 } catch (Exception $e) {
286 $exceptionparam = new stdClass();
287 $exceptionparam->message = $e->getMessage();
288 $exceptionparam->courseid = $group->courseid;
289 throw new moodle_exception(
290 get_string('errorcoursecontextnotvalid' , 'webservice', $exceptionparam));
292 require_capability('moodle/course:managegroups', $context);
294 groups_delete_group($group);
297 $transaction->allow_commit();
301 * Returns description of method result value
302 * @return external_description
304 public static function delete_groups_returns() {
305 return null;
310 * Returns description of method parameters
311 * @return external_function_parameters
313 public static function get_group_members_parameters() {
314 return new external_function_parameters(
315 array(
316 'groupids' => new external_multiple_structure(new external_value(PARAM_INT, 'Group ID')),
322 * Return all members for a group
323 * @param array $groupids array of group ids
324 * @return array with group id keys containing arrays of user ids
326 public static function get_group_members($groupids) {
327 $members = array();
329 $params = self::validate_parameters(self::get_group_members_parameters(), array('groupids'=>$groupids));
331 foreach ($params['groupids'] as $groupid) {
332 // validate params
333 $group = groups_get_group($groupid, 'id, courseid, name, enrolmentkey', MUST_EXIST);
334 // now security checks
335 $context = get_context_instance(CONTEXT_COURSE, $group->courseid);
336 try {
337 self::validate_context($context);
338 } catch (Exception $e) {
339 $exceptionparam = new stdClass();
340 $exceptionparam->message = $e->getMessage();
341 $exceptionparam->courseid = $group->courseid;
342 throw new moodle_exception(
343 get_string('errorcoursecontextnotvalid' , 'webservice', $exceptionparam));
345 require_capability('moodle/course:managegroups', $context);
347 $groupmembers = groups_get_members($group->id, 'u.id', 'lastname ASC, firstname ASC');
349 $members[] = array('groupid'=>$groupid, 'userids'=>array_keys($groupmembers));
352 return $members;
356 * Returns description of method result value
357 * @return external_description
359 public static function get_group_members_returns() {
360 return new external_multiple_structure(
361 new external_single_structure(
362 array(
363 'groupid' => new external_value(PARAM_INT, 'group record id'),
364 'userids' => new external_multiple_structure(new external_value(PARAM_INT, 'user id')),
372 * Returns description of method parameters
373 * @return external_function_parameters
375 public static function add_group_members_parameters() {
376 return new external_function_parameters(
377 array(
378 'members'=> new external_multiple_structure(
379 new external_single_structure(
380 array(
381 'groupid' => new external_value(PARAM_INT, 'group record id'),
382 'userid' => new external_value(PARAM_INT, 'user id'),
391 * Add group members
392 * @param array $members of arrays with keys userid, groupid
393 * @return void
395 public static function add_group_members($members) {
396 global $CFG, $DB;
397 require_once("$CFG->dirroot/group/lib.php");
399 $params = self::validate_parameters(self::add_group_members_parameters(), array('members'=>$members));
401 $transaction = $DB->start_delegated_transaction();
402 foreach ($params['members'] as $member) {
403 // validate params
404 $groupid = $member['groupid'];
405 $userid = $member['userid'];
407 $group = groups_get_group($groupid, 'id, courseid', MUST_EXIST);
408 $user = $DB->get_record('user', array('id'=>$userid, 'deleted'=>0, 'mnethostid'=>$CFG->mnet_localhost_id), '*', MUST_EXIST);
410 // now security checks
411 $context = get_context_instance(CONTEXT_COURSE, $group->courseid);
412 try {
413 self::validate_context($context);
414 } catch (Exception $e) {
415 $exceptionparam = new stdClass();
416 $exceptionparam->message = $e->getMessage();
417 $exceptionparam->courseid = $group->courseid;
418 throw new moodle_exception(
419 get_string('errorcoursecontextnotvalid' , 'webservice', $exceptionparam));
421 require_capability('moodle/course:managegroups', $context);
423 // now make sure user is enrolled in course - this is mandatory requirement,
424 // unfortunately this is slow
425 if (!is_enrolled($context, $userid)) {
426 throw new invalid_parameter_exception('Only enrolled users may be members of groups');
429 groups_add_member($group, $user);
432 $transaction->allow_commit();
436 * Returns description of method result value
437 * @return null
439 public static function add_group_members_returns() {
440 return null;
445 * Returns description of method parameters
446 * @return external_function_parameters
448 public static function delete_group_members_parameters() {
449 return new external_function_parameters(
450 array(
451 'members'=> new external_multiple_structure(
452 new external_single_structure(
453 array(
454 'groupid' => new external_value(PARAM_INT, 'group record id'),
455 'userid' => new external_value(PARAM_INT, 'user id'),
464 * Delete group members
465 * @param array $members of arrays with keys userid, groupid
466 * @return void
468 public static function delete_group_members($members) {
469 global $CFG, $DB;
470 require_once("$CFG->dirroot/group/lib.php");
472 $params = self::validate_parameters(self::delete_group_members_parameters(), array('members'=>$members));
474 $transaction = $DB->start_delegated_transaction();
476 foreach ($params['members'] as $member) {
477 // validate params
478 $groupid = $member['groupid'];
479 $userid = $member['userid'];
481 $group = groups_get_group($groupid, 'id, courseid', MUST_EXIST);
482 $user = $DB->get_record('user', array('id'=>$userid, 'deleted'=>0, 'mnethostid'=>$CFG->mnet_localhost_id), '*', MUST_EXIST);
484 // now security checks
485 $context = get_context_instance(CONTEXT_COURSE, $group->courseid);
486 try {
487 self::validate_context($context);
488 } catch (Exception $e) {
489 $exceptionparam = new stdClass();
490 $exceptionparam->message = $e->getMessage();
491 $exceptionparam->courseid = $group->courseid;
492 throw new moodle_exception(
493 get_string('errorcoursecontextnotvalid' , 'webservice', $exceptionparam));
495 require_capability('moodle/course:managegroups', $context);
497 groups_remove_member($group, $user);
500 $transaction->allow_commit();
504 * Returns description of method result value
505 * @return null
507 public static function delete_group_members_returns() {
508 return null;
514 * Deprecated group functions
515 * @deprecated since Moodle 2.2 please use core_group_external instead
517 class moodle_group_external extends external_api {
520 * Returns description of method parameters
521 * @deprecated since Moodle 2.2 please use core_group_external::create_groups_parameters instead
522 * @return external_function_parameters
524 public static function create_groups_parameters() {
525 return core_group_external::create_groups_parameters();
529 * Create groups
530 * @deprecated since Moodle 2.2 please use core_group_external::create_groups instead
531 * @param array $groups array of group description arrays (with keys groupname and courseid)
532 * @return array of newly created groups
534 public static function create_groups($groups) {
535 return core_group_external::create_groups($groups);
539 * Returns description of method result value
540 * @deprecated since Moodle 2.2 please use core_group_external::create_groups_returns instead
541 * @return external_description
543 public static function create_groups_returns() {
544 return core_group_external::create_groups_returns();
548 * Returns description of method parameters
549 * @deprecated since Moodle 2.2 please use core_group_external::get_groups_parameters instead
550 * @return external_function_parameters
552 public static function get_groups_parameters() {
553 return core_group_external::get_groups_parameters();
557 * Get groups definition specified by ids
558 * @deprecated since Moodle 2.2 please use core_group_external::get_groups instead
559 * @param array $groupids arrays of group ids
560 * @return array of group objects (id, courseid, name, enrolmentkey)
562 public static function get_groups($groupids) {
563 return core_group_external::get_groups($groupids);
567 * Returns description of method result value
568 * @deprecated since Moodle 2.2 please use core_group_external::get_groups_returns instead
569 * @return external_description
571 public static function get_groups_returns() {
572 return core_group_external::get_groups_returns();
576 * Returns description of method parameters
577 * @deprecated since Moodle 2.2 please use core_group_external::get_course_groups_parameters instead
578 * @return external_function_parameters
580 public static function get_course_groups_parameters() {
581 return core_group_external::get_course_groups_parameters();
585 * Get all groups in the specified course
586 * @deprecated since Moodle 2.2 please use core_group_external::get_course_groups instead
587 * @param int $courseid id of course
588 * @return array of group objects (id, courseid, name, enrolmentkey)
590 public static function get_course_groups($courseid) {
591 return core_group_external::get_course_groups($courseid);
595 * Returns description of method result value
596 * @deprecated since Moodle 2.2 please use core_group_external::get_course_groups_returns instead
597 * @return external_description
599 public static function get_course_groups_returns() {
600 return core_group_external::get_course_groups_returns();
604 * Returns description of method parameters
605 * @deprecated since Moodle 2.2 please use core_group_external::delete_group_members_parameters instead
606 * @return external_function_parameters
608 public static function delete_groups_parameters() {
609 return core_group_external::delete_group_members_parameters();
613 * Delete groups
614 * @deprecated since Moodle 2.2 please use core_group_external::delete_groups instead
615 * @param array $groupids array of group ids
616 * @return void
618 public static function delete_groups($groupids) {
619 return core_group_external::delete_groups($groupids);
623 * Returns description of method result value
624 * @deprecated since Moodle 2.2 please use core_group_external::delete_group_members_returns instead
625 * @return external_description
627 public static function delete_groups_returns() {
628 return core_group_external::delete_group_members_returns();
633 * Returns description of method parameters
634 * @deprecated since Moodle 2.2 please use core_group_external::get_group_members_parameters instead
635 * @return external_function_parameters
637 public static function get_groupmembers_parameters() {
638 return core_group_external::get_group_members_parameters();
642 * Return all members for a group
643 * @deprecated since Moodle 2.2 please use core_group_external::get_group_members instead
644 * @param array $groupids array of group ids
645 * @return array with group id keys containing arrays of user ids
647 public static function get_groupmembers($groupids) {
648 return core_group_external::get_group_members($groupids);
652 * Returns description of method result value
653 * @deprecated since Moodle 2.2 please use core_group_external::get_group_members_returns instead
654 * @return external_description
656 public static function get_groupmembers_returns() {
657 return core_group_external::get_group_members_returns();
662 * Returns description of method parameters
663 * @deprecated since Moodle 2.2 please use core_group_external::add_group_members_parameters instead
664 * @return external_function_parameters
666 public static function add_groupmembers_parameters() {
667 return core_group_external::add_group_members_parameters();
671 * Add group members
672 * @deprecated since Moodle 2.2 please use core_group_external::add_group_members instead
673 * @param array $members of arrays with keys userid, groupid
674 * @return void
676 public static function add_groupmembers($members) {
677 return core_group_external::add_group_members($members);
681 * Returns description of method result value
682 * @deprecated since Moodle 2.2 please use core_group_external::add_group_members_returns instead
683 * @return external_description
685 public static function add_groupmembers_returns() {
686 return core_group_external::add_group_members_returns();
691 * Returns description of method parameters
692 * @deprecated since Moodle 2.2 please use core_group_external::delete_group_members_parameters instead
693 * @return external_function_parameters
695 public static function delete_groupmembers_parameters() {
696 return core_group_external::delete_group_members_parameters();
700 * Delete group members
701 * @deprecated since Moodle 2.2 please use core_group_external::delete_group_members instead
702 * @param array $members of arrays with keys userid, groupid
703 * @return void
705 public static function delete_groupmembers($members) {
706 return core_group_external::delete_group_members($members);
710 * Returns description of method result value
711 * @deprecated since Moodle 2.2 please use core_group_external::delete_group_members_returns instead
712 * @return external_description
714 public static function delete_groupmembers_returns() {
715 return core_group_external::delete_group_members_returns();