General developer forum

How to distinct Teacher and Student in Moodle 2.x?

 
Picture of Mark Johnson
Re: How to distinct Teacher and Student in Moodle 2.x?
Core developersParticularly helpful MoodlersPlugin developers

In moodle, you don't check if a user has a particular role assigned, you check if they have a particular capability in the current context.  Read up on the Access API for an overview of how the system works.

In this case, your activity module might have the capability 'mod/foo:usebuttontwo'.

The teacher role has 'mod/foo:usebuttontwo' set to "allow", but the student role does not.  Your code then looks something like this:

$modulecontext = context_module::get_instance($cmid);
echo '<button1>';
if (has_capability('mod/foo:usebuttontwo', $modulecontext)) {
echo '<button2>';
}

A slightly more modern way to do this using Templates would be to pass a parameter like "showbuttontwo" to the template which is set using has_capability() as above. The template code would then look like

<button1>
{{#showbuttontwo}}
<button2>
{{/showbuttontwo}}
 
Average of ratings: Useful (2)