Merge branch 'MDL-30955_forum_ratings' of git://github.com/andyjdavis/moodle
[moodle.git] / rating / index.php
CommitLineData
a09aeee4
AD
1<?php
2
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/>.
17
18/**
19 * A page to display a list of ratings for a given item (forum post etc)
20 *
8c335cff
JF
21 * @package core_rating
22 * @category rating
5d354ded
PS
23 * @copyright 2010 Andrew Davis
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
a09aeee4
AD
25 */
26
27require_once("../config.php");
9c1f9627 28require_once("lib.php");
a09aeee4 29
2b04c41c 30$contextid = required_param('contextid', PARAM_INT);
aff24313
PS
31$component = required_param('component', PARAM_COMPONENT);
32$ratingarea = optional_param('ratingarea', null, PARAM_AREA);
2b04c41c
SH
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?
a09aeee4 37
f1f6a755
AD
38list($context, $course, $cm) = get_context_info_array($contextid);
39require_login($course, false, $cm);
a09aeee4 40
7ac928a7 41$url = new moodle_url('/rating/index.php', array('contextid'=>$contextid,'itemid'=>$itemid,'scaleid'=>$scaleid));
a09aeee4
AD
42if ($sort !== 0) {
43 $url->param('sort', $sort);
44}
45$PAGE->set_url($url);
c8e142dd
AD
46$PAGE->set_context($context);
47
63e87951
AD
48if ($popup) {
49 $PAGE->set_pagelayout('popup');
50}
a09aeee4 51
63e87951 52if (!has_capability('moodle/rating:view',$context)) {
6c5fcef7 53 print_error('noviewrate', 'rating');
a09aeee4 54}
63e87951 55if (!has_capability('moodle/rating:viewall',$context) and $USER->id != $item->userid) {
6c5fcef7 56 print_error('noviewanyrate', 'rating');
a09aeee4
AD
57}
58
59switch ($sort) {
60 case 'firstname': $sqlsort = "u.firstname ASC"; break;
61 case 'rating': $sqlsort = "r.rating ASC"; break;
62 default: $sqlsort = "r.timemodified ASC";
63}
64
65$scalemenu = make_grades_menu($scaleid);
66
6c5fcef7 67$strrating = get_string('rating', 'rating');
a09aeee4
AD
68$strname = get_string('name');
69$strtime = get_string('time');
70
d0c675f7 71$PAGE->set_title(get_string('allratingsforitem','rating'));
a09aeee4
AD
72echo $OUTPUT->header();
73
2b04c41c 74$ratingoptions = new stdClass;
b1721f67 75$ratingoptions->context = $context;
2b04c41c
SH
76$ratingoptions->component = $component;
77$ratingoptions->ratingarea = $ratingarea;
b1721f67 78$ratingoptions->itemid = $itemid;
63e87951 79$ratingoptions->sort = $sqlsort;
a8e85df6
AD
80
81$rm = new rating_manager();
63e87951 82$ratings = $rm->get_all_ratings_for_item($ratingoptions);
a09aeee4 83if (!$ratings) {
695719fa
AD
84 $msg = get_string('noratings','rating');
85 echo html_writer::tag('div', $msg, array('class'=>'mdl-align'));
a09aeee4 86} else {
2b04c41c
SH
87 $sorturl = new moodle_url('/index.php', array('contextid' => $contextid, 'itemid' => $itemid, 'scaleid' => $scaleid));
88 if ($popup) {
89 $sorturl->param('popup', $popup);
63e87951 90 }
f2e72593 91
2b04c41c
SH
92 $table = new html_table;
93 $table->cellpadding = 3;
94 $table->cellspacing = 3;
95 $table->attributes['class'] = 'generalbox ratingtable';
96 $table->head = array(
97 '',
98 html_writer::link(new moodle_url($sorturl, array('sort' => 'firstname')), $strname),
99 html_writer::link(new moodle_url($sorturl, array('sort' => 'rating')), $strrating),
100 html_writer::link(new moodle_url($sorturl, array('sort' => 'time')), $strtime)
101 );
102 $table->colclasses = array('', 'firstname', 'rating', 'time');
103 $table->data = array();
61b00708 104
08f06b1c
AD
105 // If the scale was changed after ratings were submitted some ratings may have a value above the current maximum
106 // We can't just do count($scalemenu) - 1 as custom scales start at index 1, not 0
107 $maxrating = max(array_keys($scalemenu));
108
a09aeee4 109 foreach ($ratings as $rating) {
61b00708
AD
110 //Undo the aliasing of the user id column from user_picture::fields()
111 //we could clone the rating object or preserve the rating id if we needed it again
112 //but we don't
7bbe9715 113 $rating->id = $rating->userid;
5d354ded 114
2b04c41c
SH
115 $row = new html_table_row();
116 $row->attributes['class'] = 'ratingitemheader';
117 if ($course && $course->id) {
118 $row->cells[] = $OUTPUT->user_picture($rating, array('courseid' => $course->id));
a09aeee4 119 } else {
2b04c41c 120 $row->cells[] = $OUTPUT->user_picture($rating);
a09aeee4 121 }
2b04c41c 122 $row->cells[] = fullname($rating);
07f05a04
AD
123 if ($rating->rating > $maxrating) {
124 $rating->rating = $maxrating;
125 }
2b04c41c
SH
126 $row->cells[] = $scalemenu[$rating->rating];
127 $row->cells[] = userdate($rating->timemodified);
128 $table->data[] = $row;
a09aeee4 129 }
2b04c41c 130 echo html_writer::table($table);
a09aeee4 131}
63e87951
AD
132if ($popup) {
133 echo $OUTPUT->close_window_button();
134}
f2e72593 135echo $OUTPUT->footer();