public function test_report_customsql_get_ready_to_run_daily_reports()
 {
     global $DB;
     $this->resetAfterTest(true);
     $timenow = time();
     $dateparts = getdate($timenow);
     $currenthour = $dateparts['hours'];
     list($today, $yesterday) = report_customsql_get_daily_time_starts($timenow, $currenthour);
     // Test entry 1.
     // This report is supposed to run at the current hour (wehenver this test is run).
     // The last run time recorded in the database is acutally tomorrow(!)
     // relative to $timestamp. (Acutally timestamp is yesterday.)
     $lastrun = $today;
     $timestamp = $lastrun - ($today - $yesterday);
     $id = $this->create_a_database_row('daily', $currenthour, $lastrun, 'admin');
     $report = $DB->get_record('report_customsql_queries', array('id' => $id));
     $this->assertFalse(report_customsql_is_daily_report_ready($report, $timestamp));
     // Test entry 2.
     // This report is set to run at this hour, and was last run is that time
     // yesterday, and current time exactly the time the report should be run today.
     $lastrun = $yesterday;
     $timestamp = $today;
     $id = $this->create_a_database_row('daily', $currenthour - 1, $lastrun, 'admin, s1');
     $report = $DB->get_record('report_customsql_queries', array('id' => $id));
     $this->assertTrue(report_customsql_is_daily_report_ready($report, $timestamp));
     // Test entry 3.
     // This is the same as Test entry 2, except with no emails. At one point,
     // that made a difference, but it should not.
     $lastrun = $yesterday;
     $timestamp = $today;
     $id = $this->create_a_database_row('daily', $currenthour, $lastrun, '');
     $report = $DB->get_record('report_customsql_queries', array('id' => $id));
     $this->assertTrue(report_customsql_is_daily_report_ready($report, $timestamp));
     // Test entry 4.
     // This report is set to run next hour, and was last run at this hour
     // yesterday.
     $lastrun = $yesterday;
     $timestamp = $today;
     $id = $this->create_a_database_row('daily', $currenthour + 1, $lastrun, 's1');
     $report = $DB->get_record('report_customsql_queries', array('id' => $id));
     $this->assertFalse(report_customsql_is_daily_report_ready($report, $timestamp));
     // Verify that two reports are returned - the two assertTrues above.
     $this->assertEquals(2, count(report_customsql_get_ready_to_run_daily_reports($timenow)));
     // Test entry 5.
     // Report should run at 1:00am. We need to make sure that it does not get
     // run late in the day, say at 11pm. (This might be the case if we
     // had a 20-hour cut-off or something.
     list($oneam) = report_customsql_get_daily_time_starts($timenow, 1);
     list($elevenpm) = report_customsql_get_daily_time_starts($timenow, 23);
     $timenow = $elevenpm;
     $id = $this->create_a_database_row('daily', 1, $oneam, 's1');
     $report = $DB->get_record('report_customsql_queries', array('id' => $id));
     $this->assertFalse(report_customsql_is_daily_report_ready($report, $timenow));
     // Test entry 6.
     // Suppose that yesterday, cron got delayed, so this report that should
     // run at 02:00 was acutally run at 04:00. Now today, the report should
     // run at 02:00 again, to catch up.
     list($twoam) = report_customsql_get_daily_time_starts($timenow, 2);
     list($notused, $fouramyesterday) = report_customsql_get_daily_time_starts($timenow, 4);
     $timenow = $twoam;
     $id = $this->create_a_database_row('daily', 2, $fouramyesterday, 's1');
     $report = $DB->get_record('report_customsql_queries', array('id' => $id));
     $this->assertTrue(report_customsql_is_daily_report_ready($report, $timenow));
 }
/**
 * Check if the report is ready to run.
 * @param object $report
 * @return boolean
 */
function report_customsql_is_daily_report_ready($report, $timenow)
{
    // Time when the report should run today.
    list($runtimetoday) = report_customsql_get_daily_time_starts($timenow, $report->at);
    // Values used to check whether the report has already run today.
    list($today) = report_customsql_get_daily_time_starts($timenow, 0);
    list($lastrunday) = report_customsql_get_daily_time_starts($report->lastrun, 0);
    if ($runtimetoday <= $timenow && $today > $lastrunday) {
        return true;
    }
    return false;
}