2009-05-26 05:18:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
/**
|
2010-07-25 13:35:05 +00:00
|
|
|
* @package core
|
|
|
|
* @subpackage lib
|
|
|
|
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2009-05-26 05:18:52 +00:00
|
|
|
*/
|
|
|
|
|
2011-02-16 10:54:29 +00:00
|
|
|
|
2010-07-25 13:35:05 +00:00
|
|
|
defined('MOODLE_INTERNAL') || die();
|
|
|
|
|
2011-02-16 11:31:23 +00:00
|
|
|
/**#@+
|
|
|
|
* These constants relate to the table's handling of URL parameters.
|
|
|
|
*/
|
2005-05-28 21:24:46 +00:00
|
|
|
define('TABLE_VAR_SORT', 1);
|
|
|
|
define('TABLE_VAR_HIDE', 2);
|
|
|
|
define('TABLE_VAR_SHOW', 3);
|
|
|
|
define('TABLE_VAR_IFIRST', 4);
|
|
|
|
define('TABLE_VAR_ILAST', 5);
|
|
|
|
define('TABLE_VAR_PAGE', 6);
|
2015-03-18 16:33:41 +00:00
|
|
|
define('TABLE_VAR_RESET', 7);
|
2011-02-16 11:31:23 +00:00
|
|
|
/**#@-*/
|
2005-05-28 21:24:46 +00:00
|
|
|
|
2011-02-16 11:31:23 +00:00
|
|
|
/**#@+
|
|
|
|
* Constants that indicate whether the paging bar for the table
|
|
|
|
* appears above or below the table.
|
|
|
|
*/
|
|
|
|
define('TABLE_P_TOP', 1);
|
|
|
|
define('TABLE_P_BOTTOM', 2);
|
|
|
|
/**#@-*/
|
2008-06-06 12:03:09 +00:00
|
|
|
|
2011-02-16 10:54:29 +00:00
|
|
|
|
2009-05-26 05:18:52 +00:00
|
|
|
/**
|
|
|
|
* @package moodlecore
|
|
|
|
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
2005-03-08 11:14:15 +00:00
|
|
|
class flexible_table {
|
|
|
|
|
|
|
|
var $uniqueid = NULL;
|
|
|
|
var $attributes = array();
|
|
|
|
var $headers = array();
|
2015-03-28 23:41:33 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string For create header with help icon.
|
|
|
|
*/
|
|
|
|
private $helpforheaders = array();
|
2005-03-08 11:14:15 +00:00
|
|
|
var $columns = array();
|
|
|
|
var $column_style = array();
|
2005-03-08 14:57:16 +00:00
|
|
|
var $column_class = array();
|
2005-03-08 11:14:15 +00:00
|
|
|
var $column_suppress = array();
|
2007-09-23 15:54:33 +00:00
|
|
|
var $column_nosort = array('userpic');
|
2012-12-11 14:47:33 +08:00
|
|
|
private $column_textsort = array();
|
2015-11-11 13:06:31 +00:00
|
|
|
/** @var boolean Stores if setup has already been called on this flixible table. */
|
2005-03-08 11:14:15 +00:00
|
|
|
var $setup = false;
|
|
|
|
var $baseurl = NULL;
|
2005-05-28 21:24:46 +00:00
|
|
|
var $request = array();
|
2005-03-08 11:14:15 +00:00
|
|
|
|
2015-01-28 14:19:03 +00:00
|
|
|
/**
|
|
|
|
* @var bool Whether or not to store table properties in the user_preferences table.
|
|
|
|
*/
|
|
|
|
private $persistent = false;
|
2005-03-08 11:14:15 +00:00
|
|
|
var $is_collapsible = false;
|
|
|
|
var $is_sortable = false;
|
|
|
|
var $use_pages = false;
|
|
|
|
var $use_initials = false;
|
|
|
|
|
2005-03-10 16:38:01 +00:00
|
|
|
var $maxsortkeys = 2;
|
|
|
|
var $pagesize = 30;
|
|
|
|
var $currpage = 0;
|
|
|
|
var $totalrows = 0;
|
2013-07-29 13:43:54 +08:00
|
|
|
var $currentrow = 0;
|
2005-06-20 03:47:19 +00:00
|
|
|
var $sort_default_column = NULL;
|
|
|
|
var $sort_default_order = SORT_ASC;
|
2008-06-09 10:00:35 +00:00
|
|
|
|
2008-06-06 12:03:09 +00:00
|
|
|
/**
|
|
|
|
* Array of positions in which to display download controls.
|
|
|
|
*/
|
|
|
|
var $showdownloadbuttonsat= array(TABLE_P_TOP);
|
|
|
|
|
2008-06-13 14:30:35 +00:00
|
|
|
/**
|
|
|
|
* @var string Key of field returned by db query that is the id field of the
|
|
|
|
* user table or equivalent.
|
|
|
|
*/
|
|
|
|
public $useridfield = 'id';
|
|
|
|
|
2008-06-06 12:03:09 +00:00
|
|
|
/**
|
|
|
|
* @var string which download plugin to use. Default '' means none - print
|
|
|
|
* html table with paging. Property set by is_downloading which typically
|
|
|
|
* passes in cleaned data from $
|
|
|
|
*/
|
|
|
|
var $download = '';
|
2008-06-09 10:00:35 +00:00
|
|
|
|
2008-06-06 12:03:09 +00:00
|
|
|
/**
|
2011-02-16 13:06:29 +00:00
|
|
|
* @var bool whether data is downloadable from table. Determines whether
|
2008-06-06 12:03:09 +00:00
|
|
|
* to display download buttons. Set by method downloadable().
|
|
|
|
*/
|
|
|
|
var $downloadable = false;
|
2008-06-09 10:00:35 +00:00
|
|
|
|
2008-06-06 12:03:09 +00:00
|
|
|
/**
|
|
|
|
* @var string which download plugin to use. Default '' means none - print
|
|
|
|
* html table with paging.
|
|
|
|
*/
|
|
|
|
var $defaultdownloadformat = 'csv';
|
2008-06-09 10:00:35 +00:00
|
|
|
|
2008-06-06 12:03:09 +00:00
|
|
|
/**
|
2011-02-16 13:06:29 +00:00
|
|
|
* @var bool Has start output been called yet?
|
2008-06-06 12:03:09 +00:00
|
|
|
*/
|
|
|
|
var $started_output = false;
|
2008-06-09 10:00:35 +00:00
|
|
|
|
2008-06-06 12:03:09 +00:00
|
|
|
var $exportclass = null;
|
2008-06-09 10:00:35 +00:00
|
|
|
|
2015-01-28 14:19:03 +00:00
|
|
|
/**
|
|
|
|
* @var array For storing user-customised table properties in the user_preferences db table.
|
|
|
|
*/
|
|
|
|
private $prefs = array();
|
|
|
|
|
2007-06-20 03:52:59 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
2011-02-16 13:06:29 +00:00
|
|
|
* @param int $uniqueid all tables have to have a unique id, this is used
|
|
|
|
* as a key when storing table properties like sort order in the session.
|
2007-06-20 03:52:59 +00:00
|
|
|
*/
|
2011-02-16 11:33:31 +00:00
|
|
|
function __construct($uniqueid) {
|
2005-03-08 11:14:15 +00:00
|
|
|
$this->uniqueid = $uniqueid;
|
2005-05-28 21:24:46 +00:00
|
|
|
$this->request = array(
|
2011-02-16 13:06:29 +00:00
|
|
|
TABLE_VAR_SORT => 'tsort',
|
|
|
|
TABLE_VAR_HIDE => 'thide',
|
|
|
|
TABLE_VAR_SHOW => 'tshow',
|
|
|
|
TABLE_VAR_IFIRST => 'tifirst',
|
|
|
|
TABLE_VAR_ILAST => 'tilast',
|
|
|
|
TABLE_VAR_PAGE => 'page',
|
2015-03-18 16:33:41 +00:00
|
|
|
TABLE_VAR_RESET => 'treset'
|
2005-05-28 21:24:46 +00:00
|
|
|
);
|
2005-03-08 11:14:15 +00:00
|
|
|
}
|
2008-06-06 12:03:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Call this to pass the download type. Use :
|
|
|
|
* $download = optional_param('download', '', PARAM_ALPHA);
|
|
|
|
* To get the download type. We assume that if you call this function with
|
|
|
|
* params that this table's data is downloadable, so we call is_downloadable
|
|
|
|
* for you (even if the param is '', which means no download this time.
|
|
|
|
* Also you can call this method with no params to get the current set
|
|
|
|
* download type.
|
|
|
|
* @param string $download download type. One of csv, tsv, xhtml, ods, etc
|
|
|
|
* @param string $filename filename for downloads without file extension.
|
|
|
|
* @param string $sheettitle title for downloaded data.
|
|
|
|
* @return string download type. One of csv, tsv, xhtml, ods, etc
|
|
|
|
*/
|
2011-02-16 10:54:29 +00:00
|
|
|
function is_downloading($download = null, $filename='', $sheettitle='') {
|
|
|
|
if ($download!==null) {
|
2008-06-06 12:03:09 +00:00
|
|
|
$this->sheettitle = $sheettitle;
|
|
|
|
$this->is_downloadable(true);
|
|
|
|
$this->download = $download;
|
2008-07-30 09:02:44 +00:00
|
|
|
$this->filename = clean_filename($filename);
|
|
|
|
$this->export_class_instance();
|
2008-06-06 12:03:09 +00:00
|
|
|
}
|
|
|
|
return $this->download;
|
|
|
|
}
|
2009-11-01 11:31:16 +00:00
|
|
|
|
2011-02-16 13:06:29 +00:00
|
|
|
/**
|
|
|
|
* Get, and optionally set, the export class.
|
|
|
|
* @param $exportclass (optional) if passed, set the table to use this export class.
|
|
|
|
* @return table_default_export_format_parent the export class in use (after any set).
|
|
|
|
*/
|
|
|
|
function export_class_instance($exportclass = null) {
|
2011-02-16 10:54:29 +00:00
|
|
|
if (!is_null($exportclass)) {
|
2008-09-11 12:48:08 +00:00
|
|
|
$this->started_output = true;
|
2011-02-16 13:06:29 +00:00
|
|
|
$this->exportclass = $exportclass;
|
|
|
|
$this->exportclass->table = $this;
|
|
|
|
} else if (is_null($this->exportclass) && !empty($this->download)) {
|
2008-07-30 09:02:44 +00:00
|
|
|
$classname = 'table_'.$this->download.'_export_format';
|
|
|
|
$this->exportclass = new $classname($this);
|
2011-02-16 10:54:29 +00:00
|
|
|
if (!$this->exportclass->document_started()) {
|
2008-07-30 09:02:44 +00:00
|
|
|
$this->exportclass->start_document($this->filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->exportclass;
|
|
|
|
}
|
2009-11-01 11:31:16 +00:00
|
|
|
|
2008-06-06 12:03:09 +00:00
|
|
|
/**
|
|
|
|
* Probably don't need to call this directly. Calling is_downloading with a
|
|
|
|
* param automatically sets table as downloadable.
|
2008-06-09 10:00:35 +00:00
|
|
|
*
|
2011-02-16 13:06:29 +00:00
|
|
|
* @param bool $downloadable optional param to set whether data from
|
2008-06-06 12:03:09 +00:00
|
|
|
* table is downloadable. If ommitted this function can be used to get
|
|
|
|
* current state of table.
|
2011-02-16 13:06:29 +00:00
|
|
|
* @return bool whether table data is set to be downloadable.
|
2008-06-06 12:03:09 +00:00
|
|
|
*/
|
2011-02-16 10:54:29 +00:00
|
|
|
function is_downloadable($downloadable = null) {
|
|
|
|
if ($downloadable !== null) {
|
2008-06-06 12:03:09 +00:00
|
|
|
$this->downloadable = $downloadable;
|
|
|
|
}
|
|
|
|
return $this->downloadable;
|
|
|
|
}
|
2008-06-09 10:00:35 +00:00
|
|
|
|
2015-01-28 14:19:03 +00:00
|
|
|
/**
|
|
|
|
* Call with boolean true to store table layout changes in the user_preferences table.
|
|
|
|
* Note: user_preferences.value has a maximum length of 1333 characters.
|
|
|
|
* Call with no parameter to get current state of table persistence.
|
|
|
|
*
|
|
|
|
* @param bool $persistent Optional parameter to set table layout persistence.
|
|
|
|
* @return bool Whether or not the table layout preferences will persist.
|
|
|
|
*/
|
|
|
|
public function is_persistent($persistent = null) {
|
|
|
|
if ($persistent == true) {
|
|
|
|
$this->persistent = true;
|
|
|
|
}
|
|
|
|
return $this->persistent;
|
|
|
|
}
|
|
|
|
|
2008-06-06 12:03:09 +00:00
|
|
|
/**
|
|
|
|
* Where to show download buttons.
|
|
|
|
* @param array $showat array of postions in which to show download buttons.
|
|
|
|
* Containing TABLE_P_TOP and/or TABLE_P_BOTTOM
|
|
|
|
*/
|
2011-02-16 10:54:29 +00:00
|
|
|
function show_download_buttons_at($showat) {
|
2008-06-06 12:03:09 +00:00
|
|
|
$this->showdownloadbuttonsat = $showat;
|
|
|
|
}
|
2008-06-09 10:00:35 +00:00
|
|
|
|
2007-06-20 03:52:59 +00:00
|
|
|
/**
|
2008-06-09 10:00:35 +00:00
|
|
|
* Sets the is_sortable variable to the given boolean, sort_default_column to
|
2007-06-20 03:52:59 +00:00
|
|
|
* the given string, and the sort_default_order to the given integer.
|
|
|
|
* @param bool $bool
|
|
|
|
* @param string $defaultcolumn
|
|
|
|
* @param int $defaultorder
|
|
|
|
* @return void
|
|
|
|
*/
|
2005-06-20 03:47:19 +00:00
|
|
|
function sortable($bool, $defaultcolumn = NULL, $defaultorder = SORT_ASC) {
|
2005-03-08 11:14:15 +00:00
|
|
|
$this->is_sortable = $bool;
|
2005-06-20 03:47:19 +00:00
|
|
|
$this->sort_default_column = $defaultcolumn;
|
|
|
|
$this->sort_default_order = $defaultorder;
|
2005-03-08 11:14:15 +00:00
|
|
|
}
|
|
|
|
|
2012-12-11 14:47:33 +08:00
|
|
|
/**
|
|
|
|
* Use text sorting functions for this column (required for text columns with Oracle).
|
2013-11-01 17:59:53 +00:00
|
|
|
* Be warned that you cannot use this with column aliases. You can only do this
|
|
|
|
* with real columns. See MDL-40481 for an example.
|
2012-12-11 14:47:33 +08:00
|
|
|
* @param string column name
|
|
|
|
*/
|
|
|
|
function text_sorting($column) {
|
|
|
|
$this->column_textsort[] = $column;
|
|
|
|
}
|
|
|
|
|
2007-09-23 15:54:33 +00:00
|
|
|
/**
|
|
|
|
* Do not sort using this column
|
|
|
|
* @param string column name
|
|
|
|
*/
|
|
|
|
function no_sorting($column) {
|
|
|
|
$this->column_nosort[] = $column;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is the column sortable?
|
|
|
|
* @param string column name, null means table
|
|
|
|
* @return bool
|
|
|
|
*/
|
2011-02-16 10:54:29 +00:00
|
|
|
function is_sortable($column = null) {
|
2007-09-23 15:54:33 +00:00
|
|
|
if (empty($column)) {
|
|
|
|
return $this->is_sortable;
|
|
|
|
}
|
|
|
|
if (!$this->is_sortable) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return !in_array($column, $this->column_nosort);
|
|
|
|
}
|
2011-02-16 10:54:29 +00:00
|
|
|
|
2007-06-20 03:52:59 +00:00
|
|
|
/**
|
|
|
|
* Sets the is_collapsible variable to the given boolean.
|
|
|
|
* @param bool $bool
|
|
|
|
* @return void
|
|
|
|
*/
|
2005-03-08 11:14:15 +00:00
|
|
|
function collapsible($bool) {
|
|
|
|
$this->is_collapsible = $bool;
|
|
|
|
}
|
|
|
|
|
2007-06-20 03:52:59 +00:00
|
|
|
/**
|
|
|
|
* Sets the use_pages variable to the given boolean.
|
|
|
|
* @param bool $bool
|
|
|
|
* @return void
|
|
|
|
*/
|
2005-03-08 11:14:15 +00:00
|
|
|
function pageable($bool) {
|
|
|
|
$this->use_pages = $bool;
|
|
|
|
}
|
|
|
|
|
2007-06-20 03:52:59 +00:00
|
|
|
/**
|
|
|
|
* Sets the use_initials variable to the given boolean.
|
|
|
|
* @param bool $bool
|
|
|
|
* @return void
|
|
|
|
*/
|
2005-03-08 11:14:15 +00:00
|
|
|
function initialbars($bool) {
|
|
|
|
$this->use_initials = $bool;
|
|
|
|
}
|
|
|
|
|
2007-06-20 03:52:59 +00:00
|
|
|
/**
|
|
|
|
* Sets the pagesize variable to the given integer, the totalrows variable
|
|
|
|
* to the given integer, and the use_pages variable to true.
|
|
|
|
* @param int $perpage
|
|
|
|
* @param int $total
|
|
|
|
* @return void
|
|
|
|
*/
|
2005-03-08 11:14:15 +00:00
|
|
|
function pagesize($perpage, $total) {
|
|
|
|
$this->pagesize = $perpage;
|
|
|
|
$this->totalrows = $total;
|
|
|
|
$this->use_pages = true;
|
|
|
|
}
|
|
|
|
|
2007-06-20 03:52:59 +00:00
|
|
|
/**
|
|
|
|
* Assigns each given variable in the array to the corresponding index
|
|
|
|
* in the request class variable.
|
|
|
|
* @param array $variables
|
|
|
|
* @return void
|
|
|
|
*/
|
2005-05-28 21:24:46 +00:00
|
|
|
function set_control_variables($variables) {
|
2011-02-16 10:54:29 +00:00
|
|
|
foreach ($variables as $what => $variable) {
|
|
|
|
if (isset($this->request[$what])) {
|
2005-05-28 21:24:46 +00:00
|
|
|
$this->request[$what] = $variable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-20 03:52:59 +00:00
|
|
|
/**
|
|
|
|
* Gives the given $value to the $attribute index of $this->attributes.
|
|
|
|
* @param string $attribute
|
|
|
|
* @param mixed $value
|
|
|
|
* @return void
|
|
|
|
*/
|
2005-03-08 11:14:15 +00:00
|
|
|
function set_attribute($attribute, $value) {
|
|
|
|
$this->attributes[$attribute] = $value;
|
|
|
|
}
|
|
|
|
|
2007-06-20 03:52:59 +00:00
|
|
|
/**
|
2008-06-09 10:00:35 +00:00
|
|
|
* What this method does is set the column so that if the same data appears in
|
2007-12-12 19:01:59 +00:00
|
|
|
* consecutive rows, then it is not repeated.
|
2008-06-09 10:00:35 +00:00
|
|
|
*
|
2007-12-12 19:01:59 +00:00
|
|
|
* For example, in the quiz overview report, the fullname column is set to be suppressed, so
|
|
|
|
* that when one student has made multiple attempts, their name is only printed in the row
|
|
|
|
* for their first attempt.
|
2011-02-16 13:06:29 +00:00
|
|
|
* @param int $column the index of a column.
|
2007-06-20 03:52:59 +00:00
|
|
|
*/
|
2005-03-08 11:14:15 +00:00
|
|
|
function column_suppress($column) {
|
2011-02-16 10:54:29 +00:00
|
|
|
if (isset($this->column_suppress[$column])) {
|
2005-03-08 11:14:15 +00:00
|
|
|
$this->column_suppress[$column] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-20 03:52:59 +00:00
|
|
|
/**
|
|
|
|
* Sets the given $column index to the given $classname in $this->column_class.
|
2011-02-16 13:06:29 +00:00
|
|
|
* @param int $column
|
2007-06-20 03:52:59 +00:00
|
|
|
* @param string $classname
|
|
|
|
* @return void
|
|
|
|
*/
|
2005-03-08 14:57:16 +00:00
|
|
|
function column_class($column, $classname) {
|
2011-02-16 10:54:29 +00:00
|
|
|
if (isset($this->column_class[$column])) {
|
2005-03-08 14:57:16 +00:00
|
|
|
$this->column_class[$column] = ' '.$classname; // This space needed so that classnames don't run together in the HTML
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-20 03:52:59 +00:00
|
|
|
/**
|
|
|
|
* Sets the given $column index and $property index to the given $value in $this->column_style.
|
2011-02-16 13:06:29 +00:00
|
|
|
* @param int $column
|
2007-06-20 03:52:59 +00:00
|
|
|
* @param string $property
|
|
|
|
* @param mixed $value
|
|
|
|
* @return void
|
|
|
|
*/
|
2005-03-08 11:14:15 +00:00
|
|
|
function column_style($column, $property, $value) {
|
2011-02-16 10:54:29 +00:00
|
|
|
if (isset($this->column_style[$column])) {
|
2005-03-08 11:14:15 +00:00
|
|
|
$this->column_style[$column][$property] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-20 03:52:59 +00:00
|
|
|
/**
|
2008-06-06 12:03:09 +00:00
|
|
|
* Sets all columns' $propertys to the given $value in $this->column_style.
|
2011-02-16 13:06:29 +00:00
|
|
|
* @param int $property
|
2007-06-20 03:52:59 +00:00
|
|
|
* @param string $value
|
|
|
|
* @return void
|
|
|
|
*/
|
2005-03-08 11:14:15 +00:00
|
|
|
function column_style_all($property, $value) {
|
2011-02-16 10:54:29 +00:00
|
|
|
foreach (array_keys($this->columns) as $column) {
|
2005-03-08 11:14:15 +00:00
|
|
|
$this->column_style[$column][$property] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-20 03:52:59 +00:00
|
|
|
/**
|
2011-02-16 11:37:40 +00:00
|
|
|
* Sets $this->baseurl.
|
|
|
|
* @param moodle_url|string $url the url with params needed to call up this page
|
2007-06-20 03:52:59 +00:00
|
|
|
*/
|
2005-03-08 11:14:15 +00:00
|
|
|
function define_baseurl($url) {
|
2011-02-16 11:37:40 +00:00
|
|
|
$this->baseurl = new moodle_url($url);
|
2005-03-08 11:14:15 +00:00
|
|
|
}
|
|
|
|
|
2007-06-20 03:52:59 +00:00
|
|
|
/**
|
2008-06-06 12:03:09 +00:00
|
|
|
* @param array $columns an array of identifying names for columns. If
|
|
|
|
* columns are sorted then column names must correspond to a field in sql.
|
2007-06-20 03:52:59 +00:00
|
|
|
*/
|
2005-03-08 11:14:15 +00:00
|
|
|
function define_columns($columns) {
|
|
|
|
$this->columns = array();
|
|
|
|
$this->column_style = array();
|
2005-03-08 14:57:16 +00:00
|
|
|
$this->column_class = array();
|
2005-03-08 11:14:15 +00:00
|
|
|
$colnum = 0;
|
|
|
|
|
2011-02-16 10:54:29 +00:00
|
|
|
foreach ($columns as $column) {
|
2005-03-08 11:14:15 +00:00
|
|
|
$this->columns[$column] = $colnum++;
|
|
|
|
$this->column_style[$column] = array();
|
2005-03-08 14:57:16 +00:00
|
|
|
$this->column_class[$column] = '';
|
2005-03-08 11:14:15 +00:00
|
|
|
$this->column_suppress[$column] = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-20 03:52:59 +00:00
|
|
|
/**
|
2008-06-06 12:03:09 +00:00
|
|
|
* @param array $headers numerical keyed array of displayed string titles
|
|
|
|
* for each column.
|
2007-06-20 03:52:59 +00:00
|
|
|
*/
|
2005-03-08 11:14:15 +00:00
|
|
|
function define_headers($headers) {
|
|
|
|
$this->headers = $headers;
|
|
|
|
}
|
2008-06-09 10:00:35 +00:00
|
|
|
|
2015-03-28 23:41:33 +03:00
|
|
|
/**
|
2015-04-01 13:07:42 +08:00
|
|
|
* Defines a help icon for the header
|
|
|
|
*
|
2015-03-28 23:41:33 +03:00
|
|
|
* Always use this function if you need to create header with sorting and help icon.
|
2015-04-01 13:07:42 +08:00
|
|
|
*
|
2015-08-04 11:43:18 +08:00
|
|
|
* @param renderable[] $helpicons An array of renderable objects to be used as help icons
|
2015-03-28 23:41:33 +03:00
|
|
|
*/
|
2015-08-04 11:43:18 +08:00
|
|
|
public function define_help_for_headers($helpicons) {
|
|
|
|
$this->helpforheaders = $helpicons;
|
2015-03-28 23:41:33 +03:00
|
|
|
}
|
|
|
|
|
2007-06-20 03:52:59 +00:00
|
|
|
/**
|
2008-06-06 12:03:09 +00:00
|
|
|
* Must be called after table is defined. Use methods above first. Cannot
|
|
|
|
* use functions below till after calling this method.
|
2007-06-20 03:52:59 +00:00
|
|
|
* @return type?
|
|
|
|
*/
|
2005-03-08 11:14:15 +00:00
|
|
|
function setup() {
|
2015-01-28 14:19:03 +00:00
|
|
|
global $SESSION;
|
2005-03-08 11:14:15 +00:00
|
|
|
|
2011-02-16 10:54:29 +00:00
|
|
|
if (empty($this->columns) || empty($this->uniqueid)) {
|
2005-03-08 11:14:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
2005-04-13 09:57:10 +00:00
|
|
|
|
2015-01-28 14:19:03 +00:00
|
|
|
// Load any existing user preferences.
|
|
|
|
if ($this->persistent) {
|
|
|
|
$this->prefs = json_decode(get_user_preferences('flextable_' . $this->uniqueid), true);
|
|
|
|
} else if (isset($SESSION->flextable[$this->uniqueid])) {
|
|
|
|
$this->prefs = $SESSION->flextable[$this->uniqueid];
|
2005-03-08 11:14:15 +00:00
|
|
|
}
|
2015-11-04 15:07:17 +01:00
|
|
|
|
|
|
|
// Set up default preferences if needed.
|
|
|
|
if (!$this->prefs or optional_param($this->request[TABLE_VAR_RESET], false, PARAM_BOOL)) {
|
2015-01-28 14:19:03 +00:00
|
|
|
$this->prefs = array(
|
|
|
|
'collapse' => array(),
|
|
|
|
'sortby' => array(),
|
|
|
|
'i_first' => '',
|
|
|
|
'i_last' => '',
|
|
|
|
'textsort' => $this->column_textsort,
|
|
|
|
);
|
2005-04-14 17:43:18 +00:00
|
|
|
}
|
2015-01-28 14:19:03 +00:00
|
|
|
$oldprefs = $this->prefs;
|
2005-03-08 11:14:15 +00:00
|
|
|
|
2011-02-16 11:37:40 +00:00
|
|
|
if (($showcol = optional_param($this->request[TABLE_VAR_SHOW], '', PARAM_ALPHANUMEXT)) &&
|
|
|
|
isset($this->columns[$showcol])) {
|
2015-01-28 14:19:03 +00:00
|
|
|
$this->prefs['collapse'][$showcol] = false;
|
2011-02-16 11:37:40 +00:00
|
|
|
|
2011-06-23 13:33:28 +08:00
|
|
|
} else if (($hidecol = optional_param($this->request[TABLE_VAR_HIDE], '', PARAM_ALPHANUMEXT)) &&
|
2011-02-16 11:37:40 +00:00
|
|
|
isset($this->columns[$hidecol])) {
|
2015-01-28 14:19:03 +00:00
|
|
|
$this->prefs['collapse'][$hidecol] = true;
|
|
|
|
if (array_key_exists($hidecol, $this->prefs['sortby'])) {
|
|
|
|
unset($this->prefs['sortby'][$hidecol]);
|
2005-03-08 11:14:15 +00:00
|
|
|
}
|
|
|
|
}
|
2005-04-13 09:57:10 +00:00
|
|
|
|
2005-03-08 11:14:15 +00:00
|
|
|
// Now, update the column attributes for collapsed columns
|
2011-02-16 10:54:29 +00:00
|
|
|
foreach (array_keys($this->columns) as $column) {
|
2015-01-28 14:19:03 +00:00
|
|
|
if (!empty($this->prefs['collapse'][$column])) {
|
2005-03-08 11:14:15 +00:00
|
|
|
$this->column_style[$column]['width'] = '10px';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-16 11:37:40 +00:00
|
|
|
if (($sortcol = optional_param($this->request[TABLE_VAR_SORT], '', PARAM_ALPHANUMEXT)) &&
|
2015-01-28 14:19:03 +00:00
|
|
|
$this->is_sortable($sortcol) && empty($this->prefs['collapse'][$sortcol]) &&
|
2014-09-08 14:14:17 +08:00
|
|
|
(isset($this->columns[$sortcol]) || in_array($sortcol, get_all_user_name_fields())
|
|
|
|
&& isset($this->columns['fullname']))) {
|
2011-02-16 11:37:40 +00:00
|
|
|
|
2015-01-28 14:19:03 +00:00
|
|
|
if (array_key_exists($sortcol, $this->prefs['sortby'])) {
|
2011-02-16 11:37:40 +00:00
|
|
|
// This key already exists somewhere. Change its sortorder and bring it to the top.
|
2015-01-28 14:19:03 +00:00
|
|
|
$sortorder = $this->prefs['sortby'][$sortcol] == SORT_ASC ? SORT_DESC : SORT_ASC;
|
|
|
|
unset($this->prefs['sortby'][$sortcol]);
|
|
|
|
$this->prefs['sortby'] = array_merge(array($sortcol => $sortorder), $this->prefs['sortby']);
|
2011-02-16 11:37:40 +00:00
|
|
|
} else {
|
|
|
|
// Key doesn't exist, so just add it to the beginning of the array, ascending order
|
2015-01-28 14:19:03 +00:00
|
|
|
$this->prefs['sortby'] = array_merge(array($sortcol => SORT_ASC), $this->prefs['sortby']);
|
2005-03-08 11:14:15 +00:00
|
|
|
}
|
2011-02-16 11:37:40 +00:00
|
|
|
|
|
|
|
// Finally, make sure that no more than $this->maxsortkeys are present into the array
|
2015-01-28 14:19:03 +00:00
|
|
|
$this->prefs['sortby'] = array_slice($this->prefs['sortby'], 0, $this->maxsortkeys);
|
2005-03-08 11:14:15 +00:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:56:51 +08:00
|
|
|
// 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.
|
|
|
|
// This prevents results from being returned in a random order if the only order by column contains equal values.
|
|
|
|
if (!empty($this->sort_default_column)) {
|
2015-01-28 14:19:03 +00:00
|
|
|
if (!array_key_exists($this->sort_default_column, $this->prefs['sortby'])) {
|
2012-09-19 12:56:51 +08:00
|
|
|
$defaultsort = array($this->sort_default_column => $this->sort_default_order);
|
2015-01-28 14:19:03 +00:00
|
|
|
$this->prefs['sortby'] = array_merge($this->prefs['sortby'], $defaultsort);
|
2012-09-19 12:56:51 +08:00
|
|
|
}
|
2005-06-20 03:47:19 +00:00
|
|
|
}
|
|
|
|
|
2011-02-16 11:37:40 +00:00
|
|
|
$ilast = optional_param($this->request[TABLE_VAR_ILAST], null, PARAM_RAW);
|
|
|
|
if (!is_null($ilast) && ($ilast ==='' || strpos(get_string('alphabet', 'langconfig'), $ilast) !== false)) {
|
2015-01-28 14:19:03 +00:00
|
|
|
$this->prefs['i_last'] = $ilast;
|
2005-03-08 11:14:15 +00:00
|
|
|
}
|
|
|
|
|
2011-02-16 11:37:40 +00:00
|
|
|
$ifirst = optional_param($this->request[TABLE_VAR_IFIRST], null, PARAM_RAW);
|
|
|
|
if (!is_null($ifirst) && ($ifirst === '' || strpos(get_string('alphabet', 'langconfig'), $ifirst) !== false)) {
|
2015-01-28 14:19:03 +00:00
|
|
|
$this->prefs['i_first'] = $ifirst;
|
2005-03-08 11:14:15 +00:00
|
|
|
}
|
|
|
|
|
2015-01-28 14:19:03 +00:00
|
|
|
// Save user preferences if they have changed.
|
|
|
|
if ($this->prefs != $oldprefs) {
|
|
|
|
if ($this->persistent) {
|
|
|
|
set_user_preference('flextable_' . $this->uniqueid, json_encode($this->prefs));
|
|
|
|
} else {
|
|
|
|
$SESSION->flextable[$this->uniqueid] = $this->prefs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unset($oldprefs);
|
|
|
|
|
2011-02-16 10:54:29 +00:00
|
|
|
if (empty($this->baseurl)) {
|
2011-02-16 11:37:40 +00:00
|
|
|
debugging('You should set baseurl when using flexible_table.');
|
|
|
|
global $PAGE;
|
|
|
|
$this->baseurl = $PAGE->url;
|
2005-03-08 11:14:15 +00:00
|
|
|
}
|
|
|
|
|
2009-08-27 19:14:45 +00:00
|
|
|
$this->currpage = optional_param($this->request[TABLE_VAR_PAGE], 0, PARAM_INT);
|
2005-03-08 11:14:15 +00:00
|
|
|
$this->setup = true;
|
2007-02-22 18:27:10 +00:00
|
|
|
|
2011-02-16 11:37:40 +00:00
|
|
|
// Always introduce the "flexible" class for the table if not specified
|
2007-02-22 18:27:10 +00:00
|
|
|
if (empty($this->attributes)) {
|
|
|
|
$this->attributes['class'] = 'flexible';
|
|
|
|
} else if (!isset($this->attributes['class'])) {
|
|
|
|
$this->attributes['class'] = 'flexible';
|
|
|
|
} else if (!in_array('flexible', explode(' ', $this->attributes['class']))) {
|
|
|
|
$this->attributes['class'] = trim('flexible ' . $this->attributes['class']);
|
|
|
|
}
|
2010-04-01 13:09:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-01-28 14:19:03 +00:00
|
|
|
* Get the order by clause from the session or user preferences, for the table with id $uniqueid.
|
2010-04-01 13:09:24 +00:00
|
|
|
* @param string $uniqueid the identifier for a table.
|
|
|
|
* @return SQL fragment that can be used in an ORDER BY clause.
|
|
|
|
*/
|
|
|
|
public static function get_sort_for_table($uniqueid) {
|
|
|
|
global $SESSION;
|
2015-01-28 14:19:03 +00:00
|
|
|
if (isset($SESSION->flextable[$uniqueid])) {
|
|
|
|
$prefs = $SESSION->flextable[$uniqueid];
|
|
|
|
} else if (!$prefs = json_decode(get_user_preferences('flextable_' . $uniqueid), true)) {
|
|
|
|
return '';
|
2010-04-01 13:09:24 +00:00
|
|
|
}
|
2007-02-22 18:27:10 +00:00
|
|
|
|
2015-01-28 14:19:03 +00:00
|
|
|
if (empty($prefs['sortby'])) {
|
2010-04-01 13:09:24 +00:00
|
|
|
return '';
|
|
|
|
}
|
2015-01-28 14:19:03 +00:00
|
|
|
if (empty($prefs['textsort'])) {
|
|
|
|
$prefs['textsort'] = array();
|
2012-12-13 09:46:36 +08:00
|
|
|
}
|
2010-04-01 13:09:24 +00:00
|
|
|
|
2015-01-28 14:19:03 +00:00
|
|
|
return self::construct_order_by($prefs['sortby'], $prefs['textsort']);
|
2005-03-08 11:14:15 +00:00
|
|
|
}
|
|
|
|
|
2007-06-20 03:52:59 +00:00
|
|
|
/**
|
2010-04-01 13:09:24 +00:00
|
|
|
* Prepare an an order by clause from the list of columns to be sorted.
|
|
|
|
* @param array $cols column name => SORT_ASC or SORT_DESC
|
|
|
|
* @return SQL fragment that can be used in an ORDER BY clause.
|
2007-06-20 03:52:59 +00:00
|
|
|
*/
|
2012-12-11 14:47:33 +08:00
|
|
|
public static function construct_order_by($cols, $textsortcols=array()) {
|
|
|
|
global $DB;
|
2010-04-01 13:09:24 +00:00
|
|
|
$bits = array();
|
|
|
|
|
2011-02-16 10:54:29 +00:00
|
|
|
foreach ($cols as $column => $order) {
|
2012-12-11 14:47:33 +08:00
|
|
|
if (in_array($column, $textsortcols)) {
|
|
|
|
$column = $DB->sql_order_by_text($column);
|
|
|
|
}
|
2010-04-01 13:09:24 +00:00
|
|
|
if ($order == SORT_ASC) {
|
|
|
|
$bits[] = $column . ' ASC';
|
|
|
|
} else {
|
|
|
|
$bits[] = $column . ' DESC';
|
2005-11-13 04:42:21 +00:00
|
|
|
}
|
|
|
|
}
|
2010-04-01 13:09:24 +00:00
|
|
|
|
|
|
|
return implode(', ', $bits);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return SQL fragment that can be used in an ORDER BY clause.
|
|
|
|
*/
|
|
|
|
public function get_sql_sort() {
|
2012-12-11 14:47:33 +08:00
|
|
|
return self::construct_order_by($this->get_sort_columns(), $this->column_textsort);
|
2010-04-01 13:09:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the columns to sort by, in the form required by {@link construct_order_by()}.
|
|
|
|
* @return array column name => SORT_... constant.
|
|
|
|
*/
|
|
|
|
public function get_sort_columns() {
|
|
|
|
if (!$this->setup) {
|
|
|
|
throw new coding_exception('Cannot call get_sort_columns until you have called setup.');
|
2005-03-08 11:14:15 +00:00
|
|
|
}
|
2005-11-13 04:42:21 +00:00
|
|
|
|
2015-01-28 14:19:03 +00:00
|
|
|
if (empty($this->prefs['sortby'])) {
|
2010-04-01 13:09:24 +00:00
|
|
|
return array();
|
2005-03-08 11:14:15 +00:00
|
|
|
}
|
2010-04-01 13:09:24 +00:00
|
|
|
|
2015-01-28 14:19:03 +00:00
|
|
|
foreach ($this->prefs['sortby'] as $column => $notused) {
|
MDL-26425 tablelib better validation of the sort code, also some cleanup.
You may think that the extra validation is unnecessary, since the sort fields are already validated when the URL parameters are parsed, however that overlooks an important point. There may be other options that affect which columns are in the SQL, for example the quiz show individual question grades setting. These other options can cause a column that was in the table, and being sorted on, to disappear. Therefore, it is necessary to re-validate the sort columns when they are used, to make sure they are still present, otherwise you can get ORDER BY sql that refers to non-existant columns, which then causes DB errors.
2011-02-16 11:42:33 +00:00
|
|
|
if (isset($this->columns[$column])) {
|
|
|
|
continue; // This column is OK.
|
|
|
|
}
|
2014-09-08 14:14:17 +08:00
|
|
|
if (in_array($column, get_all_user_name_fields()) &&
|
MDL-26425 tablelib better validation of the sort code, also some cleanup.
You may think that the extra validation is unnecessary, since the sort fields are already validated when the URL parameters are parsed, however that overlooks an important point. There may be other options that affect which columns are in the SQL, for example the quiz show individual question grades setting. These other options can cause a column that was in the table, and being sorted on, to disappear. Therefore, it is necessary to re-validate the sort columns when they are used, to make sure they are still present, otherwise you can get ORDER BY sql that refers to non-existant columns, which then causes DB errors.
2011-02-16 11:42:33 +00:00
|
|
|
isset($this->columns['fullname'])) {
|
|
|
|
continue; // This column is OK.
|
|
|
|
}
|
|
|
|
// This column is not OK.
|
2015-01-28 14:19:03 +00:00
|
|
|
unset($this->prefs['sortby'][$column]);
|
MDL-26425 tablelib better validation of the sort code, also some cleanup.
You may think that the extra validation is unnecessary, since the sort fields are already validated when the URL parameters are parsed, however that overlooks an important point. There may be other options that affect which columns are in the SQL, for example the quiz show individual question grades setting. These other options can cause a column that was in the table, and being sorted on, to disappear. Therefore, it is necessary to re-validate the sort columns when they are used, to make sure they are still present, otherwise you can get ORDER BY sql that refers to non-existant columns, which then causes DB errors.
2011-02-16 11:42:33 +00:00
|
|
|
}
|
|
|
|
|
2015-01-28 14:19:03 +00:00
|
|
|
return $this->prefs['sortby'];
|
2005-03-08 11:14:15 +00:00
|
|
|
}
|
|
|
|
|
2007-06-20 03:52:59 +00:00
|
|
|
/**
|
2011-02-16 13:06:29 +00:00
|
|
|
* @return int the offset for LIMIT clause of SQL
|
2007-06-20 03:52:59 +00:00
|
|
|
*/
|
2005-03-08 11:14:15 +00:00
|
|
|
function get_page_start() {
|
2011-02-16 10:54:29 +00:00
|
|
|
if (!$this->use_pages) {
|
2005-03-08 11:14:15 +00:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return $this->currpage * $this->pagesize;
|
|
|
|
}
|
|
|
|
|
2007-06-20 03:52:59 +00:00
|
|
|
/**
|
2011-02-16 13:06:29 +00:00
|
|
|
* @return int the pagesize for LIMIT clause of SQL
|
2007-06-20 03:52:59 +00:00
|
|
|
*/
|
2005-03-08 11:14:15 +00:00
|
|
|
function get_page_size() {
|
2011-02-16 10:54:29 +00:00
|
|
|
if (!$this->use_pages) {
|
2005-03-08 11:14:15 +00:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return $this->pagesize;
|
|
|
|
}
|
|
|
|
|
2007-06-20 03:52:59 +00:00
|
|
|
/**
|
MDL-26425 tablelib better validation of the sort code, also some cleanup.
You may think that the extra validation is unnecessary, since the sort fields are already validated when the URL parameters are parsed, however that overlooks an important point. There may be other options that affect which columns are in the SQL, for example the quiz show individual question grades setting. These other options can cause a column that was in the table, and being sorted on, to disappear. Therefore, it is necessary to re-validate the sort columns when they are used, to make sure they are still present, otherwise you can get ORDER BY sql that refers to non-existant columns, which then causes DB errors.
2011-02-16 11:42:33 +00:00
|
|
|
* @return string sql to add to where statement.
|
2007-06-20 03:52:59 +00:00
|
|
|
*/
|
2005-03-08 11:14:15 +00:00
|
|
|
function get_sql_where() {
|
2008-06-02 21:56:06 +00:00
|
|
|
global $DB;
|
2005-03-08 11:14:15 +00:00
|
|
|
|
2010-09-04 13:02:49 +00:00
|
|
|
$conditions = array();
|
|
|
|
$params = array();
|
|
|
|
|
2010-09-05 12:27:17 +00:00
|
|
|
if (isset($this->columns['fullname'])) {
|
|
|
|
static $i = 0;
|
|
|
|
$i++;
|
|
|
|
|
2015-01-28 14:19:03 +00:00
|
|
|
if (!empty($this->prefs['i_first'])) {
|
2010-09-05 12:27:17 +00:00
|
|
|
$conditions[] = $DB->sql_like('firstname', ':ifirstc'.$i, false, false);
|
2015-01-28 14:19:03 +00:00
|
|
|
$params['ifirstc'.$i] = $this->prefs['i_first'].'%';
|
2010-09-05 12:27:17 +00:00
|
|
|
}
|
2015-01-28 14:19:03 +00:00
|
|
|
if (!empty($this->prefs['i_last'])) {
|
2010-09-05 12:27:17 +00:00
|
|
|
$conditions[] = $DB->sql_like('lastname', ':ilastc'.$i, false, false);
|
2015-01-28 14:19:03 +00:00
|
|
|
$params['ilastc'.$i] = $this->prefs['i_last'].'%';
|
2010-09-05 12:27:17 +00:00
|
|
|
}
|
2005-03-08 11:14:15 +00:00
|
|
|
}
|
|
|
|
|
2010-09-04 13:02:49 +00:00
|
|
|
return array(implode(" AND ", $conditions), $params);
|
2005-03-08 11:14:15 +00:00
|
|
|
}
|
|
|
|
|
2007-06-20 03:52:59 +00:00
|
|
|
/**
|
2014-02-12 15:45:59 +07:00
|
|
|
* Add a row of data to the table. This function takes an array or object with
|
|
|
|
* column names as keys or property names.
|
|
|
|
*
|
2008-06-06 12:03:09 +00:00
|
|
|
* It ignores any elements with keys that are not defined as columns. It
|
|
|
|
* puts in empty strings into the row when there is no element in the passed
|
|
|
|
* array corresponding to a column in the table. It puts the row elements in
|
2014-02-12 15:45:59 +07:00
|
|
|
* the proper order (internally row table data is stored by in arrays with
|
|
|
|
* a numerical index corresponding to the column number).
|
|
|
|
*
|
|
|
|
* @param object|array $rowwithkeys array keys or object property names are column names,
|
|
|
|
* as defined in call to define_columns.
|
2009-02-25 08:07:19 +00:00
|
|
|
* @param string $classname CSS class name to add to this row's tr tag.
|
2008-06-06 12:03:09 +00:00
|
|
|
*/
|
2011-02-16 10:54:29 +00:00
|
|
|
function add_data_keyed($rowwithkeys, $classname = '') {
|
2009-02-25 08:07:19 +00:00
|
|
|
$this->add_data($this->get_row_from_keyed($rowwithkeys), $classname);
|
2008-06-06 12:03:09 +00:00
|
|
|
}
|
2008-06-09 10:00:35 +00:00
|
|
|
|
2014-02-12 15:45:59 +07:00
|
|
|
/**
|
|
|
|
* Add a number of rows to the table at once. And optionally finish output after they have been added.
|
|
|
|
*
|
|
|
|
* @param (object|array|null)[] $rowstoadd Array of rows to add to table, a null value in array adds a separator row. Or a
|
|
|
|
* object or array is added to table. We expect properties for the row array as would be
|
|
|
|
* passed to add_data_keyed.
|
|
|
|
* @param bool $finish
|
|
|
|
*/
|
|
|
|
public function format_and_add_array_of_rows($rowstoadd, $finish = true) {
|
|
|
|
foreach ($rowstoadd as $row) {
|
|
|
|
if (is_null($row)) {
|
|
|
|
$this->add_separator();
|
|
|
|
} else {
|
|
|
|
$this->add_data_keyed($this->format_row($row));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($finish) {
|
|
|
|
$this->finish_output(!$this->is_downloading());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-06 12:03:09 +00:00
|
|
|
/**
|
|
|
|
* Add a seperator line to table.
|
|
|
|
*/
|
|
|
|
function add_separator() {
|
2011-02-16 10:54:29 +00:00
|
|
|
if (!$this->setup) {
|
2008-06-06 12:03:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$this->add_data(NULL);
|
|
|
|
}
|
2008-06-09 10:00:35 +00:00
|
|
|
|
2008-06-06 12:03:09 +00:00
|
|
|
/**
|
|
|
|
* This method actually directly echoes the row passed to it now or adds it
|
|
|
|
* to the download. If this is the first row and start_output has not
|
|
|
|
* already been called this method also calls start_output to open the table
|
|
|
|
* or send headers for the downloaded.
|
|
|
|
* Can be used as before. print_html now calls finish_html to close table.
|
2008-06-09 10:00:35 +00:00
|
|
|
*
|
2008-06-06 12:03:09 +00:00
|
|
|
* @param array $row a numerically keyed row of data to add to the table.
|
2009-02-25 08:07:19 +00:00
|
|
|
* @param string $classname CSS class name to add to this row's tr tag.
|
2011-02-16 13:06:29 +00:00
|
|
|
* @return bool success.
|
2008-06-06 12:03:09 +00:00
|
|
|
*/
|
2009-02-25 08:07:19 +00:00
|
|
|
function add_data($row, $classname = '') {
|
2011-02-16 10:54:29 +00:00
|
|
|
if (!$this->setup) {
|
2008-06-06 12:03:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-02-16 10:54:29 +00:00
|
|
|
if (!$this->started_output) {
|
2008-06-06 12:03:09 +00:00
|
|
|
$this->start_output();
|
|
|
|
}
|
2011-02-16 10:54:29 +00:00
|
|
|
if ($this->exportclass!==null) {
|
|
|
|
if ($row === null) {
|
2008-06-06 12:03:09 +00:00
|
|
|
$this->exportclass->add_seperator();
|
|
|
|
} else {
|
|
|
|
$this->exportclass->add_data($row);
|
|
|
|
}
|
|
|
|
} else {
|
2009-02-25 08:07:19 +00:00
|
|
|
$this->print_row($row, $classname);
|
2008-06-06 12:03:09 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* You should call this to finish outputting the table data after adding
|
|
|
|
* data to the table with add_data or add_data_keyed.
|
2008-06-09 10:00:35 +00:00
|
|
|
*
|
2008-06-06 12:03:09 +00:00
|
|
|
*/
|
2011-02-16 10:54:29 +00:00
|
|
|
function finish_output($closeexportclassdoc = true) {
|
|
|
|
if ($this->exportclass!==null) {
|
2008-07-30 09:02:44 +00:00
|
|
|
$this->exportclass->finish_table();
|
2011-02-16 10:54:29 +00:00
|
|
|
if ($closeexportclassdoc) {
|
2008-07-30 09:02:44 +00:00
|
|
|
$this->exportclass->finish_document();
|
|
|
|
}
|
2011-02-16 10:54:29 +00:00
|
|
|
} else {
|
2008-06-06 12:03:09 +00:00
|
|
|
$this->finish_html();
|
|
|
|
}
|
|
|
|
}
|
2008-06-09 10:00:35 +00:00
|
|
|
|
2008-06-06 12:03:09 +00:00
|
|
|
/**
|
|
|
|
* Hook that can be overridden in child classes to wrap a table in a form
|
|
|
|
* for example. Called only when there is data to display and not
|
|
|
|
* downloading.
|
|
|
|
*/
|
2011-02-16 10:54:29 +00:00
|
|
|
function wrap_html_start() {
|
2008-06-06 12:03:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook that can be overridden in child classes to wrap a table in a form
|
|
|
|
* for example. Called only when there is data to display and not
|
|
|
|
* downloading.
|
|
|
|
*/
|
2011-02-16 10:54:29 +00:00
|
|
|
function wrap_html_finish() {
|
2008-06-06 12:03:09 +00:00
|
|
|
}
|
2008-06-09 10:00:35 +00:00
|
|
|
|
2008-06-13 14:30:35 +00:00
|
|
|
/**
|
2014-02-12 15:45:59 +07:00
|
|
|
* Call appropriate methods on this table class to perform any processing on values before displaying in table.
|
|
|
|
* Takes raw data from the database and process it into human readable format, perhaps also adding html linking when
|
|
|
|
* displaying table as html, adding a div wrap, etc.
|
|
|
|
*
|
|
|
|
* See for example col_fullname below which will be called for a column whose name is 'fullname'.
|
2008-06-13 14:30:35 +00:00
|
|
|
*
|
2014-02-12 15:45:59 +07:00
|
|
|
* @param array|object $row row of data from db used to make one row of the table.
|
2008-06-13 14:30:35 +00:00
|
|
|
* @return array one row for the table, added using add_data_keyed method.
|
|
|
|
*/
|
2011-02-16 10:54:29 +00:00
|
|
|
function format_row($row) {
|
2014-02-12 15:45:59 +07:00
|
|
|
if (is_array($row)) {
|
|
|
|
$row = (object)$row;
|
|
|
|
}
|
2008-06-13 14:30:35 +00:00
|
|
|
$formattedrow = array();
|
2011-02-16 10:54:29 +00:00
|
|
|
foreach (array_keys($this->columns) as $column) {
|
2008-06-13 14:30:35 +00:00
|
|
|
$colmethodname = 'col_'.$column;
|
2011-02-16 10:54:29 +00:00
|
|
|
if (method_exists($this, $colmethodname)) {
|
2008-06-13 14:30:35 +00:00
|
|
|
$formattedcolumn = $this->$colmethodname($row);
|
|
|
|
} else {
|
|
|
|
$formattedcolumn = $this->other_cols($column, $row);
|
2011-02-16 10:54:29 +00:00
|
|
|
if ($formattedcolumn===NULL) {
|
2008-06-13 14:30:35 +00:00
|
|
|
$formattedcolumn = $row->$column;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$formattedrow[$column] = $formattedcolumn;
|
|
|
|
}
|
|
|
|
return $formattedrow;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fullname is treated as a special columname in tablelib and should always
|
|
|
|
* be treated the same as the fullname of a user.
|
|
|
|
* @uses $this->useridfield if the userid field is not expected to be id
|
|
|
|
* then you need to override $this->useridfield to point at the correct
|
|
|
|
* field for the user id.
|
|
|
|
*
|
2014-02-12 15:45:59 +07:00
|
|
|
* @param object $row the data from the db containing all fields from the
|
|
|
|
* users table necessary to construct the full name of the user in
|
|
|
|
* current language.
|
|
|
|
* @return string contents of cell in column 'fullname', for this row.
|
2008-06-13 14:30:35 +00:00
|
|
|
*/
|
2011-02-16 10:54:29 +00:00
|
|
|
function col_fullname($row) {
|
2014-02-12 15:45:59 +07:00
|
|
|
global $COURSE;
|
2008-06-13 14:30:35 +00:00
|
|
|
|
2012-10-19 16:49:34 +08:00
|
|
|
$name = fullname($row);
|
|
|
|
if ($this->download) {
|
|
|
|
return $name;
|
|
|
|
}
|
2011-02-16 11:46:48 +00:00
|
|
|
|
2012-10-19 16:49:34 +08:00
|
|
|
$userid = $row->{$this->useridfield};
|
|
|
|
if ($COURSE->id == SITEID) {
|
|
|
|
$profileurl = new moodle_url('/user/profile.php', array('id' => $userid));
|
2008-06-13 14:30:35 +00:00
|
|
|
} else {
|
2012-10-19 16:49:34 +08:00
|
|
|
$profileurl = new moodle_url('/user/view.php',
|
|
|
|
array('id' => $userid, 'course' => $COURSE->id));
|
2008-06-13 14:30:35 +00:00
|
|
|
}
|
2012-10-19 16:49:34 +08:00
|
|
|
return html_writer::link($profileurl, $name);
|
2008-06-13 14:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* You can override this method in a child class. See the description of
|
|
|
|
* build_table which calls this method.
|
|
|
|
*/
|
2011-02-16 10:54:29 +00:00
|
|
|
function other_cols($column, $row) {
|
2008-06-13 14:30:35 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-10-14 15:00:26 +00:00
|
|
|
/**
|
|
|
|
* Used from col_* functions when text is to be displayed. Does the
|
|
|
|
* right thing - either converts text to html or strips any html tags
|
|
|
|
* depending on if we are downloading and what is the download type. Params
|
|
|
|
* are the same as format_text function in weblib.php but some default
|
|
|
|
* options are changed.
|
|
|
|
*/
|
2011-02-16 10:54:29 +00:00
|
|
|
function format_text($text, $format=FORMAT_MOODLE, $options=NULL, $courseid=NULL) {
|
|
|
|
if (!$this->is_downloading()) {
|
|
|
|
if (is_null($options)) {
|
2008-10-14 15:00:26 +00:00
|
|
|
$options = new stdClass;
|
|
|
|
}
|
|
|
|
//some sensible defaults
|
2011-02-16 10:54:29 +00:00
|
|
|
if (!isset($options->para)) {
|
2008-10-14 15:00:26 +00:00
|
|
|
$options->para = false;
|
|
|
|
}
|
2011-02-16 10:54:29 +00:00
|
|
|
if (!isset($options->newlines)) {
|
2008-10-14 15:00:26 +00:00
|
|
|
$options->newlines = false;
|
|
|
|
}
|
|
|
|
if (!isset($options->smiley)) {
|
|
|
|
$options->smiley = false;
|
|
|
|
}
|
|
|
|
if (!isset($options->filter)) {
|
|
|
|
$options->filter = false;
|
|
|
|
}
|
|
|
|
return format_text($text, $format, $options);
|
|
|
|
} else {
|
2014-01-28 16:43:47 +09:00
|
|
|
$eci = $this->export_class_instance();
|
2008-10-14 15:00:26 +00:00
|
|
|
return $eci->format_text($text, $format, $options, $courseid);
|
|
|
|
}
|
|
|
|
}
|
2008-06-06 12:03:09 +00:00
|
|
|
/**
|
|
|
|
* This method is deprecated although the old api is still supported.
|
|
|
|
* @deprecated 1.9.2 - Jun 2, 2008
|
|
|
|
*/
|
|
|
|
function print_html() {
|
2011-02-16 10:54:29 +00:00
|
|
|
if (!$this->setup) {
|
2008-06-06 12:03:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$this->finish_html();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function is not part of the public api.
|
|
|
|
* @return string initial of first name we are currently filtering by
|
2007-06-20 03:52:59 +00:00
|
|
|
*/
|
2005-06-03 22:48:07 +00:00
|
|
|
function get_initial_first() {
|
2011-02-16 10:54:29 +00:00
|
|
|
if (!$this->use_initials) {
|
2005-06-03 22:48:07 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-01-28 14:19:03 +00:00
|
|
|
return $this->prefs['i_first'];
|
2005-06-03 22:48:07 +00:00
|
|
|
}
|
|
|
|
|
2007-06-20 03:52:59 +00:00
|
|
|
/**
|
2008-06-06 12:03:09 +00:00
|
|
|
* This function is not part of the public api.
|
|
|
|
* @return string initial of last name we are currently filtering by
|
2007-06-20 03:52:59 +00:00
|
|
|
*/
|
2005-06-03 22:48:07 +00:00
|
|
|
function get_initial_last() {
|
2011-02-16 10:54:29 +00:00
|
|
|
if (!$this->use_initials) {
|
2005-06-03 22:48:07 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-01-28 14:19:03 +00:00
|
|
|
return $this->prefs['i_last'];
|
2005-06-03 22:48:07 +00:00
|
|
|
}
|
2008-06-09 10:00:35 +00:00
|
|
|
|
2011-02-16 13:06:29 +00:00
|
|
|
/**
|
|
|
|
* Helper function, used by {@link print_initials_bar()} to output one initial bar.
|
|
|
|
* @param array $alpha of letters in the alphabet.
|
|
|
|
* @param string $current the currently selected letter.
|
|
|
|
* @param string $class class name to add to this initial bar.
|
|
|
|
* @param string $title the name to put in front of this initial bar.
|
|
|
|
* @param string $urlvar URL parameter name for this initial.
|
|
|
|
*/
|
2011-02-16 11:55:12 +00:00
|
|
|
protected function print_one_initials_bar($alpha, $current, $class, $title, $urlvar) {
|
|
|
|
echo html_writer::start_tag('div', array('class' => 'initialbar ' . $class)) .
|
|
|
|
$title . ' : ';
|
|
|
|
if ($current) {
|
|
|
|
echo html_writer::link($this->baseurl->out(false, array($urlvar => '')), get_string('all'));
|
|
|
|
} else {
|
|
|
|
echo html_writer::tag('strong', get_string('all'));
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($alpha as $letter) {
|
|
|
|
if ($letter === $current) {
|
|
|
|
echo html_writer::tag('strong', $letter);
|
|
|
|
} else {
|
|
|
|
echo html_writer::link($this->baseurl->out(false, array($urlvar => $letter)), $letter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
echo html_writer::end_tag('div');
|
|
|
|
}
|
|
|
|
|
2007-06-20 03:52:59 +00:00
|
|
|
/**
|
2008-06-06 12:03:09 +00:00
|
|
|
* This function is not part of the public api.
|
2007-06-20 03:52:59 +00:00
|
|
|
*/
|
2011-02-16 10:54:29 +00:00
|
|
|
function print_initials_bar() {
|
2015-01-28 14:19:03 +00:00
|
|
|
if ((!empty($this->prefs['i_last']) || !empty($this->prefs['i_first']) ||$this->use_initials)
|
2008-08-29 06:03:02 +00:00
|
|
|
&& isset($this->columns['fullname'])) {
|
2008-06-09 10:00:35 +00:00
|
|
|
|
2010-04-10 17:30:00 +00:00
|
|
|
$alpha = explode(',', get_string('alphabet', 'langconfig'));
|
2008-06-09 10:00:35 +00:00
|
|
|
|
2005-03-08 11:14:15 +00:00
|
|
|
// Bar of first initials
|
2015-01-28 14:19:03 +00:00
|
|
|
if (!empty($this->prefs['i_first'])) {
|
|
|
|
$ifirst = $this->prefs['i_first'];
|
2005-03-08 11:14:15 +00:00
|
|
|
} else {
|
2011-02-16 11:55:12 +00:00
|
|
|
$ifirst = '';
|
2005-03-08 11:14:15 +00:00
|
|
|
}
|
2011-02-16 11:55:12 +00:00
|
|
|
$this->print_one_initials_bar($alpha, $ifirst, 'firstinitial',
|
|
|
|
get_string('firstname'), $this->request[TABLE_VAR_IFIRST]);
|
2008-06-09 10:00:35 +00:00
|
|
|
|
2005-03-08 11:14:15 +00:00
|
|
|
// Bar of last initials
|
2015-01-28 14:19:03 +00:00
|
|
|
if (!empty($this->prefs['i_last'])) {
|
|
|
|
$ilast = $this->prefs['i_last'];
|
2005-03-08 11:14:15 +00:00
|
|
|
} else {
|
2011-02-16 11:55:12 +00:00
|
|
|
$ilast = '';
|
2005-03-08 11:14:15 +00:00
|
|
|
}
|
2011-02-16 11:55:12 +00:00
|
|
|
$this->print_one_initials_bar($alpha, $ilast, 'lastinitial',
|
|
|
|
get_string('lastname'), $this->request[TABLE_VAR_ILAST]);
|
2008-06-06 12:03:09 +00:00
|
|
|
}
|
|
|
|
}
|
2005-03-08 11:14:15 +00:00
|
|
|
|
2008-06-06 12:03:09 +00:00
|
|
|
/**
|
|
|
|
* This function is not part of the public api.
|
|
|
|
*/
|
2011-02-16 10:54:29 +00:00
|
|
|
function print_nothing_to_display() {
|
2009-08-06 08:19:21 +00:00
|
|
|
global $OUTPUT;
|
2015-03-18 16:33:41 +00:00
|
|
|
|
|
|
|
// Render button to allow user to reset table preferences.
|
|
|
|
echo $this->render_reset_button();
|
|
|
|
|
2008-08-29 06:03:02 +00:00
|
|
|
$this->print_initials_bar();
|
2005-03-08 11:14:15 +00:00
|
|
|
|
2009-08-06 08:19:21 +00:00
|
|
|
echo $OUTPUT->heading(get_string('nothingtodisplay'));
|
2008-06-06 12:03:09 +00:00
|
|
|
}
|
2005-03-08 11:14:15 +00:00
|
|
|
|
2008-06-06 12:03:09 +00:00
|
|
|
/**
|
|
|
|
* This function is not part of the public api.
|
|
|
|
*/
|
2011-02-16 10:54:29 +00:00
|
|
|
function get_row_from_keyed($rowwithkeys) {
|
|
|
|
if (is_object($rowwithkeys)) {
|
2008-06-06 12:03:09 +00:00
|
|
|
$rowwithkeys = (array)$rowwithkeys;
|
2005-03-08 11:14:15 +00:00
|
|
|
}
|
2008-06-14 13:18:14 +00:00
|
|
|
$row = array();
|
2011-02-16 10:54:29 +00:00
|
|
|
foreach (array_keys($this->columns) as $column) {
|
|
|
|
if (isset($rowwithkeys[$column])) {
|
2008-06-06 12:03:09 +00:00
|
|
|
$row [] = $rowwithkeys[$column];
|
|
|
|
} else {
|
|
|
|
$row[] ='';
|
|
|
|
}
|
2005-03-08 11:14:15 +00:00
|
|
|
}
|
2008-06-06 12:03:09 +00:00
|
|
|
return $row;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* This function is not part of the public api.
|
|
|
|
*/
|
2011-02-16 10:54:29 +00:00
|
|
|
function get_download_menu() {
|
2008-06-06 12:03:09 +00:00
|
|
|
$allclasses= get_declared_classes();
|
|
|
|
$exportclasses = array();
|
2011-02-16 10:54:29 +00:00
|
|
|
foreach ($allclasses as $class) {
|
2008-06-06 12:03:09 +00:00
|
|
|
$matches = array();
|
2011-02-16 10:54:29 +00:00
|
|
|
if (preg_match('/^table\_([a-z]+)\_export\_format$/', $class, $matches)) {
|
2008-06-06 12:03:09 +00:00
|
|
|
$type = $matches[1];
|
|
|
|
$exportclasses[$type]= get_string("download$type", 'table');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $exportclasses;
|
|
|
|
}
|
2008-06-09 10:00:35 +00:00
|
|
|
|
2008-06-06 12:03:09 +00:00
|
|
|
/**
|
|
|
|
* This function is not part of the public api.
|
|
|
|
*/
|
2011-02-16 10:54:29 +00:00
|
|
|
function download_buttons() {
|
|
|
|
if ($this->is_downloadable() && !$this->is_downloading()) {
|
2008-06-06 12:03:09 +00:00
|
|
|
$downloadoptions = $this->get_download_menu();
|
2012-08-23 13:09:57 +08:00
|
|
|
|
|
|
|
$downloadelements = new stdClass();
|
|
|
|
$downloadelements->formatsmenu = html_writer::select($downloadoptions,
|
|
|
|
'download', $this->defaultdownloadformat, false);
|
|
|
|
$downloadelements->downloadbutton = '<input type="submit" value="'.
|
|
|
|
get_string('download').'"/>';
|
2008-06-06 12:03:09 +00:00
|
|
|
$html = '<form action="'. $this->baseurl .'" method="post">';
|
|
|
|
$html .= '<div class="mdl-align">';
|
2012-08-23 13:09:57 +08:00
|
|
|
$html .= html_writer::tag('label', get_string('downloadas', 'table', $downloadelements));
|
2008-06-06 12:03:09 +00:00
|
|
|
$html .= '</div></form>';
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* This function is not part of the public api.
|
|
|
|
* You don't normally need to call this. It is called automatically when
|
|
|
|
* needed when you start adding data to the table.
|
2008-06-09 10:00:35 +00:00
|
|
|
*
|
2008-06-06 12:03:09 +00:00
|
|
|
*/
|
2011-02-16 10:54:29 +00:00
|
|
|
function start_output() {
|
2008-06-06 12:03:09 +00:00
|
|
|
$this->started_output = true;
|
2011-02-16 10:54:29 +00:00
|
|
|
if ($this->exportclass!==null) {
|
2008-07-30 09:02:44 +00:00
|
|
|
$this->exportclass->start_table($this->sheettitle);
|
2008-06-06 12:03:09 +00:00
|
|
|
$this->exportclass->output_headers($this->headers);
|
|
|
|
} else {
|
|
|
|
$this->start_html();
|
|
|
|
$this->print_headers();
|
2012-08-21 13:50:38 +08:00
|
|
|
echo html_writer::start_tag('tbody');
|
2008-06-06 12:03:09 +00:00
|
|
|
}
|
|
|
|
}
|
2005-03-08 11:14:15 +00:00
|
|
|
|
2008-06-06 12:03:09 +00:00
|
|
|
/**
|
|
|
|
* This function is not part of the public api.
|
|
|
|
*/
|
2009-02-25 08:07:19 +00:00
|
|
|
function print_row($row, $classname = '') {
|
2014-01-17 13:42:47 +08:00
|
|
|
echo $this->get_row_html($row, $classname);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate html code for the passed row.
|
|
|
|
*
|
|
|
|
* @param array $row Row data.
|
|
|
|
* @param string $classname classes to add.
|
|
|
|
*
|
|
|
|
* @return string $html html code for the row passed.
|
|
|
|
*/
|
|
|
|
public function get_row_html($row, $classname = '') {
|
2008-06-06 12:03:09 +00:00
|
|
|
static $suppress_lastrow = NULL;
|
2014-12-09 10:52:30 +08:00
|
|
|
$rowclasses = array();
|
2009-02-25 08:07:19 +00:00
|
|
|
|
|
|
|
if ($classname) {
|
|
|
|
$rowclasses[] = $classname;
|
|
|
|
}
|
|
|
|
|
2013-07-29 13:43:54 +08:00
|
|
|
$rowid = $this->uniqueid . '_r' . $this->currentrow;
|
2014-01-17 13:42:47 +08:00
|
|
|
$html = '';
|
2013-07-29 13:43:54 +08:00
|
|
|
|
2014-01-17 13:42:47 +08:00
|
|
|
$html .= html_writer::start_tag('tr', array('class' => implode(' ', $rowclasses), 'id' => $rowid));
|
2005-03-08 11:14:15 +00:00
|
|
|
|
2008-06-06 12:03:09 +00:00
|
|
|
// If we have a separator, print it
|
2009-03-10 05:00:16 +00:00
|
|
|
if ($row === NULL) {
|
2009-02-25 08:07:19 +00:00
|
|
|
$colcount = count($this->columns);
|
2014-01-17 13:42:47 +08:00
|
|
|
$html .= html_writer::tag('td', html_writer::tag('div', '',
|
2011-02-16 12:00:30 +00:00
|
|
|
array('class' => 'tabledivider')), array('colspan' => $colcount));
|
|
|
|
|
2009-02-25 08:07:19 +00:00
|
|
|
} else {
|
|
|
|
$colbyindex = array_flip($this->columns);
|
|
|
|
foreach ($row as $index => $data) {
|
2008-06-06 12:03:09 +00:00
|
|
|
$column = $colbyindex[$index];
|
2011-02-16 12:00:30 +00:00
|
|
|
|
2015-01-28 14:19:03 +00:00
|
|
|
if (empty($this->prefs['collapse'][$column])) {
|
2009-02-25 08:07:19 +00:00
|
|
|
if ($this->column_suppress[$column] && $suppress_lastrow !== NULL && $suppress_lastrow[$index] === $data) {
|
2011-02-16 12:00:30 +00:00
|
|
|
$content = ' ';
|
2009-02-25 08:07:19 +00:00
|
|
|
} else {
|
2011-02-16 12:00:30 +00:00
|
|
|
$content = $data;
|
2008-06-06 12:03:09 +00:00
|
|
|
}
|
2009-02-25 08:07:19 +00:00
|
|
|
} else {
|
2011-02-16 12:00:30 +00:00
|
|
|
$content = ' ';
|
2008-06-06 12:03:09 +00:00
|
|
|
}
|
2011-02-16 12:00:30 +00:00
|
|
|
|
2014-01-17 13:42:47 +08:00
|
|
|
$html .= html_writer::tag('td', $content, array(
|
2011-02-16 12:00:30 +00:00
|
|
|
'class' => 'cell c' . $index . $this->column_class[$column],
|
2013-07-29 13:43:54 +08:00
|
|
|
'id' => $rowid . '_c' . $index,
|
2011-02-16 12:00:30 +00:00
|
|
|
'style' => $this->make_styles_string($this->column_style[$column])));
|
2008-06-06 12:03:09 +00:00
|
|
|
}
|
|
|
|
}
|
2009-02-25 08:07:19 +00:00
|
|
|
|
2014-01-17 13:42:47 +08:00
|
|
|
$html .= html_writer::end_tag('tr');
|
2009-02-25 08:07:19 +00:00
|
|
|
|
2005-03-08 11:14:15 +00:00
|
|
|
$suppress_enabled = array_sum($this->column_suppress);
|
2009-02-25 08:07:19 +00:00
|
|
|
if ($suppress_enabled) {
|
2008-06-06 12:03:09 +00:00
|
|
|
$suppress_lastrow = $row;
|
|
|
|
}
|
2013-07-29 13:43:54 +08:00
|
|
|
$this->currentrow++;
|
2014-01-17 13:42:47 +08:00
|
|
|
return $html;
|
2008-06-06 12:03:09 +00:00
|
|
|
}
|
2011-02-16 10:54:29 +00:00
|
|
|
|
2008-06-06 12:03:09 +00:00
|
|
|
/**
|
|
|
|
* This function is not part of the public api.
|
|
|
|
*/
|
2011-02-16 10:54:29 +00:00
|
|
|
function finish_html() {
|
2009-08-07 01:38:36 +00:00
|
|
|
global $OUTPUT;
|
2008-06-06 12:03:09 +00:00
|
|
|
if (!$this->started_output) {
|
|
|
|
//no data has been added to the table.
|
|
|
|
$this->print_nothing_to_display();
|
2011-02-16 12:00:30 +00:00
|
|
|
|
2008-06-06 12:03:09 +00:00
|
|
|
} else {
|
2013-07-29 13:43:54 +08:00
|
|
|
// Print empty rows to fill the table to the current pagesize.
|
|
|
|
// This is done so the header aria-controls attributes do not point to
|
|
|
|
// non existant elements.
|
2013-10-18 11:51:55 +08:00
|
|
|
$emptyrow = array_fill(0, count($this->columns), '');
|
2013-07-29 13:43:54 +08:00
|
|
|
while ($this->currentrow < $this->pagesize) {
|
|
|
|
$this->print_row($emptyrow, 'emptyrow');
|
|
|
|
}
|
|
|
|
|
2012-08-21 13:50:38 +08:00
|
|
|
echo html_writer::end_tag('tbody');
|
2011-02-16 12:00:30 +00:00
|
|
|
echo html_writer::end_tag('table');
|
2010-07-20 05:19:22 +00:00
|
|
|
echo html_writer::end_tag('div');
|
2008-06-06 12:03:09 +00:00
|
|
|
$this->wrap_html_finish();
|
2011-02-16 12:00:30 +00:00
|
|
|
|
2008-06-06 12:03:09 +00:00
|
|
|
// Paging bar
|
2011-02-16 12:00:30 +00:00
|
|
|
if(in_array(TABLE_P_BOTTOM, $this->showdownloadbuttonsat)) {
|
2008-06-06 12:03:09 +00:00
|
|
|
echo $this->download_buttons();
|
|
|
|
}
|
2011-02-16 12:00:30 +00:00
|
|
|
|
|
|
|
if($this->use_pages) {
|
2010-02-17 16:59:41 +00:00
|
|
|
$pagingbar = new paging_bar($this->totalrows, $this->currpage, $this->pagesize, $this->baseurl);
|
2009-09-08 08:39:30 +00:00
|
|
|
$pagingbar->pagevar = $this->request[TABLE_VAR_PAGE];
|
2010-02-17 16:59:41 +00:00
|
|
|
echo $OUTPUT->render($pagingbar);
|
2008-06-06 12:03:09 +00:00
|
|
|
}
|
|
|
|
}
|
2008-06-09 10:00:35 +00:00
|
|
|
}
|
2011-02-16 12:00:30 +00:00
|
|
|
|
2011-02-16 13:06:29 +00:00
|
|
|
/**
|
|
|
|
* Generate the HTML for the collapse/uncollapse icon. This is a helper method
|
|
|
|
* used by {@link print_headers()}.
|
|
|
|
* @param string $column the column name, index into various names.
|
|
|
|
* @param int $index numerical index of the column.
|
|
|
|
* @return string HTML fragment.
|
|
|
|
*/
|
2011-02-16 12:00:30 +00:00
|
|
|
protected function show_hide_link($column, $index) {
|
|
|
|
global $OUTPUT;
|
|
|
|
// Some headers contain <br /> tags, do not include in title, hence the
|
|
|
|
// strip tags.
|
|
|
|
|
2013-07-29 13:43:54 +08:00
|
|
|
$ariacontrols = '';
|
|
|
|
for ($i = 0; $i < $this->pagesize; $i++) {
|
|
|
|
$ariacontrols .= $this->uniqueid . '_r' . $i . '_c' . $index . ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
$ariacontrols = trim($ariacontrols);
|
|
|
|
|
2015-01-28 14:19:03 +00:00
|
|
|
if (!empty($this->prefs['collapse'][$column])) {
|
2013-07-29 13:43:54 +08:00
|
|
|
$linkattributes = array('title' => get_string('show') . ' ' . strip_tags($this->headers[$index]),
|
|
|
|
'aria-expanded' => 'false',
|
|
|
|
'aria-controls' => $ariacontrols);
|
2011-02-16 12:00:30 +00:00
|
|
|
return html_writer::link($this->baseurl->out(false, array($this->request[TABLE_VAR_SHOW] => $column)),
|
|
|
|
html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url('t/switch_plus'), 'alt' => get_string('show'))),
|
2013-07-29 13:43:54 +08:00
|
|
|
$linkattributes);
|
2011-02-16 12:00:30 +00:00
|
|
|
|
|
|
|
} else if ($this->headers[$index] !== NULL) {
|
2013-07-29 13:43:54 +08:00
|
|
|
$linkattributes = array('title' => get_string('hide') . ' ' . strip_tags($this->headers[$index]),
|
|
|
|
'aria-expanded' => 'true',
|
|
|
|
'aria-controls' => $ariacontrols);
|
2011-02-16 12:00:30 +00:00
|
|
|
return html_writer::link($this->baseurl->out(false, array($this->request[TABLE_VAR_HIDE] => $column)),
|
|
|
|
html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url('t/switch_minus'), 'alt' => get_string('hide'))),
|
2013-07-29 13:43:54 +08:00
|
|
|
$linkattributes);
|
2011-02-16 12:00:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-06 12:03:09 +00:00
|
|
|
/**
|
|
|
|
* This function is not part of the public api.
|
|
|
|
*/
|
2011-02-16 10:54:29 +00:00
|
|
|
function print_headers() {
|
2009-07-02 11:09:15 +00:00
|
|
|
global $CFG, $OUTPUT;
|
2005-03-08 11:14:15 +00:00
|
|
|
|
2012-08-15 10:23:41 +01:00
|
|
|
echo html_writer::start_tag('thead');
|
2011-02-16 12:00:30 +00:00
|
|
|
echo html_writer::start_tag('tr');
|
2011-02-16 10:54:29 +00:00
|
|
|
foreach ($this->columns as $column => $index) {
|
2005-03-08 11:14:15 +00:00
|
|
|
|
2011-02-16 12:00:30 +00:00
|
|
|
$icon_hide = '';
|
2011-02-16 10:54:29 +00:00
|
|
|
if ($this->is_collapsible) {
|
2011-02-16 12:00:30 +00:00
|
|
|
$icon_hide = $this->show_hide_link($column, $index);
|
2005-03-08 11:14:15 +00:00
|
|
|
}
|
|
|
|
|
2015-01-28 14:19:03 +00:00
|
|
|
$primarysortcolumn = '';
|
|
|
|
$primarysortorder = '';
|
|
|
|
if (reset($this->prefs['sortby'])) {
|
|
|
|
$primarysortcolumn = key($this->prefs['sortby']);
|
|
|
|
$primarysortorder = current($this->prefs['sortby']);
|
2005-03-10 16:38:01 +00:00
|
|
|
}
|
|
|
|
|
2011-02-16 10:54:29 +00:00
|
|
|
switch ($column) {
|
2005-03-08 11:14:15 +00:00
|
|
|
|
|
|
|
case 'fullname':
|
2013-04-24 10:12:42 +08:00
|
|
|
// Check the full name display for sortable fields.
|
|
|
|
$nameformat = $CFG->fullnamedisplay;
|
|
|
|
if ($nameformat == 'language') {
|
|
|
|
$nameformat = get_string('fullnamedisplay');
|
|
|
|
}
|
2014-09-08 14:14:17 +08:00
|
|
|
$requirednames = order_in_string(get_all_user_name_fields(), $nameformat);
|
2013-04-24 10:12:42 +08:00
|
|
|
|
|
|
|
if (!empty($requirednames)) {
|
|
|
|
if ($this->is_sortable($column)) {
|
|
|
|
// Done this way for the possibility of more than two sortable full name display fields.
|
|
|
|
$this->headers[$index] = '';
|
|
|
|
foreach ($requirednames as $name) {
|
|
|
|
$sortname = $this->sort_link(get_string($name),
|
2015-04-01 13:07:42 +08:00
|
|
|
$name, $primarysortcolumn === $name, $primarysortorder);
|
2013-04-24 10:12:42 +08:00
|
|
|
$this->headers[$index] .= $sortname . ' / ';
|
|
|
|
}
|
2015-04-01 13:07:42 +08:00
|
|
|
$helpicon = '';
|
|
|
|
if (isset($this->helpforheaders[$index])) {
|
|
|
|
$helpicon = $OUTPUT->render($this->helpforheaders[$index]);
|
|
|
|
}
|
|
|
|
$this->headers[$index] = substr($this->headers[$index], 0, -3). $helpicon;
|
2010-07-25 13:35:05 +00:00
|
|
|
}
|
2005-03-08 11:14:15 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2006-11-13 03:43:12 +00:00
|
|
|
case 'userpic':
|
2008-06-09 10:00:35 +00:00
|
|
|
// do nothing, do not display sortable links
|
2006-11-13 03:43:12 +00:00
|
|
|
break;
|
|
|
|
|
2005-03-08 11:14:15 +00:00
|
|
|
default:
|
2011-02-16 10:54:29 +00:00
|
|
|
if ($this->is_sortable($column)) {
|
2015-03-28 23:41:33 +03:00
|
|
|
$helpicon = '';
|
|
|
|
if (isset($this->helpforheaders[$index])) {
|
2015-04-01 13:07:42 +08:00
|
|
|
$helpicon = $OUTPUT->render($this->helpforheaders[$index]);
|
2015-03-28 23:41:33 +03:00
|
|
|
}
|
2011-02-16 12:00:30 +00:00
|
|
|
$this->headers[$index] = $this->sort_link($this->headers[$index],
|
2015-04-01 13:07:42 +08:00
|
|
|
$column, $primarysortcolumn == $column, $primarysortorder) . $helpicon;
|
2005-03-08 11:14:15 +00:00
|
|
|
}
|
|
|
|
}
|
2005-04-13 09:57:10 +00:00
|
|
|
|
2011-02-16 12:00:30 +00:00
|
|
|
$attributes = array(
|
|
|
|
'class' => 'header c' . $index . $this->column_class[$column],
|
|
|
|
'scope' => 'col',
|
|
|
|
);
|
2011-02-16 10:54:29 +00:00
|
|
|
if ($this->headers[$index] === NULL) {
|
2011-02-16 12:00:30 +00:00
|
|
|
$content = ' ';
|
2015-01-28 14:19:03 +00:00
|
|
|
} else if (!empty($this->prefs['collapse'][$column])) {
|
2011-02-16 12:00:30 +00:00
|
|
|
$content = $icon_hide;
|
2011-02-16 10:54:29 +00:00
|
|
|
} else {
|
2011-02-16 12:00:30 +00:00
|
|
|
if (is_array($this->column_style[$column])) {
|
|
|
|
$attributes['style'] = $this->make_styles_string($this->column_style[$column]);
|
|
|
|
}
|
2015-03-28 23:41:33 +03:00
|
|
|
$helpicon = '';
|
|
|
|
if (isset($this->helpforheaders[$index]) && !$this->is_sortable($column)) {
|
2015-04-01 13:07:42 +08:00
|
|
|
$helpicon = $OUTPUT->render($this->helpforheaders[$index]);
|
2015-03-28 23:41:33 +03:00
|
|
|
}
|
|
|
|
$content = $this->headers[$index] . $helpicon . html_writer::tag('div',
|
2011-02-16 12:00:30 +00:00
|
|
|
$icon_hide, array('class' => 'commands'));
|
2005-03-08 11:14:15 +00:00
|
|
|
}
|
2011-02-16 12:00:30 +00:00
|
|
|
echo html_writer::tag('th', $content, $attributes);
|
|
|
|
}
|
|
|
|
|
|
|
|
echo html_writer::end_tag('tr');
|
2012-08-15 10:23:41 +01:00
|
|
|
echo html_writer::end_tag('thead');
|
2011-02-16 12:00:30 +00:00
|
|
|
}
|
|
|
|
|
2011-02-16 13:06:29 +00:00
|
|
|
/**
|
|
|
|
* Generate the HTML for the sort icon. This is a helper method used by {@link sort_link()}.
|
|
|
|
* @param bool $isprimary whether an icon is needed (it is only needed for the primary sort column.)
|
|
|
|
* @param int $order SORT_ASC or SORT_DESC
|
|
|
|
* @return string HTML fragment.
|
|
|
|
*/
|
2011-02-16 12:00:30 +00:00
|
|
|
protected function sort_icon($isprimary, $order) {
|
|
|
|
global $OUTPUT;
|
2005-03-08 11:14:15 +00:00
|
|
|
|
2011-02-16 12:00:30 +00:00
|
|
|
if (!$isprimary) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($order == SORT_ASC) {
|
|
|
|
return html_writer::empty_tag('img',
|
2012-11-15 11:44:18 +08:00
|
|
|
array('src' => $OUTPUT->pix_url('t/sort_asc'), 'alt' => get_string('asc'), 'class' => 'iconsort'));
|
2011-02-16 12:00:30 +00:00
|
|
|
} else {
|
|
|
|
return html_writer::empty_tag('img',
|
2012-11-15 11:44:18 +08:00
|
|
|
array('src' => $OUTPUT->pix_url('t/sort_desc'), 'alt' => get_string('desc'), 'class' => 'iconsort'));
|
2011-02-16 12:00:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-16 13:06:29 +00:00
|
|
|
/**
|
|
|
|
* Generate the correct tool tip for changing the sort order. This is a
|
|
|
|
* helper method used by {@link sort_link()}.
|
|
|
|
* @param bool $isprimary whether the is column is the current primary sort column.
|
|
|
|
* @param int $order SORT_ASC or SORT_DESC
|
|
|
|
* @return string the correct title.
|
|
|
|
*/
|
2011-02-16 12:00:30 +00:00
|
|
|
protected function sort_order_name($isprimary, $order) {
|
|
|
|
if ($isprimary && $order != SORT_ASC) {
|
|
|
|
return get_string('desc');
|
|
|
|
} else {
|
|
|
|
return get_string('asc');
|
2005-03-08 11:14:15 +00:00
|
|
|
}
|
2011-02-16 12:00:30 +00:00
|
|
|
}
|
|
|
|
|
2011-02-16 13:06:29 +00:00
|
|
|
/**
|
|
|
|
* Generate the HTML for the sort link. This is a helper method used by {@link print_headers()}.
|
|
|
|
* @param string $text the text for the link.
|
|
|
|
* @param string $column the column name, may be a fake column like 'firstname' or a real one.
|
|
|
|
* @param bool $isprimary whether the is column is the current primary sort column.
|
|
|
|
* @param int $order SORT_ASC or SORT_DESC
|
|
|
|
* @return string HTML fragment.
|
|
|
|
*/
|
2015-04-01 13:07:42 +08:00
|
|
|
protected function sort_link($text, $column, $isprimary, $order) {
|
2011-02-16 12:00:30 +00:00
|
|
|
return html_writer::link($this->baseurl->out(false,
|
|
|
|
array($this->request[TABLE_VAR_SORT] => $column)),
|
|
|
|
$text . get_accesshide(get_string('sortby') . ' ' .
|
|
|
|
$text . ' ' . $this->sort_order_name($isprimary, $order))) . ' ' .
|
2015-04-01 13:07:42 +08:00
|
|
|
$this->sort_icon($isprimary, $order);
|
2008-06-06 12:03:09 +00:00
|
|
|
}
|
2008-06-09 10:00:35 +00:00
|
|
|
|
2008-06-06 12:03:09 +00:00
|
|
|
/**
|
|
|
|
* This function is not part of the public api.
|
|
|
|
*/
|
2011-02-16 10:54:29 +00:00
|
|
|
function start_html() {
|
2009-09-08 08:39:30 +00:00
|
|
|
global $OUTPUT;
|
2015-03-18 16:33:41 +00:00
|
|
|
|
|
|
|
// Render button to allow user to reset table preferences.
|
|
|
|
echo $this->render_reset_button();
|
|
|
|
|
2008-06-06 12:03:09 +00:00
|
|
|
// Do we need to print initial bars?
|
|
|
|
$this->print_initials_bar();
|
2005-03-08 11:14:15 +00:00
|
|
|
|
2005-04-13 09:57:10 +00:00
|
|
|
// Paging bar
|
2011-02-16 10:54:29 +00:00
|
|
|
if ($this->use_pages) {
|
2010-02-17 16:59:41 +00:00
|
|
|
$pagingbar = new paging_bar($this->totalrows, $this->currpage, $this->pagesize, $this->baseurl);
|
2009-09-08 08:39:30 +00:00
|
|
|
$pagingbar->pagevar = $this->request[TABLE_VAR_PAGE];
|
2010-02-17 16:59:41 +00:00
|
|
|
echo $OUTPUT->render($pagingbar);
|
2005-04-13 09:57:10 +00:00
|
|
|
}
|
2008-06-09 10:00:35 +00:00
|
|
|
|
2011-02-16 10:54:29 +00:00
|
|
|
if (in_array(TABLE_P_TOP, $this->showdownloadbuttonsat)) {
|
2008-06-06 12:03:09 +00:00
|
|
|
echo $this->download_buttons();
|
|
|
|
}
|
2008-06-09 10:00:35 +00:00
|
|
|
|
2008-06-06 12:03:09 +00:00
|
|
|
$this->wrap_html_start();
|
|
|
|
// Start of main data table
|
|
|
|
|
2011-02-16 12:00:30 +00:00
|
|
|
echo html_writer::start_tag('div', array('class' => 'no-overflow'));
|
|
|
|
echo html_writer::start_tag('table', $this->attributes);
|
2008-06-09 10:00:35 +00:00
|
|
|
|
2005-03-08 11:14:15 +00:00
|
|
|
}
|
|
|
|
|
2007-06-20 03:52:59 +00:00
|
|
|
/**
|
2008-06-06 12:03:09 +00:00
|
|
|
* This function is not part of the public api.
|
2011-02-16 12:00:30 +00:00
|
|
|
* @param array $styles CSS-property => value
|
|
|
|
* @return string values suitably to go in a style="" attribute in HTML.
|
2007-06-20 03:52:59 +00:00
|
|
|
*/
|
2011-02-16 12:00:30 +00:00
|
|
|
function make_styles_string($styles) {
|
2011-02-16 10:54:29 +00:00
|
|
|
if (empty($styles)) {
|
2011-02-16 12:00:30 +00:00
|
|
|
return null;
|
2008-06-06 12:03:09 +00:00
|
|
|
}
|
|
|
|
|
2011-02-16 12:00:30 +00:00
|
|
|
$string = '';
|
|
|
|
foreach($styles as $property => $value) {
|
|
|
|
$string .= $property . ':' . $value . ';';
|
2005-03-08 11:14:15 +00:00
|
|
|
}
|
2008-06-06 12:03:09 +00:00
|
|
|
return $string;
|
|
|
|
}
|
2015-03-18 16:33:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate the HTML for the table preferences reset button.
|
|
|
|
*
|
2015-11-04 15:07:17 +01:00
|
|
|
* @return string HTML fragment, empty string if no need to reset
|
2015-03-18 16:33:41 +00:00
|
|
|
*/
|
2015-11-04 15:07:17 +01:00
|
|
|
protected function render_reset_button() {
|
|
|
|
|
|
|
|
if (!$this->can_be_reset()) {
|
2015-09-29 14:57:19 +08:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2015-03-18 16:33:41 +00:00
|
|
|
$url = $this->baseurl->out(false, array($this->request[TABLE_VAR_RESET] => 1));
|
|
|
|
|
2015-11-04 15:07:17 +01:00
|
|
|
$html = html_writer::start_div('resettable mdl-right');
|
2015-03-18 16:33:41 +00:00
|
|
|
$html .= html_writer::link($url, get_string('resettable'));
|
|
|
|
$html .= html_writer::end_div();
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
}
|
2015-11-04 15:07:17 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Are there some table preferences that can be reset?
|
|
|
|
*
|
|
|
|
* If true, then the "reset table preferences" widget should be displayed.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function can_be_reset() {
|
|
|
|
|
|
|
|
// Loop through preferences and make sure they are empty or set to the default value.
|
|
|
|
foreach ($this->prefs as $prefname => $prefval) {
|
|
|
|
|
|
|
|
if ($prefname === 'sortby' and !empty($this->sort_default_column)) {
|
|
|
|
// Check if the actual sorting differs from the default one.
|
|
|
|
if (empty($prefval) or $prefval !== array($this->sort_default_column => $this->sort_default_order)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if ($prefname === 'collapse' and !empty($prefval)) {
|
|
|
|
// Check if there are some collapsed columns (all are expanded by default).
|
|
|
|
foreach ($prefval as $columnname => $iscollapsed) {
|
|
|
|
if ($iscollapsed) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (!empty($prefval)) {
|
|
|
|
// For all other cases, we just check if some preference is set.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2008-06-06 12:03:09 +00:00
|
|
|
}
|
|
|
|
|
2011-02-16 10:54:29 +00:00
|
|
|
|
2009-05-26 05:18:52 +00:00
|
|
|
/**
|
|
|
|
* @package moodlecore
|
|
|
|
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
2011-02-16 10:54:29 +00:00
|
|
|
class table_sql extends flexible_table {
|
2008-06-09 10:00:35 +00:00
|
|
|
|
|
|
|
public $countsql = NULL;
|
|
|
|
public $countparams = NULL;
|
2008-06-06 12:03:09 +00:00
|
|
|
/**
|
2008-06-09 10:00:35 +00:00
|
|
|
* @var object sql for querying db. Has fields 'fields', 'from', 'where', 'params'.
|
2008-06-06 12:03:09 +00:00
|
|
|
*/
|
2008-06-09 10:00:35 +00:00
|
|
|
public $sql = NULL;
|
2008-06-06 12:03:09 +00:00
|
|
|
/**
|
2015-02-23 15:56:05 +08:00
|
|
|
* @var array|\Traversable Data fetched from the db.
|
2008-06-06 12:03:09 +00:00
|
|
|
*/
|
2008-06-09 10:00:35 +00:00
|
|
|
public $rawdata = NULL;
|
|
|
|
|
2008-06-06 12:03:09 +00:00
|
|
|
/**
|
2011-02-16 13:06:29 +00:00
|
|
|
* @var bool Overriding default for this.
|
2008-06-06 12:03:09 +00:00
|
|
|
*/
|
2008-06-09 10:00:35 +00:00
|
|
|
public $is_sortable = true;
|
2008-06-06 12:03:09 +00:00
|
|
|
/**
|
2011-02-16 13:06:29 +00:00
|
|
|
* @var bool Overriding default for this.
|
2008-06-06 12:03:09 +00:00
|
|
|
*/
|
2008-06-09 10:00:35 +00:00
|
|
|
public $is_collapsible = true;
|
|
|
|
|
2008-06-06 12:03:09 +00:00
|
|
|
/**
|
|
|
|
* @param string $uniqueid a string identifying this table.Used as a key in
|
|
|
|
* session vars.
|
|
|
|
*/
|
2011-02-16 11:33:31 +00:00
|
|
|
function __construct($uniqueid) {
|
|
|
|
parent::__construct($uniqueid);
|
2008-06-06 12:03:09 +00:00
|
|
|
// some sensible defaults
|
|
|
|
$this->set_attribute('cellspacing', '0');
|
|
|
|
$this->set_attribute('class', 'generaltable generalbox');
|
2005-03-08 11:14:15 +00:00
|
|
|
}
|
2008-06-09 10:00:35 +00:00
|
|
|
|
2008-06-06 12:03:09 +00:00
|
|
|
/**
|
|
|
|
* Take the data returned from the db_query and go through all the rows
|
|
|
|
* processing each col using either col_{columnname} method or other_cols
|
|
|
|
* method or if other_cols returns NULL then put the data straight into the
|
|
|
|
* table.
|
2015-02-23 15:56:05 +08:00
|
|
|
*
|
|
|
|
* @return void
|
2008-06-06 12:03:09 +00:00
|
|
|
*/
|
2011-02-16 10:54:29 +00:00
|
|
|
function build_table() {
|
2015-02-23 15:56:05 +08:00
|
|
|
|
|
|
|
if ($this->rawdata instanceof \Traversable && !$this->rawdata->valid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!$this->rawdata) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->rawdata as $row) {
|
|
|
|
$formattedrow = $this->format_row($row);
|
|
|
|
$this->add_data_keyed($formattedrow,
|
|
|
|
$this->get_row_class($row));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->rawdata instanceof \core\dml\recordset_walk ||
|
|
|
|
$this->rawdata instanceof moodle_recordset) {
|
|
|
|
$this->rawdata->close();
|
2008-06-06 12:03:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-16 11:47:54 +00:00
|
|
|
/**
|
|
|
|
* Get any extra classes names to add to this row in the HTML.
|
|
|
|
* @param $row array the data for this row.
|
|
|
|
* @return string added to the class="" attribute of the tr.
|
|
|
|
*/
|
|
|
|
function get_row_class($row) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2008-06-06 12:03:09 +00:00
|
|
|
/**
|
|
|
|
* This is only needed if you want to use different sql to count rows.
|
|
|
|
* Used for example when perhaps all db JOINS are not needed when counting
|
|
|
|
* records. You don't need to call this function the count_sql
|
|
|
|
* will be generated automatically.
|
2008-06-09 10:00:35 +00:00
|
|
|
*
|
2008-06-06 12:03:09 +00:00
|
|
|
* We need to count rows returned by the db seperately to the query itself
|
|
|
|
* as we need to know how many pages of data we have to display.
|
|
|
|
*/
|
2011-02-16 10:54:29 +00:00
|
|
|
function set_count_sql($sql, array $params = NULL) {
|
2008-06-06 12:03:09 +00:00
|
|
|
$this->countsql = $sql;
|
2008-06-09 10:00:35 +00:00
|
|
|
$this->countparams = $params;
|
2008-06-06 12:03:09 +00:00
|
|
|
}
|
2008-06-09 10:00:35 +00:00
|
|
|
|
2008-06-06 12:03:09 +00:00
|
|
|
/**
|
|
|
|
* Set the sql to query the db. Query will be :
|
|
|
|
* SELECT $fields FROM $from WHERE $where
|
|
|
|
* Of course you can use sub-queries, JOINS etc. by putting them in the
|
|
|
|
* appropriate clause of the query.
|
|
|
|
*/
|
2011-02-16 10:54:29 +00:00
|
|
|
function set_sql($fields, $from, $where, array $params = NULL) {
|
2010-09-21 08:07:44 +00:00
|
|
|
$this->sql = new stdClass();
|
2008-06-06 12:03:09 +00:00
|
|
|
$this->sql->fields = $fields;
|
|
|
|
$this->sql->from = $from;
|
|
|
|
$this->sql->where = $where;
|
2008-06-09 10:00:35 +00:00
|
|
|
$this->sql->params = $params;
|
2008-06-06 12:03:09 +00:00
|
|
|
}
|
2008-06-09 10:00:35 +00:00
|
|
|
|
2008-06-06 12:03:09 +00:00
|
|
|
/**
|
|
|
|
* Query the db. Store results in the table object for use by build_table.
|
2008-06-09 10:00:35 +00:00
|
|
|
*
|
2011-02-16 13:06:29 +00:00
|
|
|
* @param int $pagesize size of page for paginated displayed table.
|
|
|
|
* @param bool $useinitialsbar do you want to use the initials bar. Bar
|
2008-06-06 12:03:09 +00:00
|
|
|
* will only be used if there is a fullname column defined for the table.
|
|
|
|
*/
|
2011-02-16 10:54:29 +00:00
|
|
|
function query_db($pagesize, $useinitialsbar=true) {
|
2008-06-09 10:00:35 +00:00
|
|
|
global $DB;
|
2008-06-06 12:03:09 +00:00
|
|
|
if (!$this->is_downloading()) {
|
2011-02-16 10:54:29 +00:00
|
|
|
if ($this->countsql === NULL) {
|
2008-06-06 12:03:09 +00:00
|
|
|
$this->countsql = 'SELECT COUNT(1) FROM '.$this->sql->from.' WHERE '.$this->sql->where;
|
2011-03-30 20:25:43 +01:00
|
|
|
$this->countparams = $this->sql->params;
|
2008-06-06 12:03:09 +00:00
|
|
|
}
|
2011-05-01 17:36:19 +01:00
|
|
|
$grandtotal = $DB->count_records_sql($this->countsql, $this->countparams);
|
2008-06-06 12:03:09 +00:00
|
|
|
if ($useinitialsbar && !$this->is_downloading()) {
|
2011-05-01 17:36:19 +01:00
|
|
|
$this->initialbars($grandtotal > $pagesize);
|
2008-06-06 12:03:09 +00:00
|
|
|
}
|
2008-06-09 10:00:35 +00:00
|
|
|
|
2010-09-04 13:02:49 +00:00
|
|
|
list($wsql, $wparams) = $this->get_sql_where();
|
|
|
|
if ($wsql) {
|
|
|
|
$this->countsql .= ' AND '.$wsql;
|
2010-09-18 11:20:21 +00:00
|
|
|
$this->countparams = array_merge($this->countparams, $wparams);
|
2010-09-04 13:02:49 +00:00
|
|
|
|
|
|
|
$this->sql->where .= ' AND '.$wsql;
|
|
|
|
$this->sql->params = array_merge($this->sql->params, $wparams);
|
|
|
|
|
2008-07-20 12:21:59 +00:00
|
|
|
$total = $DB->count_records_sql($this->countsql, $this->countparams);
|
|
|
|
} else {
|
2011-05-01 17:36:19 +01:00
|
|
|
$total = $grandtotal;
|
2008-06-06 12:03:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->pagesize($pagesize, $total);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the attempts
|
|
|
|
$sort = $this->get_sql_sort();
|
MDL-26425 tablelib better validation of the sort code, also some cleanup.
You may think that the extra validation is unnecessary, since the sort fields are already validated when the URL parameters are parsed, however that overlooks an important point. There may be other options that affect which columns are in the SQL, for example the quiz show individual question grades setting. These other options can cause a column that was in the table, and being sorted on, to disappear. Therefore, it is necessary to re-validate the sort columns when they are used, to make sure they are still present, otherwise you can get ORDER BY sql that refers to non-existant columns, which then causes DB errors.
2011-02-16 11:42:33 +00:00
|
|
|
if ($sort) {
|
|
|
|
$sort = "ORDER BY $sort";
|
|
|
|
}
|
|
|
|
$sql = "SELECT
|
|
|
|
{$this->sql->fields}
|
|
|
|
FROM {$this->sql->from}
|
|
|
|
WHERE {$this->sql->where}
|
|
|
|
{$sort}";
|
|
|
|
|
2008-06-06 12:03:09 +00:00
|
|
|
if (!$this->is_downloading()) {
|
2008-06-09 10:00:35 +00:00
|
|
|
$this->rawdata = $DB->get_records_sql($sql, $this->sql->params, $this->get_page_start(), $this->get_page_size());
|
2008-06-06 12:03:09 +00:00
|
|
|
} else {
|
2008-06-09 10:00:35 +00:00
|
|
|
$this->rawdata = $DB->get_records_sql($sql, $this->sql->params);
|
2008-06-06 12:03:09 +00:00
|
|
|
}
|
|
|
|
}
|
2008-06-09 10:00:35 +00:00
|
|
|
|
2008-06-06 12:03:09 +00:00
|
|
|
/**
|
|
|
|
* Convenience method to call a number of methods for you to display the
|
|
|
|
* table.
|
|
|
|
*/
|
2011-02-16 10:54:29 +00:00
|
|
|
function out($pagesize, $useinitialsbar, $downloadhelpbutton='') {
|
2008-06-09 10:00:35 +00:00
|
|
|
global $DB;
|
2011-02-16 10:54:29 +00:00
|
|
|
if (!$this->columns) {
|
2008-06-09 10:00:35 +00:00
|
|
|
$onerow = $DB->get_record_sql("SELECT {$this->sql->fields} FROM {$this->sql->from} WHERE {$this->sql->where}", $this->sql->params);
|
2008-06-06 12:03:09 +00:00
|
|
|
//if columns is not set then define columns as the keys of the rows returned
|
|
|
|
//from the db.
|
|
|
|
$this->define_columns(array_keys((array)$onerow));
|
|
|
|
$this->define_headers(array_keys((array)$onerow));
|
|
|
|
}
|
|
|
|
$this->setup();
|
|
|
|
$this->query_db($pagesize, $useinitialsbar);
|
|
|
|
$this->build_table();
|
|
|
|
$this->finish_output();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-16 10:54:29 +00:00
|
|
|
|
2009-05-26 05:18:52 +00:00
|
|
|
/**
|
|
|
|
* @package moodlecore
|
|
|
|
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
2011-02-16 10:54:29 +00:00
|
|
|
class table_default_export_format_parent {
|
2008-06-06 12:03:09 +00:00
|
|
|
/**
|
|
|
|
* @var flexible_table or child class reference pointing to table class
|
|
|
|
* object from which to export data.
|
|
|
|
*/
|
|
|
|
var $table;
|
2009-11-01 11:31:16 +00:00
|
|
|
|
2008-07-30 09:02:44 +00:00
|
|
|
/**
|
2011-02-16 13:06:29 +00:00
|
|
|
* @var bool output started. Keeps track of whether any output has been
|
2008-07-30 09:02:44 +00:00
|
|
|
* started yet.
|
|
|
|
*/
|
|
|
|
var $documentstarted = false;
|
2015-11-26 10:49:58 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param flexible_table $table
|
|
|
|
*/
|
|
|
|
public function __construct(&$table) {
|
2008-06-06 12:03:09 +00:00
|
|
|
$this->table =& $table;
|
2015-11-26 10:49:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Old syntax of class constructor. Deprecated in PHP7.
|
|
|
|
*
|
|
|
|
* @deprecated since Moodle 3.1
|
|
|
|
*/
|
|
|
|
public function table_default_export_format_parent(&$table) {
|
|
|
|
debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
|
|
|
|
self::__construct($table);
|
2008-06-06 12:03:09 +00:00
|
|
|
}
|
2009-11-01 11:31:16 +00:00
|
|
|
|
2011-02-16 10:54:29 +00:00
|
|
|
function set_table(&$table) {
|
2008-07-30 09:02:44 +00:00
|
|
|
$this->table =& $table;
|
|
|
|
}
|
2008-06-06 12:03:09 +00:00
|
|
|
|
|
|
|
function add_data($row) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-02-16 10:54:29 +00:00
|
|
|
|
2008-06-06 12:03:09 +00:00
|
|
|
function add_seperator() {
|
|
|
|
return false;
|
|
|
|
}
|
2011-02-16 10:54:29 +00:00
|
|
|
|
|
|
|
function document_started() {
|
2008-07-30 09:02:44 +00:00
|
|
|
return $this->documentstarted;
|
2008-06-06 12:03:09 +00:00
|
|
|
}
|
2008-10-14 15:00:26 +00:00
|
|
|
/**
|
|
|
|
* Given text in a variety of format codings, this function returns
|
|
|
|
* the text as safe HTML or as plain text dependent on what is appropriate
|
|
|
|
* for the download format. The default removes all tags.
|
|
|
|
*/
|
2011-02-16 10:54:29 +00:00
|
|
|
function format_text($text, $format=FORMAT_MOODLE, $options=NULL, $courseid=NULL) {
|
2008-10-14 15:00:26 +00:00
|
|
|
//use some whitespace to indicate where there was some line spacing.
|
|
|
|
$text = str_replace(array('</p>', "\n", "\r"), ' ', $text);
|
|
|
|
return strip_tags($text);
|
|
|
|
}
|
2005-03-08 11:14:15 +00:00
|
|
|
}
|
|
|
|
|
2011-02-16 10:54:29 +00:00
|
|
|
|
2009-05-26 05:18:52 +00:00
|
|
|
/**
|
|
|
|
* @package moodlecore
|
|
|
|
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
2011-02-16 10:54:29 +00:00
|
|
|
class table_spreadsheet_export_format_parent extends table_default_export_format_parent {
|
2013-07-29 13:43:54 +08:00
|
|
|
var $currentrow;
|
2008-06-06 12:03:09 +00:00
|
|
|
var $workbook;
|
|
|
|
var $worksheet;
|
|
|
|
/**
|
|
|
|
* @var object format object - format for normal table cells
|
|
|
|
*/
|
|
|
|
var $formatnormal;
|
|
|
|
/**
|
|
|
|
* @var object format object - format for header table cells
|
|
|
|
*/
|
|
|
|
var $formatheaders;
|
|
|
|
|
2008-06-09 10:00:35 +00:00
|
|
|
/**
|
2008-06-06 12:03:09 +00:00
|
|
|
* should be overriden in child class.
|
|
|
|
*/
|
|
|
|
var $fileextension;
|
2008-06-09 10:00:35 +00:00
|
|
|
|
2008-06-06 12:03:09 +00:00
|
|
|
/**
|
|
|
|
* This method will be overridden in the child class.
|
|
|
|
*/
|
2011-02-16 10:54:29 +00:00
|
|
|
function define_workbook() {
|
2008-06-06 12:03:09 +00:00
|
|
|
}
|
2011-02-16 10:54:29 +00:00
|
|
|
|
|
|
|
function start_document($filename) {
|
2008-07-30 09:02:44 +00:00
|
|
|
$filename = $filename.'.'.$this->fileextension;
|
2008-06-06 12:03:09 +00:00
|
|
|
$this->define_workbook();
|
|
|
|
// format types
|
2014-01-28 16:43:47 +09:00
|
|
|
$this->formatnormal = $this->workbook->add_format();
|
2008-06-06 12:03:09 +00:00
|
|
|
$this->formatnormal->set_bold(0);
|
2014-01-28 16:43:47 +09:00
|
|
|
$this->formatheaders = $this->workbook->add_format();
|
2008-06-06 12:03:09 +00:00
|
|
|
$this->formatheaders->set_bold(1);
|
|
|
|
$this->formatheaders->set_align('center');
|
|
|
|
// Sending HTTP headers
|
2008-07-30 09:02:44 +00:00
|
|
|
$this->workbook->send($filename);
|
|
|
|
$this->documentstarted = true;
|
|
|
|
}
|
2011-02-16 10:54:29 +00:00
|
|
|
|
|
|
|
function start_table($sheettitle) {
|
2012-11-27 16:36:35 +00:00
|
|
|
$this->worksheet = $this->workbook->add_worksheet($sheettitle);
|
2013-07-29 13:43:54 +08:00
|
|
|
$this->currentrow=0;
|
2008-06-06 12:03:09 +00:00
|
|
|
}
|
2011-02-16 10:54:29 +00:00
|
|
|
|
|
|
|
function output_headers($headers) {
|
2008-06-06 12:03:09 +00:00
|
|
|
$colnum = 0;
|
|
|
|
foreach ($headers as $item) {
|
2013-07-29 13:43:54 +08:00
|
|
|
$this->worksheet->write($this->currentrow,$colnum,$item,$this->formatheaders);
|
2008-06-06 12:03:09 +00:00
|
|
|
$colnum++;
|
|
|
|
}
|
2013-07-29 13:43:54 +08:00
|
|
|
$this->currentrow++;
|
2008-06-06 12:03:09 +00:00
|
|
|
}
|
2011-02-16 10:54:29 +00:00
|
|
|
|
|
|
|
function add_data($row) {
|
2008-06-06 12:03:09 +00:00
|
|
|
$colnum = 0;
|
2011-02-16 10:54:29 +00:00
|
|
|
foreach ($row as $item) {
|
2013-07-29 13:43:54 +08:00
|
|
|
$this->worksheet->write($this->currentrow,$colnum,$item,$this->formatnormal);
|
2008-06-06 12:03:09 +00:00
|
|
|
$colnum++;
|
|
|
|
}
|
2013-07-29 13:43:54 +08:00
|
|
|
$this->currentrow++;
|
2008-06-06 12:03:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
2011-02-16 10:54:29 +00:00
|
|
|
|
2008-06-06 12:03:09 +00:00
|
|
|
function add_seperator() {
|
2013-07-29 13:43:54 +08:00
|
|
|
$this->currentrow++;
|
2008-06-06 12:03:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
2008-07-30 09:02:44 +00:00
|
|
|
|
2011-02-16 10:54:29 +00:00
|
|
|
function finish_table() {
|
2008-07-30 09:02:44 +00:00
|
|
|
}
|
2011-02-16 10:54:29 +00:00
|
|
|
|
|
|
|
function finish_document() {
|
2008-06-06 12:03:09 +00:00
|
|
|
$this->workbook->close();
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-16 10:54:29 +00:00
|
|
|
|
2009-05-26 05:18:52 +00:00
|
|
|
/**
|
|
|
|
* @package moodlecore
|
|
|
|
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
2011-02-16 10:54:29 +00:00
|
|
|
class table_excel_export_format extends table_spreadsheet_export_format_parent {
|
2008-06-06 12:03:09 +00:00
|
|
|
var $fileextension = 'xls';
|
|
|
|
|
2011-02-16 10:54:29 +00:00
|
|
|
function define_workbook() {
|
2008-06-06 12:03:09 +00:00
|
|
|
global $CFG;
|
|
|
|
require_once("$CFG->libdir/excellib.class.php");
|
|
|
|
// Creating a workbook
|
|
|
|
$this->workbook = new MoodleExcelWorkbook("-");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-02-16 10:54:29 +00:00
|
|
|
|
2009-05-26 05:18:52 +00:00
|
|
|
/**
|
|
|
|
* @package moodlecore
|
|
|
|
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
2011-02-16 10:54:29 +00:00
|
|
|
class table_ods_export_format extends table_spreadsheet_export_format_parent {
|
2008-06-06 12:03:09 +00:00
|
|
|
var $fileextension = 'ods';
|
2011-02-16 10:54:29 +00:00
|
|
|
function define_workbook() {
|
2008-06-06 12:03:09 +00:00
|
|
|
global $CFG;
|
|
|
|
require_once("$CFG->libdir/odslib.class.php");
|
|
|
|
// Creating a workbook
|
|
|
|
$this->workbook = new MoodleODSWorkbook("-");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-16 10:54:29 +00:00
|
|
|
|
2009-05-26 05:18:52 +00:00
|
|
|
/**
|
|
|
|
* @package moodlecore
|
|
|
|
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
2010-09-22 14:14:10 +00:00
|
|
|
class table_text_export_format_parent extends table_default_export_format_parent {
|
2012-07-27 09:37:33 +08:00
|
|
|
protected $seperator = "tab";
|
2010-09-22 14:14:10 +00:00
|
|
|
protected $mimetype = 'text/tab-separated-values';
|
|
|
|
protected $ext = '.txt';
|
2012-07-27 09:37:33 +08:00
|
|
|
protected $myexporter;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->myexporter = new csv_export_writer($this->seperator, '"', $this->mimetype);
|
|
|
|
}
|
2010-09-22 14:14:10 +00:00
|
|
|
|
|
|
|
public function start_document($filename) {
|
2012-07-27 09:37:33 +08:00
|
|
|
$this->filename = $filename;
|
2008-07-30 09:02:44 +00:00
|
|
|
$this->documentstarted = true;
|
2012-07-27 09:37:33 +08:00
|
|
|
$this->myexporter->set_filename($filename, $this->ext);
|
2008-07-30 09:02:44 +00:00
|
|
|
}
|
2010-09-22 14:14:10 +00:00
|
|
|
|
|
|
|
public function start_table($sheettitle) {
|
2008-07-30 09:02:44 +00:00
|
|
|
//nothing to do here
|
2008-06-06 12:03:09 +00:00
|
|
|
}
|
2010-09-22 14:14:10 +00:00
|
|
|
|
|
|
|
public function output_headers($headers) {
|
2012-07-27 09:37:33 +08:00
|
|
|
$this->myexporter->add_data($headers);
|
2008-06-06 12:03:09 +00:00
|
|
|
}
|
2010-09-22 14:14:10 +00:00
|
|
|
|
|
|
|
public function add_data($row) {
|
2012-07-27 09:37:33 +08:00
|
|
|
$this->myexporter->add_data($row);
|
2008-06-06 12:03:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
2010-09-22 14:14:10 +00:00
|
|
|
|
|
|
|
public function finish_table() {
|
2012-07-27 09:37:33 +08:00
|
|
|
//nothing to do here
|
2008-07-30 09:02:44 +00:00
|
|
|
}
|
2010-09-22 14:14:10 +00:00
|
|
|
|
|
|
|
public function finish_document() {
|
2012-07-27 09:37:33 +08:00
|
|
|
$this->myexporter->download_file();
|
2008-06-06 12:03:09 +00:00
|
|
|
exit;
|
|
|
|
}
|
2010-09-22 14:14:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Format a row of data.
|
|
|
|
* @param array $data
|
|
|
|
*/
|
|
|
|
protected function format_row($data) {
|
|
|
|
$escapeddata = array();
|
|
|
|
foreach ($data as $value) {
|
|
|
|
$escapeddata[] = '"' . str_replace('"', '""', $value) . '"';
|
|
|
|
}
|
|
|
|
return implode($this->seperator, $escapeddata) . "\n";
|
|
|
|
}
|
2008-06-06 12:03:09 +00:00
|
|
|
}
|
|
|
|
|
2011-02-16 10:54:29 +00:00
|
|
|
|
2009-05-26 05:18:52 +00:00
|
|
|
/**
|
|
|
|
* @package moodlecore
|
|
|
|
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
2011-02-16 10:54:29 +00:00
|
|
|
class table_tsv_export_format extends table_text_export_format_parent {
|
2012-07-27 09:37:33 +08:00
|
|
|
protected $seperator = "tab";
|
2010-09-22 14:14:10 +00:00
|
|
|
protected $mimetype = 'text/tab-separated-values';
|
|
|
|
protected $ext = '.txt';
|
2008-06-06 12:03:09 +00:00
|
|
|
}
|
|
|
|
|
2012-07-27 09:37:33 +08:00
|
|
|
require_once($CFG->libdir . '/csvlib.class.php');
|
2009-05-26 05:18:52 +00:00
|
|
|
/**
|
|
|
|
* @package moodlecore
|
|
|
|
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
2011-02-16 10:54:29 +00:00
|
|
|
class table_csv_export_format extends table_text_export_format_parent {
|
2012-07-27 09:37:33 +08:00
|
|
|
protected $seperator = "comma";
|
2010-09-22 14:14:10 +00:00
|
|
|
protected $mimetype = 'text/csv';
|
|
|
|
protected $ext = '.csv';
|
2008-06-06 12:03:09 +00:00
|
|
|
}
|
|
|
|
|
2009-05-26 05:18:52 +00:00
|
|
|
/**
|
|
|
|
* @package moodlecore
|
|
|
|
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
2011-02-16 10:54:29 +00:00
|
|
|
class table_xhtml_export_format extends table_default_export_format_parent {
|
|
|
|
function start_document($filename) {
|
2008-06-06 12:03:09 +00:00
|
|
|
header("Content-Type: application/download\n");
|
2008-07-30 09:02:44 +00:00
|
|
|
header("Content-Disposition: attachment; filename=\"$filename.html\"");
|
2008-06-06 12:03:09 +00:00
|
|
|
header("Expires: 0");
|
|
|
|
header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
|
|
|
|
header("Pragma: public");
|
|
|
|
//html headers
|
|
|
|
echo <<<EOF
|
|
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<!DOCTYPE html
|
|
|
|
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
2008-06-09 10:00:35 +00:00
|
|
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
2008-06-06 12:03:09 +00:00
|
|
|
|
2008-06-09 10:00:35 +00:00
|
|
|
<html xmlns="http://www.w3.org/1999/xhtml"
|
2008-06-06 12:03:09 +00:00
|
|
|
xml:lang="en" lang="en">
|
2008-09-11 12:48:08 +00:00
|
|
|
<head>
|
2013-07-18 18:55:56 +01:00
|
|
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
2008-06-06 12:03:09 +00:00
|
|
|
<style type="text/css">/*<![CDATA[*/
|
|
|
|
|
|
|
|
.flexible th {
|
|
|
|
white-space:normal;
|
|
|
|
}
|
|
|
|
th.header, td.header, div.header {
|
|
|
|
border-color:#DDDDDD;
|
|
|
|
background-color:lightGrey;
|
|
|
|
}
|
|
|
|
.flexible th {
|
|
|
|
white-space:nowrap;
|
|
|
|
}
|
|
|
|
th {
|
|
|
|
font-weight:bold;
|
|
|
|
}
|
|
|
|
|
|
|
|
.generaltable {
|
|
|
|
border-style:solid;
|
|
|
|
}
|
|
|
|
.generalbox {
|
|
|
|
border-style:solid;
|
|
|
|
}
|
|
|
|
body, table, td, th {
|
|
|
|
font-family:Arial,Verdana,Helvetica,sans-serif;
|
|
|
|
font-size:100%;
|
|
|
|
}
|
|
|
|
td {
|
|
|
|
border-style:solid;
|
|
|
|
border-width:1pt;
|
|
|
|
}
|
|
|
|
table {
|
|
|
|
border-collapse:collapse;
|
|
|
|
border-spacing:0pt;
|
|
|
|
width:80%;
|
|
|
|
margin:auto;
|
|
|
|
}
|
|
|
|
|
2011-02-16 10:54:29 +00:00
|
|
|
h1, h2 {
|
2008-06-06 12:03:09 +00:00
|
|
|
text-align:center;
|
|
|
|
}
|
|
|
|
.bold {
|
|
|
|
font-weight:bold;
|
|
|
|
}
|
2008-09-11 12:48:08 +00:00
|
|
|
.mdl-align {
|
|
|
|
text-align:center;
|
|
|
|
}
|
2008-06-06 12:03:09 +00:00
|
|
|
/*]]>*/</style>
|
2008-09-11 12:48:08 +00:00
|
|
|
<title>$filename</title>
|
|
|
|
</head>
|
2008-06-06 12:03:09 +00:00
|
|
|
<body>
|
|
|
|
EOF;
|
2008-07-30 09:02:44 +00:00
|
|
|
$this->documentstarted = true;
|
|
|
|
}
|
2011-02-16 10:54:29 +00:00
|
|
|
|
|
|
|
function start_table($sheettitle) {
|
2008-07-30 09:02:44 +00:00
|
|
|
$this->table->sortable(false);
|
|
|
|
$this->table->collapsible(false);
|
|
|
|
echo "<h2>{$sheettitle}</h2>";
|
2008-06-06 12:03:09 +00:00
|
|
|
$this->table->start_html();
|
|
|
|
}
|
2008-07-30 09:02:44 +00:00
|
|
|
|
2011-02-16 10:54:29 +00:00
|
|
|
function output_headers($headers) {
|
2008-06-06 12:03:09 +00:00
|
|
|
$this->table->print_headers();
|
2012-08-21 13:50:38 +08:00
|
|
|
echo html_writer::start_tag('tbody');
|
2008-06-06 12:03:09 +00:00
|
|
|
}
|
2011-02-16 10:54:29 +00:00
|
|
|
|
|
|
|
function add_data($row) {
|
2008-06-06 12:03:09 +00:00
|
|
|
$this->table->print_row($row);
|
|
|
|
return true;
|
|
|
|
}
|
2011-02-16 10:54:29 +00:00
|
|
|
|
2008-06-06 12:03:09 +00:00
|
|
|
function add_seperator() {
|
|
|
|
$this->table->print_row(NULL);
|
|
|
|
return true;
|
|
|
|
}
|
2011-02-16 10:54:29 +00:00
|
|
|
|
|
|
|
function finish_table() {
|
2008-06-06 12:03:09 +00:00
|
|
|
$this->table->finish_html();
|
2008-07-30 09:02:44 +00:00
|
|
|
}
|
2011-02-16 10:54:29 +00:00
|
|
|
|
|
|
|
function finish_document() {
|
2008-09-11 12:48:08 +00:00
|
|
|
echo "</body>\n</html>";
|
2008-06-06 12:03:09 +00:00
|
|
|
exit;
|
|
|
|
}
|
2011-02-16 10:54:29 +00:00
|
|
|
|
|
|
|
function format_text($text, $format=FORMAT_MOODLE, $options=NULL, $courseid=NULL) {
|
|
|
|
if (is_null($options)) {
|
2008-10-14 15:00:26 +00:00
|
|
|
$options = new stdClass;
|
|
|
|
}
|
|
|
|
//some sensible defaults
|
2011-02-16 10:54:29 +00:00
|
|
|
if (!isset($options->para)) {
|
2008-10-14 15:00:26 +00:00
|
|
|
$options->para = false;
|
|
|
|
}
|
2011-02-16 10:54:29 +00:00
|
|
|
if (!isset($options->newlines)) {
|
2008-10-14 15:00:26 +00:00
|
|
|
$options->newlines = false;
|
|
|
|
}
|
|
|
|
if (!isset($options->smiley)) {
|
|
|
|
$options->smiley = false;
|
|
|
|
}
|
|
|
|
if (!isset($options->filter)) {
|
|
|
|
$options->filter = false;
|
|
|
|
}
|
|
|
|
return format_text($text, $format, $options);
|
|
|
|
}
|
2008-06-06 12:03:09 +00:00
|
|
|
}
|