Skip to content

Commit 4cecc04

Browse files
committed
Add url(portal)/session/user stats see BT#14192
1 parent 2d0be54 commit 4cecc04

File tree

3 files changed

+127
-14
lines changed

3 files changed

+127
-14
lines changed

main/admin/statistics/index.php

Lines changed: 90 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,27 @@
1616
$report = isset($_REQUEST['report']) ? $_REQUEST['report'] : '';
1717
$sessionDuration = isset($_GET['session_duration']) ? (int) $_GET['session_duration'] : '';
1818

19-
if ($report) {
19+
if ($report == 'recentlogins') {
2020
$htmlHeadXtra[] = api_get_js('chartjs/Chart.min.js');
2121
$htmlHeadXtra[] = '
2222
<script>
23-
$(document).ready(function() {
24-
$.ajax({
25-
url: "'.api_get_path(WEB_CODE_PATH).'inc/ajax/statistics.ajax.php?a=recentlogins&session_duration='.$sessionDuration.'",
26-
type: "POST",
27-
success: function(data) {
28-
Chart.defaults.global.responsive = true;
29-
var myLine = new Chart(document.getElementById("canvas").getContext("2d")).Line(data);
30-
}
23+
$(document).ready(function() {
24+
$.ajax({
25+
url: "'.api_get_path(WEB_CODE_PATH).'inc/ajax/statistics.ajax.php?a=recentlogins&session_duration='.$sessionDuration.'",
26+
type: "POST",
27+
success: function(data) {
28+
Chart.defaults.global.responsive = true;
29+
var myLine = new Chart(document.getElementById("canvas").getContext("2d")).Line(data);
30+
}
3131
});
3232
});
3333
</script>';
3434
}
3535

36+
if ($report == 'user_session') {
37+
$htmlHeadXtra[] = api_get_jqgrid_js();
38+
}
39+
3640
$tool_name = get_lang('Statistics');
3741
Display::display_header($tool_name);
3842
echo Display::page_header($tool_name);
@@ -62,6 +66,10 @@
6266
// system ...
6367
$tools[$strSystem]['report=activities'] = get_lang('ImportantActivities');
6468

69+
if (api_is_global_platform_admin()) {
70+
$tools[$strSystem]['report=user_session'] = get_lang('PortalUserSessionStats');
71+
}
72+
6573
// social ...
6674
$tools[$strSocial]['report=messagesent'] = get_lang('MessagesSent');
6775
$tools[$strSocial]['report=messagereceived'] = get_lang('MessagesReceived');
@@ -81,9 +89,81 @@
8189
echo '</tr></table>';
8290

8391
$course_categories = Statistics::getCourseCategories();
84-
echo '<br/><br/>'; //@todo: spaces between elements should be handled in the css, br should be removed if only there for presentation
92+
//@todo: spaces between elements should be handled in the css, br should be removed if only there for presentation
93+
echo '<br/><br/>';
8594

8695
switch ($report) {
96+
case 'user_session':
97+
$form = new FormValidator('user_session', 'get');
98+
$form->addDateRangePicker('range', get_lang('DateRange'), true);
99+
$form->addHidden('report', 'user_session');
100+
$form->addButtonSearch(get_lang('Search'));
101+
102+
$date = new DateTime($now);
103+
$startDate = $date->format('Y-m-d').' 00:00:00';
104+
$endDate = $date->format('Y-m-d').' 23:59:59';
105+
$start = $startDate;
106+
$end = $endDate;
107+
108+
if ($form->validate()) {
109+
$values = $form->getSubmitValues();
110+
$start = $values['range_start'];
111+
$end = $values['range_end'];
112+
}
113+
echo $form->returnForm();
114+
115+
$url = api_get_path(WEB_AJAX_PATH).'statistics.ajax.php?a=get_user_session&start='.$start.'&end='.$end;
116+
$columns = [
117+
'URL',
118+
get_lang('Session'),
119+
get_lang('CountUsers'),
120+
];
121+
122+
$columnModel = [
123+
[
124+
'name' => 'url',
125+
'index' => 'url',
126+
'width' => '180',
127+
'align' => 'left',
128+
],
129+
[
130+
'name' => 'session',
131+
'index' => 'session',
132+
'width' => '100',
133+
'align' => 'left',
134+
],
135+
[
136+
'name' => 'count',
137+
'index' => 'count',
138+
'width' => '500',
139+
'align' => 'left',
140+
'sortable' => 'false',
141+
],
142+
];
143+
$extraParams['autowidth'] = 'true'; //use the width of the parent
144+
$extraParams['height'] = 'auto'; //use the width of the parent
145+
$actionLinks = '';
146+
?>
147+
<script>
148+
$(function() {
149+
<?php
150+
echo Display::grid_js(
151+
'user_session_grid',
152+
$url,
153+
$columns,
154+
$columnModel,
155+
$extraParams,
156+
[],
157+
$actionLinks,
158+
true
159+
);
160+
?>
161+
});
162+
</script>
163+
<?php
164+
echo Display::grid_html('user_session_grid');
165+
166+
break;
87167
case 'courses':
88168
// total amount of courses
89169
foreach ($course_categories as $code => $name) {

main/inc/ajax/statistics.ajax.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,40 @@
1414
switch ($action) {
1515
case 'get_user_session':
1616
$list = [];
17+
$urlList = UrlManager::get_url_data();
18+
$sessionUrl = api_get_path(WEB_CODE_PATH).'session/resume_session.php?id_session=';
1719

18-
$sql = "";
20+
$start = isset($_GET['start']) ? Database::escape_string(api_get_utc_datetime($_GET['start'])) : api_get_utc_datetime();
21+
$end = isset($_GET['end']) ? Database::escape_string(api_get_utc_datetime($_GET['end'])) : api_get_utc_datetime();
22+
23+
foreach ($urlList as $url) {
24+
$urlId = $url['id'];
25+
26+
$sessionList = SessionManager::get_sessions_list([], [], null, null, $urlId);
27+
foreach ($sessionList as $session) {
28+
$sessionId = $session['id'];
29+
$row = [];
30+
$row['url'] = $url['url'];
31+
$row['session'] = Display::url(
32+
$session['name'],
33+
$sessionUrl.$sessionId
34+
);
35+
36+
$table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
37+
$sql = "SELECT
38+
count(DISTINCT user_id) count
39+
FROM $table
40+
WHERE
41+
login_course_date >= '$start' AND
42+
logout_course_date <= '$end' AND
43+
session_id = '$sessionId' ";
44+
45+
$result = Database::query($sql);
46+
$result = Database::fetch_array($result);
47+
$row['count'] = $result['count'];
48+
$list[] = $row;
49+
}
50+
}
1951

2052
echo json_encode($list);
2153
break;

main/inc/lib/sessionmanager.lib.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3086,20 +3086,21 @@ public static function delete_session_category($id_checked, $delete_session = fa
30863086
* array('status' => STUDENT) or
30873087
* array('s.name' => array('operator' => 'LIKE', value = '%$needle%'))
30883088
* @param array $order_by a list of fields on which sort
3089+
* @param int $urlId
30893090
*
30903091
* @return array an array with all sessions of the platform
30913092
*
30923093
* @todo optional course code parameter, optional sorting parameters...
30933094
*/
3094-
public static function get_sessions_list($conditions = [], $order_by = [], $from = null, $to = null)
3095+
public static function get_sessions_list($conditions = [], $order_by = [], $from = null, $to = null, $urlId = 0)
30953096
{
30963097
$session_table = Database::get_main_table(TABLE_MAIN_SESSION);
30973098
$session_category_table = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
30983099
$user_table = Database::get_main_table(TABLE_MAIN_USER);
30993100
$table_access_url_rel_session = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
31003101
$session_course_table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
31013102
$course_table = Database::get_main_table(TABLE_MAIN_COURSE);
3102-
$access_url_id = api_get_current_access_url_id();
3103+
$urlId = empty($urlId) ? api_get_current_access_url_id() : (int) $urlId;
31033104
$return_array = [];
31043105

31053106
$sql_query = " SELECT
@@ -3118,7 +3119,7 @@ public static function get_sessions_list($conditions = [], $order_by = [], $from
31183119
LEFT JOIN $session_category_table sc ON s.session_category_id = sc.id
31193120
LEFT JOIN $session_course_table sco ON (sco.session_id = s.id)
31203121
INNER JOIN $course_table c ON sco.c_id = c.id
3121-
WHERE ar.access_url_id = $access_url_id ";
3122+
WHERE ar.access_url_id = $urlId ";
31223123

31233124
$availableFields = [
31243125
's.id',

0 commit comments

Comments
 (0)