From 62e385a0b2dc13de929b88284f0306bf0ec7cdcc Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Mon, 3 Mar 2014 15:50:17 +0100 Subject: [PATCH] Improving query see BT#7297 --- main/inc/lib/sessionmanager.lib.php | 71 ++++++++++++++++------------- main/mySpace/index.php | 10 +++- 2 files changed, 48 insertions(+), 33 deletions(-) diff --git a/main/inc/lib/sessionmanager.lib.php b/main/inc/lib/sessionmanager.lib.php index 7d48abe5fd..182e8d33cb 100644 --- a/main/inc/lib/sessionmanager.lib.php +++ b/main/inc/lib/sessionmanager.lib.php @@ -2484,8 +2484,7 @@ class SessionManager // select the courses $sql = "SELECT * FROM $tbl_course c INNER JOIN $tbl_session_rel_course src ON c.code = src.course_code WHERE src.id_session = '$session_id'"; - if (!empty($course_name)) - { + if (!empty($course_name)) { $course_name = Database::escape_string($course_name); $sql .= " AND c.title LIKE '%$course_name%' "; } @@ -3491,13 +3490,13 @@ class SessionManager $statusConditions = null; switch ($status) { - // Classic DRH case 'drh': + // Classic DRH $studentListSql = UserManager::get_users_followed_by_drh($userId, STUDENT, true, true); $statusConditions = " AND u.user_id IN (".$studentListSql.") "; break; - // Show all by DRH case 'drh_all': + // Show all by DRH $sessionsListSql = SessionManager::get_sessions_followed_by_drh($userId, null, null, false, true, true); $statusConditions = " AND s.id IN (".$sessionsListSql.") "; break; @@ -3536,7 +3535,6 @@ class SessionManager if (!empty($lastConnectionDate)) { $lastConnectionDate = Database::escape_string($lastConnectionDate); $where .= " AND l.login_date <= '$lastConnectionDate' "; - } $sql .= $where; @@ -3791,39 +3789,58 @@ class SessionManager } /** + * Get teachers followed by a user * @param int $userId * @param int $active * @param string $lastConnectionDate * @param bool $getCount + * @param array $sessionIdList * @return array|int */ - public static function getTeacherTracking($userId, $active = 1, $lastConnectionDate = null, $getCount = false) - { + public static function getTeacherTracking( + $userId, + $active = 1, + $lastConnectionDate = null, + $getCount = false, + $sessionIdList = array() + ) { $teacherResult = array(); if (api_is_drh() || api_is_platform_admin()) { // Followed teachers by drh if (api_drh_can_access_all_session_content()) { - $sessions = SessionManager::get_sessions_followed_by_drh($userId); - if (!empty($sessions)) { + if (empty($sessionIdList)) { + $sessions = SessionManager::get_sessions_followed_by_drh($userId); + $sessionIdList = array(); foreach ($sessions as $session) { - $coursesFromSession = SessionManager::get_course_list_by_session_id($session['id']); - foreach ($coursesFromSession as $course) { - $teachers = CourseManager::get_teacher_list_from_course_code($course['code']); - foreach ($teachers as $teacher) { - if (isset($teacherResult[$teacher['user_id']])) { - continue; - } - $teacherResult[$teacher['user_id']] = $teacher; - } - } + $sessionIdList[] = $session['id']; } } + $sessionIdList = array_map('intval', $sessionIdList); + $sessionToString = implode("', '", $sessionIdList); + + $course = Database::get_main_table(TABLE_MAIN_COURSE); + $sessionCourse = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); + $courseUser = Database::get_main_table(TABLE_MAIN_COURSE_USER); + + // select the courses + $sql = "SELECT cu.user_id FROM $course c + INNER JOIN $sessionCourse src ON c.code = src.course_code + INNER JOIN $courseUser cu ON (cu.course_code = c.code) + WHERE src.id_session IN ('$sessionToString') AND cu.status = 1"; + $result = Database::query($sql); + $teacherListId = array(); + while($row = Database::fetch_array($result, 'ASSOC')) { + $teacherListId[$row['user_id']] = $row['user_id']; + } } else { $teacherResult = UserManager::get_users_followed_by_drh($userId, COURSEMANAGER); + $teacherListId = array(); + foreach ($teacherResult as $userInfo) { + $teacherListId[] = $userInfo['user_id']; + } } } - if (!empty($teacherResult)) { $tableUser = Database::get_main_table(TABLE_MAIN_USER); @@ -3839,22 +3856,12 @@ class SessionManager $sql .= " INNER JOIN $tableLogin l ON (l.login_user_id = u.user_id) "; } $active = intval($active); - $teacherListId = array(); - foreach ($teacherResult as $userInfo) { - $teacherListId[] = $userInfo['user_id']; - } - $teacherListId = implode("','", $teacherListId); - $where = " WHERE u.active = $active AND u.user_id IN ('$teacherListId') "; + $where = " WHERE u.active = $active AND u.user_id IN ('$teacherListId') "; if (!empty($lastConnectionDate)) { $lastConnectionDate = Database::escape_string($lastConnectionDate); - $where .= " AND l.login_date = ( - SELECT MAX(a.login_date) - FROM $tableLogin as a - WHERE a.login_user_id = u.user_id - ) AND "; - $where .= " l.login_date <= '$lastConnectionDate' "; + $where .= " AND l.login_date <= '$lastConnectionDate' "; } $sql .= $where; diff --git a/main/mySpace/index.php b/main/mySpace/index.php index 9ccb7b9194..99d0ccbd8a 100644 --- a/main/mySpace/index.php +++ b/main/mySpace/index.php @@ -246,6 +246,13 @@ if ($is_drh) { $sessions = Tracking::get_sessions_coached_by_user($user_id); } +$sessionIdList = array(); +if (!empty($sessions)) { + foreach ($sessions as $session) { + $sessionIdList[] = $session['id']; + } +} + // Courses for the user $count_courses = count($courses); @@ -328,7 +335,8 @@ if (!empty($students)) { $lastConnectionDate = api_get_utc_datetime(strtotime('15 days ago')); $countActiveUsers = SessionManager::getCountUserTracking(null, 1); $countInactiveUsers = SessionManager::getCountUserTracking(null, 0); - $countSleepingTeachers = SessionManager::getTeacherTracking(api_get_user_id(), 1, $lastConnectionDate, true); + + $countSleepingTeachers = SessionManager::getTeacherTracking(api_get_user_id(), 1, $lastConnectionDate, true, $sessionIdList); $countSleepingStudents = SessionManager::getCountUserTracking(null, 1, $lastConnectionDate); $form = new FormValidator('search_user', 'get', api_get_path(WEB_CODE_PATH).'mySpace/student.php');