Repository navigation
Expand file tree
/
Copy pathimport_customsql.php
More file actions
158 lines (136 loc) · 5.63 KB
/
Copy pathimport_customsql.php
File metadata and controls
158 lines (136 loc) · 5.63 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
<?php
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
/**
* Import SQL reports from the Ad-hoc Database Queries report as draft SQL data sources.
*
* Lists every report_customsql_queries row split into importable (translates cleanly through
* {@see \report_sql\local\customsql_import}) and rejected (with a reason). The admin ticks
* the ones to import; each lands as a fresh draft owned by the current user, ready to publish.
*
* @package report_sql
* @copyright 2026 Marcus Green
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require(__DIR__ . '/../../config.php');
require_once($CFG->libdir . '/adminlib.php');
use report_sql\local\customsql_import;
require_login();
admin_externalpage_setup('report_sql_importcustomsql');
$indexurl = new moodle_url('/report/sql/index.php');
$pageurl = new moodle_url('/report/sql/import_customsql.php');
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('customsqlimport:title', 'report_sql'));
if (!customsql_import::available()) {
echo $OUTPUT->notification(
get_string('customsqlimport:unavailable', 'report_sql'),
\core\output\notification::NOTIFY_WARNING
);
echo html_writer::link($indexurl, get_string('back'));
echo $OUTPUT->footer();
exit;
}
// Handle the submitted selection.
if (optional_param('import', 0, PARAM_BOOL) && confirm_sesskey()) {
$ids = optional_param_array('report', [], PARAM_INT);
if (empty($ids)) {
redirect(
$pageurl,
get_string('crimport:noneselected', 'report_sql'),
null,
\core\output\notification::NOTIFY_WARNING
);
}
$result = customsql_import::import($ids);
$messages = [get_string('importdone', 'report_sql', $result['imported'])];
if (!empty($result['demoted'])) {
$messages[] = get_string('importdemoted', 'report_sql', implode(', ', array_keys($result['demoted'])));
}
if (!empty($result['skipped'])) {
$messages[] = get_string('importskipped', 'report_sql', implode(', ', array_keys($result['skipped'])));
}
if (!empty($result['rejected'])) {
$messages[] = get_string('importskipped', 'report_sql', implode(', ', array_keys($result['rejected'])));
}
redirect($indexurl, implode(' ', $messages), null, \core\output\notification::NOTIFY_SUCCESS);
}
$classified = customsql_import::discover();
$importable = array_filter($classified, static fn(array $i): bool => $i['verdict'] === 'import');
$rejected = array_filter($classified, static fn(array $i): bool => $i['verdict'] === 'reject');
echo html_writer::tag('p', get_string('customsqlimport:intro', 'report_sql'));
if (empty($importable)) {
echo $OUTPUT->notification(
get_string('customsqlimport:noneimportable', 'report_sql'),
\core\output\notification::NOTIFY_INFO
);
} else {
echo html_writer::start_tag('form', ['method' => 'post', 'action' => $pageurl->out(false)]);
echo html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()]);
echo html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'import', 'value' => 1]);
echo $OUTPUT->heading(get_string('crimport:importableheading', 'report_sql'), 3);
$table = new html_table();
$table->head = [
'',
get_string('crimport:colname', 'report_sql'),
get_string('crimport:colnotes', 'report_sql'),
];
$table->attributes['class'] = 'generaltable';
foreach ($importable as $id => $info) {
$checkbox = html_writer::empty_tag('input', [
'type' => 'checkbox',
'name' => 'report[]',
'value' => $id,
'checked' => 'checked',
'id' => 'customsql_report_' . $id,
]);
$notes = $info['notes']
? html_writer::alist($info['notes'])
: html_writer::tag(
'span',
get_string('crimport:noteclean', 'report_sql'),
['class' => 'text-muted']
);
$table->data[] = [
$checkbox,
html_writer::label(s($info['name']), 'customsql_report_' . $id),
$notes,
];
}
echo html_writer::table($table);
echo html_writer::div(
html_writer::empty_tag('input', [
'type' => 'submit',
'class' => 'btn btn-primary',
'value' => get_string('crimport:importselected', 'report_sql'),
]),
'mt-2'
);
echo html_writer::end_tag('form');
}
if (!empty($rejected)) {
echo $OUTPUT->heading(get_string('crimport:rejectedheading', 'report_sql'), 3);
$table = new html_table();
$table->head = [
get_string('crimport:colname', 'report_sql'),
get_string('crimport:colreason', 'report_sql'),
];
$table->attributes['class'] = 'generaltable';
foreach ($rejected as $info) {
$table->data[] = [s($info['name']), s($info['reason'])];
}
echo html_writer::table($table);
}
echo html_writer::div(html_writer::link($indexurl, get_string('back')), 'mt-3');
echo $OUTPUT->footer();