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 * Sets the given $column index to true in $this->column_suppress.
157 * @param integer $column
160 function column_suppress($column) {
161 if(isset($this->column_suppress
[$column])) {
162 $this->column_suppress
[$column] = true;
167 * Sets the given $column index to the given $classname in $this->column_class.
168 * @param integer $column
169 * @param string $classname
172 function column_class($column, $classname) {
173 if(isset($this->column_class
[$column])) {
174 $this->column_class
[$column] = ' '.$classname; // This space needed so that classnames don't run together in the HTML
179 * Sets the given $column index and $property index to the given $value in $this->column_style.
180 * @param integer $column
181 * @param string $property
182 * @param mixed $value
185 function column_style($column, $property, $value) {
186 if(isset($this->column_style
[$column])) {
187 $this->column_style
[$column][$property] = $value;
192 * Sets all columns of the given $property to the given $value in $this->column_style.
193 * @param integer $property
194 * @param string $value
197 function column_style_all($property, $value) {
198 foreach(array_keys($this->columns
) as $column) {
199 $this->column_style
[$column][$property] = $value;
204 * Sets $this->reseturl to the given $url, and $this->baseurl to the given $url plus ? or &
208 function define_baseurl($url) {
209 $this->reseturl
= $url;
210 if(!strpos($url, '?')) {
211 $this->baseurl
= $url.'?';
214 $this->baseurl
= $url.'&';
222 function define_columns($columns) {
223 $this->columns
= array();
224 $this->column_style
= array();
225 $this->column_class
= array();
228 foreach($columns as $column) {
229 $this->columns
[$column] = $colnum++
;
230 $this->column_style
[$column] = array();
231 $this->column_class
[$column] = '';
232 $this->column_suppress
[$column] = false;
240 function define_headers($headers) {
241 $this->headers
= $headers;
248 function make_styles_string(&$styles) {
253 $string = ' style="';
254 foreach($styles as $property => $value) {
255 $string .= $property.':'.$value.';';
265 function make_attributes_string(&$attributes) {
266 if(empty($attributes)) {
271 foreach($attributes as $attr => $value) {
272 $string .= ($attr.'="'.$value.'" ');
283 global $SESSION, $CFG;
285 if(empty($this->columns
) ||
empty($this->uniqueid
)) {
289 if (!isset($SESSION->flextable
)) {
290 $SESSION->flextable
= array();
293 if(!isset($SESSION->flextable
[$this->uniqueid
])) {
294 $SESSION->flextable
[$this->uniqueid
] = new stdClass
;
295 $SESSION->flextable
[$this->uniqueid
]->uniqueid
= $this->uniqueid
;
296 $SESSION->flextable
[$this->uniqueid
]->collapse
= array();
297 $SESSION->flextable
[$this->uniqueid
]->sortby
= array();
298 $SESSION->flextable
[$this->uniqueid
]->i_first
= '';
299 $SESSION->flextable
[$this->uniqueid
]->i_last
= '';
302 $this->sess
= &$SESSION->flextable
[$this->uniqueid
];
304 if(!empty($_GET[$this->request
[TABLE_VAR_SHOW
]]) && isset($this->columns
[$_GET[$this->request
[TABLE_VAR_SHOW
]]])) {
306 $this->sess
->collapse
[$_GET[$this->request
[TABLE_VAR_SHOW
]]] = false;
308 else if(!empty($_GET[$this->request
[TABLE_VAR_HIDE
]]) && isset($this->columns
[$_GET[$this->request
[TABLE_VAR_HIDE
]]])) {
310 $this->sess
->collapse
[$_GET[$this->request
[TABLE_VAR_HIDE
]]] = true;
311 if(array_key_exists($_GET[$this->request
[TABLE_VAR_HIDE
]], $this->sess
->sortby
)) {
312 unset($this->sess
->sortby
[$_GET[$this->request
[TABLE_VAR_HIDE
]]]);
316 // Now, update the column attributes for collapsed columns
317 foreach(array_keys($this->columns
) as $column) {
318 if(!empty($this->sess
->collapse
[$column])) {
319 $this->column_style
[$column]['width'] = '10px';
324 !empty($_GET[$this->request
[TABLE_VAR_SORT
]]) && $this->is_sortable($_GET[$this->request
[TABLE_VAR_SORT
]]) &&
325 (isset($this->columns
[$_GET[$this->request
[TABLE_VAR_SORT
]]]) ||
326 (($_GET[$this->request
[TABLE_VAR_SORT
]] == 'firstname' ||
$_GET[$this->request
[TABLE_VAR_SORT
]] == 'lastname') && isset($this->columns
['fullname']))
329 if(empty($this->sess
->collapse
[$_GET[$this->request
[TABLE_VAR_SORT
]]])) {
330 if(array_key_exists($_GET[$this->request
[TABLE_VAR_SORT
]], $this->sess
->sortby
)) {
331 // This key already exists somewhere. Change its sortorder and bring it to the top.
332 $sortorder = $this->sess
->sortby
[$_GET[$this->request
[TABLE_VAR_SORT
]]] == SORT_ASC ? SORT_DESC
: SORT_ASC
;
333 unset($this->sess
->sortby
[$_GET[$this->request
[TABLE_VAR_SORT
]]]);
334 $this->sess
->sortby
= array_merge(array($_GET[$this->request
[TABLE_VAR_SORT
]] => $sortorder), $this->sess
->sortby
);
337 // Key doesn't exist, so just add it to the beginning of the array, ascending order
338 $this->sess
->sortby
= array_merge(array($_GET[$this->request
[TABLE_VAR_SORT
]] => SORT_ASC
), $this->sess
->sortby
);
340 // Finally, make sure that no more than $this->maxsortkeys are present into the array
341 if(!empty($this->maxsortkeys
) && ($sortkeys = count($this->sess
->sortby
)) > $this->maxsortkeys
) {
342 while($sortkeys-- > $this->maxsortkeys
) {
343 array_pop($this->sess
->sortby
);
349 // If we didn't sort just now, then use the default sort order if one is defined and the column exists
350 if(empty($this->sess
->sortby
) && !empty($this->sort_default_column
) && (isset($this->columns
[$this->sort_default_column
])
351 ||
(in_array('fullname',$this->columns
)
352 && in_array($this->sort_default_column
,
353 array('firstname','lastname'))))) {
354 $this->sess
->sortby
= array ($this->sort_default_column
=> ($this->sort_default_order
== SORT_DESC ? SORT_DESC
: SORT_ASC
));
357 if(isset($_GET[$this->request
[TABLE_VAR_ILAST
]])) {
358 if(empty($_GET[$this->request
[TABLE_VAR_ILAST
]]) ||
is_numeric(strpos(get_string('alphabet'), $_GET[$this->request
[TABLE_VAR_ILAST
]]))) {
359 $this->sess
->i_last
= $_GET[$this->request
[TABLE_VAR_ILAST
]];
363 if(isset($_GET[$this->request
[TABLE_VAR_IFIRST
]])) {
364 if(empty($_GET[$this->request
[TABLE_VAR_IFIRST
]]) ||
is_numeric(strpos(get_string('alphabet'), $_GET[$this->request
[TABLE_VAR_IFIRST
]]))) {
365 $this->sess
->i_first
= $_GET[$this->request
[TABLE_VAR_IFIRST
]];
369 if(empty($this->baseurl
)) {
371 unset($getcopy[$this->request
[TABLE_VAR_SHOW
]]);
372 unset($getcopy[$this->request
[TABLE_VAR_HIDE
]]);
373 unset($getcopy[$this->request
[TABLE_VAR_SORT
]]);
374 unset($getcopy[$this->request
[TABLE_VAR_IFIRST
]]);
375 unset($getcopy[$this->request
[TABLE_VAR_ILAST
]]);
376 unset($getcopy[$this->request
[TABLE_VAR_PAGE
]]);
378 $strippedurl = strip_querystring(qualified_me());
380 if(!empty($getcopy)) {
383 foreach($getcopy as $var => $val) {
386 $querystring .= '?'.$var.'='.$val;
389 $querystring .= '&'.$var.'='.$val;
392 $this->reseturl
= $strippedurl.$querystring;
393 $querystring .= '&';
396 $this->reseturl
= $strippedurl.$querystring;
400 $this->baseurl
= strip_querystring(qualified_me()) . $querystring;
403 // If it's "the first time" we 've been here, forget the previous initials filters
404 if(qualified_me() == $this->reseturl
) {
405 $this->sess
->i_first
= '';
406 $this->sess
->i_last
= '';
409 $this->currpage
= optional_param($this->request
[TABLE_VAR_PAGE
], 0);
412 /// Always introduce the "flexible" class for the table if not specified
413 /// No attributes, add flexible class
414 if (empty($this->attributes
)) {
415 $this->attributes
['class'] = 'flexible';
416 /// No classes, add flexible class
417 } else if (!isset($this->attributes
['class'])) {
418 $this->attributes
['class'] = 'flexible';
419 /// No flexible class in passed classes, add flexible class
420 } else if (!in_array('flexible', explode(' ', $this->attributes
['class']))) {
421 $this->attributes
['class'] = trim('flexible ' . $this->attributes
['class']);
430 function get_sql_sort($uniqueid = NULL) {
431 if($uniqueid === NULL) {
432 // "Non-static" function call
436 $sess = &$this->sess
;
439 // "Static" function call
441 if(empty($SESSION->flextable
[$uniqueid])) {
444 $sess = &$SESSION->flextable
[$uniqueid];
447 if(!empty($sess->sortby
)) {
449 foreach($sess->sortby
as $column => $order) {
450 if(!empty($sortstring)) {
453 $sortstring .= $column.($order == SORT_ASC ?
' ASC' : ' DESC');
464 function get_page_start() {
465 if(!$this->use_pages
) {
468 return $this->currpage
* $this->pagesize
;
475 function get_page_size() {
476 if(!$this->use_pages
) {
479 return $this->pagesize
;
486 function get_sql_where() {
487 if(!isset($this->columns
['fullname'])) {
492 if(!empty($this->sess
->i_first
) && !empty($this->sess
->i_last
)) {
493 return 'firstname '.$LIKE.' \''.$this->sess
->i_first
.'%\' AND lastname '.$LIKE.' \''.$this->sess
->i_last
.'%\'';
495 else if(!empty($this->sess
->i_first
)) {
496 return 'firstname '.$LIKE.' \''.$this->sess
->i_first
.'%\'';
498 else if(!empty($this->sess
->i_last
)) {
499 return 'lastname '.$LIKE.' \''.$this->sess
->i_last
.'%\'';
509 function get_initial_first() {
510 if(!$this->use_initials
) {
514 return $this->sess
->i_first
;
521 function get_initial_last() {
522 if(!$this->use_initials
) {
526 return $this->sess
->i_last
;
533 function print_html() {
540 $colcount = count($this->columns
);
542 // Do we need to print initial bars?
544 if($this->use_initials
&& isset($this->columns
['fullname'])) {
546 $strall = get_string('all');
547 $alpha = explode(',', get_string('alphabet'));
549 // Bar of first initials
551 echo '<div class="initialbar firstinitial">'.get_string('firstname').' : ';
552 if(!empty($this->sess
->i_first
)) {
553 echo '<a href="'.$this->baseurl
.$this->request
[TABLE_VAR_IFIRST
].'=">'.$strall.'</a>';
555 echo '<strong>'.$strall.'</strong>';
557 foreach ($alpha as $letter) {
558 if ($letter == $this->sess
->i_first
) {
559 echo ' <strong>'.$letter.'</strong>';
561 echo ' <a href="'.$this->baseurl
.$this->request
[TABLE_VAR_IFIRST
].'='.$letter.'">'.$letter.'</a>';
566 // Bar of last initials
568 echo '<div class="initialbar lastinitial">'.get_string('lastname').' : ';
569 if(!empty($this->sess
->i_last
)) {
570 echo '<a href="'.$this->baseurl
.$this->request
[TABLE_VAR_ILAST
].'=">'.$strall.'</a>';
572 echo '<strong>'.$strall.'</strong>';
574 foreach ($alpha as $letter) {
575 if ($letter == $this->sess
->i_last
) {
576 echo ' <strong>'.$letter.'</strong>';
578 echo ' <a href="'.$this->baseurl
.$this->request
[TABLE_VAR_ILAST
].'='.$letter.'">'.$letter.'</a>';
585 // End of initial bars code
588 if($this->use_pages
) {
589 print_paging_bar($this->totalrows
, $this->currpage
, $this->pagesize
, $this->baseurl
, $this->request
[TABLE_VAR_PAGE
]);
592 if (empty($this->data
)) {
593 print_heading(get_string('nothingtodisplay'));
598 $suppress_enabled = array_sum($this->column_suppress
);
599 $suppress_lastrow = NULL;
600 // Start of main data table
602 echo '<table'.$this->make_attributes_string($this->attributes
).'>';
605 foreach($this->columns
as $column => $index) {
609 if($this->is_collapsible
) {
610 if(!empty($this->sess
->collapse
[$column])) {
611 // some headers contain < br/> tags, do not include in title
612 $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>';
614 else if($this->headers
[$index] !== NULL) {
615 // some headers contain < br/> tags, do not include in title
616 $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>';
620 $primary_sort_column = '';
621 $primary_sort_order = '';
622 if(reset($this->sess
->sortby
)) {
623 $primary_sort_column = key($this->sess
->sortby
);
624 $primary_sort_order = current($this->sess
->sortby
);
630 if($this->is_sortable($column)) {
631 $icon_sort_first = $icon_sort_last = '';
632 if($primary_sort_column == 'firstname') {
633 $lsortorder = get_string('asc');
634 if($primary_sort_order == SORT_ASC
) {
635 $icon_sort_first = ' <img src="'.$CFG->pixpath
.'/t/down.gif" alt="'.get_string('asc').'" />';
636 $fsortorder = get_string('asc');
639 $icon_sort_first = ' <img src="'.$CFG->pixpath
.'/t/up.gif" alt="'.get_string('desc').'" />';
640 $fsortorder = get_string('desc');
643 else if($primary_sort_column == 'lastname') {
644 $fsortorder = get_string('asc');
645 if($primary_sort_order == SORT_ASC
) {
646 $icon_sort_last = ' <img src="'.$CFG->pixpath
.'/t/down.gif" alt="'.get_string('asc').'" />';
647 $lsortorder = get_string('asc');
650 $icon_sort_last = ' <img src="'.$CFG->pixpath
.'/t/up.gif" alt="'.get_string('desc').'" />';
651 $lsortorder = get_string('desc');
654 $fsortorder = get_string('asc');
655 $lsortorder = get_string('asc');
657 $this->headers
[$index] = '<a href="'.$this->baseurl
.$this->request
[TABLE_VAR_SORT
].'=firstname">'.get_string('firstname').'<span class="accesshide">'.get_string('sortby').' '.get_string('firstname').' '.$fsortorder.'</span></a> '.$icon_sort_first.' / '.
658 '<a href="'.$this->baseurl
.$this->request
[TABLE_VAR_SORT
].'=lastname">'.get_string('lastname').'<span class="accesshide">'.get_string('sortby').' '.get_string('lastname').' '.$lsortorder.'</span></a> '.$icon_sort_last;
663 // do nothing, do not display sortable links
667 if($this->is_sortable($column)) {
668 if($primary_sort_column == $column) {
669 if($primary_sort_order == SORT_ASC
) {
670 $icon_sort = ' <img src="'.$CFG->pixpath
.'/t/down.gif" alt="'.get_string('asc').'" />';
671 $localsortorder = get_string('asc');
674 $icon_sort = ' <img src="'.$CFG->pixpath
.'/t/up.gif" alt="'.get_string('desc').'" />';
675 $localsortorder = get_string('desc');
678 $localsortorder = get_string('asc');
680 $this->headers
[$index] = '<a href="'.$this->baseurl
.$this->request
[TABLE_VAR_SORT
].'='.$column.'">'.$this->headers
[$index].'<span class="accesshide">'.get_string('sortby').' '.$this->headers
[$index].' '.$localsortorder.'</span></a>';
684 if($this->headers
[$index] === NULL) {
685 echo '<th class="header c'.$index.$this->column_class
[$column].'" scope="col"> </th>';
687 else if(!empty($this->sess
->collapse
[$column])) {
688 echo '<th class="header c'.$index.$this->column_class
[$column].'" scope="col">'.$icon_hide.'</th>';
691 // took out nowrap for accessibility, might need replacement
692 if (!is_array($this->column_style
[$column])) {
693 // $usestyles = array('white-space:nowrap');
696 // $usestyles = $this->column_style[$column]+array('white-space'=>'nowrap');
697 $usestyles = $this->column_style
[$column];
699 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>';
705 if(!empty($this->data
)) {
707 $colbyindex = array_flip($this->columns
);
708 foreach($this->data
as $row) {
709 $oddeven = $oddeven ?
0 : 1;
710 echo '<tr class="r'.$oddeven.'">';
712 // If we have a separator, print it
713 if($row === NULL && $colcount) {
714 echo '<td colspan="'.$colcount.'"><div class="tabledivider"></div></td>';
717 foreach($row as $index => $data) {
718 if($index >= $colcount) {
721 $column = $colbyindex[$index];
722 echo '<td class="cell c'.$index.$this->column_class
[$column].'"'.$this->make_styles_string($this->column_style
[$column]).'>';
723 if(empty($this->sess
->collapse
[$column])) {
724 if($this->column_suppress
[$column] && $suppress_lastrow !== NULL && $suppress_lastrow[$index] === $data) {
738 if($suppress_enabled) {
739 $suppress_lastrow = $row;
747 if($this->use_pages
) {
748 print_paging_bar($this->totalrows
, $this->currpage
, $this->pagesize
, $this->baseurl
, $this->request
[TABLE_VAR_PAGE
]);
756 function add_data($row) {
760 $this->data
[] = $row;
767 function add_separator() {
771 $this->data
[] = NULL;