Browse Source

Move, rename, change order and default values for function api_add_sub_hours_to_date_time to(base-time, interval, operation) ChamiloAPI::AddOrSubTimeToDateTime(interval, optional base-time, optional operation) - refs BT#12212
Change event_course_login() back to previous state (hack not necessary anymore)
Add reasonable documentation to new code
Expect IP address to be given to eventCourseVirtualLogin()

Yannick Warnier 8 years ago
parent
commit
a729ce8860

+ 0 - 28
main/inc/lib/api.lib.php

@@ -8095,31 +8095,3 @@ function api_number_format($number, $decimals = 0)
 
     return number_format($number, $decimals);
 }
-
-/**
- * Adds or Subtract a time in hh:mm:ss to a datetime
- *
- * @param $datetime
- * @param $time
- * @param $operation - True for Add, False to Subtract
- * @return string
- */
-function api_add_sub_hours_to_date_time($datetime, $time, $operation)
-{
-    $date = new DateTime($datetime);
-
-    $hours = $minutes = $seconds = 0;
-
-    sscanf($time, "%d:%d:%d", $hours, $minutes, $seconds);
-
-    $timeSeconds = isset($seconds) ? $hours * 3600 + $minutes * 60 + $seconds : $hours * 60 + $minutes;
-
-    if ($operation) {
-        $date->add(new DateInterval('PT' . $timeSeconds . 'S'));
-    } else {
-        $date->sub(new DateInterval('PT' . $timeSeconds . 'S'));
-    }
-
-
-    return $date->format('Y-m-d H:i:s');
-}

+ 66 - 23
main/inc/lib/events.lib.php

@@ -2,6 +2,7 @@
 /* See license terms in /license.txt */
 
 //use Chamilo\UserBundle\Entity\User;
+use Chamilo\CoreBundle\Component\Utils\ChamiloApi;
 
 /**
  * Class Event
@@ -1764,24 +1765,19 @@ class Event
     }
 
     /**
-     * User logs in for the first time to a course
-     * @param int $courseId
-     * @param int $user_id
-     * @param int $session_id
-     * @param string $virtualTime - Minor modification to add a virtual time in hh:mm:ss to the logout datetime - see BT#12212
+     * Registers in track_e_course_access when user logs in for the first time to a course
+     * @param int $courseId ID of the course
+     * @param int $user_id ID of the user
+     * @param int $session_id ID of the session (if any)
      */
-    public static function event_course_login($courseId, $user_id, $session_id, $virtualTime = '')
+    public static function event_course_login($courseId, $user_id, $session_id)
     {
         $course_tracking_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
         $loginDate = $logoutDate = api_get_utc_datetime();
 
+        //$counter represents the number of time this record has been refreshed
         $counter = 1;
 
-        if (!empty($virtualTime)) {
-            $logoutDate = api_add_sub_hours_to_date_time(api_get_utc_datetime(), $virtualTime, true);
-            $counter = 0;
-        }
-
         $courseId = intval($courseId);
         $user_id = intval($user_id);
         $session_id = intval($session_id);
@@ -1795,7 +1791,23 @@ class Event
         CourseManager::update_course_ranking(null, null, null, null, true, false);
     }
 
-    public static function eventCourseVirtualLogin($courseId, $userId, $sessionId, $virtualTime = '')
+    /**
+     * Register a "fake" time spent on the platform, for example to match the
+     * estimated time he took to author an assignment/work, see configuration
+     * setting considered_working_time.
+     * This assumes there is already some connection of the student to the
+     * course, otherwise he wouldn't be able to upload an assignment.
+     * This works by creating a new record, copy of the current one, then
+     * updating the current one to be just the considered_working_time and
+     * end at the same second as the user connected to the course.
+     * @param int $courseId The course in which to add the time
+     * @param int $userId The user for whom to add the time
+     * @param $sessionId The session in which to add the time (if any)
+     * @param string $virtualTime The amount of time to be added, in a hh:mm:ss format. If int, we consider it is expressed in hours.
+     * @param string $ip IP address to go on record for this time record
+     * @return True on successful insertion, false otherwise
+     */
+    public static function eventCourseVirtualLogin($courseId, $userId, $sessionId, $virtualTime = '', $ip = '')
     {
         $courseTrackingTable = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
         $time = $loginDate = $logoutDate = api_get_utc_datetime();
@@ -1803,8 +1815,10 @@ class Event
         $courseId = intval($courseId);
         $userId = intval($userId);
         $sessionId = intval($sessionId);
-        $ip = api_get_real_ip();
+        $ip = Database::escape_string($ip);
 
+        // Get the current latest course connection register. We need that
+        // record to re-use the data and create a new record.
         $sql = "SELECT *
                         FROM $courseTrackingTable
                         WHERE
@@ -1815,27 +1829,56 @@ class Event
                         ORDER BY login_course_date DESC LIMIT 0,1";
         $result = Database::query($sql);
 
+        // Ignore if we didn't find any course connection record in the last
+        // hour. In this case it wouldn't be right to add a "fake" time record.
         if (Database::num_rows($result) > 0) {
+            // Found the latest connection
             $row = Database::fetch_row($result);
             $courseAccessId = $row[0];
             $courseAccessLoginDate = $row[3];
-            $courseAccessLogoutDate = $row[4];
             $counter = $row[5];
             $counter = $counter ? $counter : 0;
-
-            $sql = "INSERT INTO $courseTrackingTable(c_id, user_ip, user_id, login_course_date, logout_course_date, counter, session_id)
-                VALUES('".$courseId."', '".$ip."', '".$userId."', '$courseAccessLoginDate', '$logoutDate', $counter, '".$sessionId."')";
+            // Insert a new record, copy of the current one (except the logout
+            // date that we update to the current time)
+            $sql = "INSERT INTO $courseTrackingTable(
+                    c_id,
+                    user_ip, 
+                    user_id, 
+                    login_course_date, 
+                    logout_course_date, 
+                    counter, 
+                    session_id
+                ) VALUES(
+                    $courseId, 
+                    '$ip', 
+                    $userId, 
+                    '$courseAccessLoginDate', 
+                    '$logoutDate', 
+                    $counter, 
+                    $sessionId
+                )";
             Database::query($sql);
 
-            $loginDate = api_add_sub_hours_to_date_time($courseAccessLoginDate, $virtualTime, false);
+            $loginDate = ChamiloApi::addOrSubTimeToDateTime(
+                $virtualTime,
+                $courseAccessLoginDate,
+                false
+            );
             // We update the course tracking table
             $sql = "UPDATE $courseTrackingTable  
-                            SET login_course_date = '$loginDate', logout_course_date = '$courseAccessLoginDate', counter = 0
-                            WHERE 
-                                course_access_id = ".intval($courseAccessId)." AND 
-                                session_id = ".$sessionId;
-            Database::query($sql);
+                        SET 
+                            login_course_date = '$loginDate',
+                            logout_course_date = '$courseAccessLoginDate',
+                            counter = 0
+                        WHERE 
+                            course_access_id = ".intval($courseAccessId)." AND 
+                            session_id = ".$sessionId;
+            $result = Database::query($sql);
+
+            return $result;
         }
+
+        return false;
     }
 
     /**

+ 16 - 3
main/work/work.lib.php

@@ -3809,9 +3809,18 @@ function processWorkForm(
             sendAlertToUsers($workId, $courseInfo, $sessionId);
             Event::event_upload($workId);
 
+            // The following feature requires the creation of a work-type
+            // extra_field and the following setting in the configuration file
+            // (until moved to the database). It allows te teacher to set a
+            // "considered work time", meaning the time we assume a student
+            // would have spent, approximately, to prepare the task before
+            // handing it in Chamilo, adding this time to the student total
+            // course use time, as a register of time spent *before* his
+            // connection to the platform to hand the work in.
             $consideredWorkingTime = api_get_configuration_value('considered_working_time');
 
             if ($consideredWorkingTime) {
+                // Get the "considered work time" defined for this work
                 $fieldValue = new ExtraFieldValue('work');
                 $resultExtra = $fieldValue->getAllValuesForAnItem(
                     $workInfo['id'],
@@ -3828,8 +3837,10 @@ function processWorkForm(
                     }
                 }
 
+                // If no time was defined, or a time of "0" was set, do nothing
                 if (!empty($workingTime)) {
-
+                    // If some time is set, get the list of docs handed in by
+                    // this student (to make sure we count the time only once)
                     $userWorks = get_work_user_list(
                         0,
                         100,
@@ -3844,8 +3855,10 @@ function processWorkForm(
                     );
 
                     if (count($userWorks) == 1) {
-                        Event::eventCourseVirtualLogin($courseId, $userId, $sessionId, $workingTime);
-
+                        // The student only uploaded one doc so far, so add the
+                        // considered work time to his course connection time
+                        $ip = api_get_real_ip();
+                        Event::eventCourseVirtualLogin($courseId, $userId, $sessionId, $workingTime, $ip);
                     }
                 }
             }

+ 26 - 0
src/Chamilo/CoreBundle/Component/Utils/ChamiloApi.php

@@ -146,4 +146,30 @@ class ChamiloApi
         }
         return $string;
     }
+    /**
+     * Adds or Subtract a time in hh:mm:ss to a datetime
+     * @param string $time Time in hh:mm:ss format
+     * @param string $datetime Datetime as accepted by the Datetime class constructor
+     * @param bool $operation True for Add, False to Subtract
+     * @return string
+     */
+    public static function addOrSubTimeToDateTime($time, $datetime = 'now', $operation = true)
+    {
+        $date = new \DateTime($datetime);
+
+        $hours = $minutes = $seconds = 0;
+
+        sscanf($time, "%d:%d:%d", $hours, $minutes, $seconds);
+
+        $timeSeconds = isset($seconds) ? $hours * 3600 + $minutes * 60 + $seconds : $hours * 60 + $minutes;
+
+        if ($operation) {
+            $date->add(new \DateInterval('PT' . $timeSeconds . 'S'));
+        } else {
+            $date->sub(new \DateInterval('PT' . $timeSeconds . 'S'));
+        }
+
+
+        return $date->format('Y-m-d H:i:s');
+    }
 }