Understanding Moodle $context

时光怂恿深爱的人放手 提交于 2019-12-06 04:57:03

问题


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?

回答1:


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();



来源:https://stackoverflow.com/questions/43064153/understanding-moodle-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!