-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathphp_report_base.php
1194 lines (1024 loc) · 43.7 KB
/
php_report_base.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
/**
* ELIS(TM): Enterprise Learning Intelligence Suite
* Copyright (C) 2008-2012 Remote Learner.net Inc http://www.remote-learner.net
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package elis
* @subpackage curriculummanagement
* @author Remote-Learner.net Inc
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2008-2012 Remote Learner.net Inc http://www.remote-learner.net
*
*/
require_once($CFG->dirroot . '/blocks/php_report/lib/filtering.php');
/**
* Base class for the various report types
*/
abstract class php_report {
//defined export formats
static $EXPORT_FORMAT_HTML = 'html';
static $EXPORT_FORMAT_PDF = 'pdf';
static $EXPORT_FORMAT_CSV = 'csv';
static $EXPORT_FORMAT_EXCEL = 'excel';
static $EXPORT_FORMAT_LABEL = 'label';
// progress bar indicator for pdf format
static $EXPORT_FORMAT_PDF_BAR = 'BAR|';
// report categories
const CATEGORY_ADMIN = 'admin_reports';
const CATEGORY_CURRICULUM = 'curriculum_reports';
const CATEGORY_COURSE = 'course_reports';
const CATEGORY_CLASS = 'class_reports';
const CATEGORY_CLUSTER = 'cluster_reports';
const CATEGORY_PARTICIPATION = 'participation_reports';
const CATEGORY_USER = 'user_reports';
const CATEGORY_OUTCOMES = 'outcomes_reports';
const CATEGORY_NODISPLAY = null;
const EXECUTION_MODE_INTERACTIVE = 0;
const EXECUTION_MODE_SCHEDULED = 1;
//identifier for the current report
var $id;
//id of the Moodle user we are considering the report to be run by
var $userid;
//contenxt in which the report is being run
var $execution_mode;
//capability that should be checked for permissions, based on execution mode
var $access_capability;
protected $gas_gauge_page = 0;
/**
* PHP report constructor
*
* @param string $id Unique identifier for this report instance
* @param int|NULL $userid Id of the Moodle user who this report is being
* for
* @param int $execution_mode The mode in which this report is being executed
*/
function php_report($id, $userid = NULL, $execution_mode = php_report::EXECUTION_MODE_INTERACTIVE) {
global $USER;
$this->id = $id;
if ($userid === NULL) {
//default to the current user
$this->userid = $USER->id;
} else {
//manually specified
$this->userid = $userid;
}
$this->execution_mode = $execution_mode;
//calcuate the capability needed to access this report based on the execution mode
$this->access_capability = $this->get_access_capability();
}
/**
* Calculates the display name for this report from the appropriate language string
*
* @return string The report's displayname
*/
function get_display_name() {
$classname = get_class($this);
$folder_name = 'rlreport_' . substr($classname, 0, strlen($classname) - strlen('_report'));
//use the displayname string from the specific report instance
return get_string('displayname', $folder_name);
}
/**
* Gets the report category.
*
* @return string The report's category (should be one of the CATEGORY_*
* constants defined above).
*/
function get_category() {
return self::CATEGORY_ADMIN;
}
/**
* Determines whether the current user can view this report
*
* @return boolean True if permitted, otherwise false
*/
function can_view_report() {
//no default restrictions, implement restrictions in report instance class
return true;
}
/**
* Generates the HTML code for the header entries, based on the report's definition
* of header entries as defined by "get_header_entries"
*
* @return string The appropriate HTML code
*/
function print_header_entries() {
$result = '';
if ($entries = $this->get_header_entries(php_report::$EXPORT_FORMAT_HTML)) {
$result .= html_writer::start_tag('div', array('class' => 'php_report_header_entries'));
foreach ($entries as $value) {
$label_class = "php_report_header_{$value->css_identifier} php_report_header_label";
$value_class = "php_report_header_{$value->css_identifier} php_report_header_value";
$result .= html_writer::tag('div', $value->label, array('class' => $label_class));
$result .= html_writer::tag('div', $value->value, array('class' => $value_class));
}
$result .= html_writer::end_tag('div');
}
return $result;
}
/**
* This method provides a way for reports to define entries to show at
* top as summary data (default to none, but allow implementations to override)
*
* @param $export_format The desired export format for the headers
* @return array A mapping of display names to values
*/
function get_header_entries($export_format) {
return array();
}
/**
* Determines whether an SQL query has an active WHERE clause
* at the outermost level of nesting
*
* @param string $sql The SQL query in question
*
* @return boolean True if it query has a WHERE clause, otherwise false
*/
static function sql_has_where_clause($sql) {
$position = array();
$lowercase_sql = strtolower($sql);
//calculate position of key symbols
$symbols = array('(',
')',
'where');
foreach ($symbols as $symbol) {
$start_pos = 0;
while (($pos = strpos($lowercase_sql, $symbol, $start_pos)) !== FALSE) {
$position[$pos] = $symbol;
$start_pos = $pos + 1;
}
}
//sort based on position
ksort($position);
//maintain bracket balance
$balance = 0;
foreach ($position as $token) {
switch ($token) {
case '(':
$balance++;
break;
case ')':
$balance--;
break;
case 'where':
if ($balance == 0) {
//WHERE found at outermost level
return true;
}
break;
default:
break;
}
}
//never found an appropriate WHERE token
return false;
}
/**
* Specifies available report filters
* (empty by default but can be implemented by child class)
*
* @param boolean $init_data If true, signal the report to load the
* actual content of the filter objects
*
* @return array The list of available filters
*/
function get_filters($init_data = true) {
//no filters by default
return array();
}
/**
* Specifies whether this report definition has one or more
* filters defined for it
*
* @return boolean true if one more more filters are defined, or false if none are
*/
function has_filters() {
//default to true, since almost all reports will have some filtering options
return true;
}
/**
* Sets up the secondary filterings based on the report definitions
*
* @param string $url The URL used to dynamically reload this report
* @param string $id This report's unique identifier
* @param string $report_shortname The shortname of the configured report
*
* @return array Mapping of pre-set filtering keys to filtering objects
*/
function get_secondary_filterings($url, $id, $report_shortname) {
return array();
}
/**
* Initialize the filter object
*
* @param boolean $init_data If true, signal the report to load the
* actual content of the filter objects
*
*/
function init_filter($id, $init_data = true) {
global $CFG;
if (!isset($this->filter)) {
$filters = $this->get_filters($init_data);
$dynamic_report_filter_url = $CFG->wwwroot .'/blocks/php_report/dynamicreport.php?id='. $id;
$this->filter = new php_report_default_capable_filtering($filters, $dynamic_report_filter_url, null, $id, $this->get_report_shortname());
}
}
/**
* Specifies the particular export formats that are supported by this report
*
* @return string array List of applicable export identifiers that correspond to
* possiblities as specified by get_allowable_export_formats
*/
function get_export_formats() {
//default to no export (override in report class)
return array();
}
/**
* Specifies the list of identifiers representing the list of possible export formats
* (based on the functionality of the download method in the specific report type, currently
* only implemented in table reports)
*
* @return string array List of allowable formats
*/
static function get_allowable_export_formats() {
//currently, only PDF and CSV export formats are fully implemented
return array(php_report::$EXPORT_FORMAT_PDF,
php_report::$EXPORT_FORMAT_CSV);
}
/**
* Specifies whether the current report is available
* (a.k.a. any other components it requires are installed)
*
* @return boolean True if the report is available, otherwise false
*/
function is_available() {
//by default, make the report available,
//override in child class if necessary to restrict
return true;
}
/**
* Require any code that this report needs
* (only called after is_available returns true)
*/
function require_dependencies() {
//nothing needed by default - this is a report-specific hook
}
/**
* Returns the current filters parameter values for a specified filter name
* TODO: remove this once nobody is using it anymore
*
* @param string $filter_name The filter name
* @return array List of filter parameter values for given filter name, false if no results
*/
function get_active_filter_values($filter_name) {
global $SESSION;
mtrace('<br><font color=red>NOTICE: $this->get_active_filter_values() is now deprecated; use php_report_filtering_get_active_filter_values($this->get_report_shortname(),$filter_name) instead (sorry... lol)</font><br>');
$result = array();
/* DOES NOT WORK ANYMORE
if(!empty($this->id)) {
$filtering_array =& $SESSION->user_index_filtering[$this->id];
} else {
$filtering_array =& $SESSION->user_filtering;
}
if (isset($filtering_array[$filter_name])) {
$result = $filtering_array[$filter_name];
}
*/
$reportid = 'php_report_' . $this->get_report_shortname();
if (isset($SESSION->php_report_default_params)) {
$params = $SESSION->php_report_default_params;
foreach ($params as $key=>$val) {
if ($key == $reportid.'/'.$filter_name) {
$result[] = array('value'=>$val);
}
}
}
if (!empty($result)) {
return $result;
} else {
return false;
}
}
function set_gas_gauge_page($gas_gauge_page) {
$this->gas_gauge_page = $gas_gauge_page;
}
function get_gas_gauge_page() {
return $this->gas_gauge_page;
}
/**
* Specifies the config url for this report
*
* @return string The config url
*/
function get_config_url() {
global $CFG;
return $CFG->wwwroot .'/blocks/php_report/config_params.php?id='. $this->id .'&showcancel=1'; // TBD ???
}
/**
* Specifies the config header for this report
*
* @return string The HTML content of the config header
*/
function get_config_header() {
global $CFG, $OUTPUT;
require_once($CFG->libdir.'/filelib.php');
if (!isloggedin() || isguestuser()) {
//user is not properly logged in
return '';
}
//determine if we should show the configure filter link
$show_config_filters_link = true;
//NOTE: using has_filters instead of get_filters so reports can
//specify this info without having to construct filters
if (!$this->has_filters()) {
//no filters are defined for this report
$show_config_filters_link = false;
}
if (!$this->allow_configured_filters()) {
//configured filters are not allowed for this report
$show_config_filters_link = false;
}
//determine if we should display the scheduling link
$test_permissions_instance = php_report::get_default_instance($this->get_report_shortname(), NULL, php_report::EXECUTION_MODE_SCHEDULED);
$show_schedule_report_link = $test_permissions_instance !== FALSE;
//also assert that at least one export format is available
$export_formats = $this->get_export_formats();
//if (count($export_formats) == 0) {
// $show_schedule_report_link = false;
//}
$show_export_icons = count($export_formats) > 0;
//determine if some icon needs to be displayed
$show_something = $show_config_filters_link || $show_schedule_report_link || $show_export_icons;
if (!$show_something) {
//no filter, export or schedule links to show
return '';
}
$result = html_writer::start_tag('div', array('class' => 'php_report_config_header'));
if ($show_config_filters_link) {
//link for configuring parameters
$alt_text = get_string('config_params', 'block_php_report');
//link to parameter screen with cancel button
$config_params_url = $this->get_config_url();
//start of anchor for link
$result .= html_writer::start_tag('a', array('href' => $config_params_url));
//icon
$result .= html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url('configuration', 'block_php_report'),
'border' => 0,
'width' => 16,
'height' => 16,
'alt' => $alt_text,
'title' => $alt_text));
//end of anchor for link
$result .= html_writer::end_tag('a');
$result .= ' ';
}
//loop through the possible export formats and add the export links if
//they are supported by the report
$export_formats = $this->get_export_formats();
$allowable_export_formats = php_report::get_allowable_export_formats();
foreach ($allowable_export_formats as $allowable_export_format) {
if (in_array($allowable_export_format, $export_formats)) {
//link alt text
$alt_text = get_string('export_link_'.$allowable_export_format, 'block_php_report');
//export link, with hash for smooth flow
$export_url = $CFG->wwwroot.'/blocks/php_report/download.php?id='.$this->id.'&gas_gauge_page=' . $this->gas_gauge_page . '&format='.$allowable_export_format.'#';
//icon url
$icon = 'f/'.mimeinfo('icon', "foo.$allowable_export_format");
//start of anchor for link
$result .= html_writer::start_tag('a', array('href' => $export_url, 'target' => '_blank'));
//icon
$result .= html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url($icon),
'border' => 0,
'width' => 16,
'height' => 16,
'alt' => $alt_text,
'title' => $alt_text));
//end of anchor for link
$result .= html_writer::end_tag('a');
$result .= '  ';
}
}
if ($show_schedule_report_link) {
//link for scheduling reports
$report_shortname = $this->get_report_shortname();
//url to link to
$schedule_report_url = '/blocks/php_report/schedule.php?report='.$report_shortname.'&action=listinstancejobs&createifnone=1';
//containing span
$result .= html_writer::start_tag('span', array('class' => 'external_report_link php_report_schedule_this_link'));
$result .= get_string('schedule_this_report', 'block_php_report').' ';
//open popup code
$jsondata = array('url' => $schedule_report_url,
'name' => 'php_report_param_popup',
'options' => "\"menubar=0,location=0,scrollbars,status,resizable,width=1600,height=600\"");
$jsondata = json_encode($jsondata);
$onclick = "return openpopup(null, $jsondata)";
//start of anchor for link
$result .= html_writer::start_tag('a', array('href' => '#',
'onclick' => $onclick));
//icon
$result .= html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url('schedule', 'block_php_report')));
//end of anchor for link
$result .= html_writer::end_tag('a');
//end of span
$result .= html_writer::end_tag('span');
}
$result .= html_writer::end_tag('div');
return $result;
}
/**
* Specifies whether the report can be configured to use interactive filters
* (per-user, on the fly)
*
* @return boolean TRUE if enabled, otherwise FALSE
*/
function allow_interactive_filters() {
return FALSE;
}
/**
* Specifies whether the report can be configured to use default values
* (per-user)
*
* @return boolean TRUE if enabled, otherwise FALSE
*/
function allow_configured_filters() {
return TRUE;
}
/**
* Returns the HTML representing the interactive filter display,
* which shows available and active filters
*
* @return string The HTML content of the display
*/
function get_interactive_filter_display() {
//todo: determine if this method is even needed anymore
$result = '';
if (isset($this->filter) && $this->allow_interactive_filters()) {
$result .= $this->filter->display_add(true);
$result .= $this->filter->display_active(true);
}
return $result;
}
/**
* Specifies which filters, if any, are required in order for this report
* to be run
*
* @param int $execution_mode Mode in which the report is currently being executed
* (should be one of the EXECUTION_MODE_... constants)
*
* @return string array Listing of filter shortnames representing required filters,
* from either the main report filters or secondary filters
*/
function get_required_filters($execution_mode) {
//nothing is required by default, implement in report instance
return array();
}
/**
* Specifies the shortname for this report
*
* @return string Report shortname
*/
function get_report_shortname() {
$classname = get_class($this);
$reportname = substr($classname, 0, strlen($classname) - strlen('_report'));
return $reportname;
}
/**
* Converts a report shortname to the file path
* containing the report definition
*
* @param string shortname The shortname of the report
* (same as instance folder name)
*
* @return string The absolute path of the file
* containing the report definition
*/
static function get_report_filename($shortname) {
global $CFG;
//path to the report instance folder
$base_path = $CFG->dirroot .'/blocks/php_report/instances/';
//name of php file containing report definition
$class_filename = $shortname . '_report.class.php';
//full file path
$full_path = $base_path . $shortname . '/' . $class_filename;
return $full_path;
}
/**
* Provides a default report instance used to display a report
* (report is not a pre-configured instance)
*
* @param string $shortname Shortname of the report to be obtained
* @param int|NULL $userid Id of the Moodle user who this report is being
* for
* @param int $execution_mode The mode in which this report is being executed
*
* @return php_report|boolean A new report instance of the specified type, or FALSE
* if user doesn't have sufficient permissions
*/
static function get_default_instance($shortname, $userid = NULL, $execution_mode = php_report::EXECUTION_MODE_INTERACTIVE) {
global $CFG;
//get the name of the file containing the report definition
$filename = php_report::get_report_filename($shortname);
if (!file_exists($filename)) {
//could not find the report definition
return FALSE;
}
//load report definition
require_once($filename);
//create report instance
$classname = $shortname . '_report';
//report "id" is actually the shortname because we are not
//within a block instance
$instance = new $classname($shortname, $userid, $execution_mode);
if (!$instance->is_available()) {
//necessary components for running this report are not installed
return FALSE;
}
if (!$instance->can_view_report()) {
//user doesn't have sufficient permissions
return FALSE;
}
$instance->require_dependencies();
return $instance;
}
/**
* Temporarily allocates extra resources needed to export reports
*/
static function allocate_extra_resources() {
global $CFG;
//disable the time limit for this executing script
@set_time_limit(0);
//up the memory limit
if (empty($CFG->extramemorylimit)) {
raise_memory_limit('128M');
} else {
raise_memory_limit($CFG->extramemorylimit);
}
}
/**
* API call for exporting a report execution
*
* @param string $shortname Shortname of the report to be executed (should be
* one of the folder names found in the "instances" directory)
* @param string $format The export format (one of the EXPORT_FORMAT_... constants)
* @param string $filename Name of the file to write to (including extension)
* @param stdClass $parameter_data Submitted parameter form data (in format directly returned by get_data)
* @param int|NULL $userid Id of the Moodle user who this report is being
* for
* @param int $execution_mode The mode in which this report is being executed
*
* @return boolean true on success, otherwise false
*/
static function export_default_instance($shortname, $format, $filename, $parameter_data, $userid = NULL, $execution_mode = php_report::EXECUTION_MODE_INTERACTIVE) {
//allocate some extra resources
php_report::allocate_extra_resources();
//get a default instance
$report_instance = php_report::get_default_instance($shortname, $userid, $execution_mode);
if ($report_instance == false) {
//user no longer has access, so signal failure
return false;
}
$allowable_formats = $report_instance->get_export_formats();
if (!in_array($format, $allowable_formats)) {
//specified format is not allowed
return;
}
//make sure all dependencies are loaded
$report_instance->require_dependencies();
//initialize all necessary data
$report_instance->init_all($shortname, $parameter_data);
//calculate the query and all necessary parameter values
list($sql, $params) = $report_instance->get_complete_sql_query(FALSE);
//run the report
$report_instance->download($format, $sql, $params, $filename);
return true;
}
/**
* Specifies the capability needed to execute the current report
*
* @return string The capability string that should be used for permissions checking
*/
function get_access_capability() {
if ($this->execution_mode == php_report::EXECUTION_MODE_INTERACTIVE) {
//running for the UI
return 'block/php_report:view';
} else {
//running a scheduled report
return 'block/php_report:schedule';
}
}
/**
* Returns a float or a string which denotes the user's timezone
* A float value means that a simple offset from GMT is used, while a string (it will be the name of a timezone in the database)
* means that for this timezone there are also DST rules to be taken into account
* Checks various settings and picks the most dominant of those which have a value
* (this is essentially a copy of the core Moodle function, but allows user to be specified)
*
* @uses $CFG
* @param object $user_record The user who is running the report
* @param float $tz If this value is provided and not equal to 99, it will be returned as is and no other settings will be checked
* @return mixed
*/
static function get_user_timezone($user_record, $tz = 99) {
global $CFG;
$timezones = array(
$tz,
isset($CFG->forcetimezone) ? $CFG->forcetimezone : 99,
isset($user_record->timezone) ? $user_record->timezone : 99,
isset($CFG->timezone) ? $CFG->timezone : 99,
);
$tz = 99;
while(($tz === '' || $tz == 99 || $tz === NULL) && $next = each($timezones)) {
$tz = $next['value'];
}
return is_numeric($tz) ? (float) $tz : $tz;
}
/**
* from /lib/moodlelib.php::userdate()
* Returns a formatted string that represents a date in user time
*
* @param int $date timestamp in GMT
* @param string $format strftime format
* @param bool $fixday If true then the leading zero from %d is removed.
* If false (default) then the leading zero is mantained.
* @uses $DB
* @return string
*/
function userdate($date, $format='', $fixday = false) { // TBD: false???
global $DB;
$timezone = 99;
if ($user_record = $DB->get_record('user', array('id' => $this->userid))) {
//determine the user's timezone
$timezone = php_report::get_user_timezone($user_record, $user_record->timezone);
}
//perform the formatting
return userdate($date, $format, $timezone, $fixday);
}
/**
* Return class start/end date-time
*
* @param object $pmclass The pmclass who's date/time we desire
* @param string $start_or_end Either 'start' or 'end'
* @param string $fmt Optional format string
* @return string The formatted date/time string.
*/
function pmclassdate($pmclass, $start_or_end = 'start', $fmt = '') {
if (empty($pmclass)) {
return '-';
}
if ($start_or_end != 'start' && $start_or_end != 'end') {
error_log("php_report_base.php::pmclassdate() coding error - invalid value for arg#2: start_or_end = '{$start_or_end}'");
return '';
}
$date = $start_or_end .'date';
$hour = $start_or_end .'timehour';
$minute = $start_or_end .'timeminute';
$datetime = $pmclass->{$date};
//the hour as a number, or 25 if N/A
$hour_number = $pmclass->{$hour};
//the minute as a number, or 61 if N/A
$minute_number = $pmclass->{$minute};
//deremine whether time is set
$time_set = $hour_number < 25 && $minute_number < 61;
if (empty($datetime)) {
$fmt = get_string('strftimetime', 'langconfig');
} else if (!$time_set) {
//we don't have a time, so just use the date format
$fmt = get_string('strftimedaydate', 'langconfig');
}
if ($time_set) {
$datetime += $pmclass->{$hour} * HOURSECS;
$datetime += $pmclass->{$minute} * MINSECS;
}
return $this->userdate($datetime, $fmt);
}
/**
* Methods related to UI / output
*/
/**
* Method to determine if we should first display 'Configure parameters' filter form
*
* @return boolean true if should redirect to filter form, false otherwise.
*/
function shouldredirecttofilter() {
if (!$this->has_filters()) {
return false;
}
if ($data = data_submitted()) {
//a forum submit action happened, so render the report
return false;
}
if (!empty($_GET)) {
//determine how many URL parameters were passed in
$array_get = (array)$_GET;
$num_elements = count($array_get);
if (isset($array_get['report'])) {
//don't count the report shortname
$num_elements--;
}
if ($num_elements > 0) {
//a non-innocuous URL parameter was passed in,
//so render the report
return false;
}
}
return true;
}
/**
* Outputs the HTML content needed at the beginning of a report,
* including handling the filter form display if necessary
*/
function display_header() {
global $CFG, $PAGE, $OUTPUT;
$report_shortname = $this->get_report_shortname();
// these JS files are needed for async reporting requests
$PAGE->requires->js('/blocks/php_report/js/reportblock.js'); // TBD: is this even used?
//outermost report div
$output = '<div id="php_report_block" class="php_report_block">';
//div for containing report body
$output .= '<div class="php_report_body" id="php_report_body_' . $this->id . '">';
//for displaying the loading animation
$output .= '<div id="throbber"></div>';
if ($this->shouldredirecttofilter()) {
//display parameter / filter UI
$this->require_dependencies();
ob_start();
$_GET['id'] = $this->get_report_shortname();
$_GET['mode'] = 'bare';
$_GET['url'] = "{$CFG->wwwroot}/blocks/php_report/config_params.php";
include(dirname(__FILE__) .'/config_params.php');
$output .= ob_get_contents();
ob_end_clean();
//end of php_report_body div
$output .= '</div>';
//end of php_report_block div
$output .= '</div>';
$output .= $OUTPUT->footer();
echo $output;
die;
}
//Set up an appropriate stylesheet
$folder_name = $this->get_report_shortname();
//Calculate the URL of the stylesheet
$stylesheet_web_path = $CFG->wwwroot .'/blocks/php_report/instances/'. $folder_name .'/styles.css';
//Calculate the file path of the stylesheet for an existence check
$stylesheet_file_path = $CFG->dirroot . substr($stylesheet_web_path, strlen($CFG->wwwroot));
//Reference stylsheet if it exists
if (file_exists($stylesheet_file_path)) {
$output .= '<style>@import url("'. $stylesheet_web_path .'");</style>';
}
//Report specific div
$output .= '<div class="'. $folder_name .' '. $this->get_report_content_css_classes(get_class($this)) .'">';
$output .= '<br/>';
echo $output;
}
/**
* Uses a base classname to create a CSS class for the current report
*/
static function get_report_content_css_classes($base_classname) {
$css_classes = array();
$current = $base_classname;
//keep grabbing the parent class until you run out of classes
while ($current !== false) {
$css_classes[] = $current;
$current = get_parent_class($current);
}
//combine all classnames into a string
return implode(' ', $css_classes);
}
function display_footer() {
global $CFG;
//Add refresh button and info about when last run
$output = $this->get_lastload_display($this->get_report_shortname());
//end of report / custom CSS div
$output .= '</div>';
//end of php_report_body div
$output .= '</div>';
//end of php_report_block div
$output .= '</div>';
echo $output;
}
/**
* Calculates and returns a message to display that
* informs users on how recent their data is
*
* @param int|string $id The current report block id
* @uses $CFG
* @uses $USER
* @return string HTML for a form with the display text and a reset button
*/
function get_lastload_display($id = 0) {
global $CFG, $USER;
$format = '%A, %B %e, %l:%M:%S %P';
$element_id = 'refresh_report';
if (!empty($id)) {
$element_id .= '_' . $id;
}
$timezone = 99;
if (isset($USER->timezone)) {
$timezone = $USER->timezone;
}
$lastload = time();
$a = userdate($lastload, $format, $timezone);
return '<form id="'.$element_id.'" action="'.$CFG->wwwroot.'/blocks/php_report/dynamicreport.php" >'.
'<input type="hidden" id="id" name="id" value="' . $id . '" />' .
'<p align="center" class="php_report_caching_info">' . get_string('infocurrent', 'block_php_report', $a) . '<br/>' .
'<input id="' . $element_id . '" type="submit" value="Refresh"/>' . '</p>' .
'</form>';
}