24 return (
bool) $ilSetting->get(
'session_statistics', 1);
40 public static function createRawEntry($a_session_id, $a_session_type, $a_timestamp, $a_user_id)
44 if (!$a_user_id || !$a_session_id || !self::isActive()) {
52 "usr_session_stats_raw",
54 "session_id" =>
array(
"text", $a_session_id)
57 "type" =>
array(
"integer", $a_session_type),
58 "start_time" =>
array(
"integer", $a_timestamp),
59 "user_id" =>
array(
"integer", $a_user_id)
71 public static function closeRawEntry($a_session_id, $a_context = null, $a_expired_at = null)
75 if (!self::isActive()) {
80 if (!is_array($a_session_id)) {
82 $end_time = $a_expired_at;
86 $sql =
"UPDATE usr_session_stats_raw" .
87 " SET end_time = " . $ilDB->quote($end_time,
"integer");
89 $sql .=
",end_context = " . $ilDB->quote($a_context,
"integer");
91 $sql .=
" WHERE session_id = " . $ilDB->quote($a_session_id,
"text") .
92 " AND end_time IS NULL";
93 $ilDB->manipulate($sql);
96 elseif (!$a_expired_at) {
97 $sql =
"UPDATE usr_session_stats_raw" .
98 " SET end_time = " . $ilDB->quote(
time(),
"integer");
100 $sql .=
",end_context = " . $ilDB->quote($a_context,
"integer");
102 $sql .=
" WHERE " . $ilDB->in(
"session_id", $a_session_id,
false,
"text") .
103 " AND end_time IS NULL";
104 $ilDB->manipulate($sql);
108 foreach ($a_session_id as
$id => $ts) {
109 $sql =
"UPDATE usr_session_stats_raw" .
110 " SET end_time = " . $ilDB->quote($ts,
"integer");
112 $sql .=
",end_context = " . $ilDB->quote($a_context,
"integer");
114 $sql .=
" WHERE session_id = " . $ilDB->quote(
$id,
"text") .
115 " AND end_time IS NULL";
116 $ilDB->manipulate($sql);
132 $sql =
"SELECT MAX(slot_end) previous_slot_end" .
133 " FROM usr_session_stats";
134 $res = $ilDB->query($sql);
136 $previous_slot_end =
$row[
"previous_slot_end"];
140 if (!$previous_slot_end) {
141 $slot = floor(
date(
"i")/self::SLOT_SIZE);
144 $current_slot_begin = mktime(
date(
"H", $a_now)-1, 60-self::SLOT_SIZE, 0);
148 $current_slot_begin = mktime(
date(
"H", $a_now), ($slot-1)*self::SLOT_SIZE, 0);
151 $current_slot_begin = $previous_slot_end+1;
154 $current_slot_end = $current_slot_begin+(60*self::SLOT_SIZE)-1;
157 if ($current_slot_end < $a_now) {
158 return array($current_slot_begin, $current_slot_end);
172 $sql =
"SELECT COUNT(*) counter FROM usr_session_stats_raw" .
173 " WHERE (end_time IS NULL OR end_time >= " . $ilDB->quote($a_time,
"integer") .
")" .
174 " AND start_time <= " . $ilDB->quote($a_time,
"integer") .
176 $res = $ilDB->query($sql);
178 return $row[
"counter"];
192 $sql =
"SELECT start_time,end_time,end_context FROM usr_session_stats_raw" .
193 " WHERE start_time <= " . $ilDB->quote($a_end,
"integer") .
194 " AND (end_time IS NULL OR end_time >= " . $ilDB->quote($a_begin,
"integer") .
")" .
196 " ORDER BY start_time";
197 $res = $ilDB->query($sql);
199 while (
$row = $ilDB->fetchAssoc(
$res)) {
215 $ilAtomQuery = $ilDB->buildAtomQuery();
216 $ilAtomQuery->addTableLock(
"usr_session_stats");
218 $ilAtomQuery->addQueryCallable(
function (
ilDBInterface $ilDB) use ($a_now, &$slot) {
221 $slot = self::getCurrentSlot($a_now);
222 if (!is_array($slot)) {
229 "slot_begin" =>
array(
"integer", $slot[0]),
230 "slot_end" =>
array(
"integer", $slot[1]),
232 $ilDB->
insert(
"usr_session_stats", $fields);
247 if (!self::isActive()) {
251 $slot = self::createNewAggregationSlot($a_now);
252 while (is_array($slot)) {
253 self::aggregateRawHelper($slot[0], $slot[1]);
254 $slot = self::createNewAggregationSlot($a_now);
258 self::deleteAggregatedRaw($a_now);
280 $closed_counter = $events =
array();
282 foreach (self::getRawData($a_begin, $a_end) as $item) {
291 if ($item[
"start_time"] >= $a_begin) {
293 $events[$item[
"start_time"]][] = 1;
296 if ($item[
"end_time"] && $item[
"end_time"] <= $a_end) {
297 if (in_array($item[
"end_context"], $separate_closed)) {
298 $closed_counter[$item[
"end_context"]]++;
300 $closed_counter[0]++;
302 $events[$item[
"end_time"]][] = -1;
307 $active_begin = self::getNumberOfActiveRawSessions($a_begin-1);
308 $active_end = $active_min = $active_max = $active_avg = $active_begin;
311 if (
sizeof($events)) {
312 $last_update_avg = $a_begin-1;
313 $slot_seconds = self::SLOT_SIZE*60;
318 foreach ($events as $ts => $actions) {
320 foreach ($actions as
$action) {
332 if ($active_end > $active_max) {
333 $active_max = $active_end;
337 if ($active_end < $active_min) {
338 $active_min = $active_end;
342 $diff = $ts-$last_update_avg;
343 $active_avg += $diff/$slot_seconds*$active_end;
344 $last_update_avg = $ts;
349 if ($last_update_avg < $a_end) {
350 $diff = $a_end-$last_update_avg;
351 $active_avg += $diff/$slot_seconds*$active_end;
354 $active_avg = round($active_avg);
361 $max_sessions = self::getLimitForSlot($a_begin);
365 "active_min" =>
array(
"integer", $active_min),
366 "active_max" =>
array(
"integer", $active_max),
367 "active_avg" =>
array(
"integer", $active_avg),
368 "active_end" =>
array(
"integer", $active_end),
369 "opened" =>
array(
"integer", $opened_counter),
376 "closed_misc" =>
array(
"integer", (
int) $closed_counter[0]),
377 "max_sessions" =>
array(
"integer", (
int) $max_sessions)
382 array(
"slot_begin" =>
array(
"integer", $a_begin),
383 "slot_end" =>
array(
"integer", $a_end))
397 $cut = $a_now-(60*60*24*7);
399 $ilDB->manipulate(
"DELETE FROM usr_session_stats_raw" .
400 " WHERE start_time <= " . $ilDB->quote($cut,
"integer"));
412 $sql =
"SELECT max(slot_end) latest FROM usr_session_stats" .
413 " WHERE active_max >= max_sessions" .
414 " AND max_sessions > " . $ilDB->quote(0,
"integer");
415 $res = $ilDB->query($sql);
417 if (
$row[
"latest"]) {
418 return $row[
"latest"];
433 $sql =
"SELECT SUM(slot_end-slot_begin) dur FROM usr_session_stats" .
434 " WHERE active_max >= max_sessions" .
435 " AND max_sessions > " . $ilDB->quote(0,
"integer") .
436 " AND slot_end > " . $ilDB->quote($a_from,
"integer") .
437 " AND slot_begin < " . $ilDB->quote($a_to,
"integer");
438 $res = $ilDB->query($sql);
456 $sql =
"SELECT SUM(opened) opened, SUM(closed_manual) closed_manual," .
457 " SUM(closed_expire) closed_expire, SUM(closed_idle) closed_idle," .
458 " SUM(closed_idle_first) closed_idle_first, SUM(closed_limit) closed_limit," .
459 " SUM(closed_login) closed_login, SUM(closed_misc) closed_misc" .
460 " FROM usr_session_stats" .
461 " WHERE slot_end > " . $ilDB->quote($a_from,
"integer") .
462 " AND slot_begin < " . $ilDB->quote($a_to,
"integer");
463 $res = $ilDB->query($sql);
464 return $ilDB->fetchAssoc(
$res);
478 $sql =
"SELECT slot_begin, slot_end, active_min, active_max, active_avg," .
480 " FROM usr_session_stats" .
481 " WHERE slot_end > " . $ilDB->quote($a_from,
"integer") .
482 " AND slot_begin < " . $ilDB->quote($a_to,
"integer") .
483 " ORDER BY slot_begin";
484 $res = $ilDB->query($sql);
486 while (
$row = $ilDB->fetchAssoc(
$res)) {
501 $sql =
"SELECT max(slot_end) latest FROM usr_session_stats";
502 $res = $ilDB->query($sql);
504 if (
$row[
"latest"]) {
505 return $row[
"latest"];
520 $sql =
"SELECT maxval FROM usr_session_log" .
521 " WHERE tstamp <= " . $ilDB->quote($a_timestamp,
"integer") .
522 " ORDER BY tstamp DESC";
523 $res = $ilDB->query($sql);
524 $val = $ilDB->fetchAssoc(
$res);
525 if ($val[
"maxval"]) {
526 return (
int) $val[
"maxval"];
541 $new_value = (int) $a_new_value;
544 if ($new_value != $old_value) {
547 "maxval" =>
array(
"integer", $new_value),
548 "user_id" =>
array(
"integer", $ilUser->getId())
550 $ilDB->insert(
"usr_session_log", $fields);
static getNumberOfActiveRawSessions($a_time)
Count number of active sessions at given time.
const DEFAULT_MAX_COUNT
default value for settings that have not been defined in setup or administration yet ...
static getMaxedOutDuration($a_from, $a_to)
Get maxed out duration in given timeframe.
static deleteAggregatedRaw($a_now)
Remove already aggregated raw data.
static getCurrentSlot($a_now)
Get next slot to aggregate.
static $session_types_controlled
const SESSION_CLOSE_LOGIN
if(!array_key_exists('StateId', $_REQUEST)) $id
static getNumberOfSessionsByType($a_from, $a_to)
Get session counters by type (opened, closed)
static createNewAggregationSlot($a_now)
Create new slot (using table lock)
const SESSION_CLOSE_EXPIRE
static updateLimitLog($a_new_value)
Log max session setting.
static getLastAggregation()
Get timestamp of last aggregation.
static aggregateRawHelper($a_begin, $a_end)
Aggregate statistics data for one slot.
static getLimitForSlot($a_timestamp)
Get max session setting for given timestamp.
foreach($_POST as $key=> $value) $res
date( 'd-M-Y', $objPHPExcel->getProperties() ->getCreated())
static createRawEntry($a_session_id, $a_session_type, $a_timestamp, $a_user_id)
Create raw data entry.
const SESSION_CLOSE_LIMIT
Create styles array
The data for the language used.
static getRawData($a_begin, $a_end)
Read raw data for timespan.
static isActive()
Is session statistics active at all?
insert($table_name, $values)
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
static getLastMaxedOut()
Get latest slot during which sessions were maxed out.
static aggretateRaw($a_now)
Aggregate raw session data (older than given time)
const SESSION_CLOSE_FIRST
static closeRawEntry($a_session_id, $a_context=null, $a_expired_at=null)
Close raw data entry.
static getActiveSessions($a_from, $a_to)
Get active sessions aggregated data.