From: thepurpleblob Date: Fri, 23 Nov 2007 15:49:00 +0000 (+0000) Subject: MDL-12296: X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=220f149cfc26d3012948c9f7dc2935bc45e9ffdd;p=moodle.git MDL-12296: Unenrol disable option now works in synchronisation script. Merged from STABLE_19 --- diff --git a/enrol/database/enrol.php b/enrol/database/enrol.php index 761e5b285b..3957a3386f 100644 --- a/enrol/database/enrol.php +++ b/enrol/database/enrol.php @@ -295,22 +295,24 @@ function sync_enrolments($role = null) { // When the user logs in though, their role list will be updated // correctly. // - $to_prune = get_records_sql(" - SELECT ra.* - FROM {$CFG->prefix}role_assignments ra - JOIN {$CFG->prefix}user u ON ra.userid = u.id - WHERE ra.enrol = 'database' - AND ra.contextid = {$context->id} - AND ra.roleid = ". $role->id . ($extenrolments - ? " AND u.{$CFG->enrol_localuserfield} NOT IN (".join(", ", array_map(array(&$db, 'quote'), $extenrolments)).")" - : '')); - - if ($to_prune) { - foreach ($to_prune as $role_assignment) { - if (role_unassign($role->id, $role_assignment->userid, 0, $role_assignment->contextid)){ - error_log( "Unassigned {$role->shortname} assignment #{$role_assignment->id} for course {$course->id} (" . format_string($course->shortname) . "); user {$role_assignment->userid}"); - } else { - error_log( "Failed to unassign {$role->shortname} assignment #{$role_assignment->id} for course {$course->id} (" . format_string($course->shortname) . "); user {$role_assignment->userid}"); + if (!$CFG->enrol_db_disableunenrol) { + $to_prune = get_records_sql(" + SELECT ra.* + FROM {$CFG->prefix}role_assignments ra + JOIN {$CFG->prefix}user u ON ra.userid = u.id + WHERE ra.enrol = 'database' + AND ra.contextid = {$context->id} + AND ra.roleid = ". $role->id . ($extenrolments + ? " AND u.{$CFG->enrol_localuserfield} NOT IN (".join(", ", array_map(array(&$db, 'quote'), $extenrolments)).")" + : '')); + + if ($to_prune) { + foreach ($to_prune as $role_assignment) { + if (role_unassign($role->id, $role_assignment->userid, 0, $role_assignment->contextid)){ + error_log( "Unassigned {$role->shortname} assignment #{$role_assignment->id} for course {$course->id} (" . format_string($course->shortname) . "); user {$role_assignment->userid}"); + } else { + error_log( "Failed to unassign {$role->shortname} assignment #{$role_assignment->id} for course {$course->id} (" . format_string($course->shortname) . "); user {$role_assignment->userid}"); + } } } } @@ -370,36 +372,39 @@ function sync_enrolments($role = null) { // When the user logs in though, their role list will be updated // correctly. // - $sql = " - SELECT ra.roleid, ra.userid, ra.contextid - FROM {$CFG->prefix}role_assignments ra - LEFT OUTER JOIN ({$CFG->prefix}context cn - JOIN {$CFG->prefix}course c ON cn.contextlevel = ".CONTEXT_COURSE." AND cn.instanceid = c.id) - ON ra.contextid = cn.id - WHERE ra.enrol = 'database'" . - ($have_role ? ' AND ra.roleid = '.$role->id : '') . - ($extcourses - ? " AND (c.id IS NULL OR c.{$CFG->enrol_localcoursefield} NOT IN (" . join(",", array_map(array(&$db, 'quote'), $extcourses)) . "))" - : ''); - - $ers = $db->Execute($sql); - if (!$ers) { - trigger_error($db->ErrorMsg() .' STATEMENT: '. $sql); - return false; - } - if ( !$ers->EOF ) { - while ($user_obj = rs_fetch_next_record($ers)) { - $roleid = $user_obj->roleid; - $user = $user_obj->userid; - $contextid = $user_obj->contextid; - if (role_unassign($roleid, $user, 0, $contextid)){ - error_log( "Unassigned role {$roleid} from user $user in context $contextid"); - } else { - error_log( "Failed unassign role {$roleid} from user $user in context $contextid"); + if (!$CFG->enrol_db_disableunenrol) { + $sql = " + SELECT ra.roleid, ra.userid, ra.contextid + FROM {$CFG->prefix}role_assignments ra + LEFT OUTER JOIN ({$CFG->prefix}context cn + JOIN {$CFG->prefix}course c ON cn.contextlevel = ".CONTEXT_COURSE." AND cn.instanceid = c.id) + ON ra.contextid = cn.id + WHERE ra.enrol = 'database'" . + ($have_role ? ' AND ra.roleid = '.$role->id : '') . + ($extcourses + ? " AND (c.id IS NULL OR c.{$CFG->enrol_localcoursefield} NOT IN (" . join(",", array_map(array(&$db, 'quote'), $extcourses)) . "))" + : ''); + + $ers = $db->Execute($sql); + if (!$ers) { + trigger_error($db->ErrorMsg() .' STATEMENT: '. $sql); + return false; + } + if ( !$ers->EOF ) { + while ($user_obj = rs_fetch_next_record($ers)) { + $roleid = $user_obj->roleid; + $user = $user_obj->userid; + $contextid = $user_obj->contextid; + if (role_unassign($roleid, $user, 0, $contextid)){ + error_log( "Unassigned role {$roleid} from user $user in context $contextid"); + } else { + error_log( "Failed unassign role {$roleid} from user $user in context $contextid"); + } } + rs_close($ers); // release the handle } - rs_close($ers); // release the handle } + commit_sql(); // we are done now, a bit of housekeeping @@ -618,14 +623,6 @@ function create_course ($course,$skip_fix_course_sortorder=0){ return $newcourseid; } -/** - * Test the database connection - * @return true if it works - */ -function test() { - return true; -} - /// DB Connect /// NOTE: You MUST remember to disconnect /// when you stop using it -- as this call will