Merge branch 'wip-MDL-39836_M25' of git://github.com/gjb2048/moodle into MOODLE_25_STABLE
[moodle.git] / group / externallib.php
bloba316b51af6d7e2feeee5dbff6e93480e89663604
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, 'id, courseid', 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;
1160 * Deprecated group external functions
1162 * @package core_group
1163 * @copyright 2009 Petr Skodak
1164 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1165 * @since Moodle 2.0
1166 * @deprecated Moodle 2.2 MDL-29106 - Please do not use this class any more.
1167 * @see core_group_external
1169 class moodle_group_external extends external_api {
1172 * Returns description of method parameters
1174 * @return external_function_parameters
1175 * @since Moodle 2.0
1176 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1177 * @see core_group_external::create_groups_parameters()
1179 public static function create_groups_parameters() {
1180 return core_group_external::create_groups_parameters();
1184 * Create groups
1186 * @param array $groups array of group description arrays (with keys groupname and courseid)
1187 * @return array of newly created groups
1188 * @since Moodle 2.0
1189 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1190 * @see use core_group_external::create_groups()
1192 public static function create_groups($groups) {
1193 return core_group_external::create_groups($groups);
1197 * Returns description of method result value
1199 * @return external_description
1200 * @since Moodle 2.0
1201 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1202 * @see core_group_external::create_groups_returns()
1204 public static function create_groups_returns() {
1205 return core_group_external::create_groups_returns();
1209 * Returns description of method parameters
1211 * @return external_function_parameters
1212 * @since Moodle 2.0
1213 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1214 * @see core_group_external::get_groups_parameters()
1216 public static function get_groups_parameters() {
1217 return core_group_external::get_groups_parameters();
1221 * Get groups definition specified by ids
1223 * @param array $groupids arrays of group ids
1224 * @return array of group objects (id, courseid, name, enrolmentkey)
1225 * @since Moodle 2.0
1226 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1227 * @see core_group_external::get_groups()
1229 public static function get_groups($groupids) {
1230 return core_group_external::get_groups($groupids);
1234 * Returns description of method result value
1236 * @return external_description
1237 * @since Moodle 2.0
1238 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1239 * @see core_group_external::get_groups_returns()
1241 public static function get_groups_returns() {
1242 return core_group_external::get_groups_returns();
1246 * Returns description of method parameters
1248 * @return external_function_parameters
1249 * @since Moodle 2.0
1250 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1251 * @see core_group_external::get_course_groups_parameters()
1253 public static function get_course_groups_parameters() {
1254 return core_group_external::get_course_groups_parameters();
1258 * Get all groups in the specified course
1260 * @param int $courseid id of course
1261 * @return array of group objects (id, courseid, name, enrolmentkey)
1262 * @since Moodle 2.0
1263 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1264 * @see core_group_external::get_course_groups()
1266 public static function get_course_groups($courseid) {
1267 return core_group_external::get_course_groups($courseid);
1271 * Returns description of method result value
1273 * @return external_description
1274 * @since Moodle 2.0
1275 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1276 * @see core_group_external::get_course_groups_returns()
1278 public static function get_course_groups_returns() {
1279 return core_group_external::get_course_groups_returns();
1283 * Returns description of method parameters
1285 * @return external_function_parameters
1286 * @since Moodle 2.0
1287 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1288 * @see core_group_external::delete_group_members_parameters()
1290 public static function delete_groups_parameters() {
1291 return core_group_external::delete_group_members_parameters();
1295 * Delete groups
1297 * @param array $groupids array of group ids
1298 * @since Moodle 2.0
1299 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1300 * @see core_group_external::delete_groups()
1302 public static function delete_groups($groupids) {
1303 return core_group_external::delete_groups($groupids);
1307 * Returns description of method result value
1309 * @return null
1310 * @since Moodle 2.0
1311 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1312 * @see core_group_external::delete_group_members_returns()
1314 public static function delete_groups_returns() {
1315 return core_group_external::delete_group_members_returns();
1320 * Returns description of method parameters
1322 * @return external_function_parameters
1323 * @since Moodle 2.0
1324 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1325 * @see core_group_external::get_group_members_parameters()
1327 public static function get_groupmembers_parameters() {
1328 return core_group_external::get_group_members_parameters();
1332 * Return all members for a group
1334 * @param array $groupids array of group ids
1335 * @return array with group id keys containing arrays of user ids
1336 * @since Moodle 2.0
1337 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1338 * @see core_group_external::get_group_members()
1340 public static function get_groupmembers($groupids) {
1341 return core_group_external::get_group_members($groupids);
1345 * Returns description of method result value
1347 * @return external_description
1348 * @since Moodle 2.0
1349 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1350 * @see core_group_external::get_group_members_returns()
1352 public static function get_groupmembers_returns() {
1353 return core_group_external::get_group_members_returns();
1358 * Returns description of method parameters
1360 * @return external_function_parameters
1361 * @since Moodle 2.0
1362 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1363 * @see core_group_external::add_group_members_parameters()
1365 public static function add_groupmembers_parameters() {
1366 return core_group_external::add_group_members_parameters();
1370 * Add group members
1372 * @param array $members of arrays with keys userid, groupid
1373 * @since Moodle 2.0
1374 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1375 * @see use core_group_external::add_group_members()
1377 public static function add_groupmembers($members) {
1378 return core_group_external::add_group_members($members);
1382 * Returns description of method result value
1384 * @return external_description
1385 * @since Moodle 2.0
1386 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1387 * @see core_group_external::add_group_members_returns()
1389 public static function add_groupmembers_returns() {
1390 return core_group_external::add_group_members_returns();
1395 * Returns description of method parameters
1397 * @return external_function_parameters
1398 * @since Moodle 2.0
1399 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1400 * @see core_group_external::delete_group_members_parameters()
1402 public static function delete_groupmembers_parameters() {
1403 return core_group_external::delete_group_members_parameters();
1407 * Delete group members
1409 * @param array $members of arrays with keys userid, groupid
1410 * @since Moodle 2.0
1411 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1412 * @see core_group_external::delete_group_members()
1414 public static function delete_groupmembers($members) {
1415 return core_group_external::delete_group_members($members);
1419 * Returns description of method result value
1421 * @return external_description
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_returns()
1426 public static function delete_groupmembers_returns() {
1427 return core_group_external::delete_group_members_returns();