@ -12,34 +12,41 @@ class Statistics
* @param int $size
* @return string Formatted file size
*/
static function make_size_s tring($size)
public static function makeSizeS tring($size)
{
if ($size < pow ( 2 , 10 ) ) return $ size . " bytes " ;
if ($size >= pow(2,10) & & $size < pow ( 2 , 20 ) ) return round ( $ size / pow ( 2 , 10 ) , 0 ) . " KB " ;
if ($size >= pow(2,20) & & $size < pow ( 2 , 30 ) ) return round ( $ size / pow ( 2 , 20 ) , 1 ) . " MB " ;
if ($size > pow(2,30)) return round($size / pow(2,30), 2)." GB";
if ($size < pow ( 2 , 10 ) ) {
return $size." bytes";
}
if ($size >= pow(2, 10) & & $size < pow ( 2 , 20 ) ) {
return round($size / pow(2, 10), 0)." KB";
}
if ($size >= pow(2, 20) & & $size < pow ( 2 , 30 ) ) {
return round($size / pow(2, 20), 1)." MB";
}
if ($size > pow(2, 30)) {
return round($size / pow(2, 30), 2)." GB";
}
}
/**
* Count courses
* @param string $category_code Code of a course category. Default: count
* all courses.
* @param string $categoryCode Code of a course category. Default: count all courses.
* @return int Number of courses counted
*/
static function count_courses($category_code = NULL )
public static function countCourses($categoryCode = null )
{
$course_table = Database :: get_main_table(TABLE_MAIN_COURSE);
$access_url_rel_course_table= Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
$current_url_id = api_get_current_access_url_id();
if (api_is_multiple_url_enabled()) {
$sql = "SELECT COUNT(*) AS number FROM ".$course_table." as c, ".$access_url_rel_course_table." as u WHERE u.course_code=c.code AND access_url_id='".$current_url_id."'";
if (isset ($category_c ode)) {
$sql .= " AND category_code = '".Database::escape_string($category_c ode)."'";
if (isset ($categoryC ode)) {
$sql .= " AND category_code = '".Database::escape_string($categoryC ode)."'";
}
} else {
$sql = "SELECT COUNT(*) AS number FROM ".$course_table." ";
if (isset ($category_c ode)) {
$sql .= " WHERE category_code = '".Database::escape_string($category_c ode)."'";
if (isset ($categoryC ode)) {
$sql .= " WHERE category_code = '".Database::escape_string($categoryC ode)."'";
}
}
$res = Database::query($sql);
@ -49,13 +56,12 @@ class Statistics
/**
* Count courses by visibility
* @param int Visibility (0 = closed, 1 = private, 2 = open, 3 = public)
* all courses.
* @param int $visibility Visibility (0 = closed, 1 = private, 2 = open, 3 = public) all courses.
* @return int Number of courses counted
*/
static function count_courses_by_visibility($vis = null)
public static function countCoursesByVisibility($visibility = null)
{
if (!isset($vis)) {
if (!isset($visibility )) {
return 0;
}
$course_table = Database :: get_main_table(TABLE_MAIN_COURSE);
@ -64,13 +70,13 @@ class Statistics
if (api_is_multiple_url_enabled()) {
$sql = "SELECT COUNT(*) AS number FROM ".$course_table." as c, ".$access_url_rel_course_table." as u
WHERE u.course_code=c.code AND access_url_id='".$current_url_id."'";
if (isset ($vis)) {
$sql .= " AND visibility = ".intval($vis);
if (isset ($visibility )) {
$sql .= " AND visibility = ".intval($visibility );
}
} else {
$sql = "SELECT COUNT(*) AS number FROM ".$course_table." ";
if (isset ($vis)) {
$sql .= " WHERE visibility = ".intval($vis);
if (isset ($visibility )) {
$sql .= " WHERE visibility = ".intval($visibility );
}
}
$res = Database::query($sql);
@ -80,13 +86,13 @@ class Statistics
/**
* Count users
* @param int optional, user status (COURSEMANAGER or STUDENT), if it's not setted it'll count all users.
* @param string optional, code of a course category. Default: count only users without filtering category
* @param bool count invisible courses (todo)
* @param bool count only active users (false to only return currently active users)
* @param int $status Optional user status (COURSEMANAGER or STUDENT), if it's not setted it'll count all users.
* @param string $categ oryCode O ptional, code of a course category. Default: count only users without filtering category
* @param bool $ countInvisibleCourses C ount invisible courses (todo)
* @param bool $onlyA ctive C ount only active users (false to only return currently active users)
* @return int Number of users counted
*/
static function count_users($status = null, $category_code = null, $count_invisible_courses = true, $only_a ctive = false)
public static function countUsers($status = null, $categoryCode = null, $countInvisibleCourses = true, $onlyA ctive = false)
{
// Database table definitions
$course_user_table = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
@ -94,19 +100,28 @@ class Statistics
$user_table = Database :: get_main_table(TABLE_MAIN_USER);
$access_url_rel_user_table= Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
$current_url_id = api_get_current_access_url_id();
$active_filter = $only_a ctive?' AND active=1':'';
$active_filter = $onlyA ctive?' AND active=1':'';
$status_filter = isset($status)?' AND status = '.intval($status):'';
if (api_is_multiple_url_enabled()) {
$sql = "SELECT COUNT(DISTINCT(u.user_id)) AS number FROM $user_table as u, $access_url_rel_user_table as url WHERE u.user_id=url.user_id AND access_url_id='".$current_url_id."' $status_filter $active_filter";
if (isset ($category_code)) {
$sql = "SELECT COUNT(DISTINCT(cu.user_id)) AS number FROM $course_user_table cu, $course_table c, $access_url_rel_user_table as url WHERE c.code = cu.course_code AND c.category_code = '".Database::escape_string($category_code)."' AND cu.user_id=url.user_id AND access_url_id='".$current_url_id."' $status_filter $active_filter";
$sql = "SELECT COUNT(DISTINCT(u.user_id)) AS number
FROM $user_table as u, $access_url_rel_user_table as url
WHERE u.user_id=url.user_id
AND access_url_id='".$current_url_id."'
$status_filter $active_filter";
if (isset ($categoryCode)) {
$sql = "SELECT COUNT(DISTINCT(cu.user_id)) AS number
FROM $course_user_table cu, $course_table c, $access_url_rel_user_table as url
WHERE c.code = cu.course_code
AND c.category_code = '".Database::escape_string($categoryCode)."'
AND cu.user_id=url.user_id AND access_url_id='".$current_url_id."'
$status_filter $active_filter";
}
} else {
$sql = "SELECT COUNT(DISTINCT(user_id)) AS number FROM $user_table WHERE 1=1 $status_filter $active_filter";
if (isset ($category_code)) {
if (isset ($categoryC ode)) {
$status_filter = isset($status)?' AND status = '.intval($status):'';
$sql = "SELECT COUNT(DISTINCT(cu.user_id)) AS number FROM $course_user_table cu, $course_table c WHERE c.code = cu.course_code AND c.category_code = '".Database::escape_string($category_code)."' $status_filter $active_filter";
$sql = "SELECT COUNT(DISTINCT(cu.user_id)) AS number FROM $course_user_table cu, $course_table c WHERE c.code = cu.course_code AND c.category_code = '".Database::escape_string($categoryC ode)."' $status_filter $active_filter";
}
}
@ -119,7 +134,7 @@ class Statistics
* Count sessions
* @return int Number of sessions counted
*/
static function count_s essions()
public static function countS essions()
{
$session_table = Database :: get_main_table(TABLE_MAIN_SESSION);
$access_url_rel_session_table= Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
@ -138,7 +153,7 @@ class Statistics
* Count activities from track_e_default_table
* @return int Number of activities counted
*/
static function get_number_of_a ctivities()
public static function getNumberOfA ctivities()
{
// Database table definitions
$track_e_default = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_DEFAULT);
@ -164,12 +179,12 @@ class Statistics
/**
* Get activities data to display
* @param int $from
* @param int $number_of_i tems
* @param int $numberOfI tems
* @param int $column
* @param string $direction
* @return array
*/
static function get_activities_data($from, $number_of_i tems, $column, $direction)
public static function getActivitiesData($from, $numberOfI tems, $column, $direction)
{
global $dateTimeFormatLong;
$track_e_default = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DEFAULT);
@ -179,7 +194,7 @@ class Statistics
$column = intval($column);
$from = intval($from);
$number_of_items = intval($number_of_i tems);
$numberOfItems = intval($numberOfI tems);
if (!in_array($direction, array('ASC','DESC'))) {
$direction = 'DESC';
@ -222,7 +237,7 @@ class Statistics
} else {
$sql .= " ORDER BY col5 DESC ";
}
$sql .= " LIMIT $from,$number_of_i tems ";
$sql .= " LIMIT $from,$numberOfI tems ";
$res = Database::query($sql);
$activities = array ();
@ -251,7 +266,8 @@ class Statistics
// User id.
$row[3] = Display::url(
$row[3],
api_get_path(WEB_CODE_PATH).'admin/user_information.php?user_id='.$row[4], array('title' => get_lang('UserInfo'))
api_get_path(WEB_CODE_PATH).'admin/user_information.php?user_id='.$row[4],
array('title' => get_lang('UserInfo'))
);
$row[4] = TrackingUserLog::get_ip_from_user_event($row[4], $row[5], true);
@ -268,7 +284,7 @@ class Statistics
* Get all course categories
* @return array All course categories (code => name)
*/
static function get_course_c ategories()
public static function getCourseC ategories()
{
$category_table = Database :: get_main_table(TABLE_MAIN_CATEGORY);
$sql = "SELECT code, name FROM $category_table ORDER BY tree_pos";
@ -286,7 +302,8 @@ class Statistics
* @param int $max The maximum value in the rescaled data (default = 500);
* @return array The rescaled data, same key as $data
*/
static function rescale($data, $max = 500) {
public static function rescale($data, $max = 500)
{
$data_max = 1;
foreach ($data as $index => $value) {
$data_max = ($data_max < $value ? $value : $data_max);
@ -304,25 +321,26 @@ class Statistics
* Show statistics
* @param string $title The title
* @param array $stats
* @param bool $show_t otal
* @param bool $is_file_s ize
* @param bool $showT otal
* @param bool $isFileS ize
*/
static function print_stats($title, $stats, $show_total = true, $is_file_size = false) {
public static function printStats($title, $stats, $showTotal = true, $isFileSize = false)
{
$total = 0;
$data = Statistics::rescale($stats);
echo '< table class = "data_table" cellspacing = "0" cellpadding = "3" >
< tr > < th colspan = "'.($show_t otal ? '4' : '3').'" > '.$title.'< / th > < / tr > ';
< tr > < th colspan = "'.($showT otal ? '4' : '3').'" > '.$title.'< / th > < / tr > ';
$i = 0;
foreach ($stats as $subtitle => $number) {
$total += $number;
}
foreach ($stats as $subtitle => $number) {
if (!$is_file_s ize) {
if (!$isFileS ize) {
$number_label = number_format($number, 0, ',', '.');
} else {
$number_label = Statistics::make_size_s tring($number);
$number_label = Statistics::makeSizeS tring($number);
}
$percentage = ($total>0?number_format(100*$number/$total, 1, ',', '.'):'0');
@ -330,17 +348,17 @@ class Statistics
< td width = "150" > '.$subtitle.'< / td >
< td width = "550" > '.Display::bar_progress($percentage, false).'< / td >
< td align = "right" > '.$number_label.'< / td > ';
if ($show_t otal) {
if ($showT otal) {
echo '< td align = "right" > '.$percentage.'%< / td > ';
}
echo '< / tr > ';
$i ++;
}
if ($show_t otal) {
if (!$is_file_s ize) {
if ($showT otal) {
if (!$isFileS ize) {
$total_label = number_format($total, 0, ',', '.');
} else {
$total_label = Statistics::make_size_s tring($total);
$total_label = Statistics::makeSizeS tring($total);
}
echo '< tr > < th colspan = "4" align = "right" > '.get_lang('Total').': '.$total_label.'< / td > < / tr > ';
}
@ -351,7 +369,7 @@ class Statistics
* Show some stats about the number of logins
* @param string $type month, hour or day
*/
static function print_login_s tats($type)
public static function printLoginS tats($type)
{
$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);
@ -401,7 +419,7 @@ class Statistics
}
$result_last_x[$stat_date] = $obj->number_of_logins;
}
Statistics::print_s tats(get_lang('LastLogins').' ('.$period.')', $result_last_x, true);
Statistics::printS tats(get_lang('LastLogins').' ('.$period.')', $result_last_x, true);
flush(); //flush web request at this point to see something already while the full data set is loading
echo '< br / > ';
$res = Database::query($sql);
@ -420,15 +438,15 @@ class Statistics
}
$result[$stat_date] = $obj->number_of_logins;
}
Statistics::print_s tats(get_lang('AllLogins').' ('.$period.')', $result, true);
Statistics::printS tats(get_lang('AllLogins').' ('.$period.')', $result, true);
}
/**
* Print the number of recent logins
*/
static function print_recent_login_s tats()
public static function printRecentLoginS tats()
{
$total_logins = array();
$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();
@ -439,21 +457,21 @@ class Statistics
$table_url = '';
$where_url='';
}
$sql[get_lang('Thisd ay')] = "SELECT count(login_user_id) AS number FROM $table $table_url WHERE DATE_ADD(login_date, INTERVAL 1 DAY) >= NOW() $where_url";
$sql[get_lang('ThisD ay')] = "SELECT count(login_user_id) AS number FROM $table $table_url WHERE DATE_ADD(login_date, INTERVAL 1 DAY) >= NOW() $where_url";
$sql[get_lang('Last7days')] = "SELECT count(login_user_id) AS number FROM $table $table_url WHERE DATE_ADD(login_date, INTERVAL 7 DAY) >= NOW() $where_url";
$sql[get_lang('Last31days')] = "SELECT count(login_user_id) AS number FROM $table $table_url WHERE DATE_ADD(login_date, INTERVAL 31 DAY) >= NOW() $where_url";
$sql[get_lang('Total')] = "SELECT count(login_user_id) 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);
$total_logins [$index] = $obj->number;
$totalLogin [$index] = $obj->number;
}
Statistics::print_stats(get_lang('Logins'),$total_logins, false);
Statistics::printStats(get_lang('Logins'), $totalLogin, false);
}
/**
* Show some stats about the accesses to the different course tools
*/
static function print_tool_s tats()
public static function printToolS tats()
{
$table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ACCESS);
$access_url_rel_course_table= Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
@ -500,13 +518,13 @@ class Statistics
while ($obj = Database::fetch_object($res)) {
$result[$tool_names[$obj->access_tool]] = $obj->number_of_logins;
}
Statistics::print_stats(get_lang('PlatformToolAccess'),$result, true);
Statistics::printStats(get_lang('PlatformToolAccess'), $result, true);
}
/**
* Show some stats about the number of courses per language
*/
static function print_course_by_language_s tats()
public static function printCourseByLanguageS tats()
{
$table = Database :: get_main_table(TABLE_MAIN_COURSE);
$access_url_rel_course_table= Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
@ -525,13 +543,13 @@ class Statistics
while ($obj = Database::fetch_object($res)) {
$result[$obj->course_language] = $obj->number_of_courses;
}
Statistics::print_stats(get_lang('CountCourseByLanguage'),$result, true);
Statistics::printStats(get_lang('CountCourseByLanguage'), $result, true);
}
/**
* Shows the number of users having their picture uploaded in Dokeos.
*/
static function print_user_pictures_s tats()
public static function printUserPicturesS tats()
{
$user_table = Database :: get_main_table(TABLE_MAIN_USER);
$access_url_rel_user_table= Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
@ -554,17 +572,24 @@ class Statistics
// #users without picture
$result[get_lang('No')] = $count1->n - $count2->n;
$result[get_lang('Yes')] = $count2->n; // #users with picture
Statistics::print_s tats(get_lang('CountUsers').' ('.get_lang('UserPicture').')',$result,true);
Statistics::printS tats(get_lang('CountUsers').' ('.get_lang('UserPicture').')', $result, true);
}
/**
* Important activities
*/
static function print_activities_s tats()
public static function printActivitiesS tats()
{
echo '< h4 > '.get_lang('ImportantActivities').'< / h4 > ';
// Create a search-box
$form = new FormValidator('search_simple','get',api_get_path(WEB_CODE_PATH).'admin/statistics/index.php','','width=200px',false);
$form = new FormValidator(
'search_simple',
'get',
api_get_path(WEB_CODE_PATH).'admin/statistics/index.php',
'',
'width=200px',
false
);
$renderer =& $form->defaultRenderer();
$renderer->setCustomElementTemplate('< span > {element}< / span > ');
$form->addElement('hidden', 'report', 'activities');
@ -576,7 +601,14 @@ class Statistics
$form->display();
echo '< / div > ';
$table = new SortableTable('activities', array('Statistics','get_number_of_activities'), array('Statistics','get_activities_data'),5,50,'DESC');
$table = new SortableTable(
'activities',
array('Statistics', 'get_number_of_activities'),
array('Statistics','get_activities_data'),
5,
50,
'DESC'
);
$parameters = array();
$parameters['report'] = 'activities';
@ -597,7 +629,7 @@ class Statistics
/**
* Shows statistics about the time of last visit to each course.
*/
static function print_course_last_v isit()
public static function printCourseLastV isit()
{
$access_url_rel_course_table= Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
$current_url_id = api_get_current_access_url_id();
@ -676,7 +708,7 @@ class Statistics
* @param string Type of message: 'sent' or 'received'
* @return array Message list
*/
static function get_messages($message_t ype)
public static function getMessages($messageT ype)
{
$message_table = Database::get_main_table(TABLE_MAIN_MESSAGE);
$user_table = Database::get_main_table(TABLE_MAIN_USER);
@ -684,7 +716,7 @@ class Statistics
$current_url_id = api_get_current_access_url_id();
switch ($message_t ype) {
switch ($messageT ype) {
case 'sent':
$field = 'user_sender_id';
break;
@ -719,7 +751,7 @@ class Statistics
/**
* Count the number of friends for social network users
*/
static function get_f riends()
public static function getF riends()
{
$user_friend_table = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
$user_table = Database::get_main_table(TABLE_MAIN_USER);
@ -751,13 +783,13 @@ class Statistics
/**
* Print the number of users that didn't login for a certain period of time
*/
static function print_users_not_logged_in_s tats()
public static function printUsersNotLoggedInS tats()
{
$total_logins = array();
$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();
$total = self::count_u sers();
$total = self::countU sers();
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."'";
@ -765,7 +797,7 @@ class Statistics
$table_url = '';
$where_url='';
}
$sql[get_lang('Thisd ay')] =
$sql[get_lang('ThisD ay')] =
"SELECT count(distinct(login_user_id)) AS number ".
" FROM $table $table_url ".
" WHERE DATE_ADD(login_date, INTERVAL 1 DAY) >= NOW() $where_url";
@ -788,8 +820,12 @@ class Statistics
$res = Database::query($query);
$obj = Database::fetch_object($res);
$r = $total - $obj->number;
$total_logins [$index] = $r < 0 ? 0 : $ r ;
$totalLogin [$index] = $r < 0 ? 0 : $ r ;
}
Statistics::print_stats(get_lang('StatsUsersDidNotLoginInLastPeriods'),$total_logins,false);
Statistics::printStats(
get_lang('StatsUsersDidNotLoginInLastPeriods'),
$totalLogin,
false
);
}
}