diff --git a/lang/en_utf8/forum.php b/lang/en_utf8/forum.php index 6d9aa381a43..76b729074a9 100644 --- a/lang/en_utf8/forum.php +++ b/lang/en_utf8/forum.php @@ -165,6 +165,7 @@ $string['nopostforum'] = 'Sorry, you are not allowed to post to this forum'; $string['noposts'] = 'No posts'; $string['nopostscontaining'] = 'No posts containing \'$a\' were found'; $string['noquestions'] = 'There are no questions yet in this forum'; +$string['noratinggiven'] = 'No rating given'; $string['nosubscribers'] = 'There are no subscribers yet for this forum'; $string['nothingnew'] = 'Nothing new for $a'; $string['notingroup'] = 'Sorry, but you need to be part of a group to see this forum.'; diff --git a/mod/forum/lib.php b/mod/forum/lib.php index dc27f1d40b5..39da86735b3 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -3373,8 +3373,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) { @@ -3388,12 +3397,23 @@ function forum_get_ratings_count($postid, $scale, $ratings=NULL) { } $count = count($ratings); - $scalecount = count($scale)-1; //this should give us the last element of the scale aka the max grade with $scale[$scalecount] + $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 > $scale[$scalecount]) { //if the count exceeds the forum scale (i.e. max grade then set the score to the max grade - $count = $scale[$scalecount]; + 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]; } - return $scale[$count]; } /**