-
Notifications
You must be signed in to change notification settings - Fork 100
/
view.php
265 lines (223 loc) · 9.72 KB
/
view.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Script to view a particular custom SQL report.
*
* @package report_customsql
* @copyright 2009 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('NO_OUTPUT_BUFFERING', true);
require_once(dirname(__FILE__) . '/../../config.php');
require_once(dirname(__FILE__) . '/locallib.php');
require_once(dirname(__FILE__) . '/view_form.php');
require_once($CFG->libdir . '/adminlib.php');
$id = required_param('id', PARAM_INT);
$urlparams = ['id' => $id];
$report = $DB->get_record('report_customsql_queries', ['id' => $id]);
if (!$report) {
throw new moodle_exception('invalidreportid', 'report_customsql', report_customsql_url('index.php'), $id);
}
$category = $DB->get_record('report_customsql_categories', ['id' => $report->categoryid], '*', MUST_EXIST);
$embed = optional_param('embed', 0, PARAM_BOOL);
$urlparams['embed'] = $embed;
// Setup the page.
admin_externalpage_setup('report_customsql', '', $urlparams,
'/report/customsql/view.php', ['pagelayout' => 'report']);
$PAGE->set_title(format_string($report->displayname));
$PAGE->navbar->add(format_string($category->name), report_customsql_url('category.php', ['id' => $report->categoryid]));
$PAGE->navbar->add(format_string($report->displayname));
if ($embed) {
$PAGE->set_pagelayout('embedded');
}
$output = $PAGE->get_renderer('report_customsql');
$context = context_system::instance();
if (!empty($report->capability)) {
require_capability($report->capability, $context);
}
report_customsql_log_view($id);
// We don't want slow reports blocking the session in other tabs.
\core\session\manager::write_close();
if ($report->runable == 'manual') {
// Allow query parameters to be entered.
if (!empty($report->queryparams)) {
$queryparams = report_customsql_get_query_placeholders_and_field_names($report->querysql);
// Get any query param values that are given in the URL.
$paramvalues = [];
foreach ($queryparams as $queryparam => $notused) {
$value = optional_param($queryparam, null, PARAM_RAW);
if ($value !== null && $value !== '') {
$paramvalues[$queryparam] = $value;
}
}
$relativeurl = 'view.php?id=' . $id;
$mform = new report_customsql_view_form(report_customsql_url($relativeurl), $queryparams);
$formdefaults = [];
if ($report->queryparams) {
foreach (unserialize($report->queryparams) as $queryparam => $defaultvalue) {
$formdefaults[$queryparams[$queryparam]] = $defaultvalue;
}
}
foreach ($paramvalues as $queryparam => $value) {
$formdefaults[$queryparams[$queryparam]] = $value;
}
$mform->set_data($formdefaults);
if ($mform->is_cancelled()) {
redirect(report_customsql_url('index.php'));
}
if (($newreport = $mform->get_data()) || count($paramvalues) == count($queryparams)) {
// Pick up named parameters into serialised array.
if ($newreport) {
foreach ($queryparams as $queryparam => $formparam) {
$paramvalues[$queryparam] = $newreport->{$formparam};
}
}
$report->queryparams = serialize($paramvalues);
} else {
admin_externalpage_setup('report_customsql', '', $urlparams,
'/report/customsql/view.php');
$PAGE->set_title(format_string($report->displayname));
echo $OUTPUT->header();
echo $OUTPUT->heading(format_string($report->displayname));
if (!html_is_blank($report->description)) {
echo html_writer::tag('p', format_text($report->description, FORMAT_HTML));
}
$mform->display();
echo $output->render_report_actions($report, $category, $context);
echo $OUTPUT->footer();
die;
}
}
try {
$csvtimestamp = report_customsql_generate_csv($report, time());
// Get the updated execution times.
$report = $DB->get_record('report_customsql_queries', ['id' => $id]);
} catch (Exception $e) {
throw new moodle_exception('queryfailed', 'report_customsql', report_customsql_url('index.php'),
$e->getMessage());
}
} else {
// Runs on schedule.
$csvtimestamp = optional_param('timestamp', null, PARAM_INT);
if ($csvtimestamp === null) {
[$csvtimestamp] = report_customsql_get_starts($report, time());
}
$urlparams['timestamp'] = $csvtimestamp;
}
// Output.
echo $OUTPUT->header();
echo $OUTPUT->heading(format_string($report->displayname));
if (!html_is_blank($report->description)) {
echo html_writer::tag('p', format_text($report->description, FORMAT_HTML));
}
if (!empty($paramvalues)) {
foreach ($paramvalues as $name => $value) {
if (report_customsql_get_element_type($name) == 'date_time_selector') {
$value = userdate($value, '%F %T');
}
echo html_writer::tag('p', get_string('parametervalue', 'report_customsql',
['name' => html_writer::tag('b', str_replace('_', ' ', $name)),
'value' => s($value)]));
}
}
$count = 0;
if (is_null($csvtimestamp)) {
echo html_writer::tag('p', get_string('nodatareturned', 'report_customsql'));
} else {
list($csvfilename, $csvtimestamp) = report_customsql_csv_filename($report, $csvtimestamp);
if (!is_readable($csvfilename)) {
if (empty($report->lastrun) || $csvtimestamp > $report->lastrun) {
echo html_writer::tag('p', get_string('notrunyet', 'report_customsql'));
} else {
echo html_writer::tag('p', get_string('notanyresults', 'report_customsql', userdate($csvtimestamp)));
}
} else {
$handle = fopen($csvfilename, 'r');
if ($report->runable != 'manual' && !$report->singlerow) {
echo $OUTPUT->heading(get_string('reportfor', 'report_customsql',
userdate($csvtimestamp)), 3);
}
$table = new html_table();
$table->id = 'report_customsql_results';
list($table->head, $linkcolumns) = report_customsql_get_table_headers(
report_customsql_read_csv_row($handle));
$rowlimitexceeded = false;
while ($row = report_customsql_read_csv_row($handle)) {
$data = report_customsql_display_row($row, $linkcolumns);
if (isset($data[0]) && $data[0] === REPORT_CUSTOMSQL_LIMIT_EXCEEDED_MARKER) {
$rowlimitexceeded = true;
} else {
$table->data[] = $data;
$count += 1;
}
}
// For scheduled reports that accumulate one row at a time,
// show most recent data first.
if ($report->runable != 'manual' && $report->singlerow) {
$table->data = array_reverse($table->data);
}
fclose($handle);
echo html_writer::table($table);
if ($rowlimitexceeded) {
echo html_writer::tag('p', get_string('recordlimitreached', 'report_customsql',
$report->querylimit ?? get_config('report_customsql', 'querylimitdefault')),
['class' => 'admin_note']);
} else {
echo html_writer::tag('p', get_string('recordcount', 'report_customsql', $count),
['class' => 'admin_note']);
}
echo report_customsql_time_note($report, 'p');
$urlparams = [];
if (!empty($paramvalues)) {
$urlparams = $paramvalues;
}
$urlparams['timestamp'] = $csvtimestamp;
$downloadurl = report_customsql_downloadurl($id, $urlparams);
echo $OUTPUT->download_dataformat_selector(get_string('downloadthisreportas', 'report_customsql'),
$downloadurl, 'dataformat', $urlparams);
}
}
if (!empty($queryparams)) {
echo html_writer::tag('p',
$OUTPUT->action_link(
report_customsql_url('view.php', ['id' => $id]),
$OUTPUT->pix_icon('t/editstring', '') . ' ' .
get_string('changetheparameters', 'report_customsql')));
}
echo $output->render_report_actions($report, $category, $context);
if ($report->runable != 'manual') {
echo $OUTPUT->heading(get_string('archivedversions', 'report_customsql'), 3);
$archivetimes = report_customsql_get_archive_times($report);
if (!$archivetimes) {
echo html_writer::tag('p', get_string('notrunyet', 'report_customsql'));
} else {
echo html_writer::start_tag('ul');
foreach ($archivetimes as $time) {
$formattedtime = userdate($time);
echo html_writer::start_tag('li');
if ($time == $csvtimestamp) {
echo html_writer::tag('b', $formattedtime);
} else {
echo html_writer::tag('a', $formattedtime,
['href' => report_customsql_url('view.php',
['id' => $id, 'timestamp' => $time])]);
}
echo '</li>';
}
echo html_writer::end_tag('ul');
}
}
echo $OUTPUT->footer();