/** * 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()); } } }
/** * Cron that runs any automatically scheduled reports weekly or monthly. */ function report_customsql_cron() { global $CFG; $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} files deleted."); } $reportstorun = get_records_select('report_customsql_queries', "(runable = 'weekly' AND lastrun < {$startofthisweek}) OR\n (runable = 'monthly' AND lastrun < {$startofthismonth})", 'lastrun'); 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()); } } }
/** * 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_get_week_starts_test() { $this->assertEquals(array(strtotime('00:00 7 November 2009'), strtotime('00:00 31 October 2009')), report_customsql_get_week_starts(strtotime('12:36 10 November 2009'))); $this->assertEquals(array(strtotime('00:00 7 November 2009'), strtotime('00:00 31 October 2009')), report_customsql_get_week_starts(strtotime('00:00 7 November 2009'))); $this->assertEquals(array(strtotime('00:00 7 November 2009'), strtotime('00:00 31 October 2009')), report_customsql_get_week_starts(strtotime('23:59 13 November 2009'))); }
function report_customsql_get_starts($report, $timenow) { switch ($report->runable) { case 'daily': return report_customsql_get_daily_time_starts($timenow, $report->at); case 'weekly': return report_customsql_get_week_starts($timenow); case 'monthly': return report_customsql_get_month_starts($timenow); default: throw new Exception('unexpected $report->runable.'); } }