Skip to content

Commit

Permalink
Fix ability to sort course students table by completed date.
Browse files Browse the repository at this point in the history
  • Loading branch information
pondermatic committed Feb 22, 2022
1 parent 064019b commit fb592ea
Show file tree
Hide file tree
Showing 4 changed files with 354 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changelogs/1969-sort-course-students-by-completed.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
significance: patch
type: fixed
links:
- "#1969"
entry: Fixed ability to sort course students table by completed date.
22 changes: 16 additions & 6 deletions includes/admin/reporting/tables/llms.table.course.students.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @package LifterLMS/Admin/Reporting/Tables/Classes
*
* @since 3.2.0
* @version 3.18.0
* @version [version]
*/

defined( 'ABSPATH' ) || exit;
Expand Down Expand Up @@ -267,12 +267,13 @@ public function get_table_search_form_placeholder() {
}

/**
* Execute a query to retrieve results from the table
* Execute a query to retrieve results from the table.
*
* @param array $args array of query args
* @return void
* @since 3.15.0
* @version 3.15.0
* @since 3.15.0
* @since [version] Add ability to sort by completion date.
*
* @param array $args Array of query args.
* @return void
*/
public function get_results( $args = array() ) {

Expand All @@ -299,6 +300,15 @@ public function get_results( $args = array() ) {
$sort = array();
switch ( $this->get_orderby() ) {

case 'completed':
$sort = array(
'completed' => $this->get_order(),
'last_name' => 'ASC',
'first_name' => 'ASC',
'id' => 'ASC',
);
break;

case 'enrolled':
$sort = array(
'date' => $this->get_order(),
Expand Down
18 changes: 10 additions & 8 deletions includes/class.llms.student.query.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @package LifterLMS/Classes
*
* @since 3.4.0
* @version 4.10.2
* @version [version]
*/

defined( 'ABSPATH' ) || exit;
Expand Down Expand Up @@ -337,11 +337,12 @@ private function sql_search() {
}

/**
* Set up the SQL for the select statement
* Set up the SQL for the select statement.
*
* @since 3.13.0
* @since 4.10.2 Drop usage of `this->get_filter( 'select' )` in favor of `'llms_student_query_select'`.
* Use `$this->sql_select_columns({columns})` to determine additional columns to select.
* @since [version] Add a subquery for completed date.
*
* @return string
*/
Expand All @@ -357,6 +358,7 @@ private function sql_select() {

// All the possible fields.
$fields = array(
'completed' => "( {$this->sql_subquery( 'updated_date', '_is_complete' )} ) AS completed",
'date' => "( {$this->sql_subquery( 'updated_date' )} ) AS `date`",
'last_name' => 'm_last.meta_value AS last_name',
'first_name' => 'm_first.meta_value AS first_name',
Expand Down Expand Up @@ -417,16 +419,18 @@ private function sql_status_in( $column = 'status' ) {
}

/**
* Generate an SQL subquery for the dynamic status or date values in the main query
* Generate an SQL subquery for the dynamic status or date values in the main query.
*
* @since 3.13.0
* @since [version] Add `$meta_key` argument.
*
* @param string $column Column name.
* @param string $meta_key Optional meta key to use in the WHERE condition. Defaults to '_status'.
* @return string
*/
private function sql_subquery( $column ) {
private function sql_subquery( $column, $meta_key = '_status' ) {

$and = '';
global $wpdb;

$post_ids = $this->get( 'post_id' );
if ( $post_ids ) {
Expand All @@ -436,11 +440,9 @@ private function sql_subquery( $column ) {
$and = "AND {$this->sql_status_in( 'meta_value' )}";
}

global $wpdb;

return "SELECT {$column}
FROM {$wpdb->prefix}lifterlms_user_postmeta
WHERE meta_key = '_status'
WHERE meta_key = '{$meta_key}'
AND user_id = id
{$and}
ORDER BY updated_date DESC
Expand Down
Loading

0 comments on commit fb592ea

Please sign in to comment.