3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * A page to display a list of ratings for a given item (forum post etc)
21 * @package core_rating
23 * @copyright 2010 Andrew Davis
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require_once("../config.php");
28 require_once("lib.php");
30 $contextid = required_param('contextid', PARAM_INT
);
31 $component = required_param('component', PARAM_COMPONENT
);
32 $ratingarea = required_param('ratingarea', PARAM_AREA
);
33 $itemid = required_param('itemid', PARAM_INT
);
34 $scaleid = required_param('scaleid', PARAM_INT
);
35 $sort = optional_param('sort', '', PARAM_ALPHA
);
36 $popup = optional_param('popup', 0, PARAM_INT
); //==1 if in a popup window?
38 list($context, $course, $cm) = get_context_info_array($contextid);
39 require_login($course, false, $cm);
41 $url = new moodle_url('/rating/index.php', array('contextid' => $contextid,
42 'component' => $component,
43 'ratingarea' => $ratingarea,
45 'scaleid' => $scaleid));
48 $url->param('sort', $sort);
51 $url->param('popup', $popup);
54 $PAGE->set_context($context);
57 $PAGE->set_pagelayout('popup');
60 if (!has_capability('moodle/rating:view',$context)) {
61 print_error('noviewrate', 'rating');
64 $canviewallratings = has_capability('moodle/rating:viewall', $context);
67 case 'firstname': $sqlsort = "u.firstname ASC"; break;
68 case 'rating': $sqlsort = "r.rating ASC"; break;
69 default: $sqlsort = "r.timemodified ASC";
72 $scalemenu = make_grades_menu($scaleid);
74 $strrating = get_string('rating', 'rating');
75 $strname = get_string('name');
76 $strtime = get_string('time');
78 $PAGE->set_title(get_string('allratingsforitem','rating'));
79 echo $OUTPUT->header();
81 $ratingoptions = new stdClass
;
82 $ratingoptions->context
= $context;
83 $ratingoptions->component
= $component;
84 $ratingoptions->ratingarea
= $ratingarea;
85 $ratingoptions->itemid
= $itemid;
86 $ratingoptions->sort
= $sqlsort;
88 $rm = new rating_manager();
89 $ratings = $rm->get_all_ratings_for_item($ratingoptions);
91 $msg = get_string('noratings','rating');
92 echo html_writer
::tag('div', $msg, array('class'=>'mdl-align'));
94 // To get the sort URL, copy the current URL and remove any previous sort
95 $sorturl = new moodle_url($url);
96 $sorturl->remove_params('sort');
98 $table = new html_table
;
99 $table->cellpadding
= 3;
100 $table->cellspacing
= 3;
101 $table->attributes
['class'] = 'generalbox ratingtable';
102 $table->head
= array(
104 html_writer
::link(new moodle_url($sorturl, array('sort' => 'firstname')), $strname),
105 html_writer
::link(new moodle_url($sorturl, array('sort' => 'rating')), $strrating),
106 html_writer
::link(new moodle_url($sorturl, array('sort' => 'time')), $strtime)
108 $table->colclasses
= array('', 'firstname', 'rating', 'time');
109 $table->data
= array();
111 // If the scale was changed after ratings were submitted some ratings may have a value above the current maximum
112 // We can't just do count($scalemenu) - 1 as custom scales start at index 1, not 0
113 $maxrating = max(array_keys($scalemenu));
115 foreach ($ratings as $rating) {
116 if (!$canviewallratings and $USER->id
!= $rating->userid
) {
120 //Undo the aliasing of the user id column from user_picture::fields()
121 //we could clone the rating object or preserve the rating id if we needed it again
123 $rating->id
= $rating->userid
;
125 $row = new html_table_row();
126 $row->attributes
['class'] = 'ratingitemheader';
127 if ($course && $course->id
) {
128 $row->cells
[] = $OUTPUT->user_picture($rating, array('courseid' => $course->id
));
130 $row->cells
[] = $OUTPUT->user_picture($rating);
132 $row->cells
[] = fullname($rating);
133 if ($rating->rating
> $maxrating) {
134 $rating->rating
= $maxrating;
136 $row->cells
[] = $scalemenu[$rating->rating
];
137 $row->cells
[] = userdate($rating->timemodified
);
138 $table->data
[] = $row;
140 echo html_writer
::table($table);
143 echo $OUTPUT->close_window_button();
145 echo $OUTPUT->footer();