forked from moodleou/moodle-report_customsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit_form.php
249 lines (211 loc) · 11.1 KB
/
edit_form.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
<?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/>.
/**
* Form for editing a custom SQL report.
*
* @package report_customsql
* @copyright 2009 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/formslib.php');
require_once(dirname(__FILE__) . '/locallib.php');
/**
* Form for editing a custom SQL report.
*
* @copyright © 2009 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class report_customsql_edit_form extends moodleform {
public function definition() {
global $CFG;
$mform = $this->_form;
$mform->addElement('text', 'displayname', get_string('displayname', 'report_customsql'));
$mform->addRule('displayname', get_string('displaynamerequired', 'report_customsql'),
'required', null, 'client');
$mform->setType('displayname', PARAM_TEXT);
$mform->addElement('editor', 'description',
get_string('description', 'report_customsql'));
$mform->setType('description', PARAM_RAW);
$mform->addElement('textarea', 'querysql', get_string('querysql', 'report_customsql'),
'rows="25" cols="50"');
$mform->addRule('querysql', get_string('querysqlrequried', 'report_customsql'),
'required', null, 'client');
$mform->setType('querysql', PARAM_RAW);
$mform->addElement('submit', 'verify', get_string('verifyqueryandupdate', 'report_customsql'));
$mform->registerNoSubmitButton('verify');
$hasparameters = 0;
if (count($this->_customdata)) {
$mform->addElement('static', 'params', '', get_string('queryparams', 'report_customsql'));
foreach ($this->_customdata as $queryparam => $formparam) {
$type = report_customsql_get_element_type($queryparam);
$mform->addElement($type, $formparam, $queryparam);
if ($type == 'text') {
$mform->setType($formparam, PARAM_RAW);
}
$hasparameters++;
}
$mform->addElement('static', 'spacer', '', '');
}
$mform->addElement('static', 'note', get_string('note', 'report_customsql'),
get_string('querynote', 'report_customsql', $CFG->wwwroot));
$capabilityoptions = report_customsql_capability_options();
$mform->addElement('select', 'capability', get_string('whocanaccess', 'report_customsql'),
$capabilityoptions);
end($capabilityoptions);
$mform->setDefault('capability', key($capabilityoptions));
$mform->addElement('text', 'querylimit', get_string('querylimit', 'report_customsql'));
$mform->setType('querylimit', PARAM_INT);
$mform->setDefault('querylimit', REPORT_CUSTOMSQL_MAX_RECORDS);
$mform->addRule('querylimit', get_string('requireint', 'report_customsql'),
'numeric', null, 'client');
$runat = array();
if ($hasparameters) {
$runat[] = $mform->createElement('select', 'runable', null, report_customsql_runable_options('manual'));
} else {
$runat[] = $mform->createElement('select', 'runable', null, report_customsql_runable_options());
}
$runat[] = $mform->createElement('select', 'at', null, report_customsql_daily_at_options());
$mform->addGroup($runat, 'runablegroup', get_string('runable', 'report_customsql'),
get_string('at', 'report_customsql'), false);
$mform->addElement('checkbox', 'singlerow', get_string('typeofresult', 'report_customsql'),
get_string('onerow', 'report_customsql'));
$mform->addElement('text', 'customdir', get_string('customdir', 'report_customsql'), 'size = 70');
$mform->setType('customdir', PARAM_PATH);
$mform->disabledIf('customdir', 'runable', 'eq', 'manual');
$mform->addHelpButton('customdir', 'customdir', 'report_customsql');
$mform->addElement('text', 'emailto', get_string('emailto', 'report_customsql'), 'size = 70');
$mform->addElement('select', 'emailwhat', get_string('emailwhat', 'report_customsql'),
report_customsql_email_options());
$mform->disabledIf('singlerow', 'runable', 'eq', 'manual');
$mform->disabledIf('at', 'runable', 'ne', 'daily');
$mform->disabledIf('emailto', 'runable', 'eq', 'manual');
$mform->disabledIf('emailwhat', 'runable', 'eq', 'manual');
$mform->setType('emailto', PARAM_RAW);
// Add new category selection.
$categoryoptions = report_customsql_category_options();
$mform->addElement('select', 'categoryid', get_string('selectcategory', 'report_customsql'),
$categoryoptions);
$catdefault = isset($categoryoptions[1]) ? 1 : key($categoryoptions);
$mform->setDefault('categoryid', $catdefault);
$this->add_action_buttons();
}
public function validation($data, $files) {
global $CFG, $DB, $USER;
$errors = parent::validation($data, $files);
$sql = $data['querysql'];
if (report_customsql_contains_bad_word($sql)) {
// Obviously evil stuff in the SQL.
$errors['querysql'] = get_string('notallowedwords', 'report_customsql',
implode(', ', report_customsql_bad_words_list()));
} else if (strpos($sql, ';') !== false) {
// Do not allow any semicolons.
$errors['querysql'] = get_string('nosemicolon', 'report_customsql');
} else if ($CFG->prefix != '' && preg_match('/\b' . $CFG->prefix . '\w+/i', $sql)) {
// Make sure prefix is prefix_, not explicit.
$errors['querysql'] = get_string('noexplicitprefix', 'report_customsql', $CFG->prefix);
} else if (!array_key_exists('runable', $data)) {
// This happens when the user enters a query including placehoders, and
// selectes Run: Scheduled, and then tries to save the form.
$errors['runablegroup'] = get_string('noscheduleifplaceholders', 'report_customsql');
} else {
// Now try running the SQL, and ensure it runs without errors.
$report = new stdClass;
$report->querysql = $sql;
$report->runable = $data['runable'];
if ($report->runable === 'daily') {
$report->at = $data['at'];
}
$sql = report_customsql_prepare_sql($report, time());
// Check for required query parameters if there are any.
$queryparams = array();
foreach (report_customsql_get_query_placeholders($sql) as $queryparam) {
$queryparam = substr($queryparam, 1);
$formparam = 'queryparam' . $queryparam;
if (!isset($data[$formparam])) {
$errors['params'] = get_string('queryparamschanged', 'report_customsql');
break;
}
$queryparams[$queryparam] = $data[$formparam];
}
if (!isset($errors['params'])) {
try {
$rs = report_customsql_execute_query($sql, $queryparams, 2);
if (!empty($data['singlerow'])) {
// Count rows for Moodle 2 as all Moodle 1.9 useful and more performant
// recordset methods removed.
$rows = 0;
foreach ($rs as $value) {
$rows++;
}
if (!$rows) {
$errors['querysql'] = get_string('norowsreturned', 'report_customsql');
} else if ($rows >= 2) {
$errors['querysql'] = get_string('morethanonerowreturned',
'report_customsql');
}
}
// Check the list of users in emailto field.
if ($data['runable'] !== 'manual') {
if ($invaliduser = report_customsql_validate_users($data['emailto'], $data['capability'])) {
$errors['emailto'] = $invaliduser;
}
}
$rs->close();
} catch (dml_exception $e) {
$errors['querysql'] = get_string('queryfailed', 'report_customsql',
$e->getMessage() . ' ' . $e->debuginfo);
} catch (Exception $e) {
$errors['querysql'] = get_string('queryfailed', 'report_customsql',
$e->getMessage());
}
}
}
// Check querylimit in range 1 .. REPORT_CUSTOMSQL_MAX_RECORDS.
if (empty($data['querylimit']) || $data['querylimit'] > REPORT_CUSTOMSQL_MAX_RECORDS) {
$errors['querylimit'] = get_string('querylimitrange', 'report_customsql', REPORT_CUSTOMSQL_MAX_RECORDS);
}
if (!empty($data['customdir'])) {
$path = $data['customdir'];
// The path either needs to be a writable directory ...
if (is_dir($path) ) {
if (!is_writable($path)) {
$errors['customdir'] = get_string('customdirnotwritable', 'report_customsql', $path);
}
} else if (substr($path, -1) == DIRECTORY_SEPARATOR) {
// ... and it must exist...
$errors['customdir'] = get_string('customdirmustexist', 'report_customsql', $path);
} else {
// ... or be a path to a writable file, or a new file in a writable directory.
$dir = dirname($path);
if (!is_dir($dir)) {
$errors['customdir'] = get_string('customdirnotadirectory', 'report_customsql', $dir);
} else {
if (file_exists($path)) {
if (!is_writable($path)) {
$errors['customdir'] = get_string('filenotwritable', 'report_customsql', $path);
}
} else {
if (!is_writable($dir)) {
$errors['customdir'] = get_string('customdirmustexist', 'report_customsql', $dir);
}
}
}
}
}
return $errors;
}
}