Browse Source

Add support for work removal when using considered_working_time setting - refs BT#12212
Rename eventCoutseVirtualLogin() to eventAddVirtualCourseTime()

Yannick Warnier 8 years ago
parent
commit
0e77c2ed48
2 changed files with 102 additions and 3 deletions
  1. 65 1
      main/inc/lib/events.lib.php
  2. 37 2
      main/work/work.lib.php

+ 65 - 1
main/inc/lib/events.lib.php

@@ -1807,7 +1807,7 @@ class Event
      * @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 = '')
+    public static function eventAddVirtualCourseTime($courseId, $userId, $sessionId, $virtualTime = '', $ip = '')
     {
         $courseTrackingTable = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
         $time = $loginDate = $logoutDate = api_get_utc_datetime();
@@ -1880,6 +1880,70 @@ class Event
 
         return false;
     }
+    /**
+     * Removes 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 method should be called when something that generated a fake
+     * time record is removed. Given the database link is weak (no real
+     * relationship kept between the deleted item and this record), this
+     * method just looks for the latest record that has the same time as the
+     * item's fake time, is in the past and in this course+session. If such a
+     * record cannot be found, it doesn't do anything.
+     * The IP address is not considered a useful filter here.
+     * @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.
+     * @return True on successful removal, false otherwise
+     */
+    public static function eventRemoveVirtualCourseTime($courseId, $userId, $sessionId = 0, $virtualTime = '')
+    {
+        if (empty($virtualTime)) {
+            return false;
+        }
+        $courseTrackingTable = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
+        $time = $loginDate = $logoutDate = api_get_utc_datetime();
+
+        $courseId = intval($courseId);
+        $userId = intval($userId);
+        $sessionId = intval($sessionId);
+        // Change $virtualTime format from hh:mm:ss to hhmmss which is the
+        // format returned by SQL for a subtraction of two datetime values
+        // @todo make sure this is portable between DBMSes
+        if (preg_match('/:/', $virtualTime)) {
+            $virtualTime = preg_replace('/:/', '', $virtualTime);
+        } else {
+            $virtualTime *= 10000;
+        }
+
+        // Get the current latest course connection register. We need that
+        // record to re-use the data and create a new record.
+        $sql = "SELECT course_access_id
+                        FROM $courseTrackingTable
+                        WHERE
+                            user_id = $userId AND
+                            c_id = $courseId  AND
+                            session_id  = $sessionId AND
+                            counter = 0 AND
+                            logout_course_date - login_course_date = '$virtualTime'
+                        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];
+            $sql = "DELETE FROM $courseTrackingTable WHERE course_access_id = $courseAccessId";
+            $result = Database::query($sql);
+
+            return $result;
+        }
+
+        return false;
+    }
 
     /**
      * For the sake of genericity, this function is a switch.

+ 37 - 2
main/work/work.lib.php

@@ -3858,7 +3858,7 @@ function processWorkForm(
                         // 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);
+                        Event::eventAddVirtualCourseTime($courseId, $userId, $sessionId, $workingTime, $ip);
                     }
                 }
             }
@@ -4287,12 +4287,46 @@ function deleteWorkItem($item_id, $courseInfo)
         )
     ) {
         // We found the current user is the author
-        $sql = "SELECT url, contains_file FROM $work_table
+        $sql = "SELECT url, contains_file, user_id, session_id, parent_id FROM $work_table
                 WHERE c_id = $course_id AND id = $item_id";
         $result = Database::query($sql);
         $row = Database::fetch_array($result);
 
         if (Database::num_rows($result) > 0) {
+
+            // If the "considered_working_time" option is enabled, check
+            // whether some time should be removed from track_e_course_access
+            $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(
+                    $row['parent_id'],
+                    true
+                );
+
+                $workingTime = null;
+                foreach ($resultExtra as $field) {
+                    $field = $field['value'];
+
+                    if ($consideredWorkingTime == $field->getField()->getVariable()) {
+                        $workingTime = $field->getValue();
+                    }
+                }
+                // If no time was defined, or a time of "0" was set, do nothing
+                if (!empty($workingTime)) {
+                    $sessionId = empty($row['session_id']) ? 0 : $row['session_id'];
+                    // Getting false from the following call would mean the
+                    // time record
+                    $removalResult = Event::eventRemoveVirtualCourseTime(
+                        $course_id,
+                        $row['user_id'],
+                        $sessionId,
+                        $workingTime
+                    );
+                }
+            } // fin de sección sobre considered_working_time
+
             $sql = "UPDATE $work_table SET active = 2
                     WHERE c_id = $course_id AND id = $item_id";
             Database::query($sql);
@@ -4344,6 +4378,7 @@ function deleteWorkItem($item_id, $courseInfo)
             }
         }
     }
+
     return $file_deleted;
 }