]> git.mjollnir.org Git - moodle.git/commitdiff
web service MDL-12886 manage optional parameters
authorjerome <jerome>
Thu, 22 Jan 2009 08:03:33 +0000 (08:03 +0000)
committerjerome <jerome>
Thu, 22 Jan 2009 08:03:33 +0000 (08:03 +0000)
user/external.php
webservice/rest/locallib.php
webservice/soap/moodle.wsdl
webservice/soap/testclient/index.php

index fcd488a10b1e5caa8056f735503e4e8a94d4c6c2..4e74939c51eef53389c3b29d69bebb74777656ec 100644 (file)
@@ -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);
     }
 
index d5848133f4ce90da194278f2a0da38f5ea3661c6..064825a912a086032aa84ea3b7c98ecef13512b2 100644 (file)
@@ -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 <type>
  */
 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;
 }
index 5849f620b2197b0fbdcbcc7c77be94dcd9711199..fcaa5b049be5711323f8fa5d2de6e7c33a597a5c 100644 (file)
         </types>
 
         <message name="tmp_get_usersRequest">
-            <part name="search" type="xsd:string"/>
+            <part name="params" type="xsd:object"/>
         </message>
         <message name="tmp_get_usersResponse">
             <part name="user" type="xsd:object"/>
         </message>
         <message name="tmp_create_userRequest">
-            <part name="username" type="xsd:string"/>
-            <part name="firstname" type="xsd:string"/>
-
-            <part name="lastname" type="xsd:string"/>
-            <part name="email" type="xsd:string"/>
-            <part name="password" type="xsd:string"/>
+            <part name="params" type="xsd:object"/>
         </message>
         <message name="tmp_create_userResponse">
             <part name="userid" type="xsd:string"/>
             <part name="user" type="xsd:object"/>
         </message>
         <message name="tmp_delete_userRequest">
-            <part name="username" type="xsd:string"/>
-            <part name="mnethostid" type="xsd:integer"/>
+             <part name="params" type="xsd:object"/>
         </message>
         <message name="tmp_delete_userResponse">
 
             <part name="result" type="xsd:object"/>
         </message>
         <message name="tmp_update_userRequest">
-            <part name="username" type="xsd:string"/>
-            <part name="mnethostid" type="xsd:integer"/>
-            <part name="newusername" type="xsd:string"/>
-            <part name="firstname" type="xsd:string"/>
+             <part name="params" type="xsd:object"/>
         </message>
         <message name="tmp_update_userResponse">
 
index 22b6eb8b37886e91d14810a6b1f157188ff9b16c..fbc5a3575983423046707405e48f1e2a008b5966 100644 (file)
@@ -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;