Skip to content

Commit

Permalink
MDL-12886 refactoring: improved test client forms support; fixing ext…
Browse files Browse the repository at this point in the history
…ernal group functions
  • Loading branch information
skodak committed Oct 27, 2009
1 parent 3c2fe13 commit 2cb1ee7
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 41 deletions.
6 changes: 3 additions & 3 deletions group/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public static function delete_groups_parameters() {
* @return void
*/
public static function delete_groups($groupids) {
global $CFG;
global $CFG, $DB;
require_once("$CFG->dirroot/group/lib.php");

$params = self::validate_parameters(self::delete_groups_parameters(), array('groupids'=>$groupids));
Expand Down Expand Up @@ -352,7 +352,7 @@ public static function add_groupmembers_parameters() {
* @return void
*/
public static function add_groupmembers($members) {
global $CFG;
global $CFG, $DB;
require_once("$CFG->dirroot/group/lib.php");

$params = self::validate_parameters(self::add_groupmembers_parameters(), array('members'=>$members));
Expand Down Expand Up @@ -412,7 +412,7 @@ public static function delete_groupmembers_parameters() {
* @return void
*/
public static function delete_groupmembers($members) {
global $CFG;
global $CFG, $DB;
require_once("$CFG->dirroot/group/lib.php");

$params = self::validate_parameters(self::delete_groupmembers_parameters(), array($members=>'members'));
Expand Down
4 changes: 2 additions & 2 deletions lib/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@
'description' => 'Returns all groups in specified course.',
'type' => 'read',
),
/*

'moodle_group_delete_groups' => array(
'classname' => 'moodle_group_external',
'methodname' => 'delete_groups',
'classpath' => 'group/externallib.php',
'description' => 'Deletes all specified groups.',
'type' => 'delete',
),
/*
'moodle_group_get_groupmembers' => array(
'classname' => 'moodle_group_external',
'methodname' => 'get_groupmembers',
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 = 2009102600; // YYYYMMDD = date of the last version bump
$version = 2009102700; // YYYYMMDD = date of the last version bump
// XX = daily increments

$release = '2.0 dev (Build: 20091027)'; // Human-friendly version name
52 changes: 17 additions & 35 deletions webservice/testclient.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*
* @package webservice
* @copyright 2009 Moodle Pty Ltd (http://moodle.com)
* @author Petr Skoda (skodak)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

Expand All @@ -35,12 +36,19 @@
require_login();
require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM)); // TODO: do we need some new capability?
// 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
// 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
// list of all available functions for testing
$allfunctions = $DB->get_records('external_functions', array(), 'name ASC');
$functions = array();
foreach ($allfunctions as $f) {
$class = $f->name.'_form';
if (class_exists($class)) {
$functions[$f->name] = $f->name;
continue;
}
}

// whitelisting security
if (!isset($functions[$function])) {
$function = '';
}

Expand Down Expand Up @@ -83,11 +91,6 @@
} else if ($data = $mform->get_data()) {

$functioninfo = external_function_info($function);

// remove unused from form data
unset($data->submitbutton);
unset($data->protocol);
unset($data->function);

// first load lib of selected protocol
require_once("$CFG->dirroot/webservice/$protocol/locallib.php");
Expand All @@ -100,31 +103,10 @@

$serverurl = "$CFG->wwwroot/webservice/$protocol/simpleserver.php";
$serverurl .= '?wsusername='.urlencode($data->wsusername);
unset($data->wsusername);
$serverurl .= '&wspassword='.urlencode($data->wspassword);
unset($data->wspassword);

// now get the function parameters - each functions processing must be hardcoded here
$params = array();
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])) {
continue;
}
$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!');
}

// now get the function parameters
$params = $mform->get_params();

// now test the parameters, this also fixes PHP data types
$params = external_api::validate_parameters($functioninfo->parameters_desc, $params);
Expand Down
108 changes: 108 additions & 0 deletions webservice/testclient_forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ public function definition() {

$this->add_action_buttons(true, get_string('execute', 'webservice'));
}

public function get_params() {
if (!$data = $this->get_data()) {
return null;
}
// remove unused from form data
unset($data->submitbutton);
unset($data->protocol);
unset($data->function);
unset($data->wsusername);
unset($data->wspassword);

$params = array();
$params['groups'] = array();
$params['groups'][] = (array)$data;

return $params;
}
}

class moodle_group_get_groups_form extends moodleform {
Expand All @@ -74,6 +92,29 @@ public function definition() {

$this->add_action_buttons(true, get_string('execute', 'webservice'));
}

public function get_params() {
if (!$data = $this->get_data()) {
return null;
}
// remove unused from form data
unset($data->submitbutton);
unset($data->protocol);
unset($data->function);
unset($data->wsusername);
unset($data->wspassword);

$params = array();
$params['groupids'] = array();
for ($i=0; $i<10; $i++) {
if (empty($data->groupids[$i])) {
continue;
}
$params['groupids'][] = $data->groupids[$i];
}

return $params;
}
}

class moodle_group_get_course_groups_form extends moodleform {
Expand All @@ -97,5 +138,72 @@ public function definition() {

$this->add_action_buttons(true, get_string('execute', 'webservice'));
}

public function get_params() {
if (!$data = $this->get_data()) {
return null;
}
// remove unused from form data
unset($data->submitbutton);
unset($data->protocol);
unset($data->function);
unset($data->wsusername);
unset($data->wspassword);

$params = array();
$params['courseid'] = $data->courseid;

return $params;
}
}

class moodle_group_delete_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);

$mform->addElement('static', 'warning', '', get_string('executewarnign', 'webservice'));

$this->add_action_buttons(true, get_string('execute', 'webservice'));
}

public function get_params() {
if (!$data = $this->get_data()) {
return null;
}
// remove unused from form data
unset($data->submitbutton);
unset($data->protocol);
unset($data->function);
unset($data->wsusername);
unset($data->wspassword);

$params = array();
$params['groupids'] = array();
for ($i=0; $i<10; $i++) {
if (empty($data->groupids[$i])) {
continue;
}
$params['groupids'][] = $data->groupids[$i];
}

return $params;
}
}

0 comments on commit 2cb1ee7

Please sign in to comment.