Restrict certain resources to students only
Number of replies: 8One of our professors have asked me how he could protect only a single resource and make it only accessible for students. I didn't have an answer ready, that's why I came here.
Here is the scenario:
He has his course open to guest access. He doesn't mind that. His only concern is that when he posts solutions to the assignments, he would like only his students to access it.
He wants to prevent future students from having solutions so easily available.
Is this possible or should it be considered as a potential new feature?
Has anyone done this before?
Thanks for the help.
Re: Restrict certain resources to students only
Since this means there is a setting, one could locate it in quiz and perhaps make a type of resource that also doesn't allow guest entry.
Re: Restrict certain resources to students only
Here is the solution:
1. in /mod/resource/type/file/resource.class.php in the function display() before the comment "Form the parse string" include the following new lines:
if ($resource->blockguests == 1) {
require_login($course->id);
if (isguest($USER->id)) {
error(get_string('noguestaccess', 'resource').".");
}
}
2. with phpMyAdmin or some other tool, extend the moodle data structure in mdl_resource with a new field:
name: blockguests
type: tinyint(4)
Default: 0
3. in /mod/resource/type/file/file.html behind the line "<tr><td colspan="2"><hr /></td></tr>" insert the following lines:
<tr>
<td> </td>
<td>
<input type="checkbox" name="blockguests" value=1 <?php echo ($form->blockguests == "1") ? "checked" : "" ?> >
<?php print_string("blockguests", "resource") ?>
</td>
</tr>
4. in /mod/lang/en/resource.php insert the lines (alphabetically):
$string['blockguests'] = 'Block access by guests';5. in /mod/resource/lib.php within the function function update_instance($resource) behind the line "$resource->timemodified = time();" insert the following lines:
$string['noguestaccess'] = 'Sorry, no guest access to this resource';
if (isset($resource->blockguests)) {6. Testing:
$resource->blockguests = 1;
} else {
$resource->blockguests = 0;
}
When adding a resource, you should now see the additional option "Block access by guests". By selecting this option and saving the changes and then editing the resource again, you should see your selection (i.e.: the setting has been saved / recalled successfully). Now, browsing the course as guest, you should have access to all resources but the one marked as "block access by guests".
Good luck,
Klaus
Re: Restrict certain resources to students only
Thank you for all your help. I had already solved my own problem, though.

I posted the solution in another forum, but i'll put it here as well.
I created a "Solution" module for one of the professors at the university. He wanted to post solutions and not allow guests to view them.
So here you go, for anyone that wants to use it, feel free. It will prevent guests from looking at content, even though the course is open to guests. No need for modifying base code. just installs as a new module.
Thanks,
Eduardo
Re: Restrict certain resources to students only
Re: Restrict certain resources to students only
Thanks for posting the module. It is a great example of how to develop the core of moodle additions with very little code.
Regards,
Klaus
Re: Restrict certain resources to students only
I followed the instructions on how to create a new module given on one of the tutorials.
I believe the Solution module belongs on the Resource category instead of activities, but unfortunately, I didn't find any documentation on how to create a resource.
I will research a little more on it and maybe I'll be able to transfer "Solutions" to the Resource side.
Until then, I have posted the latest version of the Solution Module.
(I think I'll put it on a website later. I hate this re-posting of modules, you never know which one is the most recent =) )
Enjoy,
Eduardo
Re: Restrict certain resources to students only
Eudardo, Klaus, and all who've posted on this Solution module.
It was working well (version 2004121505) for me in Moodle v.1.4.3 as I used it to include copyrighted materials (permission granted from publisher) in my nonf2f course, however, our sysadmin moved everything to a new server with a concurrent upgrade to v.1.5.1 and now I get
"A required parameter (sesskey) was missing"
??
I really need this to stay compliant with my use permission (informal licence).
Suggestions? I'm not the systems administrator (who doesn't speak English) so please make this straightforward for me. Thanks.
Re: Restrict certain resources to students only
That was a excellent piece of information. It worked well.
But I have another topic which needs your help.
Hierarchially,
1. Teacher have full control over the course forwhich he is assigned.
2. Inside the course we have several topics like topic 0, topic 1...
- Is it possible to give acess to different teachers for perticular topics?
- And the teacher assigning authority for perticular topics lies with admin and the course level teacher.
- If assumed that it is done, the teacher who is assigned to a perticular topic should not be able to modify any other topics but his own.
Please help me with the php coding and the path to include it.
Thanks in advance.