From d9ad01032761efe3d3ebdb3684b0baaf34376ea6 Mon Sep 17 00:00:00 2001 From: jerome mouneyrac Date: Thu, 8 Jul 2010 08:40:21 +0000 Subject: [PATCH] webservice MDL-22589 support user preferences for creation and update --- user/externallib.php | 33 ++++++++++++++++++++++++++------- webservice/lib.php | 4 ++-- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/user/externallib.php b/user/externallib.php index c5e3f5b8f71..89245e98434 100644 --- a/user/externallib.php +++ b/user/externallib.php @@ -52,7 +52,7 @@ class moodle_user_external extends external_api { 'theme' => new external_value(PARAM_SAFEDIR, 'Theme name such as "standard", must exist on server', VALUE_OPTIONAL), 'timezone' => new external_value(PARAM_ALPHANUMEXT, 'Timezone code such as Australia/Perth, or 99 for default', VALUE_OPTIONAL), 'mailformat' => new external_value(PARAM_INTEGER, 'Mail format code is 0 for plain text, 1 for HTML etc', VALUE_OPTIONAL), - 'description' => new external_value(PARAM_TEXT, 'User profile description, as HTML', VALUE_OPTIONAL), + 'description' => new external_value(PARAM_TEXT, 'User profile description, no HTML', VALUE_OPTIONAL), 'city' => new external_value(PARAM_NOTAGS, 'Home city of the user', VALUE_OPTIONAL), 'country' => new external_value(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL), 'preferences' => new external_multiple_structure( @@ -88,16 +88,16 @@ class moodle_user_external extends external_api { require_once($CFG->dirroot."/user/profile/lib.php"); //required for customfields related function //TODO: move the functions somewhere else as //they are "user" related - + // Ensure the current user is allowed to run this function $context = get_context_instance(CONTEXT_SYSTEM); self::validate_context($context); require_capability('moodle/user:create', $context); - + // Do basic automatic PARAM checks on incoming data, using params description // If any problems are found then exceptions are thrown with helpful error messages $params = self::validate_parameters(self::create_users_parameters(), array('users'=>$users)); - + $availableauths = get_plugin_list('auth'); unset($availableauths['mnet']); // these would need mnethostid too unset($availableauths['webservice']); // we do not want new webservice users for now @@ -154,7 +154,12 @@ class moodle_user_external extends external_api { profile_save_data((object) $user); } - //TODO: preferences + //preferences + if (!empty($user['preferences'])) { + foreach($user['preferences'] as $preference) { + set_user_preference($preference['type'], $preference['value'],$user['id']); + } + } $userids[] = array('id'=>$user['id'], 'username'=>$user['username']); } @@ -249,7 +254,7 @@ class moodle_user_external extends external_api { 'theme' => new external_value(PARAM_SAFEDIR, 'Theme name such as "standard", must exist on server', VALUE_OPTIONAL), 'timezone' => new external_value(PARAM_ALPHANUMEXT, 'Timezone code such as Australia/Perth, or 99 for default', VALUE_OPTIONAL), 'mailformat' => new external_value(PARAM_INTEGER, 'Mail format code is 0 for plain text, 1 for HTML etc', VALUE_OPTIONAL), - 'description' => new external_value(PARAM_TEXT, 'User profile description, as HTML', VALUE_OPTIONAL), + 'description' => new external_value(PARAM_TEXT, 'User profile description, no HTML', VALUE_OPTIONAL), 'city' => new external_value(PARAM_NOTAGS, 'Home city of the user', VALUE_OPTIONAL), 'country' => new external_value(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL), 'customfields' => new external_multiple_structure( @@ -258,7 +263,14 @@ class moodle_user_external extends external_api { 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the custom field'), 'value' => new external_value(PARAM_RAW, 'The value of the custom field') ) - ), 'User custom fields', VALUE_OPTIONAL) + ), 'User custom fields', VALUE_OPTIONAL), + 'preferences' => new external_multiple_structure( + new external_single_structure( + array( + 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the preference'), + 'value' => new external_value(PARAM_RAW, 'The value of the preference') + ) + ), 'User preferences', VALUE_OPTIONAL), ) ) ) @@ -294,6 +306,13 @@ class moodle_user_external extends external_api { } profile_save_data((object) $user); } + + //preferences + if (!empty($user['preferences'])) { + foreach($user['preferences'] as $preference) { + set_user_preference($preference['type'], $preference['value'],$user['id']); + } + } } diff --git a/webservice/lib.php b/webservice/lib.php index f7e1f3f7182..988fa8835ed 100644 --- a/webservice/lib.php +++ b/webservice/lib.php @@ -484,7 +484,7 @@ abstract class webservice_zend_server extends webservice_server { // tell server what functions are available $this->zend_server->setClass($this->service_class); - + //log the web service request add_to_log(1, 'webservice', '', '' , $this->zend_class." ".getremoteaddr() , 0, $this->userid); @@ -663,7 +663,7 @@ class '.$classname.' { $type = 'string'; } } else if ($keydesc instanceof external_single_structure) { - $type = 'object|struct'; //only 'object' is supported by SOAP, 'struct' by XML-RPC MDL-23083 + $type = 'struct'; } else if ($keydesc instanceof external_multiple_structure) { $type = 'array'; }