Roles and contexts in Moodle 1.7

Roles and contexts in Moodle 1.7

by Greg Lyon -
Number of replies: 4
I need some help please.

I'm trying to better understand the new roles system in Moodle 1.7 well enough to make some major hacks to my Moodle. I'm finally understanding just how powerful this can be, but also not nearly sure of myself enough to proceed on certain fronts.


Hopefully somebody can either a) answer the following, or b) point me to the place where it's already been answered smile
  1. Contexts: It looks like there are currently 8 contexts in Moodle. Is the following correct? (and what does context_personal refer to?)
  • CONTEXT_SYSTEM (10) instanceid refers to id in mdl_course
  • CONTEXT_PERSONAL (20) ???
  • CONTEXT_USER (30) instanceid refers to id in mdl_user
  • CONTEXT_COURSECAT(40) instanceid refers to id in mdl_course_categories
  • CONTEXT_COURSE (50) instanceid refers to id in mdl_course
  • CONTEXT_GROUP (60) instanceid refers to id in mdl_group
  • CONTEXT_MODULE (70) instanceid refers to id in mdl_module
  • CONTEXT_BLOCK (80) instanceid refers to id in mdl_block
Roles: Can I simply query mdl_role_assignments for questions such as whether a user is a teacher in any course, or whether a user is a course creator anywhere, (or a student for that matter)? If so it seems like it'd be quite easy to recreate the isteacherinanycourse() and similar functions, right? re: isteacherinanycourse(): Do replacement functions already exist in Moodle for these deprecated functions? I'm sure having a tough time finding what I think must be there, either in the source code or moodledocs or the functions reference.
Thanks in advance!
Greg.

Average of ratings: -
In reply to Greg Lyon

Re: Roles and contexts in Moodle 1.7

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
  1. All the context levels are defined at the top of lib/accesslib.php. The contexts are in the mdl_context table, which has columns id, contextlevel and instanceid. So the context for course 27 will be the row with contextlevel = 50 and instanceid = 27. In your code, you should get the context with the get_context_instance function. E.g.

    $context = get_context_instance(CONTEXT_COURSE, 27);

    CONTEXT_PERSONAL is yourself. It is used for checking things like whether that person is allowed to update their own profile, or see their own grades.

  2. Normally in your code you should not be thinking about roles to control what the user can do, you should think about cababilities. So if you want to know whether the current user can jump up and down (for example), you don't think about roles, instead you do

    if (has_capability($context, 'mod/mymodule:jumpupanddown')) {
    ...
    }

    Then it is up to the administrator, when setting up the roles for the site, to decide which roles have which capabilities.

    You define the list of capabilities for your module with a file called access.php in the db folder of your plugin.

  3. As you can see from the previous answers, you need to develop a new way of thinking, you should not be looking for direct replacements for the old functions.
The roles stuff is documented at http://docs.moodle.org/en/Development:Roles#Programming_Interface. That page includes a list of all the existing capabilites.

Average of ratings: Useful (2)
In reply to Tim Hunt

Re: Roles and contexts in Moodle 1.7

by Greg Lyon -
Thanks, Tim.

I'm really starting to see what you mean about develop a new way of thinking. So, instead of thinking 'is this user a course creator' I have to start thinking...does this user have the ??? capabilit(y/ies), and then target the capabilit(y/ies) I care about, right?

Now if I can just get my head around that, I should be in better shape.wide eyes


In reply to Greg Lyon

Re: Roles and contexts in Moodle 1.7

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
That's exactly right.

And if what you are doing is a new bit of functionality, don't hesitate to make up a new capability for it. That way, admins can configure exactly which roles can use the new feature, so it gives them more flexibility.
Average of ratings: Useful (1)
In reply to Tim Hunt

Re: Roles and contexts in Moodle 1.7

by Greg Lyon -
That hadn't even crossed my mind. thoughtful I don't think I'm to that stage yet, but thanks, Tim, for waking me up to the possibility!