Find system role type

Find system role type

by Danny Wahl -
Number of replies: 9
I'm using the Aardvark theme with the drop-down menus. Our tech facilitator asked if there was a way to add a menu that's only for Teachers so that we could link to teacher resources and intranet files without students/parents being able to see the links.

so what I'm hoping is possible is to search in CONTEXT_SYSTEM and determine a user's role. I don't want to use a has_capability() because that feels more like a hack to me than actually checking a user's system role.

I found in the documentation that there's load_defaultuser_role() to call the permissions, but is there a way to check what that role actually is?

I'm imagining it looking something like this:

if (!defaultuser_role('Student' || 'Guest')) {
 echo "Teachers/Admins can see this";
}
anything like that exist?
Average of ratings: -
In reply to Danny Wahl

Re: Find system role type

by Mary Cooch -
Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Testers Picture of Translators
Sorry I can't help you with code but I don't see how this can work because teachers do not (should not) have the role system wide (unless yours all do, which is unusual) I have done what you want with blocks, creating a new role and applying it to all those who are teachers in the real world - then only they can see it but not the students - but as far as altering the code I couldn't say. (screencast here)
In reply to Mary Cooch

Re: Find system role type

by Danny Wahl -
Ok, reading the library code is actually more beneficial than reading through the documentation- sadly*. But I think I've figured it out. I just have to manually set the context to system, rather than defining it with the $context variable and having it be the current context ($courseid). So this is my preliminary function, though I haven't tested it yet:

if (!has_capability('moodle/legacy:guest', CONTEXT_SYSTEM, $userid) || !has_capability('moodle/legacy:student', CONTEXT_SYSTEM, $userid)) {
echo "You're not a student or a guest!";
}

I'm not sure if that's actually how to set the context (e.g. is the value of $context acutally "CONTEXT_SYSTEM" - it seems to be according to the documentation), and still I'm using 1.7 legacy functions 'moodle/legacy:' which I would like to avoid.

It would be nice if there was a way to call based on role/context rather than capability/context.

*not knocking the docs, it's just sad I understand code more than I do English.
In reply to Danny Wahl

Re: Find system role type

by Edward Beckmann -
Hi Daniel

I don't really have enough detail about what you want, but how about having a user theme as opposed to site or course themes? In this case, teachers / admins get a theme which has a slightly different header.html than the student one. You just use aardvark and aardvark_t or whatever, and could links the theme files so the only extra one on your server would be the extra header.html.

Good luck

Ed
In reply to Edward Beckmann

Re: Find system role type

by Danny Wahl -
Essentially the Aardvark theme has some extra divs that aren't part of any standard or extra Moodle blocks- meaning they're not tied in any way to the Moodle APIs, there's just space for you to hard-code in your own links: external or internal Moodle links.

What I'm trying to do is to wrap some Moodle functionality around these divs.

From what I've read in the Moodle dev docs is that a context sets a role and the Role sets capabilities, as you move from page to page within Moodle your role changes, thus forcing different capabilities.

I want to determine the user's system role capabilities and from there display these divs (or different links based on role) to the user. However, from what I understand, the user's role changes based on $context, thus affecting the capabilities- so I want to use the system capabilities, regardless of $context.


I don't know anything about user themes, but it seems that this still wouldn't help because the role would change from course to course affecting capabilities, and thus the display of the divs.
In reply to Danny Wahl

Re: Find system role type

by Edward Beckmann -
OK - I think I understand now and that is way beyond my knowledge. I imagined that you wanted some people to have one top menu (all of the time) and other people to have a different one (but still all of the time).

For info, if you enable user themes then the whole set of css, logos, icons, header.html and footer.html follows the user. I have a course which is common to two different types of customer, and they keep their own look and feel even when on that same common course. It's also useful in that I an give guests one look and feel, and logged-in users another.

Good luck.

Ed
In reply to Danny Wahl

Re: Find system role type

by Danny Wahl -
Ok, I've figured it out. And I just want to clarify what I'm trying to accomplish. I wanted to find a way to determine a user's system role, regardless of current context, and then allow them to see some content based on if they were allowed access it on a system level.

I wanted to accomplish this because we have training courses on our Moodle, so a user that would normally be a course creator or a teacher, would be a student- but I still want them to be able to access this extra information within the course that may also have traditional students enrolled in it.

So here's the final code:



if (has_capability('moodle/legacy:teacher', get_context_instance(CONTEXT_SYSTEM)))
{
echo 'system level teachers, course creators, and admins can see this, regardless of current role';
}
echo '<br>everyone can see this';



I was stumped for a while because I was trying to say something like

if (!has_capability('moodle/legacy:student')){}

and I couldn't get it to work because it was showing up for everyone- then I remembered on the system level everyone is just an 'authenticated user' for which there is no 'legacy' function.

Thanks for your help guys!
In reply to Danny Wahl

Re: Find system role type

by Mark Fleet -

Hi Dan,

Just wondering how you managed this, been trying to work it out for a while, what exactly did you do?

Cheers,

Mark

In reply to Danny Wahl

Re: Find system role type

by Mauricio Pérez Llantén -

the has_capability('moodle/legacy:teacher',....)) doesn't work on moodle 2.2 > ...

In test environment show the following message (spanish) :

"Detectado un error de codificación, debe ser corregido por un programador: Legacy capabilities can not be used any more!"

Any ideas to fix or try another alternative ? ...

Best regards !

Mauricio Pérez