forked from moodleou/moodle-report_customsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecution_action.php
More file actions
315 lines (269 loc) · 10.7 KB
/
execution_action.php
File metadata and controls
315 lines (269 loc) · 10.7 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?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/>.
/**
* Unified handler for execution actions (cancel, delete, etc.).
*
* @package report_customsql
* @copyright 2025 ISB Bayern
* @author Dr. Peter Mayer
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(dirname(__FILE__) . '/../../config.php');
require_once(dirname(__FILE__) . '/locallib.php');
require_once($CFG->libdir . '/adminlib.php');
$executionid = optional_param('id', 0, PARAM_INT);
$queryid = optional_param('queryid', 0, PARAM_INT);
$action = required_param('action', PARAM_ALPHA);
$returnurl = optional_param('returnurl', '', PARAM_LOCALURL);
$confirm = optional_param('confirm', 0, PARAM_BOOL);
// Validate action.
$validactions = ['cancel', 'delete', 'run'];
if (!in_array($action, $validactions)) {
throw new moodle_exception('invalidaction', 'error');
}
// Validate required parameters based on action.
if ($action === 'run' && $queryid <= 0) {
throw new moodle_exception('missingqueryid', 'report_customsql');
}
if (in_array($action, ['cancel', 'delete']) && $executionid <= 0) {
throw new moodle_exception('missingexecutionid', 'report_customsql');
}
require_login();
$context = context_system::instance();
require_capability('report/customsql:view', $context);
// Run action additionally requires executebackground capability.
if ($action === 'run') {
require_capability('report/customsql:executebackground', $context);
}
// Get records based on action type.
if ($action === 'run') {
// For run action, get query record.
$query = $DB->get_record('report_customsql_queries', ['id' => $queryid], '*', MUST_EXIST);
$execution = null;
// Check if query supports async execution.
if ($query->runable !== 'manual_async') {
throw new moodle_exception(
'querynotasync',
'report_customsql',
new moodle_url('/report/customsql/index.php')
);
}
} else {
// For cancel/delete actions, get execution record.
$execution = $DB->get_record('report_customsql_executions', ['id' => $executionid], '*', MUST_EXIST);
$query = $DB->get_record('report_customsql_queries', ['id' => $execution->queryid], '*', MUST_EXIST);
// Check permissions: user must own the execution or have viewallexecutions capability.
$canviewall = has_capability('report/customsql:viewallexecutions', $context);
$isowner = $execution->userid == $USER->id;
if (!$canviewall && !$isowner) {
$errorkey = 'nopermissionto' . $action . 'execution';
throw new moodle_exception(
$errorkey,
'report_customsql',
new moodle_url('/report/customsql/index.php')
);
}
}
// Action-specific validation.
if ($action === 'cancel') {
$validstatuses = ['pending', 'running'];
if (!in_array($execution->status, $validstatuses) || $execution->cancelled) {
// Build detailed error message for debugging.
$debuginfo = sprintf(
'Cannot cancel execution: status=%s (valid: %s), cancelled=%d',
$execution->status,
implode(', ', $validstatuses),
$execution->cancelled
);
throw new moodle_exception(
'cannotcancelexecution',
'report_customsql',
new moodle_url('/report/customsql/executions.php', ['queryid' => $execution->queryid]),
$debuginfo
);
}
}
// Determine return URL.
if (empty($returnurl)) {
if ($action === 'run') {
$returnurl = new moodle_url('/report/customsql/executions.php', ['queryid' => $queryid]);
} else {
$returnurl = new moodle_url('/report/customsql/executions.php', ['queryid' => $execution->queryid]);
}
} else {
$returnurl = new moodle_url($returnurl);
}
// Setup page.
$PAGE->set_url('/report/customsql/execution_action.php', ['id' => $executionid, 'action' => $action]);
$PAGE->set_context($context);
$PAGE->set_pagelayout('report');
// Action-specific strings.
$pagetitle = get_string($action . 'execution', 'report_customsql');
$PAGE->set_title($pagetitle);
$PAGE->set_heading(get_string('pluginname', 'report_customsql'));
// Navigation.
$PAGE->navbar->add(get_string('pluginname', 'report_customsql'), new moodle_url('/report/customsql/index.php'));
if ($action !== 'run') {
$PAGE->navbar->add(
get_string('viewexecutions', 'report_customsql'),
new moodle_url('/report/customsql/executions.php', ['queryid' => $execution->queryid])
);
}
$PAGE->navbar->add($pagetitle);
// Handle confirmation.
if ($confirm && confirm_sesskey()) {
try {
switch ($action) {
case 'cancel':
\report_customsql\local\execution_manager::cancel_execution($executionid);
$successmsg = get_string('executioncancelled', 'report_customsql');
break;
case 'delete':
\report_customsql\local\execution_manager::delete_execution($executionid);
$successmsg = get_string('executiondeleted', 'report_customsql');
break;
case 'run':
// Create background execution (includes execution limit check internally).
$newexecutionid = \report_customsql\local\execution_manager::create_background_execution(
$queryid,
$USER->id,
[] // Empty params for now - can be extended later.
);
$successmsg = get_string('executionqueued', 'report_customsql');
// Redirect to executions page to see the new execution.
$returnurl = new moodle_url('/report/customsql/executions.php', ['queryid' => $queryid]);
break;
}
redirect($returnurl, $successmsg, null, \core\output\notification::NOTIFY_SUCCESS);
} catch (Exception $e) {
redirect(
$returnurl,
get_string('actionfailed', 'report_customsql'),
null,
\core\output\notification::NOTIFY_ERROR
);
}
}
// Show confirmation form.
echo $OUTPUT->header();
echo $OUTPUT->heading($pagetitle, 2);
// Show action-specific warning.
if ($action === 'cancel') {
echo $OUTPUT->notification(get_string('cancelexecution_warning', 'report_customsql'), 'warning');
}
// Show details based on action.
if ($action === 'run') {
// For run action, show query details.
echo html_writer::tag('p', get_string('confirmrunexecution', 'report_customsql'));
$table = new html_table();
$table->attributes['class'] = 'generaltable';
$table->data = [];
$table->data[] = [
html_writer::tag('strong', get_string('query', 'report_customsql')),
format_string($query->displayname),
];
if (!empty($query->description)) {
$table->data[] = [
html_writer::tag('strong', get_string('description')),
format_text($query->description, FORMAT_HTML),
];
}
echo html_writer::table($table);
} else {
// For cancel/delete actions, show execution details.
$executioninfo = new stdClass();
$executioninfo->created = userdate($execution->timecreated, get_string('strftimedatetime'));
$executioninfo->status = get_string('status_' . $execution->status, 'report_customsql');
$confirmmsg = get_string('confirm' . $action . 'execution', 'report_customsql', $executioninfo);
echo html_writer::tag('p', $confirmmsg);
// Show details table.
$table = new html_table();
$table->attributes['class'] = 'generaltable';
$table->data = [];
$table->data[] = [
html_writer::tag('strong', get_string('query', 'report_customsql')),
format_string($query->displayname),
];
$table->data[] = [
html_writer::tag('strong', get_string('status', 'report_customsql')),
get_string('status_' . $execution->status, 'report_customsql'),
];
$table->data[] = [
html_writer::tag('strong', get_string('created', 'report_customsql')),
userdate($execution->timecreated, get_string('strftimedatetime')),
];
// Cancel: show running details.
if ($action === 'cancel' && $execution->timestarted) {
$table->data[] = [
html_writer::tag('strong', get_string('started', 'report_customsql')),
userdate($execution->timestarted, get_string('strftimedatetime')),
];
$runningtime = time() - $execution->timestarted;
$table->data[] = [
html_writer::tag('strong', get_string('runningfor', 'report_customsql')),
format_time($runningtime),
];
}
// Delete: show completion details.
if ($action === 'delete') {
if ($execution->timecompleted) {
$table->data[] = [
html_writer::tag('strong', get_string('completed', 'report_customsql')),
userdate($execution->timecompleted, get_string('strftimedatetime')),
];
}
if ($execution->rowsreturned) {
$table->data[] = [
html_writer::tag('strong', get_string('rows', 'report_customsql')),
$execution->rowsreturned,
];
}
if ($execution->filesize) {
$table->data[] = [
html_writer::tag('strong', get_string('filesize', 'report_customsql')),
display_size($execution->filesize),
];
}
if ($execution->status === 'failed' && $execution->errormessage) {
$table->data[] = [
html_writer::tag('strong', get_string('error')),
s($execution->errormessage),
];
}
}
echo html_writer::table($table);
}
// Confirmation buttons.
$confirmparams = [
'action' => $action,
'confirm' => 1,
'sesskey' => sesskey(),
];
if ($action === 'run') {
$confirmparams['queryid'] = $queryid;
} else {
$confirmparams['id'] = $executionid;
}
if (!empty($returnurl)) {
$confirmparams['returnurl'] = $returnurl->out_as_local_url(false);
}
$confirmurl = new moodle_url('/report/customsql/execution_action.php', $confirmparams);
echo $OUTPUT->confirm(
get_string('areyousure'),
$confirmurl,
$returnurl
);
echo $OUTPUT->footer();