Get user role in Moodle course

Get user role in Moodle course

by Luis Sola Ruiz -
Number of replies: 6

Hello everyone, I am trying to create a moodle block. So inside I want to show different links in base of the role user in that course. 

If it is a student: link A 

If it is a teacher: link B

I saw this post https://moodle.org/mod/forum/discuss.php?d=128550 but it looks quite deprecated.

I have been trying inside the function get_content() in the main class to get the current role with different methods with no results.

 Anyone could help me to get the role user?

Thanks!

Average of ratings: -
In reply to Luis Sola Ruiz

Re: Get user role in Moodle course

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
In Moodle terms, you're asking the wrong question.

The question should not be "is this user a teacher" or "is this user a student", but "does this user have the capability to see link A" and "does this user have the capability to see link B".

So, your block should define some sort of relevant capabilities to determine whether a user should see one or other of the links, then check them. If the links lead to somewhere inside of Moodle, then you will then want to check the same capabilities on the destination page (or if it is an existing page, there might already be a suitable capability there that you can check).

So, the code should look something like:

if (has_capability('block/myblock:seelinka', $this->context)) {
  // Code to output link A.
} else if (has_capability('block/myblock:seelinkb', $this->context)) {
  // Code to output link B
}

(although you should, of course, choose more meaningful names for your capabilities than 'seelinka', and 'seelinkb').
Average of ratings: Useful (4)
In reply to Davo Smith

Re: Get user role in Moodle course

by Luis Sola Ruiz -
Thanks Davo for your quick answer.

I am starting in Moodle coding, so I am trying to learn as much as I can.

My idea is to create a block inside a course that will redirect to another page create for me inside moodle ( I have to figure out yet how to create a new file with the same layout than the course where the block is. Currently, it is a admin layout) where all the relevant information will be ( the content will change depending of the role of the user in that course) .

So I coded this:

$context = context_course::instance($COURSE->id);
$roles = get_user_roles($context, $USER->id, true);
$role = key($roles);
$rolename = $roles[$role]->shortname;

if($rolename == "editingteacher" || $rolename == "teacher" ){
//Link A

}else if($rolename == "student"){
// Link B

Actually work but I don't know if it is as precise as you wrote before.
In reply to Luis Sola Ruiz

Re: Get user role in Moodle course

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Well, that might work in the short term, but isn't really going to play nicely with the rest of the Moodle system.

What happens if the site admin creates a new role 'customteacher' (based on the teacher role) - that will break your block. What happens if there are certain courses where the admin would like users who are 'students' for all other purposes, to see 'link A' in your block? (I have no idea if this is a valid scenario, as 'link A', 'link B' doesn't really give any clue about what the links are for). What about if the admin wants 'editingteacher' users to see 'link A', but doesn't want 'teacher' users to see it?

You really need to be thinking in terms of capabilities that are (by default) assigned to the student / teacher roles, which can then be assigned (or removed) by an admin who is customising the specific roles in use on their site.

Have a read through the Moodle docs for more details: https://docs.moodle.org/dev/Access_API

Average of ratings: Useful (2)
In reply to Davo Smith

Re: Get user role in Moodle course

by Luis Sola Ruiz -
Thanks Davo for your useful reply, I has been checking the link you attached and I have understood better why my previous code was not moodle friendly.

I have been also trying to add my new capability with no results, here in db/access.php:

'block/tfg:createactivity' => array(
'riskbitmask' => RISK_SPAM,
'captype' => 'write',
'contextlevel' => CONTEXT_SYSTEM, // I also try different contexts like block/course/module
'archetypes' => array(
'editingteacher' => CAP_ALLOW,
'teacher' => CAP_ALLOW,
'manager' => CAP_ALLOW
),

'clonepermissionsfrom' => 'moodle/site:manageblocks'
),

and in my block class:

$context = context_course::instance($COURSE->id); // I used this also but no results

if (has_capability('block/tfg:createactivity', $this->context)) {
$this->content->items[] = html_writer::tag('a', 'Link A', array('href' => '../blocks/tfg/test.php'));

}
But it showed nothing

By the way, the purpose of my links is to show a different view inside de course allowing users to do different actions, for that the link go to another file call test.php

Thanks for your helping.
In reply to Luis Sola Ruiz

Re: Get user role in Moodle course

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Did you update your plugin version number, in order to get it to add the new capability you created?

In reply to Davo Smith

Re: Get user role in Moodle course

by Luis Sola Ruiz -
Yes, thanks! now the text appears but it doesn't work as well as it should. I define another capability for a student:

//Teacher
'block/tfg:createactivity' => array(
'riskbitmask' => RISK_SPAM,
'captype' => 'write',
'contextlevel' => CONTEXT_SYSTEM,
'archetypes' => array(
'editingteacher' => CAP_ALLOW,
'teacher' => CAP_ALLOW,
'manager' => CAP_ALLOW
),

'clonepermissionsfrom' => 'moodle/site:manageblocks'
),

//student
'block/tfg:askactivity' => array(
'riskbitmask' => RISK_SPAM,
'captype' => 'write',
'contextlevel' => CONTEXT_SYSTEM,
'archetypes' => array(
'student' => CAP_ALLOW
),

'clonepermissionsfrom' => 'moodle/site:manageblocks'
),
I updated the version again but when I am teacher/admin it appears the 2 links and when I am a student it doesn't appear anything.