HEADS UP! Context API in Moodle 2.2+

HEADS UP! Context API in Moodle 2.2+

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 윙크).
평균 등급 :Useful (4)
In reply to Dan Poltawski

Re: HEADS UP! Context API in Moodle 2.2+

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);
평균 등급 :Useful (8)
In reply to Dan Poltawski

Re: HEADS UP! Context API in Moodle 2.2+

Gareth J Barnard
Core developers 사진 Particularly helpful Moodlers 사진 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 미소

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

Cheers,

Gareth

평균 등급 :Useful (1)
In reply to Gareth J Barnard

Re: HEADS UP! Context API in Moodle 2.2+

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. 미소 Although the general developer forum does have a lot of posts and it can be hard to keep up.

평균 등급 :Useful (1)
In reply to Dan Poltawski

Re: HEADS UP! Context API in Moodle 2.2+

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+

Tim Hunt
Core developers 사진 Documentation writers 사진 Particularly helpful Moodlers 사진 Peer reviewers 사진 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.

평균 등급 :Useful (3)
In reply to Matt Gibson

Re: HEADS UP! Context API in Moodle 2.2+

Ankit Agarwal

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