From cb94adc252b6e36591965bde40e05749414326fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20S=CC=8Ckoda?= Date: Sat, 11 Aug 2012 20:05:56 +0200 Subject: [PATCH] MDL-34776 improve multiple "NOT IN" enrolment and role related queries --- admin/roles/lib.php | 10 ++++------ enrol/locallib.php | 14 +++++--------- enrol/manual/locallib.php | 8 +++----- user/selector/lib.php | 5 ++--- 4 files changed, 14 insertions(+), 23 deletions(-) diff --git a/admin/roles/lib.php b/admin/roles/lib.php index 11341d10cfe..43eb7f530b8 100644 --- a/admin/roles/lib.php +++ b/admin/roles/lib.php @@ -1039,12 +1039,10 @@ class potential_assignees_below_course extends role_assign_user_selector_base { $countfields = 'SELECT COUNT(u.id)'; $sql = " FROM {user} u - WHERE u.id IN ($enrolsql) $wherecondition - AND u.id NOT IN ( - SELECT r.userid - FROM {role_assignments} r - WHERE r.contextid = :contextid - AND r.roleid = :roleid)"; + LEFT JOIN {role_assignments} ra ON (ra.userid = u.id AND ra.roleid = :roleid AND ra.contextid = :contextid) + WHERE u.id IN ($enrolsql) + $wherecondition + AND ra.id IS NULL"; $order = ' ORDER BY lastname ASC, firstname ASC'; $params['contextid'] = $this->context->id; diff --git a/enrol/locallib.php b/enrol/locallib.php index 30c9db8d1d7..b19a716b2ba 100644 --- a/enrol/locallib.php +++ b/enrol/locallib.php @@ -278,7 +278,7 @@ class course_enrolment_manager { global $DB, $CFG; // Add some additional sensible conditions - $tests = array("id <> :guestid", 'u.deleted = 0', 'u.confirmed = 1'); + $tests = array("u.id <> :guestid", 'u.deleted = 0', 'u.confirmed = 1'); $params = array('guestid' => $CFG->siteguest); if (!empty($search)) { $conditions = get_extra_user_fields($this->get_context()); @@ -306,10 +306,9 @@ class course_enrolment_manager { $fields = 'SELECT '.$ufields; $countfields = 'SELECT COUNT(1)'; $sql = " FROM {user} u + LEFT JOIN {user_enrolments} ue ON (ue.userid = u.id AND ue.enrolid = :enrolid) WHERE $wherecondition - AND u.id NOT IN (SELECT ue.userid - FROM {user_enrolments} ue - JOIN {enrol} e ON (e.id = ue.enrolid AND e.id = :enrolid))"; + AND ue.id IS NULL"; $order = ' ORDER BY u.lastname ASC, u.firstname ASC'; $params['enrolid'] = $enrolid; $totalusers = $DB->count_records_sql($countfields . $sql, $params); @@ -353,12 +352,9 @@ class course_enrolment_manager { $fields = 'SELECT '.user_picture::fields('u', array('username','lastaccess')); $countfields = 'SELECT COUNT(u.id)'; $sql = " FROM {user} u + LEFT JOIN {role_assignments} ra ON (ra.userid = u.id AND ra.contextid = :contextid) WHERE $wherecondition - AND u.id NOT IN ( - SELECT u.id - FROM {role_assignments} r, {user} u - WHERE r.contextid = :contextid AND - u.id = r.userid)"; + AND ra.id IS NULL"; $order = ' ORDER BY lastname ASC, firstname ASC'; $params['contextid'] = $this->context->id; diff --git a/enrol/manual/locallib.php b/enrol/manual/locallib.php index 32558827159..1d09681ad12 100644 --- a/enrol/manual/locallib.php +++ b/enrol/manual/locallib.php @@ -55,11 +55,9 @@ class enrol_manual_potential_participant extends user_selector_base { $countfields = 'SELECT COUNT(1)'; $sql = " FROM {user} u - WHERE $wherecondition AND - u.id NOT IN ( - SELECT ue.userid - FROM {user_enrolments} ue - JOIN {enrol} e ON (e.id = ue.enrolid AND e.id = :enrolid))"; + LEFT JOIN {user_enrolments} ue ON (ue.userid = u.id AND ue.enrolid = :enrolid) + WHERE $wherecondition + AND ue.id IS NULL"; $order = ' ORDER BY u.lastname ASC, u.firstname ASC'; if (!$this->is_validating()) { diff --git a/user/selector/lib.php b/user/selector/lib.php index 9aa40edf03f..daecbf1e1a0 100644 --- a/user/selector/lib.php +++ b/user/selector/lib.php @@ -832,10 +832,9 @@ class group_non_members_selector extends groups_user_selector_base { JOIN ($enrolsql) e ON e.id = u.id LEFT JOIN {role_assignments} ra ON (ra.userid = u.id AND ra.contextid " . get_related_contexts_string($context) . " AND ra.roleid $roleids) LEFT JOIN {role} r ON r.id = ra.roleid + LEFT JOIN {groups_members} gm ON (gm.userid = u.id AND gm.groupid = :groupid) WHERE u.deleted = 0 - AND u.id NOT IN (SELECT userid - FROM {groups_members} - WHERE groupid = :groupid) + AND gm.id IS NULL AND $searchcondition"; $orderby = "ORDER BY u.lastname, u.firstname";