This document discusses API driven development in Moodle and how to build API endpoints. It covers registering API endpoints in Moodle's external service description, creating external functions to define the API, and making API calls. External functions define the parameters, return values, and business logic for each API endpoint. The document provides an example of an external service description and external functions for a get_quiz_questions API. It emphasizes the need for security and monitoring when building APIs.
7. By Pass: Without Changing Moodle core
Rapid
Grader
Front-end
Moodle
DB
Moodle
API
8. Wait, What’s an API?
Google
Maps
in Uber
Request : Start and End Location and Time
Response: Distance, Traffic, etc
9. When API plugin development is needed?
● Connecting to an external software
● When overriding existing feature does not give you the
required control.
10. Amazing Moodle API
Mobile Apps
Web Clients {....}
React
Angular
GET / POST
JSON / XML
API Gateway
API Clients
Food items
in order
Order placed
message
13. API Development in Moodle
External service description
Think it of like a file where we
need to register our APIs.
External functions
External functions are used to
create API endpoints
API Calls
Call the API endpoint and
process the response received.
03
01 02
Know more about it:
https://docs.moodle.org/dev/External_services_description#Service_discovery
https://docs.moodle.org/dev/External_functions_API#Overview
14. External Service description
Registering your API endpoint in db/services.php file of a Moodle plugin.
<?php
$functions = array(
'block_grader_get_quiz_questions' => array(
'classname' => block_graderexternalapi',
'methodname' => get_quiz_questions
)
);
15. External Functions
Methods which are stored in externallib.php that can be accessed by external programs like an API client.
/**
* Describes the structure of parameters for the function.
*/
public static function get_quiz_questions_parameters() {
return new external_function_parameters(
array (
'attemptid' => new external_value(PARAM_INT, 'Attempt ID.', VALUE_DEFAULT, 0),
)
);
}
/**
* Get attempt questions
**/
public static function get_quiz_questions($attemptid) {
// business logic
return $questions;
}
/**
* Describes the structure of the function return value.
*/
public static function get_quiz_questions_returns() {
new external_single_structure(
array(
'number' => new external_value(PARAM_INT, 'Question Number'),
'status' => new external_value(PARAM_RAW, 'Question Status')
)
)
}