ronquiq
ronquiq

Reputation: 2717

MOODLE ROLES AND CAPABILITIES

I had developed a block in 2.5. I installed the block successfully..and gave permissions settings on front end to view this block only for one role users say 'Manager'. So, Manager/Admin can only view this block and no one else.

But this block is still visible for all. Could you please judge me.. where I went wrong.. Here is my leisure block code

Blocks/block_leisure/block_leisure.php

<?php

class block_leisure extends block_base
{
    public function init()
    {
        global $CFG;
        $this->title = get_string('leisure', 'block_leisure');
    }

    public function get_content()
    {
        global $COURSE, $DB, $PAGE, $CFG, $USER, $CFG, $SESSION, $OUTPUT;
        if ($this->content !== null) 
        {
             return $this->content;
        }
        $this->content = new stdClass;
        $context = $PAGE->context;
        $this->content->text = 'This is a leisure block content';           
        $this->content->footer = 'Footer here...';           
        return $this->content;
    } // Function - get_content().
    public function getmodules() 
    {
        return true;
    }
}

Blocks/block_leisure/db/access.php

<?php

 defined('MOODLE_INTERNAL') || die;

    $capabilities = array(
    'block/leisure:myaddinstance' => array(
        'captype' => 'write',
        'contextlevel' => CONTEXT_SYSTEM,
        'archetypes' => array(
            'user' => CAP_ALLOW
        ),
        'clonepermissionsfrom' => 'moodle/my:manageblocks'
    ),
    'block/leisure:addinstance' => array(
        'riskbitmask' => RISK_SPAM | RISK_XSS,
        'captype' => 'write',
        'contextlevel' => CONTEXT_BLOCK,
        'archetypes' => array(
            'editingteacher' => CAP_ALLOW,
            'manager' => CAP_ALLOW
        ),
        'clonepermissionsfrom' => 'moodle/site:manageblocks'
    ),
    'block/leisure:viewpages' => array(
        'captype' => 'read',
        'contextlevel' => CONTEXT_COURSE,
        'legacy' => array(
            'guest' => CAP_PREVENT,
            'student' => CAP_ALLOW,
            'teacher' => CAP_ALLOW,
            'editingteacher' => CAP_ALLOW,
            'coursecreator' => CAP_ALLOW,
            'manager' => CAP_ALLOW
        )
    ),
    'block/leisure:managepages' => array(
        'captype' => 'read',
        'contextlevel' => CONTEXT_COURSE,
        'legacy' => array(
            'guest' => CAP_PREVENT,
            'student' => CAP_PREVENT,
            'teacher' => CAP_PREVENT,
            'editingteacher' => CAP_ALLOW,
            'coursecreator' => CAP_ALLOW,
            'manager' => CAP_ALLOW
        )
    )
);

and as usual I have lang folder, version.php and read me file.

Upvotes: 1

Views: 1575

Answers (1)

davosmith
davosmith

Reputation: 6327

You need to do something with the capability you have defined, otherwise it will have no effect.

Check the capability within get_content, then return null if nothing should be displayed.

Upvotes: 1

Related Questions