MDL-12886 one more external group function, other minor fixes

This commit is contained in:
skodak 2009-10-22 23:54:35 +00:00
parent f507217757
commit 246f6da2b8
6 changed files with 122 additions and 33 deletions

View File

@ -124,15 +124,14 @@ class moodle_group_external extends external_api {
}
/**
* Get groups definition
* Get groups definition specified by ids
* @param array $groupids arrays of group ids
* @return array of group objects (id, courseid, name, enrolmentkey)
*/
public static function get_groups($groupids) {
$groups = array();
$params = self::validate_parameters(self::get_groups_parameters(), array('groupids'=>$groupids));
$groups = array();
foreach ($params['groupids'] as $groupid) {
// validate params
$group = groups_get_group($groupid, 'id, courseid, name, description, enrolmentkey', MUST_EXIST);
@ -166,6 +165,59 @@ class moodle_group_external extends external_api {
);
}
/**
* Returns description of method parameters
* @return external_function_parameters
*/
public static function get_course_groups_parameters() {
return new external_function_parameters(
array(
'courseid' => new external_value(PARAM_INT, 'id of course'),
)
);
}
/**
* Get all groups in the specified course
* @param int $courseid id of course
* @return array of group objects (id, courseid, name, enrolmentkey)
*/
public static function get_course_groups($courseid) {
$params = self::validate_parameters(self::get_course_groups_parameters(), array('courseid'=>$courseid));
// now security checks
$context = get_context_instance(CONTEXT_COURSE, $params['courseid']);
self::validate_context($context);
require_capability('moodle/course:managegroups', $context);
$gs = groups_get_all_groups($params['courseid'], 0, 0, 'g.id, g.courseid, g.name, g.description, g.enrolmentkey');
$groups = array();
foreach ($gs as $group) {
$groups[] = (array)$group;
}
return $groups;
}
/**
* Returns description of method result value
* @return external_description
*/
public static function get_course_groups_returns() {
return new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'group record id'),
'courseid' => new external_value(PARAM_INT, 'id of course'),
'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'),
'description' => new external_value(PARAM_RAW, 'group description text'),
'enrolmentkey' => new external_value(PARAM_RAW, 'group enrol secret phrase'),
)
)
);
}
public static function delete_groups_parameters() {
//TODO
}

View File

@ -43,6 +43,15 @@ $functions = array(
'description' => 'Returns group details.',
'type' => 'read',
),
'moodle_group_get_course_groups' => array(
'classname' => 'moodle_group_external',
'methodname' => 'get_course_groups',
'classpath' => 'group/externallib.php',
'description' => 'Returns all groups in specified course.',
'type' => 'read',
),
/*
'moodle_group_delete_groups' => array(
'classname' => 'moodle_group_external',

View File

@ -6,7 +6,7 @@
// This is compared against the values stored in the database to determine
// whether upgrades should be performed (see lib/db/*.php)
$version = 2009102200; // YYYYMMDD = date of the last version bump
$version = 2009102201; // YYYYMMDD = date of the last version bump
// XX = daily increments
$release = '2.0 dev (Build: 20091022)'; // Human-friendly version name

View File

@ -140,8 +140,8 @@ class webservice_rest_server extends webservice_base_server {
$single = '<SINGLE>'."\n";
foreach ($desc->keys as $key=>$subdesc) {
if (!array_key_exists($key, $returns)) {
if ($subdesc->rewquired) {
$single .= '<ERROR>Missing required key</ERROR>';
if ($subdesc->required) {
$single .= '<ERROR>Missing required key "'.$key.'"</ERROR>';
continue;
} else {
//optional field

View File

@ -36,7 +36,8 @@ require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM));
// list of all available functions for testing - please note there must be explicit
// support for testing of each functions, the parameter conversion and form is hardcoded
$functions = array('moodle_group_create_groups', 'moodle_group_get_groups');
// TODO: automate this list by fetching all known functiosn from db and looking if client form defined
$functions = array('moodle_group_create_groups', 'moodle_group_get_groups', 'moodle_group_get_course_groups');
$functions = array_combine($functions, $functions);
if (!isset($functions[$function])) { // whitelisting security
$function = '';
@ -109,6 +110,9 @@ if ($mform->is_cancelled()) {
$params['groupids'][] = $data->groupids[$i];
}
} else if ($function === 'moodle_group_get_course_groups') {
$params['courseid'] = $data->courseid;
} else {
throw new coding_exception('Testing of function '.$function.' not implemented yet!');
}

View File

@ -22,32 +22,6 @@ class webservice_test_client_form extends moodleform {
// === Test client forms ===
class moodle_group_get_groups_form extends moodleform {
public function definition() {
global $CFG;
$mform = $this->_form;
$mform->addElement('header', 'wstestclienthdr', get_string('testclient', 'webservice'));
//note: these values are intentionally PARAM_RAW - we want users to test any rubbish as parameters
$mform->addElement('text', 'wsusername', 'wsusername');
$mform->addElement('text', 'wspassword', 'wspassword');
$mform->addElement('text', 'groupids[0]', 'groupids[0]');
$mform->addElement('text', 'groupids[1]', 'groupids[1]');
$mform->addElement('text', 'groupids[2]', 'groupids[2]');
$mform->addElement('text', 'groupids[3]', 'groupids[3]');
$mform->addElement('hidden', 'function');
$mform->setType('function', PARAM_SAFEDIR);
$mform->addElement('hidden', 'protocol');
$mform->setType('protocol', PARAM_SAFEDIR);
$this->add_action_buttons(true, get_string('execute', 'webservice'));
}
}
class moodle_group_create_groups_form extends moodleform {
public function definition() {
global $CFG;
@ -75,3 +49,53 @@ class moodle_group_create_groups_form extends moodleform {
$this->add_action_buttons(true, get_string('execute', 'webservice'));
}
}
class moodle_group_get_groups_form extends moodleform {
public function definition() {
global $CFG;
$mform = $this->_form;
$mform->addElement('header', 'wstestclienthdr', get_string('testclient', 'webservice'));
//note: these values are intentionally PARAM_RAW - we want users to test any rubbish as parameters
$mform->addElement('text', 'wsusername', 'wsusername');
$mform->addElement('text', 'wspassword', 'wspassword');
$mform->addElement('text', 'groupids[0]', 'groupids[0]');
$mform->addElement('text', 'groupids[1]', 'groupids[1]');
$mform->addElement('text', 'groupids[2]', 'groupids[2]');
$mform->addElement('text', 'groupids[3]', 'groupids[3]');
$mform->addElement('hidden', 'function');
$mform->setType('function', PARAM_SAFEDIR);
$mform->addElement('hidden', 'protocol');
$mform->setType('protocol', PARAM_SAFEDIR);
$this->add_action_buttons(true, get_string('execute', 'webservice'));
}
}
class moodle_group_get_course_groups_form extends moodleform {
public function definition() {
global $CFG;
$mform = $this->_form;
$mform->addElement('header', 'wstestclienthdr', get_string('testclient', 'webservice'));
//note: these values are intentionally PARAM_RAW - we want users to test any rubbish as parameters
$mform->addElement('text', 'wsusername', 'wsusername');
$mform->addElement('text', 'wspassword', 'wspassword');
$mform->addElement('text', 'courseid', 'courseid');
$mform->addElement('hidden', 'function');
$mform->setType('function', PARAM_SAFEDIR);
$mform->addElement('hidden', 'protocol');
$mform->setType('protocol', PARAM_SAFEDIR);
$this->add_action_buttons(true, get_string('execute', 'webservice'));
}
}