Roles and permissions

Patch for the 1.7 role-bug for course creators

 
 
Picture of Agostino Di Ciaccio
Patch for the 1.7 role-bug for course creators
 

The bug is the following (yet considered in other posts):

- If you attribute the course creation capability at the site level to someone, he will appear in every course.

This happen also if you set the corresponding profile just to make only "create a course" capability. In this way it is impossible to let teachers to create and teach in their courses: if a teacher can create courses at the site or category level, then he results enrolled in all courses of the site or of the category.

The patch requires to create a new role which inherit all except Create courses (moodle/course:create) in the "Course" subcategory.

Look in the data-base table "mdl_role" you can see the real name assigned to each role. In italian we have "Creatori di corsi" not "Course creators". Anyway the best thing to do is to look at the number of the role we want to skip in the list, in my case I want to skip the Course creators (usually id=2) and the new role I created (id=7) which has only the "create a course" capability. The code is now:

foreach ($teachers as $teacher) {

  If ($teacher->id != 7 && $teacher->id != 2){

    if (!$teacher->hidden || $canseehidden) {

     $roles = get_user_roles($context, $teacher->id, true, 'r.sortorder ASC');

     $role = array_shift($roles); // First one

     $fullname = fullname($teacher, has_capability('moodle/site:viewfullnames', $context));

     $namesarray[] = format_string($role->name).': <a href="'.$CFG->wwwroot.'/user/view.php?id='.

     $teacher->id.'&amp;course='.SITEID.'">'.$fullname.'</a>';

     }

   }

}

Now open the file: /user/index.php

go to the following instructions

if ($roles = get_roles_used_in_context($context)) {

// We should exclude "admin" users (those with "doanything" at site level) because

// Otherwise they appear in every participant list

     $sitecontext = get_context_instance(CONTEXT_SYSTEM);

     $doanythingroles = get_roles_with_capability('moodle/site:doanything', CAP_ALLOW, $sitecontext);

     foreach ($roles as $role) {

       if (isset($doanythingroles[$role->id]) ) { // Avoid this role (ie admin)

       unset($roles[$role->id]);

       continue;

     }

     $rolenames[$role->id] = strip_tags(format_string($role->name)); // Used in menus etc later on

   }

}

now change

if (isset($doanythingroles[$role->id]) ) { // Avoid this role (ie admin)

in the following

if (isset($doanythingroles[$role->id]) || $role->id==2 || $role->id==7) { // Avoid this role (ie admin)

 

now go to the following instructions:

if ($doanythingroles) {

   $adminroles = 'AND r.roleid NOT IN (';

   foreach ($doanythingroles as $aroleid=>$role) {

         $adminroles .= "$aroleid,";

   }

   $adminroles = rtrim($adminroles,",");

   $adminroles .= ')';

} else {

   $adminroles = '';

}

to avoid to view roleid 2 and 7, change:

$adminroles .= ')';

in

$adminroles .= ',2,7)';

At this point, assign the teacher only at the course level, then at the site level, for each teacher, assign also the new role created.

 

 
Average of ratings: -
Picture of Russell Myers
Re: Patch for the 1.7 role-bug for course creators
 
Thanks Agostino!! Really appreciate the help! Works perfectly. I had a couple of questions and comments.

1. Do we need to create a new role and put all course creators in it? Why? Will I run into course creation issues if all my course creators are put into that role? I assume that I won't, but there will be other things they can't do then.

Just a couple of quick comments:

The first file to change is moodle/course/lib.php
If ($teacher->id != 7 && $teacher->id != 2){
is on line 1559 or close to it

if (isset($doanythingroles[$role->id]) ) { // Avoid this role (ie admin)
is on line 267 or close to it.

$adminroles .= ')';
is on line 353 or close to it.

I also did not move all my course creators into the new created role, and it seems to work well. Thanks again for your help! Great fix!!!

Rusty
 
Average of ratings: -
Picture of Thomas Robb
Re: Patch for the 1.7 role-bug for course creators
Group Developers

Thank you for this. You say, however, "The code is now:" but you don't say which file you are talking about. Which file is this code to be found in?

 
Average of ratings: -
Picture of Agostino Di Ciaccio
Re: Patch for the 1.7 role-bug for course creators
 

See the explanation in the comment:

http://moodle.org/mod/forum/discuss.php?d=63569

 
Average of ratings: -
Picture of Carole ROUMEC
Re: Patch for the 1.7 role-bug for course creators
 
Hello,
I have done the modification but there may be a thing that i haven't understand (i am french) because it doesn't work for me.
Where do you asign the new role ? i want the teacher to creta course in their categories but i don't want them to be in the list of all course created in this categorie.

Thanks,
Carole
 
Average of ratings: -
Picture of Tom Regy
Re: Patch for the 1.7 role-bug for course creators
 
Hi all,

I stumbled upon a similar problem at our testing site. We needed some sort of 'supervisor'-roles on site-context, who should have the capability to visit all courses without being seen by the participants.

After getting the internale RoleIDs for these roles (2 and 12) I added the following code to <moodle>/user/index.php around line 265 after the line

$doanythingroles = get_roles_with_capability('moodle/site:doanything', CAP_ALLOW, $sitecontext);

// insert me
$invroles = array('2','12');
foreach ( $invroles as $invroleid ) {
$invrole = get_record('role','id',$invroleid);
$doanythingroles[$invrole->id] = $invrole;
}

As a result, users who have one of these supervisor-roles are not shown as participants and the drops-down menu doesnt list these roles anymore.
This is a global behaviour, so these roles wont get shown in any course on the system.

Might be helpful for somebody, greets from germany

Tom


 
Average of ratings: -
Picture of Tom Regy
Re: Patch for the 1.7 role-bug for course creators
 
Ok,

the roles still showed up in the extended 'my cour
ses' view - now I finally understood the code of Agostino smile But in my opinion, in his first statement in file <moodle>/course/lib.php

If ($teacher->id != 7 && $teacher->id != 2){

he compares userIDs with roleIDs, which didn't work for me.

So I'd suggest the following code which works for me:

File
<moodle>/course/lib.php, around line 1560

foreach ($teachers as $teacher) {

// insert me
// check if teacher is in invisible roles and hide
$hide = false;
foreach ($CFG->invisible_roles as $roleid) {
if (user_has_role_assignment($teacher->id, $roleid, $context->id)) {
$hide = true;
}
}
if ( $hide ) continue;

if (!$teacher->hidden || $canseehidden ) {


If a teacher is assigned to an invisible role in the current course context he is skipped and
not printed on the my courses overview

The array
$CFG->invisible_roles is defined in <moodle>/config.php as the array holding the
invisible roleIDs. The code in my post above for file
<moodle>/user/index.php can therefore be changed accordingly.

Greets Tom




 
Average of ratings: -