MDL-11517 reserved word MOD used in table alias in questions backup code
[moodle-pu.git] / lib / tablelib.php
blob6494965c429bdcb28532ff8d63b3e214cac09323
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 $column_nosort = array('userpic');
20 var $setup = false;
21 var $sess = NULL;
22 var $baseurl = NULL;
23 var $request = array();
25 var $is_collapsible = false;
26 var $is_sortable = false;
27 var $use_pages = false;
28 var $use_initials = false;
30 var $maxsortkeys = 2;
31 var $pagesize = 30;
32 var $currpage = 0;
33 var $totalrows = 0;
34 var $sort_default_column = NULL;
35 var $sort_default_order = SORT_ASC;
37 /**
38 * Constructor
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'
54 /**
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.
57 * @param bool $bool
58 * @param string $defaultcolumn
59 * @param int $defaultorder
60 * @return void
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;
68 /**
69 * Do not sort using this column
70 * @param string column name
72 function no_sorting($column) {
73 $this->column_nosort[] = $column;
76 /**
77 * Is the column sortable?
78 * @param string column name, null means table
79 * @return bool
81 function is_sortable($column=null) {
82 if (empty($column)) {
83 return $this->is_sortable;
85 if (!$this->is_sortable) {
86 return false;
88 return !in_array($column, $this->column_nosort);
91 /**
92 * Sets the is_collapsible variable to the given boolean.
93 * @param bool $bool
94 * @return void
96 function collapsible($bool) {
97 $this->is_collapsible = $bool;
101 * Sets the use_pages variable to the given boolean.
102 * @param bool $bool
103 * @return void
105 function pageable($bool) {
106 $this->use_pages = $bool;
110 * Sets the use_initials variable to the given boolean.
111 * @param bool $bool
112 * @return void
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
122 * @param int $total
123 * @return void
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
135 * @return void
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
149 * @return void
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
158 * @return void
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
170 * @return void
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
183 * @return void
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
195 * @return void
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 &amp;
205 * @param type? $url
206 * @return type?
208 function define_baseurl($url) {
209 $this->reseturl = $url;
210 if(!strpos($url, '?')) {
211 $this->baseurl = $url.'?';
213 else {
214 $this->baseurl = $url.'&amp;';
219 * @todo Document
220 * @return type?
222 function define_columns($columns) {
223 $this->columns = array();
224 $this->column_style = array();
225 $this->column_class = array();
226 $colnum = 0;
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;
237 * @todo Document
238 * @return type?
240 function define_headers($headers) {
241 $this->headers = $headers;
245 * @todo Document
246 * @return type?
248 function make_styles_string(&$styles) {
249 if(empty($styles)) {
250 return '';
253 $string = ' style="';
254 foreach($styles as $property => $value) {
255 $string .= $property.':'.$value.';';
257 $string .= '"';
258 return $string;
262 * @todo Document
263 * @return type?
265 function make_attributes_string(&$attributes) {
266 if(empty($attributes)) {
267 return '';
270 $string = ' ';
271 foreach($attributes as $attr => $value) {
272 $string .= ($attr.'="'.$value.'" ');
275 return $string;
279 * @todo Document
280 * @return type?
282 function setup() {
283 global $SESSION, $CFG;
285 if(empty($this->columns) || empty($this->uniqueid)) {
286 return false;
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]]])) {
305 // Show this column
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]]])) {
309 // Hide this column
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);
336 else {
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)) {
370 $getcopy = $_GET;
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)) {
381 $first = false;
382 $querystring = '';
383 foreach($getcopy as $var => $val) {
384 if(!$first) {
385 $first = true;
386 $querystring .= '?'.$var.'='.$val;
388 else {
389 $querystring .= '&amp;'.$var.'='.$val;
392 $this->reseturl = $strippedurl.$querystring;
393 $querystring .= '&amp;';
395 else {
396 $this->reseturl = $strippedurl.$querystring;
397 $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);
410 $this->setup = true;
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']);
427 * @todo Document
428 * @return type?
430 function get_sql_sort($uniqueid = NULL) {
431 if($uniqueid === NULL) {
432 // "Non-static" function call
433 if(!$this->setup) {
434 return false;
436 $sess = &$this->sess;
438 else {
439 // "Static" function call
440 global $SESSION;
441 if(empty($SESSION->flextable[$uniqueid])) {
442 return '';
444 $sess = &$SESSION->flextable[$uniqueid];
447 if(!empty($sess->sortby)) {
448 $sortstring = '';
449 foreach($sess->sortby as $column => $order) {
450 if(!empty($sortstring)) {
451 $sortstring .= ', ';
453 $sortstring .= $column.($order == SORT_ASC ? ' ASC' : ' DESC');
455 return $sortstring;
457 return '';
461 * @todo Document
462 * @return type?
464 function get_page_start() {
465 if(!$this->use_pages) {
466 return '';
468 return $this->currpage * $this->pagesize;
472 * @todo Document
473 * @return type?
475 function get_page_size() {
476 if(!$this->use_pages) {
477 return '';
479 return $this->pagesize;
483 * @todo Document
484 * @return type?
486 function get_sql_where() {
487 if(!isset($this->columns['fullname'])) {
488 return '';
491 $LIKE = sql_ilike();
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.'%\'';
502 return '';
506 * @todo Document
507 * @return type?
509 function get_initial_first() {
510 if(!$this->use_initials) {
511 return NULL;
514 return $this->sess->i_first;
518 * @todo Document
519 * @return type?
521 function get_initial_last() {
522 if(!$this->use_initials) {
523 return NULL;
526 return $this->sess->i_last;
530 * @todo Document
531 * @return type?
533 function print_html() {
534 global $CFG;
536 if(!$this->setup) {
537 return false;
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>';
554 } else {
555 echo '<strong>'.$strall.'</strong>';
557 foreach ($alpha as $letter) {
558 if ($letter == $this->sess->i_first) {
559 echo ' <strong>'.$letter.'</strong>';
560 } else {
561 echo ' <a href="'.$this->baseurl.$this->request[TABLE_VAR_IFIRST].'='.$letter.'">'.$letter.'</a>';
564 echo '</div>';
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>';
571 } else {
572 echo '<strong>'.$strall.'</strong>';
574 foreach ($alpha as $letter) {
575 if ($letter == $this->sess->i_last) {
576 echo ' <strong>'.$letter.'</strong>';
577 } else {
578 echo ' <a href="'.$this->baseurl.$this->request[TABLE_VAR_ILAST].'='.$letter.'">'.$letter.'</a>';
581 echo '</div>';
585 // End of initial bars code
587 // Paging bar
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'));
594 return true;
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).'>';
604 echo '<tr>';
605 foreach($this->columns as $column => $index) {
606 $icon_hide = '';
607 $icon_sort = '';
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);
627 switch($column) {
629 case 'fullname':
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');
638 else {
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');
649 else {
650 $icon_sort_last = ' <img src="'.$CFG->pixpath.'/t/up.gif" alt="'.get_string('desc').'" />';
651 $lsortorder = get_string('desc');
653 } else {
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;
660 break;
662 case 'userpic':
663 // do nothing, do not display sortable links
664 break;
666 default:
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');
673 else {
674 $icon_sort = ' <img src="'.$CFG->pixpath.'/t/up.gif" alt="'.get_string('desc').'" />';
675 $localsortorder = get_string('desc');
677 } else {
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">&nbsp;</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>';
690 else {
691 // took out nowrap for accessibility, might need replacement
692 if (!is_array($this->column_style[$column])) {
693 // $usestyles = array('white-space:nowrap');
694 $usestyles = '';
695 } else {
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>';
703 echo '</tr>';
705 if(!empty($this->data)) {
706 $oddeven = 1;
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>';
716 else {
717 foreach($row as $index => $data) {
718 if($index >= $colcount) {
719 break;
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) {
725 echo '&nbsp;';
727 else {
728 echo $data;
731 else {
732 echo '&nbsp;';
734 echo '</td>';
737 echo '</tr>';
738 if($suppress_enabled) {
739 $suppress_lastrow = $row;
744 echo '</table>';
746 // Paging bar
747 if($this->use_pages) {
748 print_paging_bar($this->totalrows, $this->currpage, $this->pagesize, $this->baseurl, $this->request[TABLE_VAR_PAGE]);
753 * @todo Document
754 * @return type?
756 function add_data($row) {
757 if(!$this->setup) {
758 return false;
760 $this->data[] = $row;
764 * @todo Document
765 * @return type?
767 function add_separator() {
768 if(!$this->setup) {
769 return false;
771 $this->data[] = NULL;