OKTech WS Editing user (password)

OKTech WS Editing user (password)

by dave homan -
Number of replies: 4
Hi,

EDIT: Moodle 1.9.9+ installed in a WAMP environment.

I am wondering if anyone can help me. I am trying to edit a user and change their password using the OKTech Web Services.

I can successfully create a user, enroll them in a course, get user details etc, but I am having trouble changing the password.

I am using the follow code below (C#), and I played with the action attribute of the user and while Update doesnt return an error - it does not change the password.

Any help at all would be greatly appreciated.

Thanks,

Dave

public string changeUserPassword(string userName, string password) {
string retVal;
Moodle.loginReturn login = moodleWS.login(strUserName, strPassword);

Moodle.getUsersReturn res = moodleWS.get_user_byidnumber(login.client, login.sessionkey, userName);

if (res.users.Length != 1)
{
retVal = "Error retrieving user " + userName;
}
else if (res.users[0].error != String.Empty)
{
retVal = res.users[0].error;
}
else {
//
Moodle.userDatum user = new Intel.PCB.Certification.Moodle.userDatum();
user.action = "";
user.confirmed = "1";
user.policyagreed = "0";
user.deleted = "0";
user.idnumber = user.username = res.users[0].username;
user.auth = "manual";
user.password = password;
user.firstname = res.users[0].firstname;
user.lastname = res.users[0].lastname;
user.email = res.users[0].email;
user.city = res.users[0].city;
user.country = res.users[0].country;
user.timezone = "99";
user.action = "Update";

Moodle.editUsersInput eui = new Intel.PCB.Certification.Moodle.editUsersInput();
eui.users = new Intel.PCB.Certification.Moodle.userDatum[] {user};

Moodle.editUsersOutput res2 = moodleWS.edit_users(login.client, login.sessionkey, eui);

if (res2.users.Length != 1)
{
retVal = "Error editing user " + userName;
}
else if (res2.users[0].error != String.Empty)
{
retVal = res2.users[0].error;
}
else
{
retVal = MoodleAPI.SUCCESS;
}
}

moodleWS.logout(login.client, login.sessionkey);

return retVal;
}
Average of ratings: -
In reply to dave homan

Re: OKTech WS Editing user (password)

by dave homan -
Murphys Law!!!

Just after posting and doing somemore playing with the code - I got it to work...

I am using update_user rather than edit_users:


//Moodle.editUsersOutput res2 = moodleWS.edit_users(login.client, login.sessionkey, eui);
Moodle.editUsersOutput res2 = moodleWS.update_user(login.client, login.sessionkey, user, "idnumber");

In reply to dave homan

Re: OKTech WS Editing user (password)

by Patrick Pollet -
Actually update_users is a wrapper call to the more general edit_users operation and is indeed simpler to use. edit_users requires the Moodle internal id of users to update or delete and you forgot to sent it... Update_user accepts any unique identifier for the target user (username, idnumber, id or email).

You could have achieved the same by setting :

Moodle.userDatum user = new Intel.PCB.Certification.Moodle.userDatum();
user.action = "update";
user.id = res.users[0].id; //the missing information !
user.password="new password";

or
user.passwordmd5="md5encodedpassword";
//no need to sent other values that are not to be changed (lastname,firstname...)

Moodle.editUsersInput eui = new Intel.PCB.Certification.Moodle.editUsersInput();
eui.users = new Intel.PCB.Certification.Moodle.userDatum[] {user};

Moodle.editUsersOutput res2 = moodleWS.edit_users(login.client, login.sessionkey, eui);

Cheers.
Average of ratings: Useful (1)
In reply to Patrick Pollet

Re: OKTech WS Editing user (password)

by dave homan -
Thanks a lot Patrick.

I missed the id alright! Thought I was OK with idnumber!

Is the same true for update_user in that I do not need to send any unchanged data (i.e. firstname, lastname)?

Thanks,

Dave
In reply to dave homan

Re: OKTech WS Editing user (password)

by Patrick Pollet -
> s the same true for update_user in that I do not need to send any unchanged data (i.e. firstname, lastname)?

yes I think so, both operations call internally update_record and as long as id is set, it should work.

Cheers.