/**
* 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')),
)
);
}
$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);
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'),
)
)
);
// 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 = '';
// 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])) {
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));
}
}
+// === Test client forms ===
+
class moodle_group_get_groups_form extends moodleform {
public function definition() {
global $CFG;
$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'));
+ }
+}