From 7366a6f511151815d0654a84bf6dca699b0a5236 Mon Sep 17 00:00:00 2001 From: PMKuipers Date: Tue, 31 Dec 2024 14:02:42 +0100 Subject: [PATCH] Optimized looping queries --- classes/associationservice.php | 70 +++++++++++++++++----------------- 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/classes/associationservice.php b/classes/associationservice.php index 8c11427..e1c3e07 100644 --- a/classes/associationservice.php +++ b/classes/associationservice.php @@ -477,20 +477,21 @@ class associationservice extends \external_api { webservicehelper::require_capabilities(self::CAP_VIEW, $studyplan->context()); } - $sql = "SELECT DISTINCT u.* FROM {user} u INNER JOIN {local_treestudyplan_user} j ON j.user_id = u.id + $sql = "SELECT DISTINCT u.*, t.lastaccess as lastaccess + FROM {user} u INNER JOIN {local_treestudyplan_user} j ON j.user_id = u.id + LEFT JOIN (SELECT MAX(a.timeaccess) as lastaccess, a.userid as userid FROM {user_lastaccess} a + INNER JOIN {local_treestudyplan_item} i ON i.course_id = a.courseid + INNER JOIN {local_treestudyplan_line} l ON l.id = i.line_id + INNER JOIN {local_treestudyplan_page} p ON l.page_id = p.id + ) t ON t.userid = u.id WHERE j.studyplan_id = :studyplan_id AND u.deleted != 1 ORDER BY u.lastname, u.firstname"; $rs = $DB->get_recordset_sql($sql, ['studyplan_id' => $studyplanid]); - /* - ID: 30 - page: 33 - plan: 28 - */ $users = []; - foreach ($rs as $u) { - $user = self::make_user_model($u); - $user["lastaccess"] = self::user_lastaccess($u->id, $studyplanid); + foreach ($rs as $r) { + $user = self::make_user_model($r); + $user["lastaccess"] = $r->lastaccess; $users[] = $user; } @@ -621,7 +622,7 @@ class associationservice extends \external_api { webservicehelper::require_capabilities(self::CAP_VIEW, $studyplan->context()); } - $userlist = [ + $userlist = [ 0 => [ 'id' => 0, 'label' => get_string("individuals", 'local_treestudyplan'), @@ -629,33 +630,34 @@ class associationservice extends \external_api { ], ]; - $sql = "SELECT DISTINCT c.* FROM {cohort} c INNER JOIN {local_treestudyplan_cohort} j ON j.cohort_id = c.id - WHERE j.studyplan_id = :studyplan_id"; - $crs = $DB->get_recordset_sql($sql, ['studyplan_id' => $studyplanid]); - foreach ($crs as $c) { - $users = []; - $sql = "SELECT DISTINCT u.id, u.username, u.firstname, u.lastname, u.idnumber, u.email - FROM {user} u - LEFT JOIN {cohort_members} cm ON u.id = cm.userid - WHERE cm.cohortid = :cohortid AND u.deleted != 1 - ORDER BY u.lastname, u.firstname"; - $rs = $DB->get_recordset_sql($sql, ["cohortid" => $c->id]); + $sql = "SELECT DISTINCT u.id, u.username, u.firstname, u.lastname, u.idnumber, u.email, + c.id as cohortid, c.name as cohortname, t.lastaccess as lastaccess + FROM {user} u + LEFT JOIN {cohort_members} cm ON u.id = cm.userid + LEFT JOIN {cohort} c ON c.id = cm.cohortid + LEFT JOIN {local_treestudyplan_cohort} j on j.cohort_id = c.id + LEFT JOIN (SELECT MAX(a.timeaccess) as lastaccess, a.userid as userid FROM {user_lastaccess} a + INNER JOIN {local_treestudyplan_item} i ON i.course_id = a.courseid + INNER JOIN {local_treestudyplan_line} l ON l.id = i.line_id + INNER JOIN {local_treestudyplan_page} p ON l.page_id = p.id + ) t ON t.userid = u.id + WHERE j.studyplan_id = :studyplan_id AND u.deleted != 1 + ORDER BY cohortname, u.lastname, u.firstname;"; + $rs = $DB->get_recordset_sql($sql, ['studyplan_id' => $studyplanid]); + foreach ($rs as $r) { + $user = self::make_user_model($r); + $user["lastaccess"] = empty($r->lastaccess) ? 0 : $r->lastaccess; - foreach ($rs as $u) { - $user = self::make_user_model($u); - $user["lastaccess"] = self::user_lastaccess($u->id, $studyplanid); - $users[] = $user; + if (!array_key_exists($r->cohortid, $userlist)) { + $userlist[$r->cohortid] = [ + 'id' => $r->cohortid, + 'label' => $r->cohortname, + 'users' => [], + ]; } - $rs->close(); - - $userlist[] = [ - 'id' => $c->id, - 'label' => $c->name, - 'users' => $users, - ]; + $userlist[$r->cohortid]["users"][] = $user; } - $crs->close(); - + $rs->close(); return $userlist; }