Skip to content

Commit 7c14011

Browse files
committed
Adding count_only query to LLMS_Query_Quiz_Attempt.
1 parent d017ac9 commit 7c14011

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

includes/abstracts/abstract.llms.database.query.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,13 @@ protected function sql_limit() {
207207

208208
global $wpdb;
209209

210+
$sql = '';
211+
212+
// No limit or per-page when doing a count-only query.
213+
if ( $this->get( 'count_only' ) ) {
214+
return $sql;
215+
}
216+
210217
$sql = $wpdb->prepare( 'LIMIT %d, %d', $this->get_skip(), $this->get( 'per_page' ) );
211218

212219
/**

includes/admin/reporting/tables/llms.table.quizzes.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ protected function get_data( $key, $data ) {
163163
case 'attempts':
164164
$query = new LLMS_Query_Quiz_Attempt(
165165
array(
166-
'quiz_id' => $quiz->get( 'id' ),
167-
'per_page' => 1,
166+
'quiz_id' => $quiz->get( 'id' ),
167+
'count_only' => true,
168168
)
169169
);
170170

@@ -175,7 +175,7 @@ protected function get_data( $key, $data ) {
175175
'quiz_id' => $quiz->get( 'id' ),
176176
)
177177
);
178-
$value = '<a href="' . $url . '">' . $query->get_found_results() . '</a>';
178+
$value = '<a href="' . $url . '">' . $query->get_count_only_result() . '</a>';
179179

180180
break;
181181

includes/class.llms.query.quiz.attempt.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,12 @@ protected function prepare_query() {
145145
global $wpdb;
146146

147147
$select = 'SELECT SQL_CALC_FOUND_ROWS qa.id';
148-
$from = "FROM {$wpdb->prefix}lifterlms_quiz_attempts qa";
149-
$joins = $this->sql_joins();
148+
if ( $this->get( 'count_only' ) ) {
149+
$select = 'SELECT COUNT(*) AS total';
150+
}
151+
152+
$from = "FROM {$wpdb->prefix}lifterlms_quiz_attempts qa";
153+
$joins = $this->sql_joins();
150154

151155
return "{$select} {$from} {$joins} {$this->sql_where()} {$this->sql_orderby()} {$this->sql_limit()};";
152156
}

0 commit comments

Comments
 (0)