Hiding activities by role on course page

Hiding activities by role on course page

by Itamar Tzadok -
Number of replies: 10

Hiding activities by role on course page would allow controling what certain roles, guests for instance, can and cannot see on the course page. This should be particularly useful on the front page. But afaict this is currently not possible mainly because mod capabilities have no common basis (e.g. a common 'view' capability) which could be used to filter the display of activity/resource links on the course page. Access lib does not offer a method for checking the existence of a capability and so a condition in print_section cannot work unless all mods are augmented with a designated capability, or a specific list capabilities covering all mods is explicitly stated in the condition.

There are many possible ways to add this functionality, more or less painful. I'm wondering if anyone has already thought about that and have some insights into what would be the least or the most preferable approach. smile

Average of ratings: Useful (2)
In reply to Itamar Tzadok

Resolved: Hiding activities by role on course page

by Itamar Tzadok -

So, I've added the following:

1. A new core capability 'moodle/mod:view' in lib/db/access.php which has the same definition as 'moodle/block:view':

    '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 around line 1451

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

and upgraded.

With these modifications you can hide a resource/activity link on the course page in a similar way to hiding a block. You go to the resource/activity permissions settings and remove the designated role from the capability 'moodle/mod:view'.

Comments and suggestions for improvements are more than welcome. smile

In reply to Itamar Tzadok

Re: Resolved: Hiding activities by role on course page

by Mary Cooch -
Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Testers Picture of Translators

Could this work for a label then? I've seen a number of requests for labels on the front page to be visible only to guests and not logged in users (or the opposite way round) This would solve that problem??

In reply to Mary Cooch

Re: Resolved: Hiding activities by role on course page

by Itamar Tzadok -

Yes, labels, urls etc. But I still need to figure out why I get the relevant permission for editing on my dev server but not on the production server. smile

In reply to Mary Cooch

Re: Resolved: Hiding activities by role on course page

by Itamar Tzadok -

Had to add another line in lib/accesslib.php around 6403

$extracaps[] = 'moodle/mod:view';

for the production server but probably because my production is a bit earlier than 2.2.1. smile

In reply to Itamar Tzadok

Re: Resolved: Hiding activities by role on course page

by sam marshall -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers

Not sure that is quite the right way to do it but it's close.

I proposed this some time back but it was rejected. HQ didn't like the idea of this generic view permission for some reason.

I eventually agreed with them (eh just barely) because there ARE benefits to having module-specific view permissions (like for instance, mod/page:view, which already exists, but doesn't work properly). For instance, we have restrictions on our 'prisoner' role so that prisoners can't access collaborative content - meaning they DO have, say, mod/quiz:view, but they DON'T have, say, mod/forumng:view. You can do that with a global role using the module-specific permissions, which wouldn't be possible with a single generic permission.

To implement a per-module view permission (such as mod/page:view), as well as obviously doing a require_capability in the view.php (which mod/page already does), write a function like this in the module's lib.php:

/**
  * Sets the module uservisible to false if the user has not got the view capability
  * @param cm_info $cm
  */
function ouwiki_cm_info_dynamic(cm_info $cm) {
    if (!has_capability('mod/ouwiki:view',
            get_context_instance(CONTEXT_MODULE,$cm->id))) {
       $cm->set_available(false);
    }
}

This is how to ensure your module doesn't get shown in navigation and suchlike.

--sam

In reply to sam marshall

Re: Resolved: Hiding activities by role on course page

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

So, I guess the quiz should implement that function then: MDL-31519

In reply to sam marshall

Re: Resolved: Hiding activities by role on course page

by Itamar Tzadok -

Not sure that is what I'm trying to achieve. It's not an issue with my module but rather with standard modules and if I must hack I prefer to do it once for all rather than each module at a time.

In reply to Itamar Tzadok

Re: Resolved: Hiding activities by role on course page

by sam marshall -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers

Well if you want to hack, you can obviously do it however you like smile But if you or anyone ever wants to do it in standard Moodle, the one I outlined is probably the least bad approach. (Eh, as of present thinking.)

--sam

In reply to sam marshall

Re: Resolved: Hiding activities by role on course page

by Itamar Tzadok -

Either I'm misunderstanding or misunderstood (or both big grin). To make your suggestion work before joining HQ and/or becoming a trusted developer, I need to hack each one of the standard modules and add the condition in function modname_cm_info_dynamic, right? If so then either I hack your way or I hack my way. As hacks both ways are bad, but my way is less painful after upgrade. smile

In reply to Itamar Tzadok

Re: Hiding activities by role on course page

by Bernard Boucher -

Hi Itamar,

               I try to do something like that in 2004 named Regulate Rights:

http://moodle.org/mod/forum/discuss.php?d=12511

It was based on calendar, ( i was too lazy or not enough skilled to build a module ) . The code was not updated to newers Moodle versions nor the test site availlable ;-(

But it permit at that time ( conditionnals activities did'nt exist ) to control the access to many activities and with many conditions at the same time. And it supported groups!

The scope of the permissions and the exclusions was at the activity or at the module type : for example, if my old memory is good,  for the final examination of a course, with 3 keyworks it was possible to restrict for a particular group and for a limited time ( the duration of the calendar event ) all the resources and all the quiz of a course except the final one!

You will find in that old post links to olders posts that served as inspiration  to created it.

Now using role is a good idea! It can be extended to make Moodle more adaptive, by switching on the fly ( based on forces and weakness ) the student from one role to another and presenting her/him with differents activities more adapted to his profile.

I hope it may help,

 

Salutations from Québec,

 

Bernard