create a new webservice function

create a new webservice function

by markus schneider -
Number of replies: 2

hello i want to create a new webservice soap funtion in course/externallib.php. Everytime i call my webservise(with the parameter

array
0 =>
array
'username' => 'test' (length=4)
'password' => 'test' (length=4)

) it returns null


public static function user_get_all_accessable_courseids_parameters() {
return new external_function_parameters(
array( 'user'=> new external_multiple_structure(
new external_single_structure(

array(
'username' => new external_value(PARAM_RAW, 'username'),
'password' => new external_value(PARAM_RAW, 'password'),

)
)
)
)
);

}
public static function user_get_all_accessable_courseids_returns() {
array(new external_multiple_structure(
new external_single_structure(
array(
'username' => new external_value(PARAM_RAW, 'username'),
'password' => new external_value(PARAM_RAW, 'password'),
)
)
)


);

}

public static function user_get_all_accessable_courseids($user) {

global $CFG, $DB;
require_once($CFG->dirroot . "/course/lib.php");
require_once($CFG->libdir . '/completionlib.php');


$params = self::validate_parameters(self::user_get_all_accessable_courseids_parameters(),
array('user' => $user));



return $params;

}


plz help fast smile

Average of ratings: -
In reply to markus schneider

Re: create a new webservice function

by Gerwood Stewart -

Markus

Having read the code you posted it looks like you are currently returning the params you passed in after you've validated them. Is that correct? And if so have you setup the _returns function to match that data structure?

I'm just getting started with 2.0 web services myself so I wrote this tutorial yesterday to help sort out what was going on. It might give you some other hints and should allow you to make a full simple web-service that you could modify from there.

Average of ratings: Useful (1)
In reply to Gerwood Stewart

Re: create a new webservice function

by Jérôme Mouneyrac -

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