I'm new and need a little guidance with capabilities

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

by Justin Hunt -
Number of replies: 8
Picture of Particularly helpful Moodlers Picture of Plugin developers

To get the context, you should get it from the page object like this

(from online_users block)

if (has_capability('block/online_users:viewlist', $this->page->context)) {

Lets start from that and see if that makes a difference.


If you feel like trying something even more exciting ...

You seem to be adding script and style tags. You should try and go 100% Moodle if you can. 

If you put your CSS inside a file called "styles.css" in your block plugin's folder Moodle will load it automatically.

Javascript admittedly is a bit trickier, and you should be using AMD modules. Which does seem like overkill. Well, it will until some other plugin loads jquery into the global space like you are, and overwrites any jquery plugins you loaded and other bad things. 


The way to load an AMD module in your block is like this:

//here we set up any info we need to pass into javascript
$opts =Array();
$opts['somedata']='blah';
$opts['moredate']='blah;
$this->page->requires->js_call_amd( "block_eportfolio/eportfoliostuff", 'init', array($opts));

Then make the js file in the following path in your plugin: amd/src/eportfoliostuff.js

Add some code like this:

define(['jquery','core/log'], function($,log) {
    "use strict"; // jshint ;_;
    log.debug('eportfolio: initialising');
    return{
        //pick up config data and use jquery
        init: function(props){
            $('#somecontrol').text(props.somedata);
        }
    };//end of return value
});

Then Moodle will run your init function shortly after the rest of the page has been loaded

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

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

by Adam Vasarhelyi -

If I try and get the context like you've written in your suggestion, I get the following error:

Exception - Argument 2 passed to has_capability() must be an instance of context, null given, called in [dirroot]\blocks\eportfolio\block_eportfolio.php

This is my code and I believe I have added the right things in access.db


In reply to Justin Hunt

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

by Adam Vasarhelyi -

Ok so I figured out it only works in the get_content function which is okay, however, when I change capabilities to DENY as you can see in the photo, the has_capability function you recommended earlier still returns true. Why is this?

In reply to Adam Vasarhelyi

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

by Justin Hunt -
Picture of Particularly helpful Moodlers Picture of Plugin developers
Well I have never tried that. I wonder did you uninstall and reinstall before changing the definitions on the capability. Because changing the capability there won't change it, for existing users. At least that is my understanding.

What you have there seems a bit unlikely. If you are just testing , perhaps try testing with a new capability?
In reply to Adam Vasarhelyi

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

by Andrew Lyons -
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.