/**
  * Function to be run periodically according to the moodle cron
  * This function searches for things that need to be done, such
  * as sending out mail, toggling flags etc ...
  *
  * Runs any automatically scheduled reports weekly or monthly.
  *
  * @return boolean
  */
 public function execute()
 {
     global $CFG, $DB;
     require_once dirname(__FILE__) . '/../../locallib.php';
     $timenow = time();
     list($startofthisweek, $startoflastweek) = report_customsql_get_week_starts($timenow);
     list($startofthismonth) = report_customsql_get_month_starts($timenow);
     mtrace("... Looking for old temp CSV files to delete.");
     $numdeleted = report_customsql_delete_old_temp_files($startoflastweek);
     if ($numdeleted) {
         mtrace("... {$numdeleted} old temporary files deleted.");
     }
     // Get daily scheduled reports.
     $dailyreportstorun = report_customsql_get_ready_to_run_daily_reports($timenow);
     // Get weekly and monthly scheduled reports.
     $scheduledreportstorun = $DB->get_records_select('report_customsql_queries', "(runable = 'weekly' AND lastrun < :startofthisweek) OR\n                                             (runable = 'monthly' AND lastrun < :startofthismonth)", array('startofthisweek' => $startofthisweek, 'startofthismonth' => $startofthismonth), 'lastrun');
     // All reports ready to run.
     $reportstorun = array_merge($dailyreportstorun, $scheduledreportstorun);
     foreach ($reportstorun as $report) {
         mtrace("... Running report " . strip_tags($report->displayname));
         try {
             report_customsql_generate_csv($report, $timenow);
         } catch (\Exception $e) {
             mtrace("... REPORT FAILED " . $e->getMessage());
         }
     }
 }
Пример #2
0
/**
 * Cron that runs any automatically scheduled reports weekly or monthly.
 */
function report_customsql_cron()
{
    global $CFG, $DB;
    $timenow = time();
    if (!empty($CFG->reportcustomsqlmaxruntime)) {
        $timestop = $timenow + $CFG->reportcustomsqlmaxruntime;
    } else {
        $timestop = $timenow + 180;
        // Three minutes.
    }
    list($startofthisweek, $startoflastweek) = report_customsql_get_week_starts($timenow);
    list($startofthismonth) = report_customsql_get_month_starts($timenow);
    mtrace("... Looking for old temp CSV files to delete.");
    $numdeleted = report_customsql_delete_old_temp_files($startoflastweek);
    if ($numdeleted) {
        mtrace("... {$numdeleted} old temporary files deleted.");
    }
    // Get daily scheduled reports.
    $dailyreportstorun = report_customsql_get_ready_to_run_daily_reports($timenow);
    // Get weekly and monthly scheduled reports.
    $scheduledreportstorun = $DB->get_records_select('report_customsql_queries', "(runable = 'weekly' AND lastrun < :startofthisweek) OR\n                                         (runable = 'monthly' AND lastrun < :startofthismonth)", array('startofthisweek' => $startofthisweek, 'startofthismonth' => $startofthismonth), 'lastrun');
    // All reports ready to run.
    $reportstorun = array_merge($dailyreportstorun, $scheduledreportstorun);
    if (empty($reportstorun)) {
        return;
    }
    while (!empty($reportstorun) && time() < $timestop) {
        $report = array_shift($reportstorun);
        mtrace("... Running report " . strip_tags($report->displayname));
        try {
            report_customsql_generate_csv($report, $timenow);
        } catch (Exception $e) {
            mtrace("... REPORT FAILED " . $e->getMessage());
        }
    }
}
 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));
 }