Adding a link only for a specific role

Adding a link only for a specific role

by Ely Pa -
Number of replies: 1

Hi,

  I've written a set of pages for a new block. In one page I would like to display a link only for a specific role (let's say the teacher) and not to other roles (let's say the students).

What do you think is the best way to do it? Using get_user_roles() and checking if the user has the teacher (or higher) role, or defining a new capability (for my block? for the specific page?) and then verifying it for the user? Which are the main advantages/disadvantages of these strategies?

            Thanks in Advance,

              Ely

Average of ratings: -
In reply to Ely Pa

Re: Adding a link only for a specific role

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
The best way is to define a new capability. What is the purpose of this link? What thing does it let people do. suppose the new capability is called block/myblock:dothing.

Then you can assign that capabilities to teachers, but not students, and you can display the link if has_capability('block/myblock:dothing', get_context_instance(CONTEXT_BLOCK, $this->$this->instance->id)) returns true.

You can set which roles get the capability by default when you define the capability in access.php.