mirror of
https://github.com/moodle/moodle.git
synced 2025-01-19 06:18:28 +01:00
266 lines
8.2 KiB
PHP
266 lines
8.2 KiB
PHP
<?php
|
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
//
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
/**
|
|
* External groups API
|
|
*
|
|
* @package moodlecore
|
|
* @subpackage webservice
|
|
* @copyright 2009 Petr Skoda (http://skodak.org)
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
*/
|
|
|
|
require_once("$CFG->libdir/externallib.php");
|
|
|
|
class moodle_group_external extends external_api {
|
|
|
|
/**
|
|
* Returns description of method parameters
|
|
* @return ?
|
|
*/
|
|
public static function create_groups_parameters() {
|
|
//TODO
|
|
}
|
|
|
|
/**
|
|
* Create groups
|
|
* @param ? $params array of group description arrays (with keys groupname and courseid)
|
|
* @return array of newly created group ids
|
|
*/
|
|
public static function create_groups($params) {
|
|
global $CFG;
|
|
require_once("$CFG->dirroot/group/lib.php");
|
|
|
|
$params = self::validate_params(self::create_groups_parameters(), $params);
|
|
|
|
$groups = array();
|
|
|
|
foreach ($params['groupids'] as $group) {
|
|
$group = (object)$group;
|
|
|
|
if (empty(trim($group->name))) {
|
|
throw new invalid_parameter_exception('Invalid group name');
|
|
}
|
|
if ($DB->get_record('groups', array('courseid'=>$group->courseid, 'name'=>$group->name))) {
|
|
throw new invalid_parameter_exception('Group with the same name already exists in the course');
|
|
}
|
|
|
|
// now security checks
|
|
$context = get_context_instance(CONTEXT_COURSE, $group->courseid);
|
|
self::validate_context($context);
|
|
require_capability('moodle/course:managegroups', $context);
|
|
|
|
$group->id = groups_create_group($group, false);
|
|
$groups[] = (array)$group;
|
|
}
|
|
|
|
return $groups;
|
|
}
|
|
|
|
/**
|
|
* Returns description of method result value
|
|
* @return ?
|
|
*/
|
|
public static function create_groups_returns() {
|
|
//TODO
|
|
}
|
|
|
|
public static function get_groups_parameters() {
|
|
//TODO
|
|
}
|
|
|
|
/**
|
|
* Get groups definition
|
|
* @param ? $params arrays of group ids
|
|
* @return array of group objects (id, courseid, name, enrolmentkey)
|
|
*/
|
|
public static function get_groups($params) {
|
|
$groups = array();
|
|
|
|
$params = self::validate_params(self::get_groups_parameters(), $params);
|
|
|
|
//TODO: we do need to search for groups in courses too,
|
|
// fetching by id is not enough!
|
|
|
|
foreach ($params['groupids'] as $groupid) {
|
|
// validate params
|
|
$group = groups_get_group($groupid, 'id, courseid, name, description, enrolmentkey', MUST_EXIST);
|
|
|
|
// now security checks
|
|
$context = get_context_instance(CONTEXT_COURSE, $group->courseid);
|
|
self::validate_context($context);
|
|
require_capability('moodle/course:managegroups', $context);
|
|
|
|
$groups[] = (array)$group;
|
|
}
|
|
|
|
return $groups;
|
|
}
|
|
|
|
public static function get_groups_returns() {
|
|
//TODO
|
|
}
|
|
|
|
public static function delete_groups_parameters() {
|
|
//TODO
|
|
}
|
|
|
|
/**
|
|
* Delete groups
|
|
* @param ? $params array of group ids
|
|
* @return void
|
|
*/
|
|
public static function delete_groups($params) {
|
|
global $CFG;
|
|
require_once("$CFG->dirroot/group/lib.php");
|
|
|
|
$params = self::validate_params(self::delete_groups_parameters(), $params);
|
|
|
|
$groups = array();
|
|
|
|
foreach ($params['groupids'] as $groupid) {
|
|
// validate params
|
|
$groupid = validate_param($groupid, PARAM_INTEGER);
|
|
if (!$group = groups_get_group($groupid, 'id, courseid', IGNORE_MISSING)) {
|
|
// silently ignore attempts to delete nonexisting groups
|
|
continue;
|
|
}
|
|
|
|
// now security checks
|
|
$context = get_context_instance(CONTEXT_COURSE, $group->courseid);
|
|
self::validate_context($context);
|
|
require_capability('moodle/course:managegroups', $context);
|
|
|
|
groups_delete_group($group);
|
|
}
|
|
}
|
|
|
|
public static function delete_groups_returns() {
|
|
//TODO
|
|
}
|
|
|
|
|
|
public static function get_groupmembers_parameters() {
|
|
//TODO
|
|
}
|
|
|
|
/**
|
|
* Return all members for a group
|
|
* @param ? $params array of group ids
|
|
* @return array with group id keys containing arrays of user ids
|
|
*/
|
|
public static function get_groupmembers($params) {
|
|
$groups = array();
|
|
|
|
$params = self::validate_params(self::get_groupmembers_parameters(), $params);
|
|
|
|
foreach ($params['groupids'] as $groupid) {
|
|
// validate params
|
|
$group = groups_get_group($groupid, 'id, courseid, name, enrolmentkey', MUST_EXIST);
|
|
// now security checks
|
|
$context = get_context_instance(CONTEXT_COURSE, $group->courseid);
|
|
self::validate_context($context);
|
|
require_capability('moodle/course:managegroups', $context);
|
|
|
|
$groupmembers = groups_get_members($group->id, 'u.id', 'lastname ASC, firstname ASC');
|
|
|
|
$groups[$group->id] = array_keys($groupmembers);
|
|
}
|
|
|
|
return $groups;
|
|
}
|
|
|
|
public static function get_groupmembers_returns() {
|
|
//TODO
|
|
}
|
|
|
|
|
|
public static function add_groupmembers_parameters() {
|
|
//TODO
|
|
}
|
|
|
|
/**
|
|
* Add group members
|
|
* @param array of arrays with keys userid, groupid
|
|
* @return void
|
|
*/
|
|
public static function add_groupmembers($params) {
|
|
global $CFG;
|
|
require_once("$CFG->dirroot/group/lib.php");
|
|
|
|
$params = self::validate_params(self::add_groupmembers_parameters(), $params);
|
|
|
|
foreach ($params['membership'] as $member) {
|
|
// validate params
|
|
$groupid = $member['groupid'];
|
|
$userid = $member['userid'];
|
|
|
|
$group = groups_get_group($groupid, 'id, courseid', MUST_EXIST);
|
|
$user = $DB->get_record('user', array('id'=>$userid, 'deleted'=>0, 'mnethostid'=>$CFG->mnet_localhost_id), '*', MUST_EXIST);
|
|
|
|
// now security checks
|
|
$context = get_context_instance(CONTEXT_COURSE, $group->courseid);
|
|
self::validate_context($context);
|
|
require_capability('moodle/course:managegroups', $context);
|
|
require_capability('moodle/course:view', $context, $user->id, false); // only enrolled users may be members of group!!!
|
|
|
|
groups_add_member($group, $user);
|
|
}
|
|
}
|
|
|
|
public static function add_groupmembers_returns() {
|
|
//TODO
|
|
}
|
|
|
|
|
|
public static function delete_groupmembers_parameters() {
|
|
//TODO
|
|
}
|
|
|
|
/**
|
|
* Delete group members
|
|
* @param ? of arrays with keys userid, groupid
|
|
* @return void
|
|
*/
|
|
public static function delete_groupmembers($params) {
|
|
global $CFG;
|
|
require_once("$CFG->dirroot/group/lib.php");
|
|
|
|
$params = self::validate_params(self::delete_groupmembers_parameters(), $params);
|
|
|
|
foreach ($params['members'] as $member) {
|
|
// validate params
|
|
$groupid = $member['groupid'];
|
|
$userid = $member['userid'];
|
|
|
|
$group = groups_get_group($groupid, 'id, courseid', MUST_EXIST);
|
|
$user = $DB->get_record('user', array('id'=>$userid, 'deleted'=>0, 'mnethostid'=>$CFG->mnet_localhost_id), '*', MUST_EXIST);
|
|
|
|
// now security checks
|
|
$context = get_context_instance(CONTEXT_COURSE, $group->courseid);
|
|
self::validate_context($context);
|
|
require_capability('moodle/course:managegroups', $context);
|
|
|
|
groups_remove_member($group, $user);
|
|
}
|
|
}
|
|
|
|
public static function delete_groupmembers_returns() {
|
|
//TODO
|
|
}
|
|
|
|
} |