Use $this->error for only error report
[moodle-linuxchix.git] / lib / tablelib.php
blob80f7fca98852f55c2e710584caf651412b16e9f4
1 <?php // $Id$
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 {
12 var $uniqueid = NULL;
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 $setup = false;
20 var $sess = NULL;
21 var $baseurl = NULL;
22 var $request = array();
24 var $is_collapsible = false;
25 var $is_sortable = false;
26 var $use_pages = false;
27 var $use_initials = false;
29 var $maxsortkeys = 2;
30 var $pagesize = 30;
31 var $currpage = 0;
32 var $totalrows = 0;
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.'?';
113 else {
114 $this->baseurl = $url.'&amp;';
118 function define_columns($columns) {
119 $this->columns = array();
120 $this->column_style = array();
121 $this->column_class = array();
122 $colnum = 0;
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) {
137 if(empty($styles)) {
138 return '';
141 $string = ' style="';
142 foreach($styles as $property => $value) {
143 $string .= $property.':'.$value.';';
145 $string .= '"';
146 return $string;
149 function make_attributes_string(&$attributes) {
150 if(empty($attributes)) {
151 return '';
154 $string = ' ';
155 foreach($attributes as $attr => $value) {
156 $string .= ($attr.'="'.$value.'" ');
159 return $string;
162 function setup() {
163 global $SESSION, $CFG;
165 if(empty($this->columns) || empty($this->uniqueid)) {
166 return false;
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]]])) {
185 // Show this column
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]]])) {
189 // Hide this column
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);
216 else {
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)) {
250 $getcopy = $_GET;
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)) {
261 $first = false;
262 $querystring = '';
263 foreach($getcopy as $var => $val) {
264 if(!$first) {
265 $first = true;
266 $querystring .= '?'.$var.'='.$val;
268 else {
269 $querystring .= '&amp;'.$var.'='.$val;
272 $this->reseturl = $strippedurl.$querystring;
273 $querystring .= '&amp;';
275 else {
276 $this->reseturl = $strippedurl.$querystring;
277 $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);
290 $this->setup = true;
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
309 if(!$this->setup) {
310 return false;
312 $sess = &$this->sess;
314 else {
315 // "Static" function call
316 global $SESSION;
317 if(empty($SESSION->flextable[$uniqueid])) {
318 return '';
320 $sess = &$SESSION->flextable[$uniqueid];
323 if(!empty($sess->sortby)) {
324 $sortstring = '';
325 foreach($sess->sortby as $column => $order) {
326 if(!empty($sortstring)) {
327 $sortstring .= ', ';
329 $sortstring .= $column.($order == SORT_ASC ? ' ASC' : ' DESC');
331 return $sortstring;
333 return '';
336 function get_page_start() {
337 if(!$this->use_pages) {
338 return '';
340 return $this->currpage * $this->pagesize;
343 function get_page_size() {
344 if(!$this->use_pages) {
345 return '';
347 return $this->pagesize;
350 function get_sql_where() {
351 if(!isset($this->columns['fullname'])) {
352 return '';
355 $LIKE = sql_ilike();
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.'%\'';
366 return '';
369 function get_initial_first() {
370 if(!$this->use_initials) {
371 return NULL;
374 return $this->sess->i_first;
377 function get_initial_last() {
378 if(!$this->use_initials) {
379 return NULL;
382 return $this->sess->i_last;
385 function print_html() {
386 global $CFG;
388 if(!$this->setup) {
389 return false;
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>';
406 } else {
407 echo '<strong>'.$strall.'</strong>';
409 foreach ($alpha as $letter) {
410 if ($letter == $this->sess->i_first) {
411 echo ' <strong>'.$letter.'</strong>';
412 } else {
413 echo ' <a href="'.$this->baseurl.$this->request[TABLE_VAR_IFIRST].'='.$letter.'">'.$letter.'</a>';
416 echo '</div>';
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>';
423 } else {
424 echo '<strong>'.$strall.'</strong>';
426 foreach ($alpha as $letter) {
427 if ($letter == $this->sess->i_last) {
428 echo ' <strong>'.$letter.'</strong>';
429 } else {
430 echo ' <a href="'.$this->baseurl.$this->request[TABLE_VAR_ILAST].'='.$letter.'">'.$letter.'</a>';
433 echo '</div>';
437 // End of initial bars code
439 // Paging bar
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'));
446 return true;
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).'>';
456 echo '<tr>';
457 foreach($this->columns as $column => $index) {
458 $icon_hide = '';
459 $icon_sort = '';
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);
479 switch($column) {
481 case 'fullname':
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');
490 else {
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');
501 else {
502 $icon_sort_last = ' <img src="'.$CFG->pixpath.'/t/up.gif" alt="'.get_string('desc').'" />';
503 $lsortorder = get_string('desc');
505 } else {
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;
512 break;
514 case 'userpic':
515 // do nothing, do not display sortable links
516 break;
518 default:
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');
525 else {
526 $icon_sort = ' <img src="'.$CFG->pixpath.'/t/up.gif" alt="'.get_string('desc').'" />';
527 $localsortorder = get_string('desc');
529 } else {
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">&nbsp;</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>';
542 else {
543 // took out nowrap for accessibility, might need replacement
544 if (!is_array($this->column_style[$column])) {
545 // $usestyles = array('white-space:nowrap');
546 $usestyles = '';
547 } else {
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>';
555 echo '</tr>';
557 if(!empty($this->data)) {
558 $oddeven = 1;
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>';
568 else {
569 foreach($row as $index => $data) {
570 if($index >= $colcount) {
571 break;
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) {
577 echo '&nbsp;';
579 else {
580 echo $data;
583 else {
584 echo '&nbsp;';
586 echo '</td>';
589 echo '</tr>';
590 if($suppress_enabled) {
591 $suppress_lastrow = $row;
596 echo '</table>';
598 // Paging bar
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) {
605 if(!$this->setup) {
606 return false;
608 $this->data[] = $row;
611 function add_separator() {
612 if(!$this->setup) {
613 return false;
615 $this->data[] = NULL;