How to assign calendar:manageownentries capability to authenticated user which is a teacher in a course

58 Views Asked by At

In moodle 4.1, due to some security reasons, i have to remove the calendar:manageownentries capability from the authenticated user. I have to assign this capability to a authenticated user which is teacher in any course. This capability is to show the 'add event' button on calendar on the dashboard page.

I remove the capability from the authenticated user role and assign this capability to the teacher role on the course level not on the system level. I know the teacher is also an authenticated user. So if I remove the capability from an authenticated user, it will not work for the teacher(course level) either.

So please let me know how can i show the 'add event' button on the calendar of teachers (course level).

EDIT

Now the requirement has been changed. We have a theme installed in our application and we have to hide the button through the theme. We can make the code changes in the theme code.

I checked and I found out that we only can override the renderer and templates, not general functions. So I am looking for a fix that we can apply through the theme.

Thanks

1

There are 1 best solutions below

1
Veterini On

First thing first : Do not edit code in calendar/lib.php. This is part of the core code and under no circumstances should you touch it. What you could do is write a plugin or a subplugin.

Programmaticaly you can check if someone is teacher anywhere on the site like this : (not my code, but hell knows I use it more than often^^) <?php

function isTeacherAnywhere() : bool {
        // function to check if user is a teacher on any course, which ever it is across the site
    global $DB, $USER;
    //we retrieve the id of the editingteacher role (we could hardcode this but it's prone to change)
    $roleid = $DB->get_field('role', 'id', ['shortname' => 'editingteacher']);
    //we check on mdl_role_assignments if there exist at least one couple whith the user id and the editingteacher's role id
    $isteacheranywhere = $DB->record_exists('role_assignments', ['userid' => $USER->id, 'roleid' => $roleid]);
    if(isloggedin() && $isteacheranywhere): //well, not sur the isloggedin was necessary but better be too cautious^^
        return true;
    else:
        return false;
    endif;
}

If it returns true you can give the role to the teachers (it checks for the connected user. You can adapt the function to target all users but bewarn that it may be one hell of a query depending of the number of users you have, (we have several thousands user, such a request could end with a timeout).

Within the site you can also go to the administration > users > role. What you could do would be add the authorisations of the role directly to the editing teacher role, (not great). Another way which would be cleaner (even if a bit tedious), would be to manually give the roles. This is the best way to avoid conflicts / programming errors and every queries. But it'll depend on your organization.