I'm new and need a little guidance with capabilities

Re: I'm new and need a little guidance with capabilities

by Andrew Lyons -
Number of replies: 4
Picture of Core developers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Testers
Hello Adam,

The capabilities you define here are the defaults for a new installation or when creating that capability for the first time. Once a capability has been created based on this definition, changes to the defaults have no effect during an upgrade.

If you are just developing, then I would suggest that you change the live definitions in the UI at the same time as you make changes to the defaults.

If you have a live plugin where you need to make changes to a capability then you will need to write these changes in an upgrade script.

Note: Typically we do not set CAP_DENY in a capability definition, we just do not give any user the capability.

Andrew
Average of ratings: Useful (1)
In reply to Andrew Lyons

Re: I'm new and need a little guidance with capabilities

by Adam Vasarhelyi -

Alright, so basically I am making this plugin and I want it to show some content to teachers and some content to students. I need something that goes "If the user's role in this context is a teacher, show them this, else, show them that". I am trying to do this with has_capability. Is this the right way to approach the problem? How may I do this? I greatly appreciate anybody's help so thank you all.

In reply to Adam Vasarhelyi

Re: I'm new and need a little guidance with capabilities

by David Mudrák -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Plugins guardians Picture of Testers Picture of Translators

Don't think about it in terms like "if the user is a teacher" or "is a student". Better ask "Can the user view/do this?"

Define a capability to view or do something. Then specify that the teacher and manager archetypes have this capability assigned by default. And then use has_capability() to decide whether or not to display the particular widget.

Average of ratings: Useful (1)
In reply to David Mudrák

Re: I'm new and need a little guidance with capabilities

by Adam Vasarhelyi -

Thankyou for the advice

I'm not sure how to do this. I need a lot of guidance and sorry if it's asking too much. 

1) Define a capability to do something. The other guy said to update this in the UI and not access.php for some good reasons. I shall try to do that.

2) If it is access.php, I'm assuming the context level is "CONTEXT_BLOCK"?

3) Define a capability to "view or do something". In access.php, I am only aware of captype = write. I want something to be shown to teachers, and something totally different to be shown to students.

4) I'm not sure what exactly to code for has_capability. I understand the first parameter is the capability which I'm assuming refers to what I write in access.php, and the second part is the context. If I get the context of the user for the 'block', what exactly am I doing? Where is it defined by default in the system what role a user is in the block I made? Or maybe I should pick the context of the course? I'm not sure what to write or how to write it here.

We will get it working eventually. Thanks for ANY help smile 

In reply to Adam Vasarhelyi

Re: I'm new and need a little guidance with capabilities

by Dominique Palumbo -
Picture of Particularly helpful Moodlers Picture of Plugin developers
Hi,

in the view.php I've something like that.
So I use the variable $mode to choose what I display to student or to teacher (or any adminstrative role that can create the activity).
Only teacher has the capability to create the module.

if (has_capability('mod/modulename:create', $modulecontext)) {
    $mode = 'teacher';
} else {
    require_capability('mod/modulename:view', $modulecontext); // This to exclude user that have no righ to the activity.
    $mode = 'student';
}

You can also create capabilities to read.
For teacher and manager and if the user don't have this capacity display something else.

'mod/modulename:viewhigh' => array(
    'captype' => 'read',
    'contextlevel' => CONTEXT_MODULE,
    'archetypes' => array(
    'teacher' => CAP_ALLOW,
    'editingteacher' => CAP_ALLOW,
    'manager' => CAP_ALLOW)
),

I hope it's help.