Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/only show enrolled users with ratings in the allocations_table #298

Merged
merged 7 commits into from
Aug 1, 2024
38 changes: 36 additions & 2 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1354,11 +1354,18 @@ public function handle_view() {
* @return int
*/
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';
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 @@ -1573,12 +1580,22 @@ public function get_ratings_for_rateable_choices_for_raters_without_alloc() {
* @throws dml_exception
*/
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
' . $validrater .'
GROUP BY choiceid
) AS al ON c.id = al.choiceid
WHERE c.ratingallocateid =:ratingallocateid and c.active = :active';
Expand All @@ -1596,11 +1613,19 @@ public function get_choices_with_allocationcount() {
* @return array all allocation objects that belong this ratingallocate
*/
public function get_allocations() {

$raters = array_map(function($rater) {
return $rater->id;
}, $this->get_raters_in_course());

$query = 'SELECT al.userid, al.*, r.rating
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';
if (!empty($raters)) {
$query .= ' AND al.userid IN ( ' . implode(',', $raters) . ' )';
}
$records = $this->db->get_records_sql($query, [
'ratingallocateid' => $this->ratingallocateid,
]);
Expand Down Expand Up @@ -1733,18 +1758,27 @@ public function get_rating_data_for_user($userid) {
}

/**
* Returns all ids of users who handed in a rating to any choice of the instance.
* Returns all ids of users in this course who handed in a rating to any choice of the instance.
* @return array of userids
*/
public function get_users_with_ratings() {

$raters = array_map(
function ($rater) {
return $rater->id;
},
$this->get_raters_in_course());

$sql = "SELECT DISTINCT r.userid
FROM {ratingallocate_choices} c
JOIN {ratingallocate_ratings} r
ON c.id = r.choiceid
WHERE c.ratingallocateid = :ratingallocateid AND c.active = 1";
WHERE c.ratingallocateid = :ratingallocateid AND c.active = 1 AND r.userid IN (" . implode(",", $raters) . ") ";

return $this->db->get_records_sql($sql, [
'ratingallocateid' => $this->ratingallocateid,
]);

}

/**
Expand Down
Loading