Pārlūkot izejas kodu

Add charts to statistics page - Refs #8081

José Loguercio 9 gadi atpakaļ
vecāks
revīzija
a87abb12e1

+ 15 - 0
main/admin/statistics/index.php

@@ -12,6 +12,20 @@ api_protect_admin_script();
 
 $interbreadcrumb[] = array('url' => '../index.php', 'name' => get_lang('PlatformAdmin'));
 
+$htmlHeadXtra[] = '<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.min.js"></script>';
+        $htmlHeadXtra[] = ''
+        . '<script type="text/javascript">'
+            . '$(document).ready(function() {'
+                . '$.ajax({'
+                    . 'url: "'. api_get_path(WEB_CODE_PATH) .'inc/ajax/statistics.ajax.php?a=recentlogins",'
+                    . 'type: "POST",'
+                    . 'success: function(data) {'
+                        . 'var myLine = new Chart(document.getElementById("canvas").getContext("2d")).Line(data { animateScale: true, responsive: true });'
+                    . '};'
+                . '});'
+            . '});' 
+        . '</script>';
+        
 $tool_name = get_lang('Statistics');
 Display::display_header($tool_name);
 echo Display::page_header($tool_name);
@@ -102,6 +116,7 @@ switch ($report) {
         Statistics::printStats(get_lang('Students'), $students);
         break;
     case 'recentlogins':
+        echo '<canvas id="canvas" width="400" height=400"></canvas>';
         Statistics::printRecentLoginStats();
         Statistics::printRecentLoginStats(true);
         break;

+ 54 - 0
main/inc/ajax/statistics.ajax.php

@@ -0,0 +1,54 @@
+<?php
+
+/* For licensing terms, see /license.txt */
+/**
+ * Responses to AJAX calls
+ */
+
+require_once '../global.inc.php';
+
+api_protect_admin_script();
+
+$action = isset($_REQUEST['a']) ? $_REQUEST['a'] : null;
+
+switch ($action) {
+    case 'recentlogins':
+        
+        $list = [];
+        
+        $all = Statistics::getRecentLoginStats();
+        $distinct = Statistics::getRecentLoginStats(true);
+        
+        foreach ($all as $tick => $tock) {
+            $list['labels'][] = $tick;
+        }
+        
+        $list['dataset'][0]['label'] = get_lang('Logins');
+        $list['dataset'][0]['fillColor'] = "rgba(220,220,220,0.2)";
+        $list['dataset'][0]['strokeColor'] = "rgba(220,220,220,1)";
+        $list['dataset'][0]['pointColor'] = "rgba(220,220,220,1)";
+        $list['dataset'][0]['pointStrokeColor'] = "#fff";
+        $list['dataset'][0]['pointHighlightFill'] = "#fff";
+        $list['dataset'][0]['pointHighlightStroke'] = "rgba(220,220,220,1)";
+        
+        foreach ($all as $tick => $tock) {
+            $list['dataset'][0]['data'][] = $tock;
+        }
+        
+        $list['dataset'][1]['label'] = get_lang('Logins2');
+        $list['dataset'][1]['fillColor'] = "rgba(220,220,220,0.2)";
+        $list['dataset'][1]['strokeColor'] = "rgba(220,220,220,1)";
+        $list['dataset'][1]['pointColor'] = "rgba(220,220,220,1)";
+        $list['dataset'][1]['pointStrokeColor'] = "#fff";
+        $list['dataset'][1]['pointHighlightFill'] = "#fff";
+        $list['dataset'][1]['pointHighlightStroke'] = "rgba(220,220,220,1)";
+        
+        foreach ($distinct as $tick => $tock) {
+            $list['dataset'][1]['data'][] = $tock;
+        }
+        
+        echo json_encode($list);
+        break;
+}
+
+

+ 37 - 0
main/inc/lib/statistics.lib.php

@@ -530,6 +530,43 @@ class Statistics
             Statistics::printStats(get_lang('Logins'), $totalLogin, false);
         }
     }
+    
+    /**
+     * get the number of recent logins
+     * @param bool    $distinct   Whether to only give distinct users stats, or *all* logins
+     * @return array
+     */
+    public static function getRecentLoginStats($distinct = false)
+    {
+        $totalLogin = array();
+        $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
+        $access_url_rel_user_table= Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
+        $current_url_id = api_get_current_access_url_id();
+        if (api_is_multiple_url_enabled()) {
+            $table_url = ", $access_url_rel_user_table";
+            $where_url = " AND login_user_id=user_id AND access_url_id='".$current_url_id."'";
+        } else {
+            $table_url = '';
+            $where_url='';
+        }
+        $now = api_get_utc_datetime();
+        $field = 'login_user_id';
+        if ($distinct) {
+            $field = 'DISTINCT(login_user_id)';
+        }
+        $sql[get_lang('ThisDay')]    = "SELECT count($field) AS number FROM $table $table_url WHERE DATE_ADD(login_date, INTERVAL 1 DAY) >= '$now' $where_url";
+        $sql[get_lang('Last7days')]  = "SELECT count($field) AS number  FROM $table $table_url WHERE DATE_ADD(login_date, INTERVAL 7 DAY) >= '$now' $where_url";
+        $sql[get_lang('Last31days')] = "SELECT count($field) AS number  FROM $table $table_url WHERE DATE_ADD(login_date, INTERVAL 31 DAY) >= '$now' $where_url";
+        $sql[get_lang('Total')]      = "SELECT count($field) AS number  FROM $table $table_url WHERE 1=1 $where_url";
+        foreach ($sql as $index => $query) {
+            $res = Database::query($query);
+            $obj = Database::fetch_object($res);
+            $totalLogin[$index] = $obj->number;
+        }
+        
+        return $totalLogin;
+    }
+    
     /**
      * Show some stats about the accesses to the different course tools
      */