unenrolling participants during course reset

unenrolling participants during course reset

by Shamim Rezaie -
Number of replies: 0
copied from http://moodle.org/mod/forum/discuss.php?d=79667

Hi all,

In the file moodlelib.php there is a function name reset_all_course_userdata().

In this function, the following lines exists to remove (unenroll) course users.

$teachers = array_keys(get_users_by_capability($coursecontext, 'moodle/course:update'));
$participants = array_keys(get_users_by_capability($coursecontext, 'moodle/course:view'));
$students = array_diff($participants, $teachers);

if (!empty($data->reset_students)) {
foreach ($students as $studentid) {
role_unassign(0, $studentid, 0, $coursecontext->id);
}
...
}

I believe that current code is drastically inefficient and slow. It removes users one by one by calling the function role_unassign() and passing the studentid and countextid to it. In my opinion it would be better to pass roleid rather than studentid to this function. then we could call the role_unassign() function for each role rather than each participant.

I may rewrite the code like this:

$teacherroles = array_keys(get_roles_with_capability('moodle/course:update', CAP_ALLOW, $coursecontext));
$participantroles = array_keys(get_roles_with_capability('moodle/course:view', CAP_ALLOW, $coursecontext));
$studentroles = array_diff($participantroles, $studentroles);

if (!empty($data->reset_students)){
foreach ($studentroles as $studentroleid){
role_unassign($studentroleid, 0, 0, $coursecontext->id);
}
...
}

Is there any idea, assent or dissent.

Average of ratings: -