3 general questions about developing webservices

3 general questions about developing webservices

by Sebastian Boosz -
Number of replies: 4
Hi,

after the last question I posted on these forums, I was able to achieve some progress. I use the SOAP protocol for alle webservice functions now and have started creating my own webservice functions (currently 7 of them are working), however I still have some questions:

1. Is there a way to decide which capabilities the web service user should have instead of just omitting them or inserting some capabilities which sound to be correct (e.g. managegroups for group related functions)?

2. What is the best procedure to find already defined core functions? A concrete example: I managed to create a web service function which creates a label (using mod/label/lib.php). Now I want to create another function which takes a sectionid and a labelid as input, and inputs this label into the section. I guess there must be a predefined function somewhere, however I cannot find it.

3. This question is perhaps "caching" related. I have created some label with my web service function which are displayed correctly in the database table 'label' (for example this label has the id 2). In moodle itself I create a label in a section of some course. This label has the id 3. I have deduced that the 'connection' between the section and the label happens in the table 'course_modules'. There will be a row with module = 10 (for label) and instanceid 3 (the id of the created label). If I alter this row now by replacing instanceid 3 by 2 the displayed label in the moodle course will not change. Even If I delete the label with id 3 in the 'label' table, it will still be there. Do you have any suggestions why this could happen? (I have tried deleting the moodle cache as well as RESET/FLUSH in phpmyadmin)

Thank you for your patience, answers to any of the questions are greatly appreciated.

Sebastian
Average of ratings: -
In reply to Sebastian Boosz

Re: 3 general questions about developing webservices

by Jérôme Mouneyrac -

Hi Sebastian,

I'm just replying on how I write web service functions (1. and 2.).

1. First I do a quick search in Moodle for the similar calls of the core function I'm using. I often end up to find the right capabilities for the similar use of the core function.

If my web service function doesn't call any core function, I read Moodle code to find a similar functionality (generally I start from navigating in the Moodle UI and end up to the code doing the same thing as my web service function). Then I see if there is any capability checking around this code.

I can also check if an existing capability name seems matching my web service function. In this case I search where this capability is used in Moodle code. If it used used for a similar functionality, then I use it.

Finally if I could not find any capability to use, I create my own capabilities for my plugin.

Note: take care to check the capabilities on the right context too.

2. If you looked into respective lib.php files and into the Moodle main /lib folder, and you didn't find what you were excepting, it could missing from Moodle core. In this case you can write a tracker issue for it.

Average of ratings: Useful (2)
In reply to Jérôme Mouneyrac

Re: 3 general questions about developing webservices

by Patrick Pollet -

Hi,

As far as question 2 is concerned, the API function is called add_mod_to_section and is defined in course/lib.php. It seems to be the same in Moodle 1.9 and 2.0.

It requires a parameter $mod with most fields needed in the various tables already filled in. You may find examples of use in

course/modedit.php:300:            if (! $sectionid = add_mod_to_section($fromform) ) {
course/mod.php:203:                if (! $sectionid = add_mod_to_section($mod) ) {

and also in OK Tech Web Services where operations named affect_xxxx_to_section are already implemented where xxxx=label, forum, database,  assignment or wiki... see the script server.class.php and utility function ws_add_mod_to_section($modid, $modtype, $section, $groupmode = 0, $visible = 1) defined in wslib.php that take care on all the "commun" operations.

 

Cheers.

Average of ratings: Useful (2)
In reply to Patrick Pollet

Re: 3 general questions about developing webservices

by Sebastian Boosz -
Thank you both for your hints.

Last week I did not have time to go on working on the webservice functions, but I continued yesterday and today. I have created functions for adding and removing labels to a section, which work just fine.

Once you get accustomed to the way of defining these web service functions it's quite easy and intuitive. Nevertheless the tutorials should be a little more detailed. ;)

In reply to Patrick Pollet

Re: 3 general questions about developing webservices

by Sebastian Boosz -
Hey,

I am sorry for the double post, but I have found a solution for my third question. After inserting,editing or removing the labels I call the function

rebuild_course_cache($courseid=0, $clearonly=false)

defined in course/lib.php with the corresponding courseId. After doing that, the changes triggered by the web service functions are immediately visible.