Optimized looping queries
This commit is contained in:
parent
299eb0e881
commit
7366a6f511
1 changed files with 36 additions and 34 deletions
|
@ -477,20 +477,21 @@ class associationservice extends \external_api {
|
||||||
webservicehelper::require_capabilities(self::CAP_VIEW, $studyplan->context());
|
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
|
WHERE j.studyplan_id = :studyplan_id AND u.deleted != 1
|
||||||
ORDER BY u.lastname, u.firstname";
|
ORDER BY u.lastname, u.firstname";
|
||||||
$rs = $DB->get_recordset_sql($sql, ['studyplan_id' => $studyplanid]);
|
$rs = $DB->get_recordset_sql($sql, ['studyplan_id' => $studyplanid]);
|
||||||
|
|
||||||
/*
|
|
||||||
ID: 30
|
|
||||||
page: 33
|
|
||||||
plan: 28
|
|
||||||
*/
|
|
||||||
$users = [];
|
$users = [];
|
||||||
foreach ($rs as $u) {
|
foreach ($rs as $r) {
|
||||||
$user = self::make_user_model($u);
|
$user = self::make_user_model($r);
|
||||||
$user["lastaccess"] = self::user_lastaccess($u->id, $studyplanid);
|
$user["lastaccess"] = $r->lastaccess;
|
||||||
$users[] = $user;
|
$users[] = $user;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -621,7 +622,7 @@ class associationservice extends \external_api {
|
||||||
webservicehelper::require_capabilities(self::CAP_VIEW, $studyplan->context());
|
webservicehelper::require_capabilities(self::CAP_VIEW, $studyplan->context());
|
||||||
}
|
}
|
||||||
|
|
||||||
$userlist = [
|
$userlist = [ 0 =>
|
||||||
[
|
[
|
||||||
'id' => 0,
|
'id' => 0,
|
||||||
'label' => get_string("individuals", 'local_treestudyplan'),
|
'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
|
$sql = "SELECT DISTINCT u.id, u.username, u.firstname, u.lastname, u.idnumber, u.email,
|
||||||
WHERE j.studyplan_id = :studyplan_id";
|
c.id as cohortid, c.name as cohortname, t.lastaccess as lastaccess
|
||||||
$crs = $DB->get_recordset_sql($sql, ['studyplan_id' => $studyplanid]);
|
FROM {user} u
|
||||||
foreach ($crs as $c) {
|
LEFT JOIN {cohort_members} cm ON u.id = cm.userid
|
||||||
$users = [];
|
LEFT JOIN {cohort} c ON c.id = cm.cohortid
|
||||||
$sql = "SELECT DISTINCT u.id, u.username, u.firstname, u.lastname, u.idnumber, u.email
|
LEFT JOIN {local_treestudyplan_cohort} j on j.cohort_id = c.id
|
||||||
FROM {user} u
|
LEFT JOIN (SELECT MAX(a.timeaccess) as lastaccess, a.userid as userid FROM {user_lastaccess} a
|
||||||
LEFT JOIN {cohort_members} cm ON u.id = cm.userid
|
INNER JOIN {local_treestudyplan_item} i ON i.course_id = a.courseid
|
||||||
WHERE cm.cohortid = :cohortid AND u.deleted != 1
|
INNER JOIN {local_treestudyplan_line} l ON l.id = i.line_id
|
||||||
ORDER BY u.lastname, u.firstname";
|
INNER JOIN {local_treestudyplan_page} p ON l.page_id = p.id
|
||||||
$rs = $DB->get_recordset_sql($sql, ["cohortid" => $c->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) {
|
if (!array_key_exists($r->cohortid, $userlist)) {
|
||||||
$user = self::make_user_model($u);
|
$userlist[$r->cohortid] = [
|
||||||
$user["lastaccess"] = self::user_lastaccess($u->id, $studyplanid);
|
'id' => $r->cohortid,
|
||||||
$users[] = $user;
|
'label' => $r->cohortname,
|
||||||
|
'users' => [],
|
||||||
|
];
|
||||||
}
|
}
|
||||||
$rs->close();
|
$userlist[$r->cohortid]["users"][] = $user;
|
||||||
|
|
||||||
$userlist[] = [
|
|
||||||
'id' => $c->id,
|
|
||||||
'label' => $c->name,
|
|
||||||
'users' => $users,
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
$crs->close();
|
$rs->close();
|
||||||
|
|
||||||
return $userlist;
|
return $userlist;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue