From 2cb1ee78e4f009211f9708a751734b27cd6c2895 Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Tue, 27 Oct 2009 09:27:34 +0000 Subject: [PATCH] MDL-12886 refactoring: improved test client forms support; fixing external group functions --- group/externallib.php | 6 +- lib/db/services.php | 4 +- version.php | 2 +- webservice/testclient.php | 52 +++++---------- webservice/testclient_forms.php | 108 ++++++++++++++++++++++++++++++++ 5 files changed, 131 insertions(+), 41 deletions(-) diff --git a/group/externallib.php b/group/externallib.php index c6fec61b64..e58bf70edd 100644 --- a/group/externallib.php +++ b/group/externallib.php @@ -237,7 +237,7 @@ class moodle_group_external extends external_api { * @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)); @@ -352,7 +352,7 @@ class moodle_group_external extends external_api { * @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)); @@ -412,7 +412,7 @@ class moodle_group_external extends external_api { * @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')); diff --git a/lib/db/services.php b/lib/db/services.php index 18111ccb7c..c0acfbd57d 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -51,7 +51,7 @@ $functions = array( 'description' => 'Returns all groups in specified course.', 'type' => 'read', ), -/* + 'moodle_group_delete_groups' => array( 'classname' => 'moodle_group_external', 'methodname' => 'delete_groups', @@ -59,7 +59,7 @@ $functions = array( 'description' => 'Deletes all specified groups.', 'type' => 'delete', ), - +/* 'moodle_group_get_groupmembers' => array( 'classname' => 'moodle_group_external', 'methodname' => 'get_groupmembers', diff --git a/version.php b/version.php index 667a99b778..b8e5c7b55b 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 = 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 diff --git a/webservice/testclient.php b/webservice/testclient.php index 22126378ab..f4f0cb3e6d 100644 --- a/webservice/testclient.php +++ b/webservice/testclient.php @@ -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 */ @@ -35,12 +36,19 @@ $PAGE->set_url('webservice/testclient.php'); 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 = ''; } @@ -83,11 +91,6 @@ if ($mform->is_cancelled()) { } 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"); @@ -100,31 +103,10 @@ if ($mform->is_cancelled()) { $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); diff --git a/webservice/testclient_forms.php b/webservice/testclient_forms.php index 08ddef3a3a..687db3cedd 100644 --- a/webservice/testclient_forms.php +++ b/webservice/testclient_forms.php @@ -48,6 +48,24 @@ class moodle_group_create_groups_form extends moodleform { $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 { @@ -74,6 +92,29 @@ class moodle_group_get_groups_form extends moodleform { $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 { @@ -97,5 +138,72 @@ class moodle_group_get_course_groups_form extends moodleform { $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; + } +} -- 2.39.5