How to selectively hide individual files that are contained inside a folder resource

How to selectively hide individual files that are contained inside a folder resource

by Frankie Kam -
Number of replies: 4
Picture of Plugin developers

Hi Developers and coders

I am trying to solve the problem with the Folder resource. I can hide an entire folder by restricting the access based on a value in a user's custom profile field. What I cannot do is to selectively hide individual files that are contained inside the folder. See this post for the background.


So I have decided to find a hack to the Folder resource code that will selectively hide (simply not display) a file if it detects that a user's custom profile field, in my case a custom profile field named category, contains the value "Franchise".



Pseudocode:


If $user->customprofilefield named category contains "Franchise" then

   do not display the file

else

   display the file as normal

endif


Can anyone point me the way vis-a-vis code examples?

Regards
Frankie Kam

Average of ratings: -
In reply to Frankie Kam

Re: How to selectively hide individual files that are contained inside a folder resource

by Frankie Kam -
Picture of Plugin developers

Hi Y'all

Haha. I got it to work! This was a toughie, but in the end, not so tough after all.

In the file /mod/folder/renderer.php, add in the boldfaced text code:

protected function htmllize_tree($tree, $dir) {
        global $CFG;
       //The below three lines were added by Frankie Kam on 30th April 2015
       global $USER;
       require_once("$CFG->dirroot/user/profile/lib.php");
       profile_load_custom_fields($USER); 

       ...

        foreach ($dir['files'] as $file) {
           //The below two lines were added by Frankie Kam on 30th April 2015
           if((strtolower($USER->profile['category'])=="franchisee") && ($file->get_author() == "private"))
               continue;
 
       ...
}

For a fuller and clearer picture, see the below image:


What the bottom yellow-highlighted code is saying is this:

If a user who is logged into the course has a custom profile field named category that is set to "franchisee"
   AND a file's author is "private"
then
   that file will not show up in the Folder tree!
 
endif

Get it?


Now this works if you change the author of a file, inside a Moodle folder resource, to "private". Like so:

Turn Editing on and Edit the Folder resource...



and then change the Author to "private" (in this example)...



The net effect of these four actions, which are
(1)  The User Custom Profile field is created - in this example, the name of the field is category 
-and-
(2) The User Custom Profile field's value is set to "franchisee"
-and-
(3) A file, inside a Folder resource, has its Author attribute changed to "private"
-and-
(4) You insert the yellow-highlighted code shown above, 

then you get this:

Now you see it,


now you don't! 



Like in the "Chipsmore" advertisement. So ladies and gentlemen, in the absence of the Folder resource having restrictions on individual file access, this is one way how you can hide an individual file inside a Moodle Folder resource in Moodle 2.7.2.

I'll take a bow now.

Best regards
Frankie Kam, Malaysia

Average of ratings: Useful (1)
In reply to Frankie Kam

Re: How to selectively hide individual files that are contained inside a folder resource

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

Would it then be a good idea to have that user profile field set somehow centrally (maybe a database interaction by the admin?) and then hide the custom field on the user profile page itself (using css?) so that the user cannot just change their category to 'anything else' and then have access to that file anyway?

In reply to Richard Oelmann

Re: How to selectively hide individual files that are contained inside a folder resource

by Frankie Kam -
Picture of Plugin developers

Hi Richard

user profile field set somehow centrally (maybe a database interaction by the admin?) 
>
Yes, that's a good idea so that the criteria is not hardcoded like now.

so that the user cannot just change their category to 'anything else'
>
Moodle already takes care of this via the Custom Profile field settings:


The custom field can be locked if visible, otherwise it can be both locked and made invisible to the user. So no chance for the user to change it.

Regards
Frankie Kam 

Average of ratings: Useful (1)
In reply to Frankie Kam

Re: How to selectively hide individual files that are contained inside a folder resource

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

Thanks Frankie - its been a while since I looked at custom profile fields (I think the last time was when i implemented your additions to the online user block in something like Moodle2.3 smile ) So I'd missed that change in the settings! Very useful!