tablelib: MDL-22011 refactor flexible_table::get_sql_sort into several smaller methods.

Also, change assignemnt to use a separate static method, rather than overloading get_sql_sort.
This commit is contained in:
Tim Hunt 2010-04-01 13:09:24 +00:00
parent 62cebcc7a3
commit 9baf267016
2 changed files with 55 additions and 31 deletions

View File

@ -530,43 +530,67 @@ class flexible_table {
} else if (!in_array('flexible', explode(' ', $this->attributes['class']))) {
$this->attributes['class'] = trim('flexible ' . $this->attributes['class']);
}
}
/**
* @param string $uniqueid used to identify the table you want the sql sort
* string for when method is called as a static method.
* @return string sql to put after ORDER BY or empty string if there is
* none.
* Get the order by clause from the session, 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.
*/
function get_sql_sort($uniqueid = NULL) {
if($uniqueid === NULL) {
// "Non-static" function call
if(!$this->setup) {
return false;
}
$sess = &$this->sess;
}
else {
// "Static" function call
global $SESSION;
if(empty($SESSION->flextable[$uniqueid])) {
return '';
}
$sess = &$SESSION->flextable[$uniqueid];
public static function get_sort_for_table($uniqueid) {
global $SESSION;
if(empty($SESSION->flextable[$uniqueid])) {
return '';
}
if(!empty($sess->sortby)) {
$sortstring = '';
foreach($sess->sortby as $column => $order) {
if(!empty($sortstring)) {
$sortstring .= ', ';
}
$sortstring .= $column.($order == SORT_ASC ? ' ASC' : ' DESC');
}
return $sortstring;
$sess = &$SESSION->flextable[$uniqueid];
if (empty($sess->sortby)) {
return '';
}
return '';
return self::construct_order_by($sess->sortby);
}
/**
* 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.
*/
public static function construct_order_by($cols) {
$bits = array();
foreach($cols as $column => $order) {
if ($order == SORT_ASC) {
$bits[] = $column . ' ASC';
} else {
$bits[] = $column . ' DESC';
}
}
return implode(', ', $bits);
}
/**
* @return SQL fragment that can be used in an ORDER BY clause.
*/
public function get_sql_sort() {
return self::construct_order_by($this->get_sort_columns());
}
/**
* 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.');
}
if (empty($this->sess->sortby)) {
return array();
}
return $this->sess->sortby;
}
/**

View File

@ -909,7 +909,7 @@ class assignment_base {
AND s.assignment = '.$this->assignment->id.' '.
'WHERE u.id IN ('.implode(',', $users).') ';
if ($sort = flexible_table::get_sql_sort('mod-assignment-submissions')) {
if ($sort = flexible_table::get_sort_for_table('mod-assignment-submissions')) {
$sort = 'ORDER BY '.$sort.' ';
}