On the course/view.php page, I need to figure out (within a stickyblock I've created), if the currently logged in user (the one looking at the course) is a Teacher or a Student.
Does anyone know either a function, or a snippet that I could use to tell if the user viewing a course is a 'Student' or 'Instructor'. I've looked at the $USER object, and it doesn't appear to be a property of that class.
Any help is *greatly* appreciated.
Thanks,
Eric
global $COURSE, $USER;
$context = get_context_instance(CONTEXT_COURSE,$COURSE->id);
if (has_capability('moodle/legacy:student', $context, $USER->id, false) ) {
echo "is Student<br/>";
}
if (has_capability('moodle/legacy:teacher', $context, $USER->id, false) ) {
echo "is Assitent Teacher<br/>";
}
if (has_capability('moodle/legacy:editingteacher', $context, $USER->id, false)) {
echo "is Teacher<br/>";
}
if (has_capability('moodle/legacy:admin', $context, $USER->id, false)) {
echo "is ADMIN<br/>";
}
$context = get_context_instance(CONTEXT_COURSE,$SESSION->cal_course_referer);
if ($roles = get_user_roles($context, $USER->id)) {
foreach ($roles as $role) {
echo $role->roleid.'<br />';
echo $role->name.'<br />';
}
}
So, what really is the distinguishing feature of the two types of user you want to display different content to? Wouldn't it be better to check a real capability.
Additionally, is there any way for me to call a JavaScript function in order to do this, or do I have to write a PHP intermediary (for my flash app)? I was just thinking that if I could determine whether the user had student or greater than student permissions, that would be all I needed...
Thanks for any pointers,
A
Also, there is not currently a way to do this in JavaScript - trying to enforce security on the client-side is essentially impossible. You need to check all permissions server-side. Again, it is hard to say mroe without knowing exactly what you are trying to do.
I also have problems with this concept.
I want to check if a user is a student in a given context (a course). I'd like to follow your advice Tim and use a capability test instead of
if (has_capability('moodle/legacy:student', $context, $USER->id, false) )
Let's say the set of student capabilities are a subset of those of a teacher, which seems quite likely; that means I can't distinguish by testing for the presence of a capability.
You would presumably advise me to test for the absence of a capability that is available to other roles. Doesn't that mean I'd have to check all those roles, and even then someone could create a new role that also lacked that capability?
In the mean-time you have to find some way of getting the result you want without doing anything too evil.
I am afraid that doing has_capability('moodle/legacy:student', ...) is very evil.
You have not explained what you are really trying to do, so I can't really suggest something better for your situation. What is the feature you are trying to implement?
Hi,I am also looking for a "simple" solution to make a decision on where to send a current user based on her/his role -- when s/he presses the "submit" button in the "feedback activity".
I need to get the current user's role in the current context but the legacy solution does not seem to work.
What would you suggest?
Ana
Thansk, Tim, for a lead. I found two possible 'enrol' functions:
1)
course_enrolments($courseid, $roles = '') X-Ref
Get a list of users from the client server who are enrolled in a course
param: int $courseid The Course ID
param: string $roles Comma-separated list of role shortnames
return: array Array of usernames who are homed on the
2) user_enrolments($userid)
Which one should I use and how? Or should I use something else?
I do not know what I wrote below makes sense (I'm pretty much a newbee):
--------------
global $COURSE, $USER;
if (course_enrolments($courseid->id, $roles = 'student')==$userid->) {
echo "is Student<br/>";
}
-------------
Sorry, it is a nonsense. Can you correct me, please? Thanks, Eugene
PS Ana, as soon as Tim or somebody helps with checking current user's role, I think I can help you with your task.
Hello everybody--
I have found how to get User's role per course (tested):
--------------
$coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
if (!has_capability('moodle/course:viewhiddensections', $coursecontext)) {
echo "is Student<br/>";
}
-----------
Note: $course->id may not work for a specific moodle php page -- you need to figure out how to get the course id from the page (e.g., using feedback plugin, I had to get it in the following way: get_coursemodule_from_id('feedback', $id)->course).
But testing if 'somebody is a student' is rubbish anyway. Fact is that in Moodle since 1.7 or so, there is no such thing as a student, there are just a collection of roles with different permissions. You could quite easily just delete the student role for a particular system, or change it dramatically so that whatever distinction is being drawn here doesn't matter.
In other words, compared to testing if somebody has a student role as one of their roles in a particular context, I think checking if they aren't allowed to view hidden activities is only equally hacky (and almost equally likely to work on most sites and behave appropriately on a few others or in theoretical situations).
Basically the right thing to do normally is to test for an appropriate capability, or if there isn't already one, then add a new one. If you're not doing the right thing, I think this wrong thing would be ok
--sam
You can either know the user role by checking all the roles a user is assigned in particular course, as follows.
$cContext = context_course::instance($COURSE->id); // global $COURSE
$isStudent = current(get_user_roles($cContext, $USER->id))->shortname=='student'? true : false; // instead of shortname you can also use roleid
Shortnames for the roles you want to check can be found at Site Administration->users->Permissions->Define roles (moodle/admin/roles/manage.php)
I am assuming that students are only assigned one role per course and hence I get the first role assignment record with 'current()'
or you could check the user capability to update a course (assuming that the student are not granted the capacity to update a course)
$isStudent = !has_capability ('moodle/course:update', $context) ? true : false;
The previous steps that I mentioned only works at a Course level, but if you are outside a course and want to identify a user type, use the following:
NB: There is nothing such as a user type in Moodle, so its you making the call according to the roles you have assigned the users. Here I assume that a user can be called a student if he/she is assigned a 'student' role anywhere in the system.
// user_has_role_assignment($userid, $roleid, $contextid = 0) // $contextid empty means any context
This returns true if the user have a student role in any context.
user_has_role_assignment($USER->id,5) // $roleid == 5 for student role //inside functions declare "global $USER;"