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
;
38 * @param int $uniqueid
39 * @todo Document properly
41 function flexible_table($uniqueid) {
42 $this->uniqueid
= $uniqueid;
43 $this->request
= array(
44 TABLE_VAR_SORT
=> 'tsort',
45 TABLE_VAR_HIDE
=> 'thide',
46 TABLE_VAR_SHOW
=> 'tshow',
47 TABLE_VAR_IFIRST
=> 'tifirst',
48 TABLE_VAR_ILAST
=> 'tilast',
49 TABLE_VAR_PAGE
=> 'page'
54 * Sets the is_sortable variable to the given boolean, sort_default_column to
55 * the given string, and the sort_default_order to the given integer.
57 * @param string $defaultcolumn
58 * @param int $defaultorder
61 function sortable($bool, $defaultcolumn = NULL, $defaultorder = SORT_ASC
) {
62 $this->is_sortable
= $bool;
63 $this->sort_default_column
= $defaultcolumn;
64 $this->sort_default_order
= $defaultorder;
68 * Sets the is_collapsible variable to the given boolean.
72 function collapsible($bool) {
73 $this->is_collapsible
= $bool;
77 * Sets the use_pages variable to the given boolean.
81 function pageable($bool) {
82 $this->use_pages
= $bool;
86 * Sets the use_initials variable to the given boolean.
90 function initialbars($bool) {
91 $this->use_initials
= $bool;
95 * Sets the pagesize variable to the given integer, the totalrows variable
96 * to the given integer, and the use_pages variable to true.
101 function pagesize($perpage, $total) {
102 $this->pagesize
= $perpage;
103 $this->totalrows
= $total;
104 $this->use_pages
= true;
108 * Assigns each given variable in the array to the corresponding index
109 * in the request class variable.
110 * @param array $variables
113 function set_control_variables($variables) {
114 foreach($variables as $what => $variable) {
115 if(isset($this->request
[$what])) {
116 $this->request
[$what] = $variable;
122 * Gives the given $value to the $attribute index of $this->attributes.
123 * @param string $attribute
124 * @param mixed $value
127 function set_attribute($attribute, $value) {
128 $this->attributes
[$attribute] = $value;
132 * Sets the given $column index to true in $this->column_suppress.
133 * @param integer $column
136 function column_suppress($column) {
137 if(isset($this->column_suppress
[$column])) {
138 $this->column_suppress
[$column] = true;
143 * Sets the given $column index to the given $classname in $this->column_class.
144 * @param integer $column
145 * @param string $classname
148 function column_class($column, $classname) {
149 if(isset($this->column_class
[$column])) {
150 $this->column_class
[$column] = ' '.$classname; // This space needed so that classnames don't run together in the HTML
155 * Sets the given $column index and $property index to the given $value in $this->column_style.
156 * @param integer $column
157 * @param string $property
158 * @param mixed $value
161 function column_style($column, $property, $value) {
162 if(isset($this->column_style
[$column])) {
163 $this->column_style
[$column][$property] = $value;
168 * Sets all columns of the given $property to the given $value in $this->column_style.
169 * @param integer $property
170 * @param string $value
173 function column_style_all($property, $value) {
174 foreach(array_keys($this->columns
) as $column) {
175 $this->column_style
[$column][$property] = $value;
180 * Sets $this->reseturl to the given $url, and $this->baseurl to the given $url plus ? or &
184 function define_baseurl($url) {
185 $this->reseturl
= $url;
186 if(!strpos($url, '?')) {
187 $this->baseurl
= $url.'?';
190 $this->baseurl
= $url.'&';
198 function define_columns($columns) {
199 $this->columns
= array();
200 $this->column_style
= array();
201 $this->column_class
= array();
204 foreach($columns as $column) {
205 $this->columns
[$column] = $colnum++
;
206 $this->column_style
[$column] = array();
207 $this->column_class
[$column] = '';
208 $this->column_suppress
[$column] = false;
216 function define_headers($headers) {
217 $this->headers
= $headers;
224 function make_styles_string(&$styles) {
229 $string = ' style="';
230 foreach($styles as $property => $value) {
231 $string .= $property.':'.$value.';';
241 function make_attributes_string(&$attributes) {
242 if(empty($attributes)) {
247 foreach($attributes as $attr => $value) {
248 $string .= ($attr.'="'.$value.'" ');
259 global $SESSION, $CFG;
261 if(empty($this->columns
) ||
empty($this->uniqueid
)) {
265 if (!isset($SESSION->flextable
)) {
266 $SESSION->flextable
= array();
269 if(!isset($SESSION->flextable
[$this->uniqueid
])) {
270 $SESSION->flextable
[$this->uniqueid
] = new stdClass
;
271 $SESSION->flextable
[$this->uniqueid
]->uniqueid
= $this->uniqueid
;
272 $SESSION->flextable
[$this->uniqueid
]->collapse
= array();
273 $SESSION->flextable
[$this->uniqueid
]->sortby
= array();
274 $SESSION->flextable
[$this->uniqueid
]->i_first
= '';
275 $SESSION->flextable
[$this->uniqueid
]->i_last
= '';
278 $this->sess
= &$SESSION->flextable
[$this->uniqueid
];
280 if(!empty($_GET[$this->request
[TABLE_VAR_SHOW
]]) && isset($this->columns
[$_GET[$this->request
[TABLE_VAR_SHOW
]]])) {
282 $this->sess
->collapse
[$_GET[$this->request
[TABLE_VAR_SHOW
]]] = false;
284 else if(!empty($_GET[$this->request
[TABLE_VAR_HIDE
]]) && isset($this->columns
[$_GET[$this->request
[TABLE_VAR_HIDE
]]])) {
286 $this->sess
->collapse
[$_GET[$this->request
[TABLE_VAR_HIDE
]]] = true;
287 if(array_key_exists($_GET[$this->request
[TABLE_VAR_HIDE
]], $this->sess
->sortby
)) {
288 unset($this->sess
->sortby
[$_GET[$this->request
[TABLE_VAR_HIDE
]]]);
292 // Now, update the column attributes for collapsed columns
293 foreach(array_keys($this->columns
) as $column) {
294 if(!empty($this->sess
->collapse
[$column])) {
295 $this->column_style
[$column]['width'] = '10px';
300 !empty($_GET[$this->request
[TABLE_VAR_SORT
]]) &&
301 (isset($this->columns
[$_GET[$this->request
[TABLE_VAR_SORT
]]]) ||
302 (($_GET[$this->request
[TABLE_VAR_SORT
]] == 'firstname' ||
$_GET[$this->request
[TABLE_VAR_SORT
]] == 'lastname') && isset($this->columns
['fullname']))
305 if(empty($this->sess
->collapse
[$_GET[$this->request
[TABLE_VAR_SORT
]]])) {
306 if(array_key_exists($_GET[$this->request
[TABLE_VAR_SORT
]], $this->sess
->sortby
)) {
307 // This key already exists somewhere. Change its sortorder and bring it to the top.
308 $sortorder = $this->sess
->sortby
[$_GET[$this->request
[TABLE_VAR_SORT
]]] == SORT_ASC ? SORT_DESC
: SORT_ASC
;
309 unset($this->sess
->sortby
[$_GET[$this->request
[TABLE_VAR_SORT
]]]);
310 $this->sess
->sortby
= array_merge(array($_GET[$this->request
[TABLE_VAR_SORT
]] => $sortorder), $this->sess
->sortby
);
313 // Key doesn't exist, so just add it to the beginning of the array, ascending order
314 $this->sess
->sortby
= array_merge(array($_GET[$this->request
[TABLE_VAR_SORT
]] => SORT_ASC
), $this->sess
->sortby
);
316 // Finally, make sure that no more than $this->maxsortkeys are present into the array
317 if(!empty($this->maxsortkeys
) && ($sortkeys = count($this->sess
->sortby
)) > $this->maxsortkeys
) {
318 while($sortkeys-- > $this->maxsortkeys
) {
319 array_pop($this->sess
->sortby
);
325 // If we didn't sort just now, then use the default sort order if one is defined and the column exists
326 if(empty($this->sess
->sortby
) && !empty($this->sort_default_column
) && (isset($this->columns
[$this->sort_default_column
])
327 ||
(in_array('fullname',$this->columns
)
328 && in_array($this->sort_default_column
,
329 array('firstname','lastname'))))) {
330 $this->sess
->sortby
= array ($this->sort_default_column
=> ($this->sort_default_order
== SORT_DESC ? SORT_DESC
: SORT_ASC
));
333 if(isset($_GET[$this->request
[TABLE_VAR_ILAST
]])) {
334 if(empty($_GET[$this->request
[TABLE_VAR_ILAST
]]) ||
is_numeric(strpos(get_string('alphabet'), $_GET[$this->request
[TABLE_VAR_ILAST
]]))) {
335 $this->sess
->i_last
= $_GET[$this->request
[TABLE_VAR_ILAST
]];
339 if(isset($_GET[$this->request
[TABLE_VAR_IFIRST
]])) {
340 if(empty($_GET[$this->request
[TABLE_VAR_IFIRST
]]) ||
is_numeric(strpos(get_string('alphabet'), $_GET[$this->request
[TABLE_VAR_IFIRST
]]))) {
341 $this->sess
->i_first
= $_GET[$this->request
[TABLE_VAR_IFIRST
]];
345 if(empty($this->baseurl
)) {
347 unset($getcopy[$this->request
[TABLE_VAR_SHOW
]]);
348 unset($getcopy[$this->request
[TABLE_VAR_HIDE
]]);
349 unset($getcopy[$this->request
[TABLE_VAR_SORT
]]);
350 unset($getcopy[$this->request
[TABLE_VAR_IFIRST
]]);
351 unset($getcopy[$this->request
[TABLE_VAR_ILAST
]]);
352 unset($getcopy[$this->request
[TABLE_VAR_PAGE
]]);
354 $strippedurl = strip_querystring(qualified_me());
356 if(!empty($getcopy)) {
359 foreach($getcopy as $var => $val) {
362 $querystring .= '?'.$var.'='.$val;
365 $querystring .= '&'.$var.'='.$val;
368 $this->reseturl
= $strippedurl.$querystring;
369 $querystring .= '&';
372 $this->reseturl
= $strippedurl.$querystring;
376 $this->baseurl
= strip_querystring(qualified_me()) . $querystring;
379 // If it's "the first time" we 've been here, forget the previous initials filters
380 if(qualified_me() == $this->reseturl
) {
381 $this->sess
->i_first
= '';
382 $this->sess
->i_last
= '';
385 $this->currpage
= optional_param($this->request
[TABLE_VAR_PAGE
], 0);
388 /// Always introduce the "flexible" class for the table if not specified
389 /// No attributes, add flexible class
390 if (empty($this->attributes
)) {
391 $this->attributes
['class'] = 'flexible';
392 /// No classes, add flexible class
393 } else if (!isset($this->attributes
['class'])) {
394 $this->attributes
['class'] = 'flexible';
395 /// No flexible class in passed classes, add flexible class
396 } else if (!in_array('flexible', explode(' ', $this->attributes
['class']))) {
397 $this->attributes
['class'] = trim('flexible ' . $this->attributes
['class']);
406 function get_sql_sort($uniqueid = NULL) {
407 if($uniqueid === NULL) {
408 // "Non-static" function call
412 $sess = &$this->sess
;
415 // "Static" function call
417 if(empty($SESSION->flextable
[$uniqueid])) {
420 $sess = &$SESSION->flextable
[$uniqueid];
423 if(!empty($sess->sortby
)) {
425 foreach($sess->sortby
as $column => $order) {
426 if(!empty($sortstring)) {
429 $sortstring .= $column.($order == SORT_ASC ?
' ASC' : ' DESC');
440 function get_page_start() {
441 if(!$this->use_pages
) {
444 return $this->currpage
* $this->pagesize
;
451 function get_page_size() {
452 if(!$this->use_pages
) {
455 return $this->pagesize
;
462 function get_sql_where() {
463 if(!isset($this->columns
['fullname'])) {
468 if(!empty($this->sess
->i_first
) && !empty($this->sess
->i_last
)) {
469 return 'firstname '.$LIKE.' \''.$this->sess
->i_first
.'%\' AND lastname '.$LIKE.' \''.$this->sess
->i_last
.'%\'';
471 else if(!empty($this->sess
->i_first
)) {
472 return 'firstname '.$LIKE.' \''.$this->sess
->i_first
.'%\'';
474 else if(!empty($this->sess
->i_last
)) {
475 return 'lastname '.$LIKE.' \''.$this->sess
->i_last
.'%\'';
485 function get_initial_first() {
486 if(!$this->use_initials
) {
490 return $this->sess
->i_first
;
497 function get_initial_last() {
498 if(!$this->use_initials
) {
502 return $this->sess
->i_last
;
509 function print_html() {
516 $colcount = count($this->columns
);
518 // Do we need to print initial bars?
520 if($this->use_initials
&& isset($this->columns
['fullname'])) {
522 $strall = get_string('all');
523 $alpha = explode(',', get_string('alphabet'));
525 // Bar of first initials
527 echo '<div class="initialbar firstinitial">'.get_string('firstname').' : ';
528 if(!empty($this->sess
->i_first
)) {
529 echo '<a href="'.$this->baseurl
.$this->request
[TABLE_VAR_IFIRST
].'=">'.$strall.'</a>';
531 echo '<strong>'.$strall.'</strong>';
533 foreach ($alpha as $letter) {
534 if ($letter == $this->sess
->i_first
) {
535 echo ' <strong>'.$letter.'</strong>';
537 echo ' <a href="'.$this->baseurl
.$this->request
[TABLE_VAR_IFIRST
].'='.$letter.'">'.$letter.'</a>';
542 // Bar of last initials
544 echo '<div class="initialbar lastinitial">'.get_string('lastname').' : ';
545 if(!empty($this->sess
->i_last
)) {
546 echo '<a href="'.$this->baseurl
.$this->request
[TABLE_VAR_ILAST
].'=">'.$strall.'</a>';
548 echo '<strong>'.$strall.'</strong>';
550 foreach ($alpha as $letter) {
551 if ($letter == $this->sess
->i_last
) {
552 echo ' <strong>'.$letter.'</strong>';
554 echo ' <a href="'.$this->baseurl
.$this->request
[TABLE_VAR_ILAST
].'='.$letter.'">'.$letter.'</a>';
561 // End of initial bars code
564 if($this->use_pages
) {
565 print_paging_bar($this->totalrows
, $this->currpage
, $this->pagesize
, $this->baseurl
, $this->request
[TABLE_VAR_PAGE
]);
568 if (empty($this->data
)) {
569 print_heading(get_string('nothingtodisplay'));
574 $suppress_enabled = array_sum($this->column_suppress
);
575 $suppress_lastrow = NULL;
576 // Start of main data table
578 echo '<table'.$this->make_attributes_string($this->attributes
).'>';
581 foreach($this->columns
as $column => $index) {
585 if($this->is_collapsible
) {
586 if(!empty($this->sess
->collapse
[$column])) {
587 // some headers contain < br/> tags, do not include in title
588 $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>';
590 else if($this->headers
[$index] !== NULL) {
591 // some headers contain < br/> tags, do not include in title
592 $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>';
596 $primary_sort_column = '';
597 $primary_sort_order = '';
598 if(reset($this->sess
->sortby
)) {
599 $primary_sort_column = key($this->sess
->sortby
);
600 $primary_sort_order = current($this->sess
->sortby
);
606 if($this->is_sortable
) {
607 $icon_sort_first = $icon_sort_last = '';
608 if($primary_sort_column == 'firstname') {
609 $lsortorder = get_string('asc');
610 if($primary_sort_order == SORT_ASC
) {
611 $icon_sort_first = ' <img src="'.$CFG->pixpath
.'/t/down.gif" alt="'.get_string('asc').'" />';
612 $fsortorder = get_string('asc');
615 $icon_sort_first = ' <img src="'.$CFG->pixpath
.'/t/up.gif" alt="'.get_string('desc').'" />';
616 $fsortorder = get_string('desc');
619 else if($primary_sort_column == 'lastname') {
620 $fsortorder = get_string('asc');
621 if($primary_sort_order == SORT_ASC
) {
622 $icon_sort_last = ' <img src="'.$CFG->pixpath
.'/t/down.gif" alt="'.get_string('asc').'" />';
623 $lsortorder = get_string('asc');
626 $icon_sort_last = ' <img src="'.$CFG->pixpath
.'/t/up.gif" alt="'.get_string('desc').'" />';
627 $lsortorder = get_string('desc');
630 $fsortorder = get_string('asc');
631 $lsortorder = get_string('asc');
633 $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.' / '.
634 '<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;
639 // do nothing, do not display sortable links
643 if($this->is_sortable
) {
644 if($primary_sort_column == $column) {
645 if($primary_sort_order == SORT_ASC
) {
646 $icon_sort = ' <img src="'.$CFG->pixpath
.'/t/down.gif" alt="'.get_string('asc').'" />';
647 $localsortorder = get_string('asc');
650 $icon_sort = ' <img src="'.$CFG->pixpath
.'/t/up.gif" alt="'.get_string('desc').'" />';
651 $localsortorder = get_string('desc');
654 $localsortorder = get_string('asc');
656 $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>';
660 if($this->headers
[$index] === NULL) {
661 echo '<th class="header c'.$index.$this->column_class
[$column].'" scope="col"> </th>';
663 else if(!empty($this->sess
->collapse
[$column])) {
664 echo '<th class="header c'.$index.$this->column_class
[$column].'" scope="col">'.$icon_hide.'</th>';
667 // took out nowrap for accessibility, might need replacement
668 if (!is_array($this->column_style
[$column])) {
669 // $usestyles = array('white-space:nowrap');
672 // $usestyles = $this->column_style[$column]+array('white-space'=>'nowrap');
673 $usestyles = $this->column_style
[$column];
675 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>';
681 if(!empty($this->data
)) {
683 $colbyindex = array_flip($this->columns
);
684 foreach($this->data
as $row) {
685 $oddeven = $oddeven ?
0 : 1;
686 echo '<tr class="r'.$oddeven.'">';
688 // If we have a separator, print it
689 if($row === NULL && $colcount) {
690 echo '<td colspan="'.$colcount.'"><div class="tabledivider"></div></td>';
693 foreach($row as $index => $data) {
694 if($index >= $colcount) {
697 $column = $colbyindex[$index];
698 echo '<td class="cell c'.$index.$this->column_class
[$column].'"'.$this->make_styles_string($this->column_style
[$column]).'>';
699 if(empty($this->sess
->collapse
[$column])) {
700 if($this->column_suppress
[$column] && $suppress_lastrow !== NULL && $suppress_lastrow[$index] === $data) {
714 if($suppress_enabled) {
715 $suppress_lastrow = $row;
723 if($this->use_pages
) {
724 print_paging_bar($this->totalrows
, $this->currpage
, $this->pagesize
, $this->baseurl
, $this->request
[TABLE_VAR_PAGE
]);
732 function add_data($row) {
736 $this->data
[] = $row;
743 function add_separator() {
747 $this->data
[] = NULL;