Example #1
0
function cron_site_data_daily()
{
    require_once get_config('libroot') . 'registration.php';
    $current = site_data_current();
    $time = db_format_timestamp(time());
    // Total users
    insert_record('site_data', (object) array('ctime' => $time, 'type' => 'user-count-daily', 'value' => $current['users']));
    // Logged-in users
    $interval = is_postgres() ? "'1 day'" : '1 day';
    $where = "lastaccess >= DATE(?) AND lastaccess < DATE(?)+ INTERVAL {$interval}";
    insert_record('site_data', (object) array('ctime' => $time, 'type' => 'loggedin-users-daily', 'value' => count_records_select('usr', $where, array($time, $time))));
    // Process log file containing view visits
    $viewlog = get_config('dataroot') . 'log/views.log';
    if (@rename($viewlog, $viewlog . '.temp') and $fh = @fopen($viewlog . '.temp', 'r')) {
        // Read the new stuff out of the file
        $latest = get_config('viewloglatest');
        $visits = array();
        while (!feof($fh)) {
            $line = fgets($fh, 1024);
            if (preg_match('/^\\[(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2})\\] (\\d+)$/', $line, $m) && $m[1] > $latest) {
                $visits[] = (object) array('ctime' => $m[1], 'view' => $m[2]);
            }
        }
        fclose($fh);
        // Get per-view counts for the view table.
        $visitcounts = array();
        foreach ($visits as &$v) {
            if (!isset($visitcounts[$v->view])) {
                $visitcounts[$v->view] = 0;
            }
            $visitcounts[$v->view]++;
        }
        // Add visit records to view_visit
        foreach ($visits as &$v) {
            if (record_exists('view', 'id', $v->view)) {
                insert_record('view_visit', $v);
            }
        }
        // Delete view_visit records > 1 week old
        delete_records_select('view_visit', 'ctime < CURRENT_DATE - INTERVAL ' . (is_postgres() ? "'1 week'" : '1 WEEK'));
        // Update view counts
        foreach ($visitcounts as $viewid => $newvisits) {
            execute_sql("UPDATE {view} SET visits = visits + ? WHERE id = ?", array($newvisits, $viewid));
        }
        set_config('viewloglatest', $time);
        unlink($viewlog . '.temp');
    }
    require_once 'function.dirsize.php';
    if ($diskusage = dirsize(get_config('dataroot'))) {
        // Currently there is no need to track disk usage
        // over time, so delete old records first.
        delete_records('site_data', 'type', 'disk-usage');
        insert_record('site_data', (object) array('ctime' => $time, 'type' => 'disk-usage', 'value' => $diskusage));
    }
    graph_site_data_daily();
}
Example #2
0
function site_statistics($full = false)
{
    $data = array();
    if ($full) {
        $data = site_data_current();
        $data['weekly'] = true;
        if (is_postgres()) {
            $weekago = "CURRENT_DATE - INTERVAL '1 week'";
            $thisweeksql = "(lastaccess > {$weekago})::int";
            $todaysql = '(lastaccess > CURRENT_DATE)::int';
            $eversql = "(NOT lastaccess IS NULL)::int";
        } else {
            $weekago = 'CURRENT_DATE - INTERVAL 1 WEEK';
            $thisweeksql = "lastaccess > {$weekago}";
            $todaysql = 'lastaccess > CURRENT_DATE';
            $eversql = "NOT lastaccess IS NULL";
        }
        $sql = "SELECT SUM({$todaysql}) AS today, SUM({$thisweeksql}) AS thisweek, {$weekago} AS weekago, SUM({$eversql}) AS ever FROM {usr}";
        $active = get_record_sql($sql);
        $data['usersloggedin'] = get_string('loggedinsince', 'admin', $active->today, $active->thisweek, format_date(strtotime($active->weekago), 'strftimedateshort'), $active->ever);
        $memberships = count_records_sql("\n            SELECT COUNT(*)\n            FROM {group_member} m JOIN {group} g ON g.id = m.group\n            WHERE g.deleted = 0\n        ");
        $data['groupmemberaverage'] = round($memberships / $data['users'], 1);
        $data['strgroupmemberaverage'] = get_string('groupmemberaverage', 'admin', $data['groupmemberaverage']);
        $data['viewsperuser'] = get_field_sql("\n            SELECT (0.0 + COUNT(id)) / NULLIF(COUNT(DISTINCT \"owner\"), 0)\n            FROM {view}\n            WHERE NOT \"owner\" IS NULL AND \"owner\" > 0\n        ");
        $data['viewsperuser'] = round($data['viewsperuser'], 1);
        $data['strviewsperuser'] = get_string('viewsperuser', 'admin', $data['viewsperuser']);
    }
    $data['name'] = get_config('sitename');
    $data['release'] = get_config('release');
    $data['version'] = get_config('version');
    $data['installdate'] = format_date(strtotime(get_config('installation_time')), 'strftimedate');
    $data['dbsize'] = db_total_size();
    $data['diskusage'] = get_field('site_data', 'value', 'type', 'disk-usage');
    $data['cronrunning'] = !record_exists_select('cron', 'nextrun IS NULL OR nextrun < CURRENT_DATE');
    $data['siteclosedbyadmin'] = get_config('siteclosedbyadmin');
    if ($latestversion = get_config('latest_version')) {
        $data['latest_version'] = $latestversion;
        if ($data['release'] == $latestversion) {
            $data['strlatestversion'] = get_string('uptodate', 'admin');
        } else {
            $download_page = 'https://launchpad.net/mahara/+download';
            $data['strlatestversion'] = get_string('latestversionis', 'admin', $download_page, $latestversion);
        }
    }
    return $data;
}