天天看點

Moodle:通過使用者ID(user id) 擷取使用者權限

執行個體:

<?php
require_once("../config.php"); // under root 
if(!is_siteadmin())
{
	global $COURSE, $USER;
	
	$queryrole = $DB->get_records('role_assignments',array('userid'=>$USER->id));
	
	//echo '<pre>'; print_r($queryrole); echo '</pre>';
	
	//$role_ids = array();
	//foreach($queryrole as $qrole){ $role_ids[] = $qrole->roleid; }
	
	//echo '<pre>'; print_r(implode(',', $role_ids)); echo '</pre>'; die;
	
	//$role_id = max($role_ids);
	
	//if(!is_siteadmin()) exit('<script>location="/moodle/my/"</script>');
	
	//echo '<pre>'; print_r($role_id); echo '</pre>';
	
	//echo 'My Role ID is :'.$role_id; die;
	
	$students_and_teachers = array(
								'3', //Teacher
								'4', //Non-editing teacher
								'5', //Student
								'6', //Guest
								'7', //Authenticated user
								'8', //Authenticated user on frontpage
								'9', //Online Student
								'10', //onsite student
								'15', //ManagerAssistant 
								'16', //TA
							);
							
	//if(!$role_id OR in_array($role_id, $students_and_teachers))  exit('<script>location="/moodle/my/"</script>');
	
	$except = FALSE;
	foreach($queryrole as $qrole) if(!in_array($qrole->roleid, $students_and_teachers)) { $except = TRUE; break; } 
	if(!$except) exit('<script>location="/moodle/my/"</script>');
}