3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
21 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
29 * These constants relate to the table's handling of URL parameters.
31 define('TABLE_VAR_SORT', 1);
32 define('TABLE_VAR_HIDE', 2);
33 define('TABLE_VAR_SHOW', 3);
34 define('TABLE_VAR_IFIRST', 4);
35 define('TABLE_VAR_ILAST', 5);
36 define('TABLE_VAR_PAGE', 6);
37 define('TABLE_VAR_RESET', 7);
38 define('TABLE_VAR_DIR', 8);
42 * Constants that indicate whether the paging bar for the table
43 * appears above or below the table.
45 define('TABLE_P_TOP', 1);
46 define('TABLE_P_BOTTOM', 2);
52 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
53 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
55 class flexible_table
{
58 var $attributes = array();
59 var $headers = array();
62 * @var string For create header with help icon.
64 private $helpforheaders = array();
65 var $columns = array();
66 var $column_style = array();
67 var $column_class = array();
68 var $column_suppress = array();
69 var $column_nosort = array('userpic');
70 private $column_textsort = array();
71 /** @var boolean Stores if setup has already been called on this flixible table. */
74 var $request = array();
77 * @var bool Whether or not to store table properties in the user_preferences table.
79 private $persistent = false;
80 var $is_collapsible = false;
81 var $is_sortable = false;
82 var $use_pages = false;
83 var $use_initials = false;
90 var $sort_default_column = NULL;
91 var $sort_default_order = SORT_ASC
;
94 * Array of positions in which to display download controls.
96 var $showdownloadbuttonsat= array(TABLE_P_TOP
);
99 * @var string Key of field returned by db query that is the id field of the
100 * user table or equivalent.
102 public $useridfield = 'id';
105 * @var string which download plugin to use. Default '' means none - print
106 * html table with paging. Property set by is_downloading which typically
107 * passes in cleaned data from $
112 * @var bool whether data is downloadable from table. Determines whether
113 * to display download buttons. Set by method downloadable().
115 var $downloadable = false;
118 * @var bool Has start output been called yet?
120 var $started_output = false;
122 var $exportclass = null;
125 * @var array For storing user-customised table properties in the user_preferences db table.
127 private $prefs = array();
129 /** @var $sheettitle */
130 protected $sheettitle;
132 /** @var $filename */
137 * @param string $uniqueid all tables have to have a unique id, this is used
138 * as a key when storing table properties like sort order in the session.
140 function __construct($uniqueid) {
141 $this->uniqueid
= $uniqueid;
142 $this->request
= array(
143 TABLE_VAR_SORT
=> 'tsort',
144 TABLE_VAR_HIDE
=> 'thide',
145 TABLE_VAR_SHOW
=> 'tshow',
146 TABLE_VAR_IFIRST
=> 'tifirst',
147 TABLE_VAR_ILAST
=> 'tilast',
148 TABLE_VAR_PAGE
=> 'page',
149 TABLE_VAR_RESET
=> 'treset',
150 TABLE_VAR_DIR
=> 'tdir',
155 * Call this to pass the download type. Use :
156 * $download = optional_param('download', '', PARAM_ALPHA);
157 * To get the download type. We assume that if you call this function with
158 * params that this table's data is downloadable, so we call is_downloadable
159 * for you (even if the param is '', which means no download this time.
160 * Also you can call this method with no params to get the current set
162 * @param string $download dataformat type. One of csv, xhtml, ods, etc
163 * @param string $filename filename for downloads without file extension.
164 * @param string $sheettitle title for downloaded data.
165 * @return string download dataformat type. One of csv, xhtml, ods, etc
167 function is_downloading($download = null, $filename='', $sheettitle='') {
168 if ($download!==null) {
169 $this->sheettitle
= $sheettitle;
170 $this->is_downloadable(true);
171 $this->download
= $download;
172 $this->filename
= clean_filename($filename);
173 $this->export_class_instance();
175 return $this->download
;
179 * Get, and optionally set, the export class.
180 * @param $exportclass (optional) if passed, set the table to use this export class.
181 * @return table_default_export_format_parent the export class in use (after any set).
183 function export_class_instance($exportclass = null) {
184 if (!is_null($exportclass)) {
185 $this->started_output
= true;
186 $this->exportclass
= $exportclass;
187 $this->exportclass
->table
= $this;
188 } else if (is_null($this->exportclass
) && !empty($this->download
)) {
189 $this->exportclass
= new table_dataformat_export_format($this, $this->download
);
190 if (!$this->exportclass
->document_started()) {
191 $this->exportclass
->start_document($this->filename
, $this->sheettitle
);
194 return $this->exportclass
;
198 * Probably don't need to call this directly. Calling is_downloading with a
199 * param automatically sets table as downloadable.
201 * @param bool $downloadable optional param to set whether data from
202 * table is downloadable. If ommitted this function can be used to get
203 * current state of table.
204 * @return bool whether table data is set to be downloadable.
206 function is_downloadable($downloadable = null) {
207 if ($downloadable !== null) {
208 $this->downloadable
= $downloadable;
210 return $this->downloadable
;
214 * Call with boolean true to store table layout changes in the user_preferences table.
215 * Note: user_preferences.value has a maximum length of 1333 characters.
216 * Call with no parameter to get current state of table persistence.
218 * @param bool $persistent Optional parameter to set table layout persistence.
219 * @return bool Whether or not the table layout preferences will persist.
221 public function is_persistent($persistent = null) {
222 if ($persistent == true) {
223 $this->persistent
= true;
225 return $this->persistent
;
229 * Where to show download buttons.
230 * @param array $showat array of postions in which to show download buttons.
231 * Containing TABLE_P_TOP and/or TABLE_P_BOTTOM
233 function show_download_buttons_at($showat) {
234 $this->showdownloadbuttonsat
= $showat;
238 * Sets the is_sortable variable to the given boolean, sort_default_column to
239 * the given string, and the sort_default_order to the given integer.
241 * @param string $defaultcolumn
242 * @param int $defaultorder
245 function sortable($bool, $defaultcolumn = NULL, $defaultorder = SORT_ASC
) {
246 $this->is_sortable
= $bool;
247 $this->sort_default_column
= $defaultcolumn;
248 $this->sort_default_order
= $defaultorder;
252 * Use text sorting functions for this column (required for text columns with Oracle).
253 * Be warned that you cannot use this with column aliases. You can only do this
254 * with real columns. See MDL-40481 for an example.
255 * @param string column name
257 function text_sorting($column) {
258 $this->column_textsort
[] = $column;
262 * Do not sort using this column
263 * @param string column name
265 function no_sorting($column) {
266 $this->column_nosort
[] = $column;
270 * Is the column sortable?
271 * @param string column name, null means table
274 function is_sortable($column = null) {
275 if (empty($column)) {
276 return $this->is_sortable
;
278 if (!$this->is_sortable
) {
281 return !in_array($column, $this->column_nosort
);
285 * Sets the is_collapsible variable to the given boolean.
289 function collapsible($bool) {
290 $this->is_collapsible
= $bool;
294 * Sets the use_pages variable to the given boolean.
298 function pageable($bool) {
299 $this->use_pages
= $bool;
303 * Sets the use_initials variable to the given boolean.
307 function initialbars($bool) {
308 $this->use_initials
= $bool;
312 * Sets the pagesize variable to the given integer, the totalrows variable
313 * to the given integer, and the use_pages variable to true.
314 * @param int $perpage
318 function pagesize($perpage, $total) {
319 $this->pagesize
= $perpage;
320 $this->totalrows
= $total;
321 $this->use_pages
= true;
325 * Assigns each given variable in the array to the corresponding index
326 * in the request class variable.
327 * @param array $variables
330 function set_control_variables($variables) {
331 foreach ($variables as $what => $variable) {
332 if (isset($this->request
[$what])) {
333 $this->request
[$what] = $variable;
339 * Gives the given $value to the $attribute index of $this->attributes.
340 * @param string $attribute
341 * @param mixed $value
344 function set_attribute($attribute, $value) {
345 $this->attributes
[$attribute] = $value;
349 * What this method does is set the column so that if the same data appears in
350 * consecutive rows, then it is not repeated.
352 * For example, in the quiz overview report, the fullname column is set to be suppressed, so
353 * that when one student has made multiple attempts, their name is only printed in the row
354 * for their first attempt.
355 * @param int $column the index of a column.
357 function column_suppress($column) {
358 if (isset($this->column_suppress
[$column])) {
359 $this->column_suppress
[$column] = true;
364 * Sets the given $column index to the given $classname in $this->column_class.
366 * @param string $classname
369 function column_class($column, $classname) {
370 if (isset($this->column_class
[$column])) {
371 $this->column_class
[$column] = ' '.$classname; // This space needed so that classnames don't run together in the HTML
376 * Sets the given $column index and $property index to the given $value in $this->column_style.
378 * @param string $property
379 * @param mixed $value
382 function column_style($column, $property, $value) {
383 if (isset($this->column_style
[$column])) {
384 $this->column_style
[$column][$property] = $value;
389 * Sets all columns' $propertys to the given $value in $this->column_style.
390 * @param int $property
391 * @param string $value
394 function column_style_all($property, $value) {
395 foreach (array_keys($this->columns
) as $column) {
396 $this->column_style
[$column][$property] = $value;
401 * Sets $this->baseurl.
402 * @param moodle_url|string $url the url with params needed to call up this page
404 function define_baseurl($url) {
405 $this->baseurl
= new moodle_url($url);
409 * @param array $columns an array of identifying names for columns. If
410 * columns are sorted then column names must correspond to a field in sql.
412 function define_columns($columns) {
413 $this->columns
= array();
414 $this->column_style
= array();
415 $this->column_class
= array();
418 foreach ($columns as $column) {
419 $this->columns
[$column] = $colnum++
;
420 $this->column_style
[$column] = array();
421 $this->column_class
[$column] = '';
422 $this->column_suppress
[$column] = false;
427 * @param array $headers numerical keyed array of displayed string titles
430 function define_headers($headers) {
431 $this->headers
= $headers;
435 * Defines a help icon for the header
437 * Always use this function if you need to create header with sorting and help icon.
439 * @param renderable[] $helpicons An array of renderable objects to be used as help icons
441 public function define_help_for_headers($helpicons) {
442 $this->helpforheaders
= $helpicons;
446 * Must be called after table is defined. Use methods above first. Cannot
447 * use functions below till after calling this method.
453 if (empty($this->columns
) ||
empty($this->uniqueid
)) {
457 // Load any existing user preferences.
458 if ($this->persistent
) {
459 $this->prefs
= json_decode(get_user_preferences('flextable_' . $this->uniqueid
), true);
460 $oldprefs = $this->prefs
;
461 } else if (isset($SESSION->flextable
[$this->uniqueid
])) {
462 $this->prefs
= $SESSION->flextable
[$this->uniqueid
];
463 $oldprefs = $this->prefs
;
466 // Set up default preferences if needed.
467 if (!$this->prefs
or optional_param($this->request
[TABLE_VAR_RESET
], false, PARAM_BOOL
)) {
468 $this->prefs
= array(
469 'collapse' => array(),
473 'textsort' => $this->column_textsort
,
477 if (!isset($oldprefs)) {
478 $oldprefs = $this->prefs
;
481 if (($showcol = optional_param($this->request
[TABLE_VAR_SHOW
], '', PARAM_ALPHANUMEXT
)) &&
482 isset($this->columns
[$showcol])) {
483 $this->prefs
['collapse'][$showcol] = false;
485 } else if (($hidecol = optional_param($this->request
[TABLE_VAR_HIDE
], '', PARAM_ALPHANUMEXT
)) &&
486 isset($this->columns
[$hidecol])) {
487 $this->prefs
['collapse'][$hidecol] = true;
488 if (array_key_exists($hidecol, $this->prefs
['sortby'])) {
489 unset($this->prefs
['sortby'][$hidecol]);
493 // Now, update the column attributes for collapsed columns
494 foreach (array_keys($this->columns
) as $column) {
495 if (!empty($this->prefs
['collapse'][$column])) {
496 $this->column_style
[$column]['width'] = '10px';
500 if (($sortcol = optional_param($this->request
[TABLE_VAR_SORT
], '', PARAM_ALPHANUMEXT
)) &&
501 $this->is_sortable($sortcol) && empty($this->prefs
['collapse'][$sortcol]) &&
502 (isset($this->columns
[$sortcol]) ||
in_array($sortcol, get_all_user_name_fields())
503 && isset($this->columns
['fullname']))) {
505 $sortdir = optional_param($this->request
[TABLE_VAR_DIR
], $this->sort_default_order
, PARAM_INT
);
507 if (array_key_exists($sortcol, $this->prefs
['sortby'])) {
508 // This key already exists somewhere. Change its sortorder and bring it to the top.
509 $sortorder = $this->prefs
['sortby'][$sortcol] = $sortdir;
510 unset($this->prefs
['sortby'][$sortcol]);
511 $this->prefs
['sortby'] = array_merge(array($sortcol => $sortorder), $this->prefs
['sortby']);
513 // Key doesn't exist, so just add it to the beginning of the array, ascending order
514 $this->prefs
['sortby'] = array_merge(array($sortcol => $sortdir), $this->prefs
['sortby']);
517 // Finally, make sure that no more than $this->maxsortkeys are present into the array
518 $this->prefs
['sortby'] = array_slice($this->prefs
['sortby'], 0, $this->maxsortkeys
);
521 // 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.
522 // This prevents results from being returned in a random order if the only order by column contains equal values.
523 if (!empty($this->sort_default_column
)) {
524 if (!array_key_exists($this->sort_default_column
, $this->prefs
['sortby'])) {
525 $defaultsort = array($this->sort_default_column
=> $this->sort_default_order
);
526 $this->prefs
['sortby'] = array_merge($this->prefs
['sortby'], $defaultsort);
530 $ilast = optional_param($this->request
[TABLE_VAR_ILAST
], null, PARAM_RAW
);
531 if (!is_null($ilast) && ($ilast ==='' ||
strpos(get_string('alphabet', 'langconfig'), $ilast) !== false)) {
532 $this->prefs
['i_last'] = $ilast;
535 $ifirst = optional_param($this->request
[TABLE_VAR_IFIRST
], null, PARAM_RAW
);
536 if (!is_null($ifirst) && ($ifirst === '' ||
strpos(get_string('alphabet', 'langconfig'), $ifirst) !== false)) {
537 $this->prefs
['i_first'] = $ifirst;
540 // Save user preferences if they have changed.
541 if ($this->prefs
!= $oldprefs) {
542 if ($this->persistent
) {
543 set_user_preference('flextable_' . $this->uniqueid
, json_encode($this->prefs
));
545 $SESSION->flextable
[$this->uniqueid
] = $this->prefs
;
550 if (empty($this->baseurl
)) {
551 debugging('You should set baseurl when using flexible_table.');
553 $this->baseurl
= $PAGE->url
;
556 $this->currpage
= optional_param($this->request
[TABLE_VAR_PAGE
], 0, PARAM_INT
);
559 // Always introduce the "flexible" class for the table if not specified
560 if (empty($this->attributes
)) {
561 $this->attributes
['class'] = 'flexible';
562 } else if (!isset($this->attributes
['class'])) {
563 $this->attributes
['class'] = 'flexible';
564 } else if (!in_array('flexible', explode(' ', $this->attributes
['class']))) {
565 $this->attributes
['class'] = trim('flexible ' . $this->attributes
['class']);
570 * Get the order by clause from the session or user preferences, for the table with id $uniqueid.
571 * @param string $uniqueid the identifier for a table.
572 * @return SQL fragment that can be used in an ORDER BY clause.
574 public static function get_sort_for_table($uniqueid) {
576 if (isset($SESSION->flextable
[$uniqueid])) {
577 $prefs = $SESSION->flextable
[$uniqueid];
578 } else if (!$prefs = json_decode(get_user_preferences('flextable_' . $uniqueid), true)) {
582 if (empty($prefs['sortby'])) {
585 if (empty($prefs['textsort'])) {
586 $prefs['textsort'] = array();
589 return self
::construct_order_by($prefs['sortby'], $prefs['textsort']);
593 * Prepare an an order by clause from the list of columns to be sorted.
594 * @param array $cols column name => SORT_ASC or SORT_DESC
595 * @return SQL fragment that can be used in an ORDER BY clause.
597 public static function construct_order_by($cols, $textsortcols=array()) {
601 foreach ($cols as $column => $order) {
602 if (in_array($column, $textsortcols)) {
603 $column = $DB->sql_order_by_text($column);
605 if ($order == SORT_ASC
) {
606 $bits[] = $column . ' ASC';
608 $bits[] = $column . ' DESC';
612 return implode(', ', $bits);
616 * @return SQL fragment that can be used in an ORDER BY clause.
618 public function get_sql_sort() {
619 return self
::construct_order_by($this->get_sort_columns(), $this->column_textsort
);
623 * Get the columns to sort by, in the form required by {@link construct_order_by()}.
624 * @return array column name => SORT_... constant.
626 public function get_sort_columns() {
628 throw new coding_exception('Cannot call get_sort_columns until you have called setup.');
631 if (empty($this->prefs
['sortby'])) {
635 foreach ($this->prefs
['sortby'] as $column => $notused) {
636 if (isset($this->columns
[$column])) {
637 continue; // This column is OK.
639 if (in_array($column, get_all_user_name_fields()) &&
640 isset($this->columns
['fullname'])) {
641 continue; // This column is OK.
643 // This column is not OK.
644 unset($this->prefs
['sortby'][$column]);
647 return $this->prefs
['sortby'];
651 * @return int the offset for LIMIT clause of SQL
653 function get_page_start() {
654 if (!$this->use_pages
) {
657 return $this->currpage
* $this->pagesize
;
661 * @return int the pagesize for LIMIT clause of SQL
663 function get_page_size() {
664 if (!$this->use_pages
) {
667 return $this->pagesize
;
671 * @return string sql to add to where statement.
673 function get_sql_where() {
676 $conditions = array();
679 if (isset($this->columns
['fullname'])) {
683 if (!empty($this->prefs
['i_first'])) {
684 $conditions[] = $DB->sql_like('firstname', ':ifirstc'.$i, false, false);
685 $params['ifirstc'.$i] = $this->prefs
['i_first'].'%';
687 if (!empty($this->prefs
['i_last'])) {
688 $conditions[] = $DB->sql_like('lastname', ':ilastc'.$i, false, false);
689 $params['ilastc'.$i] = $this->prefs
['i_last'].'%';
693 return array(implode(" AND ", $conditions), $params);
697 * Add a row of data to the table. This function takes an array or object with
698 * column names as keys or property names.
700 * It ignores any elements with keys that are not defined as columns. It
701 * puts in empty strings into the row when there is no element in the passed
702 * array corresponding to a column in the table. It puts the row elements in
703 * the proper order (internally row table data is stored by in arrays with
704 * a numerical index corresponding to the column number).
706 * @param object|array $rowwithkeys array keys or object property names are column names,
707 * as defined in call to define_columns.
708 * @param string $classname CSS class name to add to this row's tr tag.
710 function add_data_keyed($rowwithkeys, $classname = '') {
711 $this->add_data($this->get_row_from_keyed($rowwithkeys), $classname);
715 * Add a number of rows to the table at once. And optionally finish output after they have been added.
717 * @param (object|array|null)[] $rowstoadd Array of rows to add to table, a null value in array adds a separator row. Or a
718 * object or array is added to table. We expect properties for the row array as would be
719 * passed to add_data_keyed.
720 * @param bool $finish
722 public function format_and_add_array_of_rows($rowstoadd, $finish = true) {
723 foreach ($rowstoadd as $row) {
725 $this->add_separator();
727 $this->add_data_keyed($this->format_row($row));
731 $this->finish_output(!$this->is_downloading());
736 * Add a seperator line to table.
738 function add_separator() {
742 $this->add_data(NULL);
746 * This method actually directly echoes the row passed to it now or adds it
747 * to the download. If this is the first row and start_output has not
748 * already been called this method also calls start_output to open the table
749 * or send headers for the downloaded.
750 * Can be used as before. print_html now calls finish_html to close table.
752 * @param array $row a numerically keyed row of data to add to the table.
753 * @param string $classname CSS class name to add to this row's tr tag.
754 * @return bool success.
756 function add_data($row, $classname = '') {
760 if (!$this->started_output
) {
761 $this->start_output();
763 if ($this->exportclass
!==null) {
765 $this->exportclass
->add_seperator();
767 $this->exportclass
->add_data($row);
770 $this->print_row($row, $classname);
776 * You should call this to finish outputting the table data after adding
777 * data to the table with add_data or add_data_keyed.
780 function finish_output($closeexportclassdoc = true) {
781 if ($this->exportclass
!==null) {
782 $this->exportclass
->finish_table();
783 if ($closeexportclassdoc) {
784 $this->exportclass
->finish_document();
787 $this->finish_html();
792 * Hook that can be overridden in child classes to wrap a table in a form
793 * for example. Called only when there is data to display and not
796 function wrap_html_start() {
800 * Hook that can be overridden in child classes to wrap a table in a form
801 * for example. Called only when there is data to display and not
804 function wrap_html_finish() {
808 * Call appropriate methods on this table class to perform any processing on values before displaying in table.
809 * Takes raw data from the database and process it into human readable format, perhaps also adding html linking when
810 * displaying table as html, adding a div wrap, etc.
812 * See for example col_fullname below which will be called for a column whose name is 'fullname'.
814 * @param array|object $row row of data from db used to make one row of the table.
815 * @return array one row for the table, added using add_data_keyed method.
817 function format_row($row) {
818 if (is_array($row)) {
821 $formattedrow = array();
822 foreach (array_keys($this->columns
) as $column) {
823 $colmethodname = 'col_'.$column;
824 if (method_exists($this, $colmethodname)) {
825 $formattedcolumn = $this->$colmethodname($row);
827 $formattedcolumn = $this->other_cols($column, $row);
828 if ($formattedcolumn===NULL) {
829 $formattedcolumn = $row->$column;
832 $formattedrow[$column] = $formattedcolumn;
834 return $formattedrow;
838 * Fullname is treated as a special columname in tablelib and should always
839 * be treated the same as the fullname of a user.
840 * @uses $this->useridfield if the userid field is not expected to be id
841 * then you need to override $this->useridfield to point at the correct
842 * field for the user id.
844 * @param object $row the data from the db containing all fields from the
845 * users table necessary to construct the full name of the user in
847 * @return string contents of cell in column 'fullname', for this row.
849 function col_fullname($row) {
852 $name = fullname($row);
853 if ($this->download
) {
857 $userid = $row->{$this->useridfield
};
858 if ($COURSE->id
== SITEID
) {
859 $profileurl = new moodle_url('/user/profile.php', array('id' => $userid));
861 $profileurl = new moodle_url('/user/view.php',
862 array('id' => $userid, 'course' => $COURSE->id
));
864 return html_writer
::link($profileurl, $name);
868 * You can override this method in a child class. See the description of
869 * build_table which calls this method.
871 function other_cols($column, $row) {
876 * Used from col_* functions when text is to be displayed. Does the
877 * right thing - either converts text to html or strips any html tags
878 * depending on if we are downloading and what is the download type. Params
879 * are the same as format_text function in weblib.php but some default
880 * options are changed.
882 function format_text($text, $format=FORMAT_MOODLE
, $options=NULL, $courseid=NULL) {
883 if (!$this->is_downloading()) {
884 if (is_null($options)) {
885 $options = new stdClass
;
887 //some sensible defaults
888 if (!isset($options->para
)) {
889 $options->para
= false;
891 if (!isset($options->newlines
)) {
892 $options->newlines
= false;
894 if (!isset($options->smiley
)) {
895 $options->smiley
= false;
897 if (!isset($options->filter
)) {
898 $options->filter
= false;
900 return format_text($text, $format, $options);
902 $eci = $this->export_class_instance();
903 return $eci->format_text($text, $format, $options, $courseid);
907 * This method is deprecated although the old api is still supported.
908 * @deprecated 1.9.2 - Jun 2, 2008
910 function print_html() {
914 $this->finish_html();
918 * This function is not part of the public api.
919 * @return string initial of first name we are currently filtering by
921 function get_initial_first() {
922 if (!$this->use_initials
) {
926 return $this->prefs
['i_first'];
930 * This function is not part of the public api.
931 * @return string initial of last name we are currently filtering by
933 function get_initial_last() {
934 if (!$this->use_initials
) {
938 return $this->prefs
['i_last'];
942 * Helper function, used by {@link print_initials_bar()} to output one initial bar.
943 * @param array $alpha of letters in the alphabet.
944 * @param string $current the currently selected letter.
945 * @param string $class class name to add to this initial bar.
946 * @param string $title the name to put in front of this initial bar.
947 * @param string $urlvar URL parameter name for this initial.
949 * @deprecated since Moodle 3.3
951 protected function print_one_initials_bar($alpha, $current, $class, $title, $urlvar) {
953 debugging('Method print_one_initials_bar() is no longer used and has been deprecated, ' .
954 'to print initials bar call print_initials_bar()', DEBUG_DEVELOPER
);
956 echo html_writer
::start_tag('div', array('class' => 'initialbar ' . $class)) .
959 echo html_writer
::link($this->baseurl
->out(false, array($urlvar => '')), get_string('all'));
961 echo html_writer
::tag('strong', get_string('all'));
964 foreach ($alpha as $letter) {
965 if ($letter === $current) {
966 echo html_writer
::tag('strong', $letter);
968 echo html_writer
::link($this->baseurl
->out(false, array($urlvar => $letter)), $letter);
972 echo html_writer
::end_tag('div');
976 * This function is not part of the public api.
978 function print_initials_bar() {
981 $ifirst = $this->get_initial_first();
982 $ilast = $this->get_initial_last();
983 if (is_null($ifirst)) {
986 if (is_null($ilast)) {
990 if ((!empty($ifirst) ||
!empty($ilast) ||
$this->use_initials
)
991 && isset($this->columns
['fullname'])) {
992 $prefixfirst = $this->request
[TABLE_VAR_IFIRST
];
993 $prefixlast = $this->request
[TABLE_VAR_ILAST
];
994 echo $OUTPUT->initials_bar($ifirst, 'firstinitial', get_string('firstname'), $prefixfirst, $this->baseurl
);
995 echo $OUTPUT->initials_bar($ilast, 'lastinitial', get_string('lastname'), $prefixlast, $this->baseurl
);
1001 * This function is not part of the public api.
1003 function print_nothing_to_display() {
1006 // Render button to allow user to reset table preferences.
1007 echo $this->render_reset_button();
1009 $this->print_initials_bar();
1011 echo $OUTPUT->heading(get_string('nothingtodisplay'));
1015 * This function is not part of the public api.
1017 function get_row_from_keyed($rowwithkeys) {
1018 if (is_object($rowwithkeys)) {
1019 $rowwithkeys = (array)$rowwithkeys;
1022 foreach (array_keys($this->columns
) as $column) {
1023 if (isset($rowwithkeys[$column])) {
1024 $row [] = $rowwithkeys[$column];
1033 * Get the html for the download buttons
1035 * Usually only use internally
1037 public function download_buttons() {
1040 if ($this->is_downloadable() && !$this->is_downloading()) {
1041 return $OUTPUT->download_dataformat_selector(get_string('downloadas', 'table'),
1042 $this->baseurl
->out_omit_querystring(), 'download', $this->baseurl
->params());
1049 * This function is not part of the public api.
1050 * You don't normally need to call this. It is called automatically when
1051 * needed when you start adding data to the table.
1054 function start_output() {
1055 $this->started_output
= true;
1056 if ($this->exportclass
!==null) {
1057 $this->exportclass
->start_table($this->sheettitle
);
1058 $this->exportclass
->output_headers($this->headers
);
1060 $this->start_html();
1061 $this->print_headers();
1062 echo html_writer
::start_tag('tbody');
1067 * This function is not part of the public api.
1069 function print_row($row, $classname = '') {
1070 echo $this->get_row_html($row, $classname);
1074 * Generate html code for the passed row.
1076 * @param array $row Row data.
1077 * @param string $classname classes to add.
1079 * @return string $html html code for the row passed.
1081 public function get_row_html($row, $classname = '') {
1082 static $suppress_lastrow = NULL;
1083 $rowclasses = array();
1086 $rowclasses[] = $classname;
1089 $rowid = $this->uniqueid
. '_r' . $this->currentrow
;
1092 $html .= html_writer
::start_tag('tr', array('class' => implode(' ', $rowclasses), 'id' => $rowid));
1094 // If we have a separator, print it
1095 if ($row === NULL) {
1096 $colcount = count($this->columns
);
1097 $html .= html_writer
::tag('td', html_writer
::tag('div', '',
1098 array('class' => 'tabledivider')), array('colspan' => $colcount));
1101 $colbyindex = array_flip($this->columns
);
1102 foreach ($row as $index => $data) {
1103 $column = $colbyindex[$index];
1105 if (empty($this->prefs
['collapse'][$column])) {
1106 if ($this->column_suppress
[$column] && $suppress_lastrow !== NULL && $suppress_lastrow[$index] === $data) {
1107 $content = ' ';
1112 $content = ' ';
1115 $html .= html_writer
::tag('td', $content, array(
1116 'class' => 'cell c' . $index . $this->column_class
[$column],
1117 'id' => $rowid . '_c' . $index,
1118 'style' => $this->make_styles_string($this->column_style
[$column])));
1122 $html .= html_writer
::end_tag('tr');
1124 $suppress_enabled = array_sum($this->column_suppress
);
1125 if ($suppress_enabled) {
1126 $suppress_lastrow = $row;
1128 $this->currentrow++
;
1133 * This function is not part of the public api.
1135 function finish_html() {
1137 if (!$this->started_output
) {
1138 //no data has been added to the table.
1139 $this->print_nothing_to_display();
1142 // Print empty rows to fill the table to the current pagesize.
1143 // This is done so the header aria-controls attributes do not point to
1144 // non existant elements.
1145 $emptyrow = array_fill(0, count($this->columns
), '');
1146 while ($this->currentrow
< $this->pagesize
) {
1147 $this->print_row($emptyrow, 'emptyrow');
1150 echo html_writer
::end_tag('tbody');
1151 echo html_writer
::end_tag('table');
1152 echo html_writer
::end_tag('div');
1153 $this->wrap_html_finish();
1156 if(in_array(TABLE_P_BOTTOM
, $this->showdownloadbuttonsat
)) {
1157 echo $this->download_buttons();
1160 if($this->use_pages
) {
1161 $pagingbar = new paging_bar($this->totalrows
, $this->currpage
, $this->pagesize
, $this->baseurl
);
1162 $pagingbar->pagevar
= $this->request
[TABLE_VAR_PAGE
];
1163 echo $OUTPUT->render($pagingbar);
1169 * Generate the HTML for the collapse/uncollapse icon. This is a helper method
1170 * used by {@link print_headers()}.
1171 * @param string $column the column name, index into various names.
1172 * @param int $index numerical index of the column.
1173 * @return string HTML fragment.
1175 protected function show_hide_link($column, $index) {
1177 // Some headers contain <br /> tags, do not include in title, hence the
1181 for ($i = 0; $i < $this->pagesize
; $i++
) {
1182 $ariacontrols .= $this->uniqueid
. '_r' . $i . '_c' . $index . ' ';
1185 $ariacontrols = trim($ariacontrols);
1187 if (!empty($this->prefs
['collapse'][$column])) {
1188 $linkattributes = array('title' => get_string('show') . ' ' . strip_tags($this->headers
[$index]),
1189 'aria-expanded' => 'false',
1190 'aria-controls' => $ariacontrols);
1191 return html_writer
::link($this->baseurl
->out(false, array($this->request
[TABLE_VAR_SHOW
] => $column)),
1192 $OUTPUT->pix_icon('t/switch_plus', get_string('show')), $linkattributes);
1194 } else if ($this->headers
[$index] !== NULL) {
1195 $linkattributes = array('title' => get_string('hide') . ' ' . strip_tags($this->headers
[$index]),
1196 'aria-expanded' => 'true',
1197 'aria-controls' => $ariacontrols);
1198 return html_writer
::link($this->baseurl
->out(false, array($this->request
[TABLE_VAR_HIDE
] => $column)),
1199 $OUTPUT->pix_icon('t/switch_minus', get_string('hide')), $linkattributes);
1204 * This function is not part of the public api.
1206 function print_headers() {
1207 global $CFG, $OUTPUT, $PAGE;
1209 echo html_writer
::start_tag('thead');
1210 echo html_writer
::start_tag('tr');
1211 foreach ($this->columns
as $column => $index) {
1214 if ($this->is_collapsible
) {
1215 $icon_hide = $this->show_hide_link($column, $index);
1218 $primarysortcolumn = '';
1219 $primarysortorder = '';
1220 if (reset($this->prefs
['sortby'])) {
1221 $primarysortcolumn = key($this->prefs
['sortby']);
1222 $primarysortorder = current($this->prefs
['sortby']);
1228 // Check the full name display for sortable fields.
1229 if (has_capability('moodle/site:viewfullnames', $PAGE->context
)) {
1230 $nameformat = $CFG->alternativefullnameformat
;
1232 $nameformat = $CFG->fullnamedisplay
;
1235 if ($nameformat == 'language') {
1236 $nameformat = get_string('fullnamedisplay');
1239 $requirednames = order_in_string(get_all_user_name_fields(), $nameformat);
1241 if (!empty($requirednames)) {
1242 if ($this->is_sortable($column)) {
1243 // Done this way for the possibility of more than two sortable full name display fields.
1244 $this->headers
[$index] = '';
1245 foreach ($requirednames as $name) {
1246 $sortname = $this->sort_link(get_string($name),
1247 $name, $primarysortcolumn === $name, $primarysortorder);
1248 $this->headers
[$index] .= $sortname . ' / ';
1251 if (isset($this->helpforheaders
[$index])) {
1252 $helpicon = $OUTPUT->render($this->helpforheaders
[$index]);
1254 $this->headers
[$index] = substr($this->headers
[$index], 0, -3). $helpicon;
1260 // do nothing, do not display sortable links
1264 if ($this->is_sortable($column)) {
1266 if (isset($this->helpforheaders
[$index])) {
1267 $helpicon = $OUTPUT->render($this->helpforheaders
[$index]);
1269 $this->headers
[$index] = $this->sort_link($this->headers
[$index],
1270 $column, $primarysortcolumn == $column, $primarysortorder) . $helpicon;
1274 $attributes = array(
1275 'class' => 'header c' . $index . $this->column_class
[$column],
1278 if ($this->headers
[$index] === NULL) {
1279 $content = ' ';
1280 } else if (!empty($this->prefs
['collapse'][$column])) {
1281 $content = $icon_hide;
1283 if (is_array($this->column_style
[$column])) {
1284 $attributes['style'] = $this->make_styles_string($this->column_style
[$column]);
1287 if (isset($this->helpforheaders
[$index]) && !$this->is_sortable($column)) {
1288 $helpicon = $OUTPUT->render($this->helpforheaders
[$index]);
1290 $content = $this->headers
[$index] . $helpicon . html_writer
::tag('div',
1291 $icon_hide, array('class' => 'commands'));
1293 echo html_writer
::tag('th', $content, $attributes);
1296 echo html_writer
::end_tag('tr');
1297 echo html_writer
::end_tag('thead');
1301 * Generate the HTML for the sort icon. This is a helper method used by {@link sort_link()}.
1302 * @param bool $isprimary whether an icon is needed (it is only needed for the primary sort column.)
1303 * @param int $order SORT_ASC or SORT_DESC
1304 * @return string HTML fragment.
1306 protected function sort_icon($isprimary, $order) {
1313 if ($order == SORT_ASC
) {
1314 return $OUTPUT->pix_icon('t/sort_asc', get_string('asc'));
1316 return $OUTPUT->pix_icon('t/sort_desc', get_string('desc'));
1321 * Generate the correct tool tip for changing the sort order. This is a
1322 * helper method used by {@link sort_link()}.
1323 * @param bool $isprimary whether the is column is the current primary sort column.
1324 * @param int $order SORT_ASC or SORT_DESC
1325 * @return string the correct title.
1327 protected function sort_order_name($isprimary, $order) {
1328 if ($isprimary && $order != SORT_ASC
) {
1329 return get_string('desc');
1331 return get_string('asc');
1336 * Generate the HTML for the sort link. This is a helper method used by {@link print_headers()}.
1337 * @param string $text the text for the link.
1338 * @param string $column the column name, may be a fake column like 'firstname' or a real one.
1339 * @param bool $isprimary whether the is column is the current primary sort column.
1340 * @param int $order SORT_ASC or SORT_DESC
1341 * @return string HTML fragment.
1343 protected function sort_link($text, $column, $isprimary, $order) {
1344 // If we are already sorting by this column, switch direction.
1345 if (array_key_exists($column, $this->prefs
['sortby'])) {
1346 $sortorder = $this->prefs
['sortby'][$column] == SORT_ASC ? SORT_DESC
: SORT_ASC
;
1348 $sortorder = $order;
1352 $this->request
[TABLE_VAR_SORT
] => $column,
1353 $this->request
[TABLE_VAR_DIR
] => $sortorder,
1356 return html_writer
::link($this->baseurl
->out(false, $params),
1357 $text . get_accesshide(get_string('sortby') . ' ' .
1358 $text . ' ' . $this->sort_order_name($isprimary, $order))) . ' ' .
1359 $this->sort_icon($isprimary, $order);
1363 * This function is not part of the public api.
1365 function start_html() {
1368 // Render button to allow user to reset table preferences.
1369 echo $this->render_reset_button();
1371 // Do we need to print initial bars?
1372 $this->print_initials_bar();
1375 if ($this->use_pages
) {
1376 $pagingbar = new paging_bar($this->totalrows
, $this->currpage
, $this->pagesize
, $this->baseurl
);
1377 $pagingbar->pagevar
= $this->request
[TABLE_VAR_PAGE
];
1378 echo $OUTPUT->render($pagingbar);
1381 if (in_array(TABLE_P_TOP
, $this->showdownloadbuttonsat
)) {
1382 echo $this->download_buttons();
1385 $this->wrap_html_start();
1386 // Start of main data table
1388 echo html_writer
::start_tag('div', array('class' => 'no-overflow'));
1389 echo html_writer
::start_tag('table', $this->attributes
);
1394 * This function is not part of the public api.
1395 * @param array $styles CSS-property => value
1396 * @return string values suitably to go in a style="" attribute in HTML.
1398 function make_styles_string($styles) {
1399 if (empty($styles)) {
1404 foreach($styles as $property => $value) {
1405 $string .= $property . ':' . $value . ';';
1411 * Generate the HTML for the table preferences reset button.
1413 * @return string HTML fragment, empty string if no need to reset
1415 protected function render_reset_button() {
1417 if (!$this->can_be_reset()) {
1421 $url = $this->baseurl
->out(false, array($this->request
[TABLE_VAR_RESET
] => 1));
1423 $html = html_writer
::start_div('resettable mdl-right');
1424 $html .= html_writer
::link($url, get_string('resettable'));
1425 $html .= html_writer
::end_div();
1431 * Are there some table preferences that can be reset?
1433 * If true, then the "reset table preferences" widget should be displayed.
1437 protected function can_be_reset() {
1439 // Loop through preferences and make sure they are empty or set to the default value.
1440 foreach ($this->prefs
as $prefname => $prefval) {
1442 if ($prefname === 'sortby' and !empty($this->sort_default_column
)) {
1443 // Check if the actual sorting differs from the default one.
1444 if (empty($prefval) or $prefval !== array($this->sort_default_column
=> $this->sort_default_order
)) {
1448 } else if ($prefname === 'collapse' and !empty($prefval)) {
1449 // Check if there are some collapsed columns (all are expanded by default).
1450 foreach ($prefval as $columnname => $iscollapsed) {
1456 } else if (!empty($prefval)) {
1457 // For all other cases, we just check if some preference is set.
1468 * @package moodlecore
1469 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1470 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1472 class table_sql
extends flexible_table
{
1474 public $countsql = NULL;
1475 public $countparams = NULL;
1477 * @var object sql for querying db. Has fields 'fields', 'from', 'where', 'params'.
1481 * @var array|\Traversable Data fetched from the db.
1483 public $rawdata = NULL;
1486 * @var bool Overriding default for this.
1488 public $is_sortable = true;
1490 * @var bool Overriding default for this.
1492 public $is_collapsible = true;
1495 * @param string $uniqueid a string identifying this table.Used as a key in
1498 function __construct($uniqueid) {
1499 parent
::__construct($uniqueid);
1500 // some sensible defaults
1501 $this->set_attribute('cellspacing', '0');
1502 $this->set_attribute('class', 'generaltable generalbox');
1506 * Take the data returned from the db_query and go through all the rows
1507 * processing each col using either col_{columnname} method or other_cols
1508 * method or if other_cols returns NULL then put the data straight into the
1511 * After calling this function, don't forget to call close_recordset.
1513 public function build_table() {
1515 if ($this->rawdata
instanceof \Traversable
&& !$this->rawdata
->valid()) {
1518 if (!$this->rawdata
) {
1522 foreach ($this->rawdata
as $row) {
1523 $formattedrow = $this->format_row($row);
1524 $this->add_data_keyed($formattedrow,
1525 $this->get_row_class($row));
1530 * Closes recordset (for use after building the table).
1532 public function close_recordset() {
1533 if ($this->rawdata
&& ($this->rawdata
instanceof \core\dml\recordset_walk ||
1534 $this->rawdata
instanceof moodle_recordset
)) {
1535 $this->rawdata
->close();
1536 $this->rawdata
= null;
1541 * Get any extra classes names to add to this row in the HTML.
1542 * @param $row array the data for this row.
1543 * @return string added to the class="" attribute of the tr.
1545 function get_row_class($row) {
1550 * This is only needed if you want to use different sql to count rows.
1551 * Used for example when perhaps all db JOINS are not needed when counting
1552 * records. You don't need to call this function the count_sql
1553 * will be generated automatically.
1555 * We need to count rows returned by the db seperately to the query itself
1556 * as we need to know how many pages of data we have to display.
1558 function set_count_sql($sql, array $params = NULL) {
1559 $this->countsql
= $sql;
1560 $this->countparams
= $params;
1564 * Set the sql to query the db. Query will be :
1565 * SELECT $fields FROM $from WHERE $where
1566 * Of course you can use sub-queries, JOINS etc. by putting them in the
1567 * appropriate clause of the query.
1569 function set_sql($fields, $from, $where, array $params = array()) {
1570 $this->sql
= new stdClass();
1571 $this->sql
->fields
= $fields;
1572 $this->sql
->from
= $from;
1573 $this->sql
->where
= $where;
1574 $this->sql
->params
= $params;
1578 * Query the db. Store results in the table object for use by build_table.
1580 * @param int $pagesize size of page for paginated displayed table.
1581 * @param bool $useinitialsbar do you want to use the initials bar. Bar
1582 * will only be used if there is a fullname column defined for the table.
1584 function query_db($pagesize, $useinitialsbar=true) {
1586 if (!$this->is_downloading()) {
1587 if ($this->countsql
=== NULL) {
1588 $this->countsql
= 'SELECT COUNT(1) FROM '.$this->sql
->from
.' WHERE '.$this->sql
->where
;
1589 $this->countparams
= $this->sql
->params
;
1591 $grandtotal = $DB->count_records_sql($this->countsql
, $this->countparams
);
1592 if ($useinitialsbar && !$this->is_downloading()) {
1593 $this->initialbars($grandtotal > $pagesize);
1596 list($wsql, $wparams) = $this->get_sql_where();
1598 $this->countsql
.= ' AND '.$wsql;
1599 $this->countparams
= array_merge($this->countparams
, $wparams);
1601 $this->sql
->where
.= ' AND '.$wsql;
1602 $this->sql
->params
= array_merge($this->sql
->params
, $wparams);
1604 $total = $DB->count_records_sql($this->countsql
, $this->countparams
);
1606 $total = $grandtotal;
1609 $this->pagesize($pagesize, $total);
1612 // Fetch the attempts
1613 $sort = $this->get_sql_sort();
1615 $sort = "ORDER BY $sort";
1618 {$this->sql->fields}
1619 FROM {$this->sql->from}
1620 WHERE {$this->sql->where}
1623 if (!$this->is_downloading()) {
1624 $this->rawdata
= $DB->get_records_sql($sql, $this->sql
->params
, $this->get_page_start(), $this->get_page_size());
1626 $this->rawdata
= $DB->get_records_sql($sql, $this->sql
->params
);
1631 * Convenience method to call a number of methods for you to display the
1634 function out($pagesize, $useinitialsbar, $downloadhelpbutton='') {
1636 if (!$this->columns
) {
1637 $onerow = $DB->get_record_sql("SELECT {$this->sql->fields} FROM {$this->sql->from} WHERE {$this->sql->where}",
1638 $this->sql
->params
, IGNORE_MULTIPLE
);
1639 //if columns is not set then define columns as the keys of the rows returned
1641 $this->define_columns(array_keys((array)$onerow));
1642 $this->define_headers(array_keys((array)$onerow));
1645 $this->query_db($pagesize, $useinitialsbar);
1646 $this->build_table();
1647 $this->close_recordset();
1648 $this->finish_output();
1654 * @package moodlecore
1655 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1656 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1658 class table_default_export_format_parent
{
1660 * @var flexible_table or child class reference pointing to table class
1661 * object from which to export data.
1666 * @var bool output started. Keeps track of whether any output has been
1669 var $documentstarted = false;
1674 * @param flexible_table $table
1676 public function __construct(&$table) {
1677 $this->table
=& $table;
1681 * Old syntax of class constructor. Deprecated in PHP7.
1683 * @deprecated since Moodle 3.1
1685 public function table_default_export_format_parent(&$table) {
1686 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER
);
1687 self
::__construct($table);
1690 function set_table(&$table) {
1691 $this->table
=& $table;
1694 function add_data($row) {
1698 function add_seperator() {
1702 function document_started() {
1703 return $this->documentstarted
;
1706 * Given text in a variety of format codings, this function returns
1707 * the text as safe HTML or as plain text dependent on what is appropriate
1708 * for the download format. The default removes all tags.
1710 function format_text($text, $format=FORMAT_MOODLE
, $options=NULL, $courseid=NULL) {
1711 //use some whitespace to indicate where there was some line spacing.
1712 $text = str_replace(array('</p>', "\n", "\r"), ' ', $text);
1713 return strip_tags($text);
1718 * Dataformat exporter
1721 * @subpackage tablelib
1722 * @copyright 2016 Brendan Heywood (brendan@catalyst-au.net)
1723 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1725 class table_dataformat_export_format
extends table_default_export_format_parent
{
1727 /** @var $dataformat */
1728 protected $dataformat;
1731 protected $rownum = 0;
1733 /** @var $columns */
1739 * @param string $table An sql table
1740 * @param string $dataformat type of dataformat for export
1742 public function __construct(&$table, $dataformat) {
1743 parent
::__construct($table);
1745 if (ob_get_length()) {
1746 throw new coding_exception("Output can not be buffered before instantiating table_dataformat_export_format");
1749 $classname = 'dataformat_' . $dataformat . '\writer';
1750 if (!class_exists($classname)) {
1751 throw new coding_exception("Unable to locate dataformat/$dataformat/classes/writer.php");
1753 $this->dataformat
= new $classname;
1755 // The dataformat export time to first byte could take a while to generate...
1758 // Close the session so that the users other tabs in the same session are not blocked.
1759 \core\session\manager
::write_close();
1765 * @param string $filename
1766 * @param string $sheettitle
1768 public function start_document($filename, $sheettitle) {
1769 $this->documentstarted
= true;
1770 $this->dataformat
->set_filename($filename);
1771 $this->dataformat
->send_http_headers();
1772 $this->dataformat
->set_sheettitle($sheettitle);
1773 $this->dataformat
->start_output();
1779 * @param string $sheettitle optional spreadsheet worksheet title
1781 public function start_table($sheettitle) {
1782 $this->dataformat
->set_sheettitle($sheettitle);
1788 * @param array $headers
1790 public function output_headers($headers) {
1791 $this->columns
= $headers;
1792 if (method_exists($this->dataformat
, 'write_header')) {
1793 error_log('The function write_header() does not support multiple sheets. In order to support multiple sheets you ' .
1794 'must implement start_output() and start_sheet() and remove write_header() in your dataformat.');
1795 $this->dataformat
->write_header($headers);
1797 $this->dataformat
->start_sheet($headers);
1804 * @param array $row One record of data
1806 public function add_data($row) {
1807 $this->dataformat
->write_record($row, $this->rownum++
);
1814 public function finish_table() {
1815 if (method_exists($this->dataformat
, 'write_footer')) {
1816 error_log('The function write_footer() does not support multiple sheets. In order to support multiple sheets you ' .
1817 'must implement close_sheet() and close_output() and remove write_footer() in your dataformat.');
1818 $this->dataformat
->write_footer($this->columns
);
1820 $this->dataformat
->close_sheet($this->columns
);
1827 public function finish_document() {
1828 $this->dataformat
->close_output();