General help

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

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

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: -
Picture of Howard Miller
Re: PHP code to find out the roles of logged in user.
Group DevelopersGroup Documentation writersGroup Particularly helpful Moodlers

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: -
Picture of Paul K
Re: PHP code to find out the roles of logged in user.
 

Dear Howard,

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

Thanks

 
Average of ratings: -
Picture of asha chaparala
Re: PHP code to find out the roles of logged in user.
 
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...............
 
Average of ratings: -