Skip to content

Commit

Permalink
Merge pull request #298 from learnweb/fix/dont_show_unenrolled_in_sta…
Browse files Browse the repository at this point in the history
…tistics

fix/only show enrolled users with ratings in the allocations_table
  • Loading branch information
Laur0r authored Aug 1, 2024
2 parents 41670f1 + 5d18125 commit fe1947a
Showing 1 changed file with 36 additions and 2 deletions.
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

0 comments on commit fe1947a

Please sign in to comment.