HEADS UP! Context API in Moodle 2.2+

HEADS UP! Context API in Moodle 2.2+

by Dan Poltawski -
Number of replies: 9
Hi,

In Moodle 2.2 a change was made to the context API with a new object-orientated mechanism for retrieving contexts. It is described in Development:Access_API and it shouldn't be too much of a shock to the system vs the old API functions (which are still available).

Its probably fair to say we didn't communicate this new API amazingly well, hence my post here to draw more developers' attention to it. Starting from Moodle 2.4, most uses of the old API will have been converted to the new style in Moodle core.

If you have any questions about this new style, please post them here (though i'm just being a messenger, so may not be able to answer you internals questions wink).
Average of ratings: Useful (4)
In reply to Dan Poltawski

Re: HEADS UP! Context API in Moodle 2.2+

by Dan Poltawski -
Maybe a quick code example would be useful..

If you had code that did:

$systemctx = get_context_instance(CONTEXT_SYSTEM);
$coursectx = get_context_instance(CONTEXT_COURSE, $courseid));
$modctx = get_context_instance(CONTEXT_MODULE, $modid);
$context = get_context_instance_by_id($contextid);

It will now look like:

$systemctx = context_system::instance();
$coursectx = context_course::instance($courseid);
$modctx = context_module::instance($modid);
$context = context::instance_by_id($contextid);
Average of ratings: Useful (8)
In reply to Dan Poltawski

Re: HEADS UP! Context API in Moodle 2.2+

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Dear Dan,

Thanks for the head's up, I'd spotted the change in Moodle 2.4dev but had not realised for 2.2 and 2.3.  A really useful post smile

Is there such thing as a 'Developer Bulletin' for such things or if not should there be?

Cheers,

Gareth

Average of ratings: Useful (1)
In reply to Gareth J Barnard

Re: HEADS UP! Context API in Moodle 2.2+

by Dan Poltawski -

Is there such thing as a 'Developer Bulletin' for such things or if not should there be?

Interesting idea, do you think it would be helpful?

At the moment, the closest thing for that is posts on these forums, upgrade.txt files and the release notes. There is also the 'integration exposed' blog for a summary of things going on in Moodle core.

I don't know if I would say that the 'channels' of communication are a problem, more us not doing a good enough job of the communication itself. smile Although the general developer forum does have a lot of posts and it can be hard to keep up.

Average of ratings: Useful (1)
In reply to Dan Poltawski

Re: HEADS UP! Context API in Moodle 2.2+

by Matt Gibson -

Cool. I'd noticed and have started coding to the new API, but just out of interest in the architectural approach, what was the rationale behind the change? The methods are static and seem to use the classes as a namespace. Anything beyond this? It does feel cleaner and I like it, but I'm not sure why. 

In reply to Matt Gibson

Re: HEADS UP! Context API in Moodle 2.2+

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

These are only the methods to get the context, and they are using the static factory pattern.

The real point is what you get back from these methods, namely, an object of a real class context, rather than just a stdClass. There are various advantages to that.

Old funcitons like print_context_name are now methods $context->get_context_name(), which your IDE will auto-complete for you.

APIs that expect to recieve a context as input can declare themselves like some_function(context $context) which helps catch errors and document the API.

And the code in accesslib itself is now a lot neater, and hence easier to maintain.

Average of ratings: Useful (3)
In reply to Matt Gibson

Re: HEADS UP! Context API in Moodle 2.2+

by Ankit Agarwal -

Also the new class seems to do a better job of caching the contexts..I guess