Improving query see BT#7297

1.9.x
Julio Montoya 11 years ago
parent 4f0b9676e2
commit 62e385a0b2
  1. 71
      main/inc/lib/sessionmanager.lib.php
  2. 10
      main/mySpace/index.php

@ -2484,8 +2484,7 @@ class SessionManager
// select the courses // select the courses
$sql = "SELECT * FROM $tbl_course c INNER JOIN $tbl_session_rel_course src ON c.code = src.course_code $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'"; WHERE src.id_session = '$session_id'";
if (!empty($course_name)) if (!empty($course_name)) {
{
$course_name = Database::escape_string($course_name); $course_name = Database::escape_string($course_name);
$sql .= " AND c.title LIKE '%$course_name%' "; $sql .= " AND c.title LIKE '%$course_name%' ";
} }
@ -3491,13 +3490,13 @@ class SessionManager
$statusConditions = null; $statusConditions = null;
switch ($status) { switch ($status) {
// Classic DRH
case 'drh': case 'drh':
// Classic DRH
$studentListSql = UserManager::get_users_followed_by_drh($userId, STUDENT, true, true); $studentListSql = UserManager::get_users_followed_by_drh($userId, STUDENT, true, true);
$statusConditions = " AND u.user_id IN (".$studentListSql.") "; $statusConditions = " AND u.user_id IN (".$studentListSql.") ";
break; break;
// Show all by DRH
case 'drh_all': case 'drh_all':
// Show all by DRH
$sessionsListSql = SessionManager::get_sessions_followed_by_drh($userId, null, null, false, true, true); $sessionsListSql = SessionManager::get_sessions_followed_by_drh($userId, null, null, false, true, true);
$statusConditions = " AND s.id IN (".$sessionsListSql.") "; $statusConditions = " AND s.id IN (".$sessionsListSql.") ";
break; break;
@ -3536,7 +3535,6 @@ class SessionManager
if (!empty($lastConnectionDate)) { if (!empty($lastConnectionDate)) {
$lastConnectionDate = Database::escape_string($lastConnectionDate); $lastConnectionDate = Database::escape_string($lastConnectionDate);
$where .= " AND l.login_date <= '$lastConnectionDate' "; $where .= " AND l.login_date <= '$lastConnectionDate' ";
} }
$sql .= $where; $sql .= $where;
@ -3791,39 +3789,58 @@ class SessionManager
} }
/** /**
* Get teachers followed by a user
* @param int $userId * @param int $userId
* @param int $active * @param int $active
* @param string $lastConnectionDate * @param string $lastConnectionDate
* @param bool $getCount * @param bool $getCount
* @param array $sessionIdList
* @return array|int * @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(); $teacherResult = array();
if (api_is_drh() || api_is_platform_admin()) { if (api_is_drh() || api_is_platform_admin()) {
// Followed teachers by drh // Followed teachers by drh
if (api_drh_can_access_all_session_content()) { if (api_drh_can_access_all_session_content()) {
$sessions = SessionManager::get_sessions_followed_by_drh($userId); if (empty($sessionIdList)) {
if (!empty($sessions)) { $sessions = SessionManager::get_sessions_followed_by_drh($userId);
$sessionIdList = array();
foreach ($sessions as $session) { foreach ($sessions as $session) {
$coursesFromSession = SessionManager::get_course_list_by_session_id($session['id']); $sessionIdList[] = $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 = 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 { } else {
$teacherResult = UserManager::get_users_followed_by_drh($userId, COURSEMANAGER); $teacherResult = UserManager::get_users_followed_by_drh($userId, COURSEMANAGER);
$teacherListId = array();
foreach ($teacherResult as $userInfo) {
$teacherListId[] = $userInfo['user_id'];
}
} }
} }
if (!empty($teacherResult)) { if (!empty($teacherResult)) {
$tableUser = Database::get_main_table(TABLE_MAIN_USER); $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) "; $sql .= " INNER JOIN $tableLogin l ON (l.login_user_id = u.user_id) ";
} }
$active = intval($active); $active = intval($active);
$teacherListId = array();
foreach ($teacherResult as $userInfo) {
$teacherListId[] = $userInfo['user_id'];
}
$teacherListId = implode("','", $teacherListId); $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)) { if (!empty($lastConnectionDate)) {
$lastConnectionDate = Database::escape_string($lastConnectionDate); $lastConnectionDate = Database::escape_string($lastConnectionDate);
$where .= " AND l.login_date = ( $where .= " AND l.login_date <= '$lastConnectionDate' ";
SELECT MAX(a.login_date)
FROM $tableLogin as a
WHERE a.login_user_id = u.user_id
) AND ";
$where .= " l.login_date <= '$lastConnectionDate' ";
} }
$sql .= $where; $sql .= $where;

@ -246,6 +246,13 @@ if ($is_drh) {
$sessions = Tracking::get_sessions_coached_by_user($user_id); $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 // Courses for the user
$count_courses = count($courses); $count_courses = count($courses);
@ -328,7 +335,8 @@ if (!empty($students)) {
$lastConnectionDate = api_get_utc_datetime(strtotime('15 days ago')); $lastConnectionDate = api_get_utc_datetime(strtotime('15 days ago'));
$countActiveUsers = SessionManager::getCountUserTracking(null, 1); $countActiveUsers = SessionManager::getCountUserTracking(null, 1);
$countInactiveUsers = SessionManager::getCountUserTracking(null, 0); $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); $countSleepingStudents = SessionManager::getCountUserTracking(null, 1, $lastConnectionDate);
$form = new FormValidator('search_user', 'get', api_get_path(WEB_CODE_PATH).'mySpace/student.php'); $form = new FormValidator('search_user', 'get', api_get_path(WEB_CODE_PATH).'mySpace/student.php');

Loading…
Cancel
Save