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();
22 var $request = array();
24 var $is_collapsible = false;
25 var $is_sortable = false;
26 var $use_pages = false;
27 var $use_initials = false;
33 var $sort_default_column = NULL;
34 var $sort_default_order = SORT_ASC
;
36 function flexible_table($uniqueid) {
37 $this->uniqueid
= $uniqueid;
38 $this->request
= array(
39 TABLE_VAR_SORT
=> 'tsort',
40 TABLE_VAR_HIDE
=> 'thide',
41 TABLE_VAR_SHOW
=> 'tshow',
42 TABLE_VAR_IFIRST
=> 'tifirst',
43 TABLE_VAR_ILAST
=> 'tilast',
44 TABLE_VAR_PAGE
=> 'page'
48 function sortable($bool, $defaultcolumn = NULL, $defaultorder = SORT_ASC
) {
49 $this->is_sortable
= $bool;
50 $this->sort_default_column
= $defaultcolumn;
51 $this->sort_default_order
= $defaultorder;
54 function collapsible($bool) {
55 $this->is_collapsible
= $bool;
58 function pageable($bool) {
59 $this->use_pages
= $bool;
62 function initialbars($bool) {
63 $this->use_initials
= $bool;
66 function pagesize($perpage, $total) {
67 $this->pagesize
= $perpage;
68 $this->totalrows
= $total;
69 $this->use_pages
= true;
72 function set_control_variables($variables) {
73 foreach($variables as $what => $variable) {
74 if(isset($this->request
[$what])) {
75 $this->request
[$what] = $variable;
80 function set_attribute($attribute, $value) {
81 $this->attributes
[$attribute] = $value;
84 function column_suppress($column) {
85 if(isset($this->column_suppress
[$column])) {
86 $this->column_suppress
[$column] = true;
90 function column_class($column, $classname) {
91 if(isset($this->column_class
[$column])) {
92 $this->column_class
[$column] = ' '.$classname; // This space needed so that classnames don't run together in the HTML
96 function column_style($column, $property, $value) {
97 if(isset($this->column_style
[$column])) {
98 $this->column_style
[$column][$property] = $value;
102 function column_style_all($property, $value) {
103 foreach(array_keys($this->columns
) as $column) {
104 $this->column_style
[$column][$property] = $value;
108 function define_baseurl($url) {
109 $this->reseturl
= $url;
110 if(!strpos($url, '?')) {
111 $this->baseurl
= $url.'?';
114 $this->baseurl
= $url.'&';
118 function define_columns($columns) {
119 $this->columns
= array();
120 $this->column_style
= array();
121 $this->column_class
= array();
124 foreach($columns as $column) {
125 $this->columns
[$column] = $colnum++
;
126 $this->column_style
[$column] = array();
127 $this->column_class
[$column] = '';
128 $this->column_suppress
[$column] = false;
132 function define_headers($headers) {
133 $this->headers
= $headers;
136 function make_styles_string(&$styles) {
141 $string = ' style="';
142 foreach($styles as $property => $value) {
143 $string .= $property.':'.$value.';';
149 function make_attributes_string(&$attributes) {
150 if(empty($attributes)) {
155 foreach($attributes as $attr => $value) {
156 $string .= ($attr.'="'.$value.'" ');
163 global $SESSION, $CFG;
165 if(empty($this->columns
) ||
empty($this->uniqueid
)) {
169 if (!isset($SESSION->flextable
)) {
170 $SESSION->flextable
= array();
173 if(!isset($SESSION->flextable
[$this->uniqueid
])) {
174 $SESSION->flextable
[$this->uniqueid
] = new stdClass
;
175 $SESSION->flextable
[$this->uniqueid
]->uniqueid
= $this->uniqueid
;
176 $SESSION->flextable
[$this->uniqueid
]->collapse
= array();
177 $SESSION->flextable
[$this->uniqueid
]->sortby
= array();
178 $SESSION->flextable
[$this->uniqueid
]->i_first
= '';
179 $SESSION->flextable
[$this->uniqueid
]->i_last
= '';
182 $this->sess
= &$SESSION->flextable
[$this->uniqueid
];
184 if(!empty($_GET[$this->request
[TABLE_VAR_SHOW
]]) && isset($this->columns
[$_GET[$this->request
[TABLE_VAR_SHOW
]]])) {
186 $this->sess
->collapse
[$_GET[$this->request
[TABLE_VAR_SHOW
]]] = false;
188 else if(!empty($_GET[$this->request
[TABLE_VAR_HIDE
]]) && isset($this->columns
[$_GET[$this->request
[TABLE_VAR_HIDE
]]])) {
190 $this->sess
->collapse
[$_GET[$this->request
[TABLE_VAR_HIDE
]]] = true;
191 if(array_key_exists($_GET[$this->request
[TABLE_VAR_HIDE
]], $this->sess
->sortby
)) {
192 unset($this->sess
->sortby
[$_GET[$this->request
[TABLE_VAR_HIDE
]]]);
196 // Now, update the column attributes for collapsed columns
197 foreach(array_keys($this->columns
) as $column) {
198 if(!empty($this->sess
->collapse
[$column])) {
199 $this->column_style
[$column]['width'] = '10px';
204 !empty($_GET[$this->request
[TABLE_VAR_SORT
]]) &&
205 (isset($this->columns
[$_GET[$this->request
[TABLE_VAR_SORT
]]]) ||
206 (($_GET[$this->request
[TABLE_VAR_SORT
]] == 'firstname' ||
$_GET[$this->request
[TABLE_VAR_SORT
]] == 'lastname') && isset($this->columns
['fullname']))
209 if(empty($this->sess
->collapse
[$_GET[$this->request
[TABLE_VAR_SORT
]]])) {
210 if(array_key_exists($_GET[$this->request
[TABLE_VAR_SORT
]], $this->sess
->sortby
)) {
211 // This key already exists somewhere. Change its sortorder and bring it to the top.
212 $sortorder = $this->sess
->sortby
[$_GET[$this->request
[TABLE_VAR_SORT
]]] == SORT_ASC ? SORT_DESC
: SORT_ASC
;
213 unset($this->sess
->sortby
[$_GET[$this->request
[TABLE_VAR_SORT
]]]);
214 $this->sess
->sortby
= array_merge(array($_GET[$this->request
[TABLE_VAR_SORT
]] => $sortorder), $this->sess
->sortby
);
217 // Key doesn't exist, so just add it to the beginning of the array, ascending order
218 $this->sess
->sortby
= array_merge(array($_GET[$this->request
[TABLE_VAR_SORT
]] => SORT_ASC
), $this->sess
->sortby
);
220 // Finally, make sure that no more than $this->maxsortkeys are present into the array
221 if(!empty($this->maxsortkeys
) && ($sortkeys = count($this->sess
->sortby
)) > $this->maxsortkeys
) {
222 while($sortkeys-- > $this->maxsortkeys
) {
223 array_pop($this->sess
->sortby
);
229 // If we didn't sort just now, then use the default sort order if one is defined and the column exists
230 if(empty($this->sess
->sortby
) && !empty($this->sort_default_column
) && (isset($this->columns
[$this->sort_default_column
])
231 ||
(in_array('fullname',$this->columns
)
232 && in_array($this->sort_default_column
,
233 array('firstname','lastname'))))) {
234 $this->sess
->sortby
= array ($this->sort_default_column
=> ($this->sort_default_order
== SORT_DESC ? SORT_DESC
: SORT_ASC
));
237 if(isset($_GET[$this->request
[TABLE_VAR_ILAST
]])) {
238 if(empty($_GET[$this->request
[TABLE_VAR_ILAST
]]) ||
is_numeric(strpos(get_string('alphabet'), $_GET[$this->request
[TABLE_VAR_ILAST
]]))) {
239 $this->sess
->i_last
= $_GET[$this->request
[TABLE_VAR_ILAST
]];
243 if(isset($_GET[$this->request
[TABLE_VAR_IFIRST
]])) {
244 if(empty($_GET[$this->request
[TABLE_VAR_IFIRST
]]) ||
is_numeric(strpos(get_string('alphabet'), $_GET[$this->request
[TABLE_VAR_IFIRST
]]))) {
245 $this->sess
->i_first
= $_GET[$this->request
[TABLE_VAR_IFIRST
]];
249 if(empty($this->baseurl
)) {
251 unset($getcopy[$this->request
[TABLE_VAR_SHOW
]]);
252 unset($getcopy[$this->request
[TABLE_VAR_HIDE
]]);
253 unset($getcopy[$this->request
[TABLE_VAR_SORT
]]);
254 unset($getcopy[$this->request
[TABLE_VAR_IFIRST
]]);
255 unset($getcopy[$this->request
[TABLE_VAR_ILAST
]]);
256 unset($getcopy[$this->request
[TABLE_VAR_PAGE
]]);
258 $strippedurl = strip_querystring(qualified_me());
260 if(!empty($getcopy)) {
263 foreach($getcopy as $var => $val) {
266 $querystring .= '?'.$var.'='.$val;
269 $querystring .= '&'.$var.'='.$val;
272 $this->reseturl
= $strippedurl.$querystring;
273 $querystring .= '&';
276 $this->reseturl
= $strippedurl.$querystring;
280 $this->baseurl
= strip_querystring(qualified_me()) . $querystring;
283 // If it's "the first time" we 've been here, forget the previous initials filters
284 if(qualified_me() == $this->reseturl
) {
285 $this->sess
->i_first
= '';
286 $this->sess
->i_last
= '';
289 $this->currpage
= optional_param($this->request
[TABLE_VAR_PAGE
], 0);
292 /// Always introduce the "flexible" class for the table if not specified
293 /// No attributes, add flexible class
294 if (empty($this->attributes
)) {
295 $this->attributes
['class'] = 'flexible';
296 /// No classes, add flexible class
297 } else if (!isset($this->attributes
['class'])) {
298 $this->attributes
['class'] = 'flexible';
299 /// No flexible class in passed classes, add flexible class
300 } else if (!in_array('flexible', explode(' ', $this->attributes
['class']))) {
301 $this->attributes
['class'] = trim('flexible ' . $this->attributes
['class']);
306 function get_sql_sort($uniqueid = NULL) {
307 if($uniqueid === NULL) {
308 // "Non-static" function call
312 $sess = &$this->sess
;
315 // "Static" function call
317 if(empty($SESSION->flextable
[$uniqueid])) {
320 $sess = &$SESSION->flextable
[$uniqueid];
323 if(!empty($sess->sortby
)) {
325 foreach($sess->sortby
as $column => $order) {
326 if(!empty($sortstring)) {
329 $sortstring .= $column.($order == SORT_ASC ?
' ASC' : ' DESC');
336 function get_page_start() {
337 if(!$this->use_pages
) {
340 return $this->currpage
* $this->pagesize
;
343 function get_page_size() {
344 if(!$this->use_pages
) {
347 return $this->pagesize
;
350 function get_sql_where() {
351 if(!isset($this->columns
['fullname'])) {
356 if(!empty($this->sess
->i_first
) && !empty($this->sess
->i_last
)) {
357 return 'firstname '.$LIKE.' \''.$this->sess
->i_first
.'%\' AND lastname '.$LIKE.' \''.$this->sess
->i_last
.'%\'';
359 else if(!empty($this->sess
->i_first
)) {
360 return 'firstname '.$LIKE.' \''.$this->sess
->i_first
.'%\'';
362 else if(!empty($this->sess
->i_last
)) {
363 return 'lastname '.$LIKE.' \''.$this->sess
->i_last
.'%\'';
369 function get_initial_first() {
370 if(!$this->use_initials
) {
374 return $this->sess
->i_first
;
377 function get_initial_last() {
378 if(!$this->use_initials
) {
382 return $this->sess
->i_last
;
385 function print_html() {
392 $colcount = count($this->columns
);
394 // Do we need to print initial bars?
396 if($this->use_initials
&& isset($this->columns
['fullname'])) {
398 $strall = get_string('all');
399 $alpha = explode(',', get_string('alphabet'));
401 // Bar of first initials
403 echo '<div class="initialbar firstinitial">'.get_string('firstname').' : ';
404 if(!empty($this->sess
->i_first
)) {
405 echo '<a href="'.$this->baseurl
.$this->request
[TABLE_VAR_IFIRST
].'=">'.$strall.'</a>';
407 echo '<strong>'.$strall.'</strong>';
409 foreach ($alpha as $letter) {
410 if ($letter == $this->sess
->i_first
) {
411 echo ' <strong>'.$letter.'</strong>';
413 echo ' <a href="'.$this->baseurl
.$this->request
[TABLE_VAR_IFIRST
].'='.$letter.'">'.$letter.'</a>';
418 // Bar of last initials
420 echo '<div class="initialbar lastinitial">'.get_string('lastname').' : ';
421 if(!empty($this->sess
->i_last
)) {
422 echo '<a href="'.$this->baseurl
.$this->request
[TABLE_VAR_ILAST
].'=">'.$strall.'</a>';
424 echo '<strong>'.$strall.'</strong>';
426 foreach ($alpha as $letter) {
427 if ($letter == $this->sess
->i_last
) {
428 echo ' <strong>'.$letter.'</strong>';
430 echo ' <a href="'.$this->baseurl
.$this->request
[TABLE_VAR_ILAST
].'='.$letter.'">'.$letter.'</a>';
437 // End of initial bars code
440 if($this->use_pages
) {
441 print_paging_bar($this->totalrows
, $this->currpage
, $this->pagesize
, $this->baseurl
, $this->request
[TABLE_VAR_PAGE
]);
444 if (empty($this->data
)) {
445 print_heading(get_string('nothingtodisplay'));
450 $suppress_enabled = array_sum($this->column_suppress
);
451 $suppress_lastrow = NULL;
452 // Start of main data table
454 echo '<table'.$this->make_attributes_string($this->attributes
).'>';
457 foreach($this->columns
as $column => $index) {
461 if($this->is_collapsible
) {
462 if(!empty($this->sess
->collapse
[$column])) {
463 // some headers contain < br/> tags, do not include in title
464 $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>';
466 else if($this->headers
[$index] !== NULL) {
467 // some headers contain < br/> tags, do not include in title
468 $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>';
472 $primary_sort_column = '';
473 $primary_sort_order = '';
474 if(reset($this->sess
->sortby
)) {
475 $primary_sort_column = key($this->sess
->sortby
);
476 $primary_sort_order = current($this->sess
->sortby
);
482 if($this->is_sortable
) {
483 $icon_sort_first = $icon_sort_last = '';
484 if($primary_sort_column == 'firstname') {
485 $lsortorder = get_string('asc');
486 if($primary_sort_order == SORT_ASC
) {
487 $icon_sort_first = ' <img src="'.$CFG->pixpath
.'/t/down.gif" alt="'.get_string('asc').'" />';
488 $fsortorder = get_string('asc');
491 $icon_sort_first = ' <img src="'.$CFG->pixpath
.'/t/up.gif" alt="'.get_string('desc').'" />';
492 $fsortorder = get_string('desc');
495 else if($primary_sort_column == 'lastname') {
496 $fsortorder = get_string('asc');
497 if($primary_sort_order == SORT_ASC
) {
498 $icon_sort_last = ' <img src="'.$CFG->pixpath
.'/t/down.gif" alt="'.get_string('asc').'" />';
499 $lsortorder = get_string('asc');
502 $icon_sort_last = ' <img src="'.$CFG->pixpath
.'/t/up.gif" alt="'.get_string('desc').'" />';
503 $lsortorder = get_string('desc');
506 $fsortorder = get_string('asc');
507 $lsortorder = get_string('asc');
509 $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.' / '.
510 '<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;
515 // do nothing, do not display sortable links
519 if($this->is_sortable
) {
520 if($primary_sort_column == $column) {
521 if($primary_sort_order == SORT_ASC
) {
522 $icon_sort = ' <img src="'.$CFG->pixpath
.'/t/down.gif" alt="'.get_string('asc').'" />';
523 $localsortorder = get_string('asc');
526 $icon_sort = ' <img src="'.$CFG->pixpath
.'/t/up.gif" alt="'.get_string('desc').'" />';
527 $localsortorder = get_string('desc');
530 $localsortorder = get_string('asc');
532 $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>';
536 if($this->headers
[$index] === NULL) {
537 echo '<th class="header c'.$index.$this->column_class
[$column].'" scope="col"> </th>';
539 else if(!empty($this->sess
->collapse
[$column])) {
540 echo '<th class="header c'.$index.$this->column_class
[$column].'" scope="col">'.$icon_hide.'</th>';
543 // took out nowrap for accessibility, might need replacement
544 if (!is_array($this->column_style
[$column])) {
545 // $usestyles = array('white-space:nowrap');
548 // $usestyles = $this->column_style[$column]+array('white-space'=>'nowrap');
549 $usestyles = $this->column_style
[$column];
551 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>';
557 if(!empty($this->data
)) {
559 $colbyindex = array_flip($this->columns
);
560 foreach($this->data
as $row) {
561 $oddeven = $oddeven ?
0 : 1;
562 echo '<tr class="r'.$oddeven.'">';
564 // If we have a separator, print it
565 if($row === NULL && $colcount) {
566 echo '<td colspan="'.$colcount.'"><div class="tabledivider"></div></td>';
569 foreach($row as $index => $data) {
570 if($index >= $colcount) {
573 $column = $colbyindex[$index];
574 echo '<td class="cell c'.$index.$this->column_class
[$column].'"'.$this->make_styles_string($this->column_style
[$column]).'>';
575 if(empty($this->sess
->collapse
[$column])) {
576 if($this->column_suppress
[$column] && $suppress_lastrow !== NULL && $suppress_lastrow[$index] === $data) {
590 if($suppress_enabled) {
591 $suppress_lastrow = $row;
599 if($this->use_pages
) {
600 print_paging_bar($this->totalrows
, $this->currpage
, $this->pagesize
, $this->baseurl
, $this->request
[TABLE_VAR_PAGE
]);
604 function add_data($row) {
608 $this->data
[] = $row;
611 function add_separator() {
615 $this->data
[] = NULL;