Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * Configurable Reports
19
 * A Moodle block for creating customizable reports
20
 * @package blocks
21
 * @author: Juan leyva <http://www.twitter.com/jleyvadelgado>
22
 * @date: 2009
23
 */
24
 
25
defined('BLOCK_CONFIGURABLE_REPORTS_MAX_RECORDS') || define('BLOCK_CONFIGURABLE_REPORTS_MAX_RECORDS', 5000);
26
 
27
class report_sql extends report_base {
28
 
29
    private $forExport = false;
30
 
31
    public function setForExport(bool $isForExport) {
32
        $this->forExport = $isForExport;
33
    }
34
 
35
    public function isForExport() {
36
        return $this->forExport;
37
    }
38
 
39
    public function init() {
40
        $this->components = array('customsql', 'filters', 'template', 'permissions', 'calcs', 'plot');
41
    }
42
 
43
    public function prepare_sql($sql) {
44
        global $DB, $USER, $CFG, $COURSE;
45
 
46
        // Enable debug mode from SQL query.
47
        $this->config->debug = (strpos($sql, '%%DEBUG%%') !== false) ? true : false;
48
 
49
        // Pass special custom undefined variable as filter.
50
        // Security warning !!! can be used for sql injection.
51
        // Use %%FILTER_VAR%% in your sql code with caution.
52
        $filtervar = optional_param('filter_var', '', PARAM_RAW);
53
        if (!empty($filtervar)) {
54
            $sql = str_replace('%%FILTER_VAR%%', $filtervar, $sql);
55
        }
56
 
57
        $sql = str_replace('%%USERID%%', $USER->id, $sql);
58
        $sql = str_replace('%%COURSEID%%', $COURSE->id, $sql);
59
        $sql = str_replace('%%CATEGORYID%%', $COURSE->category, $sql);
60
 
61
        // See http://en.wikipedia.org/wiki/Year_2038_problem.
62
        $sql = str_replace(array('%%STARTTIME%%', '%%ENDTIME%%'), array('0', '2145938400'), $sql);
63
        $sql = str_replace('%%WWWROOT%%', $CFG->wwwroot, $sql);
64
        $sql = preg_replace('/%{2}[^%]+%{2}/i', '', $sql);
65
 
66
        $sql = str_replace('?', '[[QUESTIONMARK]]', $sql);
67
 
68
        return $sql;
69
    }
70
 
71
    public function execute_query($sql, $limitnum = BLOCK_CONFIGURABLE_REPORTS_MAX_RECORDS) {
72
        global $remotedb, $DB, $CFG;
73
 
74
        $sql = preg_replace('/\bprefix_(?=\w+)/i', $CFG->prefix, $sql);
75
 
76
        $reportlimit = get_config('block_configurable_reports', 'reportlimit');
77
        if (empty($reportlimit) or $reportlimit == '0') {
78
                $reportlimit = BLOCK_CONFIGURABLE_REPORTS_MAX_RECORDS;
79
        }
80
 
81
        $starttime = microtime(true);
82
 
83
        if (preg_match('/\b(INSERT|INTO|CREATE)\b/i', $sql) && !empty($CFG->block_configurable_reports_enable_sql_execution)) {
84
            // Run special (dangerous) queries directly.
85
            $results = $remotedb->execute($sql);
86
        } else {
87
            $results = $remotedb->get_recordset_sql($sql, null, 0, $reportlimit);
88
        }
89
 
90
        // Update the execution time in the DB.
91
        $updaterecord = $DB->get_record('block_configurable_reports', array('id' => $this->config->id));
92
        $updaterecord->lastexecutiontime = round((microtime(true) - $starttime) * 1000);
93
        $this->config->lastexecutiontime = $updaterecord->lastexecutiontime;
94
 
95
        $DB->update_record('block_configurable_reports', $updaterecord);
96
 
97
        return $results;
98
    }
99
 
100
    public function create_report() {
101
        global $DB, $CFG;
102
 
103
        $components = cr_unserialize($this->config->components);
104
 
105
        $filters = (isset($components['filters']['elements'])) ? $components['filters']['elements'] : array();
106
        $calcs = (isset($components['calcs']['elements'])) ? $components['calcs']['elements'] : array();
107
 
108
        $tablehead = array();
109
        $finalcalcs = array();
110
        $finaltable = array();
111
        $tablehead = array();
112
 
113
        $components = cr_unserialize($this->config->components);
114
        $config = (isset($components['customsql']['config'])) ? $components['customsql']['config'] : new \stdclass;
115
        $totalrecords = 0;
116
 
117
        $sql = '';
118
        if (isset($config->querysql)) {
119
            // Filters.
120
            $sql = $config->querysql;
121
            if (!empty($filters)) {
122
                foreach ($filters as $f) {
123
                    require_once($CFG->dirroot.'/blocks/configurable_reports/components/filters/'.$f['pluginname'].'/plugin.class.php');
124
                    $classname = 'plugin_'.$f['pluginname'];
125
                    $class = new $classname($this->config);
126
                    $sql = $class->execute($sql, $f['formdata']);
127
                }
128
            }
129
 
130
            $sql = $this->prepare_sql($sql);
131
 
132
            if ($rs = $this->execute_query($sql)) {
133
                foreach ($rs as $row) {
134
                    if (empty($finaltable)) {
135
                        foreach ($row as $colname => $value) {
136
                            $tablehead[] = $colname;
137
                        }
138
                    }
139
                    $arrayrow = array_values((array) $row);
140
                    foreach ($arrayrow as $ii => $cell) {
141
                        if (!$this->isForExport()) {
142
                            $cell = format_text($cell, FORMAT_HTML, array('trusted' => true, 'noclean' => true, 'para' => false));
143
                        }
144
                        $arrayrow[$ii] = str_replace('[[QUESTIONMARK]]', '?', $cell);
145
                    }
146
                    $totalrecords++;
147
                    $finaltable[] = $arrayrow;
148
                }
149
            }
150
        }
151
        $this->sql = $sql;
152
        $this->totalrecords = $totalrecords;
153
 
154
        // Calcs.
155
 
156
        $finalcalcs = $this->get_calcs($finaltable, $tablehead);
157
 
158
        $table = new \stdclass;
159
        $table->id = 'reporttable';
160
        $table->data = $finaltable;
161
        $table->head = $tablehead;
162
 
163
        $calcs = new \html_table();
164
        $calcs->id = 'calcstable';
165
        $calcs->data = array($finalcalcs);
166
        $calcs->head = $tablehead;
167
 
168
        if (!$this->finalreport) {
169
            $this->finalreport = new \stdClass;
170
        }
171
        $this->finalreport->table = $table;
172
        $this->finalreport->calcs = $calcs;
173
 
174
        return true;
175
    }
176
 
177
}
178