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'),
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 = get_context_instance(CONTEXT_COURSE
, $group->courseid
);
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 $groups[] = (array)$group;
111 $transaction->allow_commit();
117 * Returns description of method result value
119 * @return external_description
122 public static function create_groups_returns() {
123 return new external_multiple_structure(
124 new external_single_structure(
126 'id' => new external_value(PARAM_INT
, 'group record id'),
127 'courseid' => new external_value(PARAM_INT
, 'id of course'),
128 'name' => new external_value(PARAM_TEXT
, 'multilang compatible name, course unique'),
129 'description' => new external_value(PARAM_RAW
, 'group description text'),
130 'descriptionformat' => new external_format_value('description'),
131 'enrolmentkey' => new external_value(PARAM_RAW
, 'group enrol secret phrase'),
133 ), 'List of group object. A group has an id, a courseid, a name, a description and an enrolment key.'
138 * Returns description of method parameters
140 * @return external_function_parameters
143 public static function get_groups_parameters() {
144 return new external_function_parameters(
146 'groupids' => new external_multiple_structure(new external_value(PARAM_INT
, 'Group ID')
147 ,'List of group id. A group id is an integer.'),
153 * Get groups definition specified by ids
155 * @param array $groupids arrays of group ids
156 * @return array of group objects (id, courseid, name, enrolmentkey)
159 public static function get_groups($groupids) {
160 $params = self
::validate_parameters(self
::get_groups_parameters(), array('groupids'=>$groupids));
163 foreach ($params['groupids'] as $groupid) {
165 $group = groups_get_group($groupid, 'id, courseid, name, description, descriptionformat, enrolmentkey', MUST_EXIST
);
167 // now security checks
168 $context = get_context_instance(CONTEXT_COURSE
, $group->courseid
);
170 self
::validate_context($context);
171 } catch (Exception
$e) {
172 $exceptionparam = new stdClass();
173 $exceptionparam->message
= $e->getMessage();
174 $exceptionparam->courseid
= $group->courseid
;
175 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
177 require_capability('moodle/course:managegroups', $context);
179 list($group->description
, $group->descriptionformat
) =
180 external_format_text($group->description
, $group->descriptionformat
,
181 $context->id
, 'group', 'description', $group->id
);
183 $groups[] = (array)$group;
190 * Returns description of method result value
192 * @return external_description
195 public static function get_groups_returns() {
196 return new external_multiple_structure(
197 new external_single_structure(
199 'id' => new external_value(PARAM_INT
, 'group record id'),
200 'courseid' => new external_value(PARAM_INT
, 'id of course'),
201 'name' => new external_value(PARAM_TEXT
, 'multilang compatible name, course unique'),
202 'description' => new external_value(PARAM_RAW
, 'group description text'),
203 'descriptionformat' => new external_format_value('description'),
204 'enrolmentkey' => new external_value(PARAM_RAW
, 'group enrol secret phrase'),
211 * Returns description of method parameters
213 * @return external_function_parameters
216 public static function get_course_groups_parameters() {
217 return new external_function_parameters(
219 'courseid' => new external_value(PARAM_INT
, 'id of course'),
225 * Get all groups in the specified course
227 * @param int $courseid id of course
228 * @return array of group objects (id, courseid, name, enrolmentkey)
231 public static function get_course_groups($courseid) {
232 $params = self
::validate_parameters(self
::get_course_groups_parameters(), array('courseid'=>$courseid));
234 // now security checks
235 $context = get_context_instance(CONTEXT_COURSE
, $params['courseid']);
237 self
::validate_context($context);
238 } catch (Exception
$e) {
239 $exceptionparam = new stdClass();
240 $exceptionparam->message
= $e->getMessage();
241 $exceptionparam->courseid
= $params['courseid'];
242 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
244 require_capability('moodle/course:managegroups', $context);
246 $gs = groups_get_all_groups($params['courseid'], 0, 0,
247 'g.id, g.courseid, g.name, g.description, g.descriptionformat, g.enrolmentkey');
250 foreach ($gs as $group) {
251 list($group->description
, $group->descriptionformat
) =
252 external_format_text($group->description
, $group->descriptionformat
,
253 $context->id
, 'group', 'description', $group->id
);
254 $groups[] = (array)$group;
261 * Returns description of method result value
263 * @return external_description
266 public static function get_course_groups_returns() {
267 return new external_multiple_structure(
268 new external_single_structure(
270 'id' => new external_value(PARAM_INT
, 'group record id'),
271 'courseid' => new external_value(PARAM_INT
, 'id of course'),
272 'name' => new external_value(PARAM_TEXT
, 'multilang compatible name, course unique'),
273 'description' => new external_value(PARAM_RAW
, 'group description text'),
274 'descriptionformat' => new external_format_value('description'),
275 'enrolmentkey' => new external_value(PARAM_RAW
, 'group enrol secret phrase'),
282 * Returns description of method parameters
284 * @return external_function_parameters
287 public static function delete_groups_parameters() {
288 return new external_function_parameters(
290 'groupids' => new external_multiple_structure(new external_value(PARAM_INT
, 'Group ID')),
298 * @param array $groupids array of group ids
301 public static function delete_groups($groupids) {
303 require_once("$CFG->dirroot/group/lib.php");
305 $params = self
::validate_parameters(self
::delete_groups_parameters(), array('groupids'=>$groupids));
307 $transaction = $DB->start_delegated_transaction();
309 foreach ($params['groupids'] as $groupid) {
311 $groupid = validate_param($groupid, PARAM_INTEGER
);
312 if (!$group = groups_get_group($groupid, 'id, courseid', IGNORE_MISSING
)) {
313 // silently ignore attempts to delete nonexisting groups
317 // now security checks
318 $context = get_context_instance(CONTEXT_COURSE
, $group->courseid
);
320 self
::validate_context($context);
321 } catch (Exception
$e) {
322 $exceptionparam = new stdClass();
323 $exceptionparam->message
= $e->getMessage();
324 $exceptionparam->courseid
= $group->courseid
;
325 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
327 require_capability('moodle/course:managegroups', $context);
329 groups_delete_group($group);
332 $transaction->allow_commit();
336 * Returns description of method result value
341 public static function delete_groups_returns() {
347 * Returns description of method parameters
349 * @return external_function_parameters
352 public static function get_group_members_parameters() {
353 return new external_function_parameters(
355 'groupids' => new external_multiple_structure(new external_value(PARAM_INT
, 'Group ID')),
361 * Return all members for a group
363 * @param array $groupids array of group ids
364 * @return array with group id keys containing arrays of user ids
367 public static function get_group_members($groupids) {
370 $params = self
::validate_parameters(self
::get_group_members_parameters(), array('groupids'=>$groupids));
372 foreach ($params['groupids'] as $groupid) {
374 $group = groups_get_group($groupid, 'id, courseid, name, enrolmentkey', MUST_EXIST
);
375 // now security checks
376 $context = get_context_instance(CONTEXT_COURSE
, $group->courseid
);
378 self
::validate_context($context);
379 } catch (Exception
$e) {
380 $exceptionparam = new stdClass();
381 $exceptionparam->message
= $e->getMessage();
382 $exceptionparam->courseid
= $group->courseid
;
383 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
385 require_capability('moodle/course:managegroups', $context);
387 $groupmembers = groups_get_members($group->id
, 'u.id', 'lastname ASC, firstname ASC');
389 $members[] = array('groupid'=>$groupid, 'userids'=>array_keys($groupmembers));
396 * Returns description of method result value
398 * @return external_description
401 public static function get_group_members_returns() {
402 return new external_multiple_structure(
403 new external_single_structure(
405 'groupid' => new external_value(PARAM_INT
, 'group record id'),
406 'userids' => new external_multiple_structure(new external_value(PARAM_INT
, 'user id')),
414 * Returns description of method parameters
416 * @return external_function_parameters
419 public static function add_group_members_parameters() {
420 return new external_function_parameters(
422 'members'=> new external_multiple_structure(
423 new external_single_structure(
425 'groupid' => new external_value(PARAM_INT
, 'group record id'),
426 'userid' => new external_value(PARAM_INT
, 'user id'),
437 * @param array $members of arrays with keys userid, groupid
440 public static function add_group_members($members) {
442 require_once("$CFG->dirroot/group/lib.php");
444 $params = self
::validate_parameters(self
::add_group_members_parameters(), array('members'=>$members));
446 $transaction = $DB->start_delegated_transaction();
447 foreach ($params['members'] as $member) {
449 $groupid = $member['groupid'];
450 $userid = $member['userid'];
452 $group = groups_get_group($groupid, 'id, courseid', MUST_EXIST
);
453 $user = $DB->get_record('user', array('id'=>$userid, 'deleted'=>0, 'mnethostid'=>$CFG->mnet_localhost_id
), '*', MUST_EXIST
);
455 // now security checks
456 $context = get_context_instance(CONTEXT_COURSE
, $group->courseid
);
458 self
::validate_context($context);
459 } catch (Exception
$e) {
460 $exceptionparam = new stdClass();
461 $exceptionparam->message
= $e->getMessage();
462 $exceptionparam->courseid
= $group->courseid
;
463 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
465 require_capability('moodle/course:managegroups', $context);
467 // now make sure user is enrolled in course - this is mandatory requirement,
468 // unfortunately this is slow
469 if (!is_enrolled($context, $userid)) {
470 throw new invalid_parameter_exception('Only enrolled users may be members of groups');
473 groups_add_member($group, $user);
476 $transaction->allow_commit();
480 * Returns description of method result value
485 public static function add_group_members_returns() {
491 * Returns description of method parameters
493 * @return external_function_parameters
496 public static function delete_group_members_parameters() {
497 return new external_function_parameters(
499 'members'=> new external_multiple_structure(
500 new external_single_structure(
502 'groupid' => new external_value(PARAM_INT
, 'group record id'),
503 'userid' => new external_value(PARAM_INT
, 'user id'),
512 * Delete group members
514 * @param array $members of arrays with keys userid, groupid
517 public static function delete_group_members($members) {
519 require_once("$CFG->dirroot/group/lib.php");
521 $params = self
::validate_parameters(self
::delete_group_members_parameters(), array('members'=>$members));
523 $transaction = $DB->start_delegated_transaction();
525 foreach ($params['members'] as $member) {
527 $groupid = $member['groupid'];
528 $userid = $member['userid'];
530 $group = groups_get_group($groupid, 'id, courseid', MUST_EXIST
);
531 $user = $DB->get_record('user', array('id'=>$userid, 'deleted'=>0, 'mnethostid'=>$CFG->mnet_localhost_id
), '*', MUST_EXIST
);
533 // now security checks
534 $context = get_context_instance(CONTEXT_COURSE
, $group->courseid
);
536 self
::validate_context($context);
537 } catch (Exception
$e) {
538 $exceptionparam = new stdClass();
539 $exceptionparam->message
= $e->getMessage();
540 $exceptionparam->courseid
= $group->courseid
;
541 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
543 require_capability('moodle/course:managegroups', $context);
545 groups_remove_member($group, $user);
548 $transaction->allow_commit();
552 * Returns description of method result value
557 public static function delete_group_members_returns() {
562 * Returns description of method parameters
564 * @return external_function_parameters
567 public static function create_groupings_parameters() {
568 return new external_function_parameters(
570 'groupings' => new external_multiple_structure(
571 new external_single_structure(
573 'courseid' => new external_value(PARAM_INT
, 'id of course'),
574 'name' => new external_value(PARAM_TEXT
, 'multilang compatible name, course unique'),
575 'description' => new external_value(PARAM_RAW
, 'grouping description text'),
576 'descriptionformat' => new external_format_value('descripiton', VALUE_DEFAULT
)
578 ), 'List of grouping object. A grouping has a courseid, a name and a description.'
587 * @param array $groupings array of grouping description arrays (with keys groupname and courseid)
588 * @return array of newly created groupings
591 public static function create_groupings($groupings) {
593 require_once("$CFG->dirroot/group/lib.php");
595 $params = self
::validate_parameters(self
::create_groupings_parameters(), array('groupings'=>$groupings));
597 $transaction = $DB->start_delegated_transaction();
599 $groupings = array();
601 foreach ($params['groupings'] as $grouping) {
602 $grouping = (object)$grouping;
604 if (trim($grouping->name
) == '') {
605 throw new invalid_parameter_exception('Invalid grouping name');
607 if ($DB->count_records('groupings', array('courseid'=>$grouping->courseid
, 'name'=>$grouping->name
))) {
608 throw new invalid_parameter_exception('Grouping with the same name already exists in the course');
611 // Now security checks .
612 $context = context_course
::instance($grouping->courseid
);
614 self
::validate_context($context);
615 } catch (Exception
$e) {
616 $exceptionparam = new stdClass();
617 $exceptionparam->message
= $e->getMessage();
618 $exceptionparam->courseid
= $grouping->courseid
;
619 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
621 require_capability('moodle/course:managegroups', $context);
623 $grouping->descriptionformat
= external_validate_format($grouping->descriptionformat
);
625 // Finally create the grouping.
626 $grouping->id
= groups_create_grouping($grouping);
627 $groupings[] = (array)$grouping;
630 $transaction->allow_commit();
636 * Returns description of method result value
638 * @return external_description
641 public static function create_groupings_returns() {
642 return new external_multiple_structure(
643 new external_single_structure(
645 'id' => new external_value(PARAM_INT
, 'grouping record id'),
646 'courseid' => new external_value(PARAM_INT
, 'id of course'),
647 'name' => new external_value(PARAM_TEXT
, 'multilang compatible name, course unique'),
648 'description' => new external_value(PARAM_RAW
, 'grouping description text'),
649 'descriptionformat' => new external_format_value('description')
651 ), 'List of grouping object. A grouping has an id, a courseid, a name and a description.'
656 * Returns description of method parameters
658 * @return external_function_parameters
661 public static function update_groupings_parameters() {
662 return new external_function_parameters(
664 'groupings' => new external_multiple_structure(
665 new external_single_structure(
667 'id' => new external_value(PARAM_INT
, 'id of grouping'),
668 'name' => new external_value(PARAM_TEXT
, 'multilang compatible name, course unique'),
669 'description' => new external_value(PARAM_RAW
, 'grouping description text'),
670 'descriptionformat' => new external_format_value('description', VALUE_DEFAULT
)
672 ), 'List of grouping object. A grouping has a courseid, a name and a description.'
681 * @param array $groupings array of grouping description arrays (with keys groupname and courseid)
682 * @return array of newly updated groupings
685 public static function update_groupings($groupings) {
687 require_once("$CFG->dirroot/group/lib.php");
689 $params = self
::validate_parameters(self
::update_groupings_parameters(), array('groupings'=>$groupings));
691 $transaction = $DB->start_delegated_transaction();
693 foreach ($params['groupings'] as $grouping) {
694 $grouping = (object)$grouping;
696 if (trim($grouping->name
) == '') {
697 throw new invalid_parameter_exception('Invalid grouping name');
700 if (! $currentgrouping = $DB->get_record('groupings', array('id'=>$grouping->id
))) {
701 throw new invalid_parameter_exception("Grouping $grouping->id does not exist in the course");
704 // Check if the new modified grouping name already exists in the course.
705 if ($grouping->name
!= $currentgrouping->name
and
706 $DB->count_records('groupings', array('courseid'=>$currentgrouping->courseid
, 'name'=>$grouping->name
))) {
707 throw new invalid_parameter_exception('A different grouping with the same name already exists in the course');
710 $grouping->courseid
= $currentgrouping->courseid
;
712 // Now security checks.
713 $context = context_course
::instance($grouping->courseid
);
715 self
::validate_context($context);
716 } catch (Exception
$e) {
717 $exceptionparam = new stdClass();
718 $exceptionparam->message
= $e->getMessage();
719 $exceptionparam->courseid
= $grouping->courseid
;
720 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
722 require_capability('moodle/course:managegroups', $context);
724 // We must force allways FORMAT_HTML.
725 $grouping->descriptionformat
= external_validate_format($grouping->descriptionformat
);
727 // Finally update the grouping.
728 groups_update_grouping($grouping);
731 $transaction->allow_commit();
737 * Returns description of method result value
739 * @return external_description
742 public static function update_groupings_returns() {
747 * Returns description of method parameters
749 * @return external_function_parameters
752 public static function get_groupings_parameters() {
753 return new external_function_parameters(
755 'groupingids' => new external_multiple_structure(new external_value(PARAM_INT
, 'grouping ID')
756 , 'List of grouping id. A grouping id is an integer.'),
762 * Get groupings definition specified by ids
764 * @param array $groupingids arrays of grouping ids
765 * @return array of grouping objects (id, courseid, name)
768 public static function get_groupings($groupingids) {
770 require_once("$CFG->dirroot/group/lib.php");
771 require_once("$CFG->libdir/filelib.php");
773 $params = self
::validate_parameters(self
::get_groupings_parameters(), array('groupingids'=>$groupingids));
775 $groupings = array();
776 foreach ($params['groupingids'] as $groupingid) {
778 $grouping = groups_get_grouping($groupingid, '*', MUST_EXIST
);
780 // Now security checks.
781 $context = context_course
::instance($grouping->courseid
);
783 self
::validate_context($context);
784 } catch (Exception
$e) {
785 $exceptionparam = new stdClass();
786 $exceptionparam->message
= $e->getMessage();
787 $exceptionparam->courseid
= $grouping->courseid
;
788 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
790 require_capability('moodle/course:managegroups', $context);
792 list($grouping->description
, $grouping->descriptionformat
) =
793 external_format_text($grouping->description
, $grouping->descriptionformat
,
794 $context->id
, 'grouping', 'description', $grouping->id
);
796 $groupings[] = (array)$grouping;
803 * Returns description of method result value
805 * @return external_description
808 public static function get_groupings_returns() {
809 return new external_multiple_structure(
810 new external_single_structure(
812 'id' => new external_value(PARAM_INT
, 'grouping record id'),
813 'courseid' => new external_value(PARAM_INT
, 'id of course'),
814 'name' => new external_value(PARAM_TEXT
, 'multilang compatible name, course unique'),
815 'description' => new external_value(PARAM_RAW
, 'grouping description text'),
816 'descriptionformat' => new external_format_value('description')
823 * Returns description of method parameters
825 * @return external_function_parameters
828 public static function get_course_groupings_parameters() {
829 return new external_function_parameters(
831 'courseid' => new external_value(PARAM_INT
, 'id of course'),
837 * Get all groupings in the specified course
839 * @param int $courseid id of course
840 * @return array of grouping objects (id, courseid, name, enrolmentkey)
843 public static function get_course_groupings($courseid) {
845 require_once("$CFG->dirroot/group/lib.php");
846 require_once("$CFG->libdir/filelib.php");
848 $params = self
::validate_parameters(self
::get_course_groupings_parameters(), array('courseid'=>$courseid));
850 // Now security checks.
851 $context = context_course
::instance($params['courseid']);
854 self
::validate_context($context);
855 } catch (Exception
$e) {
856 $exceptionparam = new stdClass();
857 $exceptionparam->message
= $e->getMessage();
858 $exceptionparam->courseid
= $params['courseid'];
859 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
861 require_capability('moodle/course:managegroups', $context);
863 $gs = groups_get_all_groupings($params['courseid']);
865 $groupings = array();
866 foreach ($gs as $grouping) {
867 list($grouping->description
, $grouping->descriptionformat
) =
868 external_format_text($grouping->description
, $grouping->descriptionformat
,
869 $context->id
, 'grouping', 'description', $grouping->id
);
870 $groupings[] = (array)$grouping;
877 * Returns description of method result value
879 * @return external_description
882 public static function get_course_groupings_returns() {
883 return new external_multiple_structure(
884 new external_single_structure(
886 'id' => new external_value(PARAM_INT
, 'grouping record id'),
887 'courseid' => new external_value(PARAM_INT
, 'id of course'),
888 'name' => new external_value(PARAM_TEXT
, 'multilang compatible name, course unique'),
889 'description' => new external_value(PARAM_RAW
, 'grouping description text'),
890 'descriptionformat' => new external_format_value('description')
897 * Returns description of method parameters
899 * @return external_function_parameters
902 public static function delete_groupings_parameters() {
903 return new external_function_parameters(
905 'groupingids' => new external_multiple_structure(new external_value(PARAM_INT
, 'grouping ID')),
913 * @param array $groupingids array of grouping ids
917 public static function delete_groupings($groupingids) {
919 require_once("$CFG->dirroot/group/lib.php");
921 $params = self
::validate_parameters(self
::delete_groupings_parameters(), array('groupingids'=>$groupingids));
923 $transaction = $DB->start_delegated_transaction();
925 foreach ($params['groupingids'] as $groupingid) {
927 if (!$grouping = groups_get_grouping($groupingid, 'id, courseid', IGNORE_MISSING
)) {
928 // Silently ignore attempts to delete nonexisting groupings.
932 // Now security checks.
933 $context = context_course
::instance($grouping->courseid
);
935 self
::validate_context($context);
936 } catch (Exception
$e) {
937 $exceptionparam = new stdClass();
938 $exceptionparam->message
= $e->getMessage();
939 $exceptionparam->courseid
= $grouping->courseid
;
940 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
942 require_capability('moodle/course:managegroups', $context);
944 groups_delete_grouping($grouping);
947 $transaction->allow_commit();
951 * Returns description of method result value
953 * @return external_description
956 public static function delete_groupings_returns() {
961 * Returns description of method parameters
963 * @return external_function_parameters
966 public static function assign_grouping_parameters() {
967 return new external_function_parameters(
969 'assignments'=> new external_multiple_structure(
970 new external_single_structure(
972 'groupingid' => new external_value(PARAM_INT
, 'grouping record id'),
973 'groupid' => new external_value(PARAM_INT
, 'group record id'),
982 * Assign a group to a grouping
984 * @param array $assignments of arrays with keys groupid, groupingid
988 public static function assign_grouping($assignments) {
990 require_once("$CFG->dirroot/group/lib.php");
992 $params = self
::validate_parameters(self
::assign_grouping_parameters(), array('assignments'=>$assignments));
994 $transaction = $DB->start_delegated_transaction();
995 foreach ($params['assignments'] as $assignment) {
997 $groupingid = $assignment['groupingid'];
998 $groupid = $assignment['groupid'];
1000 $grouping = groups_get_grouping($groupingid, 'id, courseid', MUST_EXIST
);
1001 $group = groups_get_group($groupid, 'id, courseid', MUST_EXIST
);
1003 if ($DB->record_exists('groupings_groups', array('groupingid'=>$groupingid, 'groupid'=>$groupid))) {
1004 // Continue silently if the group is yet assigned to the grouping.
1008 // Now security checks.
1009 $context = context_course
::instance($grouping->courseid
);
1011 self
::validate_context($context);
1012 } catch (Exception
$e) {
1013 $exceptionparam = new stdClass();
1014 $exceptionparam->message
= $e->getMessage();
1015 $exceptionparam->courseid
= $group->courseid
;
1016 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
1018 require_capability('moodle/course:managegroups', $context);
1020 groups_assign_grouping($groupingid, $groupid);
1023 $transaction->allow_commit();
1027 * Returns description of method result value
1032 public static function assign_grouping_returns() {
1037 * Returns description of method parameters
1039 * @return external_function_parameters
1042 public static function unassign_grouping_parameters() {
1043 return new external_function_parameters(
1045 'unassignments'=> new external_multiple_structure(
1046 new external_single_structure(
1048 'groupingid' => new external_value(PARAM_INT
, 'grouping record id'),
1049 'groupid' => new external_value(PARAM_INT
, 'group record id'),
1058 * Unassign a group from a grouping
1060 * @param array $unassignments of arrays with keys groupid, groupingid
1064 public static function unassign_grouping($unassignments) {
1066 require_once("$CFG->dirroot/group/lib.php");
1068 $params = self
::validate_parameters(self
::unassign_grouping_parameters(), array('unassignments'=>$unassignments));
1070 $transaction = $DB->start_delegated_transaction();
1071 foreach ($params['unassignments'] as $unassignment) {
1073 $groupingid = $unassignment['groupingid'];
1074 $groupid = $unassignment['groupid'];
1076 $grouping = groups_get_grouping($groupingid, 'id, courseid', MUST_EXIST
);
1077 $group = groups_get_group($groupid, 'id, courseid', MUST_EXIST
);
1079 if (!$DB->record_exists('groupings_groups', array('groupingid'=>$groupingid, 'groupid'=>$groupid))) {
1080 // Continue silently if the group is not assigned to the grouping.
1084 // Now security checks.
1085 $context = context_course
::instance($grouping->courseid
);
1087 self
::validate_context($context);
1088 } catch (Exception
$e) {
1089 $exceptionparam = new stdClass();
1090 $exceptionparam->message
= $e->getMessage();
1091 $exceptionparam->courseid
= $group->courseid
;
1092 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
1094 require_capability('moodle/course:managegroups', $context);
1096 groups_unassign_grouping($groupingid, $groupid);
1099 $transaction->allow_commit();
1103 * Returns description of method result value
1108 public static function unassign_grouping_returns() {
1115 * Deprecated group external functions
1117 * @package core_group
1118 * @copyright 2009 Petr Skodak
1119 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1121 * @deprecated Moodle 2.2 MDL-29106 - Please do not use this class any more.
1122 * @todo MDL-31194 This will be deleted in Moodle 2.5.
1123 * @see core_group_external
1125 class moodle_group_external
extends external_api
{
1128 * Returns description of method parameters
1130 * @return external_function_parameters
1132 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1133 * @todo MDL-31194 This will be deleted in Moodle 2.5.
1134 * @see core_group_external::create_groups_parameters()
1136 public static function create_groups_parameters() {
1137 return core_group_external
::create_groups_parameters();
1143 * @param array $groups array of group description arrays (with keys groupname and courseid)
1144 * @return array of newly created groups
1146 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1147 * @todo MDL-31194 This will be deleted in Moodle 2.5.
1148 * @see use core_group_external::create_groups()
1150 public static function create_groups($groups) {
1151 return core_group_external
::create_groups($groups);
1155 * Returns description of method result value
1157 * @return external_description
1159 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1160 * @todo MDL-31194 This will be deleted in Moodle 2.5.
1161 * @see core_group_external::create_groups_returns()
1163 public static function create_groups_returns() {
1164 return core_group_external
::create_groups_returns();
1168 * Returns description of method parameters
1170 * @return external_function_parameters
1172 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1173 * @todo MDL-31194 This will be deleted in Moodle 2.5.
1174 * @see core_group_external::get_groups_parameters()
1176 public static function get_groups_parameters() {
1177 return core_group_external
::get_groups_parameters();
1181 * Get groups definition specified by ids
1183 * @param array $groupids arrays of group ids
1184 * @return array of group objects (id, courseid, name, enrolmentkey)
1186 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1187 * @todo MDL-31194 This will be deleted in Moodle 2.5.
1188 * @see core_group_external::get_groups()
1190 public static function get_groups($groupids) {
1191 return core_group_external
::get_groups($groupids);
1195 * Returns description of method result value
1197 * @return external_description
1199 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1200 * @todo MDL-31194 This will be deleted in Moodle 2.5.
1201 * @see core_group_external::get_groups_returns()
1203 public static function get_groups_returns() {
1204 return core_group_external
::get_groups_returns();
1208 * Returns description of method parameters
1210 * @return external_function_parameters
1212 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1213 * @todo MDL-31194 This will be deleted in Moodle 2.5.
1214 * @see core_group_external::get_course_groups_parameters()
1216 public static function get_course_groups_parameters() {
1217 return core_group_external
::get_course_groups_parameters();
1221 * Get all groups in the specified course
1223 * @param int $courseid id of course
1224 * @return array of group objects (id, courseid, name, enrolmentkey)
1226 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1227 * @todo MDL-31194 This will be deleted in Moodle 2.5.
1228 * @see core_group_external::get_course_groups()
1230 public static function get_course_groups($courseid) {
1231 return core_group_external
::get_course_groups($courseid);
1235 * Returns description of method result value
1237 * @return external_description
1239 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1240 * @todo MDL-31194 This will be deleted in Moodle 2.5.
1241 * @see core_group_external::get_course_groups_returns()
1243 public static function get_course_groups_returns() {
1244 return core_group_external
::get_course_groups_returns();
1248 * Returns description of method parameters
1250 * @return external_function_parameters
1252 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1253 * @todo MDL-31194 This will be deleted in Moodle 2.5.
1254 * @see core_group_external::delete_group_members_parameters()
1256 public static function delete_groups_parameters() {
1257 return core_group_external
::delete_group_members_parameters();
1263 * @param array $groupids array of group ids
1265 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1266 * @todo MDL-31194 This will be deleted in Moodle 2.5.
1267 * @see core_group_external::delete_groups()
1269 public static function delete_groups($groupids) {
1270 return core_group_external
::delete_groups($groupids);
1274 * Returns description of method result value
1278 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1279 * @todo MDL-31194 This will be deleted in Moodle 2.5.
1280 * @see core_group_external::delete_group_members_returns()
1282 public static function delete_groups_returns() {
1283 return core_group_external
::delete_group_members_returns();
1288 * Returns description of method parameters
1290 * @return external_function_parameters
1292 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1293 * @todo MDL-31194 This will be deleted in Moodle 2.5.
1294 * @see core_group_external::get_group_members_parameters()
1296 public static function get_groupmembers_parameters() {
1297 return core_group_external
::get_group_members_parameters();
1301 * Return all members for a group
1303 * @param array $groupids array of group ids
1304 * @return array with group id keys containing arrays of user ids
1306 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1307 * @todo MDL-31194 This will be deleted in Moodle 2.5.
1308 * @see core_group_external::get_group_members()
1310 public static function get_groupmembers($groupids) {
1311 return core_group_external
::get_group_members($groupids);
1315 * Returns description of method result value
1317 * @return external_description
1319 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1320 * @todo MDL-31194 This will be deleted in Moodle 2.5.
1321 * @see core_group_external::get_group_members_returns()
1323 public static function get_groupmembers_returns() {
1324 return core_group_external
::get_group_members_returns();
1329 * Returns description of method parameters
1331 * @return external_function_parameters
1333 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1334 * @todo MDL-31194 This will be deleted in Moodle 2.5.
1335 * @see core_group_external::add_group_members_parameters()
1337 public static function add_groupmembers_parameters() {
1338 return core_group_external
::add_group_members_parameters();
1344 * @param array $members of arrays with keys userid, groupid
1346 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1347 * @todo MDL-31194 This will be deleted in Moodle 2.5.
1348 * @see use core_group_external::add_group_members()
1350 public static function add_groupmembers($members) {
1351 return core_group_external
::add_group_members($members);
1355 * Returns description of method result value
1357 * @return external_description
1359 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1360 * @todo MDL-31194 This will be deleted in Moodle 2.5.
1361 * @see core_group_external::add_group_members_returns()
1363 public static function add_groupmembers_returns() {
1364 return core_group_external
::add_group_members_returns();
1369 * Returns description of method parameters
1371 * @return external_function_parameters
1373 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1374 * @todo MDL-31194 This will be deleted in Moodle 2.5.
1375 * @see core_group_external::delete_group_members_parameters()
1377 public static function delete_groupmembers_parameters() {
1378 return core_group_external
::delete_group_members_parameters();
1382 * Delete group members
1384 * @param array $members of arrays with keys userid, groupid
1386 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1387 * @todo MDL-31194 This will be deleted in Moodle 2.5.
1388 * @see core_group_external::delete_group_members()
1390 public static function delete_groupmembers($members) {
1391 return core_group_external
::delete_group_members($members);
1395 * Returns description of method result value
1397 * @return external_description
1399 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1400 * @todo MDL-31194 This will be deleted in Moodle 2.5.
1401 * @see core_group_external::delete_group_members_returns()
1403 public static function delete_groupmembers_returns() {
1404 return core_group_external
::delete_group_members_returns();