how do I let only certain users to view a block

how do I let only certain users to view a block

- Raphael Goldman の投稿
返信数: 3

Hi,

I have created a block that is linking to a page that I created.

I want this block in the main site page and I need only some roles to be able to see that block.

I added the capability view to the block:

            'editingteacher' => CAP_ALLOW,

            'manager' => CAP_ALLOW

But actually everyone can see that block, 

How can I do it right?

Thanks

Raphael Goldman への返信

Re: how do I let only certain users to view a block

- Davo Smith の投稿
画像 Core developers 画像 Particularly helpful Moodlers 画像 Peer reviewers 画像 Plugin developers

Do you mean that everyone can see the block, once it has been added, or that everyone has the ability to add the block?

The block/BLOCKNAME:addinstance capability controls who is allowed to add the block to a Moodle page.

There is no standard Moodle capability that will automatically control who will see a particular block once it has been created - you will have to write some code in your block to do this yourself.

Thankfully, it is really easy to do, just add the following to your block's 'get_content()' function:

if (!has_capability('block/BLOCKNAME:view', $this->context)) {
  return null;
}

(Note: this assumes that you replace 'BLOCKNAME' with the name of your block and that you have defined the block/BLOCKNAME:view capability in access.php and, if necessary, you have bumped up the version number to install this new capability).