Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'MDL-45725-master' of https://github.com/lucisgit/moodle
  • Loading branch information
danpoltawski committed Mar 16, 2015
2 parents 8f53787 + 60ec267 commit 6a24f0a
Show file tree
Hide file tree
Showing 3 changed files with 231 additions and 64 deletions.
163 changes: 99 additions & 64 deletions lib/tablelib.php
Expand Up @@ -62,10 +62,13 @@ class flexible_table {
var $column_nosort = array('userpic');
private $column_textsort = array();
var $setup = false;
var $sess = NULL;
var $baseurl = NULL;
var $request = array();

/**
* @var bool Whether or not to store table properties in the user_preferences table.
*/
private $persistent = false;
var $is_collapsible = false;
var $is_sortable = false;
var $use_pages = false;
Expand Down Expand Up @@ -116,6 +119,11 @@ class flexible_table {

var $exportclass = null;

/**
* @var array For storing user-customised table properties in the user_preferences db table.
*/
private $prefs = array();

/**
* Constructor
* @param int $uniqueid all tables have to have a unique id, this is used
Expand Down Expand Up @@ -193,6 +201,21 @@ function is_downloadable($downloadable = null) {
return $this->downloadable;
}

/**
* 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;
}

/**
* Where to show download buttons.
* @param array $showat array of postions in which to show download buttons.
Expand Down Expand Up @@ -405,85 +428,96 @@ function define_headers($headers) {
* @return type?
*/
function setup() {
global $SESSION, $CFG;
global $SESSION;

if (empty($this->columns) || empty($this->uniqueid)) {
return false;
}

if (!isset($SESSION->flextable)) {
$SESSION->flextable = array();
// 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];
}

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();
$SESSION->flextable[$this->uniqueid]->sortby = array();
$SESSION->flextable[$this->uniqueid]->i_first = '';
$SESSION->flextable[$this->uniqueid]->i_last = '';
$SESSION->flextable[$this->uniqueid]->textsort = $this->column_textsort;
if (!$this->prefs) {
$this->prefs = array(
'collapse' => array(),
'sortby' => array(),
'i_first' => '',
'i_last' => '',
'textsort' => $this->column_textsort,
);
}

$this->sess = &$SESSION->flextable[$this->uniqueid];
$oldprefs = $this->prefs;

if (($showcol = optional_param($this->request[TABLE_VAR_SHOW], '', PARAM_ALPHANUMEXT)) &&
isset($this->columns[$showcol])) {
$this->sess->collapse[$showcol] = false;
$this->prefs['collapse'][$showcol] = false;

} else if (($hidecol = optional_param($this->request[TABLE_VAR_HIDE], '', PARAM_ALPHANUMEXT)) &&
isset($this->columns[$hidecol])) {
$this->sess->collapse[$hidecol] = true;
if (array_key_exists($hidecol, $this->sess->sortby)) {
unset($this->sess->sortby[$hidecol]);
$this->prefs['collapse'][$hidecol] = true;
if (array_key_exists($hidecol, $this->prefs['sortby'])) {
unset($this->prefs['sortby'][$hidecol]);
}
}

// Now, update the column attributes for collapsed columns
foreach (array_keys($this->columns) as $column) {
if (!empty($this->sess->collapse[$column])) {
if (!empty($this->prefs['collapse'][$column])) {
$this->column_style[$column]['width'] = '10px';
}
}

if (($sortcol = optional_param($this->request[TABLE_VAR_SORT], '', PARAM_ALPHANUMEXT)) &&
$this->is_sortable($sortcol) && empty($this->sess->collapse[$sortcol]) &&
$this->is_sortable($sortcol) && empty($this->prefs['collapse'][$sortcol]) &&
(isset($this->columns[$sortcol]) || in_array($sortcol, get_all_user_name_fields())
&& isset($this->columns['fullname']))) {

if (array_key_exists($sortcol, $this->sess->sortby)) {
if (array_key_exists($sortcol, $this->prefs['sortby'])) {
// This key already exists somewhere. Change its sortorder and bring it to the top.
$sortorder = $this->sess->sortby[$sortcol] == SORT_ASC ? SORT_DESC : SORT_ASC;
unset($this->sess->sortby[$sortcol]);
$this->sess->sortby = array_merge(array($sortcol => $sortorder), $this->sess->sortby);
$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']);
} else {
// Key doesn't exist, so just add it to the beginning of the array, ascending order
$this->sess->sortby = array_merge(array($sortcol => SORT_ASC), $this->sess->sortby);
$this->prefs['sortby'] = array_merge(array($sortcol => SORT_ASC), $this->prefs['sortby']);
}

// Finally, make sure that no more than $this->maxsortkeys are present into the array
$this->sess->sortby = array_slice($this->sess->sortby, 0, $this->maxsortkeys);
$this->prefs['sortby'] = array_slice($this->prefs['sortby'], 0, $this->maxsortkeys);
}

// 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)) {
if (!array_key_exists($this->sort_default_column, $this->sess->sortby)) {
if (!array_key_exists($this->sort_default_column, $this->prefs['sortby'])) {
$defaultsort = array($this->sort_default_column => $this->sort_default_order);
$this->sess->sortby = array_merge($this->sess->sortby, $defaultsort);
$this->prefs['sortby'] = array_merge($this->prefs['sortby'], $defaultsort);
}
}

$ilast = optional_param($this->request[TABLE_VAR_ILAST], null, PARAM_RAW);
if (!is_null($ilast) && ($ilast ==='' || strpos(get_string('alphabet', 'langconfig'), $ilast) !== false)) {
$this->sess->i_last = $ilast;
$this->prefs['i_last'] = $ilast;
}

$ifirst = optional_param($this->request[TABLE_VAR_IFIRST], null, PARAM_RAW);
if (!is_null($ifirst) && ($ifirst === '' || strpos(get_string('alphabet', 'langconfig'), $ifirst) !== false)) {
$this->sess->i_first = $ifirst;
$this->prefs['i_first'] = $ifirst;
}

// 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);

if (empty($this->baseurl)) {
debugging('You should set baseurl when using flexible_table.');
global $PAGE;
Expand All @@ -504,25 +538,26 @@ function setup() {
}

/**
* Get the order by clause from the session, for the table with id $uniqueid.
* Get the order by clause from the session or user preferences, for the table with id $uniqueid.
* @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;
if (empty($SESSION->flextable[$uniqueid])) {
return '';
if (isset($SESSION->flextable[$uniqueid])) {
$prefs = $SESSION->flextable[$uniqueid];
} else if (!$prefs = json_decode(get_user_preferences('flextable_' . $uniqueid), true)) {
return '';
}

$sess = &$SESSION->flextable[$uniqueid];
if (empty($sess->sortby)) {
if (empty($prefs['sortby'])) {
return '';
}
if (empty($sess->textsort)) {
$sess->textsort = array();
if (empty($prefs['textsort'])) {
$prefs['textsort'] = array();
}

return self::construct_order_by($sess->sortby, $sess->textsort);
return self::construct_order_by($prefs['sortby'], $prefs['textsort']);
}

/**
Expand Down Expand Up @@ -564,11 +599,11 @@ public function get_sort_columns() {
throw new coding_exception('Cannot call get_sort_columns until you have called setup.');
}

if (empty($this->sess->sortby)) {
if (empty($this->prefs['sortby'])) {
return array();
}

foreach ($this->sess->sortby as $column => $notused) {
foreach ($this->prefs['sortby'] as $column => $notused) {
if (isset($this->columns[$column])) {
continue; // This column is OK.
}
Expand All @@ -577,10 +612,10 @@ public function get_sort_columns() {
continue; // This column is OK.
}
// This column is not OK.
unset($this->sess->sortby[$column]);
unset($this->prefs['sortby'][$column]);
}

return $this->sess->sortby;
return $this->prefs['sortby'];
}

/**
Expand Down Expand Up @@ -616,13 +651,13 @@ function get_sql_where() {
static $i = 0;
$i++;

if (!empty($this->sess->i_first)) {
if (!empty($this->prefs['i_first'])) {
$conditions[] = $DB->sql_like('firstname', ':ifirstc'.$i, false, false);
$params['ifirstc'.$i] = $this->sess->i_first.'%';
$params['ifirstc'.$i] = $this->prefs['i_first'].'%';
}
if (!empty($this->sess->i_last)) {
if (!empty($this->prefs['i_last'])) {
$conditions[] = $DB->sql_like('lastname', ':ilastc'.$i, false, false);
$params['ilastc'.$i] = $this->sess->i_last.'%';
$params['ilastc'.$i] = $this->prefs['i_last'].'%';
}
}

Expand Down Expand Up @@ -859,7 +894,7 @@ function get_initial_first() {
return NULL;
}

return $this->sess->i_first;
return $this->prefs['i_first'];
}

/**
Expand All @@ -871,7 +906,7 @@ function get_initial_last() {
return NULL;
}

return $this->sess->i_last;
return $this->prefs['i_last'];
}

/**
Expand Down Expand Up @@ -906,23 +941,23 @@ protected function print_one_initials_bar($alpha, $current, $class, $title, $url
* This function is not part of the public api.
*/
function print_initials_bar() {
if ((!empty($this->sess->i_last) || !empty($this->sess->i_first) ||$this->use_initials)
if ((!empty($this->prefs['i_last']) || !empty($this->prefs['i_first']) ||$this->use_initials)
&& isset($this->columns['fullname'])) {

$alpha = explode(',', get_string('alphabet', 'langconfig'));

// Bar of first initials
if (!empty($this->sess->i_first)) {
$ifirst = $this->sess->i_first;
if (!empty($this->prefs['i_first'])) {
$ifirst = $this->prefs['i_first'];
} else {
$ifirst = '';
}
$this->print_one_initials_bar($alpha, $ifirst, 'firstinitial',
get_string('firstname'), $this->request[TABLE_VAR_IFIRST]);

// Bar of last initials
if (!empty($this->sess->i_last)) {
$ilast = $this->sess->i_last;
if (!empty($this->prefs['i_last'])) {
$ilast = $this->prefs['i_last'];
} else {
$ilast = '';
}
Expand Down Expand Up @@ -1053,7 +1088,7 @@ public function get_row_html($row, $classname = '') {
foreach ($row as $index => $data) {
$column = $colbyindex[$index];

if (empty($this->sess->collapse[$column])) {
if (empty($this->prefs['collapse'][$column])) {
if ($this->column_suppress[$column] && $suppress_lastrow !== NULL && $suppress_lastrow[$index] === $data) {
$content = ' ';
} else {
Expand Down Expand Up @@ -1135,7 +1170,7 @@ protected function show_hide_link($column, $index) {

$ariacontrols = trim($ariacontrols);

if (!empty($this->sess->collapse[$column])) {
if (!empty($this->prefs['collapse'][$column])) {
$linkattributes = array('title' => get_string('show') . ' ' . strip_tags($this->headers[$index]),
'aria-expanded' => 'false',
'aria-controls' => $ariacontrols);
Expand Down Expand Up @@ -1168,11 +1203,11 @@ function print_headers() {
$icon_hide = $this->show_hide_link($column, $index);
}

$primary_sort_column = '';
$primary_sort_order = '';
if (reset($this->sess->sortby)) {
$primary_sort_column = key($this->sess->sortby);
$primary_sort_order = current($this->sess->sortby);
$primarysortcolumn = '';
$primarysortorder = '';
if (reset($this->prefs['sortby'])) {
$primarysortcolumn = key($this->prefs['sortby']);
$primarysortorder = current($this->prefs['sortby']);
}

switch ($column) {
Expand All @@ -1191,7 +1226,7 @@ function print_headers() {
$this->headers[$index] = '';
foreach ($requirednames as $name) {
$sortname = $this->sort_link(get_string($name),
$name, $primary_sort_column === $name, $primary_sort_order);
$name, $primarysortcolumn === $name, $primarysortorder);
$this->headers[$index] .= $sortname . ' / ';
}
$this->headers[$index] = substr($this->headers[$index], 0, -3);
Expand All @@ -1206,7 +1241,7 @@ function print_headers() {
default:
if ($this->is_sortable($column)) {
$this->headers[$index] = $this->sort_link($this->headers[$index],
$column, $primary_sort_column == $column, $primary_sort_order);
$column, $primarysortcolumn == $column, $primarysortorder);
}
}

Expand All @@ -1216,7 +1251,7 @@ function print_headers() {
);
if ($this->headers[$index] === NULL) {
$content = ' ';
} else if (!empty($this->sess->collapse[$column])) {
} else if (!empty($this->prefs['collapse'][$column])) {
$content = $icon_hide;
} else {
if (is_array($this->column_style[$column])) {
Expand Down

0 comments on commit 6a24f0a

Please sign in to comment.