[Moodle 2.5] Adding new capabilities to an Individual or All modules and setting them

[Moodle 2.5] Adding new capabilities to an Individual or All modules and setting them

by Pranoy Agrawal -
Number of replies: 12

Hello Guys,
I want to hide a label from guest while show them to authorized users while show another label to guest but hide it from authorized user... So I followed the steps that are suggested in http://docs.moodle.org/dev/NEWMODULE_Adding_capabilities and https://moodle.org/mod/forum/discuss.php?d=195395 to add new capability for View..
But it wasn't successful.. I also tried adding this capability to label module only..but again was not successful.. on the contrary the hiding facility of the label became buggy...
I am a bit new to moodle and hence I think I am making mistake in putting up the code at right place.. Can anyone please help me out ? I am using Moodle 2.5(20130501)

Steps I Followed which were Unsuccessful:

1. Added new core capability 'moodle/mod:view' in lib/db/access.php.

'moodle/mod:view' => array(
'captype' => 'read',
'contextlevel' => CONTEXT_MODULE,
'archetypes' => array(
'guest' => CAP_ALLOW,
'user' => CAP_ALLOW,
'student' => CAP_ALLOW,
'teacher' => CAP_ALLOW,
'editingteacher' => CAP_ALLOW,
)
),

2. A respective string in lang/en/role.php

$string['mod:view'] = 'View mod';

3. A condition in course/lib.php

if (!has_capability('moodle/mod:view',
get_context_instance(CONTEXT_MODULE, $mod->id))) {
continue;
}

4.Upgraded

I think I made mistake in placing the step 3 code line...

Average of ratings: -
In reply to Pranoy Agrawal

Re: [Moodle 2.5] Adding new capabilities to an Individual or All modules and setting them

by Pranoy Agrawal -

Ohk I was able to add the capability option to the permissions page but still it's not working.. can anyone atleast tell me what function should I use in course/lib.php in order to make it working ???

See the Image in Attachment!

Attachment Capability added.jpg
In reply to Pranoy Agrawal

Re: [Moodle 2.5] Adding new capabilities to an Individual or All modules and setting them

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

You might find you are better off either using the built-in Moodle methods for hiding activities from certain users, or creating a new activity (based on label), that can show different messages based on capabilities (and properly define the capabilities as part of that activity, rather than hacking them in to core code).

The first one is easiest to explain - you could use either groupings: http://docs.moodle.org/en/Available_for_group_members_only or conditional availability (possibly tied to a user profile field): http://docs.moodle.org/en/Conditional_activities

For the second - writing custom code - you will need to investigate this a bit more carefully (especially taking into account the way that course data is cached, so it may be hard to display different label content to different users).

Average of ratings: Useful (1)
In reply to Davo Smith

Re: [Moodle 2.5] Adding new capabilities to an Individual or All modules and setting them

by Pranoy Agrawal -

Sorry but neither Groupings nor Activities can serve the purpose here.

Here actually I want one label to be shown only to guest and another only to logged in users.But groups can only be made using users not there roles and on the other hand activities gives 3 types of restrictions based on time span,grades and profile fields... and none of these define either their roles or if they are logged in or not..

I am half way through in the above process but just not getting the function that will display the module only on receiving the capability..

btw thanx for replying :D

In reply to Pranoy Agrawal

Re: [Moodle 2.5] Adding new capabilities to an Individual or All modules and setting them

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

You can have conditions based on user profile fields, so you could have a condition based on 'firstname' = 'Guest' (or something like that).

However, it does sound like this is something that you need a custom plugin for, so stop messing around with the core capabilities, and create a new plugin (based on label), with capabilities like: 'mod/MODNAME:guestmessage' and 'mod/MODNAME:studentmessage' (or similar) and control the message to display with those capabilities (to actually show/hide it you will probably need to add the following to mod/MODNAME/lib.php:

function MODNAME_cm_info_view(cm_info $cm) {
if ([capability test]) {
    $cm->content = 'Content 1'; // Show 'Content 1'.
} else if ([capability test]) {
    $cm->content = 'Content 2'; // Show 'Content 2'.
} else {
    $cm->uservisible = 0; // Hide completely.
}
}

Obviously, you'll need to fill in your own 'MODNAME', '[capability test]' and suitable content in 'Content 1' or 'Content 2'. Note: this is probably terrible for performance, as it overrides the cached label contents - you may well want to do some caching of your own to make up for this.

In reply to Davo Smith

Re: [Moodle 2.5] Adding new capabilities to an Individual or All modules and setting them

by Pranoy Agrawal -

Actually changing the content of same label again and again is a serious problem in itself.. It will affect the performance adversely and is a very bad idea to implement..

So now if we don't change the content of a label then basically making a new plugin based on label means that there is no 'read' capability.. and to add that I have to go through the same process again..

Here I just want to learn how can we add this capability to the modules as few have them at the moment but I would need this capability in other modules too..

I have been successful with adding the capability but just I want to know what condition should we put on mod/lib.php or mod/[respective module]/view.php in order to hide the section when has_capability results in false..

In reply to Pranoy Agrawal

Re: [Moodle 2.5] Adding new capabilities to an Individual or All modules and setting them

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Dear Pranoy,

Could you take a different approach by having two themes?  I.e. a site theme that has css etc. to show the labels and a user theme that does not.  By setting 'user themes' and not allowing users to select the site theme, in fact have one theme and force that as the default for users.

Cheers,

Gareth

In reply to Gareth J Barnard

Re: [Moodle 2.5] Adding new capabilities to an Individual or All modules and setting them

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

But the best solution is still your own label resource with 'has_capability()'.

In reply to Gareth J Barnard

Re: [Moodle 2.5] Adding new capabilities to an Individual or All modules and setting them

by Pranoy Agrawal -

I have achieved to hide all the labels from the users which has not got the capability to view them...(Thanx to Davo Smith)

1.Adding it to mod/label/db/access.php

 'moodle/mod:view' => array(
        'captype' => 'read',
        'contextlevel' => CONTEXT_MODULE,
        'archetypes' => array(
            'guest' => CAP_ALLOW,
            'user' => CAP_ALLOW,
            'student' => CAP_ALLOW,
            'teacher' => CAP_ALLOW,
            'editingteacher' => CAP_ALLOW,
        )
    ),

2. A respective string in mod/label/lang/en/role.php

$string['mod:view'] = 'View mod';

3. Adding this to mod/label/lib.php

function label_cm_info_view(cm_info $cm) {
$context = context_module::instance($cm->id);
if (!has_capability('mod/label:view', $context)) {
$cm->uservisible = 0;
}
}

But I need the capability to be available for individual label under Selected label>edit>assign roles>label administration>permissions menu..where we can set different permissions for different labels..

But I have noticed that even the default capabilities are not working correctly.. I hided a label and then turned on the display of hidden activities for guest but still the label is not visible to them..

 

In reply to Pranoy Agrawal

Re: [Moodle 2.5] Adding new capabilities to an Individual or All modules and setting them

by Shahab Mohd -
Hi Pranoy,
I was searching for the same type of capability for my site too. Actually my requirement was not that complicated. I created an  admin dashboard using label to make admin settings userfriendly for our managers. I created it in front page. Now I need to hide the label from all other users except manager when they logged into frontpage. 
I tried above mentioned method but its not working [ moodle 2.6]. did you succeed in your attempt to make the capability, or can u put some lights in to my requirement?

even if label is hidden for entire site, it is not a problem for us because we r not using label anywhere else.

Thank you
In reply to Gareth J Barnard

Re: [Moodle 2.5] Adding new capabilities to an Individual or All modules and setting them

by Pranoy Agrawal -

Activating two themes at a time will again decrease the performance... The changing of themes after login will definitely reduce performance.. 

In reply to Pranoy Agrawal

Re: [Moodle 2.5] Adding new capabilities to an Individual or All modules and setting them

by Pranoy Agrawal -

Anyone ?? Please Help! sad

In reply to Pranoy Agrawal

Re: [Moodle 2.5] Adding new capabilities to an Individual or All modules and setting them

by Jona Turner -

I know this is an old thread, however I was just playing with the same thing and found a very easy solution....

 

  • Have 'restrict access' enabled
  • For guests only = 
  • For logged in users = 

 

On the new Moodle 2.7 + versions, it's an updated screen but essentially the same process.

 

Hope this helps someone in the future.