adding some strings
[moodle-linuxchix.git] / lib / tablelib.php
blob8cf011b254e532b6a3837b243a61d9d362b79b08
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 /**
37 * Constructor
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'
53 /**
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.
56 * @param bool $bool
57 * @param string $defaultcolumn
58 * @param int $defaultorder
59 * @return void
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;
67 /**
68 * Sets the is_collapsible variable to the given boolean.
69 * @param bool $bool
70 * @return void
72 function collapsible($bool) {
73 $this->is_collapsible = $bool;
76 /**
77 * Sets the use_pages variable to the given boolean.
78 * @param bool $bool
79 * @return void
81 function pageable($bool) {
82 $this->use_pages = $bool;
85 /**
86 * Sets the use_initials variable to the given boolean.
87 * @param bool $bool
88 * @return void
90 function initialbars($bool) {
91 $this->use_initials = $bool;
94 /**
95 * Sets the pagesize variable to the given integer, the totalrows variable
96 * to the given integer, and the use_pages variable to true.
97 * @param int $perpage
98 * @param int $total
99 * @return void
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
111 * @return void
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
125 * @return void
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
134 * @return void
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
146 * @return void
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
159 * @return void
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
171 * @return void
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 &amp;
181 * @param type? $url
182 * @return type?
184 function define_baseurl($url) {
185 $this->reseturl = $url;
186 if(!strpos($url, '?')) {
187 $this->baseurl = $url.'?';
189 else {
190 $this->baseurl = $url.'&amp;';
195 * @todo Document
196 * @return type?
198 function define_columns($columns) {
199 $this->columns = array();
200 $this->column_style = array();
201 $this->column_class = array();
202 $colnum = 0;
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;
213 * @todo Document
214 * @return type?
216 function define_headers($headers) {
217 $this->headers = $headers;
221 * @todo Document
222 * @return type?
224 function make_styles_string(&$styles) {
225 if(empty($styles)) {
226 return '';
229 $string = ' style="';
230 foreach($styles as $property => $value) {
231 $string .= $property.':'.$value.';';
233 $string .= '"';
234 return $string;
238 * @todo Document
239 * @return type?
241 function make_attributes_string(&$attributes) {
242 if(empty($attributes)) {
243 return '';
246 $string = ' ';
247 foreach($attributes as $attr => $value) {
248 $string .= ($attr.'="'.$value.'" ');
251 return $string;
255 * @todo Document
256 * @return type?
258 function setup() {
259 global $SESSION, $CFG;
261 if(empty($this->columns) || empty($this->uniqueid)) {
262 return false;
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]]])) {
281 // Show this column
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]]])) {
285 // Hide this column
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);
312 else {
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)) {
346 $getcopy = $_GET;
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)) {
357 $first = false;
358 $querystring = '';
359 foreach($getcopy as $var => $val) {
360 if(!$first) {
361 $first = true;
362 $querystring .= '?'.$var.'='.$val;
364 else {
365 $querystring .= '&amp;'.$var.'='.$val;
368 $this->reseturl = $strippedurl.$querystring;
369 $querystring .= '&amp;';
371 else {
372 $this->reseturl = $strippedurl.$querystring;
373 $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);
386 $this->setup = true;
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']);
403 * @todo Document
404 * @return type?
406 function get_sql_sort($uniqueid = NULL) {
407 if($uniqueid === NULL) {
408 // "Non-static" function call
409 if(!$this->setup) {
410 return false;
412 $sess = &$this->sess;
414 else {
415 // "Static" function call
416 global $SESSION;
417 if(empty($SESSION->flextable[$uniqueid])) {
418 return '';
420 $sess = &$SESSION->flextable[$uniqueid];
423 if(!empty($sess->sortby)) {
424 $sortstring = '';
425 foreach($sess->sortby as $column => $order) {
426 if(!empty($sortstring)) {
427 $sortstring .= ', ';
429 $sortstring .= $column.($order == SORT_ASC ? ' ASC' : ' DESC');
431 return $sortstring;
433 return '';
437 * @todo Document
438 * @return type?
440 function get_page_start() {
441 if(!$this->use_pages) {
442 return '';
444 return $this->currpage * $this->pagesize;
448 * @todo Document
449 * @return type?
451 function get_page_size() {
452 if(!$this->use_pages) {
453 return '';
455 return $this->pagesize;
459 * @todo Document
460 * @return type?
462 function get_sql_where() {
463 if(!isset($this->columns['fullname'])) {
464 return '';
467 $LIKE = sql_ilike();
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.'%\'';
478 return '';
482 * @todo Document
483 * @return type?
485 function get_initial_first() {
486 if(!$this->use_initials) {
487 return NULL;
490 return $this->sess->i_first;
494 * @todo Document
495 * @return type?
497 function get_initial_last() {
498 if(!$this->use_initials) {
499 return NULL;
502 return $this->sess->i_last;
506 * @todo Document
507 * @return type?
509 function print_html() {
510 global $CFG;
512 if(!$this->setup) {
513 return false;
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>';
530 } else {
531 echo '<strong>'.$strall.'</strong>';
533 foreach ($alpha as $letter) {
534 if ($letter == $this->sess->i_first) {
535 echo ' <strong>'.$letter.'</strong>';
536 } else {
537 echo ' <a href="'.$this->baseurl.$this->request[TABLE_VAR_IFIRST].'='.$letter.'">'.$letter.'</a>';
540 echo '</div>';
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>';
547 } else {
548 echo '<strong>'.$strall.'</strong>';
550 foreach ($alpha as $letter) {
551 if ($letter == $this->sess->i_last) {
552 echo ' <strong>'.$letter.'</strong>';
553 } else {
554 echo ' <a href="'.$this->baseurl.$this->request[TABLE_VAR_ILAST].'='.$letter.'">'.$letter.'</a>';
557 echo '</div>';
561 // End of initial bars code
563 // Paging bar
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'));
570 return true;
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).'>';
580 echo '<tr>';
581 foreach($this->columns as $column => $index) {
582 $icon_hide = '';
583 $icon_sort = '';
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);
603 switch($column) {
605 case 'fullname':
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');
614 else {
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');
625 else {
626 $icon_sort_last = ' <img src="'.$CFG->pixpath.'/t/up.gif" alt="'.get_string('desc').'" />';
627 $lsortorder = get_string('desc');
629 } else {
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;
636 break;
638 case 'userpic':
639 // do nothing, do not display sortable links
640 break;
642 default:
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');
649 else {
650 $icon_sort = ' <img src="'.$CFG->pixpath.'/t/up.gif" alt="'.get_string('desc').'" />';
651 $localsortorder = get_string('desc');
653 } else {
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">&nbsp;</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>';
666 else {
667 // took out nowrap for accessibility, might need replacement
668 if (!is_array($this->column_style[$column])) {
669 // $usestyles = array('white-space:nowrap');
670 $usestyles = '';
671 } else {
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>';
679 echo '</tr>';
681 if(!empty($this->data)) {
682 $oddeven = 1;
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>';
692 else {
693 foreach($row as $index => $data) {
694 if($index >= $colcount) {
695 break;
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) {
701 echo '&nbsp;';
703 else {
704 echo $data;
707 else {
708 echo '&nbsp;';
710 echo '</td>';
713 echo '</tr>';
714 if($suppress_enabled) {
715 $suppress_lastrow = $row;
720 echo '</table>';
722 // Paging bar
723 if($this->use_pages) {
724 print_paging_bar($this->totalrows, $this->currpage, $this->pagesize, $this->baseurl, $this->request[TABLE_VAR_PAGE]);
729 * @todo Document
730 * @return type?
732 function add_data($row) {
733 if(!$this->setup) {
734 return false;
736 $this->data[] = $row;
740 * @todo Document
741 * @return type?
743 function add_separator() {
744 if(!$this->setup) {
745 return false;
747 $this->data[] = NULL;