Function Definitions
An External function definition is the class, and collection of functions, used to define:
- any parameters that the function expects to take, including any types
- what the function will return, including any types
- whether the function has been deprecated, or not
It also includes the code that will actually be executed by the function.
The function definition should be located within the external
namespace of a component.
Example
For a component named local_groupmanager
located in local/groupmanager
which is responsible for creating groups on request, you may have:
- a Web service function named:
local_groupmanager_create_groups
- defined in a class named
local_groupmanager\external\create_groups
- which is located
local/groupmanager/classes/external/create_groups.php
A service definition:
- must
require_once
thelib/externallib.php
file - must extend the
external_api
class - must declare an
execute_parameters
function to describe the expected parameters of the function - must declare an
execute
function which is called with the functions and performs the expected actions - must declare an
execute_returns
function to describe the values returned by the function - may declare an
execute_is_deprecated
function to declare a function as deprecated
An example definition
local/groupmanager/classes/external/create_groups.php
<?php
namespace local_groupmanager\external;
defined('MOODLE_INTERNAL') || die;
require_once("{$CFG->libdir}/externallib.php");
class create_groups extends external_api {
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters([
'groups' => new external_multiple_structure(
new external_single_structure([
'courseid' => new external_value(PARAM_INT, 'The course to create the group for'),
'idnumber' => new external_value(
PARAM_RAW,
'An arbitrary ID code number perhaps from the institution',
VALUE_DEFAULT,
null
),
'name' => new external_value(
PARAM_RAW,
'The name of the group'
),
'description' => new external_value(
PARAM_TEXT,
'A description',
VALUE_OPTIONAL
),
]),
'A list of groups to create'
),
]);
}
public static function execute(array $groups): array {
// Validate all of the parameters.
[
'groups' => $groups,
] = self::validate_parameters(self::execute_parameters(), [
'groups' => $groups,
]);
// Perform security checks, for example:
$coursecontext = \context_course::instance($courseid);
self::validate_context($coursecontext);
require_capability('moodle/course:creategroups', $coursecontext);
// Create the group using existing Moodle APIs.
$createdgroups = \local_groupmanager\util::create_groups($groups);
// Return a value as described in the returns function.
return [
'groups' => $createdgroups,
];
}
public static function execute_returns(): external_single_structure {
return new external_single_structure(
'groups' => new external_multiple_structure([
'id' => new external_value(PARAM_INT, 'Id of the created user'),
'name' => new external_value(PARAM_RAW, 'The name of the group'),
])
);
}
}
note
Available parameter types are defined in lib/moodlelib.php
and are used by the validate_parameters()
function and during return value checking to ensure that the service is called and working as defined.