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())
53 "usr_session_stats_raw",
55 "session_id" => array(
"text", $a_session_id)
58 "type" => array(
"integer", $a_session_type),
59 "start_time" => array(
"integer", $a_timestamp),
60 "user_id" => array(
"integer", $a_user_id)
72 public static function closeRawEntry($a_session_id, $a_context = null, $a_expired_at = null)
82 if(!is_array($a_session_id))
86 $end_time = $a_expired_at;
92 $sql =
"UPDATE usr_session_stats_raw".
93 " SET end_time = ".$ilDB->quote($end_time,
"integer");
96 $sql .=
",end_context = ".$ilDB->quote($a_context,
"integer");
98 $sql .=
" WHERE session_id = ".$ilDB->quote($a_session_id,
"text").
99 " AND end_time IS NULL";
100 $ilDB->manipulate($sql);
103 else if(!$a_expired_at)
105 $sql =
"UPDATE usr_session_stats_raw".
106 " SET end_time = ".$ilDB->quote(time(),
"integer");
109 $sql .=
",end_context = ".$ilDB->quote($a_context,
"integer");
111 $sql .=
" WHERE ".$ilDB->in(
"session_id", $a_session_id,
false,
"text").
112 " AND end_time IS NULL";
113 $ilDB->manipulate($sql);
118 foreach($a_session_id as $id => $ts)
120 $sql =
"UPDATE usr_session_stats_raw".
121 " SET end_time = ".$ilDB->quote($ts,
"integer");
124 $sql .=
",end_context = ".$ilDB->quote($a_context,
"integer");
126 $sql .=
" WHERE session_id = ".$ilDB->quote($id,
"text").
127 " AND end_time IS NULL";
128 $ilDB->manipulate($sql);
144 $sql =
"SELECT MAX(slot_end) previous_slot_end".
145 " FROM usr_session_stats";
146 $res = $ilDB->query($sql);
148 $previous_slot_end =
$row[
"previous_slot_end"];
152 if(!$previous_slot_end)
154 $slot = floor(date(
"i")/self::SLOT_SIZE);
158 $current_slot_begin = mktime(date(
"H", $a_now)-1, 60-self::SLOT_SIZE, 0);
163 $current_slot_begin = mktime(date(
"H", $a_now), ($slot-1)*self::SLOT_SIZE, 0);
168 $current_slot_begin = $previous_slot_end+1;
174 if($current_slot_end < $a_now)
176 return array($current_slot_begin, $current_slot_end);
190 $sql =
"SELECT COUNT(*) counter FROM usr_session_stats_raw".
191 " WHERE (end_time IS NULL OR end_time >= ".$ilDB->quote($a_time,
"integer").
")".
192 " AND start_time <= ".$ilDB->quote($a_time,
"integer").
194 $res = $ilDB->query($sql);
196 return $row[
"counter"];
210 $sql =
"SELECT start_time,end_time,end_context FROM usr_session_stats_raw".
211 " WHERE start_time <= ".$ilDB->quote($a_end,
"integer").
212 " AND (end_time IS NULL OR end_time >= ".$ilDB->quote($a_begin,
"integer").
")".
214 " ORDER BY start_time";
215 $res = $ilDB->query($sql);
217 while(
$row = $ilDB->fetchAssoc(
$res))
235 $ilDB->lockTables(array(array(
"name"=>
"usr_session_stats",
"type"=>
ilDB::LOCK_WRITE)));
241 $ilDB->unlockTables();
247 "slot_begin" => array(
"integer", $slot[0]),
248 "slot_end" => array(
"integer", $slot[1]),
250 $ilDB->insert(
"usr_session_stats", $fields);
252 $ilDB->unlockTables();
264 if(!self::isActive())
270 while(is_array($slot))
299 $closed_counter = $events = array();
301 foreach(self::getRawData($a_begin, $a_end) as $item)
311 if($item[
"start_time"] >= $a_begin)
314 $events[$item[
"start_time"]][] = 1;
317 if($item[
"end_time"] && $item[
"end_time"] <= $a_end)
319 if(in_array($item[
"end_context"], $separate_closed))
321 $closed_counter[$item[
"end_context"]]++;
325 $closed_counter[0]++;
327 $events[$item[
"end_time"]][] = -1;
333 $active_end = $active_min = $active_max = $active_avg = $active_begin;
338 $last_update_avg = $a_begin-1;
339 $slot_seconds = self::SLOT_SIZE*60;
344 foreach($events as $ts => $actions)
347 foreach($actions as $action)
362 if($active_end > $active_max)
364 $active_max = $active_end;
368 if($active_end < $active_min)
370 $active_min = $active_end;
374 $diff = $ts-$last_update_avg;
375 $active_avg += $diff/$slot_seconds*$active_end;
376 $last_update_avg = $ts;
381 if($last_update_avg < $a_end)
383 $diff = $a_end-$last_update_avg;
384 $active_avg += $diff/$slot_seconds*$active_end;
387 $active_avg = round($active_avg);
398 "active_min" => array(
"integer", $active_min),
399 "active_max" => array(
"integer", $active_max),
400 "active_avg" => array(
"integer", $active_avg),
401 "active_end" => array(
"integer", $active_end),
402 "opened" => array(
"integer", $opened_counter),
409 "closed_misc" => array(
"integer", (
int)$closed_counter[0]),
410 "max_sessions" => array(
"integer", (
int)$max_sessions)
412 $ilDB->update(
"usr_session_stats", $fields,
413 array(
"slot_begin" => array(
"integer", $a_begin),
414 "slot_end" => array(
"integer", $a_end)));
427 $cut = $a_now-(60*60*24*7);
429 $ilDB->manipulate(
"DELETE FROM usr_session_stats_raw".
430 " WHERE start_time <= ".$ilDB->quote($cut,
"integer"));
442 $sql =
"SELECT max(slot_end) latest FROM usr_session_stats".
443 " WHERE active_max >= max_sessions".
444 " AND max_sessions > ".$ilDB->quote(0,
"integer");
445 $res = $ilDB->query($sql);
449 return $row[
"latest"];
464 $sql =
"SELECT SUM(slot_end-slot_begin) dur FROM usr_session_stats".
465 " WHERE active_max >= max_sessions".
466 " AND max_sessions > ".$ilDB->quote(0,
"integer").
467 " AND slot_end > ".$ilDB->quote($a_from,
"integer").
468 " AND slot_begin < ".$ilDB->quote($a_to,
"integer");
469 $res = $ilDB->query($sql);
488 $sql =
"SELECT SUM(opened) opened, SUM(closed_manual) closed_manual,".
489 " SUM(closed_expire) closed_expire, SUM(closed_idle) closed_idle,".
490 " SUM(closed_idle_first) closed_idle_first, SUM(closed_limit) closed_limit,".
491 " SUM(closed_login) closed_login, SUM(closed_misc) closed_misc".
492 " FROM usr_session_stats".
493 " WHERE slot_end > ".$ilDB->quote($a_from,
"integer").
494 " AND slot_begin < ".$ilDB->quote($a_to,
"integer");
495 $res = $ilDB->query($sql);
496 return $ilDB->fetchAssoc(
$res);
510 $sql =
"SELECT slot_begin, slot_end, active_min, active_max, active_avg,".
512 " FROM usr_session_stats".
513 " WHERE slot_end > ".$ilDB->quote($a_from,
"integer").
514 " AND slot_begin < ".$ilDB->quote($a_to,
"integer").
515 " ORDER BY slot_begin";
516 $res = $ilDB->query($sql);
518 while(
$row = $ilDB->fetchAssoc(
$res))
534 $sql =
"SELECT max(slot_end) latest FROM usr_session_stats";
535 $res = $ilDB->query($sql);
539 return $row[
"latest"];
554 $sql =
"SELECT maxval FROM usr_session_log".
555 " WHERE tstamp <= ".$ilDB->quote($a_timestamp,
"integer").
556 " ORDER BY tstamp DESC";
557 $res = $ilDB->query($sql);
558 $val = $ilDB->fetchAssoc(
$res);
561 return (
int)$val[
"maxval"];
578 $new_value = (int)$a_new_value;
581 if($new_value != $old_value)
584 "tstamp" => array(
"timestamp", time()),
585 "maxval" => array(
"integer", $new_value),
586 "user_id" => array(
"integer", $ilUser->getId())
588 $ilDB->insert(
"usr_session_log", $fields);