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