Get user role in Moodle course

Re: Get user role in Moodle course

by Davo Smith -
Number of replies: 3
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.