2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * A page to display a list of ratings for a given item (forum post etc)
20 * @package core_rating
22 * @copyright 2010 Andrew Davis
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once("../config.php");
27 require_once("lib.php");
29 $contextid = required_param('contextid', PARAM_INT
);
30 $component = required_param('component', PARAM_COMPONENT
);
31 $ratingarea = required_param('ratingarea', PARAM_AREA
);
32 $itemid = required_param('itemid', PARAM_INT
);
33 $scaleid = required_param('scaleid', PARAM_INT
);
34 $sort = optional_param('sort', '', PARAM_ALPHA
);
35 $popup = optional_param('popup', 0, PARAM_INT
); // Any non-zero value if in a popup window.
37 list($context, $course, $cm) = get_context_info_array($contextid);
38 require_login($course, false, $cm);
40 $url = new moodle_url('/rating/index.php', array('contextid' => $contextid,
41 'component' => $component,
42 'ratingarea' => $ratingarea,
44 'scaleid' => $scaleid));
46 $url->param('sort', $sort);
49 $url->param('popup', $popup);
52 $PAGE->set_context($context);
55 $PAGE->set_pagelayout('popup');
58 $params = array('contextid' => $contextid,
59 'component' => $component,
60 'ratingarea' => $ratingarea,
62 'scaleid' => $scaleid);
63 if (!has_capability('moodle/rating:view', $context) ||
64 !component_callback($component, 'rating_can_see_item_ratings', array($params), true)) {
65 throw new \
moodle_exception('noviewrate', 'rating');
68 $canviewallratings = has_capability('moodle/rating:viewall', $context);
72 $sqlsort = "u.firstname ASC";
75 $sqlsort = "r.rating ASC";
78 $sqlsort = "r.timemodified ASC";
81 $scalemenu = make_grades_menu($scaleid);
83 $strrating = get_string('rating', 'rating');
84 $strname = get_string('name');
85 $strtime = get_string('time');
87 $PAGE->set_title(get_string('allratingsforitem', 'rating'));
88 echo $OUTPUT->header();
90 $ratingoptions = new stdClass
;
91 $ratingoptions->context
= $context;
92 $ratingoptions->component
= $component;
93 $ratingoptions->ratingarea
= $ratingarea;
94 $ratingoptions->itemid
= $itemid;
95 $ratingoptions->sort
= $sqlsort;
97 $rm = new rating_manager();
98 $ratings = $rm->get_all_ratings_for_item($ratingoptions);
100 $msg = get_string('noratings', 'rating');
101 echo html_writer
::tag('div', $msg, array('class' => 'mdl-align'));
103 // To get the sort URL, copy the current URL and remove any previous sort.
104 $sorturl = new moodle_url($url);
105 $sorturl->remove_params('sort');
107 $table = new html_table
;
108 $table->cellpadding
= 3;
109 $table->cellspacing
= 3;
110 $table->attributes
['class'] = 'generalbox ratingtable';
111 $table->head
= array(
113 html_writer
::link(new moodle_url($sorturl, array('sort' => 'firstname')), $strname),
114 html_writer
::link(new moodle_url($sorturl, array('sort' => 'rating')), $strrating),
115 html_writer
::link(new moodle_url($sorturl, array('sort' => 'time')), $strtime)
117 $table->colclasses
= array('', 'firstname', 'rating', 'time');
118 $table->data
= array();
120 // If the scale was changed after ratings were submitted some ratings may have a value above the current maximum.
121 // We can't just do count($scalemenu) - 1 as custom scales start at index 1, not 0.
122 $maxrating = max(array_keys($scalemenu));
124 foreach ($ratings as $rating) {
125 if (!$canviewallratings and $USER->id
!= $rating->userid
) {
129 // Undo the aliasing of the user id column from fields::get_sql().
130 // We could clone the rating object or preserve the rating id if we needed it again
132 $rating->id
= $rating->userid
;
134 $row = new html_table_row();
135 $row->attributes
['class'] = 'ratingitemheader';
136 if ($course && $course->id
) {
137 $row->cells
[] = $OUTPUT->user_picture($rating, array('courseid' => $course->id
));
139 $row->cells
[] = $OUTPUT->user_picture($rating);
141 $row->cells
[] = fullname($rating);
142 if ($rating->rating
> $maxrating) {
143 $rating->rating
= $maxrating;
145 $row->cells
[] = $scalemenu[$rating->rating
];
146 $row->cells
[] = userdate($rating->timemodified
);
147 $table->data
[] = $row;
149 echo html_writer
::table($table);
152 echo $OUTPUT->close_window_button();
154 echo $OUTPUT->footer();