I wanna thank Klaus Schramm for his solution on restricting to guest users. I have slightly modified it to fit my needs.
This basically gives you the choices to select which user and also which group to give restriction permission to. So you can select only users assigned to this project and groups also. If its empty it will be disabled.
First make sure to go in your resource table in mysql and add 2 fields, restrict_user and restrict_group. These will contain the user and group restricted.
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:
// Declare global variable to use.
global $USER;
// Admin is immune
if(!isadmin()) {
// First check if both group and user have been assigned restriction
if ($resource->restrict_group != "" && $resource->restrict_user != "") {
$groups = get_record("groups_members","userid","$USER->id");
// Check if current user is part of a group.
if ($groups) {
$group = get_record("groups","id","$groups->groupid");
// If yes check if current group is the restricted one.
if ($group->name != $resource->restrict_group) {
// If no check if the user is the restricted user.
if ($USER->username != $resource->restrict_user) {
error(get_string('restricted', 'resource').".");
}
}
// If user not part of a group. Check user.
} else {
if ($USER->username != $resource->restrict_user) {
error(get_string('restricted', 'resource').".");
}
}
} else {
// If both have not been set, then check for either one.
if ($resource->restrict_group != "") {
$groups = get_record("groups_members","userid","$USER->id");
if ($groups) {
$group = get_record("groups","id","$groups->groupid");
if ($group->name != $resource->restrict_group) {
error(get_string('restricted', 'resource').".");
}
} else {
error(get_string('restricted', 'resource').".");
}
} elseif ($resource->restrict_user != "") {
if ($USER->username != $resource->restrict_user) {
error(get_string('restricted', 'resource').".");
}
}
}
}
3. in /mod/resource/type/file/file.html
On top of the page put:
<?php
// Declare global variable to be able to use it
global $course;
global $form;
// Fetch records of current resource id.
$resource = get_record("resource", "id", "$form->instance");
?>
then proceed with: behind the line "<tr><td colspan="2"><hr /></td></tr>" insert the following lines:
<tr>
<td> </td>
<td>
<table cellpadding="0" cellspacing="0">
<tr>
<td>
<?php
// Get all users listed in the current project id.
$courseusers = get_course_users($course->id, '', '', 'u.username, u.firstname, u.lastname');
?>
<input type="checkbox" name="ruser" value="1" onclick="enableField('ruser');" <?php if ($resource->restrict_user != "") { echo "checked"; } ?><?php if (!$courseusers) { echo "disabled"; } ?>><?php print_string("restrict_user", "resource") ?>
</td>
<td style="padding-left: 5mm;">
<select name="restrict_user" <?php if ($resource->restrict_user == "") { echo "disabled"; } ?>>
<?php
foreach ($courseusers as $courseuser) {
// Display user's first and last name in drop down list.
?>
<option value="<?php echo $courseuser->username; ?>" <?php if ($courseuser->username == $resource->restrict_user) { echo "selected"; } ?>><?php echo $courseuser->firstname . " " . $courseuser->lastname; ?></option>
<?php
}
?>
</select>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td> </td>
<td>
<table cellpadding="0" cellspacing="0">
<tr>
<td>
<?php
// Get all groups listed for this project id.
$cgroups = get_groups($course->id);
?>
<input type="checkbox" name="rgroup" value="1" onclick="enableField('rgroup');" <?php if ($resource->restrict_group != "") { echo "checked"; } ?><?php if (!$cgroups) { echo "disabled"; } ?>><?php print_string("restrict_group", "resource") ?>
</td>
<td style="padding-left: 5mm;">
<select name="restrict_group" <?php if ($resource->restrict_group == "") { echo "disabled"; } ?>>
<?php
foreach ($cgroups as $cgroup) {
?>
<option value="<?php echo $cgroup->name; ?>"<?php if ($cgroup->name == $resource->restrict_group) { echo "selected"; } ?>><?php echo $cgroup->name; ?></option>
<?php
}
?>
</select>
</td>
</tr>
</table>
</td>
</tr>
4. in /mod/lang/en/resource.php insert the lines (alphabetically):
$string['restricted'] = 'Sorry, you do not have access to this resource.';
$string['restrict_group'] = 'Restrict access by group';
$string['restrict_user'] = 'Restrict access by user';
5. in /mod/resource/lib.php within the function function update_instance($resource) behind the line "$resource->timemodified = time();" insert the following lines:
if (isset($resource->ruser)) {
$resource->restrict_user = $resource->restrict_user;
} else {
$resource->restrict_user = "";
}
if (isset($resource->rgroup)) {
$resource->restrict_group = $resource->restrict_group;
} else {
$resource->restrict_group = "";
}