MDL-12302 stopping filter_save_ignore_tags ignoring empty tags
[moodle.git] / lib / tablelib.php
CommitLineData
7270d81f 1<?php // $Id$
2
e2ba85e9 3define('TABLE_VAR_SORT', 1);
4define('TABLE_VAR_HIDE', 2);
5define('TABLE_VAR_SHOW', 3);
6define('TABLE_VAR_IFIRST', 4);
7define('TABLE_VAR_ILAST', 5);
8define('TABLE_VAR_PAGE', 6);
9
7270d81f 10class flexible_table {
11
12 var $uniqueid = NULL;
13 var $attributes = array();
14 var $headers = array();
15 var $columns = array();
16 var $column_style = array();
554906f6 17 var $column_class = array();
7270d81f 18 var $column_suppress = array();
353f3f20 19 var $column_nosort = array('userpic');
7270d81f 20 var $setup = false;
21 var $sess = NULL;
22 var $baseurl = NULL;
e2ba85e9 23 var $request = array();
7270d81f 24
25 var $is_collapsible = false;
26 var $is_sortable = false;
27 var $use_pages = false;
28 var $use_initials = false;
29
73e4f589 30 var $maxsortkeys = 2;
31 var $pagesize = 30;
32 var $currpage = 0;
33 var $totalrows = 0;
a5a3f48a 34 var $sort_default_column = NULL;
35 var $sort_default_order = SORT_ASC;
561d406b 36
37 /**
38 * Constructor
39 * @param int $uniqueid
40 * @todo Document properly
41 */
7270d81f 42 function flexible_table($uniqueid) {
43 $this->uniqueid = $uniqueid;
e2ba85e9 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'
51 );
7270d81f 52 }
561d406b 53
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
61 */
a5a3f48a 62 function sortable($bool, $defaultcolumn = NULL, $defaultorder = SORT_ASC) {
7270d81f 63 $this->is_sortable = $bool;
a5a3f48a 64 $this->sort_default_column = $defaultcolumn;
65 $this->sort_default_order = $defaultorder;
7270d81f 66 }
67
353f3f20 68 /**
69 * Do not sort using this column
70 * @param string column name
71 */
72 function no_sorting($column) {
73 $this->column_nosort[] = $column;
74 }
75
76 /**
77 * Is the column sortable?
78 * @param string column name, null means table
79 * @return bool
80 */
81 function is_sortable($column=null) {
82 if (empty($column)) {
83 return $this->is_sortable;
84 }
85 if (!$this->is_sortable) {
86 return false;
87 }
88 return !in_array($column, $this->column_nosort);
89 }
90
561d406b 91 /**
92 * Sets the is_collapsible variable to the given boolean.
93 * @param bool $bool
94 * @return void
95 */
7270d81f 96 function collapsible($bool) {
97 $this->is_collapsible = $bool;
98 }
99
561d406b 100 /**
101 * Sets the use_pages variable to the given boolean.
102 * @param bool $bool
103 * @return void
104 */
7270d81f 105 function pageable($bool) {
106 $this->use_pages = $bool;
107 }
108
561d406b 109 /**
110 * Sets the use_initials variable to the given boolean.
111 * @param bool $bool
112 * @return void
113 */
7270d81f 114 function initialbars($bool) {
115 $this->use_initials = $bool;
116 }
117
561d406b 118 /**
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
124 */
7270d81f 125 function pagesize($perpage, $total) {
126 $this->pagesize = $perpage;
127 $this->totalrows = $total;
128 $this->use_pages = true;
129 }
130
561d406b 131 /**
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
136 */
e2ba85e9 137 function set_control_variables($variables) {
138 foreach($variables as $what => $variable) {
139 if(isset($this->request[$what])) {
140 $this->request[$what] = $variable;
141 }
142 }
143 }
144
561d406b 145 /**
146 * Gives the given $value to the $attribute index of $this->attributes.
147 * @param string $attribute
148 * @param mixed $value
149 * @return void
150 */
7270d81f 151 function set_attribute($attribute, $value) {
152 $this->attributes[$attribute] = $value;
153 }
154
561d406b 155 /**
901e25d4 156 * I think that what this method does is set the column so that if the same data appears in
157 * consecutive rows, then it is not repeated.
158 *
159 * For example, in the quiz overview report, the fullname column is set to be suppressed, so
160 * that when one student has made multiple attempts, their name is only printed in the row
161 * for their first attempt.
162 * @param integer $column the index of a column.
561d406b 163 */
7270d81f 164 function column_suppress($column) {
165 if(isset($this->column_suppress[$column])) {
166 $this->column_suppress[$column] = true;
167 }
168 }
169
561d406b 170 /**
171 * Sets the given $column index to the given $classname in $this->column_class.
172 * @param integer $column
173 * @param string $classname
174 * @return void
175 */
554906f6 176 function column_class($column, $classname) {
177 if(isset($this->column_class[$column])) {
178 $this->column_class[$column] = ' '.$classname; // This space needed so that classnames don't run together in the HTML
179 }
180 }
181
561d406b 182 /**
183 * Sets the given $column index and $property index to the given $value in $this->column_style.
184 * @param integer $column
185 * @param string $property
186 * @param mixed $value
187 * @return void
188 */
7270d81f 189 function column_style($column, $property, $value) {
190 if(isset($this->column_style[$column])) {
191 $this->column_style[$column][$property] = $value;
192 }
193 }
194
561d406b 195 /**
196 * Sets all columns of the given $property to the given $value in $this->column_style.
197 * @param integer $property
198 * @param string $value
199 * @return void
200 */
7270d81f 201 function column_style_all($property, $value) {
202 foreach(array_keys($this->columns) as $column) {
203 $this->column_style[$column][$property] = $value;
204 }
205 }
206
561d406b 207 /**
208 * Sets $this->reseturl to the given $url, and $this->baseurl to the given $url plus ? or &amp;
209 * @param type? $url
210 * @return type?
211 */
7270d81f 212 function define_baseurl($url) {
213 $this->reseturl = $url;
214 if(!strpos($url, '?')) {
215 $this->baseurl = $url.'?';
216 }
217 else {
218 $this->baseurl = $url.'&amp;';
219 }
220 }
221
561d406b 222 /**
223 * @todo Document
224 * @return type?
225 */
7270d81f 226 function define_columns($columns) {
227 $this->columns = array();
228 $this->column_style = array();
554906f6 229 $this->column_class = array();
7270d81f 230 $colnum = 0;
231
232 foreach($columns as $column) {
233 $this->columns[$column] = $colnum++;
234 $this->column_style[$column] = array();
554906f6 235 $this->column_class[$column] = '';
7270d81f 236 $this->column_suppress[$column] = false;
237 }
238 }
239
561d406b 240 /**
241 * @todo Document
242 * @return type?
243 */
7270d81f 244 function define_headers($headers) {
245 $this->headers = $headers;
246 }
247
561d406b 248 /**
249 * @todo Document
250 * @return type?
251 */
7270d81f 252 function make_styles_string(&$styles) {
253 if(empty($styles)) {
254 return '';
255 }
256
257 $string = ' style="';
258 foreach($styles as $property => $value) {
259 $string .= $property.':'.$value.';';
260 }
261 $string .= '"';
262 return $string;
263 }
264
561d406b 265 /**
266 * @todo Document
267 * @return type?
268 */
7270d81f 269 function make_attributes_string(&$attributes) {
270 if(empty($attributes)) {
271 return '';
272 }
1e42af27 273
7270d81f 274 $string = ' ';
275 foreach($attributes as $attr => $value) {
276 $string .= ($attr.'="'.$value.'" ');
277 }
1e42af27 278
7270d81f 279 return $string;
280 }
281
561d406b 282 /**
283 * @todo Document
284 * @return type?
285 */
7270d81f 286 function setup() {
287 global $SESSION, $CFG;
288
289 if(empty($this->columns) || empty($this->uniqueid)) {
290 return false;
291 }
1e42af27 292
0ce8ba58 293 if (!isset($SESSION->flextable)) {
294 $SESSION->flextable = array();
7270d81f 295 }
1e42af27 296
0ce8ba58 297 if(!isset($SESSION->flextable[$this->uniqueid])) {
298 $SESSION->flextable[$this->uniqueid] = new stdClass;
299 $SESSION->flextable[$this->uniqueid]->uniqueid = $this->uniqueid;
300 $SESSION->flextable[$this->uniqueid]->collapse = array();
301 $SESSION->flextable[$this->uniqueid]->sortby = array();
302 $SESSION->flextable[$this->uniqueid]->i_first = '';
303 $SESSION->flextable[$this->uniqueid]->i_last = '';
304 }
305
306 $this->sess = &$SESSION->flextable[$this->uniqueid];
7270d81f 307
e2ba85e9 308 if(!empty($_GET[$this->request[TABLE_VAR_SHOW]]) && isset($this->columns[$_GET[$this->request[TABLE_VAR_SHOW]]])) {
7270d81f 309 // Show this column
e2ba85e9 310 $this->sess->collapse[$_GET[$this->request[TABLE_VAR_SHOW]]] = false;
7270d81f 311 }
e2ba85e9 312 else if(!empty($_GET[$this->request[TABLE_VAR_HIDE]]) && isset($this->columns[$_GET[$this->request[TABLE_VAR_HIDE]]])) {
7270d81f 313 // Hide this column
e2ba85e9 314 $this->sess->collapse[$_GET[$this->request[TABLE_VAR_HIDE]]] = true;
315 if(array_key_exists($_GET[$this->request[TABLE_VAR_HIDE]], $this->sess->sortby)) {
316 unset($this->sess->sortby[$_GET[$this->request[TABLE_VAR_HIDE]]]);
7270d81f 317 }
318 }
1e42af27 319
7270d81f 320 // Now, update the column attributes for collapsed columns
321 foreach(array_keys($this->columns) as $column) {
322 if(!empty($this->sess->collapse[$column])) {
323 $this->column_style[$column]['width'] = '10px';
324 }
325 }
326
327 if(
353f3f20 328 !empty($_GET[$this->request[TABLE_VAR_SORT]]) && $this->is_sortable($_GET[$this->request[TABLE_VAR_SORT]]) &&
e2ba85e9 329 (isset($this->columns[$_GET[$this->request[TABLE_VAR_SORT]]]) ||
330 (($_GET[$this->request[TABLE_VAR_SORT]] == 'firstname' || $_GET[$this->request[TABLE_VAR_SORT]] == 'lastname') && isset($this->columns['fullname']))
7270d81f 331 ))
332 {
e2ba85e9 333 if(empty($this->sess->collapse[$_GET[$this->request[TABLE_VAR_SORT]]])) {
334 if(array_key_exists($_GET[$this->request[TABLE_VAR_SORT]], $this->sess->sortby)) {
73e4f589 335 // This key already exists somewhere. Change its sortorder and bring it to the top.
e2ba85e9 336 $sortorder = $this->sess->sortby[$_GET[$this->request[TABLE_VAR_SORT]]] == SORT_ASC ? SORT_DESC : SORT_ASC;
337 unset($this->sess->sortby[$_GET[$this->request[TABLE_VAR_SORT]]]);
338 $this->sess->sortby = array_merge(array($_GET[$this->request[TABLE_VAR_SORT]] => $sortorder), $this->sess->sortby);
7270d81f 339 }
340 else {
73e4f589 341 // Key doesn't exist, so just add it to the beginning of the array, ascending order
e2ba85e9 342 $this->sess->sortby = array_merge(array($_GET[$this->request[TABLE_VAR_SORT]] => SORT_ASC), $this->sess->sortby);
73e4f589 343 }
344 // Finally, make sure that no more than $this->maxsortkeys are present into the array
345 if(!empty($this->maxsortkeys) && ($sortkeys = count($this->sess->sortby)) > $this->maxsortkeys) {
346 while($sortkeys-- > $this->maxsortkeys) {
347 array_pop($this->sess->sortby);
348 }
7270d81f 349 }
350 }
351 }
352
a5a3f48a 353 // If we didn't sort just now, then use the default sort order if one is defined and the column exists
f53fba38 354 if(empty($this->sess->sortby) && !empty($this->sort_default_column) && (isset($this->columns[$this->sort_default_column])
355 || (in_array('fullname',$this->columns)
356 && in_array($this->sort_default_column,
357 array('firstname','lastname'))))) {
a5a3f48a 358 $this->sess->sortby = array ($this->sort_default_column => ($this->sort_default_order == SORT_DESC ? SORT_DESC : SORT_ASC));
359 }
360
e2ba85e9 361 if(isset($_GET[$this->request[TABLE_VAR_ILAST]])) {
362 if(empty($_GET[$this->request[TABLE_VAR_ILAST]]) || is_numeric(strpos(get_string('alphabet'), $_GET[$this->request[TABLE_VAR_ILAST]]))) {
363 $this->sess->i_last = $_GET[$this->request[TABLE_VAR_ILAST]];
7270d81f 364 }
365 }
366
e2ba85e9 367 if(isset($_GET[$this->request[TABLE_VAR_IFIRST]])) {
368 if(empty($_GET[$this->request[TABLE_VAR_IFIRST]]) || is_numeric(strpos(get_string('alphabet'), $_GET[$this->request[TABLE_VAR_IFIRST]]))) {
369 $this->sess->i_first = $_GET[$this->request[TABLE_VAR_IFIRST]];
7270d81f 370 }
371 }
372
373 if(empty($this->baseurl)) {
374 $getcopy = $_GET;
e2ba85e9 375 unset($getcopy[$this->request[TABLE_VAR_SHOW]]);
376 unset($getcopy[$this->request[TABLE_VAR_HIDE]]);
377 unset($getcopy[$this->request[TABLE_VAR_SORT]]);
378 unset($getcopy[$this->request[TABLE_VAR_IFIRST]]);
379 unset($getcopy[$this->request[TABLE_VAR_ILAST]]);
380 unset($getcopy[$this->request[TABLE_VAR_PAGE]]);
7270d81f 381
382 $strippedurl = strip_querystring(qualified_me());
383
384 if(!empty($getcopy)) {
385 $first = false;
386 $querystring = '';
387 foreach($getcopy as $var => $val) {
388 if(!$first) {
389 $first = true;
390 $querystring .= '?'.$var.'='.$val;
391 }
392 else {
393 $querystring .= '&amp;'.$var.'='.$val;
394 }
395 }
396 $this->reseturl = $strippedurl.$querystring;
397 $querystring .= '&amp;';
398 }
399 else {
7bd75cb3 400 $this->reseturl = $strippedurl;
7270d81f 401 $querystring = '?';
402 }
1e42af27 403
7270d81f 404 $this->baseurl = strip_querystring(qualified_me()) . $querystring;
405 }
406
407 // If it's "the first time" we 've been here, forget the previous initials filters
408 if(qualified_me() == $this->reseturl) {
409 $this->sess->i_first = '';
410 $this->sess->i_last = '';
411 }
412
e2ba85e9 413 $this->currpage = optional_param($this->request[TABLE_VAR_PAGE], 0);
7270d81f 414 $this->setup = true;
d1b523b7 415
416 /// Always introduce the "flexible" class for the table if not specified
417 /// No attributes, add flexible class
418 if (empty($this->attributes)) {
419 $this->attributes['class'] = 'flexible';
420 /// No classes, add flexible class
421 } else if (!isset($this->attributes['class'])) {
422 $this->attributes['class'] = 'flexible';
423 /// No flexible class in passed classes, add flexible class
424 } else if (!in_array('flexible', explode(' ', $this->attributes['class']))) {
425 $this->attributes['class'] = trim('flexible ' . $this->attributes['class']);
426 }
427
7270d81f 428 }
429
561d406b 430 /**
431 * @todo Document
432 * @return type?
433 */
9013e4ad 434 function get_sql_sort($uniqueid = NULL) {
435 if($uniqueid === NULL) {
436 // "Non-static" function call
437 if(!$this->setup) {
438 return false;
439 }
440 $sess = &$this->sess;
441 }
442 else {
443 // "Static" function call
444 global $SESSION;
445 if(empty($SESSION->flextable[$uniqueid])) {
446 return '';
447 }
448 $sess = &$SESSION->flextable[$uniqueid];
7270d81f 449 }
9013e4ad 450
451 if(!empty($sess->sortby)) {
73e4f589 452 $sortstring = '';
9013e4ad 453 foreach($sess->sortby as $column => $order) {
73e4f589 454 if(!empty($sortstring)) {
455 $sortstring .= ', ';
456 }
457 $sortstring .= $column.($order == SORT_ASC ? ' ASC' : ' DESC');
458 }
459 return $sortstring;
7270d81f 460 }
73e4f589 461 return '';
7270d81f 462 }
463
561d406b 464 /**
465 * @todo Document
466 * @return type?
467 */
7270d81f 468 function get_page_start() {
469 if(!$this->use_pages) {
470 return '';
471 }
472 return $this->currpage * $this->pagesize;
473 }
474
561d406b 475 /**
476 * @todo Document
477 * @return type?
478 */
7270d81f 479 function get_page_size() {
480 if(!$this->use_pages) {
481 return '';
482 }
483 return $this->pagesize;
484 }
485
561d406b 486 /**
487 * @todo Document
488 * @return type?
489 */
7270d81f 490 function get_sql_where() {
7e60297f 491 global $DB;
7270d81f 492 if(!isset($this->columns['fullname'])) {
493 return '';
494 }
495
7e60297f 496 $LIKE = $DB->sql_ilike();
1e42af27 497 if(!empty($this->sess->i_first) && !empty($this->sess->i_last)) {
7270d81f 498 return 'firstname '.$LIKE.' \''.$this->sess->i_first.'%\' AND lastname '.$LIKE.' \''.$this->sess->i_last.'%\'';
499 }
500 else if(!empty($this->sess->i_first)) {
501 return 'firstname '.$LIKE.' \''.$this->sess->i_first.'%\'';
502 }
503 else if(!empty($this->sess->i_last)) {
504 return 'lastname '.$LIKE.' \''.$this->sess->i_last.'%\'';
505 }
506
507 return '';
508 }
509
561d406b 510 /**
511 * @todo Document
512 * @return type?
513 */
91bb59f4 514 function get_initial_first() {
515 if(!$this->use_initials) {
516 return NULL;
517 }
518
519 return $this->sess->i_first;
520 }
521
561d406b 522 /**
523 * @todo Document
524 * @return type?
525 */
91bb59f4 526 function get_initial_last() {
527 if(!$this->use_initials) {
528 return NULL;
529 }
530
531 return $this->sess->i_last;
532 }
533
561d406b 534 /**
535 * @todo Document
536 * @return type?
537 */
7270d81f 538 function print_html() {
539 global $CFG;
540
541 if(!$this->setup) {
542 return false;
543 }
544
545 $colcount = count($this->columns);
546
547 // Do we need to print initial bars?
548
549 if($this->use_initials && isset($this->columns['fullname'])) {
550
551 $strall = get_string('all');
552 $alpha = explode(',', get_string('alphabet'));
553
554 // Bar of first initials
d07fe1a9 555
91bb59f4 556 echo '<div class="initialbar firstinitial">'.get_string('firstname').' : ';
7270d81f 557 if(!empty($this->sess->i_first)) {
e2ba85e9 558 echo '<a href="'.$this->baseurl.$this->request[TABLE_VAR_IFIRST].'=">'.$strall.'</a>';
7270d81f 559 } else {
560 echo '<strong>'.$strall.'</strong>';
561 }
562 foreach ($alpha as $letter) {
d07fe1a9 563 if (isset($this->sess->i_first) && $letter == $this->sess->i_first) {
7270d81f 564 echo ' <strong>'.$letter.'</strong>';
565 } else {
e2ba85e9 566 echo ' <a href="'.$this->baseurl.$this->request[TABLE_VAR_IFIRST].'='.$letter.'">'.$letter.'</a>';
7270d81f 567 }
568 }
569 echo '</div>';
1e42af27 570
7270d81f 571 // Bar of last initials
572
91bb59f4 573 echo '<div class="initialbar lastinitial">'.get_string('lastname').' : ';
7270d81f 574 if(!empty($this->sess->i_last)) {
e2ba85e9 575 echo '<a href="'.$this->baseurl.$this->request[TABLE_VAR_ILAST].'=">'.$strall.'</a>';
7270d81f 576 } else {
577 echo '<strong>'.$strall.'</strong>';
578 }
579 foreach ($alpha as $letter) {
d07fe1a9 580 if (isset($this->sess->i_last) && $letter == $this->sess->i_last) {
7270d81f 581 echo ' <strong>'.$letter.'</strong>';
582 } else {
e2ba85e9 583 echo ' <a href="'.$this->baseurl.$this->request[TABLE_VAR_ILAST].'='.$letter.'">'.$letter.'</a>';
7270d81f 584 }
585 }
586 echo '</div>';
587
588 }
589
590 // End of initial bars code
591
592 // Paging bar
593 if($this->use_pages) {
e2ba85e9 594 print_paging_bar($this->totalrows, $this->currpage, $this->pagesize, $this->baseurl, $this->request[TABLE_VAR_PAGE]);
7270d81f 595 }
596
68ffc100 597 if (empty($this->data)) {
598 print_heading(get_string('nothingtodisplay'));
599 return true;
7270d81f 600 }
601
602
603 $suppress_enabled = array_sum($this->column_suppress);
604 $suppress_lastrow = NULL;
605 // Start of main data table
606
607 echo '<table'.$this->make_attributes_string($this->attributes).'>';
608
609 echo '<tr>';
610 foreach($this->columns as $column => $index) {
611 $icon_hide = '';
612 $icon_sort = '';
613
614 if($this->is_collapsible) {
615 if(!empty($this->sess->collapse[$column])) {
b2603953 616 // some headers contain < br/> tags, do not include in title
617 $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>';
7270d81f 618 }
619 else if($this->headers[$index] !== NULL) {
b2603953 620 // some headers contain < br/> tags, do not include in title
621 $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>';
7270d81f 622 }
623 }
624
73e4f589 625 $primary_sort_column = '';
626 $primary_sort_order = '';
627 if(reset($this->sess->sortby)) {
628 $primary_sort_column = key($this->sess->sortby);
629 $primary_sort_order = current($this->sess->sortby);
630 }
631
7270d81f 632 switch($column) {
633
634 case 'fullname':
353f3f20 635 if($this->is_sortable($column)) {
7270d81f 636 $icon_sort_first = $icon_sort_last = '';
73e4f589 637 if($primary_sort_column == 'firstname') {
49108746 638 $lsortorder = get_string('asc');
73e4f589 639 if($primary_sort_order == SORT_ASC) {
2bbaf749 640 $icon_sort_first = ' <img src="'.$CFG->pixpath.'/t/down.gif" alt="'.get_string('asc').'" />';
49108746 641 $fsortorder = get_string('asc');
7270d81f 642 }
643 else {
2bbaf749 644 $icon_sort_first = ' <img src="'.$CFG->pixpath.'/t/up.gif" alt="'.get_string('desc').'" />';
49108746 645 $fsortorder = get_string('desc');
7270d81f 646 }
647 }
73e4f589 648 else if($primary_sort_column == 'lastname') {
49108746 649 $fsortorder = get_string('asc');
73e4f589 650 if($primary_sort_order == SORT_ASC) {
2bbaf749 651 $icon_sort_last = ' <img src="'.$CFG->pixpath.'/t/down.gif" alt="'.get_string('asc').'" />';
49108746 652 $lsortorder = get_string('asc');
7270d81f 653 }
654 else {
2bbaf749 655 $icon_sort_last = ' <img src="'.$CFG->pixpath.'/t/up.gif" alt="'.get_string('desc').'" />';
49108746 656 $lsortorder = get_string('desc');
7270d81f 657 }
49108746 658 } else {
659 $fsortorder = get_string('asc');
660 $lsortorder = get_string('asc');
7270d81f 661 }
f79f2494 662 $this->headers[$index] = '<a href="'.$this->baseurl.$this->request[TABLE_VAR_SORT].'=firstname">'.get_string('firstname').get_accesshide(get_string('sortby').' '.get_string('firstname').' '.$fsortorder).'</a> '.$icon_sort_first.' / '.
663 '<a href="'.$this->baseurl.$this->request[TABLE_VAR_SORT].'=lastname">'.get_string('lastname').get_accesshide(get_string('sortby').' '.get_string('lastname').' '.$lsortorder).'</a> '.$icon_sort_last;
7270d81f 664 }
665 break;
666
1d40c70b 667 case 'userpic':
668 // do nothing, do not display sortable links
669 break;
670
7270d81f 671 default:
353f3f20 672 if($this->is_sortable($column)) {
73e4f589 673 if($primary_sort_column == $column) {
674 if($primary_sort_order == SORT_ASC) {
2bbaf749 675 $icon_sort = ' <img src="'.$CFG->pixpath.'/t/down.gif" alt="'.get_string('asc').'" />';
49108746 676 $localsortorder = get_string('asc');
7270d81f 677 }
678 else {
2bbaf749 679 $icon_sort = ' <img src="'.$CFG->pixpath.'/t/up.gif" alt="'.get_string('desc').'" />';
49108746 680 $localsortorder = get_string('desc');
7270d81f 681 }
49108746 682 } else {
683 $localsortorder = get_string('asc');
7270d81f 684 }
f79f2494 685 $this->headers[$index] = '<a href="'.$this->baseurl.$this->request[TABLE_VAR_SORT].'='.$column.'">'.$this->headers[$index].get_accesshide(get_string('sortby').' '.$this->headers[$index].' '.$localsortorder).'</a>';
7270d81f 686 }
687 }
1e42af27 688
7270d81f 689 if($this->headers[$index] === NULL) {
8dc9a211 690 echo '<th class="header c'.$index.$this->column_class[$column].'" scope="col">&nbsp;</th>';
7270d81f 691 }
692 else if(!empty($this->sess->collapse[$column])) {
8dc9a211 693 echo '<th class="header c'.$index.$this->column_class[$column].'" scope="col">'.$icon_hide.'</th>';
7270d81f 694 }
695 else {
d9cb14b8 696 // took out nowrap for accessibility, might need replacement
6e291bc6 697 if (!is_array($this->column_style[$column])) {
698 // $usestyles = array('white-space:nowrap');
699 $usestyles = '';
82b9f43e 700 } else {
6e291bc6 701 // $usestyles = $this->column_style[$column]+array('white-space'=>'nowrap');
702 $usestyles = $this->column_style[$column];
703 }
82b9f43e 704 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>';
7270d81f 705 }
706
707 }
708 echo '</tr>';
709
710 if(!empty($this->data)) {
711 $oddeven = 1;
712 $colbyindex = array_flip($this->columns);
713 foreach($this->data as $row) {
714 $oddeven = $oddeven ? 0 : 1;
715 echo '<tr class="r'.$oddeven.'">';
716
717 // If we have a separator, print it
718 if($row === NULL && $colcount) {
719 echo '<td colspan="'.$colcount.'"><div class="tabledivider"></div></td>';
720 }
721 else {
722 foreach($row as $index => $data) {
723 if($index >= $colcount) {
724 break;
725 }
726 $column = $colbyindex[$index];
554906f6 727 echo '<td class="cell c'.$index.$this->column_class[$column].'"'.$this->make_styles_string($this->column_style[$column]).'>';
7270d81f 728 if(empty($this->sess->collapse[$column])) {
729 if($this->column_suppress[$column] && $suppress_lastrow !== NULL && $suppress_lastrow[$index] === $data) {
730 echo '&nbsp;';
731 }
732 else {
733 echo $data;
734 }
735 }
736 else {
737 echo '&nbsp;';
738 }
739 echo '</td>';
740 }
741 }
742 echo '</tr>';
743 if($suppress_enabled) {
744 $suppress_lastrow = $row;
745 }
746 }
747 }
748
749 echo '</table>';
750
1e42af27 751 // Paging bar
752 if($this->use_pages) {
e2ba85e9 753 print_paging_bar($this->totalrows, $this->currpage, $this->pagesize, $this->baseurl, $this->request[TABLE_VAR_PAGE]);
1e42af27 754 }
7270d81f 755 }
756
561d406b 757 /**
758 * @todo Document
759 * @return type?
760 */
7270d81f 761 function add_data($row) {
762 if(!$this->setup) {
763 return false;
764 }
765 $this->data[] = $row;
766 }
767
561d406b 768 /**
769 * @todo Document
770 * @return type?
771 */
7270d81f 772 function add_separator() {
773 if(!$this->setup) {
774 return false;
775 }
776 $this->data[] = NULL;
777 }
aad5b0fc 778
779 /**
780 * Add a row of data to the table. This function takes an array with
781 * column names as keys.
782 * It ignores any elements with keys that are not defined as columns. It
783 * puts in empty strings into the row when there is no element in the passed
784 * array corresponding to a column in the table. It puts the row elements in
785 * the proper order.
786 * @param $rowwithkeys array
787 *
788 */
789 function add_data_keyed($rowwithkeys){
790 foreach (array_keys($this->columns) as $column){
791 if (isset($rowwithkeys[$column])){
792 $row [] = $rowwithkeys[$column];
793 } else {
794 $row[] ='';
795 }
796 }
797 $this->add_data($row);
798 }
7270d81f 799
800}
801
802?>