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

This commit is contained in:
skodak 2009-10-22 23:07:21 +00:00
parent d463dc4a4f
commit f507217757
6 changed files with 114 additions and 38 deletions

View File

@ -30,59 +30,95 @@ class moodle_group_external extends external_api {
/**
* Returns description of method parameters
* @return ?
* @return external_function_parameters
*/
public static function create_groups_parameters() {
//TODO
return new external_function_parameters(
array(
'groups' => new external_multiple_structure(
new external_single_structure(
array(
'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'),
)
)
)
)
);
}
/**
* Create groups
* @param array $groups array of group description arrays (with keys groupname and courseid)
* @return array of newly created group ids
* @return array of newly created groups
*/
public static function create_groups($groups) {
global $CFG;
global $CFG, $DB;
require_once("$CFG->dirroot/group/lib.php");
$params = self::validate_parameters(self::create_groups_parameters(), array('groups'=>$groups));
$groups = array();
// ideally create all groups or none at all, unfortunately myisam engine does not support transactions :-(
$DB->begin_sql();
try {
$groups = array();
foreach ($params['groups'] as $group) {
$group = (object)$group;
foreach ($params['groups'] as $group) {
$group = (object)$group;
if (trim($group->name) == '') {
throw new invalid_parameter_exception('Invalid group name');
if (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);
// finally create the group
$group->id = groups_create_group($group, false);
$groups[] = (array)$group;
}
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;
} catch (Exception $ex) {
$DB->rollback_sql();
throw $ex;
}
$DB->commit_sql();
return $groups;
}
/**
* Returns description of method result value
* @return ?
* @return external_description
*/
public static function create_groups_returns() {
//TODO
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'),
)
)
);
}
/**
* Returns description of method parameters
* @return external_function_parameters
*/
public static function get_groups_parameters() {
return new external_function_parameters(
array(
'groupids' => new external_multiple_structure(new external_value(PARAM_INT, 'Group ID'))
'groupids' => new external_multiple_structure(new external_value(PARAM_INT, 'Group ID')),
)
);
}
@ -97,9 +133,6 @@ class moodle_group_external extends external_api {
$params = self::validate_parameters(self::get_groups_parameters(), array('groupids'=>$groupids));
//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);
@ -115,14 +148,19 @@ class moodle_group_external extends external_api {
return $groups;
}
/**
* Returns description of method result value
* @return external_description
*/
public static function get_groups_returns() {
return new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'some group id'),
'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, 'just some text'),
'enrolmentkey' => new external_value(PARAM_RAW, 'group enrol secret phrase')
'description' => new external_value(PARAM_RAW, 'group description text'),
'enrolmentkey' => new external_value(PARAM_RAW, 'group enrol secret phrase'),
)
)
);

View File

@ -9,6 +9,8 @@ $string['configwebserviceplugins'] = 'For security reasons enable only protocols
$string['deleteserviceconfirm'] = 'Do you really want to delete external service \"$a\"?';
$string['disabledwarning'] = 'All webs service protocols are disabled, the \Enable web services\" setting can be found in the \"Advanced features\" section.';
$string['enabled'] = 'Enabled';
$string['execute'] = 'Execute';
$string['executewarnign'] = 'WARNING: if you press execute your database will be modified and changes can not be reverted automatically!';
$string['externalservices'] = 'External services';
$string['externalservice'] = 'External service';
$string['externalservicefunctions'] = 'External service functions';
@ -30,7 +32,6 @@ $string['servicescustom'] = 'Custom services';
$string['serviceusers'] = 'Authorised users';
$string['serviceusersmatching'] = 'Authorised users matching';
$string['serviceuserssettings'] = 'Change settings for the authorised users';
$string['test'] = 'Test';
$string['testclient'] = 'Web service test client';
$string['validuntil'] = 'Valid until';
$string['webservices'] = 'Web services';

View File

@ -27,18 +27,21 @@
$functions = array(
// === group related functions ===
/*
'moodle_group_create_groups' => array(
'classname' => 'moodle_group_external',
'methodname' => 'create_groups',
'classpath' => 'group/externallib.php',
'description' => 'Creates new groups.',
'type' => 'write',
),
*/
'moodle_group_get_groups' => array(
'classname' => 'moodle_group_external',
'methodname' => 'get_groups',
'classpath' => 'group/externallib.php',
'description' => 'Returns group details.',
'type' => 'read',
),
/*
'moodle_group_delete_groups' => array(

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 = 2009102100; // YYYYMMDD = date of the last version bump
$version = 2009102200; // YYYYMMDD = date of the last version bump
// XX = daily increments
$release = '2.0 dev (Build: 20091022)'; // Human-friendly version name

View File

@ -36,7 +36,7 @@ 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_get_groups');
$functions = array('moodle_group_create_groups', 'moodle_group_get_groups');
$functions = array_combine($functions, $functions);
if (!isset($functions[$function])) { // whitelisting security
$function = '';
@ -96,7 +96,11 @@ if ($mform->is_cancelled()) {
// now get the function parameters - each functions processing must be hardcoded here
$params = array();
if ($function === 'moodle_group_get_groups') {
if ($function === 'moodle_group_create_groups') {
$params['groups'] = array();
$params['groups'][] = (array)$data;
} else if ($function === 'moodle_group_get_groups') {
$params['groupids'] = array();
for ($i=0; $i<10; $i++) {
if (empty($data->groupids[$i])) {
@ -118,7 +122,7 @@ if ($mform->is_cancelled()) {
try {
$response = $testclient->simpletest($serverurl, $function, $params);
echo str_replace("\n", '<br />', s(var_export($response, true)));
echo str_replace("\n", '<br />', s($response));
} catch (Exception $ex) {
//TODO: handle exceptions and faults without exposing of the sensitive information such as debug traces!
echo str_replace("\n", '<br />', s($ex));

View File

@ -20,6 +20,8 @@ class webservice_test_client_form extends moodleform {
}
}
// === Test client forms ===
class moodle_group_get_groups_form extends moodleform {
public function definition() {
global $CFG;
@ -42,6 +44,34 @@ class moodle_group_get_groups_form extends moodleform {
$mform->addElement('hidden', 'protocol');
$mform->setType('protocol', PARAM_SAFEDIR);
$this->add_action_buttons(true, get_string('test', 'webservice'));
$this->add_action_buttons(true, get_string('execute', 'webservice'));
}
}
}
class moodle_group_create_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('text', 'name', 'name');
$mform->addElement('text', 'description', 'description');
$mform->addElement('text', 'enrolmentkey', 'enrolmentkey');
$mform->addElement('hidden', 'function');
$mform->setType('function', PARAM_SAFEDIR);
$mform->addElement('hidden', 'protocol');
$mform->setType('protocol', PARAM_SAFEDIR);
$mform->addElement('static', 'warning', '', get_string('executewarnign', 'webservice'));
$this->add_action_buttons(true, get_string('execute', 'webservice'));
}
}