Browse Source

Setting $_configuration['save_user_last_login'] option.

This setting allows the platform to save the latest user login in the
user.last_login field see BT#7297 and #7042.
Julio Montoya 11 years ago
parent
commit
86a3bc4d49

+ 31 - 0
main/admin/update_last_login.php

@@ -0,0 +1,31 @@
+<?php
+
+require_once '../inc/global.inc.php';
+api_protect_admin_script();
+exit;
+if (isset($_configuration['save_user_last_login'])) {
+    $tableUser = Database::get_main_table(TABLE_MAIN_USER);
+
+    $userInfo = api_get_user_info(api_get_user_id());
+    if (!empty($userInfo['last_login'])) {
+        echo "<br />Script was already executed";
+        exit;
+    }
+
+    if (!isset($userInfo['last_login'])) {
+        $sql = "SELECT login_user_id, MAX(login_date) login_date from track_e_login group by login_user_id";
+        echo "Executing: <br />$sql<br /> Updating <br />";
+        $result = Database::query($sql);
+        while ($row = Database::fetch_array($result)) {
+            $date = $row['login_date'];
+            $userId = $row['login_user_id'];
+            $sql = "UPDATE $tableUser SET last_login ='$date' WHERE user_id = $userId";
+            echo "<br />Updating: <br />$sql";
+            Database::query($sql);
+        }
+    } else {
+        $sql = "ALTER TABLE $tableUser ADD COLUMN last_login DATETIME";
+        $result = Database::query($sql);
+        echo "last_login does not exits creating with: <br/> $sql";
+    }
+}

+ 8 - 0
main/inc/global.inc.php

@@ -588,6 +588,14 @@ if (!isset($_SESSION['login_as']) && isset($_user)) {
         $now = api_get_utc_datetime(time());
         $s_sql_update_logout_date = "UPDATE $tbl_track_login SET logout_date='$now' WHERE login_id='$i_id_last_connection'";
         Database::query($s_sql_update_logout_date);
+        // Saves the last login in the user table see BT#7297
+        if (isset($_configuration['save_user_last_login']) &&
+            $_configuration['save_user_last_login']
+        ) {
+            $tableUser = Database::get_main_table(TABLE_MAIN_USER);
+            $sql = "UPDATE $tableUser SET last_login ='$now' WHERE user_id = ".$_user["user_id"];
+            Database::query($sql);
+        }
     }
 }
 // Add language_measure_frequency to your main/inc/conf/configuration.php in

+ 1 - 1
main/inc/lib/main_api.lib.php

@@ -1175,7 +1175,7 @@ function _api_format_user($user, $add_password = false) {
 
     $result['creator_id'] = $user['creator_id'];
     $result['registration_date'] = $user['registration_date'];
-
+    $result['last_login'] = isset($user['last_login']) ? $user['last_login'] : null;
     return $result;
 }
 

+ 1 - 6
main/inc/lib/sessionmanager.lib.php

@@ -3598,11 +3598,6 @@ class SessionManager
                     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)";
 
-        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
@@ -3614,7 +3609,7 @@ class SessionManager
 
         if (!empty($lastConnectionDate)) {
             $lastConnectionDate = Database::escape_string($lastConnectionDate);
-            //$where .=  " AND l.login_date <= '$lastConnectionDate' ";
+            $where .=  " AND u.last_login <= '$lastConnectionDate' ";
         }
 
         $sql .= $where;

+ 23 - 0
main/inc/lib/tracking.lib.php

@@ -70,6 +70,27 @@ class Tracking
                 $teachers[] = $teacherData['user_id'];
             }
 
+            $humanResources = SessionManager::getAllUsersFromCoursesFromAllSessionFromStatus(
+                'drh_all',
+                $userId,
+                false,
+                null,
+                null,
+                null,
+                null,
+                null,
+                null,
+                null,
+                array(),
+                array(),
+                DRH
+            );
+
+            $humanResourcesList = array();
+            foreach ($humanResources as $item) {
+                $humanResourcesList[] = $item['user_id'];
+            }
+
             $platformCourses = SessionManager::getAllCoursesFromAllSessionFromDrh($userId);
             $courses = array();
             foreach ($platformCourses as $course) {
@@ -79,6 +100,7 @@ class Tracking
         } else {
             $students = array_keys(UserManager::get_users_followed_by_drh($userId, STUDENT));
             $teachers = array_keys(UserManager::get_users_followed_by_drh($userId, COURSEMANAGER));
+            $humanResourcesList = array_keys(UserManager::get_users_followed_by_drh($userId, DRH));
 
             $platformCourses = CourseManager::get_courses_followed_by_drh($userId);
             foreach ($platformCourses as $course) {
@@ -89,6 +111,7 @@ class Tracking
         }
 
         return array(
+            'drh' => $humanResourcesList,
             'teachers' => $teachers,
             'students' => $students,
             'courses' => $courses,

+ 1 - 6
main/inc/lib/usermanager.lib.php

@@ -3527,11 +3527,6 @@ class UserManager
         }
 
         $join = null;
-        if (!empty($lastConnectionDate)) {
-            $loginTable = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
-            $join .= " INNER JOIN $loginTable l ON (l.login_user_id = u.user_id) ";
-        }
-
         $sql = " $select FROM $tbl_user u
                 INNER JOIN $tbl_user_rel_user uru ON (uru.user_id = u.user_id)
                 LEFT JOIN $tbl_user_rel_access_url a ON (a.user_id = u.user_id)
@@ -3550,7 +3545,7 @@ class UserManager
 
         if (!empty($lastConnectionDate)) {
             $lastConnectionDate = Database::escape_string($lastConnectionDate);
-            $sql .=  " AND l.login_date <= '$lastConnectionDate' ";
+            $sql .=  " AND u.last_login <= '$lastConnectionDate' ";
         }
 
         if ($getSql) {

+ 14 - 6
main/mySpace/index.php

@@ -159,6 +159,7 @@ $stats = Tracking::getStats($userId);
 
 $students = $stats['students'];
 $teachers = $stats['teachers'];
+$humanResourcesUsers = $stats['drh'];
 $courses = $stats['courses'];
 $sessions = $stats['sessions'];
 
@@ -184,12 +185,15 @@ $nb_posts = $nb_assignments = 0;
 $inactiveTime = time() - (3600 * 24 * 7);
 $nb_students = 0;
 $numberTeachers = 0;
+$countHumanResourcesUsers = 0;
 
 $daysAgo = 7;
 if (!empty($students)) {
     // Students
     $nb_students = count($students);
     $studentIds = array_values($students);
+    $countHumanResourcesUsers = count($humanResourcesUsers);
+
     // average progress
     $avg_total_progress = $progress / $nb_students;
     // average assignments
@@ -255,6 +259,14 @@ echo '<div class="report_section">
                 '</td>
                 <td align="right">'.$numberTeachers.'</td>
             </tr>
+            <tr>
+                <td>'.Display::url(
+                get_lang('FollowedHumanResources'),
+                api_get_path(WEB_CODE_PATH).'mySpace/users.php?status='.DRH
+            ).
+            '</td>
+            <td align="right">'.$countHumanResourcesUsers.'</td>
+            </tr>
             <tr>
              <td>'.Display::url(
                 get_lang('FollowedUsers'),
@@ -266,7 +278,7 @@ echo '<div class="report_section">
             <tr>
                 <td>'.Display::url(
                     get_lang('FollowedCourses'),
-                    api_get_path(WEB_CODE_PATH).'mySpace/courses.php'
+                    api_get_path(WEB_CODE_PATH).'mySpace/course.php'
                 ).
                 '</td>
                 <td align="right">'.$count_courses.$linkAddCourse.'</td>
@@ -347,7 +359,6 @@ if ($export_csv) {
     // html part
     echo '<div class="report_section">
             <table class="table table-bordered">
-
                 <tr>
                     <td>'.get_lang('AverageCoursePerStudent').'</td>
                     <td align="right">'.(is_null($avg_courses_per_student) ? '' : round($avg_courses_per_student, 2)).'</td>
@@ -377,10 +388,7 @@ if ($export_csv) {
                     <td align="right">'.(is_null($nb_assignments) ? '' : round($nb_assignments, 2)).'</td>
                 </tr>
             </table>
-            <a class="btn" href="'.api_get_path(WEB_CODE_PATH).'mySpace/student.php">
-            '.get_lang('SeeStudentList').'
-            </a>
-         </div><br />';
+         </div>';
 }
 
 

+ 39 - 46
main/mySpace/student.php

@@ -17,6 +17,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']) : 1;
+$sleepingDays = isset($_GET['sleeping_days']) ? intval($_GET['sleeping_days']) : null;
 
 api_block_anonymous_users();
 
@@ -103,40 +104,22 @@ function get_users($from, $number_of_items, $column, $direction)
             );
         }
     } else {
-        if (api_is_platform_admin()) {
-            $students = SessionManager::getAllUsersFromCoursesFromAllSessionFromStatus(
-                'admin',
-                api_get_user_id(),
-                false,
-                $from,
-                $number_of_items,
-                $column,
-                $direction,
-                $keyword,
-                $active,
-                $lastConnectionDate,
-                null,
-                null,
-                STUDENT
-            );
-        } else {
-            $students = UserManager::get_users_followed_by_drh(
-                api_get_user_id(),
-                null,
-                false,
-                false,
-                false,
-                $from,
-                $number_of_items,
-                $column,
-                $direction,
-                $active,
-                $lastConnectionDate,
-                null,
-                null,
-                STUDENT
-            );
-        }
+        $students = UserManager::get_users_followed_by_drh(
+            api_get_user_id(),
+            null,
+            false,
+            false,
+            false,
+            $from,
+            $number_of_items,
+            $column,
+            $direction,
+            $active,
+            $lastConnectionDate,
+            null,
+            null,
+            STUDENT
+        );
     }
 
     $all_datas = array();
@@ -243,7 +226,8 @@ $table = new SortableTable(
 
 $params = array(
     'keyword' => $keyword,
-    'active' => $active
+    'active' => $active,
+    'sleeping_days' => $sleepingDays
 );
 $table->set_additional_parameters($params);
 
@@ -261,18 +245,18 @@ $table->set_header(4, get_lang('Details'), false);
 
 if ($export_csv) {
     if ($is_western_name_order) {
-        $csv_header[] = array (
-            get_lang('FirstName', ''),
-            get_lang('LastName', ''),
-            get_lang('FirstLogin', ''),
-            get_lang('LastConnexion', '')
+        $csv_header[] = array(
+            get_lang('FirstName'),
+            get_lang('LastName'),
+            get_lang('FirstLogin'),
+            get_lang('LastConnexion')
         );
     } else {
-        $csv_header[] = array (
-            get_lang('LastName', ''),
-            get_lang('FirstName', ''),
-            get_lang('FirstLogin', ''),
-            get_lang('LastConnexion', '')
+        $csv_header[] = array(
+            get_lang('LastName'),
+            get_lang('FirstName'),
+            get_lang('FirstLogin'),
+            get_lang('LastConnexion')
         );
     }
 }
@@ -280,7 +264,16 @@ if ($export_csv) {
 $form = new FormValidator('search_user', 'get', api_get_path(WEB_CODE_PATH).'mySpace/student.php');
 $form->addElement('text', 'keyword', get_lang('Keyword'));
 $form->addElement('select', 'active', get_lang('Status'), array(1 => get_lang('Active'), 0 => get_lang('Inactive')));
-//$form->addElement('select', 'sleeping_days', get_lang('InactiveDays'), array('', 1 => 1, 5 => 5, 15 => 15, 30 => 30, 60 => 60, 90 => 90, 120 => 120));
+if (isset($_configuration['save_user_last_login']) &&
+    $_configuration['save_user_last_login']
+) {
+    $form->addElement(
+        'select',
+        'sleeping_days',
+        get_lang('InactiveDays'),
+        array('', 1 => 1, 5 => 5, 15 => 15, 30 => 30, 60 => 60, 90 => 90, 120 => 120)
+    );
+}
 $form->addElement('button', 'submit', get_lang('Search'));
 $form->setDefaults($params);
 

+ 38 - 46
main/mySpace/teachers.php

@@ -104,40 +104,22 @@ function get_users($from, $number_of_items, $column, $direction)
             );
         }
     } else {
-        if (api_is_platform_admin()) {
-            $students = SessionManager::getAllUsersFromCoursesFromAllSessionFromStatus(
-                'admin',
-                api_get_user_id(),
-                false,
-                $from,
-                $number_of_items,
-                $column,
-                $direction,
-                $keyword,
-                $active,
-                $lastConnectionDate,
-                null,
-                null,
-                COURSEMANAGER
-            );
-        } else {
-            $students = UserManager::get_users_followed_by_drh(
-                api_get_user_id(),
-                null,
-                false,
-                false,
-                false,
-                $from,
-                $number_of_items,
-                $column,
-                $direction,
-                $active,
-                $lastConnectionDate,
-                null,
-                null,
-                COURSEMANAGER
-            );
-        }
+        $students = UserManager::get_users_followed_by_drh(
+            api_get_user_id(),
+            null,
+            false,
+            false,
+            false,
+            $from,
+            $number_of_items,
+            $column,
+            $direction,
+            $active,
+            $lastConnectionDate,
+            null,
+            null,
+            COURSEMANAGER
+        );
     }
 
     $all_datas = array();
@@ -244,7 +226,8 @@ $table = new SortableTable(
 
 $params = array(
     'keyword' => $keyword,
-    'active' => $active
+    'active' => $active,
+    'sleeping_days' => $sleepingDays
 );
 $table->set_additional_parameters($params);
 
@@ -262,18 +245,18 @@ $table->set_header(4, get_lang('Details'), false);
 
 if ($export_csv) {
     if ($is_western_name_order) {
-        $csv_header[] = array (
-            get_lang('FirstName', ''),
-            get_lang('LastName', ''),
-            get_lang('FirstLogin', ''),
-            get_lang('LastConnexion', '')
+        $csv_header[] = array(
+            get_lang('FirstName'),
+            get_lang('LastName'),
+            get_lang('FirstLogin'),
+            get_lang('LastConnexion')
         );
     } else {
-        $csv_header[] = array (
-            get_lang('LastName', ''),
-            get_lang('FirstName', ''),
-            get_lang('FirstLogin', ''),
-            get_lang('LastConnexion', '')
+        $csv_header[] = array(
+            get_lang('LastName'),
+            get_lang('FirstName'),
+            get_lang('FirstLogin'),
+            get_lang('LastConnexion')
         );
     }
 }
@@ -281,7 +264,16 @@ if ($export_csv) {
 $form = new FormValidator('search_user', 'get', api_get_path(WEB_CODE_PATH).'mySpace/teachers.php');
 $form->addElement('text', 'keyword', get_lang('Keyword'));
 $form->addElement('select', 'active', get_lang('Status'), array(1 => get_lang('Active'), 0 => get_lang('Inactive')));
-//$form->addElement('select', 'sleeping_days', get_lang('InactiveDays'), array('', 1 => 1, 5 => 5, 15 => 15, 30 => 30, 60 => 60, 90 => 90, 120 => 120));
+if (isset($_configuration['save_user_last_login']) &&
+    $_configuration['save_user_last_login']
+) {
+    $form->addElement(
+        'select',
+        'sleeping_days',
+        get_lang('InactiveDays'),
+        array('', 1 => 1, 5 => 5, 15 => 15, 30 => 30, 60 => 60, 90 => 90, 120 => 120)
+    );
+}
 $form->addElement('button', 'submit', get_lang('Search'));
 $form->setDefaults($params);
 

+ 69 - 44
main/mySpace/users.php

@@ -17,6 +17,8 @@ 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;
+$sleepingDays = isset($_GET['sleeping_days']) ? intval($_GET['sleeping_days']) : null;
+$status = isset($_GET['status']) ? Security::remove_XSS($_GET['status']) : null;
 
 api_block_anonymous_users();
 
@@ -32,16 +34,26 @@ if (isset($_GET["user_id"]) && $_GET["user_id"]!="" && isset($_GET["type"]) && $
     $interbreadcrumb[] = array ("url" => "coaches.php", "name" => get_lang('Tutors'));
 }
 
-function get_count_users($keyword = null, $active = null)
+function get_count_users()
 {
     $sleepingDays = isset($_GET['sleeping_days']) ? intval($_GET['sleeping_days']) : null;
+    $active = isset($_GET['active']) ? $_GET['active'] : 1;
+    $keyword = isset($_GET['keyword']) ? Security::remove_XSS($_GET['keyword']) : null;
+    $status = isset($_GET['status']) ? Security::remove_XSS($_GET['status']) : null;
 
     $lastConnectionDate = null;
     if (!empty($sleepingDays)) {
         $lastConnectionDate = api_get_utc_datetime(strtotime($sleepingDays.' days ago'));
     }
 
-    return SessionManager::getCountUserTracking($keyword, $active, $lastConnectionDate);
+    return SessionManager::getCountUserTracking(
+        $keyword,
+        $active,
+        $lastConnectionDate,
+        null,
+        null,
+        $status
+    );
 }
 
 function get_users($from, $number_of_items, $column, $direction)
@@ -49,6 +61,7 @@ function get_users($from, $number_of_items, $column, $direction)
     $active = isset($_GET['active']) ? $_GET['active'] : 1;
     $keyword = isset($_GET['keyword']) ? Security::remove_XSS($_GET['keyword']) : null;
     $sleepingDays = isset($_GET['sleeping_days']) ? intval($_GET['sleeping_days']) : null;
+    $status = isset($_GET['status']) ? Security::remove_XSS($_GET['status']) : null;
 
     $lastConnectionDate = null;
     if (!empty($sleepingDays)) {
@@ -71,39 +84,12 @@ function get_users($from, $number_of_items, $column, $direction)
                 $direction,
                 $keyword,
                 $active,
-                $lastConnectionDate
-            );
-        } else {
-            $students = UserManager::get_users_followed_by_drh(
-                api_get_user_id(),
+                $lastConnectionDate,
                 null,
-                false,
-                false,
-                false,
-                $from,
-                $number_of_items,
-                $column,
-                $direction,
-                $active,
-                $lastConnectionDate
-            );
-        }
-    } else {
-        if (api_is_platform_admin()) {
-            $students = SessionManager::getAllUsersFromCoursesFromAllSessionFromStatus(
-                'admin',
-                api_get_user_id(),
-                false,
-                $from,
-                $number_of_items,
-                $column,
-                $direction,
-                $keyword,
-                $active,
-                $lastConnectionDate
+                null,
+                $status
             );
         } else {
-
             $students = UserManager::get_users_followed_by_drh(
                 api_get_user_id(),
                 null,
@@ -115,9 +101,29 @@ function get_users($from, $number_of_items, $column, $direction)
                 $column,
                 $direction,
                 $active,
-                $lastConnectionDate
+                $lastConnectionDate,
+                null,
+                null,
+                $status
             );
         }
+    } else {
+        $students = UserManager::get_users_followed_by_drh(
+            api_get_user_id(),
+            null,
+            false,
+            false,
+            false,
+            $from,
+            $number_of_items,
+            $column,
+            $direction,
+            $active,
+            $lastConnectionDate,
+            null,
+            null,
+            $status
+        );
     }
 
     $all_datas = array();
@@ -171,7 +177,7 @@ function get_users($from, $number_of_items, $column, $direction)
 
         if (isset($_GET['id_coach']) && intval($_GET['id_coach']) != 0) {
             $detailsLink = '<a href="myStudents.php?student='.$student_id.'&id_coach='.$coach_id.'&id_session='.$_GET['id_session'].'">
-				          <img src="'.api_get_path(WEB_IMG_PATH).'2rightarrow.gif" border="0" /></a>';
+				            <img src="'.api_get_path(WEB_IMG_PATH).'2rightarrow.gif" border="0" /></a>';
         } else {
             $detailsLink =  '<a href="myStudents.php?student='.$student_id.'">
 				             <img src="'.api_get_path(WEB_IMG_PATH).'2rightarrow.gif" border="0" /></a>';
@@ -224,7 +230,9 @@ $table = new SortableTable(
 
 $params = array(
     'keyword' => $keyword,
-    'active' => $active
+    'active' => $active,
+    'sleeping_days' => $sleepingDays,
+    'status' => $status
 );
 $table->set_additional_parameters($params);
 
@@ -243,23 +251,40 @@ $table->set_header(4, get_lang('Details'), false);
 if ($export_csv) {
     if ($is_western_name_order) {
         $csv_header[] = array (
-            get_lang('FirstName', ''),
-            get_lang('LastName', ''),
-            get_lang('FirstLogin', ''),
-            get_lang('LastConnexion', '')
+            get_lang('FirstName'),
+            get_lang('LastName'),
+            get_lang('FirstLogin'),
+            get_lang('LastConnexion')
         );
     } else {
         $csv_header[] = array (
-            get_lang('LastName', ''),
-            get_lang('FirstName', ''),
-            get_lang('FirstLogin', ''),
-            get_lang('LastConnexion', '')
+            get_lang('LastName'),
+            get_lang('FirstName'),
+            get_lang('FirstLogin'),
+            get_lang('LastConnexion')
         );
     }
 }
 
-$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/users.php');
 $form->addElement('text', 'keyword', get_lang('User'));
+$form->addElement('select', 'active', get_lang('Status'), array(1 => get_lang('Active'), 0 => get_lang('Inactive')));
+$form->addElement('select', 'status', get_lang('Status'), array(
+    '' => '',
+    STUDENT => get_lang('Student'),
+    COURSEMANAGER => get_lang('Teacher'),
+    DRH => get_lang('DRH'))
+);
+if (isset($_configuration['save_user_last_login']) &&
+    $_configuration['save_user_last_login']
+) {
+    $form->addElement(
+        'select',
+        'sleeping_days',
+        get_lang('InactiveDays'),
+        array('', 1 => 1, 5 => 5, 15 => 15, 30 => 30, 60 => 60, 90 => 90, 120 => 120)
+    );
+}
 $form->addElement('button', 'submit', get_lang('Search'));
 $form->setDefaults($params);