Skip to content

Commit

Permalink
fix sql syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
irinahpe committed Aug 1, 2024
1 parent dc23117 commit 5d18125
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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';
Expand All @@ -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,
]);
Expand Down

0 comments on commit 5d18125

Please sign in to comment.