Restricting resources to groups or individual users.

Restricting resources to groups or individual users.

by Ken Shino -
Number of replies: 3

Hi, I was thinking of a way to restrict a resource to an individual only or a group of users.

Lets say I upload a PDF document. Id have the option to choose if I desire to affect a group or user and then be able to select either the user or the group affected (also project of course and so on).

So if a user thats been affected by this resource can view it when logged in and in the course but if a group or another user logs in the course he will not see the resource.

I also thought of that possibility with Activities, since there we're the option to add an activity for a user, but unfortunately it only adds to the current logged user and doesn't allow you to select which one. (Admin).

I wonder if all of this is possible? If yes I would appreciate some help. I have looked at the previous posts and found some interesting code tips that could guide me in doing that. If a solution is out there already I would appreciate a link to the discussion.

Thanks.

Average of ratings: -
In reply to Ken Shino

Re: Restricting resources to groups or individual users.

by ss ss -
It's a great idea. I'm looking forward this solution.
In reply to ss ss

Re: Restricting resources to groups or individual users.

by Ken Shino -

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>&nbsp;</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>&nbsp;</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 = "";
    }

In reply to Ken Shino

Re: Restricting resources to groups or individual users.

by Zach Erbaugh -
Looks very promising, and exactly what I was looking for (I love Moodle)! When I do the hacks, I get the appropriate boxes on the Editing Resource page, but checking the boxes doesn't enable the appropriate drop-down menu, and the checked settings aren't saved with the resource. 

Could this have anything to do with the 'type' I chose for the restrict_user and restrict_group fields I added to the database?  No type was mentioned, so I tried VARCHAR.