New Role Specific Block Capability - need feedback

New Role Specific Block Capability - need feedback

by Dave Kichler -
Number of replies: 14
I have just finished making some minor adjustments to the block system to allow for "role specific capabilities," a functionality needed by myself and other posters I have come across. Basically it allows configuration of any block to be role specific which lets the admin specify which roles the current block should be available for. I needed this particular functionality for my Front Page (or even My Moodle) to hide/show different blocks based on what type of user was logged in (admin, student, teacher, etc). I managed to find the Role Specific HTML Block which served some of my basic purposes but found myself wanting these type of options on many of the other standard blocks.

Anyways, my modifications were direct and I didn't bother to make this functionality 'modular.' It is more of a patch really. I would just like to post my mods for anyone else to use and hopefully get some feedback on whether you think this is a useful addition and if there are any bugs I overlooked.

I changed 2 of the base block files and created two more. All files are included in the .zip and can be applied by simply extracting into your moodle folder.
The two files I changed were:
/moodle/blocks/moodleblock.class.php
/moodle/lib/blocklib.php

IMPORTANT NOTE: If you are planning on applying these modifications, I would highly recommend backing up the original copies of the files.

I also created two files:
/moodle/blocks/config.html
/moodle/lib/rolespecific.php

Good luck to all, and I would really appreciate some feedback, thanks
Dave


EDIT: I called this functionality a Capability for convenience when in actual implementation it doesn't use the capabilities system in Moodle at all.
Average of ratings: Useful (1)
In reply to Dave Kichler

Re: New Role Specific Block Capability - need feedback

by Christopher Plaistow -
Hi Dave,

I have not yet had a chance to implement this and should be able to look at this next week, but reading it sounds like something we could use.

BTW: How easy could this type of functionality be to implement for the main page resouces i.e. Labels etc. As I would like to be able to hide specific labels etc., for different users.

Unfortunately I am not in a position to do this myself, due to I am new to Moodle and PHP.

Regards,

Chris Plaistow
In reply to Christopher Plaistow

Re: New Role Specific Block Capability - need feedback

by Dave Kichler -
Hi Chris,
When I find the time I plan on writing a simple script to apply the 'patch' and create backups of the replaced files automatically (permissions allowing) to make implementing this functionality a little more user friendly.

I don't know too much about how resources work in comparison to blocks but I will take a look at them when I get the chance although I can't see it being overly difficult to implement the same sort of functionality. I'll keep you posted,

Dave

In reply to Dave Kichler

Re: New Role Specific Block Capability - need feedback

by Carl Hodkinson -

Dave

Just found your "minor adjustments" - excellent job.  I needed to have no blocks at all for users in courses and did not want to have to get the admin block every time I wanted to change settings. Just what the doctor ordered!

Took your advise and backed up the original files (oh ye of little faith wink) but am pleased to report that it all went smoothly (1.8, MySQL) - works like a dream. Thanks for contributing it.

I did experience something worth mentioning - it was caused by my stupidity blush. I restricted the Site Administration block to administrators as it disappearedsurprise - why I did it, I don't know as it is already hidden to everyone else. It certainly made me sweat for a minute though as I thought I'd locked myself out of the site settings and would have to start againangry - doing a new instance so not backed up yet. Got it back using the Back button (disabled role specific capability in block settings and resaved)big grin. Mentioned this just in case there is someone else as daft as me out there in Moodleland!!!! 

Regards

Carl

In reply to Carl Hodkinson

Re: New Role Specific Block Capability - need feedback

by Dave Kichler -
Carl,

Thanks for your feedback! I looked into the points you made and it turns out its a bug I missed. Exactly the reason I put it up here in the first place, great job.

As you mentioned, the bug basically allows you to restrict access to the block from ALL types of accounts effectively making it disappear. I was under the impression that Moodle would still print the skeleton of the block (which includes the edit icon) to an admin account when editing was on, even if you specified not to display to admin, allowing them to at least edit (to restore privileges). Turns out this is not the case and blocks are disappearing.

Thanks again to Carl for finding this bug and I will be fixing it very soon (seeing as I can't find a way to get the blocks back without going into the DB).

In reply to Dave Kichler

Re: New Role Specific Block Capability - need feedback

by Robert Edwards -

This was REALLY helpful to me.

I'm finding now that I have so many users I won't have time to manually add and subtract people from each course.

However, I do have several large groups of users (3,500 in one, 250, in another) and a few smaller groups (10-20).

I'm trying to figure out how to get moodle to auto add based on an external feild from my databse when I grab the students from that external database with an ODBC connection.

Make any sense?

thoughts?

THanks again for the patch, it was JUST what I was looking for!

In reply to Dave Kichler

Re: New Role Specific Block Capability - need feedback

by Martín Langhoff -

Just found this patch on a Moodle I administer. Why would this be needed? AFAICS, the functionality is available in Moodle already...

  • login with an admin acct
  • on the homepage, "turn editing on"
  • every block on the page has a face-and-mask icon, the mouseover tooltop says "assign roles" -- choose a block to experiment and click on that icon...
  • you'll be on a role-assign / role-override page specific for that block
  • click on "override roles" to switch to viewing overrides
  • click on the "Guest" role, you'll see some capabilities, including "moodle/block:view" - set it to "prevent" and click submit
  • repeat the previous step for all the roles that should not see the block
  • login as different users to corroborate the effect.

Is there any flaw in the process I've outlined? What use cases are not covered with this?

In reply to Martín Langhoff

Re: New Role Specific Block Capability - need feedback

by Michael Penney -
Logic is good, afaik code is not there in most blocks (most only have
moodle/site:accessallgroups ) Which doesn't do much. Currently (1.8.3) view type capabilities have to coded on a per-block basis (such as View course menu block/course_menu:view for course menu).

It would be great to have some basic capabilities like view, edit, etc. be settable for all blocks.
In reply to Michael Penney

Re: New Role Specific Block Capability - need feedback

by Dan Poltawski -
I added a capability for all blocks in 1.9 (see MDL-10573) which is moodle/block:view, and you can control access to all blocks in the way Martín describes.

The flaw in Martín's plan is that its a new capability and thus won't work on 1.8.

Dan
In reply to Dan Poltawski

Re: New Role Specific Block Capability - need feedback

by Martín Langhoff -

Ah! That explains -- I was just going to say that every block has moodle/block:view on my setup... naturally this is a prod site running 1.9.

wide eyes

Thanks Dan!

In reply to Martín Langhoff

Re: New Role Specific Block Capability - need feedback

by Michael Penney -
naturally this is a surpriseprodsurprise site running 1.9

y0u 4r3 7h3 3|173!
In reply to Dan Poltawski

Re: New Role Specific Block Capability - need feedback

by Alistair Gill -
Sorry, i'm slightly unclear here.

So 1.9 will have the ability to hide / display blocks via Martín's method built in?

Will this include custom blocks, or will they need the correct attributes assigning to them?

Thanks,
Alistair
In reply to Alistair Gill

Re: New Role Specific Block Capability - need feedback

by Dan Poltawski -
Yes, from 1.9 you can have roles-based control of which users can view blocks based on the roles system. This includes custom blocks. I've started the documentation of this in the Block permissions page in moodledocs.