From b721783ad4dae71c798d96c351c026404189e940 Mon Sep 17 00:00:00 2001 From: jerome Date: Thu, 22 Jan 2009 08:03:33 +0000 Subject: [PATCH] web service MDL-12886 manage optional parameters --- user/external.php | 87 ++++++++++++++++------------ webservice/rest/locallib.php | 14 ++++- webservice/soap/moodle.wsdl | 17 ++---- webservice/soap/testclient/index.php | 8 +-- 4 files changed, 70 insertions(+), 56 deletions(-) diff --git a/user/external.php b/user/external.php index fcd488a10b..4e74939c51 100644 --- a/user/external.php +++ b/user/external.php @@ -25,61 +25,69 @@ final class user_external extends moodle_external { /// ///Note: web services param names have not importance. However 'paramorder' must match the function params order. ///And all web services param names defined into 'wsparams' should be included into 'paramorder' (otherwise they will not be used) - $this->descriptions['tmp_create_user'] = array( 'wsparams' => array('username'=> PARAM_RAW, 'firstname'=> PARAM_RAW, 'lastname'=> PARAM_RAW, 'email'=> PARAM_RAW, 'password'=> PARAM_RAW), - 'return' => array('userid' => PARAM_RAW)); + $this->descriptions['tmp_create_user'] = array( 'params' => array('username'=> PARAM_RAW, 'firstname'=> PARAM_RAW, 'lastname'=> PARAM_RAW, 'email'=> PARAM_RAW, 'password'=> PARAM_RAW), + 'optionalparams' => array( ), + 'return' => array('userid' => PARAM_RAW)); - $this->descriptions['tmp_get_users'] = array( 'wsparams' => array('search'=> PARAM_ALPHANUM), - 'return' => array('user' => array('id' => PARAM_RAW, 'auth' => PARAM_RAW, 'confirmed' => PARAM_RAW, 'username' => PARAM_RAW, 'idnumber' => PARAM_RAW, + $this->descriptions['tmp_get_users'] = array( 'params' => array('search'=> PARAM_ALPHANUM), + 'optionalparams' => array( ), + 'return' => array('user' => array('id' => PARAM_RAW, 'auth' => PARAM_RAW, 'confirmed' => PARAM_RAW, 'username' => PARAM_RAW, 'idnumber' => PARAM_RAW, 'firstname' => PARAM_RAW, 'lastname' => PARAM_RAW, 'email' => PARAM_RAW, 'emailstop' => PARAM_RAW, 'lang' => PARAM_RAW, 'theme' => PARAM_RAW, 'timezone' => PARAM_RAW, 'mailformat' => PARAM_RAW))); - $this->descriptions['tmp_delete_user'] = array( 'wsparams' => array('username'=> PARAM_ALPHANUM, 'mnethostid'=> PARAM_NUMBER), - 'return' => array('result' => PARAM_BOOL)); + $this->descriptions['tmp_delete_user'] = array( 'params' => array('username'=> PARAM_ALPHANUM, 'mnethostid'=> PARAM_NUMBER), + 'optionalparams' => array( ), + 'return' => array('result' => PARAM_BOOL)); - $this->descriptions['tmp_update_user'] = array( 'wsparams' => array('username'=> PARAM_ALPHANUM, 'mnethostid'=> PARAM_NUMBER, 'newusername' => PARAM_ALPHANUM, 'firstname' => PARAM_ALPHANUM), - 'return' => array('result' => PARAM_BOOL)); + $this->descriptions['tmp_update_user'] = array( 'params' => array('username'=> PARAM_ALPHANUM, 'mnethostid'=> PARAM_NUMBER), + 'optionalparams' => array( 'newusername' => PARAM_ALPHANUM, 'firstname' => PARAM_ALPHANUM), + 'return' => array('result' => PARAM_BOOL)); } /** * Retrieve all user - * @param string $search + * @param array $params + * ->search string * @return object user */ - static function tmp_get_users($search) { + static function tmp_get_users($params) { + $selectioncriteria = new stdClass(); - $selectioncriteria->search = $search; + $selectioncriteria->search = $params['search']; return user_api::tmp_get_users('firstname ASC', 999999, 0, 'id, auth, confirmed, username, idnumber, firstname, lastname, email, emailstop, lang, theme, timezone, mailformat', $selectioncriteria); } /** * Create a user - * @param string $username - * @param string $firstname - * @param string $lastname - * @param string $email - * @param string $password + * @param array $params + * @param $username string + * ->firstname string + * ->lastname string + * ->email string + * ->password string * @return integer id of new user */ - static function tmp_create_user($username, $firstname, $lastname, $email, $password) { + static function tmp_create_user($params) { $user = array(); - $user['username'] = $username; - $user['firstname'] = $firstname; - $user['lastname'] = $lastname; - $user['email'] = $email; - $user['password'] = $password; + $user['username'] = $params['username']; + $user['firstname'] = $params['firstname']; + $user['lastname'] = $params['lastname']; + $user['email'] = $params['email']; + $user['password'] = $params['password']; return user_api::tmp_create_user($user); } /** * Delete a user * @global object $DB - * @param string $username - * @param integer $mnethostid + * @param array $params + * ->username string + * ->mnethostid integer * @return boolean true if success */ - static function tmp_delete_user($username, $mnethostid) { + static function tmp_delete_user($params) { global $DB; - $user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$mnethostid)); + $user = $DB->get_record('user', array('username'=>$params['username'], 'mnethostid'=>$params['mnethostid'])); /// PLEASE UNCOMMENT HERE ONCE AUTHENTICATION IS IMPLEMENTED - $USER/context need to be set here // if (has_capability('moodle/user:delete', get_context_instance(CONTEXT_SYSTEM))) { return delete_user($user); //this function is in moodlelib.php @@ -89,21 +97,28 @@ final class user_external extends moodle_external { // } } + /** * Update some user information * @global object $DB - * @param string $username - * @param integer $mnethostid - * @param string $newusername - * @param string $firstname - * @return boolean true if success + * @param array $params + * ->username string + * ->mnethostid integer + * ->newusername string + * ->firstname string + * @return bool true if success */ - static function tmp_update_user($username, $mnethostid, $newusername, $firstname) { + static function tmp_update_user($params) { global $DB; - $user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$mnethostid)); - $user->username = $newusername; - $user->firstname = $firstname; - + $user = $DB->get_record('user', array('username'=>$params['username'], 'mnethostid'=>$params['mnethostid'])); + + if (!empty($params['newusername'])) { + $user->username = $params['newusername']; + } + if (!empty($params['firstname'])) { + $user->firstname = $params['firstname']; + } + return user_api::tmp_update_user($user); } diff --git a/webservice/rest/locallib.php b/webservice/rest/locallib.php index d5848133f4..064825a912 100644 --- a/webservice/rest/locallib.php +++ b/webservice/rest/locallib.php @@ -35,7 +35,7 @@ function call_moodle_function ($rest_arguments) { if ($params === false) { //return an error message, the REST params doesn't match with the web service description } - $res = call_user_func_array ( $classname.'::'.$functionname, $params); + $res = call_user_func_array ( $classname.'::'.$functionname, array($params)); ///Transform result into xml in order to send the REST response $return = mdl_conn_rest_object_to_xml ($res,key($description['return'])); @@ -51,13 +51,21 @@ function call_moodle_function ($rest_arguments) { * @return */ function retrieve_params ($description) { + $params = array(); //retrieve REST param matching the description (warning: PHP assign the first instanciation as the first position in the table) - foreach ($description['wsparams'] as $paramname => $paramtype) { + foreach ($description['params'] as $paramname => $paramtype) { $value = optional_param($paramname,null,$paramtype); if (!empty($value)) { $params[$paramname] = $value; - } } + } + //retrieve REST optional params + foreach ($description['optionalparams'] as $paramname => $paramtype) { + $value = optional_param($paramname,null,$paramtype); + if (!empty($value)) { + $params[$paramname] = $value; + } + } return $params; } diff --git a/webservice/soap/moodle.wsdl b/webservice/soap/moodle.wsdl index 5849f620b2..fcaa5b049b 100644 --- a/webservice/soap/moodle.wsdl +++ b/webservice/soap/moodle.wsdl @@ -17,18 +17,13 @@ - + - - - - - - + @@ -41,18 +36,14 @@ - - + - - - - + diff --git a/webservice/soap/testclient/index.php b/webservice/soap/testclient/index.php index 22b6eb8b37..fbc5a35759 100644 --- a/webservice/soap/testclient/index.php +++ b/webservice/soap/testclient/index.php @@ -20,13 +20,13 @@ $client = new SoapClient("../moodle.wsdl",array( "exceptions" => 0)); try { - var_dump($client->tmp_get_users("admin")); + var_dump($client->tmp_get_users(array('search' => "admin"))); printLastRequestResponse($client); - var_dump($client->tmp_create_user("mockuser6","firstname6","lastname6","mockuser6@mockuser6.com", "password6")); + var_dump($client->tmp_create_user(array('username' => "mockuser66",'firstname' => "firstname6",'lastname' => "lastname6",'email' => "mockuser6@mockuser6.com",'password' => "password6"))); printLastRequestResponse($client); - var_dump($client->tmp_update_user("mockuser6",1,"mockuser6b","firstname6b")); + var_dump($client->tmp_update_user(array('username' => "mockuser66",'mnethostid' => 1,'newusername' => "mockuser6b",'firstname' => "firstname6b"))); printLastRequestResponse($client); - var_dump($client->tmp_delete_user("mockuser6b",1)); + var_dump($client->tmp_delete_user(array('username' => "mockuser6b",'mnethostid' => 1))); printLastRequestResponse($client); } catch (SoapFault $exception) { echo $exception; -- 2.39.5