From: mudrd8mz Date: Thu, 29 Jan 2009 12:40:24 +0000 (+0000) Subject: MDL-13449 Fix the problem with displaying aggregation type "Count of ratings". Merged... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=8222c59e6c70e9a55fac8c571abc0158a8393678;p=moodle.git MDL-13449 Fix the problem with displaying aggregation type "Count of ratings". Merged from MOODLE_19_STABLE --- diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 1bbb3687eb..d45365f4d3 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -3588,8 +3588,17 @@ function forum_get_ratings_mean($postid, $scale, $ratings=NULL) { /** * Return the count of the ratings of a post given to the current user by others. - * Scale is an array of possible ratings in the scale - the end of the scale is the highest or max grade - * Ratings is an optional simple array of actual ratings (just integers) + * + * For numerical grades, the scale index is the same as the real grade value from interval {0..n} + * and $scale looks like Array( 0 => '0/n', 1 => '1/n', ..., n => 'n/n' ) + * + * For scales, the index is the order of the scale item {1..n} + * and $scale looks like Array( 1 => 'poor', 2 => 'weak', 3 => 'good' ) + * In case of no ratings done yet, we have nothing to display. + * + * @param int $postid + * @param array $scale Possible ratings in the scale - the end of the scale is the highest or max grade + * @param array $ratings An optional simple array of actual ratings (just integers) */ function forum_get_ratings_count($postid, $scale, $ratings=NULL) { global $DB; @@ -3602,7 +3611,24 @@ function forum_get_ratings_count($postid, $scale, $ratings=NULL) { } } - return count($ratings); + $count = count($ratings); + $maxgradeidx = max(array_keys($scale)); // For numerical grades, the index is the same as the real grade value {0..n} + // and $scale looks like Array( 0 => '0/n', 1 => '1/n', ..., n => 'n/n' ) + // For scales, the index is the order of the scale item {1..n} + // and $scale looks like Array( 1 => 'poor', 2 => 'weak', 3 => 'good' ) + if (! array_key_exists(0, $scale)) { + $scaleused = true; + } else { + $scaleused = false; + } + + if (($count == 0) && ($scaleused)) { // If no rating given yet and we use a scale + return get_string('noratinggiven', 'forum'); + } elseif ($count > $maxgradeidx) { // The count exceeds the max grade + return $scale[$maxgradeidx]; + } else { // Display the grade, eg. '3/10' or 'weak' + return $scale[$count]; + } } /**