MDL-48730 webservices: New ws core_group_get_course_user_groups
[moodle.git] / group / externallib.php
blob45eadb3f3947a6919ca06ec47c4eb195cc965ac2
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.
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/>.
18 /**
19 * External groups API
21 * @package core_group
22 * @category external
23 * @copyright 2009 Petr Skodak
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require_once("$CFG->libdir/externallib.php");
29 /**
30 * Group external functions
32 * @package core_group
33 * @category external
34 * @copyright 2011 Jerome Mouneyrac
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 * @since Moodle 2.2
38 class core_group_external extends external_api {
40 /**
41 * Returns description of method parameters
43 * @return external_function_parameters
44 * @since Moodle 2.2
46 public static function create_groups_parameters() {
47 return new external_function_parameters(
48 array(
49 'groups' => new external_multiple_structure(
50 new external_single_structure(
51 array(
52 'courseid' => new external_value(PARAM_INT, 'id of course'),
53 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'),
54 'description' => new external_value(PARAM_RAW, 'group description text'),
55 'descriptionformat' => new external_format_value('description', VALUE_DEFAULT),
56 'enrolmentkey' => new external_value(PARAM_RAW, 'group enrol secret phrase', VALUE_OPTIONAL),
58 ), 'List of group object. A group has a courseid, a name, a description and an enrolment key.'
64 /**
65 * Create groups
67 * @param array $groups array of group description arrays (with keys groupname and courseid)
68 * @return array of newly created groups
69 * @since Moodle 2.2
71 public static function create_groups($groups) {
72 global $CFG, $DB;
73 require_once("$CFG->dirroot/group/lib.php");
75 $params = self::validate_parameters(self::create_groups_parameters(), array('groups'=>$groups));
77 $transaction = $DB->start_delegated_transaction();
79 $groups = array();
81 foreach ($params['groups'] as $group) {
82 $group = (object)$group;
84 if (trim($group->name) == '') {
85 throw new invalid_parameter_exception('Invalid group name');
87 if ($DB->get_record('groups', array('courseid'=>$group->courseid, 'name'=>$group->name))) {
88 throw new invalid_parameter_exception('Group with the same name already exists in the course');
91 // now security checks
92 $context = context_course::instance($group->courseid, IGNORE_MISSING);
93 try {
94 self::validate_context($context);
95 } catch (Exception $e) {
96 $exceptionparam = new stdClass();
97 $exceptionparam->message = $e->getMessage();
98 $exceptionparam->courseid = $group->courseid;
99 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
101 require_capability('moodle/course:managegroups', $context);
103 // Validate format.
104 $group->descriptionformat = external_validate_format($group->descriptionformat);
106 // finally create the group
107 $group->id = groups_create_group($group, false);
108 if (!isset($group->enrolmentkey)) {
109 $group->enrolmentkey = '';
111 $groups[] = (array)$group;
114 $transaction->allow_commit();
116 return $groups;
120 * Returns description of method result value
122 * @return external_description
123 * @since Moodle 2.2
125 public static function create_groups_returns() {
126 return new external_multiple_structure(
127 new external_single_structure(
128 array(
129 'id' => new external_value(PARAM_INT, 'group record id'),
130 'courseid' => new external_value(PARAM_INT, 'id of course'),
131 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'),
132 'description' => new external_value(PARAM_RAW, 'group description text'),
133 'descriptionformat' => new external_format_value('description'),
134 'enrolmentkey' => new external_value(PARAM_RAW, 'group enrol secret phrase'),
136 ), 'List of group object. A group has an id, a courseid, a name, a description and an enrolment key.'
141 * Returns description of method parameters
143 * @return external_function_parameters
144 * @since Moodle 2.2
146 public static function get_groups_parameters() {
147 return new external_function_parameters(
148 array(
149 'groupids' => new external_multiple_structure(new external_value(PARAM_INT, 'Group ID')
150 ,'List of group id. A group id is an integer.'),
156 * Get groups definition specified by ids
158 * @param array $groupids arrays of group ids
159 * @return array of group objects (id, courseid, name, enrolmentkey)
160 * @since Moodle 2.2
162 public static function get_groups($groupids) {
163 $params = self::validate_parameters(self::get_groups_parameters(), array('groupids'=>$groupids));
165 $groups = array();
166 foreach ($params['groupids'] as $groupid) {
167 // validate params
168 $group = groups_get_group($groupid, 'id, courseid, name, description, descriptionformat, enrolmentkey', MUST_EXIST);
170 // now security checks
171 $context = context_course::instance($group->courseid, IGNORE_MISSING);
172 try {
173 self::validate_context($context);
174 } catch (Exception $e) {
175 $exceptionparam = new stdClass();
176 $exceptionparam->message = $e->getMessage();
177 $exceptionparam->courseid = $group->courseid;
178 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
180 require_capability('moodle/course:managegroups', $context);
182 list($group->description, $group->descriptionformat) =
183 external_format_text($group->description, $group->descriptionformat,
184 $context->id, 'group', 'description', $group->id);
186 $groups[] = (array)$group;
189 return $groups;
193 * Returns description of method result value
195 * @return external_description
196 * @since Moodle 2.2
198 public static function get_groups_returns() {
199 return new external_multiple_structure(
200 new external_single_structure(
201 array(
202 'id' => new external_value(PARAM_INT, 'group record id'),
203 'courseid' => new external_value(PARAM_INT, 'id of course'),
204 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'),
205 'description' => new external_value(PARAM_RAW, 'group description text'),
206 'descriptionformat' => new external_format_value('description'),
207 'enrolmentkey' => new external_value(PARAM_RAW, 'group enrol secret phrase'),
214 * Returns description of method parameters
216 * @return external_function_parameters
217 * @since Moodle 2.2
219 public static function get_course_groups_parameters() {
220 return new external_function_parameters(
221 array(
222 'courseid' => new external_value(PARAM_INT, 'id of course'),
228 * Get all groups in the specified course
230 * @param int $courseid id of course
231 * @return array of group objects (id, courseid, name, enrolmentkey)
232 * @since Moodle 2.2
234 public static function get_course_groups($courseid) {
235 $params = self::validate_parameters(self::get_course_groups_parameters(), array('courseid'=>$courseid));
237 // now security checks
238 $context = context_course::instance($params['courseid'], IGNORE_MISSING);
239 try {
240 self::validate_context($context);
241 } catch (Exception $e) {
242 $exceptionparam = new stdClass();
243 $exceptionparam->message = $e->getMessage();
244 $exceptionparam->courseid = $params['courseid'];
245 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
247 require_capability('moodle/course:managegroups', $context);
249 $gs = groups_get_all_groups($params['courseid'], 0, 0,
250 'g.id, g.courseid, g.name, g.description, g.descriptionformat, g.enrolmentkey');
252 $groups = array();
253 foreach ($gs as $group) {
254 list($group->description, $group->descriptionformat) =
255 external_format_text($group->description, $group->descriptionformat,
256 $context->id, 'group', 'description', $group->id);
257 $groups[] = (array)$group;
260 return $groups;
264 * Returns description of method result value
266 * @return external_description
267 * @since Moodle 2.2
269 public static function get_course_groups_returns() {
270 return new external_multiple_structure(
271 new external_single_structure(
272 array(
273 'id' => new external_value(PARAM_INT, 'group record id'),
274 'courseid' => new external_value(PARAM_INT, 'id of course'),
275 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'),
276 'description' => new external_value(PARAM_RAW, 'group description text'),
277 'descriptionformat' => new external_format_value('description'),
278 'enrolmentkey' => new external_value(PARAM_RAW, 'group enrol secret phrase'),
285 * Returns description of method parameters
287 * @return external_function_parameters
288 * @since Moodle 2.2
290 public static function delete_groups_parameters() {
291 return new external_function_parameters(
292 array(
293 'groupids' => new external_multiple_structure(new external_value(PARAM_INT, 'Group ID')),
299 * Delete groups
301 * @param array $groupids array of group ids
302 * @since Moodle 2.2
304 public static function delete_groups($groupids) {
305 global $CFG, $DB;
306 require_once("$CFG->dirroot/group/lib.php");
308 $params = self::validate_parameters(self::delete_groups_parameters(), array('groupids'=>$groupids));
310 $transaction = $DB->start_delegated_transaction();
312 foreach ($params['groupids'] as $groupid) {
313 // validate params
314 $groupid = validate_param($groupid, PARAM_INT);
315 if (!$group = groups_get_group($groupid, '*', IGNORE_MISSING)) {
316 // silently ignore attempts to delete nonexisting groups
317 continue;
320 // now security checks
321 $context = context_course::instance($group->courseid, IGNORE_MISSING);
322 try {
323 self::validate_context($context);
324 } catch (Exception $e) {
325 $exceptionparam = new stdClass();
326 $exceptionparam->message = $e->getMessage();
327 $exceptionparam->courseid = $group->courseid;
328 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
330 require_capability('moodle/course:managegroups', $context);
332 groups_delete_group($group);
335 $transaction->allow_commit();
339 * Returns description of method result value
341 * @return null
342 * @since Moodle 2.2
344 public static function delete_groups_returns() {
345 return null;
350 * Returns description of method parameters
352 * @return external_function_parameters
353 * @since Moodle 2.2
355 public static function get_group_members_parameters() {
356 return new external_function_parameters(
357 array(
358 'groupids' => new external_multiple_structure(new external_value(PARAM_INT, 'Group ID')),
364 * Return all members for a group
366 * @param array $groupids array of group ids
367 * @return array with group id keys containing arrays of user ids
368 * @since Moodle 2.2
370 public static function get_group_members($groupids) {
371 $members = array();
373 $params = self::validate_parameters(self::get_group_members_parameters(), array('groupids'=>$groupids));
375 foreach ($params['groupids'] as $groupid) {
376 // validate params
377 $group = groups_get_group($groupid, 'id, courseid, name, enrolmentkey', MUST_EXIST);
378 // now security checks
379 $context = context_course::instance($group->courseid, IGNORE_MISSING);
380 try {
381 self::validate_context($context);
382 } catch (Exception $e) {
383 $exceptionparam = new stdClass();
384 $exceptionparam->message = $e->getMessage();
385 $exceptionparam->courseid = $group->courseid;
386 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
388 require_capability('moodle/course:managegroups', $context);
390 $groupmembers = groups_get_members($group->id, 'u.id', 'lastname ASC, firstname ASC');
392 $members[] = array('groupid'=>$groupid, 'userids'=>array_keys($groupmembers));
395 return $members;
399 * Returns description of method result value
401 * @return external_description
402 * @since Moodle 2.2
404 public static function get_group_members_returns() {
405 return new external_multiple_structure(
406 new external_single_structure(
407 array(
408 'groupid' => new external_value(PARAM_INT, 'group record id'),
409 'userids' => new external_multiple_structure(new external_value(PARAM_INT, 'user id')),
417 * Returns description of method parameters
419 * @return external_function_parameters
420 * @since Moodle 2.2
422 public static function add_group_members_parameters() {
423 return new external_function_parameters(
424 array(
425 'members'=> new external_multiple_structure(
426 new external_single_structure(
427 array(
428 'groupid' => new external_value(PARAM_INT, 'group record id'),
429 'userid' => new external_value(PARAM_INT, 'user id'),
438 * Add group members
440 * @param array $members of arrays with keys userid, groupid
441 * @since Moodle 2.2
443 public static function add_group_members($members) {
444 global $CFG, $DB;
445 require_once("$CFG->dirroot/group/lib.php");
447 $params = self::validate_parameters(self::add_group_members_parameters(), array('members'=>$members));
449 $transaction = $DB->start_delegated_transaction();
450 foreach ($params['members'] as $member) {
451 // validate params
452 $groupid = $member['groupid'];
453 $userid = $member['userid'];
455 $group = groups_get_group($groupid, 'id, courseid', MUST_EXIST);
456 $user = $DB->get_record('user', array('id'=>$userid, 'deleted'=>0, 'mnethostid'=>$CFG->mnet_localhost_id), '*', MUST_EXIST);
458 // now security checks
459 $context = context_course::instance($group->courseid, IGNORE_MISSING);
460 try {
461 self::validate_context($context);
462 } catch (Exception $e) {
463 $exceptionparam = new stdClass();
464 $exceptionparam->message = $e->getMessage();
465 $exceptionparam->courseid = $group->courseid;
466 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
468 require_capability('moodle/course:managegroups', $context);
470 // now make sure user is enrolled in course - this is mandatory requirement,
471 // unfortunately this is slow
472 if (!is_enrolled($context, $userid)) {
473 throw new invalid_parameter_exception('Only enrolled users may be members of groups');
476 groups_add_member($group, $user);
479 $transaction->allow_commit();
483 * Returns description of method result value
485 * @return null
486 * @since Moodle 2.2
488 public static function add_group_members_returns() {
489 return null;
494 * Returns description of method parameters
496 * @return external_function_parameters
497 * @since Moodle 2.2
499 public static function delete_group_members_parameters() {
500 return new external_function_parameters(
501 array(
502 'members'=> new external_multiple_structure(
503 new external_single_structure(
504 array(
505 'groupid' => new external_value(PARAM_INT, 'group record id'),
506 'userid' => new external_value(PARAM_INT, 'user id'),
515 * Delete group members
517 * @param array $members of arrays with keys userid, groupid
518 * @since Moodle 2.2
520 public static function delete_group_members($members) {
521 global $CFG, $DB;
522 require_once("$CFG->dirroot/group/lib.php");
524 $params = self::validate_parameters(self::delete_group_members_parameters(), array('members'=>$members));
526 $transaction = $DB->start_delegated_transaction();
528 foreach ($params['members'] as $member) {
529 // validate params
530 $groupid = $member['groupid'];
531 $userid = $member['userid'];
533 $group = groups_get_group($groupid, 'id, courseid', MUST_EXIST);
534 $user = $DB->get_record('user', array('id'=>$userid, 'deleted'=>0, 'mnethostid'=>$CFG->mnet_localhost_id), '*', MUST_EXIST);
536 // now security checks
537 $context = context_course::instance($group->courseid, IGNORE_MISSING);
538 try {
539 self::validate_context($context);
540 } catch (Exception $e) {
541 $exceptionparam = new stdClass();
542 $exceptionparam->message = $e->getMessage();
543 $exceptionparam->courseid = $group->courseid;
544 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
546 require_capability('moodle/course:managegroups', $context);
548 if (!groups_remove_member_allowed($group, $user)) {
549 throw new moodle_exception('errorremovenotpermitted', 'group', '', fullname($user));
551 groups_remove_member($group, $user);
554 $transaction->allow_commit();
558 * Returns description of method result value
560 * @return null
561 * @since Moodle 2.2
563 public static function delete_group_members_returns() {
564 return null;
568 * Returns description of method parameters
570 * @return external_function_parameters
571 * @since Moodle 2.3
573 public static function create_groupings_parameters() {
574 return new external_function_parameters(
575 array(
576 'groupings' => new external_multiple_structure(
577 new external_single_structure(
578 array(
579 'courseid' => new external_value(PARAM_INT, 'id of course'),
580 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'),
581 'description' => new external_value(PARAM_RAW, 'grouping description text'),
582 'descriptionformat' => new external_format_value('description', VALUE_DEFAULT)
584 ), 'List of grouping object. A grouping has a courseid, a name and a description.'
591 * Create groupings
593 * @param array $groupings array of grouping description arrays (with keys groupname and courseid)
594 * @return array of newly created groupings
595 * @since Moodle 2.3
597 public static function create_groupings($groupings) {
598 global $CFG, $DB;
599 require_once("$CFG->dirroot/group/lib.php");
601 $params = self::validate_parameters(self::create_groupings_parameters(), array('groupings'=>$groupings));
603 $transaction = $DB->start_delegated_transaction();
605 $groupings = array();
607 foreach ($params['groupings'] as $grouping) {
608 $grouping = (object)$grouping;
610 if (trim($grouping->name) == '') {
611 throw new invalid_parameter_exception('Invalid grouping name');
613 if ($DB->count_records('groupings', array('courseid'=>$grouping->courseid, 'name'=>$grouping->name))) {
614 throw new invalid_parameter_exception('Grouping with the same name already exists in the course');
617 // Now security checks .
618 $context = context_course::instance($grouping->courseid);
619 try {
620 self::validate_context($context);
621 } catch (Exception $e) {
622 $exceptionparam = new stdClass();
623 $exceptionparam->message = $e->getMessage();
624 $exceptionparam->courseid = $grouping->courseid;
625 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
627 require_capability('moodle/course:managegroups', $context);
629 $grouping->descriptionformat = external_validate_format($grouping->descriptionformat);
631 // Finally create the grouping.
632 $grouping->id = groups_create_grouping($grouping);
633 $groupings[] = (array)$grouping;
636 $transaction->allow_commit();
638 return $groupings;
642 * Returns description of method result value
644 * @return external_description
645 * @since Moodle 2.3
647 public static function create_groupings_returns() {
648 return new external_multiple_structure(
649 new external_single_structure(
650 array(
651 'id' => new external_value(PARAM_INT, 'grouping record id'),
652 'courseid' => new external_value(PARAM_INT, 'id of course'),
653 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'),
654 'description' => new external_value(PARAM_RAW, 'grouping description text'),
655 'descriptionformat' => new external_format_value('description')
657 ), 'List of grouping object. A grouping has an id, a courseid, a name and a description.'
662 * Returns description of method parameters
664 * @return external_function_parameters
665 * @since Moodle 2.3
667 public static function update_groupings_parameters() {
668 return new external_function_parameters(
669 array(
670 'groupings' => new external_multiple_structure(
671 new external_single_structure(
672 array(
673 'id' => new external_value(PARAM_INT, 'id of grouping'),
674 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'),
675 'description' => new external_value(PARAM_RAW, 'grouping description text'),
676 'descriptionformat' => new external_format_value('description', VALUE_DEFAULT)
678 ), 'List of grouping object. A grouping has a courseid, a name and a description.'
685 * Update groupings
687 * @param array $groupings array of grouping description arrays (with keys groupname and courseid)
688 * @return array of newly updated groupings
689 * @since Moodle 2.3
691 public static function update_groupings($groupings) {
692 global $CFG, $DB;
693 require_once("$CFG->dirroot/group/lib.php");
695 $params = self::validate_parameters(self::update_groupings_parameters(), array('groupings'=>$groupings));
697 $transaction = $DB->start_delegated_transaction();
699 foreach ($params['groupings'] as $grouping) {
700 $grouping = (object)$grouping;
702 if (trim($grouping->name) == '') {
703 throw new invalid_parameter_exception('Invalid grouping name');
706 if (! $currentgrouping = $DB->get_record('groupings', array('id'=>$grouping->id))) {
707 throw new invalid_parameter_exception("Grouping $grouping->id does not exist in the course");
710 // Check if the new modified grouping name already exists in the course.
711 if ($grouping->name != $currentgrouping->name and
712 $DB->count_records('groupings', array('courseid'=>$currentgrouping->courseid, 'name'=>$grouping->name))) {
713 throw new invalid_parameter_exception('A different grouping with the same name already exists in the course');
716 $grouping->courseid = $currentgrouping->courseid;
718 // Now security checks.
719 $context = context_course::instance($grouping->courseid);
720 try {
721 self::validate_context($context);
722 } catch (Exception $e) {
723 $exceptionparam = new stdClass();
724 $exceptionparam->message = $e->getMessage();
725 $exceptionparam->courseid = $grouping->courseid;
726 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
728 require_capability('moodle/course:managegroups', $context);
730 // We must force allways FORMAT_HTML.
731 $grouping->descriptionformat = external_validate_format($grouping->descriptionformat);
733 // Finally update the grouping.
734 groups_update_grouping($grouping);
737 $transaction->allow_commit();
739 return null;
743 * Returns description of method result value
745 * @return external_description
746 * @since Moodle 2.3
748 public static function update_groupings_returns() {
749 return null;
753 * Returns description of method parameters
755 * @return external_function_parameters
756 * @since Moodle 2.3
758 public static function get_groupings_parameters() {
759 return new external_function_parameters(
760 array(
761 'groupingids' => new external_multiple_structure(new external_value(PARAM_INT, 'grouping ID')
762 , 'List of grouping id. A grouping id is an integer.'),
763 'returngroups' => new external_value(PARAM_BOOL, 'return associated groups', VALUE_DEFAULT, 0)
769 * Get groupings definition specified by ids
771 * @param array $groupingids arrays of grouping ids
772 * @param boolean $returngroups return the associated groups if true. The default is false.
773 * @return array of grouping objects (id, courseid, name)
774 * @since Moodle 2.3
776 public static function get_groupings($groupingids, $returngroups = false) {
777 global $CFG, $DB;
778 require_once("$CFG->dirroot/group/lib.php");
779 require_once("$CFG->libdir/filelib.php");
781 $params = self::validate_parameters(self::get_groupings_parameters(),
782 array('groupingids' => $groupingids,
783 'returngroups' => $returngroups));
785 $groupings = array();
786 foreach ($params['groupingids'] as $groupingid) {
787 // Validate params.
788 $grouping = groups_get_grouping($groupingid, '*', MUST_EXIST);
790 // Now security checks.
791 $context = context_course::instance($grouping->courseid);
792 try {
793 self::validate_context($context);
794 } catch (Exception $e) {
795 $exceptionparam = new stdClass();
796 $exceptionparam->message = $e->getMessage();
797 $exceptionparam->courseid = $grouping->courseid;
798 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
800 require_capability('moodle/course:managegroups', $context);
802 list($grouping->description, $grouping->descriptionformat) =
803 external_format_text($grouping->description, $grouping->descriptionformat,
804 $context->id, 'grouping', 'description', $grouping->id);
806 $groupingarray = (array)$grouping;
808 if ($params['returngroups']) {
809 $grouprecords = $DB->get_records_sql("SELECT * FROM {groups} g INNER JOIN {groupings_groups} gg ".
810 "ON g.id = gg.groupid WHERE gg.groupingid = ? ".
811 "ORDER BY groupid", array($groupingid));
812 if ($grouprecords) {
813 $groups = array();
814 foreach ($grouprecords as $grouprecord) {
815 list($grouprecord->description, $grouprecord->descriptionformat) =
816 external_format_text($grouprecord->description, $grouprecord->descriptionformat,
817 $context->id, 'group', 'description', $grouprecord->groupid);
818 $groups[] = array('id' => $grouprecord->groupid,
819 'name' => $grouprecord->name,
820 'description' => $grouprecord->description,
821 'descriptionformat' => $grouprecord->descriptionformat,
822 'enrolmentkey' => $grouprecord->enrolmentkey,
823 'courseid' => $grouprecord->courseid
826 $groupingarray['groups'] = $groups;
829 $groupings[] = $groupingarray;
832 return $groupings;
836 * Returns description of method result value
838 * @return external_description
839 * @since Moodle 2.3
841 public static function get_groupings_returns() {
842 return new external_multiple_structure(
843 new external_single_structure(
844 array(
845 'id' => new external_value(PARAM_INT, 'grouping record id'),
846 'courseid' => new external_value(PARAM_INT, 'id of course'),
847 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'),
848 'description' => new external_value(PARAM_RAW, 'grouping description text'),
849 'descriptionformat' => new external_format_value('description'),
850 'groups' => new external_multiple_structure(
851 new external_single_structure(
852 array(
853 'id' => new external_value(PARAM_INT, 'group record id'),
854 'courseid' => new external_value(PARAM_INT, 'id of course'),
855 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'),
856 'description' => new external_value(PARAM_RAW, 'group description text'),
857 'descriptionformat' => new external_format_value('description'),
858 'enrolmentkey' => new external_value(PARAM_RAW, 'group enrol secret phrase')
861 'optional groups', VALUE_OPTIONAL)
868 * Returns description of method parameters
870 * @return external_function_parameters
871 * @since Moodle 2.3
873 public static function get_course_groupings_parameters() {
874 return new external_function_parameters(
875 array(
876 'courseid' => new external_value(PARAM_INT, 'id of course'),
882 * Get all groupings in the specified course
884 * @param int $courseid id of course
885 * @return array of grouping objects (id, courseid, name, enrolmentkey)
886 * @since Moodle 2.3
888 public static function get_course_groupings($courseid) {
889 global $CFG;
890 require_once("$CFG->dirroot/group/lib.php");
891 require_once("$CFG->libdir/filelib.php");
893 $params = self::validate_parameters(self::get_course_groupings_parameters(), array('courseid'=>$courseid));
895 // Now security checks.
896 $context = context_course::instance($params['courseid']);
898 try {
899 self::validate_context($context);
900 } catch (Exception $e) {
901 $exceptionparam = new stdClass();
902 $exceptionparam->message = $e->getMessage();
903 $exceptionparam->courseid = $params['courseid'];
904 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
906 require_capability('moodle/course:managegroups', $context);
908 $gs = groups_get_all_groupings($params['courseid']);
910 $groupings = array();
911 foreach ($gs as $grouping) {
912 list($grouping->description, $grouping->descriptionformat) =
913 external_format_text($grouping->description, $grouping->descriptionformat,
914 $context->id, 'grouping', 'description', $grouping->id);
915 $groupings[] = (array)$grouping;
918 return $groupings;
922 * Returns description of method result value
924 * @return external_description
925 * @since Moodle 2.3
927 public static function get_course_groupings_returns() {
928 return new external_multiple_structure(
929 new external_single_structure(
930 array(
931 'id' => new external_value(PARAM_INT, 'grouping record id'),
932 'courseid' => new external_value(PARAM_INT, 'id of course'),
933 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'),
934 'description' => new external_value(PARAM_RAW, 'grouping description text'),
935 'descriptionformat' => new external_format_value('description')
942 * Returns description of method parameters
944 * @return external_function_parameters
945 * @since Moodle 2.3
947 public static function delete_groupings_parameters() {
948 return new external_function_parameters(
949 array(
950 'groupingids' => new external_multiple_structure(new external_value(PARAM_INT, 'grouping ID')),
956 * Delete groupings
958 * @param array $groupingids array of grouping ids
959 * @return void
960 * @since Moodle 2.3
962 public static function delete_groupings($groupingids) {
963 global $CFG, $DB;
964 require_once("$CFG->dirroot/group/lib.php");
966 $params = self::validate_parameters(self::delete_groupings_parameters(), array('groupingids'=>$groupingids));
968 $transaction = $DB->start_delegated_transaction();
970 foreach ($params['groupingids'] as $groupingid) {
972 if (!$grouping = groups_get_grouping($groupingid, 'id, courseid', IGNORE_MISSING)) {
973 // Silently ignore attempts to delete nonexisting groupings.
974 continue;
977 // Now security checks.
978 $context = context_course::instance($grouping->courseid);
979 try {
980 self::validate_context($context);
981 } catch (Exception $e) {
982 $exceptionparam = new stdClass();
983 $exceptionparam->message = $e->getMessage();
984 $exceptionparam->courseid = $grouping->courseid;
985 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
987 require_capability('moodle/course:managegroups', $context);
989 groups_delete_grouping($grouping);
992 $transaction->allow_commit();
996 * Returns description of method result value
998 * @return external_description
999 * @since Moodle 2.3
1001 public static function delete_groupings_returns() {
1002 return null;
1006 * Returns description of method parameters
1008 * @return external_function_parameters
1009 * @since Moodle 2.3
1011 public static function assign_grouping_parameters() {
1012 return new external_function_parameters(
1013 array(
1014 'assignments'=> new external_multiple_structure(
1015 new external_single_structure(
1016 array(
1017 'groupingid' => new external_value(PARAM_INT, 'grouping record id'),
1018 'groupid' => new external_value(PARAM_INT, 'group record id'),
1027 * Assign a group to a grouping
1029 * @param array $assignments of arrays with keys groupid, groupingid
1030 * @return void
1031 * @since Moodle 2.3
1033 public static function assign_grouping($assignments) {
1034 global $CFG, $DB;
1035 require_once("$CFG->dirroot/group/lib.php");
1037 $params = self::validate_parameters(self::assign_grouping_parameters(), array('assignments'=>$assignments));
1039 $transaction = $DB->start_delegated_transaction();
1040 foreach ($params['assignments'] as $assignment) {
1041 // Validate params.
1042 $groupingid = $assignment['groupingid'];
1043 $groupid = $assignment['groupid'];
1045 $grouping = groups_get_grouping($groupingid, 'id, courseid', MUST_EXIST);
1046 $group = groups_get_group($groupid, 'id, courseid', MUST_EXIST);
1048 if ($DB->record_exists('groupings_groups', array('groupingid'=>$groupingid, 'groupid'=>$groupid))) {
1049 // Continue silently if the group is yet assigned to the grouping.
1050 continue;
1053 // Now security checks.
1054 $context = context_course::instance($grouping->courseid);
1055 try {
1056 self::validate_context($context);
1057 } catch (Exception $e) {
1058 $exceptionparam = new stdClass();
1059 $exceptionparam->message = $e->getMessage();
1060 $exceptionparam->courseid = $group->courseid;
1061 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
1063 require_capability('moodle/course:managegroups', $context);
1065 groups_assign_grouping($groupingid, $groupid);
1068 $transaction->allow_commit();
1072 * Returns description of method result value
1074 * @return null
1075 * @since Moodle 2.3
1077 public static function assign_grouping_returns() {
1078 return null;
1082 * Returns description of method parameters
1084 * @return external_function_parameters
1085 * @since Moodle 2.3
1087 public static function unassign_grouping_parameters() {
1088 return new external_function_parameters(
1089 array(
1090 'unassignments'=> new external_multiple_structure(
1091 new external_single_structure(
1092 array(
1093 'groupingid' => new external_value(PARAM_INT, 'grouping record id'),
1094 'groupid' => new external_value(PARAM_INT, 'group record id'),
1103 * Unassign a group from a grouping
1105 * @param array $unassignments of arrays with keys groupid, groupingid
1106 * @return void
1107 * @since Moodle 2.3
1109 public static function unassign_grouping($unassignments) {
1110 global $CFG, $DB;
1111 require_once("$CFG->dirroot/group/lib.php");
1113 $params = self::validate_parameters(self::unassign_grouping_parameters(), array('unassignments'=>$unassignments));
1115 $transaction = $DB->start_delegated_transaction();
1116 foreach ($params['unassignments'] as $unassignment) {
1117 // Validate params.
1118 $groupingid = $unassignment['groupingid'];
1119 $groupid = $unassignment['groupid'];
1121 $grouping = groups_get_grouping($groupingid, 'id, courseid', MUST_EXIST);
1122 $group = groups_get_group($groupid, 'id, courseid', MUST_EXIST);
1124 if (!$DB->record_exists('groupings_groups', array('groupingid'=>$groupingid, 'groupid'=>$groupid))) {
1125 // Continue silently if the group is not assigned to the grouping.
1126 continue;
1129 // Now security checks.
1130 $context = context_course::instance($grouping->courseid);
1131 try {
1132 self::validate_context($context);
1133 } catch (Exception $e) {
1134 $exceptionparam = new stdClass();
1135 $exceptionparam->message = $e->getMessage();
1136 $exceptionparam->courseid = $group->courseid;
1137 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
1139 require_capability('moodle/course:managegroups', $context);
1141 groups_unassign_grouping($groupingid, $groupid);
1144 $transaction->allow_commit();
1148 * Returns description of method result value
1150 * @return null
1151 * @since Moodle 2.3
1153 public static function unassign_grouping_returns() {
1154 return null;
1158 * Returns description of method parameters
1160 * @return external_function_parameters
1161 * @since Moodle 2.9
1163 public static function get_course_user_groups_parameters() {
1164 return new external_function_parameters(
1165 array(
1166 'courseid' => new external_value(PARAM_INT, 'id of course'),
1167 'userid' => new external_value(PARAM_INT, 'id of user'),
1168 'groupingid' => new external_value(PARAM_INT, 'returns only groups in the specified grouping', VALUE_DEFAULT, 0)
1174 * Get all groups in the specified course for the specified user.
1176 * @throws moodle_exception
1177 * @param int $courseid id of course.
1178 * @param int $userid id of user.
1179 * @param int $groupingid optional returns only groups in the specified grouping.
1180 * @return array of group objects (id, name, description, format) and possible warnings.
1181 * @since Moodle 2.9
1183 public static function get_course_user_groups($courseid, $userid, $groupingid = 0) {
1184 global $USER;
1186 // Warnings array, it can be empty at the end but is mandatory.
1187 $warnings = array();
1189 $params = array(
1190 'courseid' => $courseid,
1191 'userid' => $userid,
1192 'groupingid' => $groupingid
1194 $params = self::validate_parameters(self::get_course_user_groups_parameters(), $params);
1195 $courseid = $params['courseid'];
1196 $userid = $params['userid'];
1197 $groupingid = $params['groupingid'];
1199 // Validate course and user. get_course throws an exception if the course does not exists.
1200 $course = get_course($courseid);
1201 $user = core_user::get_user($userid, 'id', MUST_EXIST);
1203 // Security checks.
1204 $context = context_course::instance($course->id);
1205 self::validate_context($context);
1207 // Check if we have permissions for retrieve the information.
1208 if ($user->id != $USER->id) {
1209 if (!has_capability('moodle/course:managegroups', $context)) {
1210 throw new moodle_exception('accessdenied', 'admin');
1212 // Validate if the user is enrolled in the course.
1213 if (!is_enrolled($context, $user->id)) {
1214 // We return a warning because the function does not fail for not enrolled users.
1215 $warning['item'] = 'course';
1216 $warning['itemid'] = $course->id;
1217 $warning['warningcode'] = '1';
1218 $warning['message'] = "User $user->id is not enrolled in course $course->id";
1219 $warnings[] = $warning;
1223 $usergroups = array();
1224 if (empty($warnings)) {
1225 $groups = groups_get_all_groups($course->id, $user->id, 0, 'g.id, g.name, g.description, g.descriptionformat');
1227 foreach ($groups as $group) {
1228 list($group->description, $group->descriptionformat) =
1229 external_format_text($group->description, $group->descriptionformat,
1230 $context->id, 'group', 'description', $group->id);
1231 $usergroups[] = (array)$group;
1235 $results = array(
1236 'groups' => $usergroups,
1237 'warnings' => $warnings
1239 return $results;
1243 * Returns description of method result value.
1245 * @return external_description A single structure containing groups and possible warnings.
1246 * @since Moodle 2.9
1248 public static function get_course_user_groups_returns() {
1249 return new external_single_structure(
1250 array(
1251 'groups' => new external_multiple_structure(
1252 new external_single_structure(
1253 array(
1254 'id' => new external_value(PARAM_INT, 'group record id'),
1255 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'),
1256 'description' => new external_value(PARAM_RAW, 'group description text'),
1257 'descriptionformat' => new external_format_value('description')
1261 'warnings' => new external_warnings(),
1269 * Deprecated group external functions
1271 * @package core_group
1272 * @copyright 2009 Petr Skodak
1273 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1274 * @since Moodle 2.0
1275 * @deprecated Moodle 2.2 MDL-29106 - Please do not use this class any more.
1276 * @see core_group_external
1278 class moodle_group_external extends external_api {
1281 * Returns description of method parameters
1283 * @return external_function_parameters
1284 * @since Moodle 2.0
1285 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1286 * @see core_group_external::create_groups_parameters()
1288 public static function create_groups_parameters() {
1289 return core_group_external::create_groups_parameters();
1293 * Create groups
1295 * @param array $groups array of group description arrays (with keys groupname and courseid)
1296 * @return array of newly created groups
1297 * @since Moodle 2.0
1298 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1299 * @see use core_group_external::create_groups()
1301 public static function create_groups($groups) {
1302 return core_group_external::create_groups($groups);
1306 * Returns description of method result value
1308 * @return external_description
1309 * @since Moodle 2.0
1310 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1311 * @see core_group_external::create_groups_returns()
1313 public static function create_groups_returns() {
1314 return core_group_external::create_groups_returns();
1318 * Marking the method as deprecated.
1320 * @return bool
1322 public static function create_groups_is_deprecated() {
1323 return true;
1327 * Returns description of method parameters
1329 * @return external_function_parameters
1330 * @since Moodle 2.0
1331 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1332 * @see core_group_external::get_groups_parameters()
1334 public static function get_groups_parameters() {
1335 return core_group_external::get_groups_parameters();
1339 * Get groups definition specified by ids
1341 * @param array $groupids arrays of group ids
1342 * @return array of group objects (id, courseid, name, enrolmentkey)
1343 * @since Moodle 2.0
1344 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1345 * @see core_group_external::get_groups()
1347 public static function get_groups($groupids) {
1348 return core_group_external::get_groups($groupids);
1352 * Returns description of method result value
1354 * @return external_description
1355 * @since Moodle 2.0
1356 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1357 * @see core_group_external::get_groups_returns()
1359 public static function get_groups_returns() {
1360 return core_group_external::get_groups_returns();
1364 * Marking the method as deprecated.
1366 * @return bool
1368 public static function get_groups_is_deprecated() {
1369 return true;
1373 * Returns description of method parameters
1375 * @return external_function_parameters
1376 * @since Moodle 2.0
1377 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1378 * @see core_group_external::get_course_groups_parameters()
1380 public static function get_course_groups_parameters() {
1381 return core_group_external::get_course_groups_parameters();
1385 * Get all groups in the specified course
1387 * @param int $courseid id of course
1388 * @return array of group objects (id, courseid, name, enrolmentkey)
1389 * @since Moodle 2.0
1390 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1391 * @see core_group_external::get_course_groups()
1393 public static function get_course_groups($courseid) {
1394 return core_group_external::get_course_groups($courseid);
1398 * Returns description of method result value
1400 * @return external_description
1401 * @since Moodle 2.0
1402 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1403 * @see core_group_external::get_course_groups_returns()
1405 public static function get_course_groups_returns() {
1406 return core_group_external::get_course_groups_returns();
1410 * Marking the method as deprecated.
1412 * @return bool
1414 public static function get_course_groups_is_deprecated() {
1415 return true;
1419 * Returns description of method parameters
1421 * @return external_function_parameters
1422 * @since Moodle 2.0
1423 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1424 * @see core_group_external::delete_group_members_parameters()
1426 public static function delete_groups_parameters() {
1427 return core_group_external::delete_groups_parameters();
1431 * Delete groups
1433 * @param array $groupids array of group ids
1434 * @since Moodle 2.0
1435 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1436 * @see core_group_external::delete_groups()
1438 public static function delete_groups($groupids) {
1439 return core_group_external::delete_groups($groupids);
1443 * Returns description of method result value
1445 * @return null
1446 * @since Moodle 2.0
1447 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1448 * @see core_group_external::delete_group_members_returns()
1450 public static function delete_groups_returns() {
1451 return core_group_external::delete_groups_returns();
1455 * Marking the method as deprecated.
1457 * @return bool
1459 public static function delete_groups_is_deprecated() {
1460 return true;
1464 * Returns description of method parameters
1466 * @return external_function_parameters
1467 * @since Moodle 2.0
1468 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1469 * @see core_group_external::get_group_members_parameters()
1471 public static function get_groupmembers_parameters() {
1472 return core_group_external::get_group_members_parameters();
1476 * Return all members for a group
1478 * @param array $groupids array of group ids
1479 * @return array with group id keys containing arrays of user ids
1480 * @since Moodle 2.0
1481 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1482 * @see core_group_external::get_group_members()
1484 public static function get_groupmembers($groupids) {
1485 return core_group_external::get_group_members($groupids);
1489 * Returns description of method result value
1491 * @return external_description
1492 * @since Moodle 2.0
1493 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1494 * @see core_group_external::get_group_members_returns()
1496 public static function get_groupmembers_returns() {
1497 return core_group_external::get_group_members_returns();
1501 * Marking the method as deprecated.
1503 * @return bool
1505 public static function get_groupmembers_is_deprecated() {
1506 return true;
1510 * Returns description of method parameters
1512 * @return external_function_parameters
1513 * @since Moodle 2.0
1514 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1515 * @see core_group_external::add_group_members_parameters()
1517 public static function add_groupmembers_parameters() {
1518 return core_group_external::add_group_members_parameters();
1522 * Add group members
1524 * @param array $members of arrays with keys userid, groupid
1525 * @since Moodle 2.0
1526 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1527 * @see use core_group_external::add_group_members()
1529 public static function add_groupmembers($members) {
1530 return core_group_external::add_group_members($members);
1534 * Returns description of method result value
1536 * @return external_description
1537 * @since Moodle 2.0
1538 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1539 * @see core_group_external::add_group_members_returns()
1541 public static function add_groupmembers_returns() {
1542 return core_group_external::add_group_members_returns();
1546 * Marking the method as deprecated.
1548 * @return bool
1550 public static function add_groupmembers_is_deprecated() {
1551 return true;
1555 * Returns description of method parameters
1557 * @return external_function_parameters
1558 * @since Moodle 2.0
1559 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1560 * @see core_group_external::delete_group_members_parameters()
1562 public static function delete_groupmembers_parameters() {
1563 return core_group_external::delete_group_members_parameters();
1567 * Delete group members
1569 * @param array $members of arrays with keys userid, groupid
1570 * @since Moodle 2.0
1571 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1572 * @see core_group_external::delete_group_members()
1574 public static function delete_groupmembers($members) {
1575 return core_group_external::delete_group_members($members);
1579 * Returns description of method result value
1581 * @return external_description
1582 * @since Moodle 2.0
1583 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1584 * @see core_group_external::delete_group_members_returns()
1586 public static function delete_groupmembers_returns() {
1587 return core_group_external::delete_group_members_returns();
1591 * Marking the method as deprecated.
1593 * @return bool
1595 public static function delete_groupmembers_is_deprecated() {
1596 return true;