Tuesday, July 19, 2011

Renaming Capabilities in Moodle 1.9.x

On the off chance you need to rename moodle capabilities

eg.
block/mycustomblock:viewpages
to
block/mycustomblock:viewadminpages

Here is what you have to do:

1) edit upgrade.php and add two create sql statement

eg.
$sql = "UPDATE {$CFG->prefix}capabilities
SET name = 'block/mycustomblock:viewadminpages'
WHERE name = 'block/mycustomblock:viewpages'";

$sql =
"UPDATE {$CFG->prefix}role_capabilities
SET capability = 'block/mycustomblock:viewadminpages'
WHERE capability = 'block/mycustomblock:viewpages'";

2) execute_sql($sql) both of them

3) The last part is that you MUST also change the mycustomblock/db/access.php to include the new capability type. This is because Moodle will compare capabilities in access.php to the ones stored in the database.
a) Moodle will search for new capabilities.
b) Moodle will delete them from the mdl_capabilities table, then add them as a new row.
c) Moodle will then delete the capability from mdl_role_capabilities. (I believe this is to preserve the referential integrity of foreign keys between mdl_role_capabilities.capability and mdl_capabilities.name)

In access.php

Add :
'block/mycustomblock:viewadminpages' => array(
        'riskbitmask' => RISK_PERSONAL,
        'captype' => 'read',
        'contextlevel' => CONTEXT_MODULE,
        'legacy' => array(
        )
    ),


Look at upgrade_blocks_plugins() in blocklib.php for more info.

No comments: