Files
moodle/lib/table/classes/sql_table.php
T
2024-06-26 23:00:33 +08:00

216 lines
7.3 KiB
PHP

<?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/>.
namespace core_table;
use flexible_table;
use moodle_recordset;
use stdClass;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->libdir}/tablelib.php");
/**
* A table whose data is provided by SQL queries.
*
* @package moodlecore
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class sql_table extends flexible_table {
public $countsql = NULL;
public $countparams = NULL;
/**
* @var object sql for querying db. Has fields 'fields', 'from', 'where', 'params'.
*/
public $sql = NULL;
/**
* @var array|\Traversable Data fetched from the db.
*/
public $rawdata = NULL;
/**
* @var bool Overriding default for this.
*/
public $is_sortable = true;
/**
* @var bool Overriding default for this.
*/
public $is_collapsible = true;
/**
* @param string $uniqueid a string identifying this table.Used as a key in
* session vars.
*/
function __construct($uniqueid) {
parent::__construct($uniqueid);
// some sensible defaults
$this->set_attribute('class', 'generaltable generalbox');
}
/**
* Build the table from the fetched data.
*
* 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.
*
* After calling this function, don't forget to call close_recordset.
*/
public function build_table() {
if (!$this->rawdata) {
return;
}
foreach ($this->rawdata as $row) {
$formattedrow = $this->format_row($row);
$this->add_data_keyed($formattedrow, $this->get_row_class($row));
}
}
/**
* Closes recordset (for use after building the table).
*/
public function close_recordset() {
if ($this->rawdata && ($this->rawdata instanceof \core\dml\recordset_walk ||
$this->rawdata instanceof moodle_recordset)) {
$this->rawdata->close();
$this->rawdata = null;
}
}
/**
* 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 '';
}
/**
* 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.
*
* 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.
*/
function set_count_sql($sql, array $params = NULL) {
$this->countsql = $sql;
$this->countparams = $params;
}
/**
* 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.
*/
function set_sql($fields, $from, $where, array $params = array()) {
$this->sql = new stdClass();
$this->sql->fields = $fields;
$this->sql->from = $from;
$this->sql->where = $where;
$this->sql->params = $params;
}
/**
* Query the db. Store results in the table object for use by build_table.
*
* @param int $pagesize size of page for paginated displayed table.
* @param bool $useinitialsbar do you want to use the initials bar. Bar
* will only be used if there is a fullname column defined for the table.
*/
function query_db($pagesize, $useinitialsbar=true) {
global $DB;
if (!$this->is_downloading()) {
if ($this->countsql === NULL) {
$this->countsql = 'SELECT COUNT(1) FROM '.$this->sql->from.' WHERE '.$this->sql->where;
$this->countparams = $this->sql->params;
}
$grandtotal = $DB->count_records_sql($this->countsql, $this->countparams);
if ($useinitialsbar && !$this->is_downloading()) {
$this->initialbars(true);
}
list($wsql, $wparams) = $this->get_sql_where();
if ($wsql) {
$this->countsql .= ' AND '.$wsql;
$this->countparams = array_merge($this->countparams, $wparams);
$this->sql->where .= ' AND '.$wsql;
$this->sql->params = array_merge($this->sql->params, $wparams);
$total = $DB->count_records_sql($this->countsql, $this->countparams);
} else {
$total = $grandtotal;
}
$this->pagesize($pagesize, $total);
}
// Fetch the attempts
$sort = $this->get_sql_sort();
if ($sort) {
$sort = "ORDER BY $sort";
}
$sql = "SELECT
{$this->sql->fields}
FROM {$this->sql->from}
WHERE {$this->sql->where}
{$sort}";
if (!$this->is_downloading()) {
$this->rawdata = $DB->get_records_sql($sql, $this->sql->params, $this->get_page_start(), $this->get_page_size());
} else {
$this->rawdata = $DB->get_records_sql($sql, $this->sql->params);
}
}
/**
* Convenience method to call a number of methods for you to display the
* table.
*/
function out($pagesize, $useinitialsbar, $downloadhelpbutton='') {
global $DB;
if (!$this->columns) {
$onerow = $DB->get_record_sql("SELECT {$this->sql->fields} FROM {$this->sql->from} WHERE {$this->sql->where}",
$this->sql->params, IGNORE_MULTIPLE);
//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->pagesize = $pagesize;
$this->setup();
$this->query_db($pagesize, $useinitialsbar);
$this->build_table();
$this->close_recordset();
$this->finish_output();
}
}
// Alias this class to the old name.
// This file will be autoloaded by the legacyclasses autoload system.
// In future all uses of this class will be corrected and the legacy references will be removed.
class_alias(sql_table::class, \table_sql::class);