customfields not stored when created via SOAP web services

customfields not stored when created via SOAP web services

by Anders Landberg -
Number of replies: 7
I have created a simple PHP script that utilises the Zend Soap Client to interface with Moodle's web services.

I first created a user as follows, specifying preferences and custom fields. The SOAP call is successful.

<code>

<?php

require_once('Zend/Soap/Client.php');

try {

$client = new Zend_Soap_Client("http://<URL>/webservice/soap/server.php?wstoken=241b0237f5fe1fdd7634cdd41750f591&wsdl=1");

$function = 'moodle_user_create_users';

$params = array(

array(

// user 1
array(

'username' => 'test_user_5' // string //Username policy is defined in Moodle security config
, 'password' => 'test_user_5' // string //Plain text password consisting of any characters
, 'firstname' => 'Jim5' // string //The first name(s) of the user
, 'lastname' => 'Jimson5' // string //The family name of the user
, 'email' => 'jim5@jimson5.com' // string //A valid and unique email address
, 'auth' => 'manual' // string Default to "manual" //Auth plugins include manual, ldap, imap, etc
, 'idnumber' => '' // string Default to "" //An arbitrary ID code number perhaps from the institution
, 'emailstop' => 0 // double Default to "0" //Email is blocked: 1 is blocked and 0 otherwise
, 'lang' => 'en' // string Default to "en" //Language code such as "en", must exist on server)

, 'preferences' => array(

// pair 1
array(

'type' => 'pref_icecream'
, 'value' => 'vanilla'
)
)

, 'customfields' => array(

// pair 1
array(
'type' => 'custom1'
, 'value' => 'hello'
)
)

)
)
);

$result = $client->__call($function, $params);

print_r($result);

} catch(Exception $e) {

print_r($e);

}
?>

</code>

However, when I retrieve the user information (see code snippet below) the customfields array is empty.

<code>

<?php

require_once('Zend/Soap/Client.php');

try {

$client = new Zend_Soap_Client("http://<URL>/webservice/soap/server.php?wstoken=241b0237f5fe1fdd7634cdd41750f591&wsdl=1");

$function = 'moodle_user_get_users_by_id';

$params = array(
array(1, 2, 3, 4, 5, 6, 7, 8, 9)
);

$result = $client->__call($function, $params);

for($i = 0; $i < count($result); $i++) {

echo "Custom fields: \n";
print_r($result[$i]['customfields']);
echo "Preferences: \n";
print_r($result[$i]['preferences']);
}

} catch(Exception $e) {

print_r($e);

}

?>

</code>

Outputs:

Preferences:
Custom fields:
Array
(
)



Is this a bug - or am I doing sth wrong?
Average of ratings: -
In reply to Anders Landberg

Re: customfields not stored when created via SOAP web services

by Jérôme Mouneyrac -
Hi Anders,
I just tested on my unit tests, moodle_user_create_users creates the customfields and moodle_user_get_users_by_id returns customfields.
In reply to Jérôme Mouneyrac

Re: customfields not stored when created via SOAP web services

by Anders Landberg -
Hi Jerome,

Is there a particular format that I need to use for the customfieldtype for it to work (i.e. string, int, ...)? I have been using type/value pairs such as name/John.

I tested these two functions (moodle_user_create_users and moodle_user_get_users_by_id) using the WS test client and it didn't return the customfield.

I checked out the latest version from CVS this morning and ran these tests.

I understand that your unit tests are OK, but can you please run these tests on the test client, too?
In reply to Anders Landberg

Re: customfields not stored when created via SOAP web services

by Jérôme Mouneyrac -
It works with the test client too. Have you created the custom field type before? moodle_user_create_users doesn't create custom field type.

Note: we don't have any admin custom fields web service function.
In reply to Jérôme Mouneyrac

Re: customfields not stored when created via SOAP web services

by Anders Landberg -
I found the "User profile fields" page where I can create Checkbox, Menu of choices, Text area, Text input or "Create a new profile category".

I guess that this is where I have to define the custom fields?

Is there documentation on how these profile fields tie in with the customfields and preferences of the WS?

What do you mean with "admin custom fields"? Does this mean that the user can view/edit the custom fields?
In reply to Anders Landberg

Re: customfields not stored when created via SOAP web services

by Jérôme Mouneyrac -
yes it is the place smile

For the web service documentation, go on the admin protocol page and enable the documentation, then you can access it through /webservice/wsdoc.php. If something is not clear in it, don't hesitate to suggest any better documentation text. I'll add some information about User custom fields = User profil fields.

By admin custom fields, I meant that only administrator can manage the custom fields (User profile fields).

Thank you.
In reply to Jérôme Mouneyrac

Re: customfields not stored when created via SOAP web services

by Chris Lohman -

Hi,

Either Jerome or Anders - could you explain how the information for custom fields needs to be past to the soap function?  Also, I have Moodle 2.0 and I can't for the life of me find out how to turn on the web service documentation.  There is nothing in the tokens section.  Could you possibly write a quick step by step on how to view this?  I have SOAP webservices all set up already and they work beautifully.  Having access to the documentation would be so helpful!

Best,

Chris

In reply to Chris Lohman

Re: customfields not stored when created via SOAP web services

by Jérôme Mouneyrac -

Hi Chris,

Go to webservice/simpletest/testwebservice.php:moodle_user_create_users()
You should find some examples how to pass the custom fields.

The security key doc should answer your second question about the web service documentation.