webservice MDL-21527 update_users ws function supports customfields now (MDL-20616 patch needs to be committed in order to use the test client)

This commit is contained in:
jerome mouneyrac 2010-02-15 08:56:51 +00:00
parent c5f1fdb451
commit 9baf3a7b65
3 changed files with 29 additions and 1 deletions

View File

@ -118,6 +118,10 @@ class moodle_user_update_users_form extends moodleform {
$mform->addElement('text', 'lastname', 'lastname');
$mform->addElement('text', 'email', 'email');
$mform->addElement('text', 'customfieldtype', 'customfieldtype');
$mform->addElement('text', 'customfieldvalue', 'customfieldvalue');
$mform->addElement('hidden', 'function');
$mform->setType('function', PARAM_SAFEDIR);
@ -135,6 +139,12 @@ class moodle_user_update_users_form extends moodleform {
if (!$data = $this->get_data()) {
return null;
}
//set customfields
if (!empty($data->customfieldtype)) {
$data->customfields = array(array('type' => $data->customfieldtype, 'value' => $data->customfieldvalue));
}
// remove unused from form data
unset($data->submitbutton);
unset($data->protocol);
@ -143,6 +153,8 @@ class moodle_user_update_users_form extends moodleform {
unset($data->wspassword);
unset($data->token);
unset($data->authmethod);
unset($data->customfieldtype);
unset($data->customfieldvalue);
foreach($data as $key => $value) {
if (empty($value)) {

View File

@ -262,6 +262,9 @@ class moodle_user_external extends external_api {
public static function update_users($users) {
global $CFG, $DB;
require_once($CFG->dirroot."/user/lib.php");
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);
@ -274,8 +277,21 @@ class moodle_user_external extends external_api {
foreach ($params['users'] as $user) {
user_update_user($user);
//update user custom fields
if(!empty($user['customfields'])) {
foreach($user['customfields'] as $customfield) {
$user["profile_field_".$customfield['type']] = $customfield['value']; //profile_save_data() saves profile file
//it's expecting a user with the correct id,
//and custom field to be named profile_field_"shortname"
}
profile_save_data((object) $user);
}
}
$transaction->allow_commit();
return null;

View File

@ -52,7 +52,7 @@ function user_create_user($user) {
*/
function user_update_user($user) {
global $DB;
$user->timemodified = time();
$user['timemodified'] = time();
$DB->update_record('user', $user);
}