*/
protected function clean_function_params($functionname, &$params) {
$description = $this->get_function_webservice_description($functionname);
- varlog($functionname);
- foreach ($params as $param) { //we are applying the algo for all params
- $key = key($description['params']); //get next key of the description array => params need to be ordered !
- $this->clean_params($description['params'][$key], $param);
-
- }
+ $this->clean_object($description['params'], $params);
}
/**
* @param <type> $params
*/
protected function clean_params($description, &$params) {
- if (!is_array($params)) {
- $paramvalue = clean_param($params, $description);
- } else {
- foreach ($params as $paramname => &$paramvalue) {
- if (is_array($paramvalue)) { //it's a list
- //A description array does not support list of different objects
- //it's why we retrieve the first key, because there should be only one key
- $this->clean_params($description[key($description)], $paramvalue);
+
+ if (is_array($params) ) { //it's a list
+ $nextdescriptionkey = key($description);
+ if (isset($nextdescriptionkey)) {
+ $this->clean_params($description[$nextdescriptionkey], $params[key($params)]);
+ } else {
+ throw new moodle_exception('wswrongparams');
}
- else {
- if (is_object($paramvalue)) { //is it a object
- $this->clean_object_types($description[$paramname], $paramvalue);
- }
- else { //it's a primary type
- $paramvalue = clean_param($paramvalue, $description[$paramname]);
- }
+ }
+ else {
+ if (is_object($params)) { //is it a object
+ $this->clean_object($description, $params);
+ }
+ else { //it's a primary type
+ $params = clean_param($params, $description);
}
-
-
}
- }
}
- protected function clean_object_types($objectdescription, &$paramobject) {
+ protected function clean_object($objectdescription, &$paramobject) {
foreach (get_object_vars($paramobject) as $propertyname => $propertyvalue) {
if (is_array($propertyvalue)) {
- $this->clean_params($objectdescription->$propertyname, $propertyvalue);
- $paramobject->$propertyname = $propertyvalue;
+ if (isset($objectdescription->$propertyname)) {
+ $this->clean_params($objectdescription->$propertyname, $propertyvalue);
+ $paramobject->$propertyname = $propertyvalue;
+ } else {
+ throw new moodle_exception('wswrongparams');
+ }
} else {
- $paramobject->$propertyname = clean_param($propertyvalue, $objectdescription->$propertyname);
-
+ if (isset($objectdescription->$propertyname)) {
+ $paramobject->$propertyname = clean_param($propertyvalue, $objectdescription->$propertyname);
+ } else {
+ throw new moodle_exception('wswrongparams');
+ }
}
}
}
$this->descriptions = array();
///The desciption of the web service
- $user = new stdClass();
+ $user = new object();
$user->password = PARAM_ALPHANUMEXT;
$user->auth = PARAM_ALPHANUMEXT;
$user->confirmed = PARAM_NUMBER;
$user->description = PARAM_TEXT;
$user->city = PARAM_ALPHANUMEXT;
$user->country = PARAM_ALPHANUMEXT;
-
- $this->descriptions['create_users'] = array( 'params' => array('users' => array($user)),
- 'optionalinformation' => 'All params are not mandatory',
- 'return' => array('userids' => array(PARAM_NUMBER)));
-
- $user = new stdClass();
+ $params = new object();
+ $params->users = array($user);
+ $return = new object();
+ $return->userids = array(PARAM_NUMBER);
+ $this->descriptions['create_users'] = array( 'params' => $params,
+ 'optionalinformation' => 'Username, password, firstname, and username are the only mandatory',
+ 'return' => $return);
+
+ $user = new object();
$user->id = PARAM_NUMBER;
$user->auth = PARAM_ALPHANUMEXT;
$user->confirmed = PARAM_NUMBER;
$user->description = PARAM_TEXT;
$user->city = PARAM_ALPHANUMEXT;
$user->country = PARAM_ALPHANUMEXT;
-
- $this->descriptions['get_users'] = array( 'params' => array('search'=> PARAM_ALPHANUM),
+ $params = new object();
+ $params->search = PARAM_ALPHANUM;
+ $return = new object();
+ $return->users = array($user);
+ $this->descriptions['get_users'] = array( 'params' => $params,
'optionalparams' => 'All params are not mandatory',
- 'return' => array('user' => array( $user)));
+ 'return' => $return);
-
- $this->descriptions['delete_users'] = array( 'params' => array('usernames' => array(PARAM_ALPHANUMEXT)),
+ $params = new object();
+ $params->usernames = array(PARAM_ALPHANUMEXT);
+ $return = new object();
+ $return->result = PARAM_BOOL;
+ $this->descriptions['delete_users'] = array( 'params' => $params,
'optionalparams' => 'All params are not mandatory',
- 'return' => array('result' => PARAM_BOOL));
+ 'return' => $return);
$user->newusername = PARAM_ALPHANUMEXT;
- $this->descriptions['update_users'] = array( 'params' => array('users' => array($user),
+ $params = new object();
+ $params->users = array($user);
+ $this->descriptions['update_users'] = array( 'params' => $params,
'optionalparams' => 'All params are not mandatory',
- 'return' => array('result' => PARAM_BOOL)));
+ 'return' => $return);
}
/**
* Retrieve all user
- * @param array|struct $params - need to be define as struct for XMLRPC
+ * @param object|struct $params - need to be define as struct for XMLRPC
* @return object $return
*/
public function get_users($params) {
$this->clean_function_params('get_users', $params);
if (has_capability('moodle/user:viewdetails', get_context_instance(CONTEXT_SYSTEM))) {
- return get_users(true, $params['search'], false, null, 'firstname ASC','', '', '', 1000, 'id, auth, confirmed, username, idnumber, firstname, lastname, email, emailstop, lang, theme, timezone, mailformat, city, description, country');
+ return get_users(true, $params->search, false, null, 'firstname ASC','', '', '', 1000, 'id, auth, confirmed, username, idnumber, firstname, lastname, email, emailstop, lang, theme, timezone, mailformat, city, description, country');
}
else {
throw new moodle_exception('wscouldnotvieweusernopermission');
/**
* Create multiple users
- * @param array|struct $params - need to be define as struct for XMLRPC
- * @return array $return ids of new user
+ * @param object|struct $params - need to be define as struct for XMLRPC
+ * @return object $return
*/
public function create_users($params) {
global $USER;
if (has_capability('moodle/user:create', get_context_instance(CONTEXT_SYSTEM))) {
$userids = array();
$this->clean_function_params('create_users', $params);
- foreach ($params['users'] as $user) {
+ foreach ($params->users as $user) {
try {
$userids[$user->username] = create_user($user);
}
/**
* Delete multiple users
* @global object $DB
- * @param array|struct $params - need to be define as struct for XMLRPC
+ * @param object|struct $params - need to be define as struct for XMLRPC
* @return boolean result true if success
*/
public function delete_users($params) {
if (has_capability('moodle/user:delete', get_context_instance(CONTEXT_SYSTEM))) {
$this->clean_function_params('delete_users', $params);
- foreach ($params['usernames'] as $username) {
+ foreach ($params->usernames as $username) {
$user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>1));
if (empty($user)) {
throw new moodle_exception('wscouldnotdeletenoexistinguser');
/**
* Update some users information
* @global object $DB
- * @param array|struct $params - need to be define as struct for XMLRPC
+ * @param object|struct $params - need to be define as struct for XMLRPC
* @return boolean result true if success
*/
public function update_users($params) {
$this->clean_function_params('update_users', $params);
- foreach ($params['users'] as $paramuser) {
+ foreach ($params->users as $paramuser) {
$user = $DB->get_record('user', array('username'=> $paramuser->username, 'mnethostid'=>1));
$params = new stdClass();
$params->username = 'wsuser';
$params->password = 'wspassword';
- $token = $client->get_token(array('username' => "wsuser", 'password' => "wspassword"));
+ $token = $client->get_token($params);
//$token = $client->get_token($params);
print "<pre>\n";
print "<br><br><strong>Token: </strong>".$token;
print "<br><br><strong>Get users:</strong>";
print "<pre>\n";
try {
- var_dump($client->get_users(array('search' => "admin")));
+ $params = new stdClass();
+ $params->search = "admin";
+ var_dump($client->get_users($params));
} catch (exception $exception) {
print $exception;
print "<br><br><strong>An exception occured: \n</strong>";
print "<br><br><strong>Create user:</strong>";
print "<pre>\n";
try {
+
$user = new stdClass();
$user->password = "password6";
$user->email = "mockuser6@mockuser6.com";
$user->username = "mockuser66";
$user->firstname = "firstname6";
$user->lastname = "lastname6";
- var_dump($client->create_users(array('users' => array($user))));
+ $params = new stdClass();
+ $params->users = array($user);
+ var_dump($client->create_users($params));
} catch (exception $exception) {
print $exception;
print "<br><br><strong>An exception occured: \n</strong>";
print "<br><br><strong>Update user:</strong>";
print "<pre>\n";
try {
- $user1 = new stdClass();
- $user1->email = "mockuser6@mockuser6.com";
- $user1->username = "mockuser66";
- $user1->newusername = 'mockuser6b';
- $user1->firstname = "firstname6b";
- var_dump($client->update_users(array('users' => array($user1))));
+ $usertoupdate = new stdClass();
+ $usertoupdate->email = "mockuser6@mockuser6.com";
+ $usertoupdate->username = "mockuser66";
+ $usertoupdate->newusername = 'mockuser6b';
+ $usertoupdate->firstname = "firstname6b";
+ $params = new stdClass();
+ $params->users = array($usertoupdate);
+ var_dump($client->update_users($params));
} catch (exception $exception) {
print $exception;
print "<br><br><strong>An exception occured: \n</strong>";
print "<br><br><strong>Delete user:</strong>";
print "<pre>\n";
try {
- var_dump($client->delete_users(array('usernames' => array("mockuser6b"))));
+ $params = new stdClass();
+ $params->usernames = array("mockuser6b");
+ var_dump($client->delete_users($params));
} catch (exception $exception) {
print $exception;
print "<br><br><strong>An exception occured: \n</strong>";