Automatically generated installer lang files
[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
94e40193
AD
41$url = new moodle_url('/rating/index.php', array('contextid'=>$contextid,'component'=>$component,'itemid'=>$itemid,'scaleid'=>$scaleid));
42if (!empty($ratingarea)) {
43 $url->param('ratingarea', $ratingarea);
44}
45if (!empty($sort)) {
a09aeee4
AD
46 $url->param('sort', $sort);
47}
94e40193
AD
48if (!empty($popup)) {
49 $url->param('popup', $popup);
50}
a09aeee4 51$PAGE->set_url($url);
c8e142dd
AD
52$PAGE->set_context($context);
53
63e87951
AD
54if ($popup) {
55 $PAGE->set_pagelayout('popup');
56}
a09aeee4 57
63e87951 58if (!has_capability('moodle/rating:view',$context)) {
6c5fcef7 59 print_error('noviewrate', 'rating');
a09aeee4 60}
63e87951 61if (!has_capability('moodle/rating:viewall',$context) and $USER->id != $item->userid) {
6c5fcef7 62 print_error('noviewanyrate', 'rating');
a09aeee4
AD
63}
64
65switch ($sort) {
66 case 'firstname': $sqlsort = "u.firstname ASC"; break;
67 case 'rating': $sqlsort = "r.rating ASC"; break;
68 default: $sqlsort = "r.timemodified ASC";
69}
70
71$scalemenu = make_grades_menu($scaleid);
72
6c5fcef7 73$strrating = get_string('rating', 'rating');
a09aeee4
AD
74$strname = get_string('name');
75$strtime = get_string('time');
76
d0c675f7 77$PAGE->set_title(get_string('allratingsforitem','rating'));
a09aeee4
AD
78echo $OUTPUT->header();
79
2b04c41c 80$ratingoptions = new stdClass;
b1721f67 81$ratingoptions->context = $context;
2b04c41c
SH
82$ratingoptions->component = $component;
83$ratingoptions->ratingarea = $ratingarea;
b1721f67 84$ratingoptions->itemid = $itemid;
63e87951 85$ratingoptions->sort = $sqlsort;
a8e85df6
AD
86
87$rm = new rating_manager();
63e87951 88$ratings = $rm->get_all_ratings_for_item($ratingoptions);
a09aeee4 89if (!$ratings) {
695719fa
AD
90 $msg = get_string('noratings','rating');
91 echo html_writer::tag('div', $msg, array('class'=>'mdl-align'));
a09aeee4 92} else {
94e40193
AD
93 // To get the sort URL, copy the current URL and remove any previous sort
94 $sorturl = new moodle_url($url);
95 $sorturl->remove_params('sort');
f2e72593 96
2b04c41c
SH
97 $table = new html_table;
98 $table->cellpadding = 3;
99 $table->cellspacing = 3;
100 $table->attributes['class'] = 'generalbox ratingtable';
101 $table->head = array(
102 '',
103 html_writer::link(new moodle_url($sorturl, array('sort' => 'firstname')), $strname),
104 html_writer::link(new moodle_url($sorturl, array('sort' => 'rating')), $strrating),
105 html_writer::link(new moodle_url($sorturl, array('sort' => 'time')), $strtime)
106 );
107 $table->colclasses = array('', 'firstname', 'rating', 'time');
108 $table->data = array();
61b00708 109
08f06b1c
AD
110 // If the scale was changed after ratings were submitted some ratings may have a value above the current maximum
111 // We can't just do count($scalemenu) - 1 as custom scales start at index 1, not 0
112 $maxrating = max(array_keys($scalemenu));
113
a09aeee4 114 foreach ($ratings as $rating) {
61b00708
AD
115 //Undo the aliasing of the user id column from user_picture::fields()
116 //we could clone the rating object or preserve the rating id if we needed it again
117 //but we don't
7bbe9715 118 $rating->id = $rating->userid;
5d354ded 119
2b04c41c
SH
120 $row = new html_table_row();
121 $row->attributes['class'] = 'ratingitemheader';
122 if ($course && $course->id) {
123 $row->cells[] = $OUTPUT->user_picture($rating, array('courseid' => $course->id));
a09aeee4 124 } else {
2b04c41c 125 $row->cells[] = $OUTPUT->user_picture($rating);
a09aeee4 126 }
2b04c41c 127 $row->cells[] = fullname($rating);
07f05a04
AD
128 if ($rating->rating > $maxrating) {
129 $rating->rating = $maxrating;
130 }
2b04c41c
SH
131 $row->cells[] = $scalemenu[$rating->rating];
132 $row->cells[] = userdate($rating->timemodified);
133 $table->data[] = $row;
a09aeee4 134 }
2b04c41c 135 echo html_writer::table($table);
a09aeee4 136}
63e87951
AD
137if ($popup) {
138 echo $OUTPUT->close_window_button();
139}
f2e72593 140echo $OUTPUT->footer();