Przeglądaj źródła

track_e_course_access login and logout date range its too small, so we use track_e_course_access.counter as total clicks and for ip we look for the closest lower o higher ip refs BT#7306

César Perales 11 lat temu
rodzic
commit
70b98308a1
2 zmienionych plików z 123 dodań i 26 usunięć
  1. 15 3
      main/inc/lib/sessionmanager.lib.php
  2. 108 23
      main/inc/lib/tracking.lib.php

+ 15 - 3
main/inc/lib/sessionmanager.lib.php

@@ -1076,6 +1076,7 @@ class SessionManager
                 ")."
 
                 a.logout_course_date,
+                a.counter,
                 c.title,
                 c.code,
                 u.user_id
@@ -1085,7 +1086,6 @@ class SessionManager
             $where $order $limit";
         $result = Database::query(sprintf($sql, $sessionId, $courseId));
 
-        $clicks = Tracking::get_total_clicks_by_session();
         $data = array ();
         while ($user = Database::fetch_assoc($result)) {
             $data[] = $user;
@@ -1093,22 +1093,34 @@ class SessionManager
 
         //foreach
         foreach ($data as $key => $info) {
+
+            //We are not using this becaouse the range its to small and no other date match the condition of this function
+            //$clicks = Tracking::get_total_clicks($info['user_id'], $courseId, $sessionId, $info['login_course_date'], $info['logout_course_date']);
+
             #building array to display
             $return[] = array(
+                'user_id' => $info['user_id'],
                 'logindate' => $info['login_course_date'],
                 'username' => $info['username'],
                 'firstname' => $info['firstname'],
                 'lastname' => $info['lastname'],
-                'clicks' => $clicks[$info['user_id']],
+                'clicks' => $info['counter'], //+ $clicks[$info['user_id']],
                 'ip' => '',
                 'timeLoggedIn' => gmdate("H:i:s", strtotime($info['logout_course_date']) - strtotime($info['login_course_date'])),
             );
         }
         //Search for ip, we do less querys if we iterate the final array
         foreach ($return as $key => $info) {
-            $sql = sprintf("SELECT login_ip FROM $track_e_login WHERE ('%s' BETWEEN login_date AND logout_date)", $info['logindate']); //TODO add select by user too
+            //closest lower ip
+            $sql = sprintf("SELECT login_ip FROM $track_e_login WHERE login_user_id = %d AND login_date < '%s' ORDER BY login_date DESC LIMIT 1", $info['user_id'], $info['logindate']); //TODO add select by user too
             $result = Database::query($sql);
             $ip = Database::fetch_assoc($result);
+            //if no ip founded, we search the closest higher ip
+            if (empty($ip['login_ip'])) {
+                $sql = sprintf("SELECT login_ip FROM $track_e_login WHERE login_user_id = %d AND login_date > '%s'  ORDER BY login_date ASC LIMIT 1", $info['user_id'], $info['logindate']); //TODO add select by user too
+                $result = Database::query($sql);
+                $ip = Database::fetch_assoc($result);
+            }
             #add ip to final array
             $return[$key]['ip'] = $ip['login_ip'];
         }

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

@@ -2052,47 +2052,132 @@ class Tracking
         return $data;
     }
     /**
-     * Get total clicks by session
-     * @param    int        Session id (optional), if param $session_id is null(default) it'll return results including sessions, 0 = session is not filtered
-     * @return    array     data
-     * @todo    implement total click by $course_id
+     * Get total clicks
+     * THIS FUNCTION IS NOT BEEN USED, IT WAS MEANT TO BE USE WITH track_e_course_access.date_from and track_e_course_access.date_to, 
+     * BUT NO ROW MATCH THE CONDITION, IT SHOULD BE FINE TO USE IT WHEN YOU USE USER DEFINED DATES AND NO CHAMILO DATES
+     * @param   int     User Id
+     * @param   int     Course Id
+     * @param   int     Session Id (optional), if param $session_id is null(default) it'll return results including sessions, 0 = session is not filtered
+     * @param   string  Date from
+     * @param   string  Date to
+     * @return  array   Data
+     * @author  César Perales cesar.perales@beeznest.com 2014-01-16
      */
-    public static function get_total_clicks_by_session($session_id = null) {
+    public static function get_total_clicks($userId, $courseId, $sessionId = 0, $date_from = '', $date_to = '') 
+    {
+        $course = api_get_course_info_by_id($courseId);
         $tables = array(
-            TABLE_STATISTIC_TRACK_E_LASTACCESS => array('access_session_id','access_user_id'),
-            TABLE_STATISTIC_TRACK_E_ACCESS => array('access_session_id', 'access_user_id'),
-            #TABLE_STATISTIC_TRACK_E_LOGIN,
-            TABLE_STATISTIC_TRACK_E_DOWNLOADS => array('down_session_id', 'down_user_id'),
-            TABLE_STATISTIC_TRACK_E_LINKS => array('links_session_id', 'links_user_id'),
-            TABLE_STATISTIC_TRACK_E_ONLINE => array('session_id', 'login_user_id'),
+            TABLE_STATISTIC_TRACK_E_LASTACCESS => array(
+                'course'    => 'access_cours_code',
+                'session'   => 'access_session_id',
+                'user'      => 'access_user_id',
+                'start_date'=> 'access_date',
+                ),
+            TABLE_STATISTIC_TRACK_E_ACCESS => array(
+                'course'    => 'access_cours_code',
+                'session'   => 'access_session_id',
+                'user'      => 'access_user_id',
+                'start_date'=> 'access_date',
+                ),
+            #TABLE_STATISTIC_TRACK_E_LOGIN, array(,, 'login_date', 'logout_date');
+            TABLE_STATISTIC_TRACK_E_DOWNLOADS => array(
+                'course'    => 'down_cours_id',
+                'session'   => 'down_session_id',
+                'user'      => 'down_user_id',
+                'start_date'=> 'down_date',
+                ),
+            TABLE_STATISTIC_TRACK_E_LINKS => array(
+                'course'    => 'links_cours_id',
+                'session'   => 'links_session_id',
+                'user'      => 'links_user_id',
+                'start_date'=> 'links_date',
+                ),
+            TABLE_STATISTIC_TRACK_E_ONLINE => array(
+                'course'    => 'course',
+                'session'   => 'session_id',
+                'user'      => 'login_user_id',
+                'start_date'=> 'login_date',
+                ),
             #TABLE_STATISTIC_TRACK_E_HOTPOTATOES,
-            TABLE_STATISTIC_TRACK_E_COURSE_ACCESS => array('session_id', 'user_id'),
-            TABLE_STATISTIC_TRACK_E_EXERCICES => array('session_id', 'exe_user_id'),
-            TABLE_STATISTIC_TRACK_E_ATTEMPT => array('session_id', 'user_id'),
+            /*TABLE_STATISTIC_TRACK_E_COURSE_ACCESS => array(
+                'course'    => 'course_code',
+                'session'   => 'session_id',
+                'user'      => 'user_id',
+                'start_date'=> 'login_course_date',
+                'end_date'  => 'logout_course_date',
+                ),*/
+            TABLE_STATISTIC_TRACK_E_EXERCICES => array(
+                'course'    => 'exe_cours_id',
+                'session'   => 'session_id',
+                'user'      => 'exe_user_id',
+                'start_date'=> 'exe_date',
+                ),
+            TABLE_STATISTIC_TRACK_E_ATTEMPT => array(
+                'course'    => 'course_code',
+                'session'   => 'session_id',
+                'user'      => 'user_id',
+                'start_date'=> 'tms',
+                ),
             #TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING,
             #TABLE_STATISTIC_TRACK_E_DEFAULT,
-            TABLE_STATISTIC_TRACK_E_UPLOADS => array('upload_session_id', 'upload_user_id'),
+            TABLE_STATISTIC_TRACK_E_UPLOADS => array(
+                'course'    => 'upload_cours_id',
+                'session'   => 'upload_session_id',
+                'user'      => 'upload_user_id',
+                'start_date'=> 'upload_date',
+                ),
             #TABLE_STATISTIC_TRACK_E_HOTSPOT,
             #TABLE_STATISTIC_TRACK_E_ITEM_PROPERTY,
             #TABLE_STATISTIC_TRACK_E_OPEN,
             );
 
-        if (isset($_GET['session_id']) && !empty($_GET['session_id']))
-        {
-            $sessionId = intval($_GET['session_id']);
-        }
+        $error_sql = '';
+        foreach ($tables as $tableName => $fields) {
+            //If session is defined, add it to query
+            $where = '';
+            if (isset($sessionId) && !empty($sessionId)) {
+                $sessionField = $fields['session'];
+                $where .= " AND $sessionField = $sessionId";
+            }
 
-        foreach ($tables as $tableName => $fields)
-        {
-            $sql = sprintf('SELECT %s as user, count(*) as total FROM %s WHERE %s = %s GROUP BY %s', $fields[1], $tableName, $fields[0], $sessionId, $fields[1]);
+            //filter by date
+            if (!empty($date_from) && !empty($date_to)) {
+                $fieldStartDate = $fields['start_date'];
+                if (!isset($fields['end_date'])) {
+                    $where .= sprintf(" AND ($fieldStartDate BETWEEN '%s' AND '%s' )", $date_from, $date_to) ;  
+                } else {
+                    $fieldEndDate = $fields['end_date'];
+                    $where .= sprintf(" AND fieldStartDate >= '%s'
+                        AND $fieldEndDate <= '%s'", $date_from, $date_to);
+                }
+            }
+
+
+            //query
+            $sql = "SELECT %s as user, count(*) as total
+                FROM %s
+                WHERE %s = '%s'
+                AND %s = %s
+                $where
+                GROUP BY %s";
+            $sql = sprintf($sql, 
+                $fields['user'],    //user field
+                $tableName,         //FROM 
+                $fields['course'],  //course condition
+                $course['code'],    //course condition
+                $fields['user'],    //user condition
+                $userId,            //user condition
+                $fields['user']     //GROUP BY
+                );
             $rs = Database::query($sql);
+
+            //iterate query
             if (Database::num_rows($rs) > 0) {
                 while ($row = Database::fetch_array($rs)) {
                     $data[$row['user']] = (isset($data[$row['user']])) ?  $data[$row['user']] + $row[total]: $row['total'];
                 }
             }
         }
-
         return $data;
     }