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);
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);
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
{
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 private $column_textsort = array();
66 var $request = array();
69 * @var bool Whether or not to store table properties in the user_preferences table.
71 private $persistent = false;
72 var $is_collapsible = false;
73 var $is_sortable = false;
74 var $use_pages = false;
75 var $use_initials = false;
82 var $sort_default_column = NULL;
83 var $sort_default_order = SORT_ASC
;
86 * Array of positions in which to display download controls.
88 var $showdownloadbuttonsat= array(TABLE_P_TOP
);
91 * @var string Key of field returned by db query that is the id field of the
92 * user table or equivalent.
94 public $useridfield = 'id';
97 * @var string which download plugin to use. Default '' means none - print
98 * html table with paging. Property set by is_downloading which typically
99 * passes in cleaned data from $
104 * @var bool whether data is downloadable from table. Determines whether
105 * to display download buttons. Set by method downloadable().
107 var $downloadable = false;
110 * @var string which download plugin to use. Default '' means none - print
111 * html table with paging.
113 var $defaultdownloadformat = 'csv';
116 * @var bool Has start output been called yet?
118 var $started_output = false;
120 var $exportclass = null;
123 * @var array For storing user-customised table properties in the user_preferences db table.
125 private $prefs = array();
129 * @param int $uniqueid all tables have to have a unique id, this is used
130 * as a key when storing table properties like sort order in the session.
132 function __construct($uniqueid) {
133 $this->uniqueid
= $uniqueid;
134 $this->request
= array(
135 TABLE_VAR_SORT
=> 'tsort',
136 TABLE_VAR_HIDE
=> 'thide',
137 TABLE_VAR_SHOW
=> 'tshow',
138 TABLE_VAR_IFIRST
=> 'tifirst',
139 TABLE_VAR_ILAST
=> 'tilast',
140 TABLE_VAR_PAGE
=> 'page',
145 * Call this to pass the download type. Use :
146 * $download = optional_param('download', '', PARAM_ALPHA);
147 * To get the download type. We assume that if you call this function with
148 * params that this table's data is downloadable, so we call is_downloadable
149 * for you (even if the param is '', which means no download this time.
150 * Also you can call this method with no params to get the current set
152 * @param string $download download type. One of csv, tsv, xhtml, ods, etc
153 * @param string $filename filename for downloads without file extension.
154 * @param string $sheettitle title for downloaded data.
155 * @return string download type. One of csv, tsv, xhtml, ods, etc
157 function is_downloading($download = null, $filename='', $sheettitle='') {
158 if ($download!==null) {
159 $this->sheettitle
= $sheettitle;
160 $this->is_downloadable(true);
161 $this->download
= $download;
162 $this->filename
= clean_filename($filename);
163 $this->export_class_instance();
165 return $this->download
;
169 * Get, and optionally set, the export class.
170 * @param $exportclass (optional) if passed, set the table to use this export class.
171 * @return table_default_export_format_parent the export class in use (after any set).
173 function export_class_instance($exportclass = null) {
174 if (!is_null($exportclass)) {
175 $this->started_output
= true;
176 $this->exportclass
= $exportclass;
177 $this->exportclass
->table
= $this;
178 } else if (is_null($this->exportclass
) && !empty($this->download
)) {
179 $classname = 'table_'.$this->download
.'_export_format';
180 $this->exportclass
= new $classname($this);
181 if (!$this->exportclass
->document_started()) {
182 $this->exportclass
->start_document($this->filename
);
185 return $this->exportclass
;
189 * Probably don't need to call this directly. Calling is_downloading with a
190 * param automatically sets table as downloadable.
192 * @param bool $downloadable optional param to set whether data from
193 * table is downloadable. If ommitted this function can be used to get
194 * current state of table.
195 * @return bool whether table data is set to be downloadable.
197 function is_downloadable($downloadable = null) {
198 if ($downloadable !== null) {
199 $this->downloadable
= $downloadable;
201 return $this->downloadable
;
205 * Call with boolean true to store table layout changes in the user_preferences table.
206 * Note: user_preferences.value has a maximum length of 1333 characters.
207 * Call with no parameter to get current state of table persistence.
209 * @param bool $persistent Optional parameter to set table layout persistence.
210 * @return bool Whether or not the table layout preferences will persist.
212 public function is_persistent($persistent = null) {
213 if ($persistent == true) {
214 $this->persistent
= true;
216 return $this->persistent
;
220 * Where to show download buttons.
221 * @param array $showat array of postions in which to show download buttons.
222 * Containing TABLE_P_TOP and/or TABLE_P_BOTTOM
224 function show_download_buttons_at($showat) {
225 $this->showdownloadbuttonsat
= $showat;
229 * Sets the is_sortable variable to the given boolean, sort_default_column to
230 * the given string, and the sort_default_order to the given integer.
232 * @param string $defaultcolumn
233 * @param int $defaultorder
236 function sortable($bool, $defaultcolumn = NULL, $defaultorder = SORT_ASC
) {
237 $this->is_sortable
= $bool;
238 $this->sort_default_column
= $defaultcolumn;
239 $this->sort_default_order
= $defaultorder;
243 * Use text sorting functions for this column (required for text columns with Oracle).
244 * Be warned that you cannot use this with column aliases. You can only do this
245 * with real columns. See MDL-40481 for an example.
246 * @param string column name
248 function text_sorting($column) {
249 $this->column_textsort
[] = $column;
253 * Do not sort using this column
254 * @param string column name
256 function no_sorting($column) {
257 $this->column_nosort
[] = $column;
261 * Is the column sortable?
262 * @param string column name, null means table
265 function is_sortable($column = null) {
266 if (empty($column)) {
267 return $this->is_sortable
;
269 if (!$this->is_sortable
) {
272 return !in_array($column, $this->column_nosort
);
276 * Sets the is_collapsible variable to the given boolean.
280 function collapsible($bool) {
281 $this->is_collapsible
= $bool;
285 * Sets the use_pages variable to the given boolean.
289 function pageable($bool) {
290 $this->use_pages
= $bool;
294 * Sets the use_initials variable to the given boolean.
298 function initialbars($bool) {
299 $this->use_initials
= $bool;
303 * Sets the pagesize variable to the given integer, the totalrows variable
304 * to the given integer, and the use_pages variable to true.
305 * @param int $perpage
309 function pagesize($perpage, $total) {
310 $this->pagesize
= $perpage;
311 $this->totalrows
= $total;
312 $this->use_pages
= true;
316 * Assigns each given variable in the array to the corresponding index
317 * in the request class variable.
318 * @param array $variables
321 function set_control_variables($variables) {
322 foreach ($variables as $what => $variable) {
323 if (isset($this->request
[$what])) {
324 $this->request
[$what] = $variable;
330 * Gives the given $value to the $attribute index of $this->attributes.
331 * @param string $attribute
332 * @param mixed $value
335 function set_attribute($attribute, $value) {
336 $this->attributes
[$attribute] = $value;
340 * What this method does is set the column so that if the same data appears in
341 * consecutive rows, then it is not repeated.
343 * For example, in the quiz overview report, the fullname column is set to be suppressed, so
344 * that when one student has made multiple attempts, their name is only printed in the row
345 * for their first attempt.
346 * @param int $column the index of a column.
348 function column_suppress($column) {
349 if (isset($this->column_suppress
[$column])) {
350 $this->column_suppress
[$column] = true;
355 * Sets the given $column index to the given $classname in $this->column_class.
357 * @param string $classname
360 function column_class($column, $classname) {
361 if (isset($this->column_class
[$column])) {
362 $this->column_class
[$column] = ' '.$classname; // This space needed so that classnames don't run together in the HTML
367 * Sets the given $column index and $property index to the given $value in $this->column_style.
369 * @param string $property
370 * @param mixed $value
373 function column_style($column, $property, $value) {
374 if (isset($this->column_style
[$column])) {
375 $this->column_style
[$column][$property] = $value;
380 * Sets all columns' $propertys to the given $value in $this->column_style.
381 * @param int $property
382 * @param string $value
385 function column_style_all($property, $value) {
386 foreach (array_keys($this->columns
) as $column) {
387 $this->column_style
[$column][$property] = $value;
392 * Sets $this->baseurl.
393 * @param moodle_url|string $url the url with params needed to call up this page
395 function define_baseurl($url) {
396 $this->baseurl
= new moodle_url($url);
400 * @param array $columns an array of identifying names for columns. If
401 * columns are sorted then column names must correspond to a field in sql.
403 function define_columns($columns) {
404 $this->columns
= array();
405 $this->column_style
= array();
406 $this->column_class
= array();
409 foreach ($columns as $column) {
410 $this->columns
[$column] = $colnum++
;
411 $this->column_style
[$column] = array();
412 $this->column_class
[$column] = '';
413 $this->column_suppress
[$column] = false;
418 * @param array $headers numerical keyed array of displayed string titles
421 function define_headers($headers) {
422 $this->headers
= $headers;
426 * Must be called after table is defined. Use methods above first. Cannot
427 * use functions below till after calling this method.
433 if (empty($this->columns
) ||
empty($this->uniqueid
)) {
437 // Load any existing user preferences.
438 if ($this->persistent
) {
439 $this->prefs
= json_decode(get_user_preferences('flextable_' . $this->uniqueid
), true);
440 } else if (isset($SESSION->flextable
[$this->uniqueid
])) {
441 $this->prefs
= $SESSION->flextable
[$this->uniqueid
];
444 $this->prefs
= array(
445 'collapse' => array(),
449 'textsort' => $this->column_textsort
,
452 $oldprefs = $this->prefs
;
454 if (($showcol = optional_param($this->request
[TABLE_VAR_SHOW
], '', PARAM_ALPHANUMEXT
)) &&
455 isset($this->columns
[$showcol])) {
456 $this->prefs
['collapse'][$showcol] = false;
458 } else if (($hidecol = optional_param($this->request
[TABLE_VAR_HIDE
], '', PARAM_ALPHANUMEXT
)) &&
459 isset($this->columns
[$hidecol])) {
460 $this->prefs
['collapse'][$hidecol] = true;
461 if (array_key_exists($hidecol, $this->prefs
['sortby'])) {
462 unset($this->prefs
['sortby'][$hidecol]);
466 // Now, update the column attributes for collapsed columns
467 foreach (array_keys($this->columns
) as $column) {
468 if (!empty($this->prefs
['collapse'][$column])) {
469 $this->column_style
[$column]['width'] = '10px';
473 if (($sortcol = optional_param($this->request
[TABLE_VAR_SORT
], '', PARAM_ALPHANUMEXT
)) &&
474 $this->is_sortable($sortcol) && empty($this->prefs
['collapse'][$sortcol]) &&
475 (isset($this->columns
[$sortcol]) ||
in_array($sortcol, get_all_user_name_fields())
476 && isset($this->columns
['fullname']))) {
478 if (array_key_exists($sortcol, $this->prefs
['sortby'])) {
479 // This key already exists somewhere. Change its sortorder and bring it to the top.
480 $sortorder = $this->prefs
['sortby'][$sortcol] == SORT_ASC ? SORT_DESC
: SORT_ASC
;
481 unset($this->prefs
['sortby'][$sortcol]);
482 $this->prefs
['sortby'] = array_merge(array($sortcol => $sortorder), $this->prefs
['sortby']);
484 // Key doesn't exist, so just add it to the beginning of the array, ascending order
485 $this->prefs
['sortby'] = array_merge(array($sortcol => SORT_ASC
), $this->prefs
['sortby']);
488 // Finally, make sure that no more than $this->maxsortkeys are present into the array
489 $this->prefs
['sortby'] = array_slice($this->prefs
['sortby'], 0, $this->maxsortkeys
);
492 // 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.
493 // This prevents results from being returned in a random order if the only order by column contains equal values.
494 if (!empty($this->sort_default_column
)) {
495 if (!array_key_exists($this->sort_default_column
, $this->prefs
['sortby'])) {
496 $defaultsort = array($this->sort_default_column
=> $this->sort_default_order
);
497 $this->prefs
['sortby'] = array_merge($this->prefs
['sortby'], $defaultsort);
501 $ilast = optional_param($this->request
[TABLE_VAR_ILAST
], null, PARAM_RAW
);
502 if (!is_null($ilast) && ($ilast ==='' ||
strpos(get_string('alphabet', 'langconfig'), $ilast) !== false)) {
503 $this->prefs
['i_last'] = $ilast;
506 $ifirst = optional_param($this->request
[TABLE_VAR_IFIRST
], null, PARAM_RAW
);
507 if (!is_null($ifirst) && ($ifirst === '' ||
strpos(get_string('alphabet', 'langconfig'), $ifirst) !== false)) {
508 $this->prefs
['i_first'] = $ifirst;
511 // Save user preferences if they have changed.
512 if ($this->prefs
!= $oldprefs) {
513 if ($this->persistent
) {
514 set_user_preference('flextable_' . $this->uniqueid
, json_encode($this->prefs
));
516 $SESSION->flextable
[$this->uniqueid
] = $this->prefs
;
521 if (empty($this->baseurl
)) {
522 debugging('You should set baseurl when using flexible_table.');
524 $this->baseurl
= $PAGE->url
;
527 $this->currpage
= optional_param($this->request
[TABLE_VAR_PAGE
], 0, PARAM_INT
);
530 // Always introduce the "flexible" class for the table if not specified
531 if (empty($this->attributes
)) {
532 $this->attributes
['class'] = 'flexible';
533 } else if (!isset($this->attributes
['class'])) {
534 $this->attributes
['class'] = 'flexible';
535 } else if (!in_array('flexible', explode(' ', $this->attributes
['class']))) {
536 $this->attributes
['class'] = trim('flexible ' . $this->attributes
['class']);
541 * Get the order by clause from the session or user preferences, for the table with id $uniqueid.
542 * @param string $uniqueid the identifier for a table.
543 * @return SQL fragment that can be used in an ORDER BY clause.
545 public static function get_sort_for_table($uniqueid) {
547 if (isset($SESSION->flextable
[$uniqueid])) {
548 $prefs = $SESSION->flextable
[$uniqueid];
549 } else if (!$prefs = json_decode(get_user_preferences('flextable_' . $uniqueid), true)) {
553 if (empty($prefs['sortby'])) {
556 if (empty($prefs['textsort'])) {
557 $prefs['textsort'] = array();
560 return self
::construct_order_by($prefs['sortby'], $prefs['textsort']);
564 * Prepare an an order by clause from the list of columns to be sorted.
565 * @param array $cols column name => SORT_ASC or SORT_DESC
566 * @return SQL fragment that can be used in an ORDER BY clause.
568 public static function construct_order_by($cols, $textsortcols=array()) {
572 foreach ($cols as $column => $order) {
573 if (in_array($column, $textsortcols)) {
574 $column = $DB->sql_order_by_text($column);
576 if ($order == SORT_ASC
) {
577 $bits[] = $column . ' ASC';
579 $bits[] = $column . ' DESC';
583 return implode(', ', $bits);
587 * @return SQL fragment that can be used in an ORDER BY clause.
589 public function get_sql_sort() {
590 return self
::construct_order_by($this->get_sort_columns(), $this->column_textsort
);
594 * Get the columns to sort by, in the form required by {@link construct_order_by()}.
595 * @return array column name => SORT_... constant.
597 public function get_sort_columns() {
599 throw new coding_exception('Cannot call get_sort_columns until you have called setup.');
602 if (empty($this->prefs
['sortby'])) {
606 foreach ($this->prefs
['sortby'] as $column => $notused) {
607 if (isset($this->columns
[$column])) {
608 continue; // This column is OK.
610 if (in_array($column, get_all_user_name_fields()) &&
611 isset($this->columns
['fullname'])) {
612 continue; // This column is OK.
614 // This column is not OK.
615 unset($this->prefs
['sortby'][$column]);
618 return $this->prefs
['sortby'];
622 * @return int the offset for LIMIT clause of SQL
624 function get_page_start() {
625 if (!$this->use_pages
) {
628 return $this->currpage
* $this->pagesize
;
632 * @return int the pagesize for LIMIT clause of SQL
634 function get_page_size() {
635 if (!$this->use_pages
) {
638 return $this->pagesize
;
642 * @return string sql to add to where statement.
644 function get_sql_where() {
647 $conditions = array();
650 if (isset($this->columns
['fullname'])) {
654 if (!empty($this->prefs
['i_first'])) {
655 $conditions[] = $DB->sql_like('firstname', ':ifirstc'.$i, false, false);
656 $params['ifirstc'.$i] = $this->prefs
['i_first'].'%';
658 if (!empty($this->prefs
['i_last'])) {
659 $conditions[] = $DB->sql_like('lastname', ':ilastc'.$i, false, false);
660 $params['ilastc'.$i] = $this->prefs
['i_last'].'%';
664 return array(implode(" AND ", $conditions), $params);
668 * Add a row of data to the table. This function takes an array or object with
669 * column names as keys or property names.
671 * It ignores any elements with keys that are not defined as columns. It
672 * puts in empty strings into the row when there is no element in the passed
673 * array corresponding to a column in the table. It puts the row elements in
674 * the proper order (internally row table data is stored by in arrays with
675 * a numerical index corresponding to the column number).
677 * @param object|array $rowwithkeys array keys or object property names are column names,
678 * as defined in call to define_columns.
679 * @param string $classname CSS class name to add to this row's tr tag.
681 function add_data_keyed($rowwithkeys, $classname = '') {
682 $this->add_data($this->get_row_from_keyed($rowwithkeys), $classname);
686 * Add a number of rows to the table at once. And optionally finish output after they have been added.
688 * @param (object|array|null)[] $rowstoadd Array of rows to add to table, a null value in array adds a separator row. Or a
689 * object or array is added to table. We expect properties for the row array as would be
690 * passed to add_data_keyed.
691 * @param bool $finish
693 public function format_and_add_array_of_rows($rowstoadd, $finish = true) {
694 foreach ($rowstoadd as $row) {
696 $this->add_separator();
698 $this->add_data_keyed($this->format_row($row));
702 $this->finish_output(!$this->is_downloading());
707 * Add a seperator line to table.
709 function add_separator() {
713 $this->add_data(NULL);
717 * This method actually directly echoes the row passed to it now or adds it
718 * to the download. If this is the first row and start_output has not
719 * already been called this method also calls start_output to open the table
720 * or send headers for the downloaded.
721 * Can be used as before. print_html now calls finish_html to close table.
723 * @param array $row a numerically keyed row of data to add to the table.
724 * @param string $classname CSS class name to add to this row's tr tag.
725 * @return bool success.
727 function add_data($row, $classname = '') {
731 if (!$this->started_output
) {
732 $this->start_output();
734 if ($this->exportclass
!==null) {
736 $this->exportclass
->add_seperator();
738 $this->exportclass
->add_data($row);
741 $this->print_row($row, $classname);
747 * You should call this to finish outputting the table data after adding
748 * data to the table with add_data or add_data_keyed.
751 function finish_output($closeexportclassdoc = true) {
752 if ($this->exportclass
!==null) {
753 $this->exportclass
->finish_table();
754 if ($closeexportclassdoc) {
755 $this->exportclass
->finish_document();
758 $this->finish_html();
763 * Hook that can be overridden in child classes to wrap a table in a form
764 * for example. Called only when there is data to display and not
767 function wrap_html_start() {
771 * Hook that can be overridden in child classes to wrap a table in a form
772 * for example. Called only when there is data to display and not
775 function wrap_html_finish() {
779 * Call appropriate methods on this table class to perform any processing on values before displaying in table.
780 * Takes raw data from the database and process it into human readable format, perhaps also adding html linking when
781 * displaying table as html, adding a div wrap, etc.
783 * See for example col_fullname below which will be called for a column whose name is 'fullname'.
785 * @param array|object $row row of data from db used to make one row of the table.
786 * @return array one row for the table, added using add_data_keyed method.
788 function format_row($row) {
789 if (is_array($row)) {
792 $formattedrow = array();
793 foreach (array_keys($this->columns
) as $column) {
794 $colmethodname = 'col_'.$column;
795 if (method_exists($this, $colmethodname)) {
796 $formattedcolumn = $this->$colmethodname($row);
798 $formattedcolumn = $this->other_cols($column, $row);
799 if ($formattedcolumn===NULL) {
800 $formattedcolumn = $row->$column;
803 $formattedrow[$column] = $formattedcolumn;
805 return $formattedrow;
809 * Fullname is treated as a special columname in tablelib and should always
810 * be treated the same as the fullname of a user.
811 * @uses $this->useridfield if the userid field is not expected to be id
812 * then you need to override $this->useridfield to point at the correct
813 * field for the user id.
815 * @param object $row the data from the db containing all fields from the
816 * users table necessary to construct the full name of the user in
818 * @return string contents of cell in column 'fullname', for this row.
820 function col_fullname($row) {
823 $name = fullname($row);
824 if ($this->download
) {
828 $userid = $row->{$this->useridfield
};
829 if ($COURSE->id
== SITEID
) {
830 $profileurl = new moodle_url('/user/profile.php', array('id' => $userid));
832 $profileurl = new moodle_url('/user/view.php',
833 array('id' => $userid, 'course' => $COURSE->id
));
835 return html_writer
::link($profileurl, $name);
839 * You can override this method in a child class. See the description of
840 * build_table which calls this method.
842 function other_cols($column, $row) {
847 * Used from col_* functions when text is to be displayed. Does the
848 * right thing - either converts text to html or strips any html tags
849 * depending on if we are downloading and what is the download type. Params
850 * are the same as format_text function in weblib.php but some default
851 * options are changed.
853 function format_text($text, $format=FORMAT_MOODLE
, $options=NULL, $courseid=NULL) {
854 if (!$this->is_downloading()) {
855 if (is_null($options)) {
856 $options = new stdClass
;
858 //some sensible defaults
859 if (!isset($options->para
)) {
860 $options->para
= false;
862 if (!isset($options->newlines
)) {
863 $options->newlines
= false;
865 if (!isset($options->smiley
)) {
866 $options->smiley
= false;
868 if (!isset($options->filter
)) {
869 $options->filter
= false;
871 return format_text($text, $format, $options);
873 $eci = $this->export_class_instance();
874 return $eci->format_text($text, $format, $options, $courseid);
878 * This method is deprecated although the old api is still supported.
879 * @deprecated 1.9.2 - Jun 2, 2008
881 function print_html() {
885 $this->finish_html();
889 * This function is not part of the public api.
890 * @return string initial of first name we are currently filtering by
892 function get_initial_first() {
893 if (!$this->use_initials
) {
897 return $this->prefs
['i_first'];
901 * This function is not part of the public api.
902 * @return string initial of last name we are currently filtering by
904 function get_initial_last() {
905 if (!$this->use_initials
) {
909 return $this->prefs
['i_last'];
913 * Helper function, used by {@link print_initials_bar()} to output one initial bar.
914 * @param array $alpha of letters in the alphabet.
915 * @param string $current the currently selected letter.
916 * @param string $class class name to add to this initial bar.
917 * @param string $title the name to put in front of this initial bar.
918 * @param string $urlvar URL parameter name for this initial.
920 protected function print_one_initials_bar($alpha, $current, $class, $title, $urlvar) {
921 echo html_writer
::start_tag('div', array('class' => 'initialbar ' . $class)) .
924 echo html_writer
::link($this->baseurl
->out(false, array($urlvar => '')), get_string('all'));
926 echo html_writer
::tag('strong', get_string('all'));
929 foreach ($alpha as $letter) {
930 if ($letter === $current) {
931 echo html_writer
::tag('strong', $letter);
933 echo html_writer
::link($this->baseurl
->out(false, array($urlvar => $letter)), $letter);
937 echo html_writer
::end_tag('div');
941 * This function is not part of the public api.
943 function print_initials_bar() {
944 if ((!empty($this->prefs
['i_last']) ||
!empty($this->prefs
['i_first']) ||
$this->use_initials
)
945 && isset($this->columns
['fullname'])) {
947 $alpha = explode(',', get_string('alphabet', 'langconfig'));
949 // Bar of first initials
950 if (!empty($this->prefs
['i_first'])) {
951 $ifirst = $this->prefs
['i_first'];
955 $this->print_one_initials_bar($alpha, $ifirst, 'firstinitial',
956 get_string('firstname'), $this->request
[TABLE_VAR_IFIRST
]);
958 // Bar of last initials
959 if (!empty($this->prefs
['i_last'])) {
960 $ilast = $this->prefs
['i_last'];
964 $this->print_one_initials_bar($alpha, $ilast, 'lastinitial',
965 get_string('lastname'), $this->request
[TABLE_VAR_ILAST
]);
970 * This function is not part of the public api.
972 function print_nothing_to_display() {
974 $this->print_initials_bar();
976 echo $OUTPUT->heading(get_string('nothingtodisplay'));
980 * This function is not part of the public api.
982 function get_row_from_keyed($rowwithkeys) {
983 if (is_object($rowwithkeys)) {
984 $rowwithkeys = (array)$rowwithkeys;
987 foreach (array_keys($this->columns
) as $column) {
988 if (isset($rowwithkeys[$column])) {
989 $row [] = $rowwithkeys[$column];
997 * This function is not part of the public api.
999 function get_download_menu() {
1000 $allclasses= get_declared_classes();
1001 $exportclasses = array();
1002 foreach ($allclasses as $class) {
1004 if (preg_match('/^table\_([a-z]+)\_export\_format$/', $class, $matches)) {
1005 $type = $matches[1];
1006 $exportclasses[$type]= get_string("download$type", 'table');
1009 return $exportclasses;
1013 * This function is not part of the public api.
1015 function download_buttons() {
1016 if ($this->is_downloadable() && !$this->is_downloading()) {
1017 $downloadoptions = $this->get_download_menu();
1019 $downloadelements = new stdClass();
1020 $downloadelements->formatsmenu
= html_writer
::select($downloadoptions,
1021 'download', $this->defaultdownloadformat
, false);
1022 $downloadelements->downloadbutton
= '<input type="submit" value="'.
1023 get_string('download').'"/>';
1024 $html = '<form action="'. $this->baseurl
.'" method="post">';
1025 $html .= '<div class="mdl-align">';
1026 $html .= html_writer
::tag('label', get_string('downloadas', 'table', $downloadelements));
1027 $html .= '</div></form>';
1035 * This function is not part of the public api.
1036 * You don't normally need to call this. It is called automatically when
1037 * needed when you start adding data to the table.
1040 function start_output() {
1041 $this->started_output
= true;
1042 if ($this->exportclass
!==null) {
1043 $this->exportclass
->start_table($this->sheettitle
);
1044 $this->exportclass
->output_headers($this->headers
);
1046 $this->start_html();
1047 $this->print_headers();
1048 echo html_writer
::start_tag('tbody');
1053 * This function is not part of the public api.
1055 function print_row($row, $classname = '') {
1056 echo $this->get_row_html($row, $classname);
1060 * Generate html code for the passed row.
1062 * @param array $row Row data.
1063 * @param string $classname classes to add.
1065 * @return string $html html code for the row passed.
1067 public function get_row_html($row, $classname = '') {
1068 static $suppress_lastrow = NULL;
1069 $rowclasses = array();
1072 $rowclasses[] = $classname;
1075 $rowid = $this->uniqueid
. '_r' . $this->currentrow
;
1078 $html .= html_writer
::start_tag('tr', array('class' => implode(' ', $rowclasses), 'id' => $rowid));
1080 // If we have a separator, print it
1081 if ($row === NULL) {
1082 $colcount = count($this->columns
);
1083 $html .= html_writer
::tag('td', html_writer
::tag('div', '',
1084 array('class' => 'tabledivider')), array('colspan' => $colcount));
1087 $colbyindex = array_flip($this->columns
);
1088 foreach ($row as $index => $data) {
1089 $column = $colbyindex[$index];
1091 if (empty($this->prefs
['collapse'][$column])) {
1092 if ($this->column_suppress
[$column] && $suppress_lastrow !== NULL && $suppress_lastrow[$index] === $data) {
1093 $content = ' ';
1098 $content = ' ';
1101 $html .= html_writer
::tag('td', $content, array(
1102 'class' => 'cell c' . $index . $this->column_class
[$column],
1103 'id' => $rowid . '_c' . $index,
1104 'style' => $this->make_styles_string($this->column_style
[$column])));
1108 $html .= html_writer
::end_tag('tr');
1110 $suppress_enabled = array_sum($this->column_suppress
);
1111 if ($suppress_enabled) {
1112 $suppress_lastrow = $row;
1114 $this->currentrow++
;
1119 * This function is not part of the public api.
1121 function finish_html() {
1123 if (!$this->started_output
) {
1124 //no data has been added to the table.
1125 $this->print_nothing_to_display();
1128 // Print empty rows to fill the table to the current pagesize.
1129 // This is done so the header aria-controls attributes do not point to
1130 // non existant elements.
1131 $emptyrow = array_fill(0, count($this->columns
), '');
1132 while ($this->currentrow
< $this->pagesize
) {
1133 $this->print_row($emptyrow, 'emptyrow');
1136 echo html_writer
::end_tag('tbody');
1137 echo html_writer
::end_tag('table');
1138 echo html_writer
::end_tag('div');
1139 $this->wrap_html_finish();
1142 if(in_array(TABLE_P_BOTTOM
, $this->showdownloadbuttonsat
)) {
1143 echo $this->download_buttons();
1146 if($this->use_pages
) {
1147 $pagingbar = new paging_bar($this->totalrows
, $this->currpage
, $this->pagesize
, $this->baseurl
);
1148 $pagingbar->pagevar
= $this->request
[TABLE_VAR_PAGE
];
1149 echo $OUTPUT->render($pagingbar);
1155 * Generate the HTML for the collapse/uncollapse icon. This is a helper method
1156 * used by {@link print_headers()}.
1157 * @param string $column the column name, index into various names.
1158 * @param int $index numerical index of the column.
1159 * @return string HTML fragment.
1161 protected function show_hide_link($column, $index) {
1163 // Some headers contain <br /> tags, do not include in title, hence the
1167 for ($i = 0; $i < $this->pagesize
; $i++
) {
1168 $ariacontrols .= $this->uniqueid
. '_r' . $i . '_c' . $index . ' ';
1171 $ariacontrols = trim($ariacontrols);
1173 if (!empty($this->prefs
['collapse'][$column])) {
1174 $linkattributes = array('title' => get_string('show') . ' ' . strip_tags($this->headers
[$index]),
1175 'aria-expanded' => 'false',
1176 'aria-controls' => $ariacontrols);
1177 return html_writer
::link($this->baseurl
->out(false, array($this->request
[TABLE_VAR_SHOW
] => $column)),
1178 html_writer
::empty_tag('img', array('src' => $OUTPUT->pix_url('t/switch_plus'), 'alt' => get_string('show'))),
1181 } else if ($this->headers
[$index] !== NULL) {
1182 $linkattributes = array('title' => get_string('hide') . ' ' . strip_tags($this->headers
[$index]),
1183 'aria-expanded' => 'true',
1184 'aria-controls' => $ariacontrols);
1185 return html_writer
::link($this->baseurl
->out(false, array($this->request
[TABLE_VAR_HIDE
] => $column)),
1186 html_writer
::empty_tag('img', array('src' => $OUTPUT->pix_url('t/switch_minus'), 'alt' => get_string('hide'))),
1192 * This function is not part of the public api.
1194 function print_headers() {
1195 global $CFG, $OUTPUT;
1197 echo html_writer
::start_tag('thead');
1198 echo html_writer
::start_tag('tr');
1199 foreach ($this->columns
as $column => $index) {
1202 if ($this->is_collapsible
) {
1203 $icon_hide = $this->show_hide_link($column, $index);
1206 $primarysortcolumn = '';
1207 $primarysortorder = '';
1208 if (reset($this->prefs
['sortby'])) {
1209 $primarysortcolumn = key($this->prefs
['sortby']);
1210 $primarysortorder = current($this->prefs
['sortby']);
1216 // Check the full name display for sortable fields.
1217 $nameformat = $CFG->fullnamedisplay
;
1218 if ($nameformat == 'language') {
1219 $nameformat = get_string('fullnamedisplay');
1221 $requirednames = order_in_string(get_all_user_name_fields(), $nameformat);
1223 if (!empty($requirednames)) {
1224 if ($this->is_sortable($column)) {
1225 // Done this way for the possibility of more than two sortable full name display fields.
1226 $this->headers
[$index] = '';
1227 foreach ($requirednames as $name) {
1228 $sortname = $this->sort_link(get_string($name),
1229 $name, $primarysortcolumn === $name, $primarysortorder);
1230 $this->headers
[$index] .= $sortname . ' / ';
1232 $this->headers
[$index] = substr($this->headers
[$index], 0, -3);
1238 // do nothing, do not display sortable links
1242 if ($this->is_sortable($column)) {
1243 $this->headers
[$index] = $this->sort_link($this->headers
[$index],
1244 $column, $primarysortcolumn == $column, $primarysortorder);
1248 $attributes = array(
1249 'class' => 'header c' . $index . $this->column_class
[$column],
1252 if ($this->headers
[$index] === NULL) {
1253 $content = ' ';
1254 } else if (!empty($this->prefs
['collapse'][$column])) {
1255 $content = $icon_hide;
1257 if (is_array($this->column_style
[$column])) {
1258 $attributes['style'] = $this->make_styles_string($this->column_style
[$column]);
1260 $content = $this->headers
[$index] . html_writer
::tag('div',
1261 $icon_hide, array('class' => 'commands'));
1263 echo html_writer
::tag('th', $content, $attributes);
1266 echo html_writer
::end_tag('tr');
1267 echo html_writer
::end_tag('thead');
1271 * Generate the HTML for the sort icon. This is a helper method used by {@link sort_link()}.
1272 * @param bool $isprimary whether an icon is needed (it is only needed for the primary sort column.)
1273 * @param int $order SORT_ASC or SORT_DESC
1274 * @return string HTML fragment.
1276 protected function sort_icon($isprimary, $order) {
1283 if ($order == SORT_ASC
) {
1284 return html_writer
::empty_tag('img',
1285 array('src' => $OUTPUT->pix_url('t/sort_asc'), 'alt' => get_string('asc'), 'class' => 'iconsort'));
1287 return html_writer
::empty_tag('img',
1288 array('src' => $OUTPUT->pix_url('t/sort_desc'), 'alt' => get_string('desc'), 'class' => 'iconsort'));
1293 * Generate the correct tool tip for changing the sort order. This is a
1294 * helper method used by {@link sort_link()}.
1295 * @param bool $isprimary whether the is column is the current primary sort column.
1296 * @param int $order SORT_ASC or SORT_DESC
1297 * @return string the correct title.
1299 protected function sort_order_name($isprimary, $order) {
1300 if ($isprimary && $order != SORT_ASC
) {
1301 return get_string('desc');
1303 return get_string('asc');
1308 * Generate the HTML for the sort link. This is a helper method used by {@link print_headers()}.
1309 * @param string $text the text for the link.
1310 * @param string $column the column name, may be a fake column like 'firstname' or a real one.
1311 * @param bool $isprimary whether the is column is the current primary sort column.
1312 * @param int $order SORT_ASC or SORT_DESC
1313 * @return string HTML fragment.
1315 protected function sort_link($text, $column, $isprimary, $order) {
1316 return html_writer
::link($this->baseurl
->out(false,
1317 array($this->request
[TABLE_VAR_SORT
] => $column)),
1318 $text . get_accesshide(get_string('sortby') . ' ' .
1319 $text . ' ' . $this->sort_order_name($isprimary, $order))) . ' ' .
1320 $this->sort_icon($isprimary, $order);
1324 * This function is not part of the public api.
1326 function start_html() {
1328 // Do we need to print initial bars?
1329 $this->print_initials_bar();
1332 if ($this->use_pages
) {
1333 $pagingbar = new paging_bar($this->totalrows
, $this->currpage
, $this->pagesize
, $this->baseurl
);
1334 $pagingbar->pagevar
= $this->request
[TABLE_VAR_PAGE
];
1335 echo $OUTPUT->render($pagingbar);
1338 if (in_array(TABLE_P_TOP
, $this->showdownloadbuttonsat
)) {
1339 echo $this->download_buttons();
1342 $this->wrap_html_start();
1343 // Start of main data table
1345 echo html_writer
::start_tag('div', array('class' => 'no-overflow'));
1346 echo html_writer
::start_tag('table', $this->attributes
);
1351 * This function is not part of the public api.
1352 * @param array $styles CSS-property => value
1353 * @return string values suitably to go in a style="" attribute in HTML.
1355 function make_styles_string($styles) {
1356 if (empty($styles)) {
1361 foreach($styles as $property => $value) {
1362 $string .= $property . ':' . $value . ';';
1370 * @package moodlecore
1371 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1372 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1374 class table_sql
extends flexible_table
{
1376 public $countsql = NULL;
1377 public $countparams = NULL;
1379 * @var object sql for querying db. Has fields 'fields', 'from', 'where', 'params'.
1383 * @var array|\Traversable Data fetched from the db.
1385 public $rawdata = NULL;
1388 * @var bool Overriding default for this.
1390 public $is_sortable = true;
1392 * @var bool Overriding default for this.
1394 public $is_collapsible = true;
1397 * @param string $uniqueid a string identifying this table.Used as a key in
1400 function __construct($uniqueid) {
1401 parent
::__construct($uniqueid);
1402 // some sensible defaults
1403 $this->set_attribute('cellspacing', '0');
1404 $this->set_attribute('class', 'generaltable generalbox');
1408 * Take the data returned from the db_query and go through all the rows
1409 * processing each col using either col_{columnname} method or other_cols
1410 * method or if other_cols returns NULL then put the data straight into the
1415 function build_table() {
1417 if ($this->rawdata
instanceof \Traversable
&& !$this->rawdata
->valid()) {
1420 if (!$this->rawdata
) {
1424 foreach ($this->rawdata
as $row) {
1425 $formattedrow = $this->format_row($row);
1426 $this->add_data_keyed($formattedrow,
1427 $this->get_row_class($row));
1430 if ($this->rawdata
instanceof \core\dml\recordset_walk ||
1431 $this->rawdata
instanceof moodle_recordset
) {
1432 $this->rawdata
->close();
1437 * Get any extra classes names to add to this row in the HTML.
1438 * @param $row array the data for this row.
1439 * @return string added to the class="" attribute of the tr.
1441 function get_row_class($row) {
1446 * This is only needed if you want to use different sql to count rows.
1447 * Used for example when perhaps all db JOINS are not needed when counting
1448 * records. You don't need to call this function the count_sql
1449 * will be generated automatically.
1451 * We need to count rows returned by the db seperately to the query itself
1452 * as we need to know how many pages of data we have to display.
1454 function set_count_sql($sql, array $params = NULL) {
1455 $this->countsql
= $sql;
1456 $this->countparams
= $params;
1460 * Set the sql to query the db. Query will be :
1461 * SELECT $fields FROM $from WHERE $where
1462 * Of course you can use sub-queries, JOINS etc. by putting them in the
1463 * appropriate clause of the query.
1465 function set_sql($fields, $from, $where, array $params = NULL) {
1466 $this->sql
= new stdClass();
1467 $this->sql
->fields
= $fields;
1468 $this->sql
->from
= $from;
1469 $this->sql
->where
= $where;
1470 $this->sql
->params
= $params;
1474 * Query the db. Store results in the table object for use by build_table.
1476 * @param int $pagesize size of page for paginated displayed table.
1477 * @param bool $useinitialsbar do you want to use the initials bar. Bar
1478 * will only be used if there is a fullname column defined for the table.
1480 function query_db($pagesize, $useinitialsbar=true) {
1482 if (!$this->is_downloading()) {
1483 if ($this->countsql
=== NULL) {
1484 $this->countsql
= 'SELECT COUNT(1) FROM '.$this->sql
->from
.' WHERE '.$this->sql
->where
;
1485 $this->countparams
= $this->sql
->params
;
1487 $grandtotal = $DB->count_records_sql($this->countsql
, $this->countparams
);
1488 if ($useinitialsbar && !$this->is_downloading()) {
1489 $this->initialbars($grandtotal > $pagesize);
1492 list($wsql, $wparams) = $this->get_sql_where();
1494 $this->countsql
.= ' AND '.$wsql;
1495 $this->countparams
= array_merge($this->countparams
, $wparams);
1497 $this->sql
->where
.= ' AND '.$wsql;
1498 $this->sql
->params
= array_merge($this->sql
->params
, $wparams);
1500 $total = $DB->count_records_sql($this->countsql
, $this->countparams
);
1502 $total = $grandtotal;
1505 $this->pagesize($pagesize, $total);
1508 // Fetch the attempts
1509 $sort = $this->get_sql_sort();
1511 $sort = "ORDER BY $sort";
1514 {$this->sql->fields}
1515 FROM {$this->sql->from}
1516 WHERE {$this->sql->where}
1519 if (!$this->is_downloading()) {
1520 $this->rawdata
= $DB->get_records_sql($sql, $this->sql
->params
, $this->get_page_start(), $this->get_page_size());
1522 $this->rawdata
= $DB->get_records_sql($sql, $this->sql
->params
);
1527 * Convenience method to call a number of methods for you to display the
1530 function out($pagesize, $useinitialsbar, $downloadhelpbutton='') {
1532 if (!$this->columns
) {
1533 $onerow = $DB->get_record_sql("SELECT {$this->sql->fields} FROM {$this->sql->from} WHERE {$this->sql->where}", $this->sql
->params
);
1534 //if columns is not set then define columns as the keys of the rows returned
1536 $this->define_columns(array_keys((array)$onerow));
1537 $this->define_headers(array_keys((array)$onerow));
1540 $this->query_db($pagesize, $useinitialsbar);
1541 $this->build_table();
1542 $this->finish_output();
1548 * @package moodlecore
1549 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1550 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1552 class table_default_export_format_parent
{
1554 * @var flexible_table or child class reference pointing to table class
1555 * object from which to export data.
1560 * @var bool output started. Keeps track of whether any output has been
1563 var $documentstarted = false;
1564 function table_default_export_format_parent(&$table) {
1565 $this->table
=& $table;
1568 function set_table(&$table) {
1569 $this->table
=& $table;
1572 function add_data($row) {
1576 function add_seperator() {
1580 function document_started() {
1581 return $this->documentstarted
;
1584 * Given text in a variety of format codings, this function returns
1585 * the text as safe HTML or as plain text dependent on what is appropriate
1586 * for the download format. The default removes all tags.
1588 function format_text($text, $format=FORMAT_MOODLE
, $options=NULL, $courseid=NULL) {
1589 //use some whitespace to indicate where there was some line spacing.
1590 $text = str_replace(array('</p>', "\n", "\r"), ' ', $text);
1591 return strip_tags($text);
1597 * @package moodlecore
1598 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1599 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1601 class table_spreadsheet_export_format_parent
extends table_default_export_format_parent
{
1606 * @var object format object - format for normal table cells
1610 * @var object format object - format for header table cells
1615 * should be overriden in child class.
1620 * This method will be overridden in the child class.
1622 function define_workbook() {
1625 function start_document($filename) {
1626 $filename = $filename.'.'.$this->fileextension
;
1627 $this->define_workbook();
1629 $this->formatnormal
= $this->workbook
->add_format();
1630 $this->formatnormal
->set_bold(0);
1631 $this->formatheaders
= $this->workbook
->add_format();
1632 $this->formatheaders
->set_bold(1);
1633 $this->formatheaders
->set_align('center');
1634 // Sending HTTP headers
1635 $this->workbook
->send($filename);
1636 $this->documentstarted
= true;
1639 function start_table($sheettitle) {
1640 $this->worksheet
= $this->workbook
->add_worksheet($sheettitle);
1641 $this->currentrow
=0;
1644 function output_headers($headers) {
1646 foreach ($headers as $item) {
1647 $this->worksheet
->write($this->currentrow
,$colnum,$item,$this->formatheaders
);
1650 $this->currentrow++
;
1653 function add_data($row) {
1655 foreach ($row as $item) {
1656 $this->worksheet
->write($this->currentrow
,$colnum,$item,$this->formatnormal
);
1659 $this->currentrow++
;
1663 function add_seperator() {
1664 $this->currentrow++
;
1668 function finish_table() {
1671 function finish_document() {
1672 $this->workbook
->close();
1679 * @package moodlecore
1680 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1681 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1683 class table_excel_export_format
extends table_spreadsheet_export_format_parent
{
1684 var $fileextension = 'xls';
1686 function define_workbook() {
1688 require_once("$CFG->libdir/excellib.class.php");
1689 // Creating a workbook
1690 $this->workbook
= new MoodleExcelWorkbook("-");
1697 * @package moodlecore
1698 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1699 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1701 class table_ods_export_format
extends table_spreadsheet_export_format_parent
{
1702 var $fileextension = 'ods';
1703 function define_workbook() {
1705 require_once("$CFG->libdir/odslib.class.php");
1706 // Creating a workbook
1707 $this->workbook
= new MoodleODSWorkbook("-");
1713 * @package moodlecore
1714 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1715 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1717 class table_text_export_format_parent
extends table_default_export_format_parent
{
1718 protected $seperator = "tab";
1719 protected $mimetype = 'text/tab-separated-values';
1720 protected $ext = '.txt';
1721 protected $myexporter;
1723 public function __construct() {
1724 $this->myexporter
= new csv_export_writer($this->seperator
, '"', $this->mimetype
);
1727 public function start_document($filename) {
1728 $this->filename
= $filename;
1729 $this->documentstarted
= true;
1730 $this->myexporter
->set_filename($filename, $this->ext
);
1733 public function start_table($sheettitle) {
1734 //nothing to do here
1737 public function output_headers($headers) {
1738 $this->myexporter
->add_data($headers);
1741 public function add_data($row) {
1742 $this->myexporter
->add_data($row);
1746 public function finish_table() {
1747 //nothing to do here
1750 public function finish_document() {
1751 $this->myexporter
->download_file();
1756 * Format a row of data.
1757 * @param array $data
1759 protected function format_row($data) {
1760 $escapeddata = array();
1761 foreach ($data as $value) {
1762 $escapeddata[] = '"' . str_replace('"', '""', $value) . '"';
1764 return implode($this->seperator
, $escapeddata) . "\n";
1770 * @package moodlecore
1771 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1772 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1774 class table_tsv_export_format
extends table_text_export_format_parent
{
1775 protected $seperator = "tab";
1776 protected $mimetype = 'text/tab-separated-values';
1777 protected $ext = '.txt';
1780 require_once($CFG->libdir
. '/csvlib.class.php');
1782 * @package moodlecore
1783 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1784 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1786 class table_csv_export_format
extends table_text_export_format_parent
{
1787 protected $seperator = "comma";
1788 protected $mimetype = 'text/csv';
1789 protected $ext = '.csv';
1793 * @package moodlecore
1794 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1795 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1797 class table_xhtml_export_format
extends table_default_export_format_parent
{
1798 function start_document($filename) {
1799 header("Content-Type: application/download\n");
1800 header("Content-Disposition: attachment; filename=\"$filename.html\"");
1801 header("Expires: 0");
1802 header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
1803 header("Pragma: public");
1806 <?xml version="1.0" encoding="UTF-8"?>
1808 PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
1809 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
1811 <html xmlns="http://www.w3.org/1999/xhtml"
1812 xml:lang="en" lang="en">
1814 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
1815 <style type="text/css">/*<![CDATA[*/
1820 th.header, td.header, div.header {
1821 border-color:#DDDDDD;
1822 background-color:lightGrey;
1837 body, table, td, th {
1838 font-family:Arial,Verdana,Helvetica,sans-serif;
1846 border-collapse:collapse;
1862 <title>$filename</title>
1866 $this->documentstarted
= true;
1869 function start_table($sheettitle) {
1870 $this->table
->sortable(false);
1871 $this->table
->collapsible(false);
1872 echo "<h2>{$sheettitle}</h2>";
1873 $this->table
->start_html();
1876 function output_headers($headers) {
1877 $this->table
->print_headers();
1878 echo html_writer
::start_tag('tbody');
1881 function add_data($row) {
1882 $this->table
->print_row($row);
1886 function add_seperator() {
1887 $this->table
->print_row(NULL);
1891 function finish_table() {
1892 $this->table
->finish_html();
1895 function finish_document() {
1896 echo "</body>\n</html>";
1900 function format_text($text, $format=FORMAT_MOODLE
, $options=NULL, $courseid=NULL) {
1901 if (is_null($options)) {
1902 $options = new stdClass
;
1904 //some sensible defaults
1905 if (!isset($options->para
)) {
1906 $options->para
= false;
1908 if (!isset($options->newlines
)) {
1909 $options->newlines
= false;
1911 if (!isset($options->smiley
)) {
1912 $options->smiley
= false;
1914 if (!isset($options->filter
)) {
1915 $options->filter
= false;
1917 return format_text($text, $format, $options);