Merge branch 'install_master' of https://git.in.moodle.com/amosbot/moodle-install
[moodle.git] / rating / index.php
blobae516100d321c752bedbff97fac0be99e330658a
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
18 * A page to display a list of ratings for a given item (forum post etc)
20 * @package core_rating
21 * @category 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,
43 'itemid' => $itemid,
44 'scaleid' => $scaleid));
45 if (!empty($sort)) {
46 $url->param('sort', $sort);
48 if (!empty($popup)) {
49 $url->param('popup', $popup);
51 $PAGE->set_url($url);
52 $PAGE->set_context($context);
54 if ($popup) {
55 $PAGE->set_pagelayout('popup');
58 if (!has_capability('moodle/rating:view', $context)) {
59 print_error('noviewrate', 'rating');
62 $canviewallratings = has_capability('moodle/rating:viewall', $context);
64 switch ($sort) {
65 case 'firstname':
66 $sqlsort = "u.firstname ASC";
67 break;
68 case 'rating':
69 $sqlsort = "r.rating ASC";
70 break;
71 default:
72 $sqlsort = "r.timemodified ASC";
75 $scalemenu = make_grades_menu($scaleid);
77 $strrating = get_string('rating', 'rating');
78 $strname = get_string('name');
79 $strtime = get_string('time');
81 $PAGE->set_title(get_string('allratingsforitem', 'rating'));
82 echo $OUTPUT->header();
84 $ratingoptions = new stdClass;
85 $ratingoptions->context = $context;
86 $ratingoptions->component = $component;
87 $ratingoptions->ratingarea = $ratingarea;
88 $ratingoptions->itemid = $itemid;
89 $ratingoptions->sort = $sqlsort;
91 $rm = new rating_manager();
92 $ratings = $rm->get_all_ratings_for_item($ratingoptions);
93 if (!$ratings) {
94 $msg = get_string('noratings', 'rating');
95 echo html_writer::tag('div', $msg, array('class' => 'mdl-align'));
96 } else {
97 // To get the sort URL, copy the current URL and remove any previous sort.
98 $sorturl = new moodle_url($url);
99 $sorturl->remove_params('sort');
101 $table = new html_table;
102 $table->cellpadding = 3;
103 $table->cellspacing = 3;
104 $table->attributes['class'] = 'generalbox ratingtable';
105 $table->head = array(
107 html_writer::link(new moodle_url($sorturl, array('sort' => 'firstname')), $strname),
108 html_writer::link(new moodle_url($sorturl, array('sort' => 'rating')), $strrating),
109 html_writer::link(new moodle_url($sorturl, array('sort' => 'time')), $strtime)
111 $table->colclasses = array('', 'firstname', 'rating', 'time');
112 $table->data = array();
114 // If the scale was changed after ratings were submitted some ratings may have a value above the current maximum.
115 // We can't just do count($scalemenu) - 1 as custom scales start at index 1, not 0.
116 $maxrating = max(array_keys($scalemenu));
118 foreach ($ratings as $rating) {
119 if (!$canviewallratings and $USER->id != $rating->userid) {
120 continue;
123 // Undo the aliasing of the user id column from user_picture::fields().
124 // We could clone the rating object or preserve the rating id if we needed it again
125 // but we don't.
126 $rating->id = $rating->userid;
128 $row = new html_table_row();
129 $row->attributes['class'] = 'ratingitemheader';
130 if ($course && $course->id) {
131 $row->cells[] = $OUTPUT->user_picture($rating, array('courseid' => $course->id));
132 } else {
133 $row->cells[] = $OUTPUT->user_picture($rating);
135 $row->cells[] = fullname($rating);
136 if ($rating->rating > $maxrating) {
137 $rating->rating = $maxrating;
139 $row->cells[] = $scalemenu[$rating->rating];
140 $row->cells[] = userdate($rating->timemodified);
141 $table->data[] = $row;
143 echo html_writer::table($table);
145 if ($popup) {
146 echo $OUTPUT->close_window_button();
148 echo $OUTPUT->footer();