From 311e823bd0aa36e7e339ba67bab3e82e5fd1dd0e Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Mon, 16 Dec 2013 15:23:33 +0100 Subject: [PATCH] Adding sleeping teachers/students counter in the reporting tab see BT#6770 --- main/inc/lib/sessionmanager.lib.php | 129 +++++++++++++++++++++++++--- main/mySpace/index.php | 17 +++- main/mySpace/student.php | 49 ++++++++--- main/mySpace/teachers.php | 116 ++++++++++++++++++------- 4 files changed, 257 insertions(+), 54 deletions(-) diff --git a/main/inc/lib/sessionmanager.lib.php b/main/inc/lib/sessionmanager.lib.php index b267eb7c8f..0ded4228c3 100644 --- a/main/inc/lib/sessionmanager.lib.php +++ b/main/inc/lib/sessionmanager.lib.php @@ -3141,6 +3141,8 @@ class SessionManager * @param int $column * @param string $direction * @param string $keyword + * @param string $active + * @param string $lastConnectionDate * @return array|int */ public static function getAllUsersFromCoursesFromAllSessionFromStatus( @@ -3152,7 +3154,8 @@ class SessionManager $column = 1, $direction = 'asc', $keyword = null, - $active = null + $active = null, + $lastConnectionDate = null ) { $tbl_user = Database::get_main_table(TABLE_MAIN_USER); $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); @@ -3213,6 +3216,7 @@ class SessionManager } $select = "SELECT DISTINCT u.*"; + if ($getCount) { $select = "SELECT count(DISTINCT u.user_id) as count "; } @@ -3221,11 +3225,29 @@ class SessionManager FROM $tbl_session s INNER JOIN $tbl_session_rel_course_rel_user su ON (s.id = su.id_session) INNER JOIN $tbl_user u ON (u.user_id = su.id_user AND s.id = id_session) - INNER JOIN $tbl_session_rel_access_url url ON (url.session_id = s.id) - WHERE access_url_id = $urlId + INNER JOIN $tbl_session_rel_access_url url ON (url.session_id = s.id)"; + + if (!empty($lastConnectionDate)) { + $loginTable = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN); + $sql .= " INNER JOIN $loginTable l ON (l.login_user_id = u.user_id) "; + } + + $where = " WHERE access_url_id = $urlId $statusConditions $activeCondition - "; + "; + + if (!empty($lastConnectionDate)) { + $lastConnectionDate = Database::escape_string($lastConnectionDate); + $where .= " AND l.login_date = ( + SELECT MAX(a.login_date) + FROM $loginTable as a + WHERE a.login_user_id = u.user_id + )"; + $where .= " AND l.login_date <= '$lastConnectionDate' "; + } + + $sql .= $where; if (!empty($keyword)) { $keyword = Database::escape_string($keyword); @@ -3402,10 +3424,11 @@ class SessionManager /** * @param string $keyword - * @param int $active + * @param string $active + * @param string $lastConnectionDate * @return array|int */ - public static function getCountUserTracking($keyword = null, $active = null) + public static function getCountUserTracking($keyword = null, $active = null, $lastConnectionDate = null) { if (!isset($keyword)) { $keyword = isset($_GET['keyword']) ? Security::remove_XSS($_GET['keyword']) : null; @@ -3426,7 +3449,8 @@ class SessionManager null, null, $keyword, - $active + $active, + $lastConnectionDate ); } else { $count = self::getAllUsersFromCoursesFromAllSessionFromStatus( @@ -3438,7 +3462,8 @@ class SessionManager null, null, $keyword, - $active + $active, + $lastConnectionDate ); } } else { @@ -3452,7 +3477,8 @@ class SessionManager null, null, $keyword, - $active + $active, + $lastConnectionDate ); } else { $count = self::getAllUsersFromCoursesFromAllSessionFromStatus( @@ -3464,13 +3490,96 @@ class SessionManager null, null, $keyword, - $active + $active, + $lastConnectionDate ); } } return $count; } + /** + * @param int $userId + * @param int $active + * @param string $lastConnectionDate + * @param bool $getCount + */ + public function getTeacherTracking($userId, $active = 1, $lastConnectionDate = null, $getCount = false) + { + $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)) { + 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; + } + } + } + } + } else { + $teacherResult = UserManager::get_users_followed_by_drh($userId, COURSEMANAGER); + } + } + + if (!empty($teacherResult)) { + $tableUser = Database::get_main_table(TABLE_MAIN_USER); + + $select = "SELECT DISTINCT u.* "; + if ($getCount) { + $select = "SELECT count(DISTINCT(u.user_id)) as count"; + } + + $sql = "$select FROM $tableUser u"; + + if (!empty($lastConnectionDate)) { + $tableLogin = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN); + $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') "; + + 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' "; + } + + $sql .= $where; + $result = Database::query($sql); + if (Database::num_rows($result)) { + if ($getCount) { + $row = Database::fetch_array($result); + return $row['count']; + } else { + + return Database::store_result($result, 'ASSOC'); + } + } + } + + return 0; + } + /** * Get the list of course tools that have to be dealt with in case of * registering any course to a session diff --git a/main/mySpace/index.php b/main/mySpace/index.php index bc55ab7c29..18a0458b73 100644 --- a/main/mySpace/index.php +++ b/main/mySpace/index.php @@ -374,6 +374,11 @@ if (empty($session_id)) { $countActiveUsers = SessionManager::getCountUserTracking(null, 1); $countInactiveUsers = SessionManager::getCountUserTracking(null, 0); + $lastConnectionDate = api_get_utc_datetime(strtotime('15 days ago')); + + $countSleepingTeachers = SessionManager::getTeacherTracking(api_get_user_id(), 1, $lastConnectionDate, true); + $countSleepingStudents =SessionManager::getCountUserTracking(null, 1, $lastConnectionDate); + $form = new FormValidator('search_user', 'get', api_get_path(WEB_CODE_PATH).'mySpace/student.php'); $form->addElement('text', 'keyword', get_lang('User')); $form->addElement('button', 'submit', get_lang('Search')); @@ -386,11 +391,21 @@ if (empty($session_id)) { '.Display::url(get_lang('ActiveUsers'), api_get_path(WEB_CODE_PATH).'mySpace/student.php?active=1').' '.$countActiveUsers.' - + + '.Display::url(get_lang('InactiveUsers'), api_get_path(WEB_CODE_PATH).'mySpace/student.php?active=0').' '.$countInactiveUsers.' + + '.Display::url(get_lang('SleepingTeachers'), api_get_path(WEB_CODE_PATH).'mySpace/teachers.php?sleeping_days=15').' + '.$countSleepingTeachers.' + + + '.Display::url(get_lang('SleepingStudents'), api_get_path(WEB_CODE_PATH).'mySpace/student.php?sleeping_days=15').' + '.$countSleepingStudents.' + + '.get_lang('AverageCoursePerStudent').' '.(is_null($avg_courses_per_student) ? '' : $avg_courses_per_student).' diff --git a/main/mySpace/student.php b/main/mySpace/student.php index 5b554adcd2..0531bf48c6 100644 --- a/main/mySpace/student.php +++ b/main/mySpace/student.php @@ -16,6 +16,7 @@ require_once api_get_path(LIBRARY_PATH).'export.lib.inc.php'; $export_csv = isset($_GET['export']) && $_GET['export'] == 'csv' ? true : false; $keyword = isset($_GET['keyword']) ? Security::remove_XSS($_GET['keyword']) : null; +$active = isset($_GET['active']) ? intval($_GET['active']) : null; api_block_anonymous_users(); @@ -40,6 +41,12 @@ function get_users($from, $number_of_items, $column, $direction) { $active = isset($_GET['active']) ? $_GET['active'] : null; $keyword = isset($_GET['keyword']) ? Security::remove_XSS($_GET['keyword']) : null; + $sleepingDays = isset($_GET['sleeping_days']) ? intval($_GET['sleeping_days']) : null; + + $lastConnectionDate = null; + if (!empty($sleepingDays)) { + $lastConnectionDate = api_get_utc_datetime(strtotime($sleepingDays.' days ago')); + } $is_western_name_order = api_is_western_name_order(); $coach_id = api_get_user_id(); @@ -56,7 +63,8 @@ function get_users($from, $number_of_items, $column, $direction) $column, $direction, $keyword, - $active + $active, + $lastConnectionDate ); } else { $students = SessionManager::getAllUsersFromCoursesFromAllSessionFromStatus( @@ -68,7 +76,8 @@ function get_users($from, $number_of_items, $column, $direction) $column, $direction, $keyword, - $active + $active, + $lastConnectionDate ); } } else { @@ -82,7 +91,8 @@ function get_users($from, $number_of_items, $column, $direction) $column, $direction, $keyword, - $active + $active, + $lastConnectionDate ); } else { $students = SessionManager::getAllUsersFromCoursesFromAllSessionFromStatus( @@ -94,7 +104,8 @@ function get_users($from, $number_of_items, $column, $direction) $column, $direction, $keyword, - $active + $active, + $lastConnectionDate ); } } @@ -171,12 +182,13 @@ $sort_by_first_name = api_sort_by_first_name(); $actions .= '
'; if (api_is_drh()) { - $menu_items = array(); - $menu_items[] = Display::url(Display::return_icon('stats.png', get_lang('MyStats'),'',ICON_SIZE_MEDIUM), api_get_path(WEB_CODE_PATH)."auth/my_progress.php" ); - $menu_items[] = Display::url(Display::return_icon('user_na.png', get_lang('Students'), array(), ICON_SIZE_MEDIUM), '#'); - $menu_items[] = Display::url(Display::return_icon('teacher.png', get_lang('Trainers'), array(), ICON_SIZE_MEDIUM), 'teachers.php'); - $menu_items[] = Display::url(Display::return_icon('course.png', get_lang('Courses'), array(), ICON_SIZE_MEDIUM), 'course.php'); - $menu_items[] = Display::url(Display::return_icon('session.png', get_lang('Sessions'), array(), ICON_SIZE_MEDIUM), 'session.php'); + $menu_items = array( + Display::url(Display::return_icon('stats.png', get_lang('MyStats'), '', ICON_SIZE_MEDIUM), api_get_path(WEB_CODE_PATH)."auth/my_progress.php" ), + Display::url(Display::return_icon('user_na.png', get_lang('Students'), array(), ICON_SIZE_MEDIUM), '#'), + Display::url(Display::return_icon('teacher.png', get_lang('Trainers'), array(), ICON_SIZE_MEDIUM), 'teachers.php'), + Display::url(Display::return_icon('course.png', get_lang('Courses'), array(), ICON_SIZE_MEDIUM), 'course.php'), + Display::url(Display::return_icon('session.png', get_lang('Sessions'), array(), ICON_SIZE_MEDIUM), 'session.php') + ); $nb_menu_items = count($menu_items); if ($nb_menu_items > 1) { @@ -201,7 +213,8 @@ $table = new SortableTable( ); $params = array( - 'keyword' => isset($_GET['keyword']) ? Security::remove_XSS($_GET['keyword']) : null, + 'keyword' => $keyword, + 'active' => $active ); $table->set_additional_parameters($params); @@ -214,7 +227,7 @@ if ($is_western_name_order) { } $table->set_header(2, get_lang('FirstLogin'), false); -$table->set_header(3, get_lang('LatestLogin'), false); +$table->set_header(3, get_lang('LastConnexion'), false); $table->set_header(4, get_lang('Details'), false); if ($export_csv) { @@ -223,14 +236,14 @@ if ($export_csv) { get_lang('FirstName', ''), get_lang('LastName', ''), get_lang('FirstLogin', ''), - get_lang('LatestLogin', '') + get_lang('LastConnexion', '') ); } else { $csv_header[] = array ( get_lang('LastName', ''), get_lang('FirstName', ''), get_lang('FirstLogin', ''), - get_lang('LatestLogin', '') + get_lang('LastConnexion', '') ); } } @@ -256,6 +269,14 @@ if ($export_csv) { echo $actions; $page_title = get_lang('Students'); echo Display::page_subheader($page_title); + if (isset($active)) { + if ($active) { + $activeLabel = get_lang('ActiveUsers'); + } else { + $activeLabel = get_lang('InactiveUsers'); + } + echo Display::page_subheader2($activeLabel); + } $form->display(); $table->display(); } diff --git a/main/mySpace/teachers.php b/main/mySpace/teachers.php index 33a020bb94..3b49c1c40f 100644 --- a/main/mySpace/teachers.php +++ b/main/mySpace/teachers.php @@ -15,6 +15,8 @@ $cidReset = true; require_once '../inc/global.inc.php'; require_once 'myspace.lib.php'; +$userId = api_get_user_id(); + $this_section = SECTION_TRACKING; $nameTools = get_lang('Teachers'); @@ -23,11 +25,13 @@ api_block_anonymous_users(); $interbreadcrumb[] = array ("url" => "index.php", "name" => get_lang('MySpace')); Display :: display_header($nameTools); +$sleepingDays = isset($_GET['sleeping_days']) ? intval($_GET['sleeping_days']) : null; + $formateurs = array(); if (api_is_drh() || api_is_platform_admin()) { - // Followed teachers by drh + // Followed teachers by drh if (api_drh_can_access_all_session_content()) { - $sessions = SessionManager::get_sessions_followed_by_drh(api_get_user_id()); + $sessions = SessionManager::get_sessions_followed_by_drh($userId); if (!empty($sessions)) { $formateurs = array(); foreach ($sessions as $session) { @@ -43,17 +47,24 @@ if (api_is_drh() || api_is_platform_admin()) { } } } - } else { - $formateurs = UserManager::get_users_followed_by_drh($_user['user_id'], COURSEMANAGER); + $formateurs = UserManager::get_users_followed_by_drh($userId, COURSEMANAGER); } + $lastConnectionDate = null; + + if (!empty($sleepingDays)) { + $lastConnectionDate = api_get_utc_datetime(strtotime($sleepingDays.' days ago')); + } + + $formateurs = SessionManager::getTeacherTracking($userId, 1, $lastConnectionDate); + $menu_items = array( Display::url(Display::return_icon('stats.png', get_lang('MyStats'), '', ICON_SIZE_MEDIUM), api_get_path(WEB_CODE_PATH)."auth/my_progress.php" ), - Display::url(Display::return_icon('user.png', get_lang('Students'), array(), ICON_SIZE_MEDIUM), "index.php?view=drh_students&display=yourstudents"), + Display::url(Display::return_icon('user.png', get_lang('Students'), array(), ICON_SIZE_MEDIUM), "index.php?view=drh_students&display=yourstudents"), Display::url(Display::return_icon('teacher_na.png', get_lang('Trainers'), array(), ICON_SIZE_MEDIUM), '#'), - Display::url(Display::return_icon('course.png', get_lang('Courses'), array(), ICON_SIZE_MEDIUM), 'course.php'), - Display::url(Display::return_icon('session.png', get_lang('Sessions'), array(), ICON_SIZE_MEDIUM), 'session.php') + Display::url(Display::return_icon('course.png', get_lang('Courses'), array(), ICON_SIZE_MEDIUM), 'course.php'), + Display::url(Display::return_icon('session.png', get_lang('Sessions'), array(), ICON_SIZE_MEDIUM), 'session.php') ); echo '
'; @@ -71,15 +82,17 @@ if (api_is_drh() || api_is_platform_admin()) { } echo '
'; echo Display::page_subheader(get_lang('YourTeachers')); + + if (!empty($lastConnectionDate)) { + echo Display::page_subheader2(get_lang('SleepingTeachers').' '.api_get_local_time($lastConnectionDate)); + } } if (!api_is_drh()) { api_display_tool_title($nameTools); } -/** - * MAIN PART - */ +/** MAIN PART */ if (isset($_POST['export'])) { $is_western_name_order = api_is_western_name_order(PERSON_NAME_DATA_EXPORT); @@ -135,9 +148,27 @@ $form->addelement('style_submit_button', 'submit', get_lang('Filter')); $form->display(); if ($is_western_name_order) { - echo ''; + echo '
'.get_lang('FirstName').''.get_lang('LastName').''.$time_label.''.get_lang('Email').''.get_lang('AdminCourses').''.get_lang('Students').'
+ + + + + + + + + '; } else { - echo '
'.get_lang('FirstName').''.get_lang('LastName').''.$time_label.''.get_lang('Email').''.get_lang('LastConnexion').''.get_lang('AdminCourses').''.get_lang('Students').'
'; + echo '
'.get_lang('LastName').''.get_lang('FirstName').''.$time_label.''.get_lang('Email').''.get_lang('AdminCourses').''.get_lang('Students').'
+ + + + + + + + + '; } if ($is_western_name_order) { @@ -156,21 +187,21 @@ $data = array(); if (count($formateurs) > 0) { $i = 1; - foreach ($formateurs as $formateur) { + foreach ($formateurs as $formateur) { $user_id = $formateur["user_id"]; $lastname = $formateur["lastname"]; $firstname = $formateur["firstname"]; $email = $formateur["email"]; - if ($i % 2 == 0) { - $css_class = "row_odd"; - + if ($i % 2 == 0) { + $css_class = "row_odd"; if ($i % 20 == 0 && $i != 0) { if ($is_western_name_order) { echo ' + '; @@ -179,6 +210,7 @@ if (count($formateurs) > 0) { + '; @@ -188,7 +220,7 @@ if (count($formateurs) > 0) { $css_class = "row_even"; } - $i++; + $i++; if ($is_western_name_order) { $data[$user_id]["firstname"] = $firstname; @@ -198,24 +230,50 @@ if (count($formateurs) > 0) { $data[$user_id]["firstname"] = $firstname; } - $time_on_platform = api_time_to_hms(Tracking :: get_time_spent_on_the_platform($user_id, $time_filter, $start_date, $end_date)); - $data[$user_id]["timespentlastweek"] = $time_on_platform; - $data[$user_id]["email"] = $email; + $time_on_platform = api_time_to_hms(Tracking :: get_time_spent_on_the_platform($user_id, $time_filter, $start_date, $end_date)); + $data[$user_id]["timespentlastweek"] = $time_on_platform; + $data[$user_id]["email"] = $email; + + $timestamp = Tracking::get_last_connection_date($user_id, false, true); + + $lastConnection = api_get_local_time($timestamp); - if ($is_western_name_order) { - echo ''; - } else { - echo ''; - } - } + if ($is_western_name_order) { + echo ' + + + + + + + + '; + } else { + echo ' + + + + + '; + } + } } else { - // No results - echo ''; + // No results + echo ''; } echo '
'.get_lang('LastName').''.get_lang('FirstName').''.$time_label.''.get_lang('Email').''.get_lang('LastConnexion').''.get_lang('AdminCourses').''.get_lang('Students').'
'.get_lang('FirstName').' '.get_lang('LastName').' '.get_lang('Email').''.get_lang('LastConnexion').' '.get_lang('AdminCourses').' '.get_lang('Students').'
'.get_lang('LastName').' '.get_lang('FirstName').' '.get_lang('Email').''.get_lang('LastConnexion').' '.get_lang('AdminCourses').' '.get_lang('Students').'
'.$firstname.''.$lastname.''.$time_on_platform.''.$email.'
'.$lastname.''.$firstname.''.$time_on_platform.''.$email.'
'.$firstname.''.$lastname.''.$time_on_platform.''.$email.''.$lastConnection.' + + + + +
'.$lastname.''.$firstname.''.$time_on_platform.''.$email.' + + + +
'.get_lang("NoResults").'
'.get_lang("NoResults").'
'; if (isset($_POST['export']) || (api_is_drh() && isset($_GET['export']))) { - MySpace::export_csv($header, $data, 'teachers.csv'); + MySpace::export_csv($header, $data, 'teachers.csv'); } if (!api_is_drh()) {