PHP code to find out the roles of logged in user.

PHP code to find out the roles of logged in user.

by Paul K -
Number of replies: 3

Hi,

is there any build in PHP function in Moodle 2.5.2, which can be used to find out what roles posses the logged in user? I need to have this functionality already on the mainpage and not in course, therefore such function such aus has_capability() does not help me in this case.

I want to find out, whether the logged in person have only the student rights.

It should be something in this direction:

if (isteacher() || isadmin() || isteacherinanycourse()) {
   print <a target="_blank" href="File.pdf">Link visible only for Teachers</a>
 }
else
{
   print  <a target="_blank" href="File2.pdf">Link visible only for Students</a>
}
?>

Thank you in advance for help with this.

Paul

 

Average of ratings: -
In reply to Paul K

Re: PHP code to find out the roles of logged in user.

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

If I were you, I would have a look through lib/access.php wink

However, you are almost certainly approaching this the wrong way. You do not want to think it terms of what roles a user has, you should be thinking in terms of what capabilities a user has in a given context

If you ask "is teacher()" then what happens if you copy the teacher role?

So, what you probably want is something like...  

if (has_capability('moodle/course:update, $context)) {
     ....teacher stuff...
} else {
    ....student stuff....
}

Average of ratings: Useful (1)
In reply to Howard Miller

Re: PHP code to find out the roles of logged in user.

by Paul K -

Dear Howard,

ok, you are right. I will look on the function in lib/access.php.

Thanks

In reply to Paul K

Re: PHP code to find out the roles of logged in user.

by asha chaparala -
Dear Howard, I want to find logged in user role for that site through webservises,please help me to fix this issue? waiting for reply...............