Creating a matrix : user access per module

Creating a matrix : user access per module

by aggelos panagiotakis -
Number of replies: 3
I want to create a matrix for teachers per module :


User Name/Surname

Course ID 1

Course ID 2 ... X

My name

T(eacher)

A(thor)


Teacher and Author are the two roles that i am concerned about.
I want to implement it in 1.8.3+
is this an easy task to do ?
any general guidlines on how to do it on a separate moodle page?

Average of ratings: -
In reply to aggelos panagiotakis

Απάντηση: Creating a matrix : user access per module

by aggelos panagiotakis -
so here is my code ....

but i still have a problem how can i check if in a module some user has the role assigned to him named "Author" ?


<?php

require_once('../config.php');
require_once($CFG->libdir.'/adminlib.php');

print_header("User Roles ", "User Roles ",
"<a href=\"index.php\" />",
"", "", true, "&nbsp;", navmenu($course));
print_heading("Modules Roles");

$users = get_users(true);
$courses = get_courses(all);
$counter=-1;

echo "<table border='1'>";
echo "<tr>";
//echo "<td align='center'>ID</td>";
echo "<td align='center'>Name</td>";
foreach($courses as $acourse){
//echo substr($acourse->idnumber,0,4);
if (substr($acourse->idnumber,0,4)=="WREM") {
echo "<td align='center'><b><a href='" . $CFG->wwwroot . "/course/view.php?id=$acourse->id'>$acourse->idnumber</a></b></td>"; ///$acourse->id<br />
}
}
echo "</tr>";

foreach($users as $auser){
if (substr_count($auser->lastname . " " . $auser->firstname , "Student")==0){
if (substr_count($auser->lastname . " " . $auser->firstname , "Teacher")==0){
if (substr_count($auser->lastname . " " . $auser->firstname , "Administrator Official")==0){
echo "<tr>";
if ($counter==-1){
//echo "<td align='center'>$auser->id</td>";
echo "<td align='left' style='nowrap' width='100%'><a href='". $CFG->wwwroot . "/user/view.php?id=$auser->id'>$auser->lastname $auser->firstname</a></td>";
}
foreach($courses as $acourse) {
if (substr($acourse->idnumber,0,4)=="WREM") {
echo "<td align='center'>";
//echo "$auser->id</td>";
//print_r($acourse);
//isadmin(), isteacher(), iscreator() ή isstudent().
if (!empty($auser->roles['teacher']))
{
echo "<red>T</red>";//$is_teacher;
}
//$is_student = !empty($user->roles['student']);

if (isadmin($acourse->id)){
echo "Admin";
}
elseif (isteacher($acourse->id,$auser->id,false)){
echo "<font color='red'><b>T</b></font>";
}
elseif (isauthor($acourse->id,$auser->id,false)){
echo "A";
}
elseif (isstudent($acourse->id,$auser->id,false)){
echo "<font color='blue'>S</font>";
}
else {
echo "&nbsp;";
}
echo "</td>";
}
}
echo "</tr>";

}
}
}
}
echo "</table>";

echo "<font color='blue'>S: Student</font><br />";
echo "<font color='red'>T: Teacher</font><br />";

print_footer();




function isauthor($courseid=0, $userid=0, $obsolete_includeadmin=true) {
/// Is the user able to access this course as a teacher?
global $CFG;

if (empty($CFG->rolesactive)) { // Teachers are locked out during an upgrade to 1.7
return false;
}

if ($courseid) {
$context = get_context_instance(CONTEXT_COURSE, $courseid);
} else {
$context = get_context_instance(CONTEXT_SYSTEM, SITEID);
}

//return (has_capability('moodle/legacy:author', $context, $userid, false)
//or has_capability('moodle/legacy:editingteacher', $context, $userid, false)
//or has_capability('moodle/legacy:admin', $context, $userid, false));
return (has_capability('moodle/legacy:author', $context, $userid, false));
}


?>
In reply to aggelos panagiotakis

Απάντηση: Creating a matrix : user access per module

by aggelos panagiotakis -
Ok everyone i did it!


<?php
require_once('../config.php');
require_once($CFG->libdir.'/adminlib.php');

print_header("User Roles ", "User Roles ",
"<a href=\"index.php\" />",
"", "", true, "&nbsp;", navmenu($course));
print_heading("Modules Roles");
$users = get_users(true);
$courses = get_courses(all);
$counter=-1;
echo "<table border='1'>";
echo "<tr>";
//echo "<td align='center'>ID</td>";
echo "<td align='center'>Name</td>";
foreach($courses as $acourse){
//echo substr($acourse->idnumber,0,4);
if (substr($acourse->idnumber,0,4)=="WREM") {
echo "<td align='center'><b><a href='" . $CFG->wwwroot . "/course/view.php?id=$acourse->id'>$acourse->idnumber</a></b></td>"; ///$acourse->id<br />
}
}
echo "</tr>";

foreach($users as $auser){
if (substr_count($auser->lastname . " " . $auser->firstname , "Student")==0){
if (substr_count($auser->lastname . " " . $auser->firstname , "Teacher")==0){
if (substr_count($auser->lastname . " " . $auser->firstname , "Administrator Official")==0){
echo "<tr>";
if ($counter==-1){
//echo "<td align='center'>$auser->id</td>";
echo "<td align='left' style='nowrap' width='100%'><a href='". $CFG->wwwroot . "/user/view.php?id=$auser->id'>$auser->lastname $auser->firstname</a></td>";
}
foreach($courses as $acourse) {
if (substr($acourse->idnumber,0,4)=="WREM") {
echo "<td align='center'>";

//get_user_roles($coursecontext, $user->id)) ) { // This user has roles
$coursecontext = get_context_instance(CONTEXT_COURSE, $acourse->id); // Course context

if ($a = get_user_roles($coursecontext,$auser->id)) {
//print_r($a->stdClass);
$rolestring = get_user_roles_in_context($auser->id, $coursecontext->id);
echo $rolestring ;

}
echo "</td>";
}
}
echo "</tr>";
}
}
}
}
echo "</table><br />";

echo "<font color='blue'>S: Student</font><br />";
echo "<font color='red'>T: Teacher</font><br />";

print_footer();
?>
Attachment roles_matrix.jpg
In reply to aggelos panagiotakis

Re: Απάντηση: Creating a matrix : user access per module

by Kelvin Foo -

Hi, im currently doing this. i need the solution, can you help me?