MDL-12886 new design of external service functions, groups api improved and known...
[moodle.git] / group / externallib.php
CommitLineData
9a0df45a 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * External groups API
20 *
21 * @package moodlecore
22 * @subpackage webservice
23 * @copyright 2009 Petr Skoda (http://skodak.org)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27require_once("$CFG->libdir/externallib.php");
28
29class moodle_group_external extends external_api {
30
31 /**
32 * Create groups
33 * @param array $params array of group description arrays (with keys groupname and courseid)
34 * @return array of newly created group ids
35 */
36 public static function create_groups($params) {
37 global $CFG;
38 require_once("$CFG->dirroot/group/lib.php");
39
40 $groupids = array();
41
42 foreach ($params as $groupparam) {
43 $group = new object();
44 // clean params
45 $group->courseid = clean_param($groupparam['courseid'], PARAM_INTEGER);
46 $group->name = clean_param($groupparam['groupname'], PARAM_MULTILANG);
47 if (array_key_exists('enrolmentkey', $groupparam)) {
48 $group->enrolmentkey = $groupparam['enrolmentkey'];
49 } else {
50 $group->enrolmentkey = '';
51 }
52 // now security checks
53 $context = get_context_instance(CONTEXT_COURSE, $group->courseid);
54 self::validate_context($context);
55 require_capability('moodle/course:managegroups', $context);
56
57 $id = groups_create_group($group, false);
58 $group->id = $id;
59 $groupids[$id] = $group;
60 }
61
62 return $groupids;
63 }
64
65 /**
66 * Get groups definition
67 * @param array $params arrays of group ids
68 * @return array of group objects (id, courseid, name, enrolmentkey)
69 */
70 public static function get_groups($params) {
71 $groups = array();
72
73 //TODO: we do need to search for groups in courses too,
74 // fetching by id is not enough!
75
76 foreach ($params as $groupid) {
77 $groupid = clean_param($groupid, PARAM_INTEGER);
78 $group = groups_get_group($groupid, 'id, courseid, name, enrolmentkey', MUST_EXIST);
79 // now security checks
80 $context = get_context_instance(CONTEXT_COURSE, $group->courseid);
81 self::validate_context($context);
82 require_capability('moodle/course:managegroups', $context);
83
84 $groups[$group->id] = $group;
85 }
86
87 return $groups;
88 }
89
90 /**
91 * Delete groups
92 * @param array $params array of group ids
93 * @return void
94 */
95 public static function delete_groups($params) {
96 global $CFG;
97 require_once("$CFG->dirroot/group/lib.php");
98
99 $groups = array();
100
101 foreach ($params as $groupid) {
102 $groupid = clean_param($groupid, PARAM_INTEGER);
103 if (!$group = groups_get_group($groupid, 'id, courseid', IGNORE_MISSING)) {
104 // silently ignore attempts to delete nonexisting groups
105 continue;
106 }
107 // now security checks
108 $context = get_context_instance(CONTEXT_COURSE, $group->courseid);
109 self::validate_context($context);
110 require_capability('moodle/course:managegroups', $context);
111
112 groups_delete_group($group);
113 }
114 }
115
116
117 /**
118 * Return all members for a group
119 * @param array $params array of group ids
120 * @return array with group id keys containing arrays of user ids
121 */
122 public static function get_groupmembers($params) {
123 $groups = array();
124
125 foreach ($params as $groupid) {
126 $groupid = clean_param($groupid, PARAM_INTEGER);
127 $group = groups_get_group($groupid, 'id, courseid, name, enrolmentkey', MUST_EXIST);
128 // now security checks
129 $context = get_context_instance(CONTEXT_COURSE, $group->courseid);
130 self::validate_context($context);
131 require_capability('moodle/course:managegroups', $context);
132
133 $groupmembers = groups_get_members($group->id, 'u.id', 'lastname ASC, firstname ASC');
134
135 $groups[$group->id] = array_keys($groupmembers);
136 }
137
138 return $groups;
139 }
140
141
142 /**
143 * Add group members
144 * @param array of arrays with keys userid, groupid
145 * @return void
146 */
147 public static function add_groupmembers($params) {
148 global $CFG;
149 require_once("$CFG->dirroot/group/lib.php");
150
151 $groups = array();
152
153 foreach ($params as $member) {
154 $groupid = clean_param($member['groupid'], PARAM_INTEGER);
155 $userid = clean_param($member['userid'], PARAM_INTEGER);
156 $group = groups_get_group($groupid, 'id, courseid', MUST_EXIST);
157 $user = $DB->get_record('user', array('id'=>$userid, 'deleted'=>0, 'mnethostid'=>$CFG->mnet_localhost_id));
158
159 // now security checks
160 $context = get_context_instance(CONTEXT_COURSE, $group->courseid);
161 self::validate_context($context);
162 require_capability('moodle/course:managegroups', $context);
163 require_capability('moodle/course:view', $context, $user->id, false); // only enrolled users may be members of group!!!
164
165 groups_add_member($group, $user);
166 }
167 }
168
169
170 /**
171 * Delete group members
172 * @param array of arrays with keys userid, groupid
173 * @return void
174 */
175 public static function delete_groupmembers($params){
176 global $CFG;
177 require_once("$CFG->dirroot/group/lib.php");
178
179 $groups = array();
180
181 foreach ($params as $member) {
182 $groupid = clean_param($member['groupid'], PARAM_INTEGER);
183 $userid = clean_param($member['userid'], PARAM_INTEGER);
184 $group = groups_get_group($groupid, 'id, courseid');
185 $user = $DB->get_record('user', array('id'=>$userid, 'deleted'=>0, 'mnethostid'=>$CFG->mnet_localhost_id));
186
187 // now security checks
188 $context = get_context_instance(CONTEXT_COURSE, $group->courseid);
189 self::validate_context($context);
190 require_capability('moodle/course:managegroups', $context);
191
192 groups_remove_member($group, $user);
193 }
194 }
195
196}