/**
  * 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());
        }
    }
}
require_once $CFG->libdir . '/adminlib.php';
require_once $CFG->libdir . '/validateurlsyntax.php';
$id = required_param('id', PARAM_INT);
$report = get_record('report_customsql_queries', 'id', $id);
if (!$report) {
    print_error('invalidreportid', 'report_customsql', report_customsql_url('index.php'), $id);
}
require_login();
$context = get_context_instance(CONTEXT_SYSTEM);
if (!empty($report->capability)) {
    require_capability($report->capability, $context);
}
report_customsql_log_view($id);
if ($report->runable == 'manual') {
    try {
        $cvstimestamp = report_customsql_generate_csv($report, time());
        // Get the updated execution times.
        $report = get_record('report_customsql_queries', 'id', $id);
    } catch (Exception $e) {
        print_error('queryfailed', 'report_customsql', report_customsql_url('index.php'), $e);
    }
} else {
    $cvstimestamp = optional_param('timestamp', time(), PARAM_INT);
}
// Start the page.
admin_externalpage_setup('reportcustomsql');
admin_externalpage_print_header();
print_heading(format_string($report->displayname));
echo '<style>';
// (nadavkav)
//include('styles.php');