Is there a thing called a blocks context?

Is there a thing called a blocks context?

by Matt Chambers -
Number of replies: 3

Can I ask if I have my understanding correct:

Course context is asking if the user is someone on this course (teacher, student etc. ). Then its saying, the role they are in, does it have a certain capibility?

Module context: Is asking if the user is on any course as a role (teacher or student) and if they are, does whatever role they have then have the capibility to say edit a module like a quiz? is that right?

Is there a block context thats the same? likec can they edit a block? so some teachers could, some couldnt. 

then there are the site contexts. does this check if this user has the capibility anywhere in moodle?

im trying to get my head around things and got cinfused by the docs.

Average of ratings: -
In reply to Matt Chambers

Re: Is there a thing called a blocks context?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Well, your wording is not clear. A course context does not ask anything. It is a thing, not a question.

If you understand object-oriented programming, then one way to understand it is that a context is like a base class for "subset of a Moodle site", with subclasses like Course, module, block, system, etc.

Another way to think about it is like a file system. You have your hard drive, which contains folders, which contains files. The system context is is like the whole hard drive. Course and course-category contexts are like folders, and module, block and user contexts are like files.

Checking a capability is like checking a file permissions on a file or a folder.

In reply to Tim Hunt

Re: Is there a thing called a blocks context?

by Matt Chambers -

Ahh ok, 

Thanks. 

So if you check a users permissions, (check if they have the capibility to do something) you are doing this in a context. So if you are in the context of a course, then you check if their role (like a teacher) has the capibility. If you are in the context of a Module, then this module is part of a course. So say they are a teacher on a course, you check that this teacher has the capibility to do something in the module (quiz etc) on this course. And you do this with the module context. Is the reason you are passing the module id in, because that particular module could have specific role/capibilities that have overdidden the defaults?

In that case I am guessing there must be a block context then? i.e. BLOCK_CONTEXT??

And to be clear, the user context is used to determine if this user logged in (role of say student or teacher) can do something when looking at another USER (hence being in the USER_CONTEXT?)

Thanks

Matt

In reply to Matt Chambers

Re: Is there a thing called a blocks context?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

The bit you write that is not quite accurate is:

"So if you are in the context of a course, then you check if their role (like a teacher) has the capibility"

A user does not have a role (in a particular context) they probably have several. The most normal case is to have two roles:

Suppose we are in course Roles 101, then user Matt has two roles. Authenticated user inherited from the system context, and Student from the course context.

The same two roles would apply in the module context of any activity in that course.

A more complex example, with three roles, would apply if you were a Moderator in one particular forum in that course.

Therefore, when you check a capability, you are checking the capability of the user in that context, not the capability of one of their roles. Naturally, there have to be rules for when the two roles have different permission. If you really want to know, read section 13.2 of http://www.aosabook.org/en/moodle.html.

To help understand all this, go to the Check permissions page in any context in Moodle. (You can find the link to this in the settings block, but is slighly different places depending on the type of context.) You select a user, and then it will list all the roles they have in this context, and show you all the relevant capabilties, and whether the user has them or not.

Finally, to explain user context. If I go and look at your user profile, then my ability to do that is checked in the user context for your user id. (Although user profiles are a bit more complex than that. There are other privacy settings that may affect it, in addition to the simple has_capabiltiy check.)