PageRenderTime 55ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/tablelib.php

https://github.com/mylescarrick/moodle
PHP | 1732 lines | 1058 code | 185 blank | 489 comment | 192 complexity | 3cc7f16ae1888a6718c6fad1cdc4dd51 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * @package core
  18. * @subpackage lib
  19. * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. defined('MOODLE_INTERNAL') || die();
  23. /** TABLE_VAR_SORT = 1 */
  24. define('TABLE_VAR_SORT', 1);
  25. /** TABLE_VAR_HIDE = 2 */
  26. define('TABLE_VAR_HIDE', 2);
  27. /** TABLE_VAR_SHOW = 3 */
  28. define('TABLE_VAR_SHOW', 3);
  29. /** TABLE_VAR_IFIRST = 4 */
  30. define('TABLE_VAR_IFIRST', 4);
  31. /** TABLE_VAR_ILAST = 5 */
  32. define('TABLE_VAR_ILAST', 5);
  33. /** TABLE_VAR_PAGE = 6 */
  34. define('TABLE_VAR_PAGE', 6);
  35. /** TABLE_P_TOP = 1 */
  36. define('TABLE_P_TOP', 1);
  37. /** TABLE_P_BOTTOM = 2 */
  38. define('TABLE_P_BOTTOM', 2);
  39. /**
  40. * @package moodlecore
  41. * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
  42. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43. */
  44. class flexible_table {
  45. var $uniqueid = NULL;
  46. var $attributes = array();
  47. var $headers = array();
  48. var $columns = array();
  49. var $column_style = array();
  50. var $column_class = array();
  51. var $column_suppress = array();
  52. var $column_nosort = array('userpic');
  53. var $setup = false;
  54. var $sess = NULL;
  55. var $baseurl = NULL;
  56. var $request = array();
  57. var $is_collapsible = false;
  58. var $is_sortable = false;
  59. var $use_pages = false;
  60. var $use_initials = false;
  61. var $maxsortkeys = 2;
  62. var $pagesize = 30;
  63. var $currpage = 0;
  64. var $totalrows = 0;
  65. var $sort_default_column = NULL;
  66. var $sort_default_order = SORT_ASC;
  67. /**
  68. * Array of positions in which to display download controls.
  69. */
  70. var $showdownloadbuttonsat= array(TABLE_P_TOP);
  71. /**
  72. * @var string Key of field returned by db query that is the id field of the
  73. * user table or equivalent.
  74. */
  75. public $useridfield = 'id';
  76. /**
  77. * @var string which download plugin to use. Default '' means none - print
  78. * html table with paging. Property set by is_downloading which typically
  79. * passes in cleaned data from $
  80. */
  81. var $download = '';
  82. /**
  83. * @var boolean whether data is downloadable from table. Determines whether
  84. * to display download buttons. Set by method downloadable().
  85. */
  86. var $downloadable = false;
  87. /**
  88. * @var string which download plugin to use. Default '' means none - print
  89. * html table with paging.
  90. */
  91. var $defaultdownloadformat = 'csv';
  92. /**
  93. * @var boolean Has start output been called yet?
  94. */
  95. var $started_output = false;
  96. var $exportclass = null;
  97. /**
  98. * Constructor
  99. * @param int $uniqueid
  100. * @todo Document properly
  101. */
  102. function flexible_table($uniqueid) {
  103. $this->uniqueid = $uniqueid;
  104. $this->request = array(
  105. TABLE_VAR_SORT => 'tsort',
  106. TABLE_VAR_HIDE => 'thide',
  107. TABLE_VAR_SHOW => 'tshow',
  108. TABLE_VAR_IFIRST => 'tifirst',
  109. TABLE_VAR_ILAST => 'tilast',
  110. TABLE_VAR_PAGE => 'page'
  111. );
  112. }
  113. /**
  114. * Call this to pass the download type. Use :
  115. * $download = optional_param('download', '', PARAM_ALPHA);
  116. * To get the download type. We assume that if you call this function with
  117. * params that this table's data is downloadable, so we call is_downloadable
  118. * for you (even if the param is '', which means no download this time.
  119. * Also you can call this method with no params to get the current set
  120. * download type.
  121. * @param string $download download type. One of csv, tsv, xhtml, ods, etc
  122. * @param string $filename filename for downloads without file extension.
  123. * @param string $sheettitle title for downloaded data.
  124. * @return string download type. One of csv, tsv, xhtml, ods, etc
  125. */
  126. function is_downloading($download = null, $filename='', $sheettitle=''){
  127. if ($download!==null){
  128. $this->sheettitle = $sheettitle;
  129. $this->is_downloadable(true);
  130. $this->download = $download;
  131. $this->filename = clean_filename($filename);
  132. $this->export_class_instance();
  133. }
  134. return $this->download;
  135. }
  136. function export_class_instance(&$exportclass=null){
  137. if (!is_null($exportclass)){
  138. $this->started_output = true;
  139. $this->exportclass =& $exportclass;
  140. $this->exportclass->table =& $this;
  141. } elseif (is_null($this->exportclass) && !empty($this->download)){
  142. $classname = 'table_'.$this->download.'_export_format';
  143. $this->exportclass = new $classname($this);
  144. if (!$this->exportclass->document_started()){
  145. $this->exportclass->start_document($this->filename);
  146. }
  147. }
  148. return $this->exportclass;
  149. }
  150. /**
  151. * Probably don't need to call this directly. Calling is_downloading with a
  152. * param automatically sets table as downloadable.
  153. *
  154. * @param boolean $downloadable optional param to set whether data from
  155. * table is downloadable. If ommitted this function can be used to get
  156. * current state of table.
  157. * @return boolean whether table data is set to be downloadable.
  158. */
  159. function is_downloadable($downloadable = null){
  160. if ($downloadable !== null){
  161. $this->downloadable = $downloadable;
  162. }
  163. return $this->downloadable;
  164. }
  165. /**
  166. * Where to show download buttons.
  167. * @param array $showat array of postions in which to show download buttons.
  168. * Containing TABLE_P_TOP and/or TABLE_P_BOTTOM
  169. */
  170. function show_download_buttons_at($showat){
  171. $this->showdownloadbuttonsat = $showat;
  172. }
  173. /**
  174. * Sets the is_sortable variable to the given boolean, sort_default_column to
  175. * the given string, and the sort_default_order to the given integer.
  176. * @param bool $bool
  177. * @param string $defaultcolumn
  178. * @param int $defaultorder
  179. * @return void
  180. */
  181. function sortable($bool, $defaultcolumn = NULL, $defaultorder = SORT_ASC) {
  182. $this->is_sortable = $bool;
  183. $this->sort_default_column = $defaultcolumn;
  184. $this->sort_default_order = $defaultorder;
  185. }
  186. /**
  187. * Do not sort using this column
  188. * @param string column name
  189. */
  190. function no_sorting($column) {
  191. $this->column_nosort[] = $column;
  192. }
  193. /**
  194. * Is the column sortable?
  195. * @param string column name, null means table
  196. * @return bool
  197. */
  198. function is_sortable($column=null) {
  199. if (empty($column)) {
  200. return $this->is_sortable;
  201. }
  202. if (!$this->is_sortable) {
  203. return false;
  204. }
  205. return !in_array($column, $this->column_nosort);
  206. }
  207. /**
  208. * Sets the is_collapsible variable to the given boolean.
  209. * @param bool $bool
  210. * @return void
  211. */
  212. function collapsible($bool) {
  213. $this->is_collapsible = $bool;
  214. }
  215. /**
  216. * Sets the use_pages variable to the given boolean.
  217. * @param bool $bool
  218. * @return void
  219. */
  220. function pageable($bool) {
  221. $this->use_pages = $bool;
  222. }
  223. /**
  224. * Sets the use_initials variable to the given boolean.
  225. * @param bool $bool
  226. * @return void
  227. */
  228. function initialbars($bool) {
  229. $this->use_initials = $bool;
  230. }
  231. /**
  232. * Sets the pagesize variable to the given integer, the totalrows variable
  233. * to the given integer, and the use_pages variable to true.
  234. * @param int $perpage
  235. * @param int $total
  236. * @return void
  237. */
  238. function pagesize($perpage, $total) {
  239. $this->pagesize = $perpage;
  240. $this->totalrows = $total;
  241. $this->use_pages = true;
  242. }
  243. /**
  244. * Assigns each given variable in the array to the corresponding index
  245. * in the request class variable.
  246. * @param array $variables
  247. * @return void
  248. */
  249. function set_control_variables($variables) {
  250. foreach($variables as $what => $variable) {
  251. if(isset($this->request[$what])) {
  252. $this->request[$what] = $variable;
  253. }
  254. }
  255. }
  256. /**
  257. * Gives the given $value to the $attribute index of $this->attributes.
  258. * @param string $attribute
  259. * @param mixed $value
  260. * @return void
  261. */
  262. function set_attribute($attribute, $value) {
  263. $this->attributes[$attribute] = $value;
  264. }
  265. /**
  266. * What this method does is set the column so that if the same data appears in
  267. * consecutive rows, then it is not repeated.
  268. *
  269. * For example, in the quiz overview report, the fullname column is set to be suppressed, so
  270. * that when one student has made multiple attempts, their name is only printed in the row
  271. * for their first attempt.
  272. * @param integer $column the index of a column.
  273. */
  274. function column_suppress($column) {
  275. if(isset($this->column_suppress[$column])) {
  276. $this->column_suppress[$column] = true;
  277. }
  278. }
  279. /**
  280. * Sets the given $column index to the given $classname in $this->column_class.
  281. * @param integer $column
  282. * @param string $classname
  283. * @return void
  284. */
  285. function column_class($column, $classname) {
  286. if(isset($this->column_class[$column])) {
  287. $this->column_class[$column] = ' '.$classname; // This space needed so that classnames don't run together in the HTML
  288. }
  289. }
  290. /**
  291. * Sets the given $column index and $property index to the given $value in $this->column_style.
  292. * @param integer $column
  293. * @param string $property
  294. * @param mixed $value
  295. * @return void
  296. */
  297. function column_style($column, $property, $value) {
  298. if(isset($this->column_style[$column])) {
  299. $this->column_style[$column][$property] = $value;
  300. }
  301. }
  302. /**
  303. * Sets all columns' $propertys to the given $value in $this->column_style.
  304. * @param integer $property
  305. * @param string $value
  306. * @return void
  307. */
  308. function column_style_all($property, $value) {
  309. foreach(array_keys($this->columns) as $column) {
  310. $this->column_style[$column][$property] = $value;
  311. }
  312. }
  313. /**
  314. * Sets $this->reseturl to the given $url, and $this->baseurl to the given $url plus ? or &amp;
  315. * @param string $url the url with params needed to call up this page
  316. */
  317. function define_baseurl($url) {
  318. $this->reseturl = $url;
  319. if(!strpos($url, '?')) {
  320. $this->baseurl = $url.'?';
  321. }
  322. else {
  323. $this->baseurl = $url.'&amp;';
  324. }
  325. }
  326. /**
  327. * @param array $columns an array of identifying names for columns. If
  328. * columns are sorted then column names must correspond to a field in sql.
  329. */
  330. function define_columns($columns) {
  331. $this->columns = array();
  332. $this->column_style = array();
  333. $this->column_class = array();
  334. $colnum = 0;
  335. foreach($columns as $column) {
  336. $this->columns[$column] = $colnum++;
  337. $this->column_style[$column] = array();
  338. $this->column_class[$column] = '';
  339. $this->column_suppress[$column] = false;
  340. }
  341. }
  342. /**
  343. * @param array $headers numerical keyed array of displayed string titles
  344. * for each column.
  345. */
  346. function define_headers($headers) {
  347. $this->headers = $headers;
  348. }
  349. /**
  350. * Must be called after table is defined. Use methods above first. Cannot
  351. * use functions below till after calling this method.
  352. * @return type?
  353. */
  354. function setup() {
  355. global $SESSION, $CFG;
  356. if(empty($this->columns) || empty($this->uniqueid)) {
  357. return false;
  358. }
  359. if (!isset($SESSION->flextable)) {
  360. $SESSION->flextable = array();
  361. }
  362. if(!isset($SESSION->flextable[$this->uniqueid])) {
  363. $SESSION->flextable[$this->uniqueid] = new stdClass;
  364. $SESSION->flextable[$this->uniqueid]->uniqueid = $this->uniqueid;
  365. $SESSION->flextable[$this->uniqueid]->collapse = array();
  366. $SESSION->flextable[$this->uniqueid]->sortby = array();
  367. $SESSION->flextable[$this->uniqueid]->i_first = '';
  368. $SESSION->flextable[$this->uniqueid]->i_last = '';
  369. }
  370. $this->sess = &$SESSION->flextable[$this->uniqueid];
  371. if(!empty($_GET[$this->request[TABLE_VAR_SHOW]]) && isset($this->columns[$_GET[$this->request[TABLE_VAR_SHOW]]])) {
  372. // Show this column
  373. $this->sess->collapse[$_GET[$this->request[TABLE_VAR_SHOW]]] = false;
  374. }
  375. else if(!empty($_GET[$this->request[TABLE_VAR_HIDE]]) && isset($this->columns[$_GET[$this->request[TABLE_VAR_HIDE]]])) {
  376. // Hide this column
  377. $this->sess->collapse[$_GET[$this->request[TABLE_VAR_HIDE]]] = true;
  378. if(array_key_exists($_GET[$this->request[TABLE_VAR_HIDE]], $this->sess->sortby)) {
  379. unset($this->sess->sortby[$_GET[$this->request[TABLE_VAR_HIDE]]]);
  380. }
  381. }
  382. // Now, update the column attributes for collapsed columns
  383. foreach(array_keys($this->columns) as $column) {
  384. if(!empty($this->sess->collapse[$column])) {
  385. $this->column_style[$column]['width'] = '10px';
  386. }
  387. }
  388. if(
  389. !empty($_GET[$this->request[TABLE_VAR_SORT]]) && $this->is_sortable($_GET[$this->request[TABLE_VAR_SORT]]) &&
  390. (isset($this->columns[$_GET[$this->request[TABLE_VAR_SORT]]]) ||
  391. (($_GET[$this->request[TABLE_VAR_SORT]] == 'firstname' || $_GET[$this->request[TABLE_VAR_SORT]] == 'lastname') && isset($this->columns['fullname']))
  392. ))
  393. {
  394. if(empty($this->sess->collapse[$_GET[$this->request[TABLE_VAR_SORT]]])) {
  395. if(array_key_exists($_GET[$this->request[TABLE_VAR_SORT]], $this->sess->sortby)) {
  396. // This key already exists somewhere. Change its sortorder and bring it to the top.
  397. $sortorder = $this->sess->sortby[$_GET[$this->request[TABLE_VAR_SORT]]] == SORT_ASC ? SORT_DESC : SORT_ASC;
  398. unset($this->sess->sortby[$_GET[$this->request[TABLE_VAR_SORT]]]);
  399. $this->sess->sortby = array_merge(array($_GET[$this->request[TABLE_VAR_SORT]] => $sortorder), $this->sess->sortby);
  400. }
  401. else {
  402. // Key doesn't exist, so just add it to the beginning of the array, ascending order
  403. $this->sess->sortby = array_merge(array($_GET[$this->request[TABLE_VAR_SORT]] => SORT_ASC), $this->sess->sortby);
  404. }
  405. // Finally, make sure that no more than $this->maxsortkeys are present into the array
  406. if(!empty($this->maxsortkeys) && ($sortkeys = count($this->sess->sortby)) > $this->maxsortkeys) {
  407. while($sortkeys-- > $this->maxsortkeys) {
  408. array_pop($this->sess->sortby);
  409. }
  410. }
  411. }
  412. }
  413. // If we didn't sort just now, then use the default sort order if one is defined and the column exists
  414. if(empty($this->sess->sortby) && !empty($this->sort_default_column)) {
  415. $this->sess->sortby = array ($this->sort_default_column => ($this->sort_default_order == SORT_DESC ? SORT_DESC : SORT_ASC));
  416. }
  417. if(isset($_GET[$this->request[TABLE_VAR_ILAST]])) {
  418. if(empty($_GET[$this->request[TABLE_VAR_ILAST]]) || is_numeric(strpos(get_string('alphabet', 'langconfig'), $_GET[$this->request[TABLE_VAR_ILAST]]))) {
  419. $this->sess->i_last = $_GET[$this->request[TABLE_VAR_ILAST]];
  420. }
  421. }
  422. if(isset($_GET[$this->request[TABLE_VAR_IFIRST]])) {
  423. if(empty($_GET[$this->request[TABLE_VAR_IFIRST]]) || is_numeric(strpos(get_string('alphabet', 'langconfig'), $_GET[$this->request[TABLE_VAR_IFIRST]]))) {
  424. $this->sess->i_first = $_GET[$this->request[TABLE_VAR_IFIRST]];
  425. }
  426. }
  427. if(empty($this->baseurl)) {
  428. $getcopy = $_GET;
  429. unset($getcopy[$this->request[TABLE_VAR_SHOW]]);
  430. unset($getcopy[$this->request[TABLE_VAR_HIDE]]);
  431. unset($getcopy[$this->request[TABLE_VAR_SORT]]);
  432. unset($getcopy[$this->request[TABLE_VAR_IFIRST]]);
  433. unset($getcopy[$this->request[TABLE_VAR_ILAST]]);
  434. unset($getcopy[$this->request[TABLE_VAR_PAGE]]);
  435. $strippedurl = strip_querystring(qualified_me());
  436. if(!empty($getcopy)) {
  437. $first = false;
  438. $querystring = '';
  439. foreach($getcopy as $var => $val) {
  440. if(!$first) {
  441. $first = true;
  442. $querystring .= '?'.$var.'='.$val;
  443. }
  444. else {
  445. $querystring .= '&amp;'.$var.'='.$val;
  446. }
  447. }
  448. $this->reseturl = $strippedurl.$querystring;
  449. $querystring .= '&amp;';
  450. }
  451. else {
  452. $this->reseturl = $strippedurl;
  453. $querystring = '?';
  454. }
  455. $this->baseurl = strip_querystring(qualified_me()) . $querystring;
  456. }
  457. // If it's "the first time" we 've been here, forget the previous initials filters
  458. if(qualified_me() == $this->reseturl) {
  459. $this->sess->i_first = '';
  460. $this->sess->i_last = '';
  461. }
  462. $this->currpage = optional_param($this->request[TABLE_VAR_PAGE], 0, PARAM_INT);
  463. $this->setup = true;
  464. /// Always introduce the "flexible" class for the table if not specified
  465. /// No attributes, add flexible class
  466. if (empty($this->attributes)) {
  467. $this->attributes['class'] = 'flexible';
  468. /// No classes, add flexible class
  469. } else if (!isset($this->attributes['class'])) {
  470. $this->attributes['class'] = 'flexible';
  471. /// No flexible class in passed classes, add flexible class
  472. } else if (!in_array('flexible', explode(' ', $this->attributes['class']))) {
  473. $this->attributes['class'] = trim('flexible ' . $this->attributes['class']);
  474. }
  475. }
  476. /**
  477. * Get the order by clause from the session, for the table with id $uniqueid.
  478. * @param string $uniqueid the identifier for a table.
  479. * @return SQL fragment that can be used in an ORDER BY clause.
  480. */
  481. public static function get_sort_for_table($uniqueid) {
  482. global $SESSION;
  483. if(empty($SESSION->flextable[$uniqueid])) {
  484. return '';
  485. }
  486. $sess = &$SESSION->flextable[$uniqueid];
  487. if (empty($sess->sortby)) {
  488. return '';
  489. }
  490. return self::construct_order_by($sess->sortby);
  491. }
  492. /**
  493. * Prepare an an order by clause from the list of columns to be sorted.
  494. * @param array $cols column name => SORT_ASC or SORT_DESC
  495. * @return SQL fragment that can be used in an ORDER BY clause.
  496. */
  497. public static function construct_order_by($cols) {
  498. $bits = array();
  499. foreach($cols as $column => $order) {
  500. if ($order == SORT_ASC) {
  501. $bits[] = $column . ' ASC';
  502. } else {
  503. $bits[] = $column . ' DESC';
  504. }
  505. }
  506. return implode(', ', $bits);
  507. }
  508. /**
  509. * @return SQL fragment that can be used in an ORDER BY clause.
  510. */
  511. public function get_sql_sort() {
  512. return self::construct_order_by($this->get_sort_columns());
  513. }
  514. /**
  515. * Get the columns to sort by, in the form required by {@link construct_order_by()}.
  516. * @return array column name => SORT_... constant.
  517. */
  518. public function get_sort_columns() {
  519. if (!$this->setup) {
  520. throw new coding_exception('Cannot call get_sort_columns until you have called setup.');
  521. }
  522. if (empty($this->sess->sortby)) {
  523. return array();
  524. }
  525. return $this->sess->sortby;
  526. }
  527. /**
  528. * @return integer the offset for LIMIT clause of SQL
  529. */
  530. function get_page_start() {
  531. if(!$this->use_pages) {
  532. return '';
  533. }
  534. return $this->currpage * $this->pagesize;
  535. }
  536. /**
  537. * @return integer the pagesize for LIMIT clause of SQL
  538. */
  539. function get_page_size() {
  540. if(!$this->use_pages) {
  541. return '';
  542. }
  543. return $this->pagesize;
  544. }
  545. /**
  546. * @return array - sql where, params array
  547. */
  548. function get_sql_where() {
  549. global $DB;
  550. $conditions = array();
  551. $params = array();
  552. if (isset($this->columns['fullname'])) {
  553. static $i = 0;
  554. $i++;
  555. if (!empty($this->sess->i_first)) {
  556. $conditions[] = $DB->sql_like('firstname', ':ifirstc'.$i, false, false);
  557. $params['ifirstc'.$i] = $this->sess->i_first.'%';
  558. }
  559. if (!empty($this->sess->i_last)) {
  560. $conditions[] = $DB->sql_like('lastname', ':ilastc'.$i, false, false);
  561. $params['ilastc'.$i] = $this->sess->i_last.'%';
  562. }
  563. }
  564. return array(implode(" AND ", $conditions), $params);
  565. }
  566. /**
  567. * Add a row of data to the table. This function takes an array with
  568. * column names as keys.
  569. * It ignores any elements with keys that are not defined as columns. It
  570. * puts in empty strings into the row when there is no element in the passed
  571. * array corresponding to a column in the table. It puts the row elements in
  572. * the proper order.
  573. * @param $rowwithkeys array
  574. * @param string $classname CSS class name to add to this row's tr tag.
  575. */
  576. function add_data_keyed($rowwithkeys, $classname = ''){
  577. $this->add_data($this->get_row_from_keyed($rowwithkeys), $classname);
  578. }
  579. /**
  580. * Add a seperator line to table.
  581. */
  582. function add_separator() {
  583. if(!$this->setup) {
  584. return false;
  585. }
  586. $this->add_data(NULL);
  587. }
  588. /**
  589. * This method actually directly echoes the row passed to it now or adds it
  590. * to the download. If this is the first row and start_output has not
  591. * already been called this method also calls start_output to open the table
  592. * or send headers for the downloaded.
  593. * Can be used as before. print_html now calls finish_html to close table.
  594. *
  595. * @param array $row a numerically keyed row of data to add to the table.
  596. * @param string $classname CSS class name to add to this row's tr tag.
  597. * @return boolean success.
  598. */
  599. function add_data($row, $classname = '') {
  600. if(!$this->setup) {
  601. return false;
  602. }
  603. if (!$this->started_output){
  604. $this->start_output();
  605. }
  606. if ($this->exportclass!==null){
  607. if ($row === null){
  608. $this->exportclass->add_seperator();
  609. } else {
  610. $this->exportclass->add_data($row);
  611. }
  612. } else {
  613. $this->print_row($row, $classname);
  614. }
  615. return true;
  616. }
  617. /**
  618. * You should call this to finish outputting the table data after adding
  619. * data to the table with add_data or add_data_keyed.
  620. *
  621. */
  622. function finish_output($closeexportclassdoc = true){
  623. if ($this->exportclass!==null){
  624. $this->exportclass->finish_table();
  625. if ($closeexportclassdoc){
  626. $this->exportclass->finish_document();
  627. }
  628. }else{
  629. $this->finish_html();
  630. }
  631. }
  632. /**
  633. * Hook that can be overridden in child classes to wrap a table in a form
  634. * for example. Called only when there is data to display and not
  635. * downloading.
  636. */
  637. function wrap_html_start(){
  638. }
  639. /**
  640. * Hook that can be overridden in child classes to wrap a table in a form
  641. * for example. Called only when there is data to display and not
  642. * downloading.
  643. */
  644. function wrap_html_finish(){
  645. }
  646. /**
  647. *
  648. * @param array $row row of data from db used to make one row of the table.
  649. * @return array one row for the table, added using add_data_keyed method.
  650. */
  651. function format_row($row){
  652. $formattedrow = array();
  653. foreach (array_keys($this->columns) as $column){
  654. $colmethodname = 'col_'.$column;
  655. if (method_exists($this, $colmethodname)){
  656. $formattedcolumn = $this->$colmethodname($row);
  657. } else {
  658. $formattedcolumn = $this->other_cols($column, $row);
  659. if ($formattedcolumn===NULL){
  660. $formattedcolumn = $row->$column;
  661. }
  662. }
  663. $formattedrow[$column] = $formattedcolumn;
  664. }
  665. return $formattedrow;
  666. }
  667. /**
  668. * Fullname is treated as a special columname in tablelib and should always
  669. * be treated the same as the fullname of a user.
  670. * @uses $this->useridfield if the userid field is not expected to be id
  671. * then you need to override $this->useridfield to point at the correct
  672. * field for the user id.
  673. *
  674. */
  675. function col_fullname($row){
  676. global $COURSE, $CFG;
  677. if (!$this->download){
  678. if ($COURSE->id == SITEID) {
  679. return '<a href="'.$CFG->wwwroot.'/user/profile.php?id='.$row->{$this->useridfield}. '">'.
  680. fullname($row).'</a>';
  681. } else {
  682. return '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$row->{$this->useridfield}.
  683. '&amp;course='.$COURSE->id.'">'.fullname($row).'</a>';
  684. }
  685. } else {
  686. return fullname($row);
  687. }
  688. }
  689. /**
  690. * You can override this method in a child class. See the description of
  691. * build_table which calls this method.
  692. */
  693. function other_cols($column, $row){
  694. return NULL;
  695. }
  696. /**
  697. * Used from col_* functions when text is to be displayed. Does the
  698. * right thing - either converts text to html or strips any html tags
  699. * depending on if we are downloading and what is the download type. Params
  700. * are the same as format_text function in weblib.php but some default
  701. * options are changed.
  702. */
  703. function format_text($text, $format=FORMAT_MOODLE, $options=NULL, $courseid=NULL){
  704. if (!$this->is_downloading()){
  705. if (is_null($options)){
  706. $options = new stdClass;
  707. }
  708. //some sensible defaults
  709. if (!isset($options->para)){
  710. $options->para = false;
  711. }
  712. if (!isset($options->newlines)){
  713. $options->newlines = false;
  714. }
  715. if (!isset($options->smiley)) {
  716. $options->smiley = false;
  717. }
  718. if (!isset($options->filter)) {
  719. $options->filter = false;
  720. }
  721. return format_text($text, $format, $options);
  722. } else {
  723. $eci =& $this->export_class_instance();
  724. return $eci->format_text($text, $format, $options, $courseid);
  725. }
  726. }
  727. /**
  728. * This method is deprecated although the old api is still supported.
  729. * @deprecated 1.9.2 - Jun 2, 2008
  730. */
  731. function print_html() {
  732. if(!$this->setup) {
  733. return false;
  734. }
  735. $this->finish_html();
  736. }
  737. /**
  738. * This function is not part of the public api.
  739. * @return string initial of first name we are currently filtering by
  740. */
  741. function get_initial_first() {
  742. if(!$this->use_initials) {
  743. return NULL;
  744. }
  745. return $this->sess->i_first;
  746. }
  747. /**
  748. * This function is not part of the public api.
  749. * @return string initial of last name we are currently filtering by
  750. */
  751. function get_initial_last() {
  752. if(!$this->use_initials) {
  753. return NULL;
  754. }
  755. return $this->sess->i_last;
  756. }
  757. /**
  758. * This function is not part of the public api.
  759. */
  760. function print_initials_bar(){
  761. if ((!empty($this->sess->i_last) || !empty($this->sess->i_first) || $this->use_initials)
  762. && isset($this->columns['fullname'])) {
  763. $strall = get_string('all');
  764. $alpha = explode(',', get_string('alphabet', 'langconfig'));
  765. // Bar of first initials
  766. echo '<div class="initialbar firstinitial">'.get_string('firstname').' : ';
  767. if(!empty($this->sess->i_first)) {
  768. echo '<a href="'.$this->baseurl.$this->request[TABLE_VAR_IFIRST].'=">'.$strall.'</a>';
  769. } else {
  770. echo '<strong>'.$strall.'</strong>';
  771. }
  772. foreach ($alpha as $letter) {
  773. if (isset($this->sess->i_first) && $letter == $this->sess->i_first) {
  774. echo ' <strong>'.$letter.'</strong>';
  775. } else {
  776. echo ' <a href="'.$this->baseurl.$this->request[TABLE_VAR_IFIRST].'='.$letter.'">'.$letter.'</a>';
  777. }
  778. }
  779. echo '</div>';
  780. // Bar of last initials
  781. echo '<div class="initialbar lastinitial">'.get_string('lastname').' : ';
  782. if(!empty($this->sess->i_last)) {
  783. echo '<a href="'.$this->baseurl.$this->request[TABLE_VAR_ILAST].'=">'.$strall.'</a>';
  784. } else {
  785. echo '<strong>'.$strall.'</strong>';
  786. }
  787. foreach ($alpha as $letter) {
  788. if (isset($this->sess->i_last) && $letter == $this->sess->i_last) {
  789. echo ' <strong>'.$letter.'</strong>';
  790. } else {
  791. echo ' <a href="'.$this->baseurl.$this->request[TABLE_VAR_ILAST].'='.$letter.'">'.$letter.'</a>';
  792. }
  793. }
  794. echo '</div>';
  795. }
  796. }
  797. /**
  798. * This function is not part of the public api.
  799. */
  800. function print_nothing_to_display(){
  801. global $OUTPUT;
  802. $this->print_initials_bar();
  803. echo $OUTPUT->heading(get_string('nothingtodisplay'));
  804. }
  805. /**
  806. * This function is not part of the public api.
  807. */
  808. function get_row_from_keyed($rowwithkeys){
  809. if (is_object($rowwithkeys)){
  810. $rowwithkeys = (array)$rowwithkeys;
  811. }
  812. $row = array();
  813. foreach (array_keys($this->columns) as $column){
  814. if (isset($rowwithkeys[$column])){
  815. $row [] = $rowwithkeys[$column];
  816. } else {
  817. $row[] ='';
  818. }
  819. }
  820. return $row;
  821. }
  822. /**
  823. * This function is not part of the public api.
  824. */
  825. function get_download_menu(){
  826. $allclasses= get_declared_classes();
  827. $exportclasses = array();
  828. foreach ($allclasses as $class){
  829. $matches = array();
  830. if (preg_match('/^table\_([a-z]+)\_export\_format$/', $class, $matches)){
  831. $type = $matches[1];
  832. $exportclasses[$type]= get_string("download$type", 'table');
  833. }
  834. }
  835. return $exportclasses;
  836. }
  837. /**
  838. * This function is not part of the public api.
  839. */
  840. function download_buttons(){
  841. global $OUTPUT;
  842. if ($this->is_downloadable() && !$this->is_downloading()){
  843. $downloadoptions = $this->get_download_menu();
  844. $html = '<form action="'. $this->baseurl .'" method="post">';
  845. $html .= '<div class="mdl-align">';
  846. $html .= '<input type="submit" value="'.get_string('downloadas', 'table').'"/>';
  847. $html .= html_writer::select($downloadoptions, 'download', $this->defaultdownloadformat, false);
  848. $html .= '</div></form>';
  849. return $html;
  850. } else {
  851. return '';
  852. }
  853. }
  854. /**
  855. * This function is not part of the public api.
  856. * You don't normally need to call this. It is called automatically when
  857. * needed when you start adding data to the table.
  858. *
  859. */
  860. function start_output(){
  861. $this->started_output = true;
  862. if ($this->exportclass!==null){
  863. $this->exportclass->start_table($this->sheettitle);
  864. $this->exportclass->output_headers($this->headers);
  865. } else {
  866. $this->start_html();
  867. $this->print_headers();
  868. }
  869. }
  870. /**
  871. * This function is not part of the public api.
  872. */
  873. function print_row($row, $classname = '') {
  874. static $suppress_lastrow = NULL;
  875. static $oddeven = 1;
  876. $rowclasses = array('r' . $oddeven);
  877. $oddeven = $oddeven ? 0 : 1;
  878. if ($classname) {
  879. $rowclasses[] = $classname;
  880. }
  881. echo '<tr class="' . implode(' ', $rowclasses) . '">';
  882. // If we have a separator, print it
  883. if ($row === NULL) {
  884. $colcount = count($this->columns);
  885. echo '<td colspan="'.$colcount.'"><div class="tabledivider"></div></td>';
  886. } else {
  887. $colbyindex = array_flip($this->columns);
  888. foreach ($row as $index => $data) {
  889. $column = $colbyindex[$index];
  890. echo '<td class="cell c'.$index.$this->column_class[$column].'"'.$this->make_styles_string($this->column_style[$column]).'>';
  891. if (empty($this->sess->collapse[$column])) {
  892. if ($this->column_suppress[$column] && $suppress_lastrow !== NULL && $suppress_lastrow[$index] === $data) {
  893. echo '&nbsp;';
  894. } else {
  895. echo $data;
  896. }
  897. } else {
  898. echo '&nbsp;';
  899. }
  900. echo '</td>';
  901. }
  902. }
  903. echo '</tr>';
  904. $suppress_enabled = array_sum($this->column_suppress);
  905. if ($suppress_enabled) {
  906. $suppress_lastrow = $row;
  907. }
  908. }
  909. /**
  910. * This function is not part of the public api.
  911. */
  912. function finish_html(){
  913. global $OUTPUT;
  914. if (!$this->started_output) {
  915. //no data has been added to the table.
  916. $this->print_nothing_to_display();
  917. } else {
  918. echo '</table>';
  919. echo html_writer::end_tag('div');
  920. $this->wrap_html_finish();
  921. // Paging bar
  922. if(in_array(TABLE_P_BOTTOM, $this->showdownloadbuttonsat)) {
  923. echo $this->download_buttons();
  924. }
  925. if($this->use_pages) {
  926. $pagingbar = new paging_bar($this->totalrows, $this->currpage, $this->pagesize, $this->baseurl);
  927. $pagingbar->pagevar = $this->request[TABLE_VAR_PAGE];
  928. echo $OUTPUT->render($pagingbar);
  929. }
  930. }
  931. }
  932. /**
  933. * This function is not part of the public api.
  934. */
  935. function print_headers(){
  936. global $CFG, $OUTPUT;
  937. echo '<tr>';
  938. foreach($this->columns as $column => $index) {
  939. $icon_hide = '';
  940. $icon_sort = '';
  941. if($this->is_collapsible) {
  942. if(!empty($this->sess->collapse[$column])) {
  943. // some headers contain < br/> tags, do not include in title
  944. $icon_hide = ' <a href="'.$this->baseurl.$this->request[TABLE_VAR_SHOW].'='.$column.'"><img src="'.$OUTPUT->pix_url('t/switch_plus') . '" title="'.get_string('show').' '.strip_tags($this->headers[$index]).'" alt="'.get_string('show').'" /></a>';
  945. }
  946. else if($this->headers[$index] !== NULL) {
  947. // some headers contain < br/> tags, do not include in title
  948. $icon_hide = ' <a href="'.$this->baseurl.$this->request[TABLE_VAR_HIDE].'='.$column.'"><img src="'.$OUTPUT->pix_url('t/switch_minus') . '" title="'.get_string('hide').' '.strip_tags($this->headers[$index]).'" alt="'.get_string('hide').'" /></a>';
  949. }
  950. }
  951. $primary_sort_column = '';
  952. $primary_sort_order = '';
  953. if(reset($this->sess->sortby)) {
  954. $primary_sort_column = key($this->sess->sortby);
  955. $primary_sort_order = current($this->sess->sortby);
  956. }
  957. switch($column) {
  958. case 'fullname':
  959. if($this->is_sortable($column)) {
  960. $icon_sort_first = $icon_sort_last = '';
  961. if($primary_sort_column == 'firstname') {
  962. $lsortorder = get_string('asc');
  963. if($primary_sort_order == SORT_ASC) {
  964. $icon_sort_first = ' <img src="'.$OUTPUT->pix_url('t/down') . '" alt="'.get_string('asc').'" />';
  965. $fsortorder = get_string('asc');
  966. }
  967. else {
  968. $icon_sort_first = ' <img src="'.$OUTPUT->pix_url('t/up') . '" alt="'.get_string('desc').'" />';
  969. $fsortorder = get_string('desc');
  970. }
  971. }
  972. else if($primary_sort_column == 'lastname') {
  973. $fsortorder = get_string('asc');
  974. if($primary_sort_order == SORT_ASC) {
  975. $icon_sort_last = ' <img src="'.$OUTPUT->pix_url('t/down') . '" alt="'.get_string('asc').'" />';
  976. $lsortorder = get_string('asc');
  977. }
  978. else {
  979. $icon_sort_last = ' <img src="'.$OUTPUT->pix_url('t/up') . '" alt="'.get_string('desc').'" />';
  980. $lsortorder = get_string('desc');
  981. }
  982. } else {
  983. $fsortorder = get_string('asc');
  984. $lsortorder = get_string('asc');
  985. }
  986. $override = new stdClass();
  987. $override->firstname = 'firstname';
  988. $override->lastname = 'lastname';
  989. $fullnamelanguage = get_string('fullnamedisplay', '', $override);
  990. if (($CFG->fullnamedisplay == 'firstname lastname') or
  991. ($CFG->fullnamedisplay == 'firstname') or
  992. ($CFG->fullnamedisplay == 'language' and $fullnamelanguage == 'firstname lastname' )) {
  993. $this->headers[$index] = '<a href="'.$this->baseurl.$this->request[TABLE_VAR_SORT].'=firstname">'.get_string('firstname').get_accesshide(get_string('sortby').' '.get_string('firstname').' '.$fsortorder).'</a> '.$icon_sort_first.' / '.
  994. '<a href="'.$this->baseurl.$this->request[TABLE_VAR_SORT].'=lastname">'.get_string('lastname').get_accesshide(get_string('sortby').' '.get_string('lastname').' '.$lsortorder).'</a> '.$icon_sort_last;
  995. } else {
  996. $this->headers[$index] = '<a href="'.$this->baseurl.$this->request[TABLE_VAR_SORT].'=lastname">'.get_string('lastname').get_accesshide(get_string('sortby').' '.get_string('lastname').' '.$lsortorder).'</a> '.$icon_sort_last.' / '.
  997. '<a href="'.$this->baseurl.$this->request[TABLE_VAR_SORT].'=firstname">'.get_string('firstname').get_accesshide(get_string('sortby').' '.get_string('firstname').' '.$fsortorder).'</a> '.$icon_sort_first;
  998. }
  999. }
  1000. break;
  1001. case 'userpic':
  1002. // do nothing, do not display sortable links
  1003. break;
  1004. default:
  1005. if($this->is_sortable($column)) {
  1006. if($primary_sort_column == $column) {
  1007. if($primary_sort_order == SORT_ASC) {
  1008. $icon_sort = ' <img src="'.$OUTPUT->pix_url('t/down') . '" alt="'.get_string('asc').'" />';
  1009. $localsortorder = get_string('asc');
  1010. }
  1011. else {
  1012. $icon_sort = ' <img src="'.$OUTPUT->pix_url('t/up') . '" alt="'.get_string('desc').'" />';
  1013. $localsortorder = get_string('desc');
  1014. }
  1015. } else {
  1016. $localsortorder = get_string('asc');
  1017. }
  1018. $this->headers[$index] = '<a href="'.$this->baseurl.$this->request[TABLE_VAR_SORT].'='.$column.'">'.$this->headers[$index].get_accesshide(get_string('sortby').' '.$this->headers[$index].' '.$localsortorder).'</a>';
  1019. }
  1020. }
  1021. if($this->headers[$index] === NULL) {
  1022. echo '<th class="header c'.$index.$this->column_class[$column].'" scope="col">&nbsp;</th>';
  1023. }
  1024. else if(!empty($this->sess->collapse[$column])) {
  1025. echo '<th class="header c'.$index.$this->column_class[$column].'" scope="col">'.$icon_hide.'</th>';
  1026. }
  1027. else {
  1028. // took out nowrap for accessibility, might need replacement
  1029. if (!is_array($this->column_style[$column])) {
  1030. // $usestyles = array('white-space:nowrap');
  1031. $usestyles = '';
  1032. } else {
  1033. // $usestyles = $this->column_style[$column]+array('white-space'=>'nowrap');
  1034. $usestyles = $this->column_style[$column];
  1035. }
  1036. echo '<th class="header c'.$index.$this->column_class[$column].'" '.$this->make_styles_string($usestyles).' scope="col">'.$this->headers[$index].$icon_sort.'<div class="commands">'.$icon_hide.'</div></th>';
  1037. }
  1038. }
  1039. echo '</tr>';
  1040. }
  1041. /**
  1042. * This function is not part of the public api.
  1043. */
  1044. function start_html(){
  1045. global $OUTPUT;
  1046. // Do we need to print initial bars?
  1047. $this->print_initials_bar();
  1048. // Paging bar
  1049. if($this->use_pages) {
  1050. $pagingbar = new paging_bar($this->totalrows, $this->currpage, $this->pagesize, $this->baseurl);
  1051. $pagingbar->pagevar = $this->request[TABLE_VAR_PAGE];
  1052. echo $OUTPUT->render($pagingbar);
  1053. }
  1054. if(in_array(TABLE_P_TOP, $this->showdownloadbuttonsat)) {
  1055. echo $this->download_buttons();
  1056. }
  1057. $this->wrap_html_start();
  1058. // Start of main data table
  1059. echo html_writer::start_tag('div', array('class'=>'no-overflow'));
  1060. echo '<table'.$this->make_attributes_string($this->attributes).'>';
  1061. }
  1062. /**
  1063. * This function is not part of the public api.
  1064. * @todo Document
  1065. * @return type?
  1066. */
  1067. function make_styles_string(&$styles) {
  1068. if(empty($styles)) {
  1069. return '';
  1070. }
  1071. $string = ' style="';
  1072. foreach($styles as $property => $value) {
  1073. $string .= $property.':'.$value.';';
  1074. }
  1075. $string .= '"';
  1076. return $string;
  1077. }
  1078. /**
  1079. * This function is not part of the public api.
  1080. * @todo Document
  1081. * @return type?
  1082. */
  1083. function make_attributes_string(&$attributes) {
  1084. if(empty($attributes)) {
  1085. return '';
  1086. }
  1087. $string = ' ';
  1088. foreach($attributes as $attr => $value) {
  1089. $string .= ($attr.'="'.$value.'" ');
  1090. }
  1091. return $string;
  1092. }
  1093. }
  1094. /**
  1095. * @package moodlecore
  1096. * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
  1097. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  1098. */
  1099. class table_sql extends flexible_table{
  1100. public $countsql = NULL;
  1101. public $countparams = NULL;
  1102. /**
  1103. * @var object sql for querying db. Has fields 'fields', 'from', 'where', 'params'.
  1104. */
  1105. public $sql = NULL;
  1106. /**
  1107. * @var array Data fetched from the db.
  1108. */
  1109. public $rawdata = NULL;
  1110. /**
  1111. * @var boolean Overriding default for this.
  1112. */
  1113. public $is_sortable = true;
  1114. /**
  1115. * @var boolean Overriding default for this.
  1116. */
  1117. public $is_collapsible = true;
  1118. /**
  1119. * @param string $uniqueid a string identifying this table.Used as a key in
  1120. * session vars.
  1121. */
  1122. function table_sql($uniqueid){
  1123. parent::flexible_table($uniqueid);
  1124. // some sensible defaults
  1125. $this->set_attribute('cellspacing', '0');
  1126. $this->set_attribute('class', 'generaltable generalbox');
  1127. }
  1128. /**
  1129. * Take the data returned from the db_query and go through all the rows
  1130. * processing each col using either col_{columnname} method or other_cols
  1131. * method or if other_cols returns NULL then put the data straight into the
  1132. * table.
  1133. */
  1134. function build_table(){
  1135. if ($this->rawdata){
  1136. foreach($this->rawdata as $row){
  1137. $formattedrow = $this->format_row($row);
  1138. $this->add_data_keyed($formattedrow);
  1139. }
  1140. }
  1141. }
  1142. /**
  1143. * This is only needed if you want to use different sql to count rows.
  1144. * Used for example when perhaps all db JOINS are not needed when counting
  1145. * records. You don't need to call this function the count_sql
  1146. * will be generated automatically.
  1147. *
  1148. * We need to count rows returned by the db seperately to the query itself
  1149. * as we need to know how many pages of data we have to display.
  1150. */
  1151. function set_count_sql($sql, array $params = NULL){
  1152. $this->countsql = $sql;
  1153. $this->countparams = $params;
  1154. }
  1155. /**
  1156. * Set the sql to query the db. Query will be :
  1157. * SELECT $fields FROM $from WHERE $where
  1158. * Of course you can use sub-queries, JOINS etc. by putting them in the
  1159. * appropriate clause of the query.
  1160. */
  1161. function set_sql($fields, $from, $where, array $params = NULL){
  1162. $this->sql = new stdClass();
  1163. $this->sql->fields = $fields;
  1164. $this->sql->from = $from;
  1165. $this->sql->where = $where;
  1166. $this->sql->params = $params;
  1167. }
  1168. /**
  1169. * Query the db. Store results in the table object for use by build_table.
  1170. *
  1171. * @param integer $pagesize size of page for paginated displayed table.
  1172. * @param boolean $useinitialsbar do you want to use the initials bar. Bar
  1173. * will only be used if there is a fullname column defined for the table.
  1174. */
  1175. function query_db($pagesize, $useinitialsbar=true){
  1176. global $DB;
  1177. if (!$this->is_downloading()) {
  1178. if ($this->countsql === NULL){
  1179. $this->countsql = 'SELECT COUNT(1) FROM '.$this->sql->from.' WHERE '.$this->sql->where;
  1180. }
  1181. if ($useinitialsbar && !$this->is_downloading()) {
  1182. $totalinitials = $DB->count_records_sql($this->countsql, $this->countparams);
  1183. $this->initialbars($totalinitials>$pagesize);
  1184. }
  1185. list($wsql, $wparams) = $this->get_sql_where();
  1186. if ($wsql) {
  1187. $this->countsql .= ' AND '.$wsql;
  1188. $this->countparams = array_merge($this->countparams, $wparams);
  1189. $this->sql->where .= ' AND '.$wsql;
  1190. $this->sql->params = array_merge($this->sql->params, $wparams);
  1191. $total = $DB->count_records_sql($this->countsql, $this->countparams);
  1192. } else {
  1193. $total = $totalinitials;
  1194. }
  1195. $this->pagesize($pagesize, $total);
  1196. }
  1197. // Fetch the attempts
  1198. $sort = $this->get_sql_sort();
  1199. $sort = $sort?" ORDER BY {$sort}":'';
  1200. $sql = "SELECT {$this->sql->fields} FROM {$this->sql->from} WHERE {$this->sql->where}{$sort}";
  1201. if (!$this->is_downloading()) {
  1202. $this->rawdata = $DB->get_records_sql($sql, $this->sql->params, $this->get_page_start(), $this->get_page_size());
  1203. } else {
  1204. $this->rawdata = $DB->get_records_sql($sql, $this->sql->params);
  1205. }
  1206. }
  1207. /**
  1208. * Convenience method to call a number of methods for you to display the
  1209. * table.
  1210. */
  1211. function out($pagesize, $useinitialsbar, $downloadhelpbutton=''){
  1212. global $DB;
  1213. if (!$this->columns){
  1214. $onerow = $DB->get_record_sql("SELECT {$this->sql->fields} FROM {$this->sql->from} WHERE {$this->sql->where}", $this->sql->params);
  1215. //if columns is not set then define columns as the keys of the rows returned
  1216. //from the db.
  1217. $this->define_columns(array_keys((array)$onerow));
  1218. $this->define_headers(array_keys((array)$onerow));
  1219. }
  1220. $this->setup();
  1221. $this->query_db($pagesize, $useinitialsbar);
  1222. $this->build_table();
  1223. $this->finish_output();
  1224. }
  1225. }
  1226. /**
  1227. * @package moodlecore
  1228. * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
  1229. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  1230. */
  1231. class table_default_export_format_parent{
  1232. /**
  1233. * @var flexible_table or child class reference pointing to table class
  1234. * object from which to export data.
  1235. */
  1236. var $table;
  1237. /

Large files files are truncated, but you can click here to view the full file