MDL-27962 Fixed typo in tablelib preventing columns from being collapsed
[moodle.git] / lib / tablelib.php
CommitLineData
72fb21b6 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
78bfb562
PS
19 * @package core
20 * @subpackage lib
21 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
72fb21b6 23 */
24
9683db71 25
78bfb562
PS
26defined('MOODLE_INTERNAL') || die();
27
4b7079c1
TH
28/**#@+
29 * These constants relate to the table's handling of URL parameters.
30 */
e2ba85e9 31define('TABLE_VAR_SORT', 1);
32define('TABLE_VAR_HIDE', 2);
33define('TABLE_VAR_SHOW', 3);
34define('TABLE_VAR_IFIRST', 4);
35define('TABLE_VAR_ILAST', 5);
36define('TABLE_VAR_PAGE', 6);
4b7079c1 37/**#@-*/
e2ba85e9 38
4b7079c1
TH
39/**#@+
40 * Constants that indicate whether the paging bar for the table
41 * appears above or below the table.
42 */
43define('TABLE_P_TOP', 1);
44define('TABLE_P_BOTTOM', 2);
45/**#@-*/
20e1f1c0 46
9683db71 47
72fb21b6 48/**
49 * @package moodlecore
50 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
51 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
52 */
7270d81f 53class flexible_table {
54
55 var $uniqueid = NULL;
56 var $attributes = array();
57 var $headers = array();
58 var $columns = array();
59 var $column_style = array();
554906f6 60 var $column_class = array();
7270d81f 61 var $column_suppress = array();
353f3f20 62 var $column_nosort = array('userpic');
7270d81f 63 var $setup = false;
64 var $sess = NULL;
65 var $baseurl = NULL;
e2ba85e9 66 var $request = array();
7270d81f 67
68 var $is_collapsible = false;
69 var $is_sortable = false;
70 var $use_pages = false;
71 var $use_initials = false;
72
73e4f589 73 var $maxsortkeys = 2;
74 var $pagesize = 30;
75 var $currpage = 0;
76 var $totalrows = 0;
a5a3f48a 77 var $sort_default_column = NULL;
78 var $sort_default_order = SORT_ASC;
9cf4a18b 79
20e1f1c0 80 /**
81 * Array of positions in which to display download controls.
82 */
83 var $showdownloadbuttonsat= array(TABLE_P_TOP);
84
1f9ca535 85 /**
86 * @var string Key of field returned by db query that is the id field of the
87 * user table or equivalent.
88 */
89 public $useridfield = 'id';
90
20e1f1c0 91 /**
92 * @var string which download plugin to use. Default '' means none - print
93 * html table with paging. Property set by is_downloading which typically
94 * passes in cleaned data from $
95 */
96 var $download = '';
9cf4a18b 97
20e1f1c0 98 /**
ba9164e5 99 * @var bool whether data is downloadable from table. Determines whether
20e1f1c0 100 * to display download buttons. Set by method downloadable().
101 */
102 var $downloadable = false;
9cf4a18b 103
20e1f1c0 104 /**
105 * @var string which download plugin to use. Default '' means none - print
106 * html table with paging.
107 */
108 var $defaultdownloadformat = 'csv';
9cf4a18b 109
20e1f1c0 110 /**
ba9164e5 111 * @var bool Has start output been called yet?
20e1f1c0 112 */
113 var $started_output = false;
9cf4a18b 114
20e1f1c0 115 var $exportclass = null;
9cf4a18b 116
561d406b 117 /**
118 * Constructor
ba9164e5
TH
119 * @param int $uniqueid all tables have to have a unique id, this is used
120 * as a key when storing table properties like sort order in the session.
561d406b 121 */
a49c17b4 122 function __construct($uniqueid) {
7270d81f 123 $this->uniqueid = $uniqueid;
e2ba85e9 124 $this->request = array(
ba9164e5
TH
125 TABLE_VAR_SORT => 'tsort',
126 TABLE_VAR_HIDE => 'thide',
127 TABLE_VAR_SHOW => 'tshow',
128 TABLE_VAR_IFIRST => 'tifirst',
129 TABLE_VAR_ILAST => 'tilast',
130 TABLE_VAR_PAGE => 'page',
e2ba85e9 131 );
7270d81f 132 }
20e1f1c0 133
a49c17b4
TH
134 /**
135 * Backwards-compatible constructor, so that legacy code subclassing
136 * flexible_table does not break.
137 * @deprecated since Moodle 2.0. Will be removed in Moodle 2.1.
138 */
139 function flexible_table($uniqueid) {
140 debugging('Please update your code to user PHP5-style parent::__construct(...), ' .
141 'not parent::flexible_table(...).');
142 $this->__construct($uniqueid);
143 }
144
20e1f1c0 145 /**
146 * Call this to pass the download type. Use :
147 * $download = optional_param('download', '', PARAM_ALPHA);
148 * To get the download type. We assume that if you call this function with
149 * params that this table's data is downloadable, so we call is_downloadable
150 * for you (even if the param is '', which means no download this time.
151 * Also you can call this method with no params to get the current set
152 * download type.
153 * @param string $download download type. One of csv, tsv, xhtml, ods, etc
154 * @param string $filename filename for downloads without file extension.
155 * @param string $sheettitle title for downloaded data.
156 * @return string download type. One of csv, tsv, xhtml, ods, etc
157 */
9683db71
TH
158 function is_downloading($download = null, $filename='', $sheettitle='') {
159 if ($download!==null) {
20e1f1c0 160 $this->sheettitle = $sheettitle;
161 $this->is_downloadable(true);
162 $this->download = $download;
43ec99aa 163 $this->filename = clean_filename($filename);
164 $this->export_class_instance();
20e1f1c0 165 }
166 return $this->download;
167 }
117bd748 168
ba9164e5
TH
169 /**
170 * Get, and optionally set, the export class.
171 * @param $exportclass (optional) if passed, set the table to use this export class.
172 * @return table_default_export_format_parent the export class in use (after any set).
173 */
174 function export_class_instance($exportclass = null) {
9683db71 175 if (!is_null($exportclass)) {
869309b8 176 $this->started_output = true;
ba9164e5
TH
177 $this->exportclass = $exportclass;
178 $this->exportclass->table = $this;
179 } else if (is_null($this->exportclass) && !empty($this->download)) {
43ec99aa 180 $classname = 'table_'.$this->download.'_export_format';
181 $this->exportclass = new $classname($this);
9683db71 182 if (!$this->exportclass->document_started()) {
43ec99aa 183 $this->exportclass->start_document($this->filename);
184 }
185 }
186 return $this->exportclass;
187 }
117bd748 188
20e1f1c0 189 /**
190 * Probably don't need to call this directly. Calling is_downloading with a
191 * param automatically sets table as downloadable.
9cf4a18b 192 *
ba9164e5 193 * @param bool $downloadable optional param to set whether data from
20e1f1c0 194 * table is downloadable. If ommitted this function can be used to get
195 * current state of table.
ba9164e5 196 * @return bool whether table data is set to be downloadable.
20e1f1c0 197 */
9683db71
TH
198 function is_downloadable($downloadable = null) {
199 if ($downloadable !== null) {
20e1f1c0 200 $this->downloadable = $downloadable;
201 }
202 return $this->downloadable;
203 }
9cf4a18b 204
20e1f1c0 205 /**
206 * Where to show download buttons.
207 * @param array $showat array of postions in which to show download buttons.
208 * Containing TABLE_P_TOP and/or TABLE_P_BOTTOM
209 */
9683db71 210 function show_download_buttons_at($showat) {
20e1f1c0 211 $this->showdownloadbuttonsat = $showat;
212 }
9cf4a18b 213
561d406b 214 /**
9cf4a18b 215 * Sets the is_sortable variable to the given boolean, sort_default_column to
561d406b 216 * the given string, and the sort_default_order to the given integer.
217 * @param bool $bool
218 * @param string $defaultcolumn
219 * @param int $defaultorder
220 * @return void
221 */
a5a3f48a 222 function sortable($bool, $defaultcolumn = NULL, $defaultorder = SORT_ASC) {
7270d81f 223 $this->is_sortable = $bool;
a5a3f48a 224 $this->sort_default_column = $defaultcolumn;
225 $this->sort_default_order = $defaultorder;
7270d81f 226 }
227
353f3f20 228 /**
229 * Do not sort using this column
230 * @param string column name
231 */
232 function no_sorting($column) {
233 $this->column_nosort[] = $column;
234 }
235
236 /**
237 * Is the column sortable?
238 * @param string column name, null means table
239 * @return bool
240 */
9683db71 241 function is_sortable($column = null) {
353f3f20 242 if (empty($column)) {
243 return $this->is_sortable;
244 }
245 if (!$this->is_sortable) {
246 return false;
247 }
248 return !in_array($column, $this->column_nosort);
249 }
9683db71 250
561d406b 251 /**
252 * Sets the is_collapsible variable to the given boolean.
253 * @param bool $bool
254 * @return void
255 */
7270d81f 256 function collapsible($bool) {
257 $this->is_collapsible = $bool;
258 }
259
561d406b 260 /**
261 * Sets the use_pages variable to the given boolean.
262 * @param bool $bool
263 * @return void
264 */
7270d81f 265 function pageable($bool) {
266 $this->use_pages = $bool;
267 }
268
561d406b 269 /**
270 * Sets the use_initials variable to the given boolean.
271 * @param bool $bool
272 * @return void
273 */
7270d81f 274 function initialbars($bool) {
275 $this->use_initials = $bool;
276 }
277
561d406b 278 /**
279 * Sets the pagesize variable to the given integer, the totalrows variable
280 * to the given integer, and the use_pages variable to true.
281 * @param int $perpage
282 * @param int $total
283 * @return void
284 */
7270d81f 285 function pagesize($perpage, $total) {
286 $this->pagesize = $perpage;
287 $this->totalrows = $total;
288 $this->use_pages = true;
289 }
290
561d406b 291 /**
292 * Assigns each given variable in the array to the corresponding index
293 * in the request class variable.
294 * @param array $variables
295 * @return void
296 */
e2ba85e9 297 function set_control_variables($variables) {
9683db71
TH
298 foreach ($variables as $what => $variable) {
299 if (isset($this->request[$what])) {
e2ba85e9 300 $this->request[$what] = $variable;
301 }
302 }
303 }
304
561d406b 305 /**
306 * Gives the given $value to the $attribute index of $this->attributes.
307 * @param string $attribute
308 * @param mixed $value
309 * @return void
310 */
7270d81f 311 function set_attribute($attribute, $value) {
312 $this->attributes[$attribute] = $value;
313 }
314
561d406b 315 /**
9cf4a18b 316 * What this method does is set the column so that if the same data appears in
901e25d4 317 * consecutive rows, then it is not repeated.
9cf4a18b 318 *
901e25d4 319 * For example, in the quiz overview report, the fullname column is set to be suppressed, so
320 * that when one student has made multiple attempts, their name is only printed in the row
321 * for their first attempt.
ba9164e5 322 * @param int $column the index of a column.
561d406b 323 */
7270d81f 324 function column_suppress($column) {
9683db71 325 if (isset($this->column_suppress[$column])) {
7270d81f 326 $this->column_suppress[$column] = true;
327 }
328 }
329
561d406b 330 /**
331 * Sets the given $column index to the given $classname in $this->column_class.
ba9164e5 332 * @param int $column
561d406b 333 * @param string $classname
334 * @return void
335 */
554906f6 336 function column_class($column, $classname) {
9683db71 337 if (isset($this->column_class[$column])) {
554906f6 338 $this->column_class[$column] = ' '.$classname; // This space needed so that classnames don't run together in the HTML
339 }
340 }
341
561d406b 342 /**
343 * Sets the given $column index and $property index to the given $value in $this->column_style.
ba9164e5 344 * @param int $column
561d406b 345 * @param string $property
346 * @param mixed $value
347 * @return void
348 */
7270d81f 349 function column_style($column, $property, $value) {
9683db71 350 if (isset($this->column_style[$column])) {
7270d81f 351 $this->column_style[$column][$property] = $value;
352 }
353 }
354
561d406b 355 /**
20e1f1c0 356 * Sets all columns' $propertys to the given $value in $this->column_style.
ba9164e5 357 * @param int $property
561d406b 358 * @param string $value
359 * @return void
360 */
7270d81f 361 function column_style_all($property, $value) {
9683db71 362 foreach (array_keys($this->columns) as $column) {
7270d81f 363 $this->column_style[$column][$property] = $value;
364 }
365 }
366
561d406b 367 /**
59f392b3
TH
368 * Sets $this->baseurl.
369 * @param moodle_url|string $url the url with params needed to call up this page
561d406b 370 */
7270d81f 371 function define_baseurl($url) {
59f392b3 372 $this->baseurl = new moodle_url($url);
7270d81f 373 }
374
561d406b 375 /**
20e1f1c0 376 * @param array $columns an array of identifying names for columns. If
377 * columns are sorted then column names must correspond to a field in sql.
561d406b 378 */
7270d81f 379 function define_columns($columns) {
380 $this->columns = array();
381 $this->column_style = array();
554906f6 382 $this->column_class = array();
7270d81f 383 $colnum = 0;
384
9683db71 385 foreach ($columns as $column) {
7270d81f 386 $this->columns[$column] = $colnum++;
387 $this->column_style[$column] = array();
554906f6 388 $this->column_class[$column] = '';
7270d81f 389 $this->column_suppress[$column] = false;
390 }
391 }
392
561d406b 393 /**
20e1f1c0 394 * @param array $headers numerical keyed array of displayed string titles
395 * for each column.
561d406b 396 */
7270d81f 397 function define_headers($headers) {
398 $this->headers = $headers;
399 }
9cf4a18b 400
561d406b 401 /**
20e1f1c0 402 * Must be called after table is defined. Use methods above first. Cannot
403 * use functions below till after calling this method.
561d406b 404 * @return type?
405 */
7270d81f 406 function setup() {
407 global $SESSION, $CFG;
408
9683db71 409 if (empty($this->columns) || empty($this->uniqueid)) {
7270d81f 410 return false;
411 }
1e42af27 412
0ce8ba58 413 if (!isset($SESSION->flextable)) {
414 $SESSION->flextable = array();
7270d81f 415 }
1e42af27 416
9683db71 417 if (!isset($SESSION->flextable[$this->uniqueid])) {
0ce8ba58 418 $SESSION->flextable[$this->uniqueid] = new stdClass;
419 $SESSION->flextable[$this->uniqueid]->uniqueid = $this->uniqueid;
420 $SESSION->flextable[$this->uniqueid]->collapse = array();
421 $SESSION->flextable[$this->uniqueid]->sortby = array();
422 $SESSION->flextable[$this->uniqueid]->i_first = '';
423 $SESSION->flextable[$this->uniqueid]->i_last = '';
424 }
425
426 $this->sess = &$SESSION->flextable[$this->uniqueid];
7270d81f 427
59f392b3
TH
428 if (($showcol = optional_param($this->request[TABLE_VAR_SHOW], '', PARAM_ALPHANUMEXT)) &&
429 isset($this->columns[$showcol])) {
430 $this->sess->collapse[$showcol] = false;
431
8b05c5e2 432 } else if (($hidecol = optional_param($this->request[TABLE_VAR_HIDE], '', PARAM_ALPHANUMEXT)) &&
59f392b3
TH
433 isset($this->columns[$hidecol])) {
434 $this->sess->collapse[$hidecol] = true;
435 if (array_key_exists($hidecol, $this->sess->sortby)) {
436 unset($this->sess->sortby[$hidecol]);
7270d81f 437 }
438 }
1e42af27 439
7270d81f 440 // Now, update the column attributes for collapsed columns
9683db71
TH
441 foreach (array_keys($this->columns) as $column) {
442 if (!empty($this->sess->collapse[$column])) {
7270d81f 443 $this->column_style[$column]['width'] = '10px';
444 }
445 }
446
59f392b3
TH
447 if (($sortcol = optional_param($this->request[TABLE_VAR_SORT], '', PARAM_ALPHANUMEXT)) &&
448 $this->is_sortable($sortcol) && empty($this->sess->collapse[$sortcol]) &&
449 (isset($this->columns[$sortcol]) || in_array($sortcol, array('firstname', 'lastname')) && isset($this->columns['fullname']))) {
450
451 if (array_key_exists($sortcol, $this->sess->sortby)) {
452 // This key already exists somewhere. Change its sortorder and bring it to the top.
453 $sortorder = $this->sess->sortby[$sortcol] == SORT_ASC ? SORT_DESC : SORT_ASC;
454 unset($this->sess->sortby[$sortcol]);
455 $this->sess->sortby = array_merge(array($sortcol => $sortorder), $this->sess->sortby);
456 } else {
457 // Key doesn't exist, so just add it to the beginning of the array, ascending order
458 $this->sess->sortby = array_merge(array($sortcol => SORT_ASC), $this->sess->sortby);
7270d81f 459 }
59f392b3
TH
460
461 // Finally, make sure that no more than $this->maxsortkeys are present into the array
462 $this->sess->sortby = array_slice($this->sess->sortby, 0, $this->maxsortkeys);
7270d81f 463 }
464
a5a3f48a 465 // If we didn't sort just now, then use the default sort order if one is defined and the column exists
9683db71 466 if (empty($this->sess->sortby) && !empty($this->sort_default_column)) {
a5a3f48a 467 $this->sess->sortby = array ($this->sort_default_column => ($this->sort_default_order == SORT_DESC ? SORT_DESC : SORT_ASC));
468 }
469
59f392b3
TH
470 $ilast = optional_param($this->request[TABLE_VAR_ILAST], null, PARAM_RAW);
471 if (!is_null($ilast) && ($ilast ==='' || strpos(get_string('alphabet', 'langconfig'), $ilast) !== false)) {
472 $this->sess->i_last = $ilast;
7270d81f 473 }
474
59f392b3
TH
475 $ifirst = optional_param($this->request[TABLE_VAR_IFIRST], null, PARAM_RAW);
476 if (!is_null($ifirst) && ($ifirst === '' || strpos(get_string('alphabet', 'langconfig'), $ifirst) !== false)) {
477 $this->sess->i_first = $ifirst;
7270d81f 478 }
479
9683db71 480 if (empty($this->baseurl)) {
59f392b3
TH
481 debugging('You should set baseurl when using flexible_table.');
482 global $PAGE;
483 $this->baseurl = $PAGE->url;
7270d81f 484 }
485
92ebcabe 486 $this->currpage = optional_param($this->request[TABLE_VAR_PAGE], 0, PARAM_INT);
7270d81f 487 $this->setup = true;
d1b523b7 488
59f392b3 489 // Always introduce the "flexible" class for the table if not specified
d1b523b7 490 if (empty($this->attributes)) {
491 $this->attributes['class'] = 'flexible';
d1b523b7 492 } else if (!isset($this->attributes['class'])) {
493 $this->attributes['class'] = 'flexible';
d1b523b7 494 } else if (!in_array('flexible', explode(' ', $this->attributes['class']))) {
495 $this->attributes['class'] = trim('flexible ' . $this->attributes['class']);
496 }
9baf2670
TH
497 }
498
499 /**
500 * Get the order by clause from the session, for the table with id $uniqueid.
501 * @param string $uniqueid the identifier for a table.
502 * @return SQL fragment that can be used in an ORDER BY clause.
503 */
504 public static function get_sort_for_table($uniqueid) {
505 global $SESSION;
9683db71 506 if (empty($SESSION->flextable[$uniqueid])) {
9baf2670
TH
507 return '';
508 }
d1b523b7 509
9baf2670
TH
510 $sess = &$SESSION->flextable[$uniqueid];
511 if (empty($sess->sortby)) {
512 return '';
513 }
514
515 return self::construct_order_by($sess->sortby);
7270d81f 516 }
517
561d406b 518 /**
9baf2670
TH
519 * Prepare an an order by clause from the list of columns to be sorted.
520 * @param array $cols column name => SORT_ASC or SORT_DESC
521 * @return SQL fragment that can be used in an ORDER BY clause.
561d406b 522 */
9baf2670
TH
523 public static function construct_order_by($cols) {
524 $bits = array();
525
9683db71 526 foreach ($cols as $column => $order) {
9baf2670
TH
527 if ($order == SORT_ASC) {
528 $bits[] = $column . ' ASC';
529 } else {
530 $bits[] = $column . ' DESC';
9013e4ad 531 }
9013e4ad 532 }
9baf2670
TH
533
534 return implode(', ', $bits);
535 }
536
537 /**
538 * @return SQL fragment that can be used in an ORDER BY clause.
539 */
540 public function get_sql_sort() {
541 return self::construct_order_by($this->get_sort_columns());
542 }
543
544 /**
545 * Get the columns to sort by, in the form required by {@link construct_order_by()}.
546 * @return array column name => SORT_... constant.
547 */
548 public function get_sort_columns() {
549 if (!$this->setup) {
550 throw new coding_exception('Cannot call get_sort_columns until you have called setup.');
7270d81f 551 }
9013e4ad 552
9baf2670
TH
553 if (empty($this->sess->sortby)) {
554 return array();
7270d81f 555 }
9baf2670 556
e81eed54
TH
557 foreach ($this->sess->sortby as $column => $notused) {
558 if (isset($this->columns[$column])) {
559 continue; // This column is OK.
560 }
561 if (in_array($column, array('firstname', 'lastname')) &&
562 isset($this->columns['fullname'])) {
563 continue; // This column is OK.
564 }
565 // This column is not OK.
566 unset($this->sess->sortby[$column]);
567 }
568
9baf2670 569 return $this->sess->sortby;
7270d81f 570 }
571
561d406b 572 /**
ba9164e5 573 * @return int the offset for LIMIT clause of SQL
561d406b 574 */
7270d81f 575 function get_page_start() {
9683db71 576 if (!$this->use_pages) {
7270d81f 577 return '';
578 }
579 return $this->currpage * $this->pagesize;
580 }
581
561d406b 582 /**
ba9164e5 583 * @return int the pagesize for LIMIT clause of SQL
561d406b 584 */
7270d81f 585 function get_page_size() {
9683db71 586 if (!$this->use_pages) {
7270d81f 587 return '';
588 }
589 return $this->pagesize;
590 }
591
561d406b 592 /**
e81eed54 593 * @return string sql to add to where statement.
561d406b 594 */
7270d81f 595 function get_sql_where() {
7e60297f 596 global $DB;
7270d81f 597
20a79b43
PS
598 $conditions = array();
599 $params = array();
600
ee70439a
PS
601 if (isset($this->columns['fullname'])) {
602 static $i = 0;
603 $i++;
604
605 if (!empty($this->sess->i_first)) {
606 $conditions[] = $DB->sql_like('firstname', ':ifirstc'.$i, false, false);
607 $params['ifirstc'.$i] = $this->sess->i_first.'%';
608 }
609 if (!empty($this->sess->i_last)) {
610 $conditions[] = $DB->sql_like('lastname', ':ilastc'.$i, false, false);
611 $params['ilastc'.$i] = $this->sess->i_last.'%';
612 }
7270d81f 613 }
614
20a79b43 615 return array(implode(" AND ", $conditions), $params);
7270d81f 616 }
617
561d406b 618 /**
9cf4a18b 619 * Add a row of data to the table. This function takes an array with
620 * column names as keys.
20e1f1c0 621 * It ignores any elements with keys that are not defined as columns. It
622 * puts in empty strings into the row when there is no element in the passed
623 * array corresponding to a column in the table. It puts the row elements in
624 * the proper order.
625 * @param $rowwithkeys array
c7ecf78e 626 * @param string $classname CSS class name to add to this row's tr tag.
20e1f1c0 627 */
9683db71 628 function add_data_keyed($rowwithkeys, $classname = '') {
c7ecf78e 629 $this->add_data($this->get_row_from_keyed($rowwithkeys), $classname);
20e1f1c0 630 }
9cf4a18b 631
20e1f1c0 632 /**
633 * Add a seperator line to table.
634 */
635 function add_separator() {
9683db71 636 if (!$this->setup) {
20e1f1c0 637 return false;
638 }
639 $this->add_data(NULL);
640 }
9cf4a18b 641
20e1f1c0 642 /**
643 * This method actually directly echoes the row passed to it now or adds it
644 * to the download. If this is the first row and start_output has not
645 * already been called this method also calls start_output to open the table
646 * or send headers for the downloaded.
647 * Can be used as before. print_html now calls finish_html to close table.
9cf4a18b 648 *
20e1f1c0 649 * @param array $row a numerically keyed row of data to add to the table.
c7ecf78e 650 * @param string $classname CSS class name to add to this row's tr tag.
ba9164e5 651 * @return bool success.
20e1f1c0 652 */
c7ecf78e 653 function add_data($row, $classname = '') {
9683db71 654 if (!$this->setup) {
20e1f1c0 655 return false;
656 }
9683db71 657 if (!$this->started_output) {
20e1f1c0 658 $this->start_output();
659 }
9683db71
TH
660 if ($this->exportclass!==null) {
661 if ($row === null) {
20e1f1c0 662 $this->exportclass->add_seperator();
663 } else {
664 $this->exportclass->add_data($row);
665 }
666 } else {
c7ecf78e 667 $this->print_row($row, $classname);
20e1f1c0 668 }
669 return true;
670 }
671
20e1f1c0 672 /**
673 * You should call this to finish outputting the table data after adding
674 * data to the table with add_data or add_data_keyed.
9cf4a18b 675 *
20e1f1c0 676 */
9683db71
TH
677 function finish_output($closeexportclassdoc = true) {
678 if ($this->exportclass!==null) {
43ec99aa 679 $this->exportclass->finish_table();
9683db71 680 if ($closeexportclassdoc) {
43ec99aa 681 $this->exportclass->finish_document();
682 }
9683db71 683 } else {
20e1f1c0 684 $this->finish_html();
685 }
686 }
9cf4a18b 687
20e1f1c0 688 /**
689 * Hook that can be overridden in child classes to wrap a table in a form
690 * for example. Called only when there is data to display and not
691 * downloading.
692 */
9683db71 693 function wrap_html_start() {
20e1f1c0 694 }
695
696 /**
697 * Hook that can be overridden in child classes to wrap a table in a form
698 * for example. Called only when there is data to display and not
699 * downloading.
700 */
9683db71 701 function wrap_html_finish() {
20e1f1c0 702 }
9cf4a18b 703
1f9ca535 704 /**
705 *
706 * @param array $row row of data from db used to make one row of the table.
707 * @return array one row for the table, added using add_data_keyed method.
708 */
9683db71 709 function format_row($row) {
1f9ca535 710 $formattedrow = array();
9683db71 711 foreach (array_keys($this->columns) as $column) {
1f9ca535 712 $colmethodname = 'col_'.$column;
9683db71 713 if (method_exists($this, $colmethodname)) {
1f9ca535 714 $formattedcolumn = $this->$colmethodname($row);
715 } else {
716 $formattedcolumn = $this->other_cols($column, $row);
9683db71 717 if ($formattedcolumn===NULL) {
1f9ca535 718 $formattedcolumn = $row->$column;
719 }
720 }
721 $formattedrow[$column] = $formattedcolumn;
722 }
723 return $formattedrow;
724 }
725
726 /**
727 * Fullname is treated as a special columname in tablelib and should always
728 * be treated the same as the fullname of a user.
729 * @uses $this->useridfield if the userid field is not expected to be id
730 * then you need to override $this->useridfield to point at the correct
731 * field for the user id.
732 *
733 */
9683db71 734 function col_fullname($row) {
1f9ca535 735 global $COURSE, $CFG;
1f9ca535 736
9683db71 737 if (!$this->download) {
abf72969
TH
738 $profileurl = new moodle_url('/user/profile.php', array('id' => $row->{$this->useridfield}));
739 if ($COURSE->id != SITEID) {
740 $profileurl->param('course', $COURSE->id);
03d9401e 741 }
abf72969
TH
742 return html_writer::link($profileurl, fullname($row));
743
1f9ca535 744 } else {
745 return fullname($row);
746 }
747 }
748
749 /**
750 * You can override this method in a child class. See the description of
751 * build_table which calls this method.
752 */
9683db71 753 function other_cols($column, $row) {
1f9ca535 754 return NULL;
755 }
756
ef27e742 757 /**
758 * Used from col_* functions when text is to be displayed. Does the
759 * right thing - either converts text to html or strips any html tags
760 * depending on if we are downloading and what is the download type. Params
761 * are the same as format_text function in weblib.php but some default
762 * options are changed.
763 */
9683db71
TH
764 function format_text($text, $format=FORMAT_MOODLE, $options=NULL, $courseid=NULL) {
765 if (!$this->is_downloading()) {
766 if (is_null($options)) {
ef27e742 767 $options = new stdClass;
768 }
769 //some sensible defaults
9683db71 770 if (!isset($options->para)) {
ef27e742 771 $options->para = false;
772 }
9683db71 773 if (!isset($options->newlines)) {
ef27e742 774 $options->newlines = false;
775 }
776 if (!isset($options->smiley)) {
777 $options->smiley = false;
778 }
779 if (!isset($options->filter)) {
780 $options->filter = false;
781 }
782 return format_text($text, $format, $options);
783 } else {
784 $eci =& $this->export_class_instance();
785 return $eci->format_text($text, $format, $options, $courseid);
786 }
787 }
20e1f1c0 788 /**
789 * This method is deprecated although the old api is still supported.
790 * @deprecated 1.9.2 - Jun 2, 2008
791 */
792 function print_html() {
9683db71 793 if (!$this->setup) {
20e1f1c0 794 return false;
795 }
796 $this->finish_html();
797 }
798
799 /**
800 * This function is not part of the public api.
801 * @return string initial of first name we are currently filtering by
561d406b 802 */
91bb59f4 803 function get_initial_first() {
9683db71 804 if (!$this->use_initials) {
91bb59f4 805 return NULL;
806 }
807
808 return $this->sess->i_first;
809 }
810
561d406b 811 /**
20e1f1c0 812 * This function is not part of the public api.
813 * @return string initial of last name we are currently filtering by
561d406b 814 */
91bb59f4 815 function get_initial_last() {
9683db71 816 if (!$this->use_initials) {
91bb59f4 817 return NULL;
818 }
819
820 return $this->sess->i_last;
821 }
9cf4a18b 822
ba9164e5
TH
823 /**
824 * Helper function, used by {@link print_initials_bar()} to output one initial bar.
825 * @param array $alpha of letters in the alphabet.
826 * @param string $current the currently selected letter.
827 * @param string $class class name to add to this initial bar.
828 * @param string $title the name to put in front of this initial bar.
829 * @param string $urlvar URL parameter name for this initial.
830 */
6f0d3480
TH
831 protected function print_one_initials_bar($alpha, $current, $class, $title, $urlvar) {
832 echo html_writer::start_tag('div', array('class' => 'initialbar ' . $class)) .
833 $title . ' : ';
834 if ($current) {
835 echo html_writer::link($this->baseurl->out(false, array($urlvar => '')), get_string('all'));
836 } else {
837 echo html_writer::tag('strong', get_string('all'));
838 }
839
840 foreach ($alpha as $letter) {
841 if ($letter === $current) {
842 echo html_writer::tag('strong', $letter);
843 } else {
844 echo html_writer::link($this->baseurl->out(false, array($urlvar => $letter)), $letter);
845 }
846 }
847
848 echo html_writer::end_tag('div');
849 }
850
561d406b 851 /**
20e1f1c0 852 * This function is not part of the public api.
561d406b 853 */
9683db71 854 function print_initials_bar() {
6f0d3480 855 if ((!empty($this->sess->i_last) || !empty($this->sess->i_first) ||$this->use_initials)
db16ac50 856 && isset($this->columns['fullname'])) {
9cf4a18b 857
61bb07c2 858 $alpha = explode(',', get_string('alphabet', 'langconfig'));
9cf4a18b 859
7270d81f 860 // Bar of first initials
9683db71 861 if (!empty($this->sess->i_first)) {
6f0d3480 862 $ifirst = $this->sess->i_first;
7270d81f 863 } else {
6f0d3480 864 $ifirst = '';
7270d81f 865 }
6f0d3480
TH
866 $this->print_one_initials_bar($alpha, $ifirst, 'firstinitial',
867 get_string('firstname'), $this->request[TABLE_VAR_IFIRST]);
9cf4a18b 868
7270d81f 869 // Bar of last initials
9683db71 870 if (!empty($this->sess->i_last)) {
6f0d3480 871 $ilast = $this->sess->i_last;
7270d81f 872 } else {
6f0d3480 873 $ilast = '';
7270d81f 874 }
6f0d3480
TH
875 $this->print_one_initials_bar($alpha, $ilast, 'lastinitial',
876 get_string('lastname'), $this->request[TABLE_VAR_ILAST]);
20e1f1c0 877 }
878 }
7270d81f 879
20e1f1c0 880 /**
881 * This function is not part of the public api.
882 */
9683db71 883 function print_nothing_to_display() {
3c159385 884 global $OUTPUT;
db16ac50 885 $this->print_initials_bar();
7270d81f 886
3c159385 887 echo $OUTPUT->heading(get_string('nothingtodisplay'));
20e1f1c0 888 }
7270d81f 889
20e1f1c0 890 /**
891 * This function is not part of the public api.
892 */
9683db71
TH
893 function get_row_from_keyed($rowwithkeys) {
894 if (is_object($rowwithkeys)) {
20e1f1c0 895 $rowwithkeys = (array)$rowwithkeys;
7270d81f 896 }
f36a3308 897 $row = array();
9683db71
TH
898 foreach (array_keys($this->columns) as $column) {
899 if (isset($rowwithkeys[$column])) {
20e1f1c0 900 $row [] = $rowwithkeys[$column];
901 } else {
902 $row[] ='';
903 }
7270d81f 904 }
20e1f1c0 905 return $row;
906 }
907 /**
908 * This function is not part of the public api.
909 */
9683db71 910 function get_download_menu() {
20e1f1c0 911 $allclasses= get_declared_classes();
912 $exportclasses = array();
9683db71 913 foreach ($allclasses as $class) {
20e1f1c0 914 $matches = array();
9683db71 915 if (preg_match('/^table\_([a-z]+)\_export\_format$/', $class, $matches)) {
20e1f1c0 916 $type = $matches[1];
917 $exportclasses[$type]= get_string("download$type", 'table');
918 }
919 }
920 return $exportclasses;
921 }
9cf4a18b 922
20e1f1c0 923 /**
924 * This function is not part of the public api.
925 */
9683db71 926 function download_buttons() {
9683db71 927 if ($this->is_downloadable() && !$this->is_downloading()) {
20e1f1c0 928 $downloadoptions = $this->get_download_menu();
929 $html = '<form action="'. $this->baseurl .'" method="post">';
930 $html .= '<div class="mdl-align">';
931 $html .= '<input type="submit" value="'.get_string('downloadas', 'table').'"/>';
d776d59e 932 $html .= html_writer::select($downloadoptions, 'download', $this->defaultdownloadformat, false);
20e1f1c0 933 $html .= '</div></form>';
934
935 return $html;
936 } else {
937 return '';
938 }
939 }
940 /**
941 * This function is not part of the public api.
942 * You don't normally need to call this. It is called automatically when
943 * needed when you start adding data to the table.
9cf4a18b 944 *
20e1f1c0 945 */
9683db71 946 function start_output() {
20e1f1c0 947 $this->started_output = true;
9683db71 948 if ($this->exportclass!==null) {
43ec99aa 949 $this->exportclass->start_table($this->sheettitle);
20e1f1c0 950 $this->exportclass->output_headers($this->headers);
951 } else {
952 $this->start_html();
953 $this->print_headers();
954 }
955 }
7270d81f 956
20e1f1c0 957 /**
958 * This function is not part of the public api.
959 */
c7ecf78e 960 function print_row($row, $classname = '') {
20e1f1c0 961 static $suppress_lastrow = NULL;
962 static $oddeven = 1;
c7ecf78e 963 $rowclasses = array('r' . $oddeven);
964 $oddeven = $oddeven ? 0 : 1;
965
966 if ($classname) {
967 $rowclasses[] = $classname;
968 }
969
11a67635 970 echo html_writer::start_tag('tr', array('class' => implode(' ', $rowclasses)));
7270d81f 971
20e1f1c0 972 // If we have a separator, print it
eba40bfe 973 if ($row === NULL) {
c7ecf78e 974 $colcount = count($this->columns);
76dc1e25
TH
975 echo html_writer::tag('td', html_writer::tag('div', '',
976 array('class' => 'tabledivider')), array('colspan' => $colcount));
977
c7ecf78e 978 } else {
979 $colbyindex = array_flip($this->columns);
980 foreach ($row as $index => $data) {
20e1f1c0 981 $column = $colbyindex[$index];
76dc1e25 982
c7ecf78e 983 if (empty($this->sess->collapse[$column])) {
984 if ($this->column_suppress[$column] && $suppress_lastrow !== NULL && $suppress_lastrow[$index] === $data) {
76dc1e25 985 $content = '&nbsp;';
c7ecf78e 986 } else {
76dc1e25 987 $content = $data;
20e1f1c0 988 }
c7ecf78e 989 } else {
76dc1e25 990 $content = '&nbsp;';
20e1f1c0 991 }
76dc1e25
TH
992
993 echo html_writer::tag('td', $content, array(
994 'class' => 'cell c' . $index . $this->column_class[$column],
995 'style' => $this->make_styles_string($this->column_style[$column])));
20e1f1c0 996 }
997 }
c7ecf78e 998
76dc1e25 999 echo html_writer::end_tag('tr');
c7ecf78e 1000
7270d81f 1001 $suppress_enabled = array_sum($this->column_suppress);
c7ecf78e 1002 if ($suppress_enabled) {
20e1f1c0 1003 $suppress_lastrow = $row;
1004 }
1005 }
9683db71 1006
20e1f1c0 1007 /**
1008 * This function is not part of the public api.
1009 */
9683db71 1010 function finish_html() {
25911860 1011 global $OUTPUT;
20e1f1c0 1012 if (!$this->started_output) {
1013 //no data has been added to the table.
1014 $this->print_nothing_to_display();
76dc1e25 1015
20e1f1c0 1016 } else {
76dc1e25 1017 echo html_writer::end_tag('table');
f4136193 1018 echo html_writer::end_tag('div');
20e1f1c0 1019 $this->wrap_html_finish();
76dc1e25 1020
20e1f1c0 1021 // Paging bar
76dc1e25 1022 if(in_array(TABLE_P_BOTTOM, $this->showdownloadbuttonsat)) {
20e1f1c0 1023 echo $this->download_buttons();
1024 }
76dc1e25
TH
1025
1026 if($this->use_pages) {
929d7a83 1027 $pagingbar = new paging_bar($this->totalrows, $this->currpage, $this->pagesize, $this->baseurl);
b28c784f 1028 $pagingbar->pagevar = $this->request[TABLE_VAR_PAGE];
929d7a83 1029 echo $OUTPUT->render($pagingbar);
20e1f1c0 1030 }
1031 }
9cf4a18b 1032 }
76dc1e25 1033
ba9164e5
TH
1034 /**
1035 * Generate the HTML for the collapse/uncollapse icon. This is a helper method
1036 * used by {@link print_headers()}.
1037 * @param string $column the column name, index into various names.
1038 * @param int $index numerical index of the column.
1039 * @return string HTML fragment.
1040 */
76dc1e25
TH
1041 protected function show_hide_link($column, $index) {
1042 global $OUTPUT;
1043 // Some headers contain <br /> tags, do not include in title, hence the
1044 // strip tags.
1045
1046 if (!empty($this->sess->collapse[$column])) {
1047 return html_writer::link($this->baseurl->out(false, array($this->request[TABLE_VAR_SHOW] => $column)),
1048 html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url('t/switch_plus'), 'alt' => get_string('show'))),
1049 array('title' => get_string('show') . ' ' . strip_tags($this->headers[$index])));
1050
1051 } else if ($this->headers[$index] !== NULL) {
1052 return html_writer::link($this->baseurl->out(false, array($this->request[TABLE_VAR_HIDE] => $column)),
1053 html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url('t/switch_minus'), 'alt' => get_string('hide'))),
1054 array('title' => get_string('hide') . ' ' . strip_tags($this->headers[$index])));
1055 }
1056 }
1057
20e1f1c0 1058 /**
1059 * This function is not part of the public api.
1060 */
9683db71 1061 function print_headers() {
f2a1963c 1062 global $CFG, $OUTPUT;
7270d81f 1063
76dc1e25 1064 echo html_writer::start_tag('tr');
9683db71 1065 foreach ($this->columns as $column => $index) {
7270d81f 1066
76dc1e25 1067 $icon_hide = '';
9683db71 1068 if ($this->is_collapsible) {
76dc1e25 1069 $icon_hide = $this->show_hide_link($column, $index);
7270d81f 1070 }
1071
73e4f589 1072 $primary_sort_column = '';
1073 $primary_sort_order = '';
9683db71 1074 if (reset($this->sess->sortby)) {
73e4f589 1075 $primary_sort_column = key($this->sess->sortby);
1076 $primary_sort_order = current($this->sess->sortby);
1077 }
1078
9683db71 1079 switch ($column) {
7270d81f 1080
1081 case 'fullname':
9683db71 1082 if ($this->is_sortable($column)) {
76dc1e25
TH
1083 $firstnamesortlink = $this->sort_link(get_string('firstname'),
1084 'firstname', $primary_sort_column === 'firstname', $primary_sort_order);
1085
1086 $lastnamesortlink = $this->sort_link(get_string('lastname'),
1087 'lastname', $primary_sort_column === 'lastname', $primary_sort_order);
78bfb562 1088
365a5941 1089 $override = new stdClass();
4eb95309
RW
1090 $override->firstname = 'firstname';
1091 $override->lastname = 'lastname';
1092 $fullnamelanguage = get_string('fullnamedisplay', '', $override);
1093
1094 if (($CFG->fullnamedisplay == 'firstname lastname') or
1095 ($CFG->fullnamedisplay == 'firstname') or
1096 ($CFG->fullnamedisplay == 'language' and $fullnamelanguage == 'firstname lastname' )) {
76dc1e25 1097 $this->headers[$index] = $firstnamesortlink . ' / ' . $lastnamesortlink;
4eb95309 1098 } else {
76dc1e25 1099 $this->headers[$index] = $lastnamesortlink . ' / ' . $firstnamesortlink;
78bfb562 1100 }
7270d81f 1101 }
1102 break;
1103
1d40c70b 1104 case 'userpic':
9cf4a18b 1105 // do nothing, do not display sortable links
1d40c70b 1106 break;
1107
7270d81f 1108 default:
9683db71 1109 if ($this->is_sortable($column)) {
76dc1e25
TH
1110 $this->headers[$index] = $this->sort_link($this->headers[$index],
1111 $column, $primary_sort_column == $column, $primary_sort_order);
7270d81f 1112 }
1113 }
1e42af27 1114
76dc1e25
TH
1115 $attributes = array(
1116 'class' => 'header c' . $index . $this->column_class[$column],
1117 'scope' => 'col',
1118 );
9683db71 1119 if ($this->headers[$index] === NULL) {
76dc1e25
TH
1120 $content = '&nbsp;';
1121 } else if (!empty($this->sess->collapse[$column])) {
1122 $content = $icon_hide;
9683db71 1123 } else {
76dc1e25
TH
1124 if (is_array($this->column_style[$column])) {
1125 $attributes['style'] = $this->make_styles_string($this->column_style[$column]);
1126 }
1127 $content = $this->headers[$index] . html_writer::tag('div',
1128 $icon_hide, array('class' => 'commands'));
7270d81f 1129 }
76dc1e25
TH
1130 echo html_writer::tag('th', $content, $attributes);
1131 }
1132
1133 echo html_writer::end_tag('tr');
1134 }
1135
ba9164e5
TH
1136 /**
1137 * Generate the HTML for the sort icon. This is a helper method used by {@link sort_link()}.
1138 * @param bool $isprimary whether an icon is needed (it is only needed for the primary sort column.)
1139 * @param int $order SORT_ASC or SORT_DESC
1140 * @return string HTML fragment.
1141 */
76dc1e25
TH
1142 protected function sort_icon($isprimary, $order) {
1143 global $OUTPUT;
7270d81f 1144
76dc1e25
TH
1145 if (!$isprimary) {
1146 return '';
1147 }
1148
1149 if ($order == SORT_ASC) {
1150 return html_writer::empty_tag('img',
1151 array('src' => $OUTPUT->pix_url('t/down'), 'alt' => get_string('asc')));
1152 } else {
1153 return html_writer::empty_tag('img',
1154 array('src' => $OUTPUT->pix_url('t/up'), 'alt' => get_string('desc')));
1155 }
1156 }
1157
ba9164e5
TH
1158 /**
1159 * Generate the correct tool tip for changing the sort order. This is a
1160 * helper method used by {@link sort_link()}.
1161 * @param bool $isprimary whether the is column is the current primary sort column.
1162 * @param int $order SORT_ASC or SORT_DESC
1163 * @return string the correct title.
1164 */
76dc1e25
TH
1165 protected function sort_order_name($isprimary, $order) {
1166 if ($isprimary && $order != SORT_ASC) {
1167 return get_string('desc');
1168 } else {
1169 return get_string('asc');
7270d81f 1170 }
76dc1e25
TH
1171 }
1172
ba9164e5
TH
1173 /**
1174 * Generate the HTML for the sort link. This is a helper method used by {@link print_headers()}.
1175 * @param string $text the text for the link.
1176 * @param string $column the column name, may be a fake column like 'firstname' or a real one.
1177 * @param bool $isprimary whether the is column is the current primary sort column.
1178 * @param int $order SORT_ASC or SORT_DESC
1179 * @return string HTML fragment.
1180 */
76dc1e25
TH
1181 protected function sort_link($text, $column, $isprimary, $order) {
1182 return html_writer::link($this->baseurl->out(false,
1183 array($this->request[TABLE_VAR_SORT] => $column)),
1184 $text . get_accesshide(get_string('sortby') . ' ' .
1185 $text . ' ' . $this->sort_order_name($isprimary, $order))) . ' ' .
1186 $this->sort_icon($isprimary, $order);
20e1f1c0 1187 }
9cf4a18b 1188
20e1f1c0 1189 /**
1190 * This function is not part of the public api.
1191 */
9683db71 1192 function start_html() {
b28c784f 1193 global $OUTPUT;
20e1f1c0 1194 // Do we need to print initial bars?
1195 $this->print_initials_bar();
7270d81f 1196
1e42af27 1197 // Paging bar
9683db71 1198 if ($this->use_pages) {
929d7a83 1199 $pagingbar = new paging_bar($this->totalrows, $this->currpage, $this->pagesize, $this->baseurl);
b28c784f 1200 $pagingbar->pagevar = $this->request[TABLE_VAR_PAGE];
929d7a83 1201 echo $OUTPUT->render($pagingbar);
1e42af27 1202 }
9cf4a18b 1203
9683db71 1204 if (in_array(TABLE_P_TOP, $this->showdownloadbuttonsat)) {
20e1f1c0 1205 echo $this->download_buttons();
1206 }
9cf4a18b 1207
20e1f1c0 1208 $this->wrap_html_start();
1209 // Start of main data table
1210
76dc1e25
TH
1211 echo html_writer::start_tag('div', array('class' => 'no-overflow'));
1212 echo html_writer::start_tag('table', $this->attributes);
9cf4a18b 1213
7270d81f 1214 }
1215
561d406b 1216 /**
20e1f1c0 1217 * This function is not part of the public api.
76dc1e25
TH
1218 * @param array $styles CSS-property => value
1219 * @return string values suitably to go in a style="" attribute in HTML.
561d406b 1220 */
76dc1e25 1221 function make_styles_string($styles) {
9683db71 1222 if (empty($styles)) {
76dc1e25 1223 return null;
20e1f1c0 1224 }
1225
76dc1e25
TH
1226 $string = '';
1227 foreach($styles as $property => $value) {
1228 $string .= $property . ':' . $value . ';';
7270d81f 1229 }
20e1f1c0 1230 return $string;
1231 }
1232}
1233
9683db71 1234
72fb21b6 1235/**
1236 * @package moodlecore
1237 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1238 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1239 */
9683db71 1240class table_sql extends flexible_table {
9cf4a18b 1241
1242 public $countsql = NULL;
1243 public $countparams = NULL;
20e1f1c0 1244 /**
9cf4a18b 1245 * @var object sql for querying db. Has fields 'fields', 'from', 'where', 'params'.
20e1f1c0 1246 */
9cf4a18b 1247 public $sql = NULL;
20e1f1c0 1248 /**
1249 * @var array Data fetched from the db.
1250 */
9cf4a18b 1251 public $rawdata = NULL;
1252
20e1f1c0 1253 /**
ba9164e5 1254 * @var bool Overriding default for this.
20e1f1c0 1255 */
9cf4a18b 1256 public $is_sortable = true;
20e1f1c0 1257 /**
ba9164e5 1258 * @var bool Overriding default for this.
20e1f1c0 1259 */
9cf4a18b 1260 public $is_collapsible = true;
1261
20e1f1c0 1262 /**
1263 * @param string $uniqueid a string identifying this table.Used as a key in
1264 * session vars.
1265 */
a49c17b4
TH
1266 function __construct($uniqueid) {
1267 parent::__construct($uniqueid);
20e1f1c0 1268 // some sensible defaults
1269 $this->set_attribute('cellspacing', '0');
1270 $this->set_attribute('class', 'generaltable generalbox');
7270d81f 1271 }
9cf4a18b 1272
a49c17b4
TH
1273 /**
1274 * Backwards-compatible constructor, so that legacy code subclassing
1275 * table_sql does not break.
1276 * @deprecated since Moodle 2.0. Will be removed in Moodle 2.1.
1277 */
1278 function table_sql($uniqueid) {
1279 debugging('Please update your code to user PHP5-style parent::__construct(...), ' .
1280 'not parent::table_sql(...).');
1281 $this->__construct($uniqueid);
1282 }
1283
20e1f1c0 1284 /**
1285 * Take the data returned from the db_query and go through all the rows
1286 * processing each col using either col_{columnname} method or other_cols
1287 * method or if other_cols returns NULL then put the data straight into the
1288 * table.
1289 */
9683db71
TH
1290 function build_table() {
1291 if ($this->rawdata) {
1292 foreach ($this->rawdata as $row) {
20e1f1c0 1293 $formattedrow = $this->format_row($row);
b08fb128
TH
1294 $this->add_data_keyed($formattedrow,
1295 $this->get_row_class($row));
20e1f1c0 1296 }
1297 }
1298 }
1299
b08fb128
TH
1300 /**
1301 * Get any extra classes names to add to this row in the HTML.
1302 * @param $row array the data for this row.
1303 * @return string added to the class="" attribute of the tr.
1304 */
1305 function get_row_class($row) {
1306 return '';
1307 }
1308
20e1f1c0 1309 /**
1310 * This is only needed if you want to use different sql to count rows.
1311 * Used for example when perhaps all db JOINS are not needed when counting
1312 * records. You don't need to call this function the count_sql
1313 * will be generated automatically.
9cf4a18b 1314 *
20e1f1c0 1315 * We need to count rows returned by the db seperately to the query itself
1316 * as we need to know how many pages of data we have to display.
1317 */
9683db71 1318 function set_count_sql($sql, array $params = NULL) {
20e1f1c0 1319 $this->countsql = $sql;
9cf4a18b 1320 $this->countparams = $params;
20e1f1c0 1321 }
9cf4a18b 1322
20e1f1c0 1323 /**
1324 * Set the sql to query the db. Query will be :
1325 * SELECT $fields FROM $from WHERE $where
1326 * Of course you can use sub-queries, JOINS etc. by putting them in the
1327 * appropriate clause of the query.
1328 */
9683db71 1329 function set_sql($fields, $from, $where, array $params = NULL) {
365a5941 1330 $this->sql = new stdClass();
20e1f1c0 1331 $this->sql->fields = $fields;
1332 $this->sql->from = $from;
1333 $this->sql->where = $where;
9cf4a18b 1334 $this->sql->params = $params;
20e1f1c0 1335 }
9cf4a18b 1336
20e1f1c0 1337 /**
1338 * Query the db. Store results in the table object for use by build_table.
9cf4a18b 1339 *
ba9164e5
TH
1340 * @param int $pagesize size of page for paginated displayed table.
1341 * @param bool $useinitialsbar do you want to use the initials bar. Bar
20e1f1c0 1342 * will only be used if there is a fullname column defined for the table.
1343 */
9683db71 1344 function query_db($pagesize, $useinitialsbar=true) {
9cf4a18b 1345 global $DB;
20e1f1c0 1346 if (!$this->is_downloading()) {
9683db71 1347 if ($this->countsql === NULL) {
20e1f1c0 1348 $this->countsql = 'SELECT COUNT(1) FROM '.$this->sql->from.' WHERE '.$this->sql->where;
71811083 1349 $this->countparams = $this->sql->params;
20e1f1c0 1350 }
d8a3b87c 1351 $grandtotal = $DB->count_records_sql($this->countsql, $this->countparams);
20e1f1c0 1352 if ($useinitialsbar && !$this->is_downloading()) {
d8a3b87c 1353 $this->initialbars($grandtotal > $pagesize);
20e1f1c0 1354 }
9cf4a18b 1355
20a79b43
PS
1356 list($wsql, $wparams) = $this->get_sql_where();
1357 if ($wsql) {
1358 $this->countsql .= ' AND '.$wsql;
23040521 1359 $this->countparams = array_merge($this->countparams, $wparams);
20a79b43
PS
1360
1361 $this->sql->where .= ' AND '.$wsql;
1362 $this->sql->params = array_merge($this->sql->params, $wparams);
1363
0fa4107d 1364 $total = $DB->count_records_sql($this->countsql, $this->countparams);
1365 } else {
d8a3b87c 1366 $total = $grandtotal;
20e1f1c0 1367 }
20e1f1c0 1368
20e1f1c0 1369 $this->pagesize($pagesize, $total);
1370 }
1371
1372 // Fetch the attempts
1373 $sort = $this->get_sql_sort();
e81eed54
TH
1374 if ($sort) {
1375 $sort = "ORDER BY $sort";
1376 }
1377 $sql = "SELECT
1378 {$this->sql->fields}
1379 FROM {$this->sql->from}
1380 WHERE {$this->sql->where}
1381 {$sort}";
1382
20e1f1c0 1383 if (!$this->is_downloading()) {
9cf4a18b 1384 $this->rawdata = $DB->get_records_sql($sql, $this->sql->params, $this->get_page_start(), $this->get_page_size());
20e1f1c0 1385 } else {
9cf4a18b 1386 $this->rawdata = $DB->get_records_sql($sql, $this->sql->params);
20e1f1c0 1387 }
1388 }
9cf4a18b 1389
20e1f1c0 1390 /**
1391 * Convenience method to call a number of methods for you to display the
1392 * table.
1393 */
9683db71 1394 function out($pagesize, $useinitialsbar, $downloadhelpbutton='') {
9cf4a18b 1395 global $DB;
9683db71 1396 if (!$this->columns) {
9cf4a18b 1397 $onerow = $DB->get_record_sql("SELECT {$this->sql->fields} FROM {$this->sql->from} WHERE {$this->sql->where}", $this->sql->params);
20e1f1c0 1398 //if columns is not set then define columns as the keys of the rows returned
1399 //from the db.
1400 $this->define_columns(array_keys((array)$onerow));
1401 $this->define_headers(array_keys((array)$onerow));
1402 }
1403 $this->setup();
1404 $this->query_db($pagesize, $useinitialsbar);
1405 $this->build_table();
1406 $this->finish_output();
1407 }
1408}
1409
9683db71 1410
72fb21b6 1411/**
1412 * @package moodlecore
1413 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1414 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1415 */
9683db71 1416class table_default_export_format_parent {
20e1f1c0 1417 /**
1418 * @var flexible_table or child class reference pointing to table class
1419 * object from which to export data.
1420 */
1421 var $table;
117bd748 1422
43ec99aa 1423 /**
ba9164e5 1424 * @var bool output started. Keeps track of whether any output has been
43ec99aa 1425 * started yet.
1426 */
1427 var $documentstarted = false;
9683db71 1428 function table_default_export_format_parent(&$table) {
20e1f1c0 1429 $this->table =& $table;
1430 }
117bd748 1431
9683db71 1432 function set_table(&$table) {
43ec99aa 1433 $this->table =& $table;
1434 }
20e1f1c0 1435
1436 function add_data($row) {
1437 return false;
1438 }
9683db71 1439
20e1f1c0 1440 function add_seperator() {
1441 return false;
1442 }
9683db71
TH
1443
1444 function document_started() {
43ec99aa 1445 return $this->documentstarted;
20e1f1c0 1446 }
ef27e742 1447 /**
1448 * Given text in a variety of format codings, this function returns
1449 * the text as safe HTML or as plain text dependent on what is appropriate
1450 * for the download format. The default removes all tags.
1451 */
9683db71 1452 function format_text($text, $format=FORMAT_MOODLE, $options=NULL, $courseid=NULL) {
ef27e742 1453 //use some whitespace to indicate where there was some line spacing.
1454 $text = str_replace(array('</p>', "\n", "\r"), ' ', $text);
1455 return strip_tags($text);
1456 }
7270d81f 1457}
1458
9683db71 1459
72fb21b6 1460/**
1461 * @package moodlecore
1462 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1463 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1464 */
9683db71 1465class table_spreadsheet_export_format_parent extends table_default_export_format_parent {
20e1f1c0 1466 var $rownum;
1467 var $workbook;
1468 var $worksheet;
1469 /**
1470 * @var object format object - format for normal table cells
1471 */
1472 var $formatnormal;
1473 /**
1474 * @var object format object - format for header table cells
1475 */
1476 var $formatheaders;
1477
9cf4a18b 1478 /**
20e1f1c0 1479 * should be overriden in child class.
1480 */
1481 var $fileextension;
9cf4a18b 1482
20e1f1c0 1483 /**
1484 * This method will be overridden in the child class.
1485 */
9683db71 1486 function define_workbook() {
20e1f1c0 1487 }
9683db71
TH
1488
1489 function start_document($filename) {
43ec99aa 1490 $filename = $filename.'.'.$this->fileextension;
20e1f1c0 1491 $this->define_workbook();
20e1f1c0 1492 // format types
1493 $this->formatnormal =& $this->workbook->add_format();
1494 $this->formatnormal->set_bold(0);
1495 $this->formatheaders =& $this->workbook->add_format();
1496 $this->formatheaders->set_bold(1);
1497 $this->formatheaders->set_align('center');
20e1f1c0 1498 // Sending HTTP headers
43ec99aa 1499 $this->workbook->send($filename);
1500 $this->documentstarted = true;
1501 }
9683db71
TH
1502
1503 function start_table($sheettitle) {
43ec99aa 1504 $this->worksheet =& $this->workbook->add_worksheet($sheettitle);
20e1f1c0 1505 $this->rownum=0;
1506 }
9683db71
TH
1507
1508 function output_headers($headers) {
20e1f1c0 1509 $colnum = 0;
1510 foreach ($headers as $item) {
1511 $this->worksheet->write($this->rownum,$colnum,$item,$this->formatheaders);
1512 $colnum++;
1513 }
1514 $this->rownum++;
1515 }
9683db71
TH
1516
1517 function add_data($row) {
20e1f1c0 1518 $colnum = 0;
9683db71 1519 foreach ($row as $item) {
20e1f1c0 1520 $this->worksheet->write($this->rownum,$colnum,$item,$this->formatnormal);
1521 $colnum++;
1522 }
1523 $this->rownum++;
1524 return true;
1525 }
9683db71 1526
20e1f1c0 1527 function add_seperator() {
1528 $this->rownum++;
1529 return true;
1530 }
43ec99aa 1531
9683db71 1532 function finish_table() {
43ec99aa 1533 }
9683db71
TH
1534
1535 function finish_document() {
20e1f1c0 1536 $this->workbook->close();
1537 exit;
1538 }
1539}
1540
9683db71 1541
72fb21b6 1542/**
1543 * @package moodlecore
1544 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1545 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1546 */
9683db71 1547class table_excel_export_format extends table_spreadsheet_export_format_parent {
20e1f1c0 1548 var $fileextension = 'xls';
1549
9683db71 1550 function define_workbook() {
20e1f1c0 1551 global $CFG;
1552 require_once("$CFG->libdir/excellib.class.php");
1553 // Creating a workbook
1554 $this->workbook = new MoodleExcelWorkbook("-");
1555 }
1556
1557}
1558
9683db71 1559
72fb21b6 1560/**
1561 * @package moodlecore
1562 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1563 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1564 */
9683db71 1565class table_ods_export_format extends table_spreadsheet_export_format_parent {
20e1f1c0 1566 var $fileextension = 'ods';
9683db71 1567 function define_workbook() {
20e1f1c0 1568 global $CFG;
1569 require_once("$CFG->libdir/odslib.class.php");
1570 // Creating a workbook
1571 $this->workbook = new MoodleODSWorkbook("-");
1572 }
1573}
1574
9683db71 1575
72fb21b6 1576/**
1577 * @package moodlecore
1578 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1579 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1580 */
6944b5e4
TH
1581class table_text_export_format_parent extends table_default_export_format_parent {
1582 protected $seperator = "\t";
1583 protected $mimetype = 'text/tab-separated-values';
1584 protected $ext = '.txt';
1585
1586 public function start_document($filename) {
1587 $this->filename = $filename . $this->ext;
1588 header('Content-Type: ' . $this->mimetype . '; charset=UTF-8');
1589 header('Content-Disposition: attachment; filename="' . $this->filename . '"');
1590 header('Expires: 0');
1591 header('Cache-Control: must-revalidate,post-check=0,pre-check=0');
1592 header('Pragma: public');
43ec99aa 1593 $this->documentstarted = true;
1594 }
6944b5e4
TH
1595
1596 public function start_table($sheettitle) {
43ec99aa 1597 //nothing to do here
20e1f1c0 1598 }
6944b5e4
TH
1599
1600 public function output_headers($headers) {
1601 echo $this->format_row($headers);
20e1f1c0 1602 }
6944b5e4
TH
1603
1604 public function add_data($row) {
1605 echo $this->format_row($row);
20e1f1c0 1606 return true;
1607 }
6944b5e4
TH
1608
1609 public function finish_table() {
43ec99aa 1610 echo "\n\n";
1611 }
6944b5e4
TH
1612
1613 public function finish_document() {
20e1f1c0 1614 exit;
1615 }
6944b5e4
TH
1616
1617 /**
1618 * Format a row of data.
1619 * @param array $data
1620 */
1621 protected function format_row($data) {
1622 $escapeddata = array();
1623 foreach ($data as $value) {
1624 $escapeddata[] = '"' . str_replace('"', '""', $value) . '"';
1625 }
1626 return implode($this->seperator, $escapeddata) . "\n";
1627 }
20e1f1c0 1628}
1629
9683db71 1630
72fb21b6 1631/**
1632 * @package moodlecore
1633 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1634 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1635 */
9683db71 1636class table_tsv_export_format extends table_text_export_format_parent {
6944b5e4
TH
1637 protected $seperator = "\t";
1638 protected $mimetype = 'text/tab-separated-values';
1639 protected $ext = '.txt';
20e1f1c0 1640}
1641
9683db71 1642
72fb21b6 1643/**
1644 * @package moodlecore
1645 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1646 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1647 */
9683db71 1648class table_csv_export_format extends table_text_export_format_parent {
6944b5e4
TH
1649 protected $seperator = ",";
1650 protected $mimetype = 'text/csv';
1651 protected $ext = '.csv';
20e1f1c0 1652}
1653
9683db71 1654
72fb21b6 1655/**
1656 * @package moodlecore
1657 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1658 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1659 */
9683db71
TH
1660class table_xhtml_export_format extends table_default_export_format_parent {
1661 function start_document($filename) {
20e1f1c0 1662 header("Content-Type: application/download\n");
43ec99aa 1663 header("Content-Disposition: attachment; filename=\"$filename.html\"");
20e1f1c0 1664 header("Expires: 0");
1665 header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
1666 header("Pragma: public");
20e1f1c0 1667 //html headers
20e1f1c0 1668 echo <<<EOF
1669<?xml version="1.0" encoding="UTF-8"?>
1670<!DOCTYPE html
1671 PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
9cf4a18b 1672 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
20e1f1c0 1673
9cf4a18b 1674<html xmlns="http://www.w3.org/1999/xhtml"
20e1f1c0 1675 xml:lang="en" lang="en">
869309b8 1676<head>
20e1f1c0 1677<style type="text/css">/*<![CDATA[*/
1678
1679.flexible th {
1680white-space:normal;
1681}
1682th.header, td.header, div.header {
1683border-color:#DDDDDD;
1684background-color:lightGrey;
1685}
1686.flexible th {
1687white-space:nowrap;
1688}
1689th {
1690font-weight:bold;
1691}
1692
1693.generaltable {
1694border-style:solid;
1695}
1696.generalbox {
1697border-style:solid;
1698}
1699body, table, td, th {
1700font-family:Arial,Verdana,Helvetica,sans-serif;
1701font-size:100%;
1702}
1703td {
1704 border-style:solid;
1705 border-width:1pt;
1706}
1707table {
1708 border-collapse:collapse;
1709 border-spacing:0pt;
1710 width:80%;
1711 margin:auto;
1712}
1713
9683db71 1714h1, h2 {
20e1f1c0 1715 text-align:center;
1716}
1717.bold {
1718font-weight:bold;
1719}
869309b8 1720.mdl-align {
1721 text-align:center;
1722}
20e1f1c0 1723/*]]>*/</style>
869309b8 1724<title>$filename</title>
1725</head>
20e1f1c0 1726<body>
20e1f1c0 1727EOF;
43ec99aa 1728 $this->documentstarted = true;
1729 }
9683db71
TH
1730
1731 function start_table($sheettitle) {
43ec99aa 1732 $this->table->sortable(false);
1733 $this->table->collapsible(false);
1734 echo "<h2>{$sheettitle}</h2>";
20e1f1c0 1735 $this->table->start_html();
1736 }
43ec99aa 1737
9683db71 1738 function output_headers($headers) {
20e1f1c0 1739 $this->table->print_headers();
1740 }
9683db71
TH
1741
1742 function add_data($row) {
20e1f1c0 1743 $this->table->print_row($row);
1744 return true;
1745 }
9683db71 1746
20e1f1c0 1747 function add_seperator() {
1748 $this->table->print_row(NULL);
1749 return true;
1750 }
9683db71
TH
1751
1752 function finish_table() {
20e1f1c0 1753 $this->table->finish_html();
43ec99aa 1754 }
9683db71
TH
1755
1756 function finish_document() {
869309b8 1757 echo "</body>\n</html>";
20e1f1c0 1758 exit;
1759 }
9683db71
TH
1760
1761 function format_text($text, $format=FORMAT_MOODLE, $options=NULL, $courseid=NULL) {
1762 if (is_null($options)) {
ef27e742 1763 $options = new stdClass;
1764 }
1765 //some sensible defaults
9683db71 1766 if (!isset($options->para)) {
ef27e742 1767 $options->para = false;
1768 }
9683db71 1769 if (!isset($options->newlines)) {
ef27e742 1770 $options->newlines = false;
1771 }
1772 if (!isset($options->smiley)) {
1773 $options->smiley = false;
1774 }
1775 if (!isset($options->filter)) {
1776 $options->filter = false;
1777 }
1778 return format_text($text, $format, $options);
1779 }
20e1f1c0 1780}