diff --git a/main/inc/lib/api.lib.php b/main/inc/lib/api.lib.php index 3022a00b33..75e157e13c 100644 --- a/main/inc/lib/api.lib.php +++ b/main/inc/lib/api.lib.php @@ -8269,31 +8269,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'); -} diff --git a/main/inc/lib/events.lib.php b/main/inc/lib/events.lib.php index 72e5b6335f..9333ab413d 100644 --- a/main/inc/lib/events.lib.php +++ b/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 @@ -1731,24 +1732,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); @@ -1762,7 +1758,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(); @@ -1770,8 +1782,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 @@ -1782,27 +1796,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; } /** diff --git a/main/work/work.lib.php b/main/work/work.lib.php index 886f8ffe6d..2f4d44a1cf 100755 --- a/main/work/work.lib.php +++ b/main/work/work.lib.php @@ -3804,9 +3804,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'], @@ -3823,8 +3832,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, @@ -3839,8 +3850,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); } } }