This is a test site. Any changes will be lost!

Development:Moodle API

From MoodleDocs

Note: This article is a work in progress. Please use the page comments for any recommendations/suggestions for improvement.

Moodle 2.0


Introduction

This document is about the Moodle API introduced in Moodle 2.0, which is intended to standardise and streamline methods for calling common control functions in Moodle core and modules. This is most useful for web services (where external systems want to control Moodle) but also for alternative interfaces such as Flex and so on.

To do this, Moodle 2.0 relocates important functions into new api.php files within each module, with standard documentation and structure to help reduce code redundancy and maintenance, while improving readability and functionality.

Over time we expect more and more Moodle functionality to move into these files.

Related documents

Implementation

Convention

A new api.php file can be created for each module/block folder, and also the current admin/user/course/... common moodle folders. These files must contain a class named after the unique module path:

   class mod_forum_api
   class user_api
   class course_api
   ...

In this class, static methods will provide information and operations to external callers, eg:

   create_forum()
   get_user()
   get_content()
   get_discussions()
   ...

Standard function names

So that the whole API is consistent, we recommend to use methods with names like this: get_xxx() //for retrieving one or more objects named xxx set_xxx() //for updating OR creating one or more object xxx (ie when you don't care if it exists yet) update_xxx() //for updating one or more xxx objects, throws an exception if the object doesn't exist create_xxx() //for creating an object, throws an exception if the object (primary key) already exist delete_xxx() //for deleting an object, TO BE DEFINED: throws an exception if the object doesn't exist

API functions must be defined/declared

The web services module needs to know what parameters are required and what information are returned. We need a way to define these parameters so that web service descriptions can be automatically generated and so that incoming data can be cleaned/checked.


TODO: Look closely at PHPdoc to see if we can represent all required information as part of the standard function documentation. We can scan all these whenever files change, and then store the result as XML or something in dataroot/cache. The web services module will look at this file in order to create a WSDL file for soap, in order to know what function are available in REST, to check the range and types of incoming parameters, etc.

Call the API functions

The API functions will not only be used by web services, but by any code that needs to talk to those modules. As they are static methods, they should be called as:

   mod_forum_api::get_discussions
   user_api::create_users
   course_api::get_course
   ...

Functions List

The names of each of the sections below represent a pathname in Moodle, where the api.php for that code will be located.
In order to keep this Moodle API page easy to read, we wrote the Moodle API functions on another page. Please review them as well.