rating MDL-26461 fixed an off by 1 bug that made the safeguard against too high ratin...
[moodle.git] / rating / index.php
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
18 /**
19  * A page to display a list of ratings for a given item (forum post etc)
20  *
21  * @package    core
22  * @subpackage rating
23  * @copyright  2010 Andrew Davis
24  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  */
27 require_once("../config.php");
28 require_once("lib.php");
30 $contextid   = required_param('contextid', PARAM_INT);
31 $itemid   = required_param('itemid', PARAM_INT);
32 $scaleid   = required_param('scaleid', PARAM_INT);
33 $sort = optional_param('sort', '', PARAM_ALPHA);
34 $popup = optional_param('popup', 0, PARAM_INT);//==1 if in a popup window?
36 list($context, $course, $cm) = get_context_info_array($contextid);
37 require_login($course, false, $cm);
39 $url = new moodle_url('/rating/index.php', array('contextid'=>$contextid,'itemid'=>$itemid,'scaleid'=>$scaleid));
40 if ($sort !== 0) {
41     $url->param('sort', $sort);
42 }
43 $PAGE->set_url($url);
44 $PAGE->set_context($context);
46 if ($popup) {
47     $PAGE->set_pagelayout('popup');
48 }
50 if (!has_capability('moodle/rating:view',$context)) {
51     print_error('noviewrate', 'rating');
52 }
53 if (!has_capability('moodle/rating:viewall',$context) and $USER->id != $item->userid) {
54     print_error('noviewanyrate', 'rating');
55 }
57 switch ($sort) {
58     case 'firstname': $sqlsort = "u.firstname ASC"; break;
59     case 'rating':    $sqlsort = "r.rating ASC"; break;
60     default:          $sqlsort = "r.timemodified ASC";
61 }
63 $scalemenu = make_grades_menu($scaleid);
65 $strrating  = get_string('rating', 'rating');
66 $strname    = get_string('name');
67 $strtime    = get_string('time');
69 $PAGE->set_title(get_string('allratingsforitem','rating'));
70 echo $OUTPUT->header();
72 $ratingoptions = new stdclass();
73 $ratingoptions->context = $context;
74 $ratingoptions->itemid = $itemid;
75 $ratingoptions->sort = $sqlsort;
77 $rm = new rating_manager();
78 $ratings = $rm->get_all_ratings_for_item($ratingoptions);
79 if (!$ratings) {
80     $msg = get_string('noratings','rating');
81     echo html_writer::tag('div', $msg, array('class'=>'mdl-align'));
82 } else {
83     $sortargs = "contextid=$contextid&amp;itemid=$itemid&amp;scaleid=$scaleid";
84     if($popup) {
85         $sortargs.="&amp;popup=$popup";
86     }
87     echo "<table border=\"0\" cellpadding=\"3\" cellspacing=\"3\" class=\"generalbox\" style=\"width:100%\">";
88     echo "<tr>";
89     echo "<th class=\"header\" scope=\"col\">&nbsp;</th>";
90     echo "<th class=\"header\" scope=\"col\"><a href=\"index.php?$sortargs&amp;sort=firstname\">$strname</a></th>";
91     echo "<th class=\"header\" scope=\"col\" style=\"width:100%\"><a href=\"index.php?$sortargs&amp;sort=rating\">$strrating</a></th>";
92     echo "<th class=\"header\" scope=\"col\"><a href=\"index.php?$sortargs&amp;sort=time\">$strtime</a></th>";
93     echo "</tr>";
95     //if the scale was changed after ratings were submitted some ratings may have a value above the current maximum
96     $maxrating = count($scalemenu) - 1;
97     foreach ($ratings as $rating) {
98         //Undo the aliasing of the user id column from user_picture::fields()
99         //we could clone the rating object or preserve the rating id if we needed it again
100         //but we don't
101         $rating->id = $rating->uid;
103         echo '<tr class="ratingitemheader">';
104         echo "<td>";
105         if($course && $course->id) {
106             echo $OUTPUT->user_picture($rating, array('courseid'=>$course->id));
107         } else {
108             echo $OUTPUT->user_picture($rating);
109         }
110         echo '</td><td>'.fullname($rating).'</td>';
111         
112         //if they've switched to rating out of 5 but there were ratings submitted out of 10 for example
113         //Not doing this within $rm->get_all_ratings_for_item to allow access to the raw data
114         if ($rating->rating > $maxrating) {
115             $rating->rating = $maxrating;
116         }
117         echo '<td style="white-space:nowrap" align="center" class="rating">'.$scalemenu[$rating->rating]."</td>";
118         echo '<td style="white-space:nowrap" align="center" class="time">'.userdate($rating->timemodified)."</td>";
119         echo "</tr>\n";
120     }
121     echo "</table>";
122     echo "<br />";
125 if ($popup) {
126     echo $OUTPUT->close_window_button();
128 echo $OUTPUT->footer();