MDL-35375: Ensure the assignment grading table is always sorted by at least one uniqu...
[moodle.git] / lib / tablelib.php
blob6de22af776ad59e99c19e1d57aa63eccc4e7f512
1 <?php
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.
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/>.
18 /**
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
26 defined('MOODLE_INTERNAL') || die();
28 /**#@+
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 /**#@-*/
39 /**#@+
40 * Constants that indicate whether the paging bar for the table
41 * appears above or below the table.
43 define('TABLE_P_TOP', 1);
44 define('TABLE_P_BOTTOM', 2);
45 /**#@-*/
48 /**
49 * @package moodlecore
50 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
51 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
53 class flexible_table {
55 var $uniqueid = NULL;
56 var $attributes = array();
57 var $headers = array();
58 var $columns = array();
59 var $column_style = array();
60 var $column_class = array();
61 var $column_suppress = array();
62 var $column_nosort = array('userpic');
63 var $setup = false;
64 var $sess = NULL;
65 var $baseurl = NULL;
66 var $request = array();
68 var $is_collapsible = false;
69 var $is_sortable = false;
70 var $use_pages = false;
71 var $use_initials = false;
73 var $maxsortkeys = 2;
74 var $pagesize = 30;
75 var $currpage = 0;
76 var $totalrows = 0;
77 var $sort_default_column = NULL;
78 var $sort_default_order = SORT_ASC;
80 /**
81 * Array of positions in which to display download controls.
83 var $showdownloadbuttonsat= array(TABLE_P_TOP);
85 /**
86 * @var string Key of field returned by db query that is the id field of the
87 * user table or equivalent.
89 public $useridfield = 'id';
91 /**
92 * @var string which download plugin to use. Default '' means none - print
93 * html table with paging. Property set by is_downloading which typically
94 * passes in cleaned data from $
96 var $download = '';
98 /**
99 * @var bool whether data is downloadable from table. Determines whether
100 * to display download buttons. Set by method downloadable().
102 var $downloadable = false;
105 * @var string which download plugin to use. Default '' means none - print
106 * html table with paging.
108 var $defaultdownloadformat = 'csv';
111 * @var bool Has start output been called yet?
113 var $started_output = false;
115 var $exportclass = null;
118 * Constructor
119 * @param int $uniqueid all tables have to have a unique id, this is used
120 * as a key when storing table properties like sort order in the session.
122 function __construct($uniqueid) {
123 $this->uniqueid = $uniqueid;
124 $this->request = array(
125 TABLE_VAR_SORT => 'tsort',
126 TABLE_VAR_HIDE => 'thide',
127 TABLE_VAR_SHOW => 'tshow',
128 TABLE_VAR_IFIRST => 'tifirst',
129 TABLE_VAR_ILAST => 'tilast',
130 TABLE_VAR_PAGE => 'page',
135 * Call this to pass the download type. Use :
136 * $download = optional_param('download', '', PARAM_ALPHA);
137 * To get the download type. We assume that if you call this function with
138 * params that this table's data is downloadable, so we call is_downloadable
139 * for you (even if the param is '', which means no download this time.
140 * Also you can call this method with no params to get the current set
141 * download type.
142 * @param string $download download type. One of csv, tsv, xhtml, ods, etc
143 * @param string $filename filename for downloads without file extension.
144 * @param string $sheettitle title for downloaded data.
145 * @return string download type. One of csv, tsv, xhtml, ods, etc
147 function is_downloading($download = null, $filename='', $sheettitle='') {
148 if ($download!==null) {
149 $this->sheettitle = $sheettitle;
150 $this->is_downloadable(true);
151 $this->download = $download;
152 $this->filename = clean_filename($filename);
153 $this->export_class_instance();
155 return $this->download;
159 * Get, and optionally set, the export class.
160 * @param $exportclass (optional) if passed, set the table to use this export class.
161 * @return table_default_export_format_parent the export class in use (after any set).
163 function export_class_instance($exportclass = null) {
164 if (!is_null($exportclass)) {
165 $this->started_output = true;
166 $this->exportclass = $exportclass;
167 $this->exportclass->table = $this;
168 } else if (is_null($this->exportclass) && !empty($this->download)) {
169 $classname = 'table_'.$this->download.'_export_format';
170 $this->exportclass = new $classname($this);
171 if (!$this->exportclass->document_started()) {
172 $this->exportclass->start_document($this->filename);
175 return $this->exportclass;
179 * Probably don't need to call this directly. Calling is_downloading with a
180 * param automatically sets table as downloadable.
182 * @param bool $downloadable optional param to set whether data from
183 * table is downloadable. If ommitted this function can be used to get
184 * current state of table.
185 * @return bool whether table data is set to be downloadable.
187 function is_downloadable($downloadable = null) {
188 if ($downloadable !== null) {
189 $this->downloadable = $downloadable;
191 return $this->downloadable;
195 * Where to show download buttons.
196 * @param array $showat array of postions in which to show download buttons.
197 * Containing TABLE_P_TOP and/or TABLE_P_BOTTOM
199 function show_download_buttons_at($showat) {
200 $this->showdownloadbuttonsat = $showat;
204 * Sets the is_sortable variable to the given boolean, sort_default_column to
205 * the given string, and the sort_default_order to the given integer.
206 * @param bool $bool
207 * @param string $defaultcolumn
208 * @param int $defaultorder
209 * @return void
211 function sortable($bool, $defaultcolumn = NULL, $defaultorder = SORT_ASC) {
212 $this->is_sortable = $bool;
213 $this->sort_default_column = $defaultcolumn;
214 $this->sort_default_order = $defaultorder;
218 * Do not sort using this column
219 * @param string column name
221 function no_sorting($column) {
222 $this->column_nosort[] = $column;
226 * Is the column sortable?
227 * @param string column name, null means table
228 * @return bool
230 function is_sortable($column = null) {
231 if (empty($column)) {
232 return $this->is_sortable;
234 if (!$this->is_sortable) {
235 return false;
237 return !in_array($column, $this->column_nosort);
241 * Sets the is_collapsible variable to the given boolean.
242 * @param bool $bool
243 * @return void
245 function collapsible($bool) {
246 $this->is_collapsible = $bool;
250 * Sets the use_pages variable to the given boolean.
251 * @param bool $bool
252 * @return void
254 function pageable($bool) {
255 $this->use_pages = $bool;
259 * Sets the use_initials variable to the given boolean.
260 * @param bool $bool
261 * @return void
263 function initialbars($bool) {
264 $this->use_initials = $bool;
268 * Sets the pagesize variable to the given integer, the totalrows variable
269 * to the given integer, and the use_pages variable to true.
270 * @param int $perpage
271 * @param int $total
272 * @return void
274 function pagesize($perpage, $total) {
275 $this->pagesize = $perpage;
276 $this->totalrows = $total;
277 $this->use_pages = true;
281 * Assigns each given variable in the array to the corresponding index
282 * in the request class variable.
283 * @param array $variables
284 * @return void
286 function set_control_variables($variables) {
287 foreach ($variables as $what => $variable) {
288 if (isset($this->request[$what])) {
289 $this->request[$what] = $variable;
295 * Gives the given $value to the $attribute index of $this->attributes.
296 * @param string $attribute
297 * @param mixed $value
298 * @return void
300 function set_attribute($attribute, $value) {
301 $this->attributes[$attribute] = $value;
305 * What this method does is set the column so that if the same data appears in
306 * consecutive rows, then it is not repeated.
308 * For example, in the quiz overview report, the fullname column is set to be suppressed, so
309 * that when one student has made multiple attempts, their name is only printed in the row
310 * for their first attempt.
311 * @param int $column the index of a column.
313 function column_suppress($column) {
314 if (isset($this->column_suppress[$column])) {
315 $this->column_suppress[$column] = true;
320 * Sets the given $column index to the given $classname in $this->column_class.
321 * @param int $column
322 * @param string $classname
323 * @return void
325 function column_class($column, $classname) {
326 if (isset($this->column_class[$column])) {
327 $this->column_class[$column] = ' '.$classname; // This space needed so that classnames don't run together in the HTML
332 * Sets the given $column index and $property index to the given $value in $this->column_style.
333 * @param int $column
334 * @param string $property
335 * @param mixed $value
336 * @return void
338 function column_style($column, $property, $value) {
339 if (isset($this->column_style[$column])) {
340 $this->column_style[$column][$property] = $value;
345 * Sets all columns' $propertys to the given $value in $this->column_style.
346 * @param int $property
347 * @param string $value
348 * @return void
350 function column_style_all($property, $value) {
351 foreach (array_keys($this->columns) as $column) {
352 $this->column_style[$column][$property] = $value;
357 * Sets $this->baseurl.
358 * @param moodle_url|string $url the url with params needed to call up this page
360 function define_baseurl($url) {
361 $this->baseurl = new moodle_url($url);
365 * @param array $columns an array of identifying names for columns. If
366 * columns are sorted then column names must correspond to a field in sql.
368 function define_columns($columns) {
369 $this->columns = array();
370 $this->column_style = array();
371 $this->column_class = array();
372 $colnum = 0;
374 foreach ($columns as $column) {
375 $this->columns[$column] = $colnum++;
376 $this->column_style[$column] = array();
377 $this->column_class[$column] = '';
378 $this->column_suppress[$column] = false;
383 * @param array $headers numerical keyed array of displayed string titles
384 * for each column.
386 function define_headers($headers) {
387 $this->headers = $headers;
391 * Must be called after table is defined. Use methods above first. Cannot
392 * use functions below till after calling this method.
393 * @return type?
395 function setup() {
396 global $SESSION, $CFG;
398 if (empty($this->columns) || empty($this->uniqueid)) {
399 return false;
402 if (!isset($SESSION->flextable)) {
403 $SESSION->flextable = array();
406 if (!isset($SESSION->flextable[$this->uniqueid])) {
407 $SESSION->flextable[$this->uniqueid] = new stdClass;
408 $SESSION->flextable[$this->uniqueid]->uniqueid = $this->uniqueid;
409 $SESSION->flextable[$this->uniqueid]->collapse = array();
410 $SESSION->flextable[$this->uniqueid]->sortby = array();
411 $SESSION->flextable[$this->uniqueid]->i_first = '';
412 $SESSION->flextable[$this->uniqueid]->i_last = '';
415 $this->sess = &$SESSION->flextable[$this->uniqueid];
417 if (($showcol = optional_param($this->request[TABLE_VAR_SHOW], '', PARAM_ALPHANUMEXT)) &&
418 isset($this->columns[$showcol])) {
419 $this->sess->collapse[$showcol] = false;
421 } else if (($hidecol = optional_param($this->request[TABLE_VAR_HIDE], '', PARAM_ALPHANUMEXT)) &&
422 isset($this->columns[$hidecol])) {
423 $this->sess->collapse[$hidecol] = true;
424 if (array_key_exists($hidecol, $this->sess->sortby)) {
425 unset($this->sess->sortby[$hidecol]);
429 // Now, update the column attributes for collapsed columns
430 foreach (array_keys($this->columns) as $column) {
431 if (!empty($this->sess->collapse[$column])) {
432 $this->column_style[$column]['width'] = '10px';
436 if (($sortcol = optional_param($this->request[TABLE_VAR_SORT], '', PARAM_ALPHANUMEXT)) &&
437 $this->is_sortable($sortcol) && empty($this->sess->collapse[$sortcol]) &&
438 (isset($this->columns[$sortcol]) || in_array($sortcol, array('firstname', 'lastname')) && isset($this->columns['fullname']))) {
440 if (array_key_exists($sortcol, $this->sess->sortby)) {
441 // This key already exists somewhere. Change its sortorder and bring it to the top.
442 $sortorder = $this->sess->sortby[$sortcol] == SORT_ASC ? SORT_DESC : SORT_ASC;
443 unset($this->sess->sortby[$sortcol]);
444 $this->sess->sortby = array_merge(array($sortcol => $sortorder), $this->sess->sortby);
445 } else {
446 // Key doesn't exist, so just add it to the beginning of the array, ascending order
447 $this->sess->sortby = array_merge(array($sortcol => SORT_ASC), $this->sess->sortby);
450 // Finally, make sure that no more than $this->maxsortkeys are present into the array
451 $this->sess->sortby = array_slice($this->sess->sortby, 0, $this->maxsortkeys);
454 // 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.
455 // This prevents results from being returned in a random order if the only order by column contains equal values.
456 if (!empty($this->sort_default_column)) {
457 if (!array_key_exists($this->sort_default_column, $this->sess->sortby)) {
458 $defaultsort = array($this->sort_default_column => $this->sort_default_order);
459 $this->sess->sortby = array_merge($this->sess->sortby, $defaultsort);
463 $ilast = optional_param($this->request[TABLE_VAR_ILAST], null, PARAM_RAW);
464 if (!is_null($ilast) && ($ilast ==='' || strpos(get_string('alphabet', 'langconfig'), $ilast) !== false)) {
465 $this->sess->i_last = $ilast;
468 $ifirst = optional_param($this->request[TABLE_VAR_IFIRST], null, PARAM_RAW);
469 if (!is_null($ifirst) && ($ifirst === '' || strpos(get_string('alphabet', 'langconfig'), $ifirst) !== false)) {
470 $this->sess->i_first = $ifirst;
473 if (empty($this->baseurl)) {
474 debugging('You should set baseurl when using flexible_table.');
475 global $PAGE;
476 $this->baseurl = $PAGE->url;
479 $this->currpage = optional_param($this->request[TABLE_VAR_PAGE], 0, PARAM_INT);
480 $this->setup = true;
482 // Always introduce the "flexible" class for the table if not specified
483 if (empty($this->attributes)) {
484 $this->attributes['class'] = 'flexible';
485 } else if (!isset($this->attributes['class'])) {
486 $this->attributes['class'] = 'flexible';
487 } else if (!in_array('flexible', explode(' ', $this->attributes['class']))) {
488 $this->attributes['class'] = trim('flexible ' . $this->attributes['class']);
493 * Get the order by clause from the session, for the table with id $uniqueid.
494 * @param string $uniqueid the identifier for a table.
495 * @return SQL fragment that can be used in an ORDER BY clause.
497 public static function get_sort_for_table($uniqueid) {
498 global $SESSION;
499 if (empty($SESSION->flextable[$uniqueid])) {
500 return '';
503 $sess = &$SESSION->flextable[$uniqueid];
504 if (empty($sess->sortby)) {
505 return '';
508 return self::construct_order_by($sess->sortby);
512 * Prepare an an order by clause from the list of columns to be sorted.
513 * @param array $cols column name => SORT_ASC or SORT_DESC
514 * @return SQL fragment that can be used in an ORDER BY clause.
516 public static function construct_order_by($cols) {
517 $bits = array();
519 foreach ($cols as $column => $order) {
520 if ($order == SORT_ASC) {
521 $bits[] = $column . ' ASC';
522 } else {
523 $bits[] = $column . ' DESC';
527 return implode(', ', $bits);
531 * @return SQL fragment that can be used in an ORDER BY clause.
533 public function get_sql_sort() {
534 return self::construct_order_by($this->get_sort_columns());
538 * Get the columns to sort by, in the form required by {@link construct_order_by()}.
539 * @return array column name => SORT_... constant.
541 public function get_sort_columns() {
542 if (!$this->setup) {
543 throw new coding_exception('Cannot call get_sort_columns until you have called setup.');
546 if (empty($this->sess->sortby)) {
547 return array();
550 foreach ($this->sess->sortby as $column => $notused) {
551 if (isset($this->columns[$column])) {
552 continue; // This column is OK.
554 if (in_array($column, array('firstname', 'lastname')) &&
555 isset($this->columns['fullname'])) {
556 continue; // This column is OK.
558 // This column is not OK.
559 unset($this->sess->sortby[$column]);
562 return $this->sess->sortby;
566 * @return int the offset for LIMIT clause of SQL
568 function get_page_start() {
569 if (!$this->use_pages) {
570 return '';
572 return $this->currpage * $this->pagesize;
576 * @return int the pagesize for LIMIT clause of SQL
578 function get_page_size() {
579 if (!$this->use_pages) {
580 return '';
582 return $this->pagesize;
586 * @return string sql to add to where statement.
588 function get_sql_where() {
589 global $DB;
591 $conditions = array();
592 $params = array();
594 if (isset($this->columns['fullname'])) {
595 static $i = 0;
596 $i++;
598 if (!empty($this->sess->i_first)) {
599 $conditions[] = $DB->sql_like('firstname', ':ifirstc'.$i, false, false);
600 $params['ifirstc'.$i] = $this->sess->i_first.'%';
602 if (!empty($this->sess->i_last)) {
603 $conditions[] = $DB->sql_like('lastname', ':ilastc'.$i, false, false);
604 $params['ilastc'.$i] = $this->sess->i_last.'%';
608 return array(implode(" AND ", $conditions), $params);
612 * Add a row of data to the table. This function takes an array with
613 * column names as keys.
614 * It ignores any elements with keys that are not defined as columns. It
615 * puts in empty strings into the row when there is no element in the passed
616 * array corresponding to a column in the table. It puts the row elements in
617 * the proper order.
618 * @param $rowwithkeys array
619 * @param string $classname CSS class name to add to this row's tr tag.
621 function add_data_keyed($rowwithkeys, $classname = '') {
622 $this->add_data($this->get_row_from_keyed($rowwithkeys), $classname);
626 * Add a seperator line to table.
628 function add_separator() {
629 if (!$this->setup) {
630 return false;
632 $this->add_data(NULL);
636 * This method actually directly echoes the row passed to it now or adds it
637 * to the download. If this is the first row and start_output has not
638 * already been called this method also calls start_output to open the table
639 * or send headers for the downloaded.
640 * Can be used as before. print_html now calls finish_html to close table.
642 * @param array $row a numerically keyed row of data to add to the table.
643 * @param string $classname CSS class name to add to this row's tr tag.
644 * @return bool success.
646 function add_data($row, $classname = '') {
647 if (!$this->setup) {
648 return false;
650 if (!$this->started_output) {
651 $this->start_output();
653 if ($this->exportclass!==null) {
654 if ($row === null) {
655 $this->exportclass->add_seperator();
656 } else {
657 $this->exportclass->add_data($row);
659 } else {
660 $this->print_row($row, $classname);
662 return true;
666 * You should call this to finish outputting the table data after adding
667 * data to the table with add_data or add_data_keyed.
670 function finish_output($closeexportclassdoc = true) {
671 if ($this->exportclass!==null) {
672 $this->exportclass->finish_table();
673 if ($closeexportclassdoc) {
674 $this->exportclass->finish_document();
676 } else {
677 $this->finish_html();
682 * Hook that can be overridden in child classes to wrap a table in a form
683 * for example. Called only when there is data to display and not
684 * downloading.
686 function wrap_html_start() {
690 * Hook that can be overridden in child classes to wrap a table in a form
691 * for example. Called only when there is data to display and not
692 * downloading.
694 function wrap_html_finish() {
699 * @param array $row row of data from db used to make one row of the table.
700 * @return array one row for the table, added using add_data_keyed method.
702 function format_row($row) {
703 $formattedrow = array();
704 foreach (array_keys($this->columns) as $column) {
705 $colmethodname = 'col_'.$column;
706 if (method_exists($this, $colmethodname)) {
707 $formattedcolumn = $this->$colmethodname($row);
708 } else {
709 $formattedcolumn = $this->other_cols($column, $row);
710 if ($formattedcolumn===NULL) {
711 $formattedcolumn = $row->$column;
714 $formattedrow[$column] = $formattedcolumn;
716 return $formattedrow;
720 * Fullname is treated as a special columname in tablelib and should always
721 * be treated the same as the fullname of a user.
722 * @uses $this->useridfield if the userid field is not expected to be id
723 * then you need to override $this->useridfield to point at the correct
724 * field for the user id.
727 function col_fullname($row) {
728 global $COURSE, $CFG;
730 if (!$this->download) {
731 $profileurl = new moodle_url('/user/profile.php', array('id' => $row->{$this->useridfield}));
732 if ($COURSE->id != SITEID) {
733 $profileurl->param('course', $COURSE->id);
735 return html_writer::link($profileurl, fullname($row));
737 } else {
738 return fullname($row);
743 * You can override this method in a child class. See the description of
744 * build_table which calls this method.
746 function other_cols($column, $row) {
747 return NULL;
751 * Used from col_* functions when text is to be displayed. Does the
752 * right thing - either converts text to html or strips any html tags
753 * depending on if we are downloading and what is the download type. Params
754 * are the same as format_text function in weblib.php but some default
755 * options are changed.
757 function format_text($text, $format=FORMAT_MOODLE, $options=NULL, $courseid=NULL) {
758 if (!$this->is_downloading()) {
759 if (is_null($options)) {
760 $options = new stdClass;
762 //some sensible defaults
763 if (!isset($options->para)) {
764 $options->para = false;
766 if (!isset($options->newlines)) {
767 $options->newlines = false;
769 if (!isset($options->smiley)) {
770 $options->smiley = false;
772 if (!isset($options->filter)) {
773 $options->filter = false;
775 return format_text($text, $format, $options);
776 } else {
777 $eci =& $this->export_class_instance();
778 return $eci->format_text($text, $format, $options, $courseid);
782 * This method is deprecated although the old api is still supported.
783 * @deprecated 1.9.2 - Jun 2, 2008
785 function print_html() {
786 if (!$this->setup) {
787 return false;
789 $this->finish_html();
793 * This function is not part of the public api.
794 * @return string initial of first name we are currently filtering by
796 function get_initial_first() {
797 if (!$this->use_initials) {
798 return NULL;
801 return $this->sess->i_first;
805 * This function is not part of the public api.
806 * @return string initial of last name we are currently filtering by
808 function get_initial_last() {
809 if (!$this->use_initials) {
810 return NULL;
813 return $this->sess->i_last;
817 * Helper function, used by {@link print_initials_bar()} to output one initial bar.
818 * @param array $alpha of letters in the alphabet.
819 * @param string $current the currently selected letter.
820 * @param string $class class name to add to this initial bar.
821 * @param string $title the name to put in front of this initial bar.
822 * @param string $urlvar URL parameter name for this initial.
824 protected function print_one_initials_bar($alpha, $current, $class, $title, $urlvar) {
825 echo html_writer::start_tag('div', array('class' => 'initialbar ' . $class)) .
826 $title . ' : ';
827 if ($current) {
828 echo html_writer::link($this->baseurl->out(false, array($urlvar => '')), get_string('all'));
829 } else {
830 echo html_writer::tag('strong', get_string('all'));
833 foreach ($alpha as $letter) {
834 if ($letter === $current) {
835 echo html_writer::tag('strong', $letter);
836 } else {
837 echo html_writer::link($this->baseurl->out(false, array($urlvar => $letter)), $letter);
841 echo html_writer::end_tag('div');
845 * This function is not part of the public api.
847 function print_initials_bar() {
848 if ((!empty($this->sess->i_last) || !empty($this->sess->i_first) ||$this->use_initials)
849 && isset($this->columns['fullname'])) {
851 $alpha = explode(',', get_string('alphabet', 'langconfig'));
853 // Bar of first initials
854 if (!empty($this->sess->i_first)) {
855 $ifirst = $this->sess->i_first;
856 } else {
857 $ifirst = '';
859 $this->print_one_initials_bar($alpha, $ifirst, 'firstinitial',
860 get_string('firstname'), $this->request[TABLE_VAR_IFIRST]);
862 // Bar of last initials
863 if (!empty($this->sess->i_last)) {
864 $ilast = $this->sess->i_last;
865 } else {
866 $ilast = '';
868 $this->print_one_initials_bar($alpha, $ilast, 'lastinitial',
869 get_string('lastname'), $this->request[TABLE_VAR_ILAST]);
874 * This function is not part of the public api.
876 function print_nothing_to_display() {
877 global $OUTPUT;
878 $this->print_initials_bar();
880 echo $OUTPUT->heading(get_string('nothingtodisplay'));
884 * This function is not part of the public api.
886 function get_row_from_keyed($rowwithkeys) {
887 if (is_object($rowwithkeys)) {
888 $rowwithkeys = (array)$rowwithkeys;
890 $row = array();
891 foreach (array_keys($this->columns) as $column) {
892 if (isset($rowwithkeys[$column])) {
893 $row [] = $rowwithkeys[$column];
894 } else {
895 $row[] ='';
898 return $row;
901 * This function is not part of the public api.
903 function get_download_menu() {
904 $allclasses= get_declared_classes();
905 $exportclasses = array();
906 foreach ($allclasses as $class) {
907 $matches = array();
908 if (preg_match('/^table\_([a-z]+)\_export\_format$/', $class, $matches)) {
909 $type = $matches[1];
910 $exportclasses[$type]= get_string("download$type", 'table');
913 return $exportclasses;
917 * This function is not part of the public api.
919 function download_buttons() {
920 if ($this->is_downloadable() && !$this->is_downloading()) {
921 $downloadoptions = $this->get_download_menu();
922 $html = '<form action="'. $this->baseurl .'" method="post">';
923 $html .= '<div class="mdl-align">';
924 $html .= '<input type="submit" value="'.get_string('downloadas', 'table').'"/>';
925 $html .= html_writer::label(get_string('downloadoptions', 'table'), 'menudownload', false, array('class' => 'accesshide'));
926 $html .= html_writer::select($downloadoptions, 'download', $this->defaultdownloadformat, false);
927 $html .= '</div></form>';
929 return $html;
930 } else {
931 return '';
935 * This function is not part of the public api.
936 * You don't normally need to call this. It is called automatically when
937 * needed when you start adding data to the table.
940 function start_output() {
941 $this->started_output = true;
942 if ($this->exportclass!==null) {
943 $this->exportclass->start_table($this->sheettitle);
944 $this->exportclass->output_headers($this->headers);
945 } else {
946 $this->start_html();
947 $this->print_headers();
952 * This function is not part of the public api.
954 function print_row($row, $classname = '') {
955 static $suppress_lastrow = NULL;
956 static $oddeven = 1;
957 $rowclasses = array('r' . $oddeven);
958 $oddeven = $oddeven ? 0 : 1;
960 if ($classname) {
961 $rowclasses[] = $classname;
964 echo html_writer::start_tag('tr', array('class' => implode(' ', $rowclasses)));
966 // If we have a separator, print it
967 if ($row === NULL) {
968 $colcount = count($this->columns);
969 echo html_writer::tag('td', html_writer::tag('div', '',
970 array('class' => 'tabledivider')), array('colspan' => $colcount));
972 } else {
973 $colbyindex = array_flip($this->columns);
974 foreach ($row as $index => $data) {
975 $column = $colbyindex[$index];
977 if (empty($this->sess->collapse[$column])) {
978 if ($this->column_suppress[$column] && $suppress_lastrow !== NULL && $suppress_lastrow[$index] === $data) {
979 $content = '&nbsp;';
980 } else {
981 $content = $data;
983 } else {
984 $content = '&nbsp;';
987 echo html_writer::tag('td', $content, array(
988 'class' => 'cell c' . $index . $this->column_class[$column],
989 'style' => $this->make_styles_string($this->column_style[$column])));
993 echo html_writer::end_tag('tr');
995 $suppress_enabled = array_sum($this->column_suppress);
996 if ($suppress_enabled) {
997 $suppress_lastrow = $row;
1002 * This function is not part of the public api.
1004 function finish_html() {
1005 global $OUTPUT;
1006 if (!$this->started_output) {
1007 //no data has been added to the table.
1008 $this->print_nothing_to_display();
1010 } else {
1011 echo html_writer::end_tag('table');
1012 echo html_writer::end_tag('div');
1013 $this->wrap_html_finish();
1015 // Paging bar
1016 if(in_array(TABLE_P_BOTTOM, $this->showdownloadbuttonsat)) {
1017 echo $this->download_buttons();
1020 if($this->use_pages) {
1021 $pagingbar = new paging_bar($this->totalrows, $this->currpage, $this->pagesize, $this->baseurl);
1022 $pagingbar->pagevar = $this->request[TABLE_VAR_PAGE];
1023 echo $OUTPUT->render($pagingbar);
1029 * Generate the HTML for the collapse/uncollapse icon. This is a helper method
1030 * used by {@link print_headers()}.
1031 * @param string $column the column name, index into various names.
1032 * @param int $index numerical index of the column.
1033 * @return string HTML fragment.
1035 protected function show_hide_link($column, $index) {
1036 global $OUTPUT;
1037 // Some headers contain <br /> tags, do not include in title, hence the
1038 // strip tags.
1040 if (!empty($this->sess->collapse[$column])) {
1041 return html_writer::link($this->baseurl->out(false, array($this->request[TABLE_VAR_SHOW] => $column)),
1042 html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url('t/switch_plus'), 'alt' => get_string('show'))),
1043 array('title' => get_string('show') . ' ' . strip_tags($this->headers[$index])));
1045 } else if ($this->headers[$index] !== NULL) {
1046 return html_writer::link($this->baseurl->out(false, array($this->request[TABLE_VAR_HIDE] => $column)),
1047 html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url('t/switch_minus'), 'alt' => get_string('hide'))),
1048 array('title' => get_string('hide') . ' ' . strip_tags($this->headers[$index])));
1053 * This function is not part of the public api.
1055 function print_headers() {
1056 global $CFG, $OUTPUT;
1058 echo html_writer::start_tag('tr');
1059 foreach ($this->columns as $column => $index) {
1061 $icon_hide = '';
1062 if ($this->is_collapsible) {
1063 $icon_hide = $this->show_hide_link($column, $index);
1066 $primary_sort_column = '';
1067 $primary_sort_order = '';
1068 if (reset($this->sess->sortby)) {
1069 $primary_sort_column = key($this->sess->sortby);
1070 $primary_sort_order = current($this->sess->sortby);
1073 switch ($column) {
1075 case 'fullname':
1076 if ($this->is_sortable($column)) {
1077 $firstnamesortlink = $this->sort_link(get_string('firstname'),
1078 'firstname', $primary_sort_column === 'firstname', $primary_sort_order);
1080 $lastnamesortlink = $this->sort_link(get_string('lastname'),
1081 'lastname', $primary_sort_column === 'lastname', $primary_sort_order);
1083 $override = new stdClass();
1084 $override->firstname = 'firstname';
1085 $override->lastname = 'lastname';
1086 $fullnamelanguage = get_string('fullnamedisplay', '', $override);
1088 if (($CFG->fullnamedisplay == 'firstname lastname') or
1089 ($CFG->fullnamedisplay == 'firstname') or
1090 ($CFG->fullnamedisplay == 'language' and $fullnamelanguage == 'firstname lastname' )) {
1091 $this->headers[$index] = $firstnamesortlink . ' / ' . $lastnamesortlink;
1092 } else {
1093 $this->headers[$index] = $lastnamesortlink . ' / ' . $firstnamesortlink;
1096 break;
1098 case 'userpic':
1099 // do nothing, do not display sortable links
1100 break;
1102 default:
1103 if ($this->is_sortable($column)) {
1104 $this->headers[$index] = $this->sort_link($this->headers[$index],
1105 $column, $primary_sort_column == $column, $primary_sort_order);
1109 $attributes = array(
1110 'class' => 'header c' . $index . $this->column_class[$column],
1111 'scope' => 'col',
1113 if ($this->headers[$index] === NULL) {
1114 $content = '&nbsp;';
1115 } else if (!empty($this->sess->collapse[$column])) {
1116 $content = $icon_hide;
1117 } else {
1118 if (is_array($this->column_style[$column])) {
1119 $attributes['style'] = $this->make_styles_string($this->column_style[$column]);
1121 $content = $this->headers[$index] . html_writer::tag('div',
1122 $icon_hide, array('class' => 'commands'));
1124 echo html_writer::tag('th', $content, $attributes);
1127 echo html_writer::end_tag('tr');
1131 * Generate the HTML for the sort icon. This is a helper method used by {@link sort_link()}.
1132 * @param bool $isprimary whether an icon is needed (it is only needed for the primary sort column.)
1133 * @param int $order SORT_ASC or SORT_DESC
1134 * @return string HTML fragment.
1136 protected function sort_icon($isprimary, $order) {
1137 global $OUTPUT;
1139 if (!$isprimary) {
1140 return '';
1143 if ($order == SORT_ASC) {
1144 return html_writer::empty_tag('img',
1145 array('src' => $OUTPUT->pix_url('t/down'), 'alt' => get_string('asc')));
1146 } else {
1147 return html_writer::empty_tag('img',
1148 array('src' => $OUTPUT->pix_url('t/up'), 'alt' => get_string('desc')));
1153 * Generate the correct tool tip for changing the sort order. This is a
1154 * helper method used by {@link sort_link()}.
1155 * @param bool $isprimary whether the is column is the current primary sort column.
1156 * @param int $order SORT_ASC or SORT_DESC
1157 * @return string the correct title.
1159 protected function sort_order_name($isprimary, $order) {
1160 if ($isprimary && $order != SORT_ASC) {
1161 return get_string('desc');
1162 } else {
1163 return get_string('asc');
1168 * Generate the HTML for the sort link. This is a helper method used by {@link print_headers()}.
1169 * @param string $text the text for the link.
1170 * @param string $column the column name, may be a fake column like 'firstname' or a real one.
1171 * @param bool $isprimary whether the is column is the current primary sort column.
1172 * @param int $order SORT_ASC or SORT_DESC
1173 * @return string HTML fragment.
1175 protected function sort_link($text, $column, $isprimary, $order) {
1176 return html_writer::link($this->baseurl->out(false,
1177 array($this->request[TABLE_VAR_SORT] => $column)),
1178 $text . get_accesshide(get_string('sortby') . ' ' .
1179 $text . ' ' . $this->sort_order_name($isprimary, $order))) . ' ' .
1180 $this->sort_icon($isprimary, $order);
1184 * This function is not part of the public api.
1186 function start_html() {
1187 global $OUTPUT;
1188 // Do we need to print initial bars?
1189 $this->print_initials_bar();
1191 // Paging bar
1192 if ($this->use_pages) {
1193 $pagingbar = new paging_bar($this->totalrows, $this->currpage, $this->pagesize, $this->baseurl);
1194 $pagingbar->pagevar = $this->request[TABLE_VAR_PAGE];
1195 echo $OUTPUT->render($pagingbar);
1198 if (in_array(TABLE_P_TOP, $this->showdownloadbuttonsat)) {
1199 echo $this->download_buttons();
1202 $this->wrap_html_start();
1203 // Start of main data table
1205 echo html_writer::start_tag('div', array('class' => 'no-overflow'));
1206 echo html_writer::start_tag('table', $this->attributes);
1211 * This function is not part of the public api.
1212 * @param array $styles CSS-property => value
1213 * @return string values suitably to go in a style="" attribute in HTML.
1215 function make_styles_string($styles) {
1216 if (empty($styles)) {
1217 return null;
1220 $string = '';
1221 foreach($styles as $property => $value) {
1222 $string .= $property . ':' . $value . ';';
1224 return $string;
1230 * @package moodlecore
1231 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1232 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1234 class table_sql extends flexible_table {
1236 public $countsql = NULL;
1237 public $countparams = NULL;
1239 * @var object sql for querying db. Has fields 'fields', 'from', 'where', 'params'.
1241 public $sql = NULL;
1243 * @var array Data fetched from the db.
1245 public $rawdata = NULL;
1248 * @var bool Overriding default for this.
1250 public $is_sortable = true;
1252 * @var bool Overriding default for this.
1254 public $is_collapsible = true;
1257 * @param string $uniqueid a string identifying this table.Used as a key in
1258 * session vars.
1260 function __construct($uniqueid) {
1261 parent::__construct($uniqueid);
1262 // some sensible defaults
1263 $this->set_attribute('cellspacing', '0');
1264 $this->set_attribute('class', 'generaltable generalbox');
1268 * Take the data returned from the db_query and go through all the rows
1269 * processing each col using either col_{columnname} method or other_cols
1270 * method or if other_cols returns NULL then put the data straight into the
1271 * table.
1273 function build_table() {
1274 if ($this->rawdata) {
1275 foreach ($this->rawdata as $row) {
1276 $formattedrow = $this->format_row($row);
1277 $this->add_data_keyed($formattedrow,
1278 $this->get_row_class($row));
1284 * Get any extra classes names to add to this row in the HTML.
1285 * @param $row array the data for this row.
1286 * @return string added to the class="" attribute of the tr.
1288 function get_row_class($row) {
1289 return '';
1293 * This is only needed if you want to use different sql to count rows.
1294 * Used for example when perhaps all db JOINS are not needed when counting
1295 * records. You don't need to call this function the count_sql
1296 * will be generated automatically.
1298 * We need to count rows returned by the db seperately to the query itself
1299 * as we need to know how many pages of data we have to display.
1301 function set_count_sql($sql, array $params = NULL) {
1302 $this->countsql = $sql;
1303 $this->countparams = $params;
1307 * Set the sql to query the db. Query will be :
1308 * SELECT $fields FROM $from WHERE $where
1309 * Of course you can use sub-queries, JOINS etc. by putting them in the
1310 * appropriate clause of the query.
1312 function set_sql($fields, $from, $where, array $params = NULL) {
1313 $this->sql = new stdClass();
1314 $this->sql->fields = $fields;
1315 $this->sql->from = $from;
1316 $this->sql->where = $where;
1317 $this->sql->params = $params;
1321 * Query the db. Store results in the table object for use by build_table.
1323 * @param int $pagesize size of page for paginated displayed table.
1324 * @param bool $useinitialsbar do you want to use the initials bar. Bar
1325 * will only be used if there is a fullname column defined for the table.
1327 function query_db($pagesize, $useinitialsbar=true) {
1328 global $DB;
1329 if (!$this->is_downloading()) {
1330 if ($this->countsql === NULL) {
1331 $this->countsql = 'SELECT COUNT(1) FROM '.$this->sql->from.' WHERE '.$this->sql->where;
1332 $this->countparams = $this->sql->params;
1334 $grandtotal = $DB->count_records_sql($this->countsql, $this->countparams);
1335 if ($useinitialsbar && !$this->is_downloading()) {
1336 $this->initialbars($grandtotal > $pagesize);
1339 list($wsql, $wparams) = $this->get_sql_where();
1340 if ($wsql) {
1341 $this->countsql .= ' AND '.$wsql;
1342 $this->countparams = array_merge($this->countparams, $wparams);
1344 $this->sql->where .= ' AND '.$wsql;
1345 $this->sql->params = array_merge($this->sql->params, $wparams);
1347 $total = $DB->count_records_sql($this->countsql, $this->countparams);
1348 } else {
1349 $total = $grandtotal;
1352 $this->pagesize($pagesize, $total);
1355 // Fetch the attempts
1356 $sort = $this->get_sql_sort();
1357 if ($sort) {
1358 $sort = "ORDER BY $sort";
1360 $sql = "SELECT
1361 {$this->sql->fields}
1362 FROM {$this->sql->from}
1363 WHERE {$this->sql->where}
1364 {$sort}";
1366 if (!$this->is_downloading()) {
1367 $this->rawdata = $DB->get_records_sql($sql, $this->sql->params, $this->get_page_start(), $this->get_page_size());
1368 } else {
1369 $this->rawdata = $DB->get_records_sql($sql, $this->sql->params);
1374 * Convenience method to call a number of methods for you to display the
1375 * table.
1377 function out($pagesize, $useinitialsbar, $downloadhelpbutton='') {
1378 global $DB;
1379 if (!$this->columns) {
1380 $onerow = $DB->get_record_sql("SELECT {$this->sql->fields} FROM {$this->sql->from} WHERE {$this->sql->where}", $this->sql->params);
1381 //if columns is not set then define columns as the keys of the rows returned
1382 //from the db.
1383 $this->define_columns(array_keys((array)$onerow));
1384 $this->define_headers(array_keys((array)$onerow));
1386 $this->setup();
1387 $this->query_db($pagesize, $useinitialsbar);
1388 $this->build_table();
1389 $this->finish_output();
1395 * @package moodlecore
1396 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1397 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1399 class table_default_export_format_parent {
1401 * @var flexible_table or child class reference pointing to table class
1402 * object from which to export data.
1404 var $table;
1407 * @var bool output started. Keeps track of whether any output has been
1408 * started yet.
1410 var $documentstarted = false;
1411 function table_default_export_format_parent(&$table) {
1412 $this->table =& $table;
1415 function set_table(&$table) {
1416 $this->table =& $table;
1419 function add_data($row) {
1420 return false;
1423 function add_seperator() {
1424 return false;
1427 function document_started() {
1428 return $this->documentstarted;
1431 * Given text in a variety of format codings, this function returns
1432 * the text as safe HTML or as plain text dependent on what is appropriate
1433 * for the download format. The default removes all tags.
1435 function format_text($text, $format=FORMAT_MOODLE, $options=NULL, $courseid=NULL) {
1436 //use some whitespace to indicate where there was some line spacing.
1437 $text = str_replace(array('</p>', "\n", "\r"), ' ', $text);
1438 return strip_tags($text);
1444 * @package moodlecore
1445 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1446 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1448 class table_spreadsheet_export_format_parent extends table_default_export_format_parent {
1449 var $rownum;
1450 var $workbook;
1451 var $worksheet;
1453 * @var object format object - format for normal table cells
1455 var $formatnormal;
1457 * @var object format object - format for header table cells
1459 var $formatheaders;
1462 * should be overriden in child class.
1464 var $fileextension;
1467 * This method will be overridden in the child class.
1469 function define_workbook() {
1472 function start_document($filename) {
1473 $filename = $filename.'.'.$this->fileextension;
1474 $this->define_workbook();
1475 // format types
1476 $this->formatnormal =& $this->workbook->add_format();
1477 $this->formatnormal->set_bold(0);
1478 $this->formatheaders =& $this->workbook->add_format();
1479 $this->formatheaders->set_bold(1);
1480 $this->formatheaders->set_align('center');
1481 // Sending HTTP headers
1482 $this->workbook->send($filename);
1483 $this->documentstarted = true;
1486 function start_table($sheettitle) {
1487 $this->worksheet =& $this->workbook->add_worksheet($sheettitle);
1488 $this->rownum=0;
1491 function output_headers($headers) {
1492 $colnum = 0;
1493 foreach ($headers as $item) {
1494 $this->worksheet->write($this->rownum,$colnum,$item,$this->formatheaders);
1495 $colnum++;
1497 $this->rownum++;
1500 function add_data($row) {
1501 $colnum = 0;
1502 foreach ($row as $item) {
1503 $this->worksheet->write($this->rownum,$colnum,$item,$this->formatnormal);
1504 $colnum++;
1506 $this->rownum++;
1507 return true;
1510 function add_seperator() {
1511 $this->rownum++;
1512 return true;
1515 function finish_table() {
1518 function finish_document() {
1519 $this->workbook->close();
1520 exit;
1526 * @package moodlecore
1527 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1528 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1530 class table_excel_export_format extends table_spreadsheet_export_format_parent {
1531 var $fileextension = 'xls';
1533 function define_workbook() {
1534 global $CFG;
1535 require_once("$CFG->libdir/excellib.class.php");
1536 // Creating a workbook
1537 $this->workbook = new MoodleExcelWorkbook("-");
1544 * @package moodlecore
1545 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1546 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1548 class table_ods_export_format extends table_spreadsheet_export_format_parent {
1549 var $fileextension = 'ods';
1550 function define_workbook() {
1551 global $CFG;
1552 require_once("$CFG->libdir/odslib.class.php");
1553 // Creating a workbook
1554 $this->workbook = new MoodleODSWorkbook("-");
1560 * @package moodlecore
1561 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1562 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1564 class table_text_export_format_parent extends table_default_export_format_parent {
1565 protected $seperator = "\t";
1566 protected $mimetype = 'text/tab-separated-values';
1567 protected $ext = '.txt';
1569 public function start_document($filename) {
1570 $this->filename = $filename . $this->ext;
1571 header('Content-Type: ' . $this->mimetype . '; charset=UTF-8');
1572 header('Content-Disposition: attachment; filename="' . $this->filename . '"');
1573 header('Expires: 0');
1574 header('Cache-Control: must-revalidate,post-check=0,pre-check=0');
1575 header('Pragma: public');
1576 $this->documentstarted = true;
1579 public function start_table($sheettitle) {
1580 //nothing to do here
1583 public function output_headers($headers) {
1584 echo $this->format_row($headers);
1587 public function add_data($row) {
1588 echo $this->format_row($row);
1589 return true;
1592 public function finish_table() {
1593 echo "\n\n";
1596 public function finish_document() {
1597 exit;
1601 * Format a row of data.
1602 * @param array $data
1604 protected function format_row($data) {
1605 $escapeddata = array();
1606 foreach ($data as $value) {
1607 $escapeddata[] = '"' . str_replace('"', '""', $value) . '"';
1609 return implode($this->seperator, $escapeddata) . "\n";
1615 * @package moodlecore
1616 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1617 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1619 class table_tsv_export_format extends table_text_export_format_parent {
1620 protected $seperator = "\t";
1621 protected $mimetype = 'text/tab-separated-values';
1622 protected $ext = '.txt';
1627 * @package moodlecore
1628 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1629 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1631 class table_csv_export_format extends table_text_export_format_parent {
1632 protected $seperator = ",";
1633 protected $mimetype = 'text/csv';
1634 protected $ext = '.csv';
1639 * @package moodlecore
1640 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1641 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1643 class table_xhtml_export_format extends table_default_export_format_parent {
1644 function start_document($filename) {
1645 header("Content-Type: application/download\n");
1646 header("Content-Disposition: attachment; filename=\"$filename.html\"");
1647 header("Expires: 0");
1648 header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
1649 header("Pragma: public");
1650 //html headers
1651 echo <<<EOF
1652 <?xml version="1.0" encoding="UTF-8"?>
1653 <!DOCTYPE html
1654 PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
1655 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
1657 <html xmlns="http://www.w3.org/1999/xhtml"
1658 xml:lang="en" lang="en">
1659 <head>
1660 <style type="text/css">/*<![CDATA[*/
1662 .flexible th {
1663 white-space:normal;
1665 th.header, td.header, div.header {
1666 border-color:#DDDDDD;
1667 background-color:lightGrey;
1669 .flexible th {
1670 white-space:nowrap;
1672 th {
1673 font-weight:bold;
1676 .generaltable {
1677 border-style:solid;
1679 .generalbox {
1680 border-style:solid;
1682 body, table, td, th {
1683 font-family:Arial,Verdana,Helvetica,sans-serif;
1684 font-size:100%;
1686 td {
1687 border-style:solid;
1688 border-width:1pt;
1690 table {
1691 border-collapse:collapse;
1692 border-spacing:0pt;
1693 width:80%;
1694 margin:auto;
1697 h1, h2 {
1698 text-align:center;
1700 .bold {
1701 font-weight:bold;
1703 .mdl-align {
1704 text-align:center;
1706 /*]]>*/</style>
1707 <title>$filename</title>
1708 </head>
1709 <body>
1710 EOF;
1711 $this->documentstarted = true;
1714 function start_table($sheettitle) {
1715 $this->table->sortable(false);
1716 $this->table->collapsible(false);
1717 echo "<h2>{$sheettitle}</h2>";
1718 $this->table->start_html();
1721 function output_headers($headers) {
1722 $this->table->print_headers();
1725 function add_data($row) {
1726 $this->table->print_row($row);
1727 return true;
1730 function add_seperator() {
1731 $this->table->print_row(NULL);
1732 return true;
1735 function finish_table() {
1736 $this->table->finish_html();
1739 function finish_document() {
1740 echo "</body>\n</html>";
1741 exit;
1744 function format_text($text, $format=FORMAT_MOODLE, $options=NULL, $courseid=NULL) {
1745 if (is_null($options)) {
1746 $options = new stdClass;
1748 //some sensible defaults
1749 if (!isset($options->para)) {
1750 $options->para = false;
1752 if (!isset($options->newlines)) {
1753 $options->newlines = false;
1755 if (!isset($options->smiley)) {
1756 $options->smiley = false;
1758 if (!isset($options->filter)) {
1759 $options->filter = false;
1761 return format_text($text, $format, $options);