Assigning roles to users in Moodle based on their profile data using php script
In case you want to allow some particular blocks to be seen only by a particular group of users in your Moodle, you can do as below:
Note: assuming you've already created all the user accounts in your Moodle.
1. Categorize your users using some fields like department or description:
For example, I have Middle School Students and High School Students. Each of the students has Graduation Year information stored in the department field of the user profile. I will then use that field to determine which school they are studying now:
2. In Moodle, create 2 custom roles (Student archtype) named (Site administration -> Users -> Permissions -> Define roles -> Add a new role):
http://your.moodle.com/admin/roles/define.php?action=view&roleid=9
I created those two roles with the following ids:
$ cd /var/www/moodle/your_script_folder
$ php assign_school_role.php
The script has been tested with Moodle 2.9.
5. Awesome!
Note: assuming you've already created all the user accounts in your Moodle.
1. Categorize your users using some fields like department or description:
For example, I have Middle School Students and High School Students. Each of the students has Graduation Year information stored in the department field of the user profile. I will then use that field to determine which school they are studying now:
- Student Number 1's department: 2015 class => High School
- Student Number 2's department: 2021 class => Middle School
The below snippet is pretty handy to identify which school a student is studying based on her graduation year:
<?php
function class_to_grade_level($class_of) {
// you may want to change the timezone
date_default_timezone_set('Asia/Ho_Chi_Minh');
$grad_year = intval($class_of);
$current_year = intval(strftime("%Y"));
$current_month = intval(strftime("%m"));
$grade_level = 13 - ($grad_year - $current_year);
if(($current_month >=1) and ($current_month <= 6)) {
$grade_level -= 1;
}
if($grade_level >= 13){
$grade_level = "Graduated";
}
return $grade_level;
}
function class_to_school($class_of) {
$MS = array(6, 7, 8);
$HS = array(9, 10, 11, 12);
$grade_level = class_to_grade_level($class_of);
if (in_array($grade_level, $MS)) {
return 'MS';
} elseif (in_array($grade_level, $HS)) {
return 'HS';
} else {
return 'Grad';
}
}
?>
2. In Moodle, create 2 custom roles (Student archtype) named (Site administration -> Users -> Permissions -> Define roles -> Add a new role):
- Middle School
- High School
- Block
- (You may want to restrict students of each school to only see course's categories of that school)
http://your.moodle.com/admin/roles/define.php?action=view&roleid=9
I created those two roles with the following ids:
- Middle School: 10
- High School: 9
3. Go to the front page, add two blocks for two schools, and override their permissions:
=> Create
- Middle School Announcement RSS feed block (feed links of the MS announcement)
- High School Announcement RSS feed block (feed links of the HS announcement)
=> Then click "Assign role..." of each block.
=> Write down the context id of the block by checking the url:
http://your.moodle.com/admin/roles/assign.php?contextid=48&returnurl
So for two school blocks, I have:
- Middle School block's context id: 49
- High School block's context id: 48
=> Go to http\://your.moodle.com/admin/roles/permissions.php?contextid=<contextid>
=> In the "View block" section, remove every role except for the school role:
- Middle School block: Middle School role
- High School block: High School role
4. Place the following php script in a folder inside your moodle root, /var/www/moodle/your_script_folder/. Go into that folder and run it:
$ cd /var/www/moodle/your_script_folder
$ php assign_school_role.php
The script has been tested with Moodle 2.9.
5. Awesome!