From d7f687c16f8678764d6afe6ce6413f31c64280f2 Mon Sep 17 00:00:00 2001 From: Irina Hoppe Date: Mon, 15 Apr 2024 10:51:28 +0200 Subject: [PATCH 1/6] only show enrolled users with ratings in the allocations_table --- locallib.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/locallib.php b/locallib.php index 615d0e70..96e2bf82 100644 --- a/locallib.php +++ b/locallib.php @@ -1619,18 +1619,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() { + $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 ( :ratersincourse )"; + return $this->db->get_records_sql($sql, array( - 'ratingallocateid' => $this->ratingallocateid + 'ratingallocateid' => $this->ratingallocateid, + 'ratersincourse' => implode( + " , ", + array_map(function ($rater) { + return $rater->id; + }, $this-> get_raters_in_course()) + ) )); + } /** From 41bf92a0c1cb4b6e077010a2ec9475074f0d6b79 Mon Sep 17 00:00:00 2001 From: Irina Hoppe Date: Mon, 15 Apr 2024 10:58:33 +0200 Subject: [PATCH 2/6] codestyle fixes --- locallib.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/locallib.php b/locallib.php index 96e2bf82..e74d860f 100644 --- a/locallib.php +++ b/locallib.php @@ -1634,9 +1634,11 @@ public function get_users_with_ratings() { 'ratingallocateid' => $this->ratingallocateid, 'ratersincourse' => implode( " , ", - array_map(function ($rater) { - return $rater->id; - }, $this-> get_raters_in_course()) + array_map( + function ($rater) { + return $rater->id; + }, + $this->get_raters_in_course()) ) )); From 6085cb3ae2dfd0e07f9297f993a64e7507a20593 Mon Sep 17 00:00:00 2001 From: Irina Hoppe Date: Tue, 30 Jul 2024 10:40:52 +0200 Subject: [PATCH 3/6] modify sql for returning users with ratings --- locallib.php | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/locallib.php b/locallib.php index 84125b79..34b474e7 100644 --- a/locallib.php +++ b/locallib.php @@ -1738,22 +1738,20 @@ public function get_rating_data_for_user($userid) { */ 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 AND r.userid IN ( :ratersincourse )"; + WHERE c.ratingallocateid = :ratingallocateid AND c.active = 1 AND r.userid IN (" . implode(",", $raters) . ") "; return $this->db->get_records_sql($sql, [ 'ratingallocateid' => $this->ratingallocateid, - 'ratersincourse' => implode( - " , ", - array_map( - function ($rater) { - return $rater->id; - }, - $this->get_raters_in_course()) - ), ]); } From 89fbca375ba5f62bc289dfe4717cd444ac0555ec Mon Sep 17 00:00:00 2001 From: Irina Hoppe Date: Tue, 30 Jul 2024 14:11:15 +0200 Subject: [PATCH 4/6] show correct usercount in allocation statistics --- locallib.php | 26 +++++++++++++++++++++++++- renderer.php | 2 +- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/locallib.php b/locallib.php index 34b474e7..ad8bf064 100644 --- a/locallib.php +++ b/locallib.php @@ -1354,11 +1354,15 @@ 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'; + WHERE ra.course = :courseid AND ra.id = :ratingallocateid + AND ra_ratings.userid IN ( ' . implode(',', $raters) . ' )'; $numberofratersfromdb = $this->db->get_field_sql($sql, [ 'courseid' => $this->course->id, 'ratingallocateid' => $this->ratingallocateid]); return (int) $numberofratersfromdb; @@ -1607,6 +1611,26 @@ public function get_allocations() { return $records; } + /** + * Returns the allocation for each ENROLLED user. The keys of the returned array contain the userids. + * @return array all allocation objects that belong this ratingallocate + */ + public function get_valid_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 + AND al.userid IN ( ' . implode(',', $raters) . ' )'; + $records = $this->db->get_records_sql($query, [ + 'ratingallocateid' => $this->ratingallocateid, + ]); + return $records; + } + /** * Removes all allocations for choices in $ratingallocateid */ diff --git a/renderer.php b/renderer.php index 777c3bcc..e902d9d5 100644 --- a/renderer.php +++ b/renderer.php @@ -800,7 +800,7 @@ public function statistics_table_for_ratingallocate(ratingallocate $ratingalloca // Count the number of allocations with a specific rating. $distributiondata = []; - $memberships = $ratingallocate->get_allocations(); + $memberships = $ratingallocate->get_valid_allocations(); foreach ($memberships as $id => $membership) { $rating = $membership->rating; From dc23117085cda5a8879a6daebf63361b339a2418 Mon Sep 17 00:00:00 2001 From: Irina Hoppe Date: Thu, 1 Aug 2024 09:36:20 +0200 Subject: [PATCH 5/6] show correct usercount in summary row of ratings_and_allocations_table --- locallib.php | 22 ++++++---------------- renderer.php | 2 +- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/locallib.php b/locallib.php index ad8bf064..a94be369 100644 --- a/locallib.php +++ b/locallib.php @@ -1577,12 +1577,16 @@ 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()); $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) . ' ) GROUP BY choiceid ) AS al ON c.id = al.choiceid WHERE c.ratingallocateid =:ratingallocateid and c.active = :active'; @@ -1600,25 +1604,11 @@ public function get_choices_with_allocationcount() { * @return array all allocation objects that belong this ratingallocate */ public function get_allocations() { - $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'; - $records = $this->db->get_records_sql($query, [ - 'ratingallocateid' => $this->ratingallocateid, - ]); - return $records; - } - /** - * Returns the allocation for each ENROLLED user. The keys of the returned array contain the userids. - * @return array all allocation objects that belong this ratingallocate - */ - public function get_valid_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 @@ -1626,7 +1616,7 @@ public function get_valid_allocations() { WHERE al.ratingallocateid = :ratingallocateid AND c.active = 1 AND al.userid IN ( ' . implode(',', $raters) . ' )'; $records = $this->db->get_records_sql($query, [ - 'ratingallocateid' => $this->ratingallocateid, + 'ratingallocateid' => $this->ratingallocateid, ]); return $records; } diff --git a/renderer.php b/renderer.php index e902d9d5..777c3bcc 100644 --- a/renderer.php +++ b/renderer.php @@ -800,7 +800,7 @@ public function statistics_table_for_ratingallocate(ratingallocate $ratingalloca // Count the number of allocations with a specific rating. $distributiondata = []; - $memberships = $ratingallocate->get_valid_allocations(); + $memberships = $ratingallocate->get_allocations(); foreach ($memberships as $id => $membership) { $rating = $membership->rating; From 5d1812500b411a289dc16e4904628d43cca3d2c0 Mon Sep 17 00:00:00 2001 From: Irina Hoppe Date: Thu, 1 Aug 2024 10:52:04 +0200 Subject: [PATCH 6/6] 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, ]);