This repository was archived by the owner on Dec 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathexternal.php
More file actions
executable file
·390 lines (317 loc) · 12.2 KB
/
external.php
File metadata and controls
executable file
·390 lines (317 loc) · 12.2 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?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/>.
/**
* Web services functions.
*
* These functions are used for ajax calls.
*
* @package block_filescan
* @copyright 2018 Swarthmore College ITS
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
global $CFG;
require_once($CFG->libdir . "/externallib.php");
class block_filescan_external extends external_api
{
/**
* Returns description of get_access_files parameters
*
* @return external_function_parameters
*/
public static function request_files_parameters()
{
return new external_function_parameters (
array(
'draw' => new external_value (PARAM_INT, 'Draw counter'),
'start' => new external_value (PARAM_INT, 'Paging first record indicator'),
'length' => new external_value (PARAM_INT, 'Number of records the table can display in the current draw.'),
'search' => new external_single_structure (array(
'value' => new external_value (PARAM_TEXT, 'Search string'),
'regex' => new external_value (PARAM_RAW, 'Is a regex search')
)),
'columns' => new external_multiple_structure(
new external_single_structure([
'data' => new external_value(PARAM_RAW, 'The column data.'),
'name' => new external_value(PARAM_RAW, 'The column name.'),
'searchable' => new external_value(PARAM_RAW, 'Is the column searchable'),
'orderable' => new external_value(PARAM_RAW, 'Is the column orderable'),
'search' => new external_single_structure([
'value' => new external_value(PARAM_RAW, 'Search value'),
'regex' => new external_value(PARAM_RAW, 'Is this a regex search')
])
])
),
'order' => new external_multiple_structure(
new external_single_structure([
'column' => new external_value(PARAM_RAW, 'Column to which ordering should be applied.'),
'dir' => new external_value(PARAM_RAW, 'Ordering direction for this column')
])
)
));
}
/**
* Makes the data tables request for data
*
* @param $draw
* @param $start
* @param $length
* @param $search
* @param $cols
* @param $order
*
* @return array
*/
public static function request_files($draw, $start, $length, $search, $cols, $order) {
global $DB;
// set up all the variables we will need to handle the request to the database
$config = include_once 'config/config.php';
$table = 'block_filescan_files';
$primaryKey = 'id';
$columns = array(
array('db' => 'id', 'dt' => 'id'),
array('db' => 'status', 'dt' => 'status'),
array('db' => 'hastext', 'dt' => 'hastext'),
array('db' => 'hastitle', 'dt' => 'hastitle'),
array('db' => 'hasoutline', 'dt' => 'hasoutline'),
array('db' => 'haslanguage', 'dt' => 'haslanguage'),
array('db' => 'timechecked', 'dt' => 'timechecked'),
array('db' => 'courseinfo', 'dt' => 'courseinfo')
);
// todo: validate parameters before sending the request
// Set up the parameters required for DataTables
// These are parameters given to us by DataTables
$request = array(
'draw' => $draw,
'start' => $start,
'length' => $length,
'search' => $search,
'columns' => $cols,
'order' => $order,
);
// Build the SQL query string from the request
$limit = self::limit( $request, $columns );
$order = self::order( $request, $columns );
$where = self::filter( $request, $columns );
$dataSQL = "SELECT `".implode("`, `", self::pluck($columns, 'db'))."` FROM {block_filescan_files}";
$search = null;
$resFilterLengthSQL = "SELECT COUNT({$primaryKey}) FROM {block_filescan_files}";
if ($request['search']['value'] || $request['search'][0]) {
$dataSQL .= " WHERE courseinfo LIKE :search";
$search = array('search' => "%" . $request['search']['value'] . "%");
$resFilterLengthSQL .= ' WHERE courseinfo LIKE :search';
}
$recordsFiltered = reset($DB->get_record_sql($resFilterLengthSQL, $search));
// Total data set length
$resTotalLengthSQL = "SELECT COUNT({$primaryKey}) FROM {block_filescan_files}";
$recordsTotal = reset($DB->get_record_sql($resTotalLengthSQL));
$dataSQL .= ' ' . $order . ' ' . $limit;
$warnings = [];
$data = $DB->get_records_sql($dataSQL, $search);
return array(
"draw" => isset ( $request['draw'] ) ? intval( $request['draw'] ) : 0,
"recordsTotal" => intval( $recordsTotal ),
"recordsFiltered" => intval( $recordsFiltered ),
"data" => $data,
"warnings" => $warnings
);
}
/**
* Create the data output array for the DataTables rows
*
* @param array $columns Column information array
* @param array $data Data from the SQL get
* @return array Formatted data in a row based format
*/
static function data_output ( $columns, $data )
{
$out = array();
for ( $i=0, $ien=count($data) ; $i<$ien ; $i++ ) {
$row = array();
for ( $j=0, $jen=count($columns) ; $j<$jen ; $j++ ) {
$column = $columns[$j];
// Is there a formatter?
if ( isset( $column['formatter'] ) ) {
$row[ $column['dt'] ] = $column['formatter']( $data[$i][ $column['db'] ], $data[$i] );
}
else {
$row[ $column['dt'] ] = $data[$i][ $columns[$j]['db'] ];
}
}
$out[] = $row;
}
return $out;
}
/**
* Paging
*
* Construct the LIMIT clause for server-side processing SQL query
*
* @param array $request Data sent to server by DataTables
* @param array $columns Column information array
* @return string SQL limit clause
*/
static function limit ( $request, $columns )
{
$limit = '';
if ( isset($request['start']) && $request['length'] != -1 ) {
$limit = "LIMIT ".intval($request['start']).", ".intval($request['length']);
}
return $limit;
}
/**
* Ordering
*
* Construct the ORDER BY clause for server-side processing SQL query
*
* @param array $request Data sent to server by DataTables
* @param array $columns Column information array
* @return string SQL order by clause
*/
static function order ( $request, $columns )
{
$order = '';
if ( isset($request['order']) && count($request['order']) ) {
$orderBy = array();
$dtColumns = self::pluck( $columns, 'dt' );
for ( $i=0, $ien=count($request['order']) ; $i<$ien ; $i++ ) {
// Convert the column index into the column data property
$columnIdx = intval($request['order'][$i]['column']);
$requestColumn = $request['columns'][$columnIdx];
$columnIdx = array_search( $requestColumn['data'], $dtColumns );
$column = $columns[ $columnIdx ];
if ( $requestColumn['orderable'] == 'true' ) {
$dir = $request['order'][$i]['dir'] === 'asc' ?
'ASC' :
'DESC';
$orderBy[] = '`'.$column['db'].'` '.$dir;
}
}
if ( count( $orderBy ) ) {
$order = 'ORDER BY '.implode(', ', $orderBy);
}
}
return $order;
}
/**
* Searching / Filtering
*
* Construct the WHERE clause for server-side processing SQL query.
*
* NOTE this does not match the built-in DataTables filtering which does it
* word by word on any field. It's possible to do here performance on large
* databases would be very poor
*
* @param array $request Data sent to server by DataTables
* @param array $columns Column information array
* @param array $bindings Array of values for PDO bindings, used in the
* sql_exec() function
* @return string SQL where clause
*/
static function filter ($request, $columns)
{
global $DB;
$globalSearch = array();
$columnSearch = array();
$dtColumns = self::pluck( $columns, 'dt' );
if ( isset($request['search']) && $request['search']['value'] != '' ) {
$str = $request['search']['value'];
for ( $i=0, $ien=count($request['columns']) ; $i<$ien ; $i++ ) {
$requestColumn = $request['columns'][$i];
$columnIdx = array_search( $requestColumn['data'], $dtColumns );
$column = $columns[ $columnIdx ];
if ( $requestColumn['searchable'] == 'true' ) {
//$binding = self::bind( $bindings, '%'.$str.'%', PDO::PARAM_STR );
$globalSearch[] = "`".$column['db']."` LIKE ".$str;
//$globalSearch[] = "`".$column['db']."` LIKE ". $str;
}
}
}
// Individual column filtering
if ( isset( $request['columns'] ) ) {
for ( $i=0, $ien=count($request['columns']) ; $i<$ien ; $i++ ) {
$requestColumn = $request['columns'][$i];
$columnIdx = array_search( $requestColumn['data'], $dtColumns );
$column = $columns[ $columnIdx ];
$str = $requestColumn['search']['value'];
if ( $requestColumn['searchable'] == 'true' && $str != '' ) {
//$binding = self::bind( $bindings, '%'.$str.'%', PDO::PARAM_STR );
$columnSearch[] = "`".$column['db']."` LIKE ".$str;
//$globalSearch[] = $column['db'] . " LIKE %\'".$str."\'";
}
}
}
// Combine the filters into a single string
$where = '';
if ( count( $globalSearch ) ) {
$where = '('.implode(' OR ', $globalSearch).')';
}
if ( count( $columnSearch ) ) {
$where = $where === '' ?
implode(' AND ', $columnSearch) :
$where .' AND '. implode(' AND ', $columnSearch);
}
if ( $where !== '' ) {
$where = 'WHERE '.$where;
}
return $where;
}
/**
* Pull a particular property from each assoc. array in a numeric array,
* returning and array of the property values from each item.
*
* @param array $a Array to get data from
* @param string $prop Property to read
* @return array Array of property values
*/
static function pluck ( $a, $prop )
{
$out = array();
for ( $i=0, $len=count($a) ; $i<$len ; $i++ ) {
$out[] = $a[$i][$prop];
}
return $out;
}
/**
* Describes the fields returned from get_access_files
* In this case, these are columns in the block_filescan_files table
*
* @return external_single_structure
*/
public static function request_files_returns()
{
return new external_single_structure(
array(
'draw' => new external_value(PARAM_INT, 'Draw counter'),
'recordsTotal' => new external_value(PARAM_INT, 'Total records'),
'recordsFiltered' => new external_value(PARAM_INT, 'Total records after filtering'),
'data' => new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'Unique identifier.'),
'status' => new external_value(PARAM_TEXT, 'The file scan status.'),
'hastext' => new external_value(PARAM_BOOL, 'Bool representing if the file has scannable text.'),
'hastitle' => new external_value(PARAM_BOOL, 'Bool representing if the file has a title.'),
'hasoutline' => new external_value(PARAM_BOOL, 'Bool representing if the file has an outline.'),
'haslanguage' => new external_value(PARAM_BOOL, 'Bool representing if the file has a langauge.'),
'timechecked' => new external_value(PARAM_RAW, 'Timestamp of the when the file was last scanned.'),
'courseinfo' => new external_value(PARAM_RAW, 'JSON representation of course info.')
), 'File')),
'warnings' => new external_warnings(),
)
);
}
}