From: skodak Date: Thu, 22 Oct 2009 23:07:21 +0000 (+0000) Subject: MDL-12886 one more external group function, other minor fixes X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=f507217757c53f461b86682a4b849484c86cc74a;p=moodle.git MDL-12886 one more external group function, other minor fixes --- diff --git a/group/externallib.php b/group/externallib.php index a0af9d4741..800f12ec4b 100644 --- a/group/externallib.php +++ b/group/externallib.php @@ -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(); - - foreach ($params['groups'] as $group) { - $group = (object)$group; - - 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'); + // 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; + + 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; } - - // 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'), ) ) ); diff --git a/lang/en_utf8/webservice.php b/lang/en_utf8/webservice.php index ae122ab3a0..624fc98944 100644 --- a/lang/en_utf8/webservice.php +++ b/lang/en_utf8/webservice.php @@ -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'; diff --git a/lib/db/services.php b/lib/db/services.php index 25e582430e..72a30600c3 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -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( diff --git a/version.php b/version.php index 635b87e79f..8ef71103da 100644 --- a/version.php +++ b/version.php @@ -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 diff --git a/webservice/testclient.php b/webservice/testclient.php index 38a715ce05..c2e6829fff 100644 --- a/webservice/testclient.php +++ b/webservice/testclient.php @@ -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", '
', s(var_export($response, true))); + echo str_replace("\n", '
', s($response)); } catch (Exception $ex) { //TODO: handle exceptions and faults without exposing of the sensitive information such as debug traces! echo str_replace("\n", '
', s($ex)); diff --git a/webservice/testclient_forms.php b/webservice/testclient_forms.php index ea0a6ca8d2..0221e9974c 100644 --- a/webservice/testclient_forms.php +++ b/webservice/testclient_forms.php @@ -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')); } -} \ No newline at end of file +} + +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')); + } +}