Ejemplo n.º 1
0
/**
 * Fix missing entries in the statistics.
 *
 * This creates a dummy stat when nothing happened during a day/week/month.
 *
 * @param array $stats array of statistics.
 * @param int $timeafter unused.
 * @param string $timestr type of statistics to generate (dayly, weekly, monthly).
 * @param boolean $line2
 * @param boolean $line3
 * @return array of fixed statistics.
 */
function stats_fix_zeros($stats, $timeafter, $timestr, $line2 = true, $line3 = false)
{
    if (empty($stats)) {
        return;
    }
    $timestr = str_replace('user_', '', $timestr);
    // just in case.
    // Gets the current user base time.
    $fun = 'stats_get_base_' . $timestr;
    $now = $fun();
    // Extract the ending time of the statistics.
    $actualtimes = array();
    $actualtimeshour = null;
    foreach ($stats as $statid => $s) {
        // Normalise the month date to the 1st if for any reason it's set to later. But we ignore
        // anything above or equal to 29 because sometimes we get the end of the month. Also, we will
        // set the hours of the result to all of them, that way we prevent DST differences.
        if ($timestr == 'monthly') {
            $day = date('d', $s->timeend);
            if (date('d', $s->timeend) > 1 && date('d', $s->timeend) < 29) {
                $day = 1;
            }
            if (is_null($actualtimeshour)) {
                $actualtimeshour = date('H', $s->timeend);
            }
            $s->timeend = mktime($actualtimeshour, 0, 0, date('m', $s->timeend), $day, date('Y', $s->timeend));
        }
        $stats[$statid] = $s;
        $actualtimes[] = $s->timeend;
    }
    $actualtimesvalues = array_values($actualtimes);
    $timeafter = array_pop($actualtimesvalues);
    // Generate a base timestamp for each possible month/week/day.
    $times = array();
    while ($timeafter < $now) {
        $times[] = $timeafter;
        if ($timestr == 'daily') {
            $timeafter = stats_get_next_day_start($timeafter);
        } else {
            if ($timestr == 'weekly') {
                $timeafter = stats_get_next_week_start($timeafter);
            } else {
                if ($timestr == 'monthly') {
                    // We can't just simply +1 month because the 31st Jan + 1 month = 2nd of March.
                    $year = date('Y', $timeafter);
                    $month = date('m', $timeafter);
                    $day = date('d', $timeafter);
                    $dayofnextmonth = $day;
                    if ($day >= 29) {
                        $daysinmonth = date('n', mktime(0, 0, 0, $month + 1, 1, $year));
                        if ($day > $daysinmonth) {
                            $dayofnextmonth = $daysinmonth;
                        }
                    }
                    $timeafter = mktime($actualtimeshour, 0, 0, $month + 1, $dayofnextmonth, $year);
                } else {
                    // This will put us in a never ending loop.
                    return $stats;
                }
            }
        }
    }
    // Add the base timestamp to the statistics if not present.
    foreach ($times as $count => $time) {
        if (!in_array($time, $actualtimes) && $count != count($times) - 1) {
            $newobj = new StdClass();
            $newobj->timeend = $time;
            $newobj->id = 0;
            $newobj->roleid = 0;
            $newobj->line1 = 0;
            if (!empty($line2)) {
                $newobj->line2 = 0;
            }
            if (!empty($line3)) {
                $newobj->line3 = 0;
            }
            $newobj->zerofixed = true;
            $stats[] = $newobj;
        }
    }
    usort($stats, "stats_compare_times");
    return $stats;
}
Ejemplo n.º 2
0
function stats_fix_zeros($stats, $timeafter, $timestr, $line2 = true, $line3 = false)
{
    if (empty($stats)) {
        return;
    }
    $timestr = str_replace('user_', '', $timestr);
    // just in case.
    $fun = 'stats_get_base_' . $timestr;
    $now = $fun();
    $times = array();
    // add something to timeafter since it is our absolute base
    $actualtimes = array();
    foreach ($stats as $statid => $s) {
        //normalize the times in stats - those might have been created in different timezone, DST etc.
        $s->timeend = $fun($s->timeend + 60 * 60 * 5);
        $stats[$statid] = $s;
        $actualtimes[] = $s->timeend;
    }
    $timeafter = array_pop(array_values($actualtimes));
    while ($timeafter < $now) {
        $times[] = $timeafter;
        if ($timestr == 'daily') {
            $timeafter = stats_get_next_day_start($timeafter);
        } else {
            if ($timestr == 'weekly') {
                $timeafter = stats_get_next_week_start($timeafter);
            } else {
                if ($timestr == 'monthly') {
                    $timeafter = stats_get_next_month_start($timeafter);
                } else {
                    return $stats;
                    // this will put us in a never ending loop.
                }
            }
        }
    }
    foreach ($times as $count => $time) {
        if (!in_array($time, $actualtimes) && $count != count($times) - 1) {
            $newobj = new StdClass();
            $newobj->timeend = $time;
            $newobj->id = 0;
            $newobj->roleid = 0;
            $newobj->line1 = 0;
            if (!empty($line2)) {
                $newobj->line2 = 0;
            }
            if (!empty($line3)) {
                $newobj->line3 = 0;
            }
            $newobj->zerofixed = true;
            $stats[] = $newobj;
        }
    }
    usort($stats, "stats_compare_times");
    return $stats;
}
Ejemplo n.º 3
0
 /**
  * Test the daily stats function.
  *
  * @depends test_statslib_get_base_daily
  * @depends test_statslib_get_next_day_start
  */
 public function test_statslib_cron_daily_no_default_profile_id()
 {
     global $CFG, $DB;
     $CFG->defaultfrontpageroleid = 0;
     $course1 = $DB->get_record('course', array('shortname' => 'course1'));
     $guestid = $CFG->siteguest;
     $start = stats_get_base_daily(1272758400);
     $end = stats_get_next_day_start($start);
     $fpid = (int) $CFG->defaultfrontpageroleid;
     $gr = get_guest_role();
     $dataset = $this->load_xml_data_file(__DIR__ . "/fixtures/statslib-test10.xml");
     $this->prepare_db($dataset[0], array('log'));
     // Stats cron daily uses mtrace, turn on buffering to silence output.
     ob_start();
     stats_cron_daily($maxdays = 1);
     $output = ob_get_contents();
     ob_end_clean();
     $this->verify_stats($dataset[1], $output);
 }