Skip to content

Commit

Permalink
tablelib: MDL-22011 refactor flexible_table::get_sql_sort into severa…
Browse files Browse the repository at this point in the history
…l smaller methods.

Also, change assignemnt to use a separate static method, rather than overloading get_sql_sort.
  • Loading branch information
timhunt committed Apr 1, 2010
1 parent 62cebcc commit 9baf267
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 28 deletions.
78 changes: 51 additions & 27 deletions lib/tablelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -530,43 +530,67 @@ function setup() {
} else if (!in_array('flexible', explode(' ', $this->attributes['class']))) {
$this->attributes['class'] = trim('flexible ' . $this->attributes['class']);
}
}

/**
* 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.
*/
public static function get_sort_for_table($uniqueid) {
global $SESSION;
if(empty($SESSION->flextable[$uniqueid])) {
return '';
}

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

return self::construct_order_by($sess->sortby);
}

/**
* @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.
* 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.
*/
function get_sql_sort($uniqueid = NULL) {
if($uniqueid === NULL) {
// "Non-static" function call
if(!$this->setup) {
return false;
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';
}
$sess = &$this->sess;
}
else {
// "Static" function call
global $SESSION;
if(empty($SESSION->flextable[$uniqueid])) {
return '';
}
$sess = &$SESSION->flextable[$uniqueid];

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($sess->sortby)) {
$sortstring = '';
foreach($sess->sortby as $column => $order) {
if(!empty($sortstring)) {
$sortstring .= ', ';
}
$sortstring .= $column.($order == SORT_ASC ? ' ASC' : ' DESC');
}
return $sortstring;
if (empty($this->sess->sortby)) {
return array();
}
return '';

return $this->sess->sortby;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion mod/assignment/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ function display_submission($extra_javascript = '') {
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.' ';
}

Expand Down

0 comments on commit 9baf267

Please sign in to comment.