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()
1.10.x
Yannick Warnier 9 years ago committed by Angel Fernando Quiroz Campos
parent ab402b95ee
commit d10d3bbf5b
  1. 28
      main/inc/lib/api.lib.php
  2. 89
      main/inc/lib/events.lib.php
  3. 19
      main/work/work.lib.php

@ -8269,31 +8269,3 @@ function api_number_format($number, $decimals = 0)
return number_format($number, $decimals); 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');
}

@ -2,6 +2,7 @@
/* See license terms in /license.txt */ /* See license terms in /license.txt */
//use Chamilo\UserBundle\Entity\User; //use Chamilo\UserBundle\Entity\User;
use Chamilo\CoreBundle\Component\Utils\ChamiloApi;
/** /**
* Class Event * Class Event
@ -1731,24 +1732,19 @@ class Event
} }
/** /**
* User logs in for the first time to a course * Registers in track_e_course_access when user logs in for the first time to a course
* @param int $courseId * @param int $courseId ID of the course
* @param int $user_id * @param int $user_id ID of the user
* @param int $session_id * @param int $session_id ID of the session (if any)
* @param string $virtualTime - Minor modification to add a virtual time in hh:mm:ss to the logout datetime - see BT#12212
*/ */
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); $course_tracking_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
$loginDate = $logoutDate = api_get_utc_datetime(); $loginDate = $logoutDate = api_get_utc_datetime();
//$counter represents the number of time this record has been refreshed
$counter = 1; $counter = 1;
if (!empty($virtualTime)) {
$logoutDate = api_add_sub_hours_to_date_time(api_get_utc_datetime(), $virtualTime, true);
$counter = 0;
}
$courseId = intval($courseId); $courseId = intval($courseId);
$user_id = intval($user_id); $user_id = intval($user_id);
$session_id = intval($session_id); $session_id = intval($session_id);
@ -1762,7 +1758,23 @@ class Event
CourseManager::update_course_ranking(null, null, null, null, true, false); 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); $courseTrackingTable = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
$time = $loginDate = $logoutDate = api_get_utc_datetime(); $time = $loginDate = $logoutDate = api_get_utc_datetime();
@ -1770,8 +1782,10 @@ class Event
$courseId = intval($courseId); $courseId = intval($courseId);
$userId = intval($userId); $userId = intval($userId);
$sessionId = intval($sessionId); $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 * $sql = "SELECT *
FROM $courseTrackingTable FROM $courseTrackingTable
WHERE WHERE
@ -1782,27 +1796,56 @@ class Event
ORDER BY login_course_date DESC LIMIT 0,1"; ORDER BY login_course_date DESC LIMIT 0,1";
$result = Database::query($sql); $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) { if (Database::num_rows($result) > 0) {
// Found the latest connection
$row = Database::fetch_row($result); $row = Database::fetch_row($result);
$courseAccessId = $row[0]; $courseAccessId = $row[0];
$courseAccessLoginDate = $row[3]; $courseAccessLoginDate = $row[3];
$courseAccessLogoutDate = $row[4];
$counter = $row[5]; $counter = $row[5];
$counter = $counter ? $counter : 0; $counter = $counter ? $counter : 0;
// Insert a new record, copy of the current one (except the logout
$sql = "INSERT INTO $courseTrackingTable(c_id, user_ip, user_id, login_course_date, logout_course_date, counter, session_id) // date that we update to the current time)
VALUES('".$courseId."', '".$ip."', '".$userId."', '$courseAccessLoginDate', '$logoutDate', $counter, '".$sessionId."')"; $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); 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 // We update the course tracking table
$sql = "UPDATE $courseTrackingTable $sql = "UPDATE $courseTrackingTable
SET login_course_date = '$loginDate', logout_course_date = '$courseAccessLoginDate', counter = 0 SET
WHERE login_course_date = '$loginDate',
course_access_id = ".intval($courseAccessId)." AND logout_course_date = '$courseAccessLoginDate',
session_id = ".$sessionId; counter = 0
Database::query($sql); WHERE
course_access_id = ".intval($courseAccessId)." AND
session_id = ".$sessionId;
$result = Database::query($sql);
return $result;
} }
return false;
} }
/** /**

@ -3804,9 +3804,18 @@ function processWorkForm(
sendAlertToUsers($workId, $courseInfo, $sessionId); sendAlertToUsers($workId, $courseInfo, $sessionId);
Event::event_upload($workId); 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'); $consideredWorkingTime = api_get_configuration_value('considered_working_time');
if ($consideredWorkingTime) { if ($consideredWorkingTime) {
// Get the "considered work time" defined for this work
$fieldValue = new ExtraFieldValue('work'); $fieldValue = new ExtraFieldValue('work');
$resultExtra = $fieldValue->getAllValuesForAnItem( $resultExtra = $fieldValue->getAllValuesForAnItem(
$workInfo['id'], $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 (!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( $userWorks = get_work_user_list(
0, 0,
100, 100,
@ -3839,8 +3850,10 @@ function processWorkForm(
); );
if (count($userWorks) == 1) { 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);
} }
} }
} }

Loading…
Cancel
Save