Function parameters

Function parameters

by Richard Hayes -
Number of replies: 8

Hi,

I'm totally new to Moodle but need to integrate it with a new website, I've been given a list of functions that we need to use, but I have no idea what the parameters for the functions are nor can I find any documentation on the functions.

Does anyone know where/how I can find these out?

Average of ratings: -
In reply to Richard Hayes

Re: Function parameters

by RICHARD gillette -

Which of the webservice functions (REST, SOAP, XML-RPC...ect) are you going to use? I use the REST api and if you send me the functions you need to use I can share with you what I have written so far.

For looking at what the parameters will be for the functions, for course related functions go to moodle/course/externallib.php in your moodle directory. same for user/externallib.php and enrol/externallib.php. In those files are the functions params, definitions, and returns. If you are going to write your own functions, moodle/lib/db/services.php is another place to create references for adding the functions to moodle.

 

Richard Gillette
www.omnibond.com

In reply to RICHARD gillette

Re: Function parameters

by Richard Hayes -

Hi Richard,

Thanks for your response. The functions I need to use are:

  • moodle_enrol_manual_enrol_users 
  • moodle_user_create_users 
  • moodle_role_assign
I'm using XML-RPC which I happy to use so all I'm after is the parameters for each function and I should be good to go.
If you do have these then great, if not I'll get the company to send their moodle dir over so I can trawl through that.
Many thanks
In reply to Richard Hayes

Re: Function parameters

by RICHARD gillette -

I do not know how xml-rpc works but it should be very similar. I use an LWP agent to post the following for create users in perl with the REST api.

my $params = {

                              'wstoken' => $token,
                              'wsfunction' => 'moodle_user_create_users',
                             'users[0][username]' => $self->username,
                             'users[0][firstname]' => $self->firstname,
                             'users[0][lastname]' => $self->lastname,
                             'users[0][email]' => $self->email,                                                                      so on and so forth

                       }

the $url looks something like 'http://moodleserver/webservice/rest/server.php'

yours will most likely be 'http://moodleserver/webservice/XML-RPC/server.php'

the LWP post then goes - $response = $userAgent->post($url, $params);

and your can print $response->content to see what you get back or just do a check for $response->is_success or !$response->is_success.

for enrol users my params look like

$params = {
            'wstoken' => $token,
            'wsfunction' => 'moodle_enrol_manual_enrol_users',
            'enrolments[0][roleid]' => $self->userid,
            'enrolments[0][courseid]' => $self->courseid,
            'enrolments[0][roleid]' => $self->roleid,

                }

where roleid is the id of the role such as Student = 5, Teacher = 8 or whatever.

You can also go into the functions and change the parameters that they accept. I have made delete courses by shortname and id, get users by id and username, enrol users by coursename and username, ect and am in the process of posting them to the dev forums in several tickets.

I have not used role_assign but looking at the params it takes in enrol/manual/externallib.php I am going to guess it will be

'assignments[0][roleid]' => "38",  #all of these should be ints unless you
'assignments[0][userid]' => "15",    # change the function      

'assignments[0][contextid]' => "15",

Let me know if I can help any other way, I hope these work for XML-RPC too as well as they do for REST

 

Richard Gillette

www.omnibond.com

In reply to RICHARD gillette

Re: Function parameters

by Jérôme Mouneyrac -

Hi,

to write a web service client there is a Wiki documentation that can help you: http://docs.moodle.org/dev/Creating_a_web_service_client 

To know about web service API documentation, there is a way if you follow the description of this issue: MDL-28650

This issue already has a full working patch, there is just a little issue with the crumbtrail. So if you cherry-pick the patch with git, you can go on Admin block > Plugins > Web services > Manage Service and a "View full API" link should be available (functions parameters, return values...).

In reply to Jérôme Mouneyrac

Re: Function parameters

by Josh Jones -

Hi guys, 

Just wondering what the easiest way is to to view all the parameters a function can take. I am building a rest client. I have no link to manage service in my Moodle. 

Regards, 

Josh

In reply to Josh Jones

Re: Function parameters

by Jérôme Mouneyrac -

Hi Josh,

if you don't have access to the Moodle admin, you can try to install your own Moodle site and as admin check Administration > Plugins > Web services > API documentation. You'll see at least the core functions description.

Otherwise the Moodle admin enables the mobile documentation (Administration > Plugins > Web services > Manage service). Then connect into the Moodle site as a ws user (meaning that the admin gave you the capability to create a token 'moodle/webservice/createtoken' on system context or the admin manually created a token for you.). Then you'll find the documentation in your security keys page (My profiles > Security Keys if I remember, look in your menu if it's not that). 

That's the only two solutions to know the API if you don't have admin access into the Moodle site.

In reply to Jérôme Mouneyrac

Re: Function parameters

by Josh Jones -
Hi Jerome, 
I managed to find the API, but I can only perform tasks that get information from a database none that can edit information, such as create user, below are my parameters, I am using a zend HTTP client to make the transaction. 
'wsfunction' => 'moodle_user_create_users',
'wstoken' => '360163d2f8f376ae7b7f8bb7bd2cc4a0',
'username' => 'testusername1',
'password' => 'Testpassword1#',
'firstname' => 'billy',
'lastname' => 'bobby',
'email' => 'testemail1@moodle.com',
'auth' => 'manual',
'idnumber' => '',
'lang' => 'en',
'theme' => 'standard',
'timezone' => '-12.5',
'description' => 'Hello World!',
'city' => 'testcity1',
'country' => 'au',
Thank you for your time appreciate it. 
In reply to RICHARD gillette

Re: Function parameters

by Josh Jones -

Hi Richard, 

Could you please let me know what the $self variable is that you declare? and what the structure  'users[0][username]' => $self->username, means? 

Thank you