diff --git a/lib/tablelib.php b/lib/tablelib.php index 975b41823a0..0202b624f44 100644 --- a/lib/tablelib.php +++ b/lib/tablelib.php @@ -22,6 +22,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ + defined('MOODLE_INTERNAL') || die(); /** TABLE_VAR_SORT = 1 */ @@ -42,6 +43,7 @@ define('TABLE_P_TOP', 1); /** TABLE_P_BOTTOM = 2 */ define('TABLE_P_BOTTOM', 2); + /** * @package moodlecore * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} @@ -79,7 +81,6 @@ class flexible_table { */ var $showdownloadbuttonsat= array(TABLE_P_TOP); - /** * @var string Key of field returned by db query that is the id field of the * user table or equivalent. @@ -142,8 +143,8 @@ class flexible_table { * @param string $sheettitle title for downloaded data. * @return string download type. One of csv, tsv, xhtml, ods, etc */ - function is_downloading($download = null, $filename='', $sheettitle=''){ - if ($download!==null){ + function is_downloading($download = null, $filename='', $sheettitle='') { + if ($download!==null) { $this->sheettitle = $sheettitle; $this->is_downloadable(true); $this->download = $download; @@ -153,22 +154,21 @@ class flexible_table { return $this->download; } - function export_class_instance(&$exportclass=null){ - if (!is_null($exportclass)){ + function export_class_instance(&$exportclass=null) { + if (!is_null($exportclass)) { $this->started_output = true; $this->exportclass =& $exportclass; $this->exportclass->table =& $this; - } elseif (is_null($this->exportclass) && !empty($this->download)){ + } elseif (is_null($this->exportclass) && !empty($this->download)) { $classname = 'table_'.$this->download.'_export_format'; $this->exportclass = new $classname($this); - if (!$this->exportclass->document_started()){ + if (!$this->exportclass->document_started()) { $this->exportclass->start_document($this->filename); } } return $this->exportclass; } - /** * Probably don't need to call this directly. Calling is_downloading with a * param automatically sets table as downloadable. @@ -178,8 +178,8 @@ class flexible_table { * current state of table. * @return boolean whether table data is set to be downloadable. */ - function is_downloadable($downloadable = null){ - if ($downloadable !== null){ + function is_downloadable($downloadable = null) { + if ($downloadable !== null) { $this->downloadable = $downloadable; } return $this->downloadable; @@ -190,11 +190,10 @@ class flexible_table { * @param array $showat array of postions in which to show download buttons. * Containing TABLE_P_TOP and/or TABLE_P_BOTTOM */ - function show_download_buttons_at($showat){ + function show_download_buttons_at($showat) { $this->showdownloadbuttonsat = $showat; } - /** * Sets the is_sortable variable to the given boolean, sort_default_column to * the given string, and the sort_default_order to the given integer. @@ -222,7 +221,7 @@ class flexible_table { * @param string column name, null means table * @return bool */ - function is_sortable($column=null) { + function is_sortable($column = null) { if (empty($column)) { return $this->is_sortable; } @@ -231,6 +230,7 @@ class flexible_table { } return !in_array($column, $this->column_nosort); } + /** * Sets the is_collapsible variable to the given boolean. * @param bool $bool @@ -278,8 +278,8 @@ class flexible_table { * @return void */ function set_control_variables($variables) { - foreach($variables as $what => $variable) { - if(isset($this->request[$what])) { + foreach ($variables as $what => $variable) { + if (isset($this->request[$what])) { $this->request[$what] = $variable; } } @@ -305,7 +305,7 @@ class flexible_table { * @param integer $column the index of a column. */ function column_suppress($column) { - if(isset($this->column_suppress[$column])) { + if (isset($this->column_suppress[$column])) { $this->column_suppress[$column] = true; } } @@ -317,7 +317,7 @@ class flexible_table { * @return void */ function column_class($column, $classname) { - if(isset($this->column_class[$column])) { + if (isset($this->column_class[$column])) { $this->column_class[$column] = ' '.$classname; // This space needed so that classnames don't run together in the HTML } } @@ -330,7 +330,7 @@ class flexible_table { * @return void */ function column_style($column, $property, $value) { - if(isset($this->column_style[$column])) { + if (isset($this->column_style[$column])) { $this->column_style[$column][$property] = $value; } } @@ -342,7 +342,7 @@ class flexible_table { * @return void */ function column_style_all($property, $value) { - foreach(array_keys($this->columns) as $column) { + foreach (array_keys($this->columns) as $column) { $this->column_style[$column][$property] = $value; } } @@ -353,10 +353,9 @@ class flexible_table { */ function define_baseurl($url) { $this->reseturl = $url; - if(!strpos($url, '?')) { + if (!strpos($url, '?')) { $this->baseurl = $url.'?'; - } - else { + } else { $this->baseurl = $url.'&'; } } @@ -371,7 +370,7 @@ class flexible_table { $this->column_class = array(); $colnum = 0; - foreach($columns as $column) { + foreach ($columns as $column) { $this->columns[$column] = $colnum++; $this->column_style[$column] = array(); $this->column_class[$column] = ''; @@ -387,9 +386,6 @@ class flexible_table { $this->headers = $headers; } - - - /** * Must be called after table is defined. Use methods above first. Cannot * use functions below till after calling this method. @@ -398,7 +394,7 @@ class flexible_table { function setup() { global $SESSION, $CFG; - if(empty($this->columns) || empty($this->uniqueid)) { + if (empty($this->columns) || empty($this->uniqueid)) { return false; } @@ -406,7 +402,7 @@ class flexible_table { $SESSION->flextable = array(); } - if(!isset($SESSION->flextable[$this->uniqueid])) { + if (!isset($SESSION->flextable[$this->uniqueid])) { $SESSION->flextable[$this->uniqueid] = new stdClass; $SESSION->flextable[$this->uniqueid]->uniqueid = $this->uniqueid; $SESSION->flextable[$this->uniqueid]->collapse = array(); @@ -417,45 +413,43 @@ class flexible_table { $this->sess = &$SESSION->flextable[$this->uniqueid]; - if(!empty($_GET[$this->request[TABLE_VAR_SHOW]]) && isset($this->columns[$_GET[$this->request[TABLE_VAR_SHOW]]])) { + if (!empty($_GET[$this->request[TABLE_VAR_SHOW]]) && isset($this->columns[$_GET[$this->request[TABLE_VAR_SHOW]]])) { // Show this column $this->sess->collapse[$_GET[$this->request[TABLE_VAR_SHOW]]] = false; - } - else if(!empty($_GET[$this->request[TABLE_VAR_HIDE]]) && isset($this->columns[$_GET[$this->request[TABLE_VAR_HIDE]]])) { + } else if(!empty($_GET[$this->request[TABLE_VAR_HIDE]]) && isset($this->columns[$_GET[$this->request[TABLE_VAR_HIDE]]])) { // Hide this column $this->sess->collapse[$_GET[$this->request[TABLE_VAR_HIDE]]] = true; - if(array_key_exists($_GET[$this->request[TABLE_VAR_HIDE]], $this->sess->sortby)) { + if (array_key_exists($_GET[$this->request[TABLE_VAR_HIDE]], $this->sess->sortby)) { unset($this->sess->sortby[$_GET[$this->request[TABLE_VAR_HIDE]]]); } } // Now, update the column attributes for collapsed columns - foreach(array_keys($this->columns) as $column) { - if(!empty($this->sess->collapse[$column])) { + foreach (array_keys($this->columns) as $column) { + if (!empty($this->sess->collapse[$column])) { $this->column_style[$column]['width'] = '10px'; } } - if( + if ( !empty($_GET[$this->request[TABLE_VAR_SORT]]) && $this->is_sortable($_GET[$this->request[TABLE_VAR_SORT]]) && (isset($this->columns[$_GET[$this->request[TABLE_VAR_SORT]]]) || (($_GET[$this->request[TABLE_VAR_SORT]] == 'firstname' || $_GET[$this->request[TABLE_VAR_SORT]] == 'lastname') && isset($this->columns['fullname'])) )) { - if(empty($this->sess->collapse[$_GET[$this->request[TABLE_VAR_SORT]]])) { - if(array_key_exists($_GET[$this->request[TABLE_VAR_SORT]], $this->sess->sortby)) { + if (empty($this->sess->collapse[$_GET[$this->request[TABLE_VAR_SORT]]])) { + if (array_key_exists($_GET[$this->request[TABLE_VAR_SORT]], $this->sess->sortby)) { // This key already exists somewhere. Change its sortorder and bring it to the top. $sortorder = $this->sess->sortby[$_GET[$this->request[TABLE_VAR_SORT]]] == SORT_ASC ? SORT_DESC : SORT_ASC; unset($this->sess->sortby[$_GET[$this->request[TABLE_VAR_SORT]]]); $this->sess->sortby = array_merge(array($_GET[$this->request[TABLE_VAR_SORT]] => $sortorder), $this->sess->sortby); - } - else { + } else { // Key doesn't exist, so just add it to the beginning of the array, ascending order $this->sess->sortby = array_merge(array($_GET[$this->request[TABLE_VAR_SORT]] => SORT_ASC), $this->sess->sortby); } // Finally, make sure that no more than $this->maxsortkeys are present into the array - if(!empty($this->maxsortkeys) && ($sortkeys = count($this->sess->sortby)) > $this->maxsortkeys) { - while($sortkeys-- > $this->maxsortkeys) { + if (!empty($this->maxsortkeys) && ($sortkeys = count($this->sess->sortby)) > $this->maxsortkeys) { + while ($sortkeys-- > $this->maxsortkeys) { array_pop($this->sess->sortby); } } @@ -463,23 +457,23 @@ class flexible_table { } // If we didn't sort just now, then use the default sort order if one is defined and the column exists - if(empty($this->sess->sortby) && !empty($this->sort_default_column)) { + if (empty($this->sess->sortby) && !empty($this->sort_default_column)) { $this->sess->sortby = array ($this->sort_default_column => ($this->sort_default_order == SORT_DESC ? SORT_DESC : SORT_ASC)); } - if(isset($_GET[$this->request[TABLE_VAR_ILAST]])) { - if(empty($_GET[$this->request[TABLE_VAR_ILAST]]) || is_numeric(strpos(get_string('alphabet', 'langconfig'), $_GET[$this->request[TABLE_VAR_ILAST]]))) { + if (isset($_GET[$this->request[TABLE_VAR_ILAST]])) { + if (empty($_GET[$this->request[TABLE_VAR_ILAST]]) || is_numeric(strpos(get_string('alphabet', 'langconfig'), $_GET[$this->request[TABLE_VAR_ILAST]]))) { $this->sess->i_last = $_GET[$this->request[TABLE_VAR_ILAST]]; } } - if(isset($_GET[$this->request[TABLE_VAR_IFIRST]])) { - if(empty($_GET[$this->request[TABLE_VAR_IFIRST]]) || is_numeric(strpos(get_string('alphabet', 'langconfig'), $_GET[$this->request[TABLE_VAR_IFIRST]]))) { + if (isset($_GET[$this->request[TABLE_VAR_IFIRST]])) { + if (empty($_GET[$this->request[TABLE_VAR_IFIRST]]) || is_numeric(strpos(get_string('alphabet', 'langconfig'), $_GET[$this->request[TABLE_VAR_IFIRST]]))) { $this->sess->i_first = $_GET[$this->request[TABLE_VAR_IFIRST]]; } } - if(empty($this->baseurl)) { + if (empty($this->baseurl)) { $getcopy = $_GET; unset($getcopy[$this->request[TABLE_VAR_SHOW]]); unset($getcopy[$this->request[TABLE_VAR_HIDE]]); @@ -490,22 +484,20 @@ class flexible_table { $strippedurl = strip_querystring(qualified_me()); - if(!empty($getcopy)) { + if (!empty($getcopy)) { $first = false; $querystring = ''; - foreach($getcopy as $var => $val) { - if(!$first) { + foreach ($getcopy as $var => $val) { + if (!$first) { $first = true; $querystring .= '?'.$var.'='.$val; - } - else { + } else { $querystring .= '&'.$var.'='.$val; } } $this->reseturl = $strippedurl.$querystring; $querystring .= '&'; - } - else { + } else { $this->reseturl = $strippedurl; $querystring = '?'; } @@ -514,7 +506,7 @@ class flexible_table { } // If it's "the first time" we 've been here, forget the previous initials filters - if(qualified_me() == $this->reseturl) { + if (qualified_me() == $this->reseturl) { $this->sess->i_first = ''; $this->sess->i_last = ''; } @@ -542,7 +534,7 @@ class flexible_table { */ public static function get_sort_for_table($uniqueid) { global $SESSION; - if(empty($SESSION->flextable[$uniqueid])) { + if (empty($SESSION->flextable[$uniqueid])) { return ''; } @@ -562,7 +554,7 @@ class flexible_table { public static function construct_order_by($cols) { $bits = array(); - foreach($cols as $column => $order) { + foreach ($cols as $column => $order) { if ($order == SORT_ASC) { $bits[] = $column . ' ASC'; } else { @@ -600,7 +592,7 @@ class flexible_table { * @return integer the offset for LIMIT clause of SQL */ function get_page_start() { - if(!$this->use_pages) { + if (!$this->use_pages) { return ''; } return $this->currpage * $this->pagesize; @@ -610,7 +602,7 @@ class flexible_table { * @return integer the pagesize for LIMIT clause of SQL */ function get_page_size() { - if(!$this->use_pages) { + if (!$this->use_pages) { return ''; } return $this->pagesize; @@ -652,7 +644,7 @@ class flexible_table { * @param $rowwithkeys array * @param string $classname CSS class name to add to this row's tr tag. */ - function add_data_keyed($rowwithkeys, $classname = ''){ + function add_data_keyed($rowwithkeys, $classname = '') { $this->add_data($this->get_row_from_keyed($rowwithkeys), $classname); } @@ -660,7 +652,7 @@ class flexible_table { * Add a seperator line to table. */ function add_separator() { - if(!$this->setup) { + if (!$this->setup) { return false; } $this->add_data(NULL); @@ -678,14 +670,14 @@ class flexible_table { * @return boolean success. */ function add_data($row, $classname = '') { - if(!$this->setup) { + if (!$this->setup) { return false; } - if (!$this->started_output){ + if (!$this->started_output) { $this->start_output(); } - if ($this->exportclass!==null){ - if ($row === null){ + if ($this->exportclass!==null) { + if ($row === null) { $this->exportclass->add_seperator(); } else { $this->exportclass->add_data($row); @@ -696,20 +688,18 @@ class flexible_table { 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. * */ - function finish_output($closeexportclassdoc = true){ - if ($this->exportclass!==null){ + function finish_output($closeexportclassdoc = true) { + if ($this->exportclass!==null) { $this->exportclass->finish_table(); - if ($closeexportclassdoc){ + if ($closeexportclassdoc) { $this->exportclass->finish_document(); } - }else{ + } else { $this->finish_html(); } } @@ -719,7 +709,7 @@ class flexible_table { * for example. Called only when there is data to display and not * downloading. */ - function wrap_html_start(){ + function wrap_html_start() { } /** @@ -727,24 +717,23 @@ class flexible_table { * for example. Called only when there is data to display and not * downloading. */ - function wrap_html_finish(){ + function wrap_html_finish() { } - /** * * @param array $row row of data from db used to make one row of the table. * @return array one row for the table, added using add_data_keyed method. */ - function format_row($row){ + function format_row($row) { $formattedrow = array(); - foreach (array_keys($this->columns) as $column){ + foreach (array_keys($this->columns) as $column) { $colmethodname = 'col_'.$column; - if (method_exists($this, $colmethodname)){ + if (method_exists($this, $colmethodname)) { $formattedcolumn = $this->$colmethodname($row); } else { $formattedcolumn = $this->other_cols($column, $row); - if ($formattedcolumn===NULL){ + if ($formattedcolumn===NULL) { $formattedcolumn = $row->$column; } } @@ -761,10 +750,10 @@ class flexible_table { * field for the user id. * */ - function col_fullname($row){ + function col_fullname($row) { global $COURSE, $CFG; - if (!$this->download){ + if (!$this->download) { if ($COURSE->id == SITEID) { return ''. fullname($row).''; @@ -781,11 +770,10 @@ class flexible_table { * You can override this method in a child class. See the description of * build_table which calls this method. */ - function other_cols($column, $row){ + function other_cols($column, $row) { return NULL; } - /** * Used from col_* functions when text is to be displayed. Does the * right thing - either converts text to html or strips any html tags @@ -793,16 +781,16 @@ class flexible_table { * are the same as format_text function in weblib.php but some default * options are changed. */ - function format_text($text, $format=FORMAT_MOODLE, $options=NULL, $courseid=NULL){ - if (!$this->is_downloading()){ - if (is_null($options)){ + function format_text($text, $format=FORMAT_MOODLE, $options=NULL, $courseid=NULL) { + if (!$this->is_downloading()) { + if (is_null($options)) { $options = new stdClass; } //some sensible defaults - if (!isset($options->para)){ + if (!isset($options->para)) { $options->para = false; } - if (!isset($options->newlines)){ + if (!isset($options->newlines)) { $options->newlines = false; } if (!isset($options->smiley)) { @@ -822,7 +810,7 @@ class flexible_table { * @deprecated 1.9.2 - Jun 2, 2008 */ function print_html() { - if(!$this->setup) { + if (!$this->setup) { return false; } $this->finish_html(); @@ -833,7 +821,7 @@ class flexible_table { * @return string initial of first name we are currently filtering by */ function get_initial_first() { - if(!$this->use_initials) { + if (!$this->use_initials) { return NULL; } @@ -845,7 +833,7 @@ class flexible_table { * @return string initial of last name we are currently filtering by */ function get_initial_last() { - if(!$this->use_initials) { + if (!$this->use_initials) { return NULL; } @@ -855,7 +843,7 @@ class flexible_table { /** * This function is not part of the public api. */ - function print_initials_bar(){ + function print_initials_bar() { if ((!empty($this->sess->i_last) || !empty($this->sess->i_first) || $this->use_initials) && isset($this->columns['fullname'])) { @@ -865,7 +853,7 @@ class flexible_table { // Bar of first initials echo '