create a new webservice function

Re: create a new webservice function

by Jérôme Mouneyrac -
Number of replies: 0

Very nice work on your doc smile

You are adding a web service to the core code not to your plugin code. I'll explain how to quickly change your document to follow the recommended way (not touching core code smile):
* name your externallib class local_PLUGINFOLDER_external
* the validate_parameters() function throws an exception if a param is missing (except if told, all params are VALUE_REQUIRED). You don't need this:

//retrieve courses
       if (!key_exists('courseid', $params)) { //Will probably need to chuck a narny.
           throw new moodle_exception(get_string('nocourseid', 'webservice', $exceptionparam));
       }

* Avoid to insert data directly into the database, create /PLUGINFOLDER/db/service.php:


$functions = array(
'PLUGINFOLDER_get_course_activities' => 
		array('classname'   => 'local_PLUGINFOLDER_external', 
			'methodname'  => 'get_course_activities',  
			'classpath'   => 'local/PLUGINFOLDER/externallib.php',  '
			description' => 'Get course activities',                
			'type'        => 'read',  
			'capabilities'=> 'moodle/course:view')); //optional, useful to let the administrator know what potential capabilities the user 'could' need

Note: for your core example, we would have edited /lib/db/service.php

* the Flash web service client does load automatically every core functions. I'm not sure about plugin though. But it's a bit of an hidden gem in Moodle (Administration > Development > Web service test client > Amf test client link) ;)
* for unit test, your probably better having a /PLUGINFOLDER/simpletest/testexternallib.php. (Administration > Development > Unit tests)
* don't forget to check capabilities into your externallib.php. Even though many core functions do check capabilities, it is recommended when writting web service to check capabilities into the external function and not depending of a core function doing it (if you call one in your external function).

 

Good luck, and thanks for your documentation smile