Skip to content

Commit

Permalink
MDL-76155 qbank_comment: Move comment count queries to a JOIN
Browse files Browse the repository at this point in the history
  • Loading branch information
marxjohnson committed Nov 29, 2024
1 parent 9195db9 commit f753096
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions question/bank/comment/classes/comment_count_column.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,7 @@ public function get_title(): string {
* @param string $rowclasses Classes that can be added.
*/
protected function display_content($question, $rowclasses): void {
global $DB;
$syscontext = \context_system::instance();
$args = [
'component' => 'qbank_comment',
'commentarea' => 'question',
'itemid' => $question->id,
'contextid' => $syscontext->id,
];
$commentcount = $DB->count_records('comments', $args);
$attributes = [];
if (question_has_capability_on($question, 'comment')) {
$target = 'questioncommentpreview_' . $question->id;
Expand All @@ -103,7 +95,27 @@ protected function display_content($question, $rowclasses): void {
'data-contextid' => $syscontext->id,
];
}
echo \html_writer::tag('a', $commentcount, $attributes);
echo \html_writer::tag('a', $question->commentcount ?? 0, $attributes);
}

public function get_extra_joins(): array {
$syscontext = \context_system::instance();
$subquery = "
SELECT COUNT('x') AS commentcount, itemid AS questionid
FROM {comments} c
WHERE c.component = 'qbank_comment'
AND commentarea = 'question'
AND contextid = {$syscontext->id}
GROUP BY c.itemid
";

return [
'comments' => "LEFT JOIN ({$subquery}) comments ON comments.questionid = q.id"
];
}

public function get_required_fields(): array {
return ['comments.commentcount AS commentcount'];
}

public function get_extra_classes(): array {
Expand Down

0 comments on commit f753096

Please sign in to comment.