3 define('TABLE_VAR_SORT', 1);
4 define('TABLE_VAR_HIDE', 2);
5 define('TABLE_VAR_SHOW', 3);
6 define('TABLE_VAR_IFIRST', 4);
7 define('TABLE_VAR_ILAST', 5);
8 define('TABLE_VAR_PAGE', 6);
10 class flexible_table
{
13 var $attributes = array();
14 var $headers = array();
15 var $columns = array();
16 var $column_style = array();
17 var $column_class = array();
18 var $column_suppress = array();
19 var $column_nosort = array('userpic');
23 var $request = array();
25 var $is_collapsible = false;
26 var $is_sortable = false;
27 var $use_pages = false;
28 var $use_initials = false;
34 var $sort_default_column = NULL;
35 var $sort_default_order = SORT_ASC
;
39 * @param int $uniqueid
40 * @todo Document properly
42 function flexible_table($uniqueid) {
43 $this->uniqueid
= $uniqueid;
44 $this->request
= array(
45 TABLE_VAR_SORT
=> 'tsort',
46 TABLE_VAR_HIDE
=> 'thide',
47 TABLE_VAR_SHOW
=> 'tshow',
48 TABLE_VAR_IFIRST
=> 'tifirst',
49 TABLE_VAR_ILAST
=> 'tilast',
50 TABLE_VAR_PAGE
=> 'page'
55 * Sets the is_sortable variable to the given boolean, sort_default_column to
56 * the given string, and the sort_default_order to the given integer.
58 * @param string $defaultcolumn
59 * @param int $defaultorder
62 function sortable($bool, $defaultcolumn = NULL, $defaultorder = SORT_ASC
) {
63 $this->is_sortable
= $bool;
64 $this->sort_default_column
= $defaultcolumn;
65 $this->sort_default_order
= $defaultorder;
69 * Do not sort using this column
70 * @param string column name
72 function no_sorting($column) {
73 $this->column_nosort
[] = $column;
77 * Is the column sortable?
78 * @param string column name, null means table
81 function is_sortable($column=null) {
83 return $this->is_sortable
;
85 if (!$this->is_sortable
) {
88 return !in_array($column, $this->column_nosort
);
92 * Sets the is_collapsible variable to the given boolean.
96 function collapsible($bool) {
97 $this->is_collapsible
= $bool;
101 * Sets the use_pages variable to the given boolean.
105 function pageable($bool) {
106 $this->use_pages
= $bool;
110 * Sets the use_initials variable to the given boolean.
114 function initialbars($bool) {
115 $this->use_initials
= $bool;
119 * Sets the pagesize variable to the given integer, the totalrows variable
120 * to the given integer, and the use_pages variable to true.
121 * @param int $perpage
125 function pagesize($perpage, $total) {
126 $this->pagesize
= $perpage;
127 $this->totalrows
= $total;
128 $this->use_pages
= true;
132 * Assigns each given variable in the array to the corresponding index
133 * in the request class variable.
134 * @param array $variables
137 function set_control_variables($variables) {
138 foreach($variables as $what => $variable) {
139 if(isset($this->request
[$what])) {
140 $this->request
[$what] = $variable;
146 * Gives the given $value to the $attribute index of $this->attributes.
147 * @param string $attribute
148 * @param mixed $value
151 function set_attribute($attribute, $value) {
152 $this->attributes
[$attribute] = $value;
156 * I think that what this method does is set the column so that if the same data appears in
157 * consecutive rows, then it is not repeated.
159 * For example, in the quiz overview report, the fullname column is set to be suppressed, so
160 * that when one student has made multiple attempts, their name is only printed in the row
161 * for their first attempt.
162 * @param integer $column the index of a column.
164 function column_suppress($column) {
165 if(isset($this->column_suppress
[$column])) {
166 $this->column_suppress
[$column] = true;
171 * Sets the given $column index to the given $classname in $this->column_class.
172 * @param integer $column
173 * @param string $classname
176 function column_class($column, $classname) {
177 if(isset($this->column_class
[$column])) {
178 $this->column_class
[$column] = ' '.$classname; // This space needed so that classnames don't run together in the HTML
183 * Sets the given $column index and $property index to the given $value in $this->column_style.
184 * @param integer $column
185 * @param string $property
186 * @param mixed $value
189 function column_style($column, $property, $value) {
190 if(isset($this->column_style
[$column])) {
191 $this->column_style
[$column][$property] = $value;
196 * Sets all columns of the given $property to the given $value in $this->column_style.
197 * @param integer $property
198 * @param string $value
201 function column_style_all($property, $value) {
202 foreach(array_keys($this->columns
) as $column) {
203 $this->column_style
[$column][$property] = $value;
208 * Sets $this->reseturl to the given $url, and $this->baseurl to the given $url plus ? or &
212 function define_baseurl($url) {
213 $this->reseturl
= $url;
214 if(!strpos($url, '?')) {
215 $this->baseurl
= $url.'?';
218 $this->baseurl
= $url.'&';
226 function define_columns($columns) {
227 $this->columns
= array();
228 $this->column_style
= array();
229 $this->column_class
= array();
232 foreach($columns as $column) {
233 $this->columns
[$column] = $colnum++
;
234 $this->column_style
[$column] = array();
235 $this->column_class
[$column] = '';
236 $this->column_suppress
[$column] = false;
244 function define_headers($headers) {
245 $this->headers
= $headers;
252 function make_styles_string(&$styles) {
257 $string = ' style="';
258 foreach($styles as $property => $value) {
259 $string .= $property.':'.$value.';';
269 function make_attributes_string(&$attributes) {
270 if(empty($attributes)) {
275 foreach($attributes as $attr => $value) {
276 $string .= ($attr.'="'.$value.'" ');
287 global $SESSION, $CFG;
289 if(empty($this->columns
) ||
empty($this->uniqueid
)) {
293 if (!isset($SESSION->flextable
)) {
294 $SESSION->flextable
= array();
297 if(!isset($SESSION->flextable
[$this->uniqueid
])) {
298 $SESSION->flextable
[$this->uniqueid
] = new stdClass
;
299 $SESSION->flextable
[$this->uniqueid
]->uniqueid
= $this->uniqueid
;
300 $SESSION->flextable
[$this->uniqueid
]->collapse
= array();
301 $SESSION->flextable
[$this->uniqueid
]->sortby
= array();
302 $SESSION->flextable
[$this->uniqueid
]->i_first
= '';
303 $SESSION->flextable
[$this->uniqueid
]->i_last
= '';
306 $this->sess
= &$SESSION->flextable
[$this->uniqueid
];
308 if(!empty($_GET[$this->request
[TABLE_VAR_SHOW
]]) && isset($this->columns
[$_GET[$this->request
[TABLE_VAR_SHOW
]]])) {
310 $this->sess
->collapse
[$_GET[$this->request
[TABLE_VAR_SHOW
]]] = false;
312 else if(!empty($_GET[$this->request
[TABLE_VAR_HIDE
]]) && isset($this->columns
[$_GET[$this->request
[TABLE_VAR_HIDE
]]])) {
314 $this->sess
->collapse
[$_GET[$this->request
[TABLE_VAR_HIDE
]]] = true;
315 if(array_key_exists($_GET[$this->request
[TABLE_VAR_HIDE
]], $this->sess
->sortby
)) {
316 unset($this->sess
->sortby
[$_GET[$this->request
[TABLE_VAR_HIDE
]]]);
320 // Now, update the column attributes for collapsed columns
321 foreach(array_keys($this->columns
) as $column) {
322 if(!empty($this->sess
->collapse
[$column])) {
323 $this->column_style
[$column]['width'] = '10px';
328 !empty($_GET[$this->request
[TABLE_VAR_SORT
]]) && $this->is_sortable($_GET[$this->request
[TABLE_VAR_SORT
]]) &&
329 (isset($this->columns
[$_GET[$this->request
[TABLE_VAR_SORT
]]]) ||
330 (($_GET[$this->request
[TABLE_VAR_SORT
]] == 'firstname' ||
$_GET[$this->request
[TABLE_VAR_SORT
]] == 'lastname') && isset($this->columns
['fullname']))
333 if(empty($this->sess
->collapse
[$_GET[$this->request
[TABLE_VAR_SORT
]]])) {
334 if(array_key_exists($_GET[$this->request
[TABLE_VAR_SORT
]], $this->sess
->sortby
)) {
335 // This key already exists somewhere. Change its sortorder and bring it to the top.
336 $sortorder = $this->sess
->sortby
[$_GET[$this->request
[TABLE_VAR_SORT
]]] == SORT_ASC ? SORT_DESC
: SORT_ASC
;
337 unset($this->sess
->sortby
[$_GET[$this->request
[TABLE_VAR_SORT
]]]);
338 $this->sess
->sortby
= array_merge(array($_GET[$this->request
[TABLE_VAR_SORT
]] => $sortorder), $this->sess
->sortby
);
341 // Key doesn't exist, so just add it to the beginning of the array, ascending order
342 $this->sess
->sortby
= array_merge(array($_GET[$this->request
[TABLE_VAR_SORT
]] => SORT_ASC
), $this->sess
->sortby
);
344 // Finally, make sure that no more than $this->maxsortkeys are present into the array
345 if(!empty($this->maxsortkeys
) && ($sortkeys = count($this->sess
->sortby
)) > $this->maxsortkeys
) {
346 while($sortkeys-- > $this->maxsortkeys
) {
347 array_pop($this->sess
->sortby
);
353 // If we didn't sort just now, then use the default sort order if one is defined and the column exists
354 if(empty($this->sess
->sortby
) && !empty($this->sort_default_column
) && (isset($this->columns
[$this->sort_default_column
])
355 ||
(in_array('fullname',$this->columns
)
356 && in_array($this->sort_default_column
,
357 array('firstname','lastname'))))) {
358 $this->sess
->sortby
= array ($this->sort_default_column
=> ($this->sort_default_order
== SORT_DESC ? SORT_DESC
: SORT_ASC
));
361 if(isset($_GET[$this->request
[TABLE_VAR_ILAST
]])) {
362 if(empty($_GET[$this->request
[TABLE_VAR_ILAST
]]) ||
is_numeric(strpos(get_string('alphabet'), $_GET[$this->request
[TABLE_VAR_ILAST
]]))) {
363 $this->sess
->i_last
= $_GET[$this->request
[TABLE_VAR_ILAST
]];
367 if(isset($_GET[$this->request
[TABLE_VAR_IFIRST
]])) {
368 if(empty($_GET[$this->request
[TABLE_VAR_IFIRST
]]) ||
is_numeric(strpos(get_string('alphabet'), $_GET[$this->request
[TABLE_VAR_IFIRST
]]))) {
369 $this->sess
->i_first
= $_GET[$this->request
[TABLE_VAR_IFIRST
]];
373 if(empty($this->baseurl
)) {
375 unset($getcopy[$this->request
[TABLE_VAR_SHOW
]]);
376 unset($getcopy[$this->request
[TABLE_VAR_HIDE
]]);
377 unset($getcopy[$this->request
[TABLE_VAR_SORT
]]);
378 unset($getcopy[$this->request
[TABLE_VAR_IFIRST
]]);
379 unset($getcopy[$this->request
[TABLE_VAR_ILAST
]]);
380 unset($getcopy[$this->request
[TABLE_VAR_PAGE
]]);
382 $strippedurl = strip_querystring(qualified_me());
384 if(!empty($getcopy)) {
387 foreach($getcopy as $var => $val) {
390 $querystring .= '?'.$var.'='.$val;
393 $querystring .= '&'.$var.'='.$val;
396 $this->reseturl
= $strippedurl.$querystring;
397 $querystring .= '&';
400 $this->reseturl
= $strippedurl;
404 $this->baseurl
= strip_querystring(qualified_me()) . $querystring;
407 // If it's "the first time" we 've been here, forget the previous initials filters
408 if(qualified_me() == $this->reseturl
) {
409 $this->sess
->i_first
= '';
410 $this->sess
->i_last
= '';
413 $this->currpage
= optional_param($this->request
[TABLE_VAR_PAGE
], 0);
416 /// Always introduce the "flexible" class for the table if not specified
417 /// No attributes, add flexible class
418 if (empty($this->attributes
)) {
419 $this->attributes
['class'] = 'flexible';
420 /// No classes, add flexible class
421 } else if (!isset($this->attributes
['class'])) {
422 $this->attributes
['class'] = 'flexible';
423 /// No flexible class in passed classes, add flexible class
424 } else if (!in_array('flexible', explode(' ', $this->attributes
['class']))) {
425 $this->attributes
['class'] = trim('flexible ' . $this->attributes
['class']);
434 function get_sql_sort($uniqueid = NULL) {
435 if($uniqueid === NULL) {
436 // "Non-static" function call
440 $sess = &$this->sess
;
443 // "Static" function call
445 if(empty($SESSION->flextable
[$uniqueid])) {
448 $sess = &$SESSION->flextable
[$uniqueid];
451 if(!empty($sess->sortby
)) {
453 foreach($sess->sortby
as $column => $order) {
454 if(!empty($sortstring)) {
457 $sortstring .= $column.($order == SORT_ASC ?
' ASC' : ' DESC');
468 function get_page_start() {
469 if(!$this->use_pages
) {
472 return $this->currpage
* $this->pagesize
;
479 function get_page_size() {
480 if(!$this->use_pages
) {
483 return $this->pagesize
;
490 function get_sql_where() {
491 if(!isset($this->columns
['fullname'])) {
496 if(!empty($this->sess
->i_first
) && !empty($this->sess
->i_last
)) {
497 return 'firstname '.$LIKE.' \''.$this->sess
->i_first
.'%\' AND lastname '.$LIKE.' \''.$this->sess
->i_last
.'%\'';
499 else if(!empty($this->sess
->i_first
)) {
500 return 'firstname '.$LIKE.' \''.$this->sess
->i_first
.'%\'';
502 else if(!empty($this->sess
->i_last
)) {
503 return 'lastname '.$LIKE.' \''.$this->sess
->i_last
.'%\'';
513 function get_initial_first() {
514 if(!$this->use_initials
) {
518 return $this->sess
->i_first
;
525 function get_initial_last() {
526 if(!$this->use_initials
) {
530 return $this->sess
->i_last
;
537 function print_html() {
544 $colcount = count($this->columns
);
546 // Do we need to print initial bars?
548 if($this->use_initials
&& isset($this->columns
['fullname'])) {
550 $strall = get_string('all');
551 $alpha = explode(',', get_string('alphabet'));
553 // Bar of first initials
555 echo '<div class="initialbar firstinitial">'.get_string('firstname').' : ';
556 if(!empty($this->sess
->i_first
)) {
557 echo '<a href="'.$this->baseurl
.$this->request
[TABLE_VAR_IFIRST
].'=">'.$strall.'</a>';
559 echo '<strong>'.$strall.'</strong>';
561 foreach ($alpha as $letter) {
562 if (isset($this->sess
->i_first
) && $letter == $this->sess
->i_first
) {
563 echo ' <strong>'.$letter.'</strong>';
565 echo ' <a href="'.$this->baseurl
.$this->request
[TABLE_VAR_IFIRST
].'='.$letter.'">'.$letter.'</a>';
570 // Bar of last initials
572 echo '<div class="initialbar lastinitial">'.get_string('lastname').' : ';
573 if(!empty($this->sess
->i_last
)) {
574 echo '<a href="'.$this->baseurl
.$this->request
[TABLE_VAR_ILAST
].'=">'.$strall.'</a>';
576 echo '<strong>'.$strall.'</strong>';
578 foreach ($alpha as $letter) {
579 if (isset($this->sess
->i_last
) && $letter == $this->sess
->i_last
) {
580 echo ' <strong>'.$letter.'</strong>';
582 echo ' <a href="'.$this->baseurl
.$this->request
[TABLE_VAR_ILAST
].'='.$letter.'">'.$letter.'</a>';
589 // End of initial bars code
592 if($this->use_pages
) {
593 print_paging_bar($this->totalrows
, $this->currpage
, $this->pagesize
, $this->baseurl
, $this->request
[TABLE_VAR_PAGE
]);
596 if (empty($this->data
)) {
597 print_heading(get_string('nothingtodisplay'));
602 $suppress_enabled = array_sum($this->column_suppress
);
603 $suppress_lastrow = NULL;
604 // Start of main data table
606 echo '<table'.$this->make_attributes_string($this->attributes
).'>';
609 foreach($this->columns
as $column => $index) {
613 if($this->is_collapsible
) {
614 if(!empty($this->sess
->collapse
[$column])) {
615 // some headers contain < br/> tags, do not include in title
616 $icon_hide = ' <a href="'.$this->baseurl
.$this->request
[TABLE_VAR_SHOW
].'='.$column.'"><img src="'.$CFG->pixpath
.'/t/switch_plus.gif" title="'.get_string('show').' '.strip_tags($this->headers
[$index]).'" alt="'.get_string('show').'" /></a>';
618 else if($this->headers
[$index] !== NULL) {
619 // some headers contain < br/> tags, do not include in title
620 $icon_hide = ' <a href="'.$this->baseurl
.$this->request
[TABLE_VAR_HIDE
].'='.$column.'"><img src="'.$CFG->pixpath
.'/t/switch_minus.gif" title="'.get_string('hide').' '.strip_tags($this->headers
[$index]).'" alt="'.get_string('hide').'" /></a>';
624 $primary_sort_column = '';
625 $primary_sort_order = '';
626 if(reset($this->sess
->sortby
)) {
627 $primary_sort_column = key($this->sess
->sortby
);
628 $primary_sort_order = current($this->sess
->sortby
);
634 if($this->is_sortable($column)) {
635 $icon_sort_first = $icon_sort_last = '';
636 if($primary_sort_column == 'firstname') {
637 $lsortorder = get_string('asc');
638 if($primary_sort_order == SORT_ASC
) {
639 $icon_sort_first = ' <img src="'.$CFG->pixpath
.'/t/down.gif" alt="'.get_string('asc').'" />';
640 $fsortorder = get_string('asc');
643 $icon_sort_first = ' <img src="'.$CFG->pixpath
.'/t/up.gif" alt="'.get_string('desc').'" />';
644 $fsortorder = get_string('desc');
647 else if($primary_sort_column == 'lastname') {
648 $fsortorder = get_string('asc');
649 if($primary_sort_order == SORT_ASC
) {
650 $icon_sort_last = ' <img src="'.$CFG->pixpath
.'/t/down.gif" alt="'.get_string('asc').'" />';
651 $lsortorder = get_string('asc');
654 $icon_sort_last = ' <img src="'.$CFG->pixpath
.'/t/up.gif" alt="'.get_string('desc').'" />';
655 $lsortorder = get_string('desc');
658 $fsortorder = get_string('asc');
659 $lsortorder = get_string('asc');
661 $this->headers
[$index] = '<a href="'.$this->baseurl
.$this->request
[TABLE_VAR_SORT
].'=firstname">'.get_string('firstname').get_accesshide(get_string('sortby').' '.get_string('firstname').' '.$fsortorder).'</a> '.$icon_sort_first.' / '.
662 '<a href="'.$this->baseurl
.$this->request
[TABLE_VAR_SORT
].'=lastname">'.get_string('lastname').get_accesshide(get_string('sortby').' '.get_string('lastname').' '.$lsortorder).'</a> '.$icon_sort_last;
667 // do nothing, do not display sortable links
671 if($this->is_sortable($column)) {
672 if($primary_sort_column == $column) {
673 if($primary_sort_order == SORT_ASC
) {
674 $icon_sort = ' <img src="'.$CFG->pixpath
.'/t/down.gif" alt="'.get_string('asc').'" />';
675 $localsortorder = get_string('asc');
678 $icon_sort = ' <img src="'.$CFG->pixpath
.'/t/up.gif" alt="'.get_string('desc').'" />';
679 $localsortorder = get_string('desc');
682 $localsortorder = get_string('asc');
684 $this->headers
[$index] = '<a href="'.$this->baseurl
.$this->request
[TABLE_VAR_SORT
].'='.$column.'">'.$this->headers
[$index].get_accesshide(get_string('sortby').' '.$this->headers
[$index].' '.$localsortorder).'</a>';
688 if($this->headers
[$index] === NULL) {
689 echo '<th class="header c'.$index.$this->column_class
[$column].'" scope="col"> </th>';
691 else if(!empty($this->sess
->collapse
[$column])) {
692 echo '<th class="header c'.$index.$this->column_class
[$column].'" scope="col">'.$icon_hide.'</th>';
695 // took out nowrap for accessibility, might need replacement
696 if (!is_array($this->column_style
[$column])) {
697 // $usestyles = array('white-space:nowrap');
700 // $usestyles = $this->column_style[$column]+array('white-space'=>'nowrap');
701 $usestyles = $this->column_style
[$column];
703 echo '<th class="header c'.$index.$this->column_class
[$column].'" '.$this->make_styles_string($usestyles).' scope="col">'.$this->headers
[$index].$icon_sort.'<div class="commands">'.$icon_hide.'</div></th>';
709 if(!empty($this->data
)) {
711 $colbyindex = array_flip($this->columns
);
712 foreach($this->data
as $row) {
713 $oddeven = $oddeven ?
0 : 1;
714 echo '<tr class="r'.$oddeven.'">';
716 // If we have a separator, print it
717 if($row === NULL && $colcount) {
718 echo '<td colspan="'.$colcount.'"><div class="tabledivider"></div></td>';
721 foreach($row as $index => $data) {
722 if($index >= $colcount) {
725 $column = $colbyindex[$index];
726 echo '<td class="cell c'.$index.$this->column_class
[$column].'"'.$this->make_styles_string($this->column_style
[$column]).'>';
727 if(empty($this->sess
->collapse
[$column])) {
728 if($this->column_suppress
[$column] && $suppress_lastrow !== NULL && $suppress_lastrow[$index] === $data) {
742 if($suppress_enabled) {
743 $suppress_lastrow = $row;
751 if($this->use_pages
) {
752 print_paging_bar($this->totalrows
, $this->currpage
, $this->pagesize
, $this->baseurl
, $this->request
[TABLE_VAR_PAGE
]);
760 function add_data($row) {
764 $this->data
[] = $row;
771 function add_separator() {
775 $this->data
[] = NULL;
779 * Add a row of data to the table. This function takes an array with
780 * column names as keys.
781 * It ignores any elements with keys that are not defined as columns. It
782 * puts in empty strings into the row when there is no element in the passed
783 * array corresponding to a column in the table. It puts the row elements in
785 * @param $rowwithkeys array
788 function add_data_keyed($rowwithkeys){
789 foreach (array_keys($this->columns
) as $column){
790 if (isset($rowwithkeys[$column])){
791 $row [] = $rowwithkeys[$column];
796 $this->add_data($row);