如何使区块仅对管理者和教师可见? [英] How to make a block visible only for admin and teacher in moodle?
问题描述
我认为有多种方法可以使学生看不见积木.
I think there are more than one way to make block invisible for students.
1.
Hide the block
2.
Assign role to block and set permission to block
但是这些是由管理员通过更改设置来完成的.我需要一种通过代码的方式.我该如何编写代码以使该块对学生不可见.
But these are done by admin by change the settings. I need a way by code. How can I write the code to make the block invisible for student.
对于activity I can make invisible the activity
,通过更改 db/access.php
'mod/questionbank:view' => array(
'captype' => 'read',
'contextlevel' => CONTEXT_MODULE,
'legacy' => array(
//'guest' => CAP_ALLOW,
//'student' => CAP_ALLOW,
'teacher' => CAP_ALLOW,
'editingteacher' => CAP_ALLOW,
'admin' => CAP_ALLOW
)
),
像这样如何通过代码使学生看不见该区块.
编辑
根据 Davosmith的答案.
我放入了get_content
函数
if (!has_capability('blocks/blockname:view')) {
return null;
}
在 blocks/blockname/block_blockname.php
,并且在我的 blocks/blockname/db/access.php 中包含:
'blocks/blockname:view' => array(
'captype' => 'read',
'contextlevel' => CONTEXT_BLOCK,
'legacy' => array(
//'guest' => CAP_ALLOW,
//'student' => CAP_ALLOW,
'teacher' => CAP_ALLOW,
// 'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW
)
),
但这会导致错误页面说
检测到编码错误,必须由程序员修复:PHP可捕获 致命错误
Coding error detected, it must be fixed by a programmer: PHP catchable fatal error
推荐答案
对于任何块,如果get_contents返回null(并且已关闭编辑),则不会显示该块.
For any block, if get_contents returns null (and editing is off), the block will not be displayed.
因此,将以下内容放入您块的get_content函数中(但要放入您在db/access.php中定义的实际功能):
So, put the following in the get_content function of your block (but put in a real capability that you define in db/access.php):
if (!has_capability('block/myblock:somecapability', $this->context)) {
return null;
}
这篇关于如何使区块仅对管理者和教师可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!