2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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");
30 * Group external functions
34 * @copyright 2011 Jerome Mouneyrac
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class core_group_external
extends external_api
{
41 * Returns description of method parameters
43 * @return external_function_parameters
46 public static function create_groups_parameters() {
47 return new external_function_parameters(
49 'groups' => new external_multiple_structure(
50 new external_single_structure(
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.'
67 * @param array $groups array of group description arrays (with keys groupname and courseid)
68 * @return array of newly created groups
71 public static function create_groups($groups) {
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();
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
);
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);
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();
120 * Returns description of method result value
122 * @return external_description
125 public static function create_groups_returns() {
126 return new external_multiple_structure(
127 new external_single_structure(
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
146 public static function get_groups_parameters() {
147 return new external_function_parameters(
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)
162 public static function get_groups($groupids) {
163 $params = self
::validate_parameters(self
::get_groups_parameters(), array('groupids'=>$groupids));
166 foreach ($params['groupids'] as $groupid) {
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
);
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;
193 * Returns description of method result value
195 * @return external_description
198 public static function get_groups_returns() {
199 return new external_multiple_structure(
200 new external_single_structure(
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
219 public static function get_course_groups_parameters() {
220 return new external_function_parameters(
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)
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
);
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');
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;
264 * Returns description of method result value
266 * @return external_description
269 public static function get_course_groups_returns() {
270 return new external_multiple_structure(
271 new external_single_structure(
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
290 public static function delete_groups_parameters() {
291 return new external_function_parameters(
293 'groupids' => new external_multiple_structure(new external_value(PARAM_INT
, 'Group ID')),
301 * @param array $groupids array of group ids
304 public static function delete_groups($groupids) {
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) {
314 $groupid = validate_param($groupid, PARAM_INT
);
315 if (!$group = groups_get_group($groupid, '*', IGNORE_MISSING
)) {
316 // silently ignore attempts to delete nonexisting groups
320 // now security checks
321 $context = context_course
::instance($group->courseid
, IGNORE_MISSING
);
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
344 public static function delete_groups_returns() {
350 * Returns description of method parameters
352 * @return external_function_parameters
355 public static function get_group_members_parameters() {
356 return new external_function_parameters(
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
370 public static function get_group_members($groupids) {
373 $params = self
::validate_parameters(self
::get_group_members_parameters(), array('groupids'=>$groupids));
375 foreach ($params['groupids'] as $groupid) {
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
);
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));
399 * Returns description of method result value
401 * @return external_description
404 public static function get_group_members_returns() {
405 return new external_multiple_structure(
406 new external_single_structure(
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
422 public static function add_group_members_parameters() {
423 return new external_function_parameters(
425 'members'=> new external_multiple_structure(
426 new external_single_structure(
428 'groupid' => new external_value(PARAM_INT
, 'group record id'),
429 'userid' => new external_value(PARAM_INT
, 'user id'),
440 * @param array $members of arrays with keys userid, groupid
443 public static function add_group_members($members) {
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) {
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
);
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
488 public static function add_group_members_returns() {
494 * Returns description of method parameters
496 * @return external_function_parameters
499 public static function delete_group_members_parameters() {
500 return new external_function_parameters(
502 'members'=> new external_multiple_structure(
503 new external_single_structure(
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
520 public static function delete_group_members($members) {
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) {
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
);
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
563 public static function delete_group_members_returns() {
568 * Returns description of method parameters
570 * @return external_function_parameters
573 public static function create_groupings_parameters() {
574 return new external_function_parameters(
576 'groupings' => new external_multiple_structure(
577 new external_single_structure(
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.'
593 * @param array $groupings array of grouping description arrays (with keys groupname and courseid)
594 * @return array of newly created groupings
597 public static function create_groupings($groupings) {
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
);
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();
642 * Returns description of method result value
644 * @return external_description
647 public static function create_groupings_returns() {
648 return new external_multiple_structure(
649 new external_single_structure(
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
667 public static function update_groupings_parameters() {
668 return new external_function_parameters(
670 'groupings' => new external_multiple_structure(
671 new external_single_structure(
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.'
687 * @param array $groupings array of grouping description arrays (with keys groupname and courseid)
688 * @return array of newly updated groupings
691 public static function update_groupings($groupings) {
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
);
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();
743 * Returns description of method result value
745 * @return external_description
748 public static function update_groupings_returns() {
753 * Returns description of method parameters
755 * @return external_function_parameters
758 public static function get_groupings_parameters() {
759 return new external_function_parameters(
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)
776 public static function get_groupings($groupingids, $returngroups = false) {
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) {
788 $grouping = groups_get_grouping($groupingid, '*', MUST_EXIST
);
790 // Now security checks.
791 $context = context_course
::instance($grouping->courseid
);
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));
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;
836 * Returns description of method result value
838 * @return external_description
841 public static function get_groupings_returns() {
842 return new external_multiple_structure(
843 new external_single_structure(
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(
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
873 public static function get_course_groupings_parameters() {
874 return new external_function_parameters(
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)
888 public static function get_course_groupings($courseid) {
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']);
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;
922 * Returns description of method result value
924 * @return external_description
927 public static function get_course_groupings_returns() {
928 return new external_multiple_structure(
929 new external_single_structure(
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
947 public static function delete_groupings_parameters() {
948 return new external_function_parameters(
950 'groupingids' => new external_multiple_structure(new external_value(PARAM_INT
, 'grouping ID')),
958 * @param array $groupingids array of grouping ids
962 public static function delete_groupings($groupingids) {
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.
977 // Now security checks.
978 $context = context_course
::instance($grouping->courseid
);
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
1001 public static function delete_groupings_returns() {
1006 * Returns description of method parameters
1008 * @return external_function_parameters
1011 public static function assign_grouping_parameters() {
1012 return new external_function_parameters(
1014 'assignments'=> new external_multiple_structure(
1015 new external_single_structure(
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
1033 public static function assign_grouping($assignments) {
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) {
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.
1053 // Now security checks.
1054 $context = context_course
::instance($grouping->courseid
);
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
1077 public static function assign_grouping_returns() {
1082 * Returns description of method parameters
1084 * @return external_function_parameters
1087 public static function unassign_grouping_parameters() {
1088 return new external_function_parameters(
1090 'unassignments'=> new external_multiple_structure(
1091 new external_single_structure(
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
1109 public static function unassign_grouping($unassignments) {
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) {
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.
1129 // Now security checks.
1130 $context = context_course
::instance($grouping->courseid
);
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
1153 public static function unassign_grouping_returns() {
1158 * Returns description of method parameters
1160 * @return external_function_parameters
1163 public static function get_course_user_groups_parameters() {
1164 return new external_function_parameters(
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.
1183 public static function get_course_user_groups($courseid, $userid, $groupingid = 0) {
1186 // Warnings array, it can be empty at the end but is mandatory.
1187 $warnings = 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
);
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;
1236 'groups' => $usergroups,
1237 'warnings' => $warnings
1243 * Returns description of method result value.
1245 * @return external_description A single structure containing groups and possible warnings.
1248 public static function get_course_user_groups_returns() {
1249 return new external_single_structure(
1251 'groups' => new external_multiple_structure(
1252 new external_single_structure(
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
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
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();
1295 * @param array $groups array of group description arrays (with keys groupname and courseid)
1296 * @return array of newly created groups
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
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.
1322 public static function create_groups_is_deprecated() {
1327 * Returns description of method parameters
1329 * @return external_function_parameters
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)
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
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.
1368 public static function get_groups_is_deprecated() {
1373 * Returns description of method parameters
1375 * @return external_function_parameters
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)
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
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.
1414 public static function get_course_groups_is_deprecated() {
1419 * Returns description of method parameters
1421 * @return external_function_parameters
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();
1433 * @param array $groupids array of group ids
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
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.
1459 public static function delete_groups_is_deprecated() {
1464 * Returns description of method parameters
1466 * @return external_function_parameters
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
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
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.
1505 public static function get_groupmembers_is_deprecated() {
1510 * Returns description of method parameters
1512 * @return external_function_parameters
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();
1524 * @param array $members of arrays with keys userid, groupid
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
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.
1550 public static function add_groupmembers_is_deprecated() {
1555 * Returns description of method parameters
1557 * @return external_function_parameters
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
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
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.
1595 public static function delete_groupmembers_is_deprecated() {