Pages

Thursday, April 30, 2015

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


Pixabay. License: CC0 Public Domain.
Free for commercial use / No attribution required


Does the structure below look familiar to you? Have you ever used a Folder resource in Moodle? If you have then you will know that you can hide an entire folder by restricting access. However, you cannot selectively hide and show different files within the folder.



Can you hide just the file named GA01 Ver1.0 Service Tracking Form.pdf from the tree? I think not!

In Moodle 2.7.2 until Moodle 2.8.5, you are not able to hide from view, an individual file that is put inside a Moodle folder resource. However, I needed to to this. Desperately. Thus I sought to find the answers on Moodle.org. Alas, no one had come up with an answer. Only questions.  I threw myself into the work with gusto. After four days, I had cracked the problem. I am sharing my solution in this post. 

The Learning Outcome of this post is you will know how to hide from a user, individual files inside a Moodle folder.



My solution is best explained as a series of posts in chronological order.

From: https://moodle.org/mod/forum/discuss.php?d=261918










As above... has anybody stumbled across a plugin that provides the equivalent of the current folder functionality but allows individual items to be hidden to students?








No, sorry.
How about putting them in a different folder and hiding that from students? smile


From: https://moodle.org/mod/forum/discuss.php?d=279593






Hi there
When teachers create a folder in a course and add files to it, they don't have the ability to hide individual files. They can only hide the entire folder, along with all its contents. Is there a way to enable them to hide indivual files within the folder? Is there a setting in course administration or site administration, or maybe there is a plugin that can help? We're using Moodle 2.7.2+.
Thanks in advance!






Hi Frank
This question has been asked before in this post - albeit without any anwers still! I for one would love this feature to be in. 
I mean, in Moodle we can set a single file resource so that a user who is set with a custom role cannot see the file.
In Moodle we can also set a Folder resource so that a user who is set with a custom role cannot see the folder.
But what I want is for all users to access a Folder resource, but some files inside the folder are hidden from one group of students, whilst the other files are visible to another group of students. I don't think this is possible in Moodle, yet. Anyone?
Regards
Frankie Kam
http://moodurian.blogspot.com





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






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" 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







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?






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 







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!

If you like this post or site
a small donation would be nice but would last only a day,
otherwise leaving a comment (or a compliment) below will last me a month!

No comments:

Post a Comment

Ratings and Recommendations by outbrain