From 5d1812500b411a289dc16e4904628d43cca3d2c0 Mon Sep 17 00:00:00 2001 From: Irina Hoppe Date: Thu, 1 Aug 2024 10:52:04 +0200 Subject: [PATCH] fix sql syntax --- locallib.php | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/locallib.php b/locallib.php index a94be369..96951aec 100644 --- a/locallib.php +++ b/locallib.php @@ -1357,12 +1357,15 @@ public function get_number_of_active_raters() { $raters = array_map(function($rater) { return $rater->id; }, $this->get_raters_in_course()); + $sql = 'SELECT COUNT(DISTINCT ra_ratings.userid) AS number FROM {ratingallocate} ra INNER JOIN {ratingallocate_choices} ra_choices ON ra.id = ra_choices.ratingallocateid INNER JOIN {ratingallocate_ratings} ra_ratings ON ra_choices.id = ra_ratings.choiceid - WHERE ra.course = :courseid AND ra.id = :ratingallocateid - AND ra_ratings.userid IN ( ' . implode(',', $raters) . ' )'; + WHERE ra.course = :courseid AND ra.id = :ratingallocateid'; + if (!empty($raters)) { + $sql .= ' AND ra_ratings.userid IN ( ' . implode(',', $raters) . ' )'; + } $numberofratersfromdb = $this->db->get_field_sql($sql, [ 'courseid' => $this->course->id, 'ratingallocateid' => $this->ratingallocateid]); return (int) $numberofratersfromdb; @@ -1580,13 +1583,19 @@ public function get_choices_with_allocationcount() { $raters = array_map(function($rater) { return $rater->id; }, $this->get_raters_in_course()); + + $validrater = ''; + if (!empty($raters)) { + $validrater .= 'AND userid IN ( ' . implode(',', $raters) . ' )'; + } + $sql = 'SELECT c.*, al.usercount FROM {ratingallocate_choices} c LEFT JOIN ( SELECT choiceid, count( userid ) AS usercount FROM {ratingallocate_allocations} WHERE ratingallocateid =:ratingallocateid1 - AND userid IN ( ' . implode(',', $raters) . ' ) + ' . $validrater .' GROUP BY choiceid ) AS al ON c.id = al.choiceid WHERE c.ratingallocateid =:ratingallocateid and c.active = :active'; @@ -1613,8 +1622,10 @@ public function get_allocations() { FROM {ratingallocate_allocations} al LEFT JOIN {ratingallocate_choices} c ON al.choiceid = c.id LEFT JOIN {ratingallocate_ratings} r ON al.choiceid = r.choiceid AND al.userid = r.userid - WHERE al.ratingallocateid = :ratingallocateid AND c.active = 1 - AND al.userid IN ( ' . implode(',', $raters) . ' )'; + WHERE al.ratingallocateid = :ratingallocateid AND c.active = 1'; + if (!empty($raters)) { + $query .= ' AND al.userid IN ( ' . implode(',', $raters) . ' )'; + } $records = $this->db->get_records_sql($query, [ 'ratingallocateid' => $this->ratingallocateid, ]);