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 defined('MOODLE_INTERNAL') ||
die();
29 require_once("$CFG->libdir/externallib.php");
32 * Group external functions
36 * @copyright 2011 Jerome Mouneyrac
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class core_group_external
extends external_api
{
43 * Returns description of method parameters
45 * @return external_function_parameters
48 public static function create_groups_parameters() {
49 return new external_function_parameters(
51 'groups' => new external_multiple_structure(
52 new external_single_structure(
54 'courseid' => new external_value(PARAM_INT
, 'id of course'),
55 'name' => new external_value(PARAM_TEXT
, 'multilang compatible name, course unique'),
56 'description' => new external_value(PARAM_RAW
, 'group description text'),
57 'descriptionformat' => new external_format_value('description', VALUE_DEFAULT
),
58 'enrolmentkey' => new external_value(PARAM_RAW
, 'group enrol secret phrase', VALUE_OPTIONAL
),
59 'idnumber' => new external_value(PARAM_RAW
, 'id number', VALUE_OPTIONAL
)
61 ), 'List of group object. A group has a courseid, a name, a description and an enrolment key.'
70 * @param array $groups array of group description arrays (with keys groupname and courseid)
71 * @return array of newly created groups
74 public static function create_groups($groups) {
76 require_once("$CFG->dirroot/group/lib.php");
78 $params = self
::validate_parameters(self
::create_groups_parameters(), array('groups'=>$groups));
80 $transaction = $DB->start_delegated_transaction();
84 foreach ($params['groups'] as $group) {
85 $group = (object)$group;
87 if (trim($group->name
) == '') {
88 throw new invalid_parameter_exception('Invalid group name');
90 if ($DB->get_record('groups', array('courseid'=>$group->courseid
, 'name'=>$group->name
))) {
91 throw new invalid_parameter_exception('Group with the same name already exists in the course');
94 // now security checks
95 $context = context_course
::instance($group->courseid
, IGNORE_MISSING
);
97 self
::validate_context($context);
98 } catch (Exception
$e) {
99 $exceptionparam = new stdClass();
100 $exceptionparam->message
= $e->getMessage();
101 $exceptionparam->courseid
= $group->courseid
;
102 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
104 require_capability('moodle/course:managegroups', $context);
107 $group->descriptionformat
= external_validate_format($group->descriptionformat
);
109 // finally create the group
110 $group->id
= groups_create_group($group, false);
111 if (!isset($group->enrolmentkey
)) {
112 $group->enrolmentkey
= '';
114 if (!isset($group->idnumber
)) {
115 $group->idnumber
= '';
118 $groups[] = (array)$group;
121 $transaction->allow_commit();
127 * Returns description of method result value
129 * @return external_description
132 public static function create_groups_returns() {
133 return new external_multiple_structure(
134 new external_single_structure(
136 'id' => new external_value(PARAM_INT
, 'group record id'),
137 'courseid' => new external_value(PARAM_INT
, 'id of course'),
138 'name' => new external_value(PARAM_TEXT
, 'multilang compatible name, course unique'),
139 'description' => new external_value(PARAM_RAW
, 'group description text'),
140 'descriptionformat' => new external_format_value('description'),
141 'enrolmentkey' => new external_value(PARAM_RAW
, 'group enrol secret phrase'),
142 'idnumber' => new external_value(PARAM_RAW
, 'id number')
144 ), 'List of group object. A group has an id, a courseid, a name, a description and an enrolment key.'
149 * Returns description of method parameters
151 * @return external_function_parameters
154 public static function get_groups_parameters() {
155 return new external_function_parameters(
157 'groupids' => new external_multiple_structure(new external_value(PARAM_INT
, 'Group ID')
158 ,'List of group id. A group id is an integer.'),
164 * Get groups definition specified by ids
166 * @param array $groupids arrays of group ids
167 * @return array of group objects (id, courseid, name, enrolmentkey)
170 public static function get_groups($groupids) {
171 $params = self
::validate_parameters(self
::get_groups_parameters(), array('groupids'=>$groupids));
174 foreach ($params['groupids'] as $groupid) {
176 $group = groups_get_group($groupid, 'id, courseid, name, idnumber, description, descriptionformat, enrolmentkey', MUST_EXIST
);
178 // now security checks
179 $context = context_course
::instance($group->courseid
, IGNORE_MISSING
);
181 self
::validate_context($context);
182 } catch (Exception
$e) {
183 $exceptionparam = new stdClass();
184 $exceptionparam->message
= $e->getMessage();
185 $exceptionparam->courseid
= $group->courseid
;
186 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
188 require_capability('moodle/course:managegroups', $context);
190 list($group->description
, $group->descriptionformat
) =
191 external_format_text($group->description
, $group->descriptionformat
,
192 $context->id
, 'group', 'description', $group->id
);
194 $groups[] = (array)$group;
201 * Returns description of method result value
203 * @return external_description
206 public static function get_groups_returns() {
207 return new external_multiple_structure(
208 new external_single_structure(
210 'id' => new external_value(PARAM_INT
, 'group record id'),
211 'courseid' => new external_value(PARAM_INT
, 'id of course'),
212 'name' => new external_value(PARAM_TEXT
, 'multilang compatible name, course unique'),
213 'description' => new external_value(PARAM_RAW
, 'group description text'),
214 'descriptionformat' => new external_format_value('description'),
215 'enrolmentkey' => new external_value(PARAM_RAW
, 'group enrol secret phrase'),
216 'idnumber' => new external_value(PARAM_RAW
, 'id number')
223 * Returns description of method parameters
225 * @return external_function_parameters
228 public static function get_course_groups_parameters() {
229 return new external_function_parameters(
231 'courseid' => new external_value(PARAM_INT
, 'id of course'),
237 * Get all groups in the specified course
239 * @param int $courseid id of course
240 * @return array of group objects (id, courseid, name, enrolmentkey)
243 public static function get_course_groups($courseid) {
244 $params = self
::validate_parameters(self
::get_course_groups_parameters(), array('courseid'=>$courseid));
246 // now security checks
247 $context = context_course
::instance($params['courseid'], IGNORE_MISSING
);
249 self
::validate_context($context);
250 } catch (Exception
$e) {
251 $exceptionparam = new stdClass();
252 $exceptionparam->message
= $e->getMessage();
253 $exceptionparam->courseid
= $params['courseid'];
254 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
256 require_capability('moodle/course:managegroups', $context);
258 $gs = groups_get_all_groups($params['courseid'], 0, 0,
259 'g.id, g.courseid, g.name, g.idnumber, g.description, g.descriptionformat, g.enrolmentkey');
262 foreach ($gs as $group) {
263 list($group->description
, $group->descriptionformat
) =
264 external_format_text($group->description
, $group->descriptionformat
,
265 $context->id
, 'group', 'description', $group->id
);
266 $groups[] = (array)$group;
273 * Returns description of method result value
275 * @return external_description
278 public static function get_course_groups_returns() {
279 return new external_multiple_structure(
280 new external_single_structure(
282 'id' => new external_value(PARAM_INT
, 'group record id'),
283 'courseid' => new external_value(PARAM_INT
, 'id of course'),
284 'name' => new external_value(PARAM_TEXT
, 'multilang compatible name, course unique'),
285 'description' => new external_value(PARAM_RAW
, 'group description text'),
286 'descriptionformat' => new external_format_value('description'),
287 'enrolmentkey' => new external_value(PARAM_RAW
, 'group enrol secret phrase'),
288 'idnumber' => new external_value(PARAM_RAW
, 'id number')
295 * Returns description of method parameters
297 * @return external_function_parameters
300 public static function delete_groups_parameters() {
301 return new external_function_parameters(
303 'groupids' => new external_multiple_structure(new external_value(PARAM_INT
, 'Group ID')),
311 * @param array $groupids array of group ids
314 public static function delete_groups($groupids) {
316 require_once("$CFG->dirroot/group/lib.php");
318 $params = self
::validate_parameters(self
::delete_groups_parameters(), array('groupids'=>$groupids));
320 $transaction = $DB->start_delegated_transaction();
322 foreach ($params['groupids'] as $groupid) {
324 $groupid = validate_param($groupid, PARAM_INT
);
325 if (!$group = groups_get_group($groupid, '*', IGNORE_MISSING
)) {
326 // silently ignore attempts to delete nonexisting groups
330 // now security checks
331 $context = context_course
::instance($group->courseid
, IGNORE_MISSING
);
333 self
::validate_context($context);
334 } catch (Exception
$e) {
335 $exceptionparam = new stdClass();
336 $exceptionparam->message
= $e->getMessage();
337 $exceptionparam->courseid
= $group->courseid
;
338 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
340 require_capability('moodle/course:managegroups', $context);
342 groups_delete_group($group);
345 $transaction->allow_commit();
349 * Returns description of method result value
354 public static function delete_groups_returns() {
360 * Returns description of method parameters
362 * @return external_function_parameters
365 public static function get_group_members_parameters() {
366 return new external_function_parameters(
368 'groupids' => new external_multiple_structure(new external_value(PARAM_INT
, 'Group ID')),
374 * Return all members for a group
376 * @param array $groupids array of group ids
377 * @return array with group id keys containing arrays of user ids
380 public static function get_group_members($groupids) {
383 $params = self
::validate_parameters(self
::get_group_members_parameters(), array('groupids'=>$groupids));
385 foreach ($params['groupids'] as $groupid) {
387 $group = groups_get_group($groupid, 'id, courseid, name, enrolmentkey', MUST_EXIST
);
388 // now security checks
389 $context = context_course
::instance($group->courseid
, IGNORE_MISSING
);
391 self
::validate_context($context);
392 } catch (Exception
$e) {
393 $exceptionparam = new stdClass();
394 $exceptionparam->message
= $e->getMessage();
395 $exceptionparam->courseid
= $group->courseid
;
396 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
398 require_capability('moodle/course:managegroups', $context);
400 $groupmembers = groups_get_members($group->id
, 'u.id', 'lastname ASC, firstname ASC');
402 $members[] = array('groupid'=>$groupid, 'userids'=>array_keys($groupmembers));
409 * Returns description of method result value
411 * @return external_description
414 public static function get_group_members_returns() {
415 return new external_multiple_structure(
416 new external_single_structure(
418 'groupid' => new external_value(PARAM_INT
, 'group record id'),
419 'userids' => new external_multiple_structure(new external_value(PARAM_INT
, 'user id')),
427 * Returns description of method parameters
429 * @return external_function_parameters
432 public static function add_group_members_parameters() {
433 return new external_function_parameters(
435 'members'=> new external_multiple_structure(
436 new external_single_structure(
438 'groupid' => new external_value(PARAM_INT
, 'group record id'),
439 'userid' => new external_value(PARAM_INT
, 'user id'),
450 * @param array $members of arrays with keys userid, groupid
453 public static function add_group_members($members) {
455 require_once("$CFG->dirroot/group/lib.php");
457 $params = self
::validate_parameters(self
::add_group_members_parameters(), array('members'=>$members));
459 $transaction = $DB->start_delegated_transaction();
460 foreach ($params['members'] as $member) {
462 $groupid = $member['groupid'];
463 $userid = $member['userid'];
465 $group = groups_get_group($groupid, 'id, courseid', MUST_EXIST
);
466 $user = $DB->get_record('user', array('id'=>$userid, 'deleted'=>0, 'mnethostid'=>$CFG->mnet_localhost_id
), '*', MUST_EXIST
);
468 // now security checks
469 $context = context_course
::instance($group->courseid
, IGNORE_MISSING
);
471 self
::validate_context($context);
472 } catch (Exception
$e) {
473 $exceptionparam = new stdClass();
474 $exceptionparam->message
= $e->getMessage();
475 $exceptionparam->courseid
= $group->courseid
;
476 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
478 require_capability('moodle/course:managegroups', $context);
480 // now make sure user is enrolled in course - this is mandatory requirement,
481 // unfortunately this is slow
482 if (!is_enrolled($context, $userid)) {
483 throw new invalid_parameter_exception('Only enrolled users may be members of groups');
486 groups_add_member($group, $user);
489 $transaction->allow_commit();
493 * Returns description of method result value
498 public static function add_group_members_returns() {
504 * Returns description of method parameters
506 * @return external_function_parameters
509 public static function delete_group_members_parameters() {
510 return new external_function_parameters(
512 'members'=> new external_multiple_structure(
513 new external_single_structure(
515 'groupid' => new external_value(PARAM_INT
, 'group record id'),
516 'userid' => new external_value(PARAM_INT
, 'user id'),
525 * Delete group members
527 * @param array $members of arrays with keys userid, groupid
530 public static function delete_group_members($members) {
532 require_once("$CFG->dirroot/group/lib.php");
534 $params = self
::validate_parameters(self
::delete_group_members_parameters(), array('members'=>$members));
536 $transaction = $DB->start_delegated_transaction();
538 foreach ($params['members'] as $member) {
540 $groupid = $member['groupid'];
541 $userid = $member['userid'];
543 $group = groups_get_group($groupid, 'id, courseid', MUST_EXIST
);
544 $user = $DB->get_record('user', array('id'=>$userid, 'deleted'=>0, 'mnethostid'=>$CFG->mnet_localhost_id
), '*', MUST_EXIST
);
546 // now security checks
547 $context = context_course
::instance($group->courseid
, IGNORE_MISSING
);
549 self
::validate_context($context);
550 } catch (Exception
$e) {
551 $exceptionparam = new stdClass();
552 $exceptionparam->message
= $e->getMessage();
553 $exceptionparam->courseid
= $group->courseid
;
554 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
556 require_capability('moodle/course:managegroups', $context);
558 if (!groups_remove_member_allowed($group, $user)) {
559 throw new moodle_exception('errorremovenotpermitted', 'group', '', fullname($user));
561 groups_remove_member($group, $user);
564 $transaction->allow_commit();
568 * Returns description of method result value
573 public static function delete_group_members_returns() {
578 * Returns description of method parameters
580 * @return external_function_parameters
583 public static function create_groupings_parameters() {
584 return new external_function_parameters(
586 'groupings' => new external_multiple_structure(
587 new external_single_structure(
589 'courseid' => new external_value(PARAM_INT
, 'id of course'),
590 'name' => new external_value(PARAM_TEXT
, 'multilang compatible name, course unique'),
591 'description' => new external_value(PARAM_RAW
, 'grouping description text'),
592 'descriptionformat' => new external_format_value('description', VALUE_DEFAULT
),
593 'idnumber' => new external_value(PARAM_RAW
, 'id number', VALUE_OPTIONAL
)
595 ), 'List of grouping object. A grouping has a courseid, a name and a description.'
604 * @param array $groupings array of grouping description arrays (with keys groupname and courseid)
605 * @return array of newly created groupings
608 public static function create_groupings($groupings) {
610 require_once("$CFG->dirroot/group/lib.php");
612 $params = self
::validate_parameters(self
::create_groupings_parameters(), array('groupings'=>$groupings));
614 $transaction = $DB->start_delegated_transaction();
616 $groupings = array();
618 foreach ($params['groupings'] as $grouping) {
619 $grouping = (object)$grouping;
621 if (trim($grouping->name
) == '') {
622 throw new invalid_parameter_exception('Invalid grouping name');
624 if ($DB->count_records('groupings', array('courseid'=>$grouping->courseid
, 'name'=>$grouping->name
))) {
625 throw new invalid_parameter_exception('Grouping with the same name already exists in the course');
628 // Now security checks .
629 $context = context_course
::instance($grouping->courseid
);
631 self
::validate_context($context);
632 } catch (Exception
$e) {
633 $exceptionparam = new stdClass();
634 $exceptionparam->message
= $e->getMessage();
635 $exceptionparam->courseid
= $grouping->courseid
;
636 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
638 require_capability('moodle/course:managegroups', $context);
640 $grouping->descriptionformat
= external_validate_format($grouping->descriptionformat
);
642 // Finally create the grouping.
643 $grouping->id
= groups_create_grouping($grouping);
644 $groupings[] = (array)$grouping;
647 $transaction->allow_commit();
653 * Returns description of method result value
655 * @return external_description
658 public static function create_groupings_returns() {
659 return new external_multiple_structure(
660 new external_single_structure(
662 'id' => new external_value(PARAM_INT
, 'grouping record id'),
663 'courseid' => new external_value(PARAM_INT
, 'id of course'),
664 'name' => new external_value(PARAM_TEXT
, 'multilang compatible name, course unique'),
665 'description' => new external_value(PARAM_RAW
, 'grouping description text'),
666 'descriptionformat' => new external_format_value('description'),
667 'idnumber' => new external_value(PARAM_RAW
, 'id number')
669 ), 'List of grouping object. A grouping has an id, a courseid, a name and a description.'
674 * Returns description of method parameters
676 * @return external_function_parameters
679 public static function update_groupings_parameters() {
680 return new external_function_parameters(
682 'groupings' => new external_multiple_structure(
683 new external_single_structure(
685 'id' => new external_value(PARAM_INT
, 'id of grouping'),
686 'name' => new external_value(PARAM_TEXT
, 'multilang compatible name, course unique'),
687 'description' => new external_value(PARAM_RAW
, 'grouping description text'),
688 'descriptionformat' => new external_format_value('description', VALUE_DEFAULT
),
689 'idnumber' => new external_value(PARAM_RAW
, 'id number', VALUE_OPTIONAL
)
691 ), 'List of grouping object. A grouping has a courseid, a name and a description.'
700 * @param array $groupings array of grouping description arrays (with keys groupname and courseid)
701 * @return array of newly updated groupings
704 public static function update_groupings($groupings) {
706 require_once("$CFG->dirroot/group/lib.php");
708 $params = self
::validate_parameters(self
::update_groupings_parameters(), array('groupings'=>$groupings));
710 $transaction = $DB->start_delegated_transaction();
712 foreach ($params['groupings'] as $grouping) {
713 $grouping = (object)$grouping;
715 if (trim($grouping->name
) == '') {
716 throw new invalid_parameter_exception('Invalid grouping name');
719 if (! $currentgrouping = $DB->get_record('groupings', array('id'=>$grouping->id
))) {
720 throw new invalid_parameter_exception("Grouping $grouping->id does not exist in the course");
723 // Check if the new modified grouping name already exists in the course.
724 if ($grouping->name
!= $currentgrouping->name
and
725 $DB->count_records('groupings', array('courseid'=>$currentgrouping->courseid
, 'name'=>$grouping->name
))) {
726 throw new invalid_parameter_exception('A different grouping with the same name already exists in the course');
729 $grouping->courseid
= $currentgrouping->courseid
;
731 // Now security checks.
732 $context = context_course
::instance($grouping->courseid
);
734 self
::validate_context($context);
735 } catch (Exception
$e) {
736 $exceptionparam = new stdClass();
737 $exceptionparam->message
= $e->getMessage();
738 $exceptionparam->courseid
= $grouping->courseid
;
739 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
741 require_capability('moodle/course:managegroups', $context);
743 // We must force allways FORMAT_HTML.
744 $grouping->descriptionformat
= external_validate_format($grouping->descriptionformat
);
746 // Finally update the grouping.
747 groups_update_grouping($grouping);
750 $transaction->allow_commit();
756 * Returns description of method result value
758 * @return external_description
761 public static function update_groupings_returns() {
766 * Returns description of method parameters
768 * @return external_function_parameters
771 public static function get_groupings_parameters() {
772 return new external_function_parameters(
774 'groupingids' => new external_multiple_structure(new external_value(PARAM_INT
, 'grouping ID')
775 , 'List of grouping id. A grouping id is an integer.'),
776 'returngroups' => new external_value(PARAM_BOOL
, 'return associated groups', VALUE_DEFAULT
, 0)
782 * Get groupings definition specified by ids
784 * @param array $groupingids arrays of grouping ids
785 * @param boolean $returngroups return the associated groups if true. The default is false.
786 * @return array of grouping objects (id, courseid, name)
789 public static function get_groupings($groupingids, $returngroups = false) {
791 require_once("$CFG->dirroot/group/lib.php");
792 require_once("$CFG->libdir/filelib.php");
794 $params = self
::validate_parameters(self
::get_groupings_parameters(),
795 array('groupingids' => $groupingids,
796 'returngroups' => $returngroups));
798 $groupings = array();
799 foreach ($params['groupingids'] as $groupingid) {
801 $grouping = groups_get_grouping($groupingid, '*', MUST_EXIST
);
803 // Now security checks.
804 $context = context_course
::instance($grouping->courseid
);
806 self
::validate_context($context);
807 } catch (Exception
$e) {
808 $exceptionparam = new stdClass();
809 $exceptionparam->message
= $e->getMessage();
810 $exceptionparam->courseid
= $grouping->courseid
;
811 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
813 require_capability('moodle/course:managegroups', $context);
815 list($grouping->description
, $grouping->descriptionformat
) =
816 external_format_text($grouping->description
, $grouping->descriptionformat
,
817 $context->id
, 'grouping', 'description', $grouping->id
);
819 $groupingarray = (array)$grouping;
821 if ($params['returngroups']) {
822 $grouprecords = $DB->get_records_sql("SELECT * FROM {groups} g INNER JOIN {groupings_groups} gg ".
823 "ON g.id = gg.groupid WHERE gg.groupingid = ? ".
824 "ORDER BY groupid", array($groupingid));
827 foreach ($grouprecords as $grouprecord) {
828 list($grouprecord->description
, $grouprecord->descriptionformat
) =
829 external_format_text($grouprecord->description
, $grouprecord->descriptionformat
,
830 $context->id
, 'group', 'description', $grouprecord->groupid
);
831 $groups[] = array('id' => $grouprecord->groupid
,
832 'name' => $grouprecord->name
,
833 'idnumber' => $grouprecord->idnumber
,
834 'description' => $grouprecord->description
,
835 'descriptionformat' => $grouprecord->descriptionformat
,
836 'enrolmentkey' => $grouprecord->enrolmentkey
,
837 'courseid' => $grouprecord->courseid
840 $groupingarray['groups'] = $groups;
843 $groupings[] = $groupingarray;
850 * Returns description of method result value
852 * @return external_description
855 public static function get_groupings_returns() {
856 return new external_multiple_structure(
857 new external_single_structure(
859 'id' => new external_value(PARAM_INT
, 'grouping record id'),
860 'courseid' => new external_value(PARAM_INT
, 'id of course'),
861 'name' => new external_value(PARAM_TEXT
, 'multilang compatible name, course unique'),
862 'description' => new external_value(PARAM_RAW
, 'grouping description text'),
863 'descriptionformat' => new external_format_value('description'),
864 'idnumber' => new external_value(PARAM_RAW
, 'id number'),
865 'groups' => new external_multiple_structure(
866 new external_single_structure(
868 'id' => new external_value(PARAM_INT
, 'group record id'),
869 'courseid' => new external_value(PARAM_INT
, 'id of course'),
870 'name' => new external_value(PARAM_TEXT
, 'multilang compatible name, course unique'),
871 'description' => new external_value(PARAM_RAW
, 'group description text'),
872 'descriptionformat' => new external_format_value('description'),
873 'enrolmentkey' => new external_value(PARAM_RAW
, 'group enrol secret phrase'),
874 'idnumber' => new external_value(PARAM_RAW
, 'id number')
877 'optional groups', VALUE_OPTIONAL
)
884 * Returns description of method parameters
886 * @return external_function_parameters
889 public static function get_course_groupings_parameters() {
890 return new external_function_parameters(
892 'courseid' => new external_value(PARAM_INT
, 'id of course'),
898 * Get all groupings in the specified course
900 * @param int $courseid id of course
901 * @return array of grouping objects (id, courseid, name, enrolmentkey)
904 public static function get_course_groupings($courseid) {
906 require_once("$CFG->dirroot/group/lib.php");
907 require_once("$CFG->libdir/filelib.php");
909 $params = self
::validate_parameters(self
::get_course_groupings_parameters(), array('courseid'=>$courseid));
911 // Now security checks.
912 $context = context_course
::instance($params['courseid']);
915 self
::validate_context($context);
916 } catch (Exception
$e) {
917 $exceptionparam = new stdClass();
918 $exceptionparam->message
= $e->getMessage();
919 $exceptionparam->courseid
= $params['courseid'];
920 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
922 require_capability('moodle/course:managegroups', $context);
924 $gs = groups_get_all_groupings($params['courseid']);
926 $groupings = array();
927 foreach ($gs as $grouping) {
928 list($grouping->description
, $grouping->descriptionformat
) =
929 external_format_text($grouping->description
, $grouping->descriptionformat
,
930 $context->id
, 'grouping', 'description', $grouping->id
);
931 $groupings[] = (array)$grouping;
938 * Returns description of method result value
940 * @return external_description
943 public static function get_course_groupings_returns() {
944 return new external_multiple_structure(
945 new external_single_structure(
947 'id' => new external_value(PARAM_INT
, 'grouping record id'),
948 'courseid' => new external_value(PARAM_INT
, 'id of course'),
949 'name' => new external_value(PARAM_TEXT
, 'multilang compatible name, course unique'),
950 'description' => new external_value(PARAM_RAW
, 'grouping description text'),
951 'descriptionformat' => new external_format_value('description'),
952 'idnumber' => new external_value(PARAM_RAW
, 'id number')
959 * Returns description of method parameters
961 * @return external_function_parameters
964 public static function delete_groupings_parameters() {
965 return new external_function_parameters(
967 'groupingids' => new external_multiple_structure(new external_value(PARAM_INT
, 'grouping ID')),
975 * @param array $groupingids array of grouping ids
979 public static function delete_groupings($groupingids) {
981 require_once("$CFG->dirroot/group/lib.php");
983 $params = self
::validate_parameters(self
::delete_groupings_parameters(), array('groupingids'=>$groupingids));
985 $transaction = $DB->start_delegated_transaction();
987 foreach ($params['groupingids'] as $groupingid) {
989 if (!$grouping = groups_get_grouping($groupingid, 'id, courseid', IGNORE_MISSING
)) {
990 // Silently ignore attempts to delete nonexisting groupings.
994 // Now security checks.
995 $context = context_course
::instance($grouping->courseid
);
997 self
::validate_context($context);
998 } catch (Exception
$e) {
999 $exceptionparam = new stdClass();
1000 $exceptionparam->message
= $e->getMessage();
1001 $exceptionparam->courseid
= $grouping->courseid
;
1002 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
1004 require_capability('moodle/course:managegroups', $context);
1006 groups_delete_grouping($grouping);
1009 $transaction->allow_commit();
1013 * Returns description of method result value
1015 * @return external_description
1018 public static function delete_groupings_returns() {
1023 * Returns description of method parameters
1025 * @return external_function_parameters
1028 public static function assign_grouping_parameters() {
1029 return new external_function_parameters(
1031 'assignments'=> new external_multiple_structure(
1032 new external_single_structure(
1034 'groupingid' => new external_value(PARAM_INT
, 'grouping record id'),
1035 'groupid' => new external_value(PARAM_INT
, 'group record id'),
1044 * Assign a group to a grouping
1046 * @param array $assignments of arrays with keys groupid, groupingid
1050 public static function assign_grouping($assignments) {
1052 require_once("$CFG->dirroot/group/lib.php");
1054 $params = self
::validate_parameters(self
::assign_grouping_parameters(), array('assignments'=>$assignments));
1056 $transaction = $DB->start_delegated_transaction();
1057 foreach ($params['assignments'] as $assignment) {
1059 $groupingid = $assignment['groupingid'];
1060 $groupid = $assignment['groupid'];
1062 $grouping = groups_get_grouping($groupingid, 'id, courseid', MUST_EXIST
);
1063 $group = groups_get_group($groupid, 'id, courseid', MUST_EXIST
);
1065 if ($DB->record_exists('groupings_groups', array('groupingid'=>$groupingid, 'groupid'=>$groupid))) {
1066 // Continue silently if the group is yet assigned to the grouping.
1070 // Now security checks.
1071 $context = context_course
::instance($grouping->courseid
);
1073 self
::validate_context($context);
1074 } catch (Exception
$e) {
1075 $exceptionparam = new stdClass();
1076 $exceptionparam->message
= $e->getMessage();
1077 $exceptionparam->courseid
= $group->courseid
;
1078 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
1080 require_capability('moodle/course:managegroups', $context);
1082 groups_assign_grouping($groupingid, $groupid);
1085 $transaction->allow_commit();
1089 * Returns description of method result value
1094 public static function assign_grouping_returns() {
1099 * Returns description of method parameters
1101 * @return external_function_parameters
1104 public static function unassign_grouping_parameters() {
1105 return new external_function_parameters(
1107 'unassignments'=> new external_multiple_structure(
1108 new external_single_structure(
1110 'groupingid' => new external_value(PARAM_INT
, 'grouping record id'),
1111 'groupid' => new external_value(PARAM_INT
, 'group record id'),
1120 * Unassign a group from a grouping
1122 * @param array $unassignments of arrays with keys groupid, groupingid
1126 public static function unassign_grouping($unassignments) {
1128 require_once("$CFG->dirroot/group/lib.php");
1130 $params = self
::validate_parameters(self
::unassign_grouping_parameters(), array('unassignments'=>$unassignments));
1132 $transaction = $DB->start_delegated_transaction();
1133 foreach ($params['unassignments'] as $unassignment) {
1135 $groupingid = $unassignment['groupingid'];
1136 $groupid = $unassignment['groupid'];
1138 $grouping = groups_get_grouping($groupingid, 'id, courseid', MUST_EXIST
);
1139 $group = groups_get_group($groupid, 'id, courseid', MUST_EXIST
);
1141 if (!$DB->record_exists('groupings_groups', array('groupingid'=>$groupingid, 'groupid'=>$groupid))) {
1142 // Continue silently if the group is not assigned to the grouping.
1146 // Now security checks.
1147 $context = context_course
::instance($grouping->courseid
);
1149 self
::validate_context($context);
1150 } catch (Exception
$e) {
1151 $exceptionparam = new stdClass();
1152 $exceptionparam->message
= $e->getMessage();
1153 $exceptionparam->courseid
= $group->courseid
;
1154 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
1156 require_capability('moodle/course:managegroups', $context);
1158 groups_unassign_grouping($groupingid, $groupid);
1161 $transaction->allow_commit();
1165 * Returns description of method result value
1170 public static function unassign_grouping_returns() {
1175 * Returns description of method parameters
1177 * @return external_function_parameters
1180 public static function get_course_user_groups_parameters() {
1181 return new external_function_parameters(
1183 'courseid' => new external_value(PARAM_INT
, 'id of course'),
1184 'userid' => new external_value(PARAM_INT
, 'id of user'),
1185 'groupingid' => new external_value(PARAM_INT
, 'returns only groups in the specified grouping', VALUE_DEFAULT
, 0)
1191 * Get all groups in the specified course for the specified user.
1193 * @throws moodle_exception
1194 * @param int $courseid id of course.
1195 * @param int $userid id of user.
1196 * @param int $groupingid optional returns only groups in the specified grouping.
1197 * @return array of group objects (id, name, description, format) and possible warnings.
1200 public static function get_course_user_groups($courseid, $userid, $groupingid = 0) {
1203 // Warnings array, it can be empty at the end but is mandatory.
1204 $warnings = array();
1207 'courseid' => $courseid,
1208 'userid' => $userid,
1209 'groupingid' => $groupingid
1211 $params = self
::validate_parameters(self
::get_course_user_groups_parameters(), $params);
1212 $courseid = $params['courseid'];
1213 $userid = $params['userid'];
1214 $groupingid = $params['groupingid'];
1216 // Validate course and user. get_course throws an exception if the course does not exists.
1217 $course = get_course($courseid);
1218 $user = core_user
::get_user($userid, '*', MUST_EXIST
);
1219 core_user
::require_active_user($user);
1222 $context = context_course
::instance($course->id
);
1223 self
::validate_context($context);
1225 // Check if we have permissions for retrieve the information.
1226 if ($user->id
!= $USER->id
) {
1227 if (!has_capability('moodle/course:managegroups', $context)) {
1228 throw new moodle_exception('accessdenied', 'admin');
1230 // Validate if the user is enrolled in the course.
1231 if (!is_enrolled($context, $user->id
)) {
1232 // We return a warning because the function does not fail for not enrolled users.
1233 $warning['item'] = 'course';
1234 $warning['itemid'] = $course->id
;
1235 $warning['warningcode'] = '1';
1236 $warning['message'] = "User $user->id is not enrolled in course $course->id";
1237 $warnings[] = $warning;
1241 $usergroups = array();
1242 if (empty($warnings)) {
1243 $groups = groups_get_all_groups($course->id
, $user->id
, 0, 'g.id, g.name, g.description, g.descriptionformat, g.idnumber');
1245 foreach ($groups as $group) {
1246 list($group->description
, $group->descriptionformat
) =
1247 external_format_text($group->description
, $group->descriptionformat
,
1248 $context->id
, 'group', 'description', $group->id
);
1249 $group->courseid
= $course->id
;
1250 $usergroups[] = $group;
1255 'groups' => $usergroups,
1256 'warnings' => $warnings
1262 * Returns description of method result value.
1264 * @return external_description A single structure containing groups and possible warnings.
1267 public static function get_course_user_groups_returns() {
1268 return new external_single_structure(
1270 'groups' => new external_multiple_structure(self
::group_description()),
1271 'warnings' => new external_warnings(),
1277 * Create group return value description.
1279 * @return external_single_structure The group description
1281 public static function group_description() {
1282 return new external_single_structure(
1284 'id' => new external_value(PARAM_INT
, 'group record id'),
1285 'name' => new external_value(PARAM_TEXT
, 'multilang compatible name, course unique'),
1286 'description' => new external_value(PARAM_RAW
, 'group description text'),
1287 'descriptionformat' => new external_format_value('description'),
1288 'idnumber' => new external_value(PARAM_RAW
, 'id number'),
1289 'courseid' => new external_value(PARAM_INT
, 'course id', VALUE_OPTIONAL
),
1295 * Returns description of method parameters
1297 * @return external_function_parameters
1300 public static function get_activity_allowed_groups_parameters() {
1301 return new external_function_parameters(
1303 'cmid' => new external_value(PARAM_INT
, 'course module id'),
1304 'userid' => new external_value(PARAM_INT
, 'id of user, empty for current user', VALUE_DEFAULT
, 0)
1310 * Gets a list of groups that the user is allowed to access within the specified activity.
1312 * @throws moodle_exception
1313 * @param int $cmid course module id
1314 * @param int $userid id of user.
1315 * @return array of group objects (id, name, description, format) and possible warnings.
1318 public static function get_activity_allowed_groups($cmid, $userid = 0) {
1321 // Warnings array, it can be empty at the end but is mandatory.
1322 $warnings = array();
1328 $params = self
::validate_parameters(self
::get_activity_allowed_groups_parameters(), $params);
1329 $cmid = $params['cmid'];
1330 $userid = $params['userid'];
1332 $cm = get_coursemodule_from_id(null, $cmid, 0, false, MUST_EXIST
);
1335 $context = context_module
::instance($cm->id
);
1336 $coursecontext = context_course
::instance($cm->course
);
1337 self
::validate_context($context);
1339 if (empty($userid)) {
1340 $userid = $USER->id
;
1343 $user = core_user
::get_user($userid, '*', MUST_EXIST
);
1344 core_user
::require_active_user($user);
1346 // Check if we have permissions for retrieve the information.
1347 if ($user->id
!= $USER->id
) {
1348 if (!has_capability('moodle/course:managegroups', $context)) {
1349 throw new moodle_exception('accessdenied', 'admin');
1352 // Validate if the user is enrolled in the course.
1353 $course = get_course($cm->course
);
1354 if (!can_access_course($course, $user, '', true)) {
1355 // We return a warning because the function does not fail for not enrolled users.
1357 $warning['item'] = 'course';
1358 $warning['itemid'] = $cm->course
;
1359 $warning['warningcode'] = '1';
1360 $warning['message'] = "User $user->id cannot access course $cm->course";
1361 $warnings[] = $warning;
1365 $usergroups = array();
1366 if (empty($warnings)) {
1367 $groups = groups_get_activity_allowed_groups($cm, $user->id
);
1369 foreach ($groups as $group) {
1370 list($group->description
, $group->descriptionformat
) =
1371 external_format_text($group->description
, $group->descriptionformat
,
1372 $coursecontext->id
, 'group', 'description', $group->id
);
1373 $group->courseid
= $cm->course
;
1374 $usergroups[] = $group;
1379 'groups' => $usergroups,
1380 'canaccessallgroups' => has_capability('moodle/site:accessallgroups', $context, $user),
1381 'warnings' => $warnings
1387 * Returns description of method result value.
1389 * @return external_description A single structure containing groups and possible warnings.
1392 public static function get_activity_allowed_groups_returns() {
1393 return new external_single_structure(
1395 'groups' => new external_multiple_structure(self
::group_description()),
1396 'canaccessallgroups' => new external_value(PARAM_BOOL
,
1397 'Whether the user will be able to access all the activity groups.', VALUE_OPTIONAL
),
1398 'warnings' => new external_warnings(),
1404 * Returns description of method parameters
1406 * @return external_function_parameters
1409 public static function get_activity_groupmode_parameters() {
1410 return new external_function_parameters(
1412 'cmid' => new external_value(PARAM_INT
, 'course module id')
1418 * Returns effective groupmode used in a given activity.
1420 * @throws moodle_exception
1421 * @param int $cmid course module id.
1422 * @return array containing the group mode and possible warnings.
1424 * @throws moodle_exception
1426 public static function get_activity_groupmode($cmid) {
1429 // Warnings array, it can be empty at the end but is mandatory.
1430 $warnings = array();
1435 $params = self
::validate_parameters(self
::get_activity_groupmode_parameters(), $params);
1436 $cmid = $params['cmid'];
1438 $cm = get_coursemodule_from_id(null, $cmid, 0, false, MUST_EXIST
);
1441 $context = context_module
::instance($cm->id
);
1442 self
::validate_context($context);
1444 $groupmode = groups_get_activity_groupmode($cm);
1447 'groupmode' => $groupmode,
1448 'warnings' => $warnings
1454 * Returns description of method result value.
1456 * @return external_description
1459 public static function get_activity_groupmode_returns() {
1460 return new external_single_structure(
1462 'groupmode' => new external_value(PARAM_INT
, 'group mode:
1463 0 for no groups, 1 for separate groups, 2 for visible groups'),
1464 'warnings' => new external_warnings(),