Skip to content

Commit

Permalink
MDL-12886 one more external group function, other minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Oct 22, 2009
1 parent f507217 commit 246f6da
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 13 deletions.
58 changes: 55 additions & 3 deletions group/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,14 @@ public static function get_groups_parameters() {
}

/**
* 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);
Expand Down Expand Up @@ -166,6 +165,59 @@ public static function get_groups_returns() {
);
}

/**
* 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
}
Expand Down
9 changes: 9 additions & 0 deletions lib/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@
'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',
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions webservice/rest/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ protected static function xmlize_result($returns, $desc) {
$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
Expand Down
6 changes: 5 additions & 1 deletion webservice/testclient.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
// 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 = '';
Expand Down Expand Up @@ -109,6 +110,9 @@
$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!');
}
Expand Down
36 changes: 30 additions & 6 deletions webservice/testclient_forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,34 @@ public function definition() {

// === Test client forms ===

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'));
}
}

class moodle_group_get_groups_form extends moodleform {
public function definition() {
global $CFG;
Expand All @@ -48,7 +76,7 @@ public function definition() {
}
}

class moodle_group_create_groups_form extends moodleform {
class moodle_group_get_course_groups_form extends moodleform {
public function definition() {
global $CFG;

Expand All @@ -60,18 +88,14 @@ public function definition() {
$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'));
}
}

0 comments on commit 246f6da

Please sign in to comment.