-
Notifications
You must be signed in to change notification settings - Fork 0
/
locallib.php
1362 lines (1230 loc) · 42.9 KB
/
locallib.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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?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/>.
/**
* Library code for the report datawarehouse.
*
* @package report_datawarehouse
* @copyright 2023 Luca Bösch <[email protected]>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/validateurlsyntax.php');
/**
* The marker for exceeded row limit.
*/
define('REPORT_DATAWAREHOUSE_LIMIT_EXCEEDED_MARKER', '-- ROW LIMIT EXCEEDED --');
/**
* Execute a query
*
* @param string $sql the sql
* @param array|null $params the params
* @param int|null $limitnum the result limit
* @return moodle_recordset
* @throws dml_exception
*/
function report_datawarehouse_execute_query($sql, $params = null, $limitnum = null) {
global $CFG, $DB;
if ($limitnum === null) {
$limitnum = 0;
}
$sql = preg_replace('/\bprefix_(?=\w+)/i', $CFG->prefix, $sql);
if (isset($params)) {
foreach ($params as $name => $value) {
if (((string) (int) $value) === ((string) $value)) {
$params[$name] = (int) $value;
}
}
} else {
$params = [];
}
// Note: throws Exception if there is an error.
return $DB->get_recordset_sql($sql, $params, 0, $limitnum);
}
/**
* A function to substitute the time and user tokens.
*
* @param \stdClass $query A query object
* @param int $cmid The course module
* @param int $courseid The course
* @param string $filename The file name
* @return array|string|string[]
*/
function report_datawarehouse_prepare_sql($query, int $cmid, int $courseid, $filename = '') {
global $USER;
$sql = $query->querysql;
if ($filename != '' && str_starts_with($sql, 'SELECT')) {
// Write the filename in a first field 'loadid'.
$sql = preg_replace('/SELECT/', 'SELECT \'' . $filename . '\' AS "loadid", ', $sql, 1);
}
$sql = report_datawarehouse_substitute_user_token($sql, $USER->id);
$sql = report_datawarehouse_substitute_course_module_id($sql, $cmid);
$sql = report_datawarehouse_substitute_course_id($sql, $courseid);
return $sql;
}
/**
* Extract all the placeholder names from the SQL.
*
* @param string $sql The sql.
* @return array placeholder names including the leading colon.
*/
function report_datawarehouse_get_query_placeholders($sql) {
preg_match_all('/(?<!:):[a-z][a-z0-9_]*/', $sql, $matches);
return $matches[0];
}
/**
* Extract all the placeholder names from the SQL, and work out the corresponding form field names.
*
* @param string $querysql The sql.
* @return string[] placeholder name => form field name.
*/
function report_datawarehouse_get_query_placeholders_and_field_names(string $querysql): array {
$queryparams = [];
foreach (report_datawarehouse_get_query_placeholders($querysql) as $queryparam) {
$queryparams[substr($queryparam, 1)] = 'queryparam' . substr($queryparam, 1);
}
return $queryparams;
}
/**
* Return the type of form field to use for a placeholder, based on its name.
*
* @param string $name the placeholder name.
* @return string a formslib element type, for example 'text' or 'date_time_selector'.
*/
function report_datawarehouse_get_element_type($name) {
$regex = '/^date|date$/';
if (preg_match($regex, $name)) {
return 'date_time_selector';
}
return 'text';
}
/**
* Execute a run
*
* @param int $runid The run id
* @throws dml_exception
*/
function report_datawarehouse_execute_run($runid) {
global $DB;
$timenow = time();
$run = $DB->get_record('report_datawarehouse_runs', ['id' => $runid]);
report_datawarehouse_generate_csv($run->queryid, $run->backendid, $timenow, $run->cmid, $run->courseid);
report_datawarehouse_note_lastrun($runid, $timenow);
}
/**
* Generate a CSV
*
* @param int $queryid A query id
* @param int $backendid A backend
* @param int $timenow A timestamp
* @param int $cmid The course module id
* @param int $courseid The course id
* @return mixed|null A timestamp
* @throws coding_exception
* @throws dml_exception
* @throws file_exception
* @throws stored_file_creation_exception
*/
function report_datawarehouse_generate_csv(int $queryid, int $backendid, int $timenow, int $cmid, int $courseid) {
global $DB;
$starttime = microtime(true);
$itemid = get_file_itemid() + 1;
$csvfilenames = [];
$csvtimestamp = null;
$count = 0;
$query = $DB->get_record('report_datawarehouse_queries', ['id' => $queryid]);
$filename = report_datawarehouse_get_filename($cmid, $query, $itemid);
$sql = report_datawarehouse_prepare_sql($query, $cmid, $courseid, $filename);
$rs = report_datawarehouse_execute_query($sql);
foreach ($rs as $row) {
if (!$csvtimestamp) {
list($csvfilename, $tempfolder, $csvtimestamp) = report_datawarehouse_csv_filename($filename, $timenow);
$csvfilenames[] = $csvfilename;
if (!file_exists($csvfilename)) {
$handle = fopen($csvfilename, 'w');
report_datawarehouse_start_csv($handle, $row, $query);
} else {
$handle = fopen($csvfilename, 'a');
}
}
$data = get_object_vars($row);
foreach ($data as $name => $value) {
if (report_datawarehouse_get_element_type($name) == 'date_time_selector' &&
report_datawarehouse_is_integer($value) && $value > 0) {
$data[$name] = userdate($value, '%F %T');
}
}
report_datawarehouse_write_csv_row($handle, $data);
$count += 1;
}
$rs->close();
if (!empty($handle)) {
if (isset($querylimit)) {
if ($count > $querylimit) {
report_datawarehouse_write_csv_row($handle, [REPORT_DATAWAREHOUSE_LIMIT_EXCEEDED_MARKER]);
}
}
fclose($handle);
}
// Now copy the file over to the 'real' files in moodledata.
$fs = get_file_storage();
if (!isset($tempfolder)) {
$tempfolder = make_temp_directory('report_datawarehouse');
}
$filerecord = [
'component' => 'report_datawarehouse',
'contextid' => \context_system::instance()->id,
'filearea' => 'data',
'itemid' => $itemid,
'filepath' => '/',
'filename' => $filename, ];
$fs->create_file_from_pathname($filerecord, $tempfolder . '/' . $filename);
if ($backend->username == '' && $backend->password == '' ) {
// PUT to a Pre-Authenticated Requests enabled Oracle Object Storage Bucket.
$url = $DB->get_field('report_datawarehouse_bkends', 'url', ['id' => $backendid]);
// Initiate cURL object.
$curl = curl_init();
// Set your URL.
curl_setopt($curl, CURLOPT_URL, $url . $filename);
// Indicate, that you plan to upload a file.
curl_setopt($curl, CURLOPT_UPLOAD, true);
// Indicate your protocol.
curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
// Set flags for transfer.
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
// Disable header (optional).
curl_setopt($curl, CURLOPT_HEADER, false);
// Set HTTP method to PUT.
curl_setopt($curl, CURLOPT_PUT, 1);
// Indicate the file you want to upload.
curl_setopt($curl, CURLOPT_INFILE, fopen($tempfolder . '/' . $filename, 'rb'));
// Indicate the size of the file (it does not look like this is mandatory, though).
curl_setopt($curl, CURLOPT_INFILESIZE, filesize($tempfolder . '/' . $filename));
// Only use below option on TEST environment if you have a self-signed certificate!!! On production this can cause security
// issues.
// curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
// Execute.
curl_exec($curl);
} else {
// REST to a REST enabled table in an Oracle Autonomous Data Warehouse.
$url = $DB->get_field('report_datawarehouse_bkends', 'url', ['id' => $backend->id]);
// Initiate cURL object with URL.
$ch = curl_init($url);
// Setup request to send json via POST.
$fp = fopen($tempfolder . '/' . $filename, 'r');
$headers = fgetcsv($fp); // Get column headers.
$data = [];
while (($row = fgetcsv($fp)) !== false) {
$data[] = array_combine($headers, $row);
}
fclose($fp);
curl_setopt( $ch, CURLOPT_POSTFIELDS, trim(json_encode($data), '[]'));
curl_setopt( $ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']);
// Return response instead of printing.
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
// Send request.
$result = curl_exec($ch);
curl_close($ch);
// Print response.
echo "<pre>$result</pre>";
}
return $csvtimestamp;
}
/**
* Return whether a value is an integer or looks like an integer.
*
* @param mixed $value some value
* @return bool whether $value is an integer, or a string that looks like an integer.
*/
function report_datawarehouse_is_integer($value) {
return (string) (int) $value === (string) $value;
}
/**
* Return a CSV filename
*
* @param string $filename The file name
* @param int $timenow A timestamp
* @return array
*/
function report_datawarehouse_csv_filename($filename, $timenow) {
return report_datawarehouse_temp_csv_name($filename, $timenow);
}
/**
* Return a temporary CSV filename
*
* @param string $filename The file name
* @param int $timestamp A timestamp
* @return array Some result
*/
function report_datawarehouse_temp_csv_name($filename, $timestamp) {
// Prepare temp area.
$tempfolder = make_temp_directory('report_datawarehouse');
$tempfile = $tempfolder . '/' . $filename;
return [$tempfile, $tempfolder, $timestamp];
}
/**
* Get the csv location and the time start of a report
*
* @param int $reportid The reportid.
* @param int $timestart a timestamp.
* @return array
*/
function report_datawarehouse_scheduled_csv_name($reportid, $timestart) {
global $CFG;
$path = 'admin_report_datawarehouse/'.$reportid;
make_upload_directory($path);
return [$CFG->dataroot.'/'.$path.'/'.date('%Y%m%d-%H%M%S', $timestart).'.csv',
$timestart, ];
}
/**
* Get the accumulate.csv file name and 0 of a report
*
* @param int $reportid The reportid.
* @return array
*/
function report_datawarehouse_accumulating_csv_name($reportid) {
global $CFG;
$path = 'admin_report_datawarehouse/'.$reportid;
make_upload_directory($path);
return [$CFG->dataroot.'/'.$path.'/accumulate.csv', 0];
}
/**
* Get the times the reports were archived.
*
* @param object $report report settings from the database.
* @return array
*/
function report_datawarehouse_get_archive_times($report) {
global $CFG;
if ($report->runable == 'manual' || $report->singlerow) {
return [];
}
$files = glob($CFG->dataroot.'/admin_report_datawarehouse/'.$report->id.'/*.csv');
$archivetimes = [];
foreach ($files as $file) {
if (preg_match('|/(\d\d\d\d)(\d\d)(\d\d)-(\d\d)(\d\d)(\d\d)\.csv$|', $file, $matches)) {
$archivetimes[] = mktime($matches[4], $matches[5], $matches[6], $matches[2],
$matches[3], $matches[1]);
}
}
rsort($archivetimes);
return $archivetimes;
}
/**
* Substitute time tokens
*
* @param string $sql the sql
* @param int $start a timestamp.
* @param int $end a timestamp.
* @return array|string|string[]
*/
function report_datawarehouse_substitute_time_tokens($sql, $start, $end) {
return str_replace(['%%STARTTIME%%', '%%ENDTIME%%'], [$start, $end], $sql);
}
/**
* Substitute user tokens
*
* @param string $sql the sql
* @param int $userid a user id
* @return array|string|string[]
*/
function report_datawarehouse_substitute_user_token($sql, $userid) {
return str_replace('%%USERID%%', $userid, $sql);
}
/**
* Substitute course ids in a SQL string.
*
* @param string $sql The SQL query.
* @param int $courseid A user id
* @return array|string|string[] Some result
*/
function report_datawarehouse_substitute_course_id($sql, $courseid) {
return str_replace('%%COURSEID%%', $courseid, $sql);
}
/**
* Substitute course module ids in a SQL string.
*
* @param string $sql The SQL query.
* @param int $cmid A course module id
* @return array|string|string[] Some result
*/
function report_datawarehouse_substitute_course_module_id($sql, $cmid) {
return str_replace('%%CMID%%', $cmid, $sql);
}
/**
* Create url to $relativeurl.
*
* @param string $relativeurl Relative url.
* @param array $params Parameter for url.
* @return moodle_url the relative url.
*/
function report_datawarehouse_url($relativeurl, $params = []) {
return new moodle_url('/report/datawarehouse/' . $relativeurl, $params);
}
/**
* Create the download url for the report.
*
* @param int $reportid The reportid.
* @param array $params Parameters for the url.
*
* @return moodle_url The download url.
*/
function report_datawarehouse_downloadurl($reportid, $params = []) {
$downloadurl = moodle_url::make_pluginfile_url(
context_system::instance()->id,
'report_datawarehouse',
'download',
$reportid,
null,
null
);
// Add the params to the url.
// Used to pass values for the arbitrary number of params in the sql report.
$downloadurl->params($params);
return $downloadurl;
}
/**
* Get the capability options.
*
* @return array
* @throws coding_exception
*/
function report_datawarehouse_capability_options() {
return [
'report/datawarehouse:view' => get_string('anyonewhocanveiwthisreport', 'report_datawarehouse'),
'moodle/site:viewreports' => get_string('userswhocanviewsitereports', 'report_datawarehouse'),
'moodle/site:config' => get_string('userswhocanconfig', 'report_datawarehouse'),
];
}
/**
* Get the available type of reports given a super type.
*
* @param string|null $type the type of report
* @return array
* @throws coding_exception
*/
function report_datawarehouse_runable_options($type = null) {
if ($type === 'manual') {
return ['manual' => get_string('manual', 'report_datawarehouse')];
}
return ['manual' => get_string('manual', 'report_datawarehouse'),
'daily' => get_string('automaticallydaily', 'report_datawarehouse'),
'weekly' => get_string('automaticallyweekly', 'report_datawarehouse'),
'monthly' => get_string('automaticallymonthly', 'report_datawarehouse'),
];
}
/**
* Get the options for the time for daily digests.
*
* @return array
*/
function report_datawarehouse_daily_at_options() {
$time = [];
for ($h = 0; $h < 24; $h++) {
$hour = ($h < 10) ? "0$h" : $h;
$time[$h] = "$hour:00";
}
return $time;
}
/**
* Get the email options.
*
* @return array
* @throws coding_exception
*/
function report_datawarehouse_email_options() {
return ['emailnumberofrows' => get_string('emailnumberofrows', 'report_datawarehouse'),
'emailresults' => get_string('emailresults', 'report_datawarehouse'),
];
}
/**
* Get the bad words list.
*
* @return string[]
*/
function report_datawarehouse_bad_words_list() {
return ['ALTER', 'CREATE', 'DELETE', 'DROP', 'GRANT', 'INSERT', 'INTO',
'TRUNCATE', 'UPDATE', ];
}
/**
* Check if a string contains bad words
*
* @param string $string the string to check
* @return false|int
*/
function report_datawarehouse_contains_bad_word($string) {
return preg_match('/\b('.implode('|', report_datawarehouse_bad_words_list()).')\b/i', $string);
}
/**
* Delete a log
*
* @param int $id The id
* @throws dml_exception
*/
function report_datawarehouse_log_delete($id) {
$event = \report_datawarehouse\event\query_deleted::create(
['objectid' => $id, 'context' => context_system::instance()]);
$event->trigger();
}
/**
* Edit a log
*
* @param int $id The id
* @throws dml_exception
*/
function report_datawarehouse_log_edit($id) {
$event = \report_datawarehouse\event\query_edited::create(
['objectid' => $id, 'context' => context_system::instance()]);
$event->trigger();
}
/**
* View a log
*
* @param int $id The id
* @throws dml_exception
*/
function report_datawarehouse_log_view($id) {
$event = \report_datawarehouse\event\query_viewed::create(
['objectid' => $id, 'context' => context_system::instance()]);
$event->trigger();
}
/**
* Returns all reports for a given type sorted by report 'displayname'.
*
* @param int $categoryid The category id
* @param string $type the type of report (manual, daily, weekly or monthly)
* @return stdClass[] relevant rows from report_datawarehouse_queries.
*/
function report_datawarehouse_get_reports_for($categoryid, $type) {
global $DB;
$records = $DB->get_records('report_datawarehouse_queries',
['runable' => $type, 'categoryid' => $categoryid]);
return report_datawarehouse_sort_reports_by_displayname($records);
}
/**
* Display a list of reports of one type in one query_category.
*
* @param object $reports the result of DB query
* @param string $type the type of report (manual, daily, weekly or monthly)
*/
function report_datawarehouse_print_reports_for($reports, $type) {
global $OUTPUT;
if (empty($reports)) {
return;
}
if (!empty($type)) {
$help = html_writer::tag('span', $OUTPUT->help_icon($type . 'header', 'report_datawarehouse'));
echo $OUTPUT->heading(get_string($type . 'header', 'report_datawarehouse') . $help, 3);
}
$context = context_system::instance();
$canedit = has_capability('report/datawarehouse:definequeries', $context);
$capabilities = report_datawarehouse_capability_options();
foreach ($reports as $report) {
if (!empty($report->capability) && !has_capability($report->capability, $context)) {
continue;
}
echo html_writer::start_tag('p');
echo html_writer::tag('a', format_string($report->displayname),
['href' => report_datawarehouse_url('view.php?id='.$report->id)]).
' '.report_datawarehouse_time_note($report, 'span');
if ($canedit) {
$imgedit = $OUTPUT->pix_icon('t/edit', get_string('edit'));
$imgdelete = $OUTPUT->pix_icon('t/delete', get_string('delete'));
echo ' '.html_writer::tag('span', get_string('availableto', 'report_datawarehouse',
$capabilities[$report->capability]),
['class' => 'admin_note']).' '.
html_writer::tag('a', $imgedit,
['title' => get_string('editreportx', 'report_datawarehouse', format_string($report->displayname)),
'href' => report_datawarehouse_url('edit.php?id='.$report->id), ]) . ' ' .
html_writer::tag('a', $imgdelete,
['title' => get_string('deletereportx', 'report_datawarehouse',
format_string($report->displayname)),
'href' => report_datawarehouse_url('delete.php?id='.$report->id), ]);
}
echo html_writer::end_tag('p');
echo "\n";
}
}
/**
* Get the list of actual column headers from the list of raw column names.
*
* This matches up the 'Column name' and 'Column name link url' columns.
*
* @param string[] $row the row of raw column headers from the CSV file.
* @return array with two elements: the column headers to use in the table, and the columns that are links.
*/
function report_datawarehouse_get_table_headers($row) {
$colnames = array_combine($row, $row);
$linkcolumns = [];
$colheaders = [];
foreach ($row as $key => $colname) {
if (substr($colname, -9) === ' link url' && isset($colnames[substr($colname, 0, -9)])) {
// This is a link_url column for another column. Skip.
$linkcolumns[$key] = -1;
} else if (isset($colnames[$colname . ' link url'])) {
$colheaders[] = $colname;
$linkcolumns[$key] = array_search($colname . ' link url', $row);
} else {
$colheaders[] = $colname;
}
}
return [$colheaders, $linkcolumns];
}
/**
* Prepare the values in a data row for display.
*
* This deals with $linkcolumns as detected above and other values that looks like links.
* Auto-formatting dates is handled when the CSV is generated.
*
* @param string[] $row the row of raw data.
* @param int[] $linkcolumns
* @return string[] cell contents for output.
*/
function report_datawarehouse_display_row($row, $linkcolumns) {
$rowdata = [];
foreach ($row as $key => $value) {
if (isset($linkcolumns[$key]) && $linkcolumns[$key] === -1) {
// This row is the link url for another row.
continue;
} else if (isset($linkcolumns[$key])) {
// Column with link url coming from another column.
if (validateUrlSyntax($row[$linkcolumns[$key]], 's+H?S?F?E?u-P-a?I?p?f?q?r?')) {
$rowdata[] = '<a href="' . s($row[$linkcolumns[$key]]) . '">' . s($value) . '</a>';
} else {
$rowdata[] = s($value);
}
} else if (validateUrlSyntax($value, 's+H?S?F?E?u-P-a?I?p?f?q?r?')) {
// Column where the value just looks like a link.
$rowdata[] = '<a href="' . s($value) . '">' . s($value) . '</a>';
} else {
$rowdata[] = s($value);
}
}
return $rowdata;
}
/**
* Get the time tag
*
* @param object $report report settings from the database.
* @param string $tag a tag
* @return string
* @throws coding_exception
*/
function report_datawarehouse_time_note($report, $tag) {
if ($report->lastrun) {
$a = new stdClass;
$a->lastrun = userdate($report->lastrun);
$a->lastexecutiontime = $report->lastexecutiontime / 1000;
$note = get_string('lastexecuted', 'report_datawarehouse', $a);
} else {
$note = get_string('notrunyet', 'report_datawarehouse');
}
return html_writer::tag($tag, $note, ['class' => 'admin_note']);
}
/**
* Prettify columns name
*
* @param string $row a row
* @param string $querysql the query sql
* @return array
*/
function report_datawarehouse_prettify_column_names($row, $querysql) {
$colnames = [];
foreach (get_object_vars($row) as $colname => $ignored) {
// Databases tend to return the columns lower-cased.
// Try to get the original case from the query.
if (preg_match('~SELECT.*?\s(' . preg_quote($colname, '~') . ')\b~is',
$querysql, $matches)) {
$colname = $matches[1];
}
// Change underscores to spaces.
$colnames[] = str_replace('_', ' ', $colname);
}
return $colnames;
}
/**
* Writes a CSV row and replaces placeholders.
* @param resource $handle the file pointer
* @param array $data a data row
*/
function report_datawarehouse_write_csv_row($handle, $data) {
global $CFG;
$escapeddata = [];
foreach ($data as $value) {
if (!isset($value)) {
$value = '';
}
$value = str_replace('%%WWWROOT%%', $CFG->wwwroot, $value);
$value = str_replace('%%Q%%', '?', $value);
$value = str_replace('%%C%%', ':', $value);
$value = str_replace('%%S%%', ';', $value);
$escapeddata[] = '"'.str_replace('"', '""', $value).'"';
}
fwrite($handle, implode(',', $escapeddata)."\r\n");
}
/**
* Read the next row of data from a CSV file.
*
* Wrapper around fgetcsv to eliminate the non-standard escaping behaviour.
*
* @param resource $handle pointer to the file to read.
* @return array|false|null next row of data (as for fgetcsv).
*/
function report_datawarehouse_read_csv_row($handle) {
static $disablestupidphpescaping = null;
if ($disablestupidphpescaping === null) {
// One-time init, can be removed once we only need to support PHP 7.4+.
$disablestupidphpescaping = '';
if (!check_php_version('7.4')) {
// This argument of fgetcsv cannot be unset in PHP < 7.4, so substitute a character which is unlikely to ever appear.
$disablestupidphpescaping = "\v";
}
}
return fgetcsv($handle, 0, ',', '"', $disablestupidphpescaping);
}
/**
* Start CSV output
*
* @param resource $handle the file pointer
* @param string $firstrow the first row
* @param object $report report settings from the database.
* @throws coding_exception
*/
function report_datawarehouse_start_csv($handle, $firstrow, $report) {
$colnames = report_datawarehouse_prettify_column_names($firstrow, $report->querysql);
if ($report->singlerow) {
array_unshift($colnames, get_string('queryrundate', 'report_datawarehouse'));
}
report_datawarehouse_write_csv_row($handle, $colnames);
}
/**
* Get the daily start time.
*
* @param int $timenow a timestamp.
* @param int $at an hour, 0 to 23.
* @return array with two elements: the timestamp for hour $at today (where today
* is defined by $timenow) and the timestamp for hour $at yesterday.
*/
function report_datawarehouse_get_daily_time_starts($timenow, $at) {
$hours = $at;
$minutes = 0;
$dateparts = getdate($timenow);
return [
mktime((int)$hours, (int)$minutes, 0,
$dateparts['mon'], $dateparts['mday'], $dateparts['year']),
mktime((int)$hours, (int)$minutes, 0,
$dateparts['mon'], $dateparts['mday'] - 1, $dateparts['year']),
];
}
/**
* Get the start of the week.
*
* @param int $timenow a timestamp.
* @return array
* @throws coding_exception
* @throws dml_exception
*/
function report_datawarehouse_get_week_starts($timenow) {
$dateparts = getdate($timenow);
// Get configured start of week value. If -1 then use the value from the site calendar.
$startofweek = get_config('report_datawarehouse', 'startwday');
if ($startofweek == -1) {
$startofweek = \core_calendar\type_factory::get_calendar_instance()->get_starting_weekday();
}
$daysafterweekstart = ($dateparts['wday'] - $startofweek + 7) % 7;
return [
mktime(0, 0, 0, $dateparts['mon'], $dateparts['mday'] - $daysafterweekstart,
$dateparts['year']),
mktime(0, 0, 0, $dateparts['mon'], $dateparts['mday'] - $daysafterweekstart - 7,
$dateparts['year']),
];
}
/**
* Get the start of the month.
*
* @param int $timenow a timestamp.
* @return array
*/
function report_datawarehouse_get_month_starts($timenow) {
$dateparts = getdate($timenow);
return [
mktime(0, 0, 0, $dateparts['mon'], 1, $dateparts['year']),
mktime(0, 0, 0, $dateparts['mon'] - 1, 1, $dateparts['year']),
];
}
/**
* Generates and returns the data warehouse report query result file name.
*
* @param int $cmid the course module id for this activity.
* @param \stdClass $query A query object
* @param string $itemid The item id
* @throws coding_exception
*/
function report_datawarehouse_get_filename(int $cmid, stdClass $query, string $itemid): string {
global $USER;
$timezone = \core_date::get_user_timezone_object();
$timestamp = time();
$calendartype = \core_calendar\type_factory::get_calendar_instance();
$timestamparray = $calendartype->timestamp_to_date_array($timestamp, $timezone);
$timestamptext = $timestamparray['year'] . "-" .
sprintf("%02d", $timestamparray['mon']) . "-" .
sprintf("%02d", $timestamparray['mday']) . "-" .
sprintf("%02d", $timestamparray['hours']) . "-" .
sprintf("%02d", $timestamparray['minutes']) . "-" .
sprintf("%02d", $timestamparray['seconds']);
$queryname = $query->name;
return $USER->id . '-' . $itemid . '-' . $cmid . '-' . str_replace(' ', '_', $queryname) . '-' . $timestamp . '-' .
$timestamptext . '.csv';
}
/**
* Get the queries ids and names.
*
* @return array
*/
function report_datawarehouse_get_queries() {
global $DB;
$queries = $DB->get_records_select('report_datawarehouse_queries', "enabled = ?", [1], 'sortorder');
$activequeries = [];
foreach ($queries as $id => $b) {
$activequeries[] = $b;
}
return $activequeries;
}
/**
* Get the query name by id.
*
* @param int $queryid a query id.
* @return bool
*/
function report_datawarehouse_get_query_used_by_id($queryid) {
global $DB;
$queryinuse = $DB->get_field('report_datawarehouse_runs', 'COUNT(id)', ['queryid' => $queryid]);
if ($queryinuse > 0) {
return true;
} else {
return false;
}
}
/**
* Get the query name by id.
*
* @param int $queryid a query id.
* @return array
*/
function report_datawarehouse_get_query_name_by_id($queryid) {
global $DB;
$queryname = $DB->get_field("report_datawarehouse_queries", "name", ["id" => $queryid]);
return format_string($queryname);
}
/**
* Get the backend ids and names.
*
* @return array
*/
function report_datawarehouse_get_backends() {
global $DB;
$backends = $DB->get_records_select('report_datawarehouse_bkends', "enabled = ?", [1], 'sortorder');
$activebackends = [];
foreach ($backends as $id => $b) {
$activebackends[] = $b;
}
return $activebackends;
}
/**
* Get the backend name by id.
*
* @param int $backendid a backend id.
* @return array
*/
function report_datawarehouse_get_backend_name_by_id($backendid) {
global $DB;
$backendname = $DB->get_field("report_datawarehouse_bkends", "name", ["id" => $backendid]);
return format_string($backendname);
}
/**
* Get the backend usage by id.
*
* @param int $backendid a backend id.
* @return bool
*/
function report_datawarehouse_get_backend_used_by_id($backendid) {
global $DB;
$queryinuse = $DB->get_field('report_datawarehouse_runs', 'COUNT(id)', ['backendid' => $backendid]);
if ($queryinuse > 0) {
return true;
} else {
return false;
}
}
/**
* Get the start times of the reports.
*
* @param object $report report settings from the database.
* @param int $timenow a timestamp.
* @return array
* @throws coding_exception
* @throws dml_exception
*/
function report_datawarehouse_get_starts($report, $timenow) {
switch ($report->runable) {
case 'daily':
return report_datawarehouse_get_daily_time_starts($timenow, $report->at);
case 'weekly':
return report_datawarehouse_get_week_starts($timenow);
case 'monthly':
return report_datawarehouse_get_month_starts($timenow);
default:
throw new Exception('unexpected $report->runable.');
}
}
/**
* Delete old temp files
*
* @param int $upto a time stamp
* @return int|void
*/
function report_datawarehouse_delete_old_temp_files($upto) {
global $CFG;
$count = 0;
$comparison = date('%Y%m%d-%H%M%S', $upto).'csv';
$files = glob($CFG->dataroot.'/admin_report_datawarehouse/temp/*/*.csv');
if (empty($files)) {