forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtablelib.php
1699 lines (1523 loc) · 56.9 KB
/
tablelib.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/>.
/**
* @package moodlecore
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/** TABLE_VAR_SORT = 1 */
define('TABLE_VAR_SORT', 1);
/** TABLE_VAR_HIDE = 2 */
define('TABLE_VAR_HIDE', 2);
/** TABLE_VAR_SHOW = 3 */
define('TABLE_VAR_SHOW', 3);
/** TABLE_VAR_IFIRST = 4 */
define('TABLE_VAR_IFIRST', 4);
/** TABLE_VAR_ILAST = 5 */
define('TABLE_VAR_ILAST', 5);
/** TABLE_VAR_PAGE = 6 */
define('TABLE_VAR_PAGE', 6);
/** TABLE_P_TOP = 1 */
define('TABLE_P_TOP', 1);
/** TABLE_P_BOTTOM = 2 */
define('TABLE_P_BOTTOM', 2);
/**
* @package moodlecore
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class flexible_table {
var $uniqueid = NULL;
var $attributes = array();
var $headers = array();
var $columns = array();
var $column_style = array();
var $column_class = array();
var $column_suppress = array();
var $column_nosort = array('userpic');
var $setup = false;
var $sess = NULL;
var $baseurl = NULL;
var $request = array();
var $is_collapsible = false;
var $is_sortable = false;
var $use_pages = false;
var $use_initials = false;
var $maxsortkeys = 2;
var $pagesize = 30;
var $currpage = 0;
var $totalrows = 0;
var $sort_default_column = NULL;
var $sort_default_order = SORT_ASC;
/**
* Array of positions in which to display download controls.
*/
var $showdownloadbuttonsat= array(TABLE_P_TOP);
/**
* @var string Key of field returned by db query that is the id field of the
* user table or equivalent.
*/
public $useridfield = 'id';
/**
* @var string which download plugin to use. Default '' means none - print
* html table with paging. Property set by is_downloading which typically
* passes in cleaned data from $
*/
var $download = '';
/**
* @var boolean whether data is downloadable from table. Determines whether
* to display download buttons. Set by method downloadable().
*/
var $downloadable = false;
/**
* @var string which download plugin to use. Default '' means none - print
* html table with paging.
*/
var $defaultdownloadformat = 'csv';
/**
* @var boolean Has start output been called yet?
*/
var $started_output = false;
var $exportclass = null;
/**
* Constructor
* @param int $uniqueid
* @todo Document properly
*/
function flexible_table($uniqueid) {
$this->uniqueid = $uniqueid;
$this->request = array(
TABLE_VAR_SORT => 'tsort',
TABLE_VAR_HIDE => 'thide',
TABLE_VAR_SHOW => 'tshow',
TABLE_VAR_IFIRST => 'tifirst',
TABLE_VAR_ILAST => 'tilast',
TABLE_VAR_PAGE => 'page'
);
}
/**
* Call this to pass the download type. Use :
* $download = optional_param('download', '', PARAM_ALPHA);
* To get the download type. We assume that if you call this function with
* params that this table's data is downloadable, so we call is_downloadable
* for you (even if the param is '', which means no download this time.
* Also you can call this method with no params to get the current set
* download type.
* @param string $download download type. One of csv, tsv, xhtml, ods, etc
* @param string $filename filename for downloads without file extension.
* @param string $sheettitle title for downloaded data.
* @return string download type. One of csv, tsv, xhtml, ods, etc
*/
function is_downloading($download = null, $filename='', $sheettitle=''){
if ($download!==null){
$this->sheettitle = $sheettitle;
$this->is_downloadable(true);
$this->download = $download;
$this->filename = clean_filename($filename);
$this->export_class_instance();
}
return $this->download;
}
function export_class_instance(&$exportclass=null){
if (!is_null($exportclass)){
$this->started_output = true;
$this->exportclass =& $exportclass;
$this->exportclass->table =& $this;
} elseif (is_null($this->exportclass) && !empty($this->download)){
$classname = 'table_'.$this->download.'_export_format';
$this->exportclass = new $classname($this);
if (!$this->exportclass->document_started()){
$this->exportclass->start_document($this->filename);
}
}
return $this->exportclass;
}
/**
* Probably don't need to call this directly. Calling is_downloading with a
* param automatically sets table as downloadable.
*
* @param boolean $downloadable optional param to set whether data from
* table is downloadable. If ommitted this function can be used to get
* current state of table.
* @return boolean whether table data is set to be downloadable.
*/
function is_downloadable($downloadable = null){
if ($downloadable !== null){
$this->downloadable = $downloadable;
}
return $this->downloadable;
}
/**
* Where to show download buttons.
* @param array $showat array of postions in which to show download buttons.
* Containing TABLE_P_TOP and/or TABLE_P_BOTTOM
*/
function show_download_buttons_at($showat){
$this->showdownloadbuttonsat = $showat;
}
/**
* Sets the is_sortable variable to the given boolean, sort_default_column to
* the given string, and the sort_default_order to the given integer.
* @param bool $bool
* @param string $defaultcolumn
* @param int $defaultorder
* @return void
*/
function sortable($bool, $defaultcolumn = NULL, $defaultorder = SORT_ASC) {
$this->is_sortable = $bool;
$this->sort_default_column = $defaultcolumn;
$this->sort_default_order = $defaultorder;
}
/**
* Do not sort using this column
* @param string column name
*/
function no_sorting($column) {
$this->column_nosort[] = $column;
}
/**
* Is the column sortable?
* @param string column name, null means table
* @return bool
*/
function is_sortable($column=null) {
if (empty($column)) {
return $this->is_sortable;
}
if (!$this->is_sortable) {
return false;
}
return !in_array($column, $this->column_nosort);
}
/**
* Sets the is_collapsible variable to the given boolean.
* @param bool $bool
* @return void
*/
function collapsible($bool) {
$this->is_collapsible = $bool;
}
/**
* Sets the use_pages variable to the given boolean.
* @param bool $bool
* @return void
*/
function pageable($bool) {
$this->use_pages = $bool;
}
/**
* Sets the use_initials variable to the given boolean.
* @param bool $bool
* @return void
*/
function initialbars($bool) {
$this->use_initials = $bool;
}
/**
* Sets the pagesize variable to the given integer, the totalrows variable
* to the given integer, and the use_pages variable to true.
* @param int $perpage
* @param int $total
* @return void
*/
function pagesize($perpage, $total) {
$this->pagesize = $perpage;
$this->totalrows = $total;
$this->use_pages = true;
}
/**
* Assigns each given variable in the array to the corresponding index
* in the request class variable.
* @param array $variables
* @return void
*/
function set_control_variables($variables) {
foreach($variables as $what => $variable) {
if(isset($this->request[$what])) {
$this->request[$what] = $variable;
}
}
}
/**
* Gives the given $value to the $attribute index of $this->attributes.
* @param string $attribute
* @param mixed $value
* @return void
*/
function set_attribute($attribute, $value) {
$this->attributes[$attribute] = $value;
}
/**
* What this method does is set the column so that if the same data appears in
* consecutive rows, then it is not repeated.
*
* For example, in the quiz overview report, the fullname column is set to be suppressed, so
* that when one student has made multiple attempts, their name is only printed in the row
* for their first attempt.
* @param integer $column the index of a column.
*/
function column_suppress($column) {
if(isset($this->column_suppress[$column])) {
$this->column_suppress[$column] = true;
}
}
/**
* Sets the given $column index to the given $classname in $this->column_class.
* @param integer $column
* @param string $classname
* @return void
*/
function column_class($column, $classname) {
if(isset($this->column_class[$column])) {
$this->column_class[$column] = ' '.$classname; // This space needed so that classnames don't run together in the HTML
}
}
/**
* Sets the given $column index and $property index to the given $value in $this->column_style.
* @param integer $column
* @param string $property
* @param mixed $value
* @return void
*/
function column_style($column, $property, $value) {
if(isset($this->column_style[$column])) {
$this->column_style[$column][$property] = $value;
}
}
/**
* Sets all columns' $propertys to the given $value in $this->column_style.
* @param integer $property
* @param string $value
* @return void
*/
function column_style_all($property, $value) {
foreach(array_keys($this->columns) as $column) {
$this->column_style[$column][$property] = $value;
}
}
/**
* Sets $this->reseturl to the given $url, and $this->baseurl to the given $url plus ? or &
* @param string $url the url with params needed to call up this page
*/
function define_baseurl($url) {
$this->reseturl = $url;
if(!strpos($url, '?')) {
$this->baseurl = $url.'?';
}
else {
$this->baseurl = $url.'&';
}
}
/**
* @param array $columns an array of identifying names for columns. If
* columns are sorted then column names must correspond to a field in sql.
*/
function define_columns($columns) {
$this->columns = array();
$this->column_style = array();
$this->column_class = array();
$colnum = 0;
foreach($columns as $column) {
$this->columns[$column] = $colnum++;
$this->column_style[$column] = array();
$this->column_class[$column] = '';
$this->column_suppress[$column] = false;
}
}
/**
* @param array $headers numerical keyed array of displayed string titles
* for each column.
*/
function define_headers($headers) {
$this->headers = $headers;
}
/**
* Must be called after table is defined. Use methods above first. Cannot
* use functions below till after calling this method.
* @return type?
*/
function setup() {
global $SESSION, $CFG;
if(empty($this->columns) || empty($this->uniqueid)) {
return false;
}
if (!isset($SESSION->flextable)) {
$SESSION->flextable = array();
}
if(!isset($SESSION->flextable[$this->uniqueid])) {
$SESSION->flextable[$this->uniqueid] = new stdClass;
$SESSION->flextable[$this->uniqueid]->uniqueid = $this->uniqueid;
$SESSION->flextable[$this->uniqueid]->collapse = array();
$SESSION->flextable[$this->uniqueid]->sortby = array();
$SESSION->flextable[$this->uniqueid]->i_first = '';
$SESSION->flextable[$this->uniqueid]->i_last = '';
}
$this->sess = &$SESSION->flextable[$this->uniqueid];
if(!empty($_GET[$this->request[TABLE_VAR_SHOW]]) && isset($this->columns[$_GET[$this->request[TABLE_VAR_SHOW]]])) {
// Show this column
$this->sess->collapse[$_GET[$this->request[TABLE_VAR_SHOW]]] = false;
}
else if(!empty($_GET[$this->request[TABLE_VAR_HIDE]]) && isset($this->columns[$_GET[$this->request[TABLE_VAR_HIDE]]])) {
// Hide this column
$this->sess->collapse[$_GET[$this->request[TABLE_VAR_HIDE]]] = true;
if(array_key_exists($_GET[$this->request[TABLE_VAR_HIDE]], $this->sess->sortby)) {
unset($this->sess->sortby[$_GET[$this->request[TABLE_VAR_HIDE]]]);
}
}
// Now, update the column attributes for collapsed columns
foreach(array_keys($this->columns) as $column) {
if(!empty($this->sess->collapse[$column])) {
$this->column_style[$column]['width'] = '10px';
}
}
if(
!empty($_GET[$this->request[TABLE_VAR_SORT]]) && $this->is_sortable($_GET[$this->request[TABLE_VAR_SORT]]) &&
(isset($this->columns[$_GET[$this->request[TABLE_VAR_SORT]]]) ||
(($_GET[$this->request[TABLE_VAR_SORT]] == 'firstname' || $_GET[$this->request[TABLE_VAR_SORT]] == 'lastname') && isset($this->columns['fullname']))
))
{
if(empty($this->sess->collapse[$_GET[$this->request[TABLE_VAR_SORT]]])) {
if(array_key_exists($_GET[$this->request[TABLE_VAR_SORT]], $this->sess->sortby)) {
// This key already exists somewhere. Change its sortorder and bring it to the top.
$sortorder = $this->sess->sortby[$_GET[$this->request[TABLE_VAR_SORT]]] == SORT_ASC ? SORT_DESC : SORT_ASC;
unset($this->sess->sortby[$_GET[$this->request[TABLE_VAR_SORT]]]);
$this->sess->sortby = array_merge(array($_GET[$this->request[TABLE_VAR_SORT]] => $sortorder), $this->sess->sortby);
}
else {
// Key doesn't exist, so just add it to the beginning of the array, ascending order
$this->sess->sortby = array_merge(array($_GET[$this->request[TABLE_VAR_SORT]] => SORT_ASC), $this->sess->sortby);
}
// Finally, make sure that no more than $this->maxsortkeys are present into the array
if(!empty($this->maxsortkeys) && ($sortkeys = count($this->sess->sortby)) > $this->maxsortkeys) {
while($sortkeys-- > $this->maxsortkeys) {
array_pop($this->sess->sortby);
}
}
}
}
// If we didn't sort just now, then use the default sort order if one is defined and the column exists
if(empty($this->sess->sortby) && !empty($this->sort_default_column)) {
$this->sess->sortby = array ($this->sort_default_column => ($this->sort_default_order == SORT_DESC ? SORT_DESC : SORT_ASC));
}
if(isset($_GET[$this->request[TABLE_VAR_ILAST]])) {
if(empty($_GET[$this->request[TABLE_VAR_ILAST]]) || is_numeric(strpos(get_string('alphabet'), $_GET[$this->request[TABLE_VAR_ILAST]]))) {
$this->sess->i_last = $_GET[$this->request[TABLE_VAR_ILAST]];
}
}
if(isset($_GET[$this->request[TABLE_VAR_IFIRST]])) {
if(empty($_GET[$this->request[TABLE_VAR_IFIRST]]) || is_numeric(strpos(get_string('alphabet'), $_GET[$this->request[TABLE_VAR_IFIRST]]))) {
$this->sess->i_first = $_GET[$this->request[TABLE_VAR_IFIRST]];
}
}
if(empty($this->baseurl)) {
$getcopy = $_GET;
unset($getcopy[$this->request[TABLE_VAR_SHOW]]);
unset($getcopy[$this->request[TABLE_VAR_HIDE]]);
unset($getcopy[$this->request[TABLE_VAR_SORT]]);
unset($getcopy[$this->request[TABLE_VAR_IFIRST]]);
unset($getcopy[$this->request[TABLE_VAR_ILAST]]);
unset($getcopy[$this->request[TABLE_VAR_PAGE]]);
$strippedurl = strip_querystring(qualified_me());
if(!empty($getcopy)) {
$first = false;
$querystring = '';
foreach($getcopy as $var => $val) {
if(!$first) {
$first = true;
$querystring .= '?'.$var.'='.$val;
}
else {
$querystring .= '&'.$var.'='.$val;
}
}
$this->reseturl = $strippedurl.$querystring;
$querystring .= '&';
}
else {
$this->reseturl = $strippedurl;
$querystring = '?';
}
$this->baseurl = strip_querystring(qualified_me()) . $querystring;
}
// If it's "the first time" we 've been here, forget the previous initials filters
if(qualified_me() == $this->reseturl) {
$this->sess->i_first = '';
$this->sess->i_last = '';
}
$this->currpage = optional_param($this->request[TABLE_VAR_PAGE], 0, PARAM_INT);
$this->setup = true;
/// Always introduce the "flexible" class for the table if not specified
/// No attributes, add flexible class
if (empty($this->attributes)) {
$this->attributes['class'] = 'flexible';
/// No classes, add flexible class
} else if (!isset($this->attributes['class'])) {
$this->attributes['class'] = 'flexible';
/// No flexible class in passed classes, add flexible class
} else if (!in_array('flexible', explode(' ', $this->attributes['class']))) {
$this->attributes['class'] = trim('flexible ' . $this->attributes['class']);
}
}
/**
* Get the order by clause from the session, for the table with id $uniqueid.
* @param string $uniqueid the identifier for a table.
* @return SQL fragment that can be used in an ORDER BY clause.
*/
public static function get_sort_for_table($uniqueid) {
global $SESSION;
if(empty($SESSION->flextable[$uniqueid])) {
return '';
}
$sess = &$SESSION->flextable[$uniqueid];
if (empty($sess->sortby)) {
return '';
}
return self::construct_order_by($sess->sortby);
}
/**
* Prepare an an order by clause from the list of columns to be sorted.
* @param array $cols column name => SORT_ASC or SORT_DESC
* @return SQL fragment that can be used in an ORDER BY clause.
*/
public static function construct_order_by($cols) {
$bits = array();
foreach($cols as $column => $order) {
if ($order == SORT_ASC) {
$bits[] = $column . ' ASC';
} else {
$bits[] = $column . ' DESC';
}
}
return implode(', ', $bits);
}
/**
* @return SQL fragment that can be used in an ORDER BY clause.
*/
public function get_sql_sort() {
return self::construct_order_by($this->get_sort_columns());
}
/**
* Get the columns to sort by, in the form required by {@link construct_order_by()}.
* @return array column name => SORT_... constant.
*/
public function get_sort_columns() {
if (!$this->setup) {
throw new coding_exception('Cannot call get_sort_columns until you have called setup.');
}
if (empty($this->sess->sortby)) {
return array();
}
return $this->sess->sortby;
}
/**
* @return integer the offset for LIMIT clause of SQL
*/
function get_page_start() {
if(!$this->use_pages) {
return '';
}
return $this->currpage * $this->pagesize;
}
/**
* @return integer the pagesize for LIMIT clause of SQL
*/
function get_page_size() {
if(!$this->use_pages) {
return '';
}
return $this->pagesize;
}
/**
* @return string sql to add to where statement.
*/
function get_sql_where() {
global $DB;
if(!isset($this->columns['fullname'])) {
return '';
}
$LIKE = $DB->sql_ilike();
if(!empty($this->sess->i_first) && !empty($this->sess->i_last)) {
return 'firstname '.$LIKE.' \''.$this->sess->i_first.'%\' AND lastname '.$LIKE.' \''.$this->sess->i_last.'%\'';
}
else if(!empty($this->sess->i_first)) {
return 'firstname '.$LIKE.' \''.$this->sess->i_first.'%\'';
}
else if(!empty($this->sess->i_last)) {
return 'lastname '.$LIKE.' \''.$this->sess->i_last.'%\'';
}
return '';
}
/**
* Add a row of data to the table. This function takes an array with
* column names as keys.
* It ignores any elements with keys that are not defined as columns. It
* puts in empty strings into the row when there is no element in the passed
* array corresponding to a column in the table. It puts the row elements in
* the proper order.
* @param $rowwithkeys array
* @param string $classname CSS class name to add to this row's tr tag.
*/
function add_data_keyed($rowwithkeys, $classname = ''){
$this->add_data($this->get_row_from_keyed($rowwithkeys), $classname);
}
/**
* Add a seperator line to table.
*/
function add_separator() {
if(!$this->setup) {
return false;
}
$this->add_data(NULL);
}
/**
* This method actually directly echoes the row passed to it now or adds it
* to the download. If this is the first row and start_output has not
* already been called this method also calls start_output to open the table
* or send headers for the downloaded.
* Can be used as before. print_html now calls finish_html to close table.
*
* @param array $row a numerically keyed row of data to add to the table.
* @param string $classname CSS class name to add to this row's tr tag.
* @return boolean success.
*/
function add_data($row, $classname = '') {
if(!$this->setup) {
return false;
}
if (!$this->started_output){
$this->start_output();
}
if ($this->exportclass!==null){
if ($row === null){
$this->exportclass->add_seperator();
} else {
$this->exportclass->add_data($row);
}
} else {
$this->print_row($row, $classname);
}
return true;
}
/**
* You should call this to finish outputting the table data after adding
* data to the table with add_data or add_data_keyed.
*
*/
function finish_output($closeexportclassdoc = true){
if ($this->exportclass!==null){
$this->exportclass->finish_table();
if ($closeexportclassdoc){
$this->exportclass->finish_document();
}
}else{
$this->finish_html();
}
}
/**
* Hook that can be overridden in child classes to wrap a table in a form
* for example. Called only when there is data to display and not
* downloading.
*/
function wrap_html_start(){
}
/**
* Hook that can be overridden in child classes to wrap a table in a form
* for example. Called only when there is data to display and not
* downloading.
*/
function wrap_html_finish(){
}
/**
*
* @param array $row row of data from db used to make one row of the table.
* @return array one row for the table, added using add_data_keyed method.
*/
function format_row($row){
$formattedrow = array();
foreach (array_keys($this->columns) as $column){
$colmethodname = 'col_'.$column;
if (method_exists($this, $colmethodname)){
$formattedcolumn = $this->$colmethodname($row);
} else {
$formattedcolumn = $this->other_cols($column, $row);
if ($formattedcolumn===NULL){
$formattedcolumn = $row->$column;
}
}
$formattedrow[$column] = $formattedcolumn;
}
return $formattedrow;
}
/**
* Fullname is treated as a special columname in tablelib and should always
* be treated the same as the fullname of a user.
* @uses $this->useridfield if the userid field is not expected to be id
* then you need to override $this->useridfield to point at the correct
* field for the user id.
*
*/
function col_fullname($row){
global $COURSE, $CFG;
if (!$this->download){
if ($COURSE->id == SITEID) {
return '<a href="'.$CFG->wwwroot.'/user/profile.php?id='.$row->{$this->useridfield}. '">'.
fullname($row).'</a>';
} else {
return '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$row->{$this->useridfield}.
'&course='.$COURSE->id.'">'.fullname($row).'</a>';
}
} else {
return fullname($row);
}
}
/**
* You can override this method in a child class. See the description of
* build_table which calls this method.
*/
function other_cols($column, $row){
return NULL;
}
/**
* Used from col_* functions when text is to be displayed. Does the
* right thing - either converts text to html or strips any html tags
* depending on if we are downloading and what is the download type. Params
* are the same as format_text function in weblib.php but some default
* options are changed.
*/
function format_text($text, $format=FORMAT_MOODLE, $options=NULL, $courseid=NULL){
if (!$this->is_downloading()){
if (is_null($options)){
$options = new stdClass;
}
//some sensible defaults
if (!isset($options->para)){
$options->para = false;
}
if (!isset($options->newlines)){
$options->newlines = false;
}
if (!isset($options->smiley)) {
$options->smiley = false;
}
if (!isset($options->filter)) {
$options->filter = false;
}
return format_text($text, $format, $options);
} else {
$eci =& $this->export_class_instance();
return $eci->format_text($text, $format, $options, $courseid);
}
}
/**
* This method is deprecated although the old api is still supported.
* @deprecated 1.9.2 - Jun 2, 2008
*/
function print_html() {
if(!$this->setup) {
return false;
}
$this->finish_html();
}
/**
* This function is not part of the public api.
* @return string initial of first name we are currently filtering by
*/
function get_initial_first() {
if(!$this->use_initials) {
return NULL;
}
return $this->sess->i_first;
}
/**
* This function is not part of the public api.
* @return string initial of last name we are currently filtering by
*/
function get_initial_last() {
if(!$this->use_initials) {
return NULL;
}
return $this->sess->i_last;
}
/**
* This function is not part of the public api.
*/
function print_initials_bar(){
if ((!empty($this->sess->i_last) || !empty($this->sess->i_first) || $this->use_initials)
&& isset($this->columns['fullname'])) {
$strall = get_string('all');
$alpha = explode(',', get_string('alphabet', 'langconfig'));
// Bar of first initials
echo '<div class="initialbar firstinitial">'.get_string('firstname').' : ';
if(!empty($this->sess->i_first)) {
echo '<a href="'.$this->baseurl.$this->request[TABLE_VAR_IFIRST].'=">'.$strall.'</a>';
} else {
echo '<strong>'.$strall.'</strong>';
}
foreach ($alpha as $letter) {
if (isset($this->sess->i_first) && $letter == $this->sess->i_first) {
echo ' <strong>'.$letter.'</strong>';
} else {
echo ' <a href="'.$this->baseurl.$this->request[TABLE_VAR_IFIRST].'='.$letter.'">'.$letter.'</a>';
}
}
echo '</div>';
// Bar of last initials
echo '<div class="initialbar lastinitial">'.get_string('lastname').' : ';
if(!empty($this->sess->i_last)) {
echo '<a href="'.$this->baseurl.$this->request[TABLE_VAR_ILAST].'=">'.$strall.'</a>';
} else {
echo '<strong>'.$strall.'</strong>';
}
foreach ($alpha as $letter) {
if (isset($this->sess->i_last) && $letter == $this->sess->i_last) {
echo ' <strong>'.$letter.'</strong>';
} else {
echo ' <a href="'.$this->baseurl.$this->request[TABLE_VAR_ILAST].'='.$letter.'">'.$letter.'</a>';
}
}
echo '</div>';
}
}
/**
* This function is not part of the public api.
*/
function print_nothing_to_display(){
global $OUTPUT;
$this->print_initials_bar();
echo $OUTPUT->heading(get_string('nothingtodisplay'));
}
/**
* This function is not part of the public api.
*/
function get_row_from_keyed($rowwithkeys){
if (is_object($rowwithkeys)){
$rowwithkeys = (array)$rowwithkeys;
}
$row = array();
foreach (array_keys($this->columns) as $column){
if (isset($rowwithkeys[$column])){
$row [] = $rowwithkeys[$column];
} else {
$row[] ='';
}
}
return $row;
}
/**
* This function is not part of the public api.
*/
function get_download_menu(){
$allclasses= get_declared_classes();
$exportclasses = array();
foreach ($allclasses as $class){
$matches = array();
if (preg_match('/^table\_([a-z]+)\_export\_format$/', $class, $matches)){
$type = $matches[1];
$exportclasses[$type]= get_string("download$type", 'table');
}
}
return $exportclasses;
}
/**
* This function is not part of the public api.
*/
function download_buttons(){
global $OUTPUT;
if ($this->is_downloadable() && !$this->is_downloading()){
$downloadoptions = $this->get_download_menu();
$html = '<form action="'. $this->baseurl .'" method="post">';
$html .= '<div class="mdl-align">';
$html .= '<input type="submit" value="'.get_string('downloadas', 'table').'"/>';
$html .= html_writer::select($downloadoptions, 'download', $this->defaultdownloadformat, false);
$html .= $OUTPUT->old_help_icon('tableexportformats', get_string('tableexportformats', 'table'));
$html .= '</div></form>';
return $html;
} else {
return '';
}
}
/**
* This function is not part of the public api.
* You don't normally need to call this. It is called automatically when
* needed when you start adding data to the table.
*
*/
function start_output(){
$this->started_output = true;
if ($this->exportclass!==null){
$this->exportclass->start_table($this->sheettitle);
$this->exportclass->output_headers($this->headers);
} else {
$this->start_html();
$this->print_headers();
}
}
/**
* This function is not part of the public api.
*/
function print_row($row, $classname = '') {
static $suppress_lastrow = NULL;
static $oddeven = 1;
$rowclasses = array('r' . $oddeven);
$oddeven = $oddeven ? 0 : 1;
if ($classname) {
$rowclasses[] = $classname;
}
echo '<tr class="' . implode(' ', $rowclasses) . '">';
// If we have a separator, print it
if ($row === NULL) {
$colcount = count($this->columns);
echo '<td colspan="'.$colcount.'"><div class="tabledivider"></div></td>';
} else {
$colbyindex = array_flip($this->columns);
foreach ($row as $index => $data) {
$column = $colbyindex[$index];
echo '<td class="cell c'.$index.$this->column_class[$column].'"'.$this->make_styles_string($this->column_style[$column]).'>';
if (empty($this->sess->collapse[$column])) {
if ($this->column_suppress[$column] && $suppress_lastrow !== NULL && $suppress_lastrow[$index] === $data) {