This is a test site. Any changes will be lost!

Development:NEWMODULE Adding capabilities: Difference between revisions

From MoodleDocs
No edit summary
 
(25 intermediate revisions by the same user not shown)
Line 1: Line 1:
Create one access.php file in the <<NEWMODULE>>/db directory.
In order to add a capabilities your <<NEWMODULE>> you have to:
 
==Create one access.php file in the <<NEWMODULE>>/db directory.==
 
It must contain
It must contain
*the capabilities to be installed and
*the capabilities to be installed and
Line 5: Line 8:


A model of what has to be added is:  
A model of what has to be added is:  
$mod_glossary_capabilities = array(
    $mod_glossary_capabilities = array(
        'mod/<<NEWMODULE>>:<<CAPABILITYNAME>>' => array(
            'riskbitmask' => RISK_SPAM | RISK_PERSONAL | RISK_XSS | RISK_CONFIG,
            'captype' => 'write',
            'contextlevel' => CONTEXT_MODULE,
            'legacy' => array(
                'student' => CAP_ALLOW,
                'teacher' => CAP_ALLOW,
                'editingteacher' => CAP_ALLOW,
                'admin' => CAP_ALLOW
            )
        ),
    and you are supposed to iterate the array 'mod/<<NEWMODULE>>:<<CAPABILITYNAME>>'
    for each <<CAPABILITYNAME>> you want to add
    following the same structure described before and, at the end,
    close the main array with
    )
 
The element 'riskbitmask' will be reflected in the list of icons of each row of the 'Override permissions'->roles page.
 
You should add the right number of icon ranging from
    all four icons
        'riskbitmask' => RISK_SPAM | RISK_PERSONAL | RISK_XSS | RISK_CONFIG,
    up to no icon at all,
        just removing the element 'riskbitmask' from the array 'mod/<<NEWMODULE>>:<<CAPABILITYNAME>>'
 
The element 'captype' is ???


'mod/<<NEWMODULE>>:<<CAPABILITYNAME>>' => array(
The element 'contextlevel' is ???


'riskbitmask' => RISK_SPAM | RISK_PERSONAL | RISK_XSS | RISK_CONFIG,
The element 'legacy' defines the legacy default for that permission. It is an array and for each element you are allowed to choose one (and only one) of these four elements: CAP_ALLOW, CAP_PREVENT, CAP_PROHIBIT, and inherit (if you don't set it). This defines the default selected radio button in the 'Legacy roles' page.


'captype' => 'write',
== Change the version number of your <<NEWMODULE>> ==
'contextlevel' => CONTEXT_MODULE,
Just change the version number of your <<NEWMODULE>> by editing the file mod/version.php. Of course, you have to change it with a bigger one. :-)
'legacy' => array(
    'student' => CAP_ALLOW,
    'teacher' => CAP_ALLOW,
    'editingteacher' => CAP_ALLOW,
    'admin' => CAP_ALLOW
        )
    ),


iterate the array 'mod/<<NEWMODULE>>:<<PERMISSIONNAME>>' for each <<CAPABILITYNAME>> you want to add following the same structure and, at the end, close the array
==Visit the "notification page" of the Site Administration block==
)


The element 'riskbitmask' will be reflected in the list of icons of each row of the 'Override permissions'->roles page. You should add the right number of icon ranging from
And follow the instruction on the monitor.
*all four icons
 
        'riskbitmask' => RISK_SPAM | RISK_PERSONAL | RISK_XSS | RISK_CONFIG,
==Add to your code the right triggger==
*up to no icon at all, just removing the element 'riskbitmask' from the array 'mod/<<NEWMODULE>>:<<CAPABILITYNAME>>'
 
Add to your code something like:
 
    if (has_capability('mod/<<NEWMODULE>>:<<CAPABILITYNAME>>', $context)) {
 
In order to let it works, just add before it the definition of the $context variable


The element 'captype' is ???
    global $COURSE;
The element 'contextlevel' is ???
    if (! $cm = get_record("course_modules", "id", $COURSE->id)) {
The element 'legacy' define the legacy default for that permission. It is an array and for each each element you are allowed to choose one (and only one) of this four elements: CAP_ALLOW, CAP_PREVENT, CAP_PROHIBIT, and inherit (if you don't set it). This define the default radio button selected in the 'Legacy roles' page.
        error("Course Module ID was incorrect");
    }
    $context = get_context_instance(CONTEXT_MODULE, $cm->id);

Latest revision as of 16:49, 7 April 2008

In order to add a capabilities your <<NEWMODULE>> you have to:

Create one access.php file in the <<NEWMODULE>>/db directory.

It must contain

  • the capabilities to be installed and
  • the defaults for each standard role.

A model of what has to be added is:

   $mod_glossary_capabilities = array(
       'mod/<<NEWMODULE>>:<<CAPABILITYNAME>>' => array(
           'riskbitmask' => RISK_SPAM | RISK_PERSONAL | RISK_XSS | RISK_CONFIG,
           'captype' => 'write',
           'contextlevel' => CONTEXT_MODULE,
           'legacy' => array(
               'student' => CAP_ALLOW,
               'teacher' => CAP_ALLOW,
               'editingteacher' => CAP_ALLOW,
               'admin' => CAP_ALLOW
           )
       ),
   and you are supposed to iterate the array 'mod/<<NEWMODULE>>:<<CAPABILITYNAME>>' 
   for each <<CAPABILITYNAME>> you want to add
   following the same structure described before and, at the end,
   close the main array with
   )

The element 'riskbitmask' will be reflected in the list of icons of each row of the 'Override permissions'->roles page.

You should add the right number of icon ranging from

   all four icons 
       'riskbitmask' => RISK_SPAM | RISK_PERSONAL | RISK_XSS | RISK_CONFIG,
   up to no icon at all,
       just removing the element 'riskbitmask' from the array 'mod/<<NEWMODULE>>:<<CAPABILITYNAME>>'

The element 'captype' is ???

The element 'contextlevel' is ???

The element 'legacy' defines the legacy default for that permission. It is an array and for each element you are allowed to choose one (and only one) of these four elements: CAP_ALLOW, CAP_PREVENT, CAP_PROHIBIT, and inherit (if you don't set it). This defines the default selected radio button in the 'Legacy roles' page.

Change the version number of your <<NEWMODULE>>

Just change the version number of your <<NEWMODULE>> by editing the file mod/version.php. Of course, you have to change it with a bigger one. :-)

Visit the "notification page" of the Site Administration block

And follow the instruction on the monitor.

Add to your code the right triggger

Add to your code something like:

   if (has_capability('mod/<<NEWMODULE>>:<<CAPABILITYNAME>>', $context)) {

In order to let it works, just add before it the definition of the $context variable

   global $COURSE;
   if (! $cm = get_record("course_modules", "id", $COURSE->id)) {
       error("Course Module ID was incorrect");
   }
   $context = get_context_instance(CONTEXT_MODULE, $cm->id);