Exemplo n.º 1
0
 public function cron()
 {
     global $CFG, $DB;
     $hour = get_config('block_configurable_reports', 'cron_hour');
     $min = get_config('block_configurable_reports', 'cron_minute');
     $date = usergetdate(time());
     $usertime = mktime($date['hours'], $date['minutes'], $date['seconds'], $date['mon'], $date['mday'], $date['year']);
     $crontime = mktime($hour, $min, $date['seconds'], $date['mon'], $date['mday'], $date['year']);
     if ($crontime - $usertime < 0) {
         return false;
     }
     $lastcron = $DB->get_field('block', 'lastcron', array('name' => 'configurable_reports'));
     if (!$lastcron and $lastcron + $this->cron < time()) {
         return false;
     }
     // Starting to run...
     //$DB->set_field('blocks', 'lastcron',time(), array('name' => 'configurable_reports'));
     require_once $CFG->dirroot . "/blocks/configurable_reports/locallib.php";
     require_once $CFG->dirroot . '/blocks/configurable_reports/report.class.php';
     require_once $CFG->dirroot . '/blocks/configurable_reports/reports/sql/report.class.php';
     mtrace("\nConfigurable report (block)");
     $reports = $DB->get_records('block_configurable_reports');
     if ($reports) {
         foreach ($reports as $report) {
             // Running only SQL reports. $report->type == 'sql'.
             if ($report->type == 'sql' and (!empty($report->cron) and $report->cron == '1')) {
                 $reportclass = new report_sql($report);
                 // Execute it using $remotedb.
                 $starttime = microtime(true);
                 mtrace("\nExecuting query '{$report->name}'");
                 $components = cr_unserialize($reportclass->config->components);
                 $config = isset($components['customsql']['config']) ? $components['customsql']['config'] : new stdclass();
                 $sql = $reportclass->prepare_sql($config->querysql);
                 $sqlqueries = explode(';', $sql);
                 foreach ($sqlqueries as $sql) {
                     mtrace(substr($sql, 0, 60));
                     // Show some SQL.
                     $results = $reportclass->execute_query($sql);
                     mtrace($results == 1 ? '...OK time=' . round((microtime(true) - $starttime) * 1000) . 'mSec' : 'Some SQL Error' . '\\n');
                 }
                 unset($reportclass);
             }
         }
     }
     return true;
     // Finished OK.
 }
$totalrecords = 0;

$sql = '';
if (isset($config->querysql)) {
    // FILTERS
    $sql = $config->querysql;
    if (!empty($filters)) {
        foreach ($filters as $f) {
            require_once($CFG->dirroot . '/blocks/cobalt_reports/components/filters/' . $f['pluginname'] . '/plugin.class.php');
            $classname = 'plugin_' . $f['pluginname'];
            $class = new $classname($rconfig);
            $sql = $class->execute($sql, $f['formdata']);
        }
    }

    $sql = $rsql->prepare_sql($sql);
    $i = 0;
    if ($rs = $rsql->execute_query($sql)) {
        foreach ($rs as $row) {
            if (empty($finaltable)) {
                foreach ($row as $colname => $value) {
                    $tablehead[] = str_replace('_', ' ', $colname);
                    $columns[] = array('db' => '$colname', 'dt' => $i);
                    $i++;
                }
            }
            $array_row = array_values((array) $row);
            foreach ($array_row as $ii => $cell) {
                $array_row[$ii] = str_replace('[[QUESTIONMARK]]', '?', $cell);
            }
            $totalrecords++;