Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Moodle $context

From Moodle doc:

A context is a space in Moodle where roles can be assigned.

I understand that a context is a logical space used to manage Moodle objects.

I developed a custom block plugin with a file upload where I use file_prepare_draft_area andfile_save_draft_area_files functions.There is a $context parameter that must be passed and I am don't really know what context should I pass ? This mean, I guess, in which logical space should I put my block plugin uploaded files ? In my opinion, the most logical would be store the uploaded files in a context related to my block plugin.

I tried to use context_block::instance($instanceid) but I don't know how to get $instanceid param.

  • Which context should I use in this case?
  • How to get it?
like image 874
ben.IT Avatar asked Mar 28 '17 08:03

ben.IT


1 Answers

The types of context are as follows:

  • System
  • Course category
  • Course
  • Activity module
  • Block
  • User

The hierarchy of contexts are: System => Course category => Course => Activity module

Block contexts can appear within courses or within the 'site' course. User contexts are outside of courses.

If you want the files tied to a specific instance of the block (e.g. so they are deleted automatically when the block is deleted and you can keep the files from different instances of the block separate), then you should use the block context (but you'll have to pass the instanceid of the block to the sub-pages in order to use this to get the context:

$context = context_block::instance($blockinstanceid);

If you want the files tied to the course - so all instances of the block in the course share the same file space and the files are only deleted when the whole course is deleted, then use the course context (pass the courseid into the subpages, as a param, then use:

$context = context_course::instance($courseid);

If, however, you want to share that file area across all blocks on the site, then the system context is what you want:

$context = context_system();

like image 125
davosmith Avatar answered Sep 28 '22 00:09

davosmith