MDL-24092 removing STATEMENTS support from install.xml, use db/install.php or db...
[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 *
21 * @package moodlecore
22 * @copyright 2010 Andrew Davis
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26require_once("../config.php");
9c1f9627 27require_once("lib.php");
a09aeee4 28
f1f6a755 29$contextid = required_param('contextid', PARAM_INT);
a09aeee4
AD
30$itemid = required_param('itemid', PARAM_INT);
31$scaleid = required_param('scaleid', PARAM_INT);
32$sort = optional_param('sort', '', PARAM_ALPHA);
63e87951 33$popup = optional_param('popup', 0, PARAM_INT);//==1 if in a popup window?
a09aeee4 34
f1f6a755
AD
35list($context, $course, $cm) = get_context_info_array($contextid);
36require_login($course, false, $cm);
a09aeee4 37
7ac928a7 38$url = new moodle_url('/rating/index.php', array('contextid'=>$contextid,'itemid'=>$itemid,'scaleid'=>$scaleid));
a09aeee4
AD
39if ($sort !== 0) {
40 $url->param('sort', $sort);
41}
42$PAGE->set_url($url);
c8e142dd
AD
43$PAGE->set_context($context);
44
63e87951
AD
45if ($popup) {
46 $PAGE->set_pagelayout('popup');
47}
a09aeee4 48
63e87951 49if (!has_capability('moodle/rating:view',$context)) {
6c5fcef7 50 print_error('noviewrate', 'rating');
a09aeee4 51}
63e87951 52if (!has_capability('moodle/rating:viewall',$context) and $USER->id != $item->userid) {
6c5fcef7 53 print_error('noviewanyrate', 'rating');
a09aeee4
AD
54}
55
56switch ($sort) {
57 case 'firstname': $sqlsort = "u.firstname ASC"; break;
58 case 'rating': $sqlsort = "r.rating ASC"; break;
59 default: $sqlsort = "r.timemodified ASC";
60}
61
62$scalemenu = make_grades_menu($scaleid);
63
6c5fcef7
AD
64$strratings = get_string('ratings', 'rating');
65$strrating = get_string('rating', 'rating');
a09aeee4
AD
66$strname = get_string('name');
67$strtime = get_string('time');
68
63e87951 69//Is there something more meaningful we can put in the title? It used to be forum post title
a09aeee4
AD
70$PAGE->set_title("$strratings: ".format_string($itemid));
71echo $OUTPUT->header();
72
b1721f67
AD
73$ratingoptions = new stdclass();
74$ratingoptions->context = $context;
75$ratingoptions->itemid = $itemid;
63e87951 76$ratingoptions->sort = $sqlsort;
a8e85df6
AD
77
78$rm = new rating_manager();
63e87951 79$ratings = $rm->get_all_ratings_for_item($ratingoptions);
a09aeee4 80if (!$ratings) {
63e87951 81 print_error('noresult', 'forum', '', format_string($itemid));
a09aeee4 82} else {
63e87951
AD
83 $sortargs = "contextid=$contextid&amp;itemid=$itemid&amp;scaleid=$scaleid";
84 if($popup) {
85 $sortargs.="&amp;popup=$popup";
86 }
a09aeee4
AD
87 echo "<table border=\"0\" cellpadding=\"3\" cellspacing=\"3\" class=\"generalbox\" style=\"width:100%\">";
88 echo "<tr>";
89 echo "<th class=\"header\" scope=\"col\">&nbsp;</th>";
63e87951
AD
90 echo "<th class=\"header\" scope=\"col\"><a href=\"index.php?$sortargs&amp;sort=firstname\">$strname</a></th>";
91 echo "<th class=\"header\" scope=\"col\" style=\"width:100%\"><a href=\"index.php?$sortargs&amp;sort=rating\">$strrating</a></th>";
92 echo "<th class=\"header\" scope=\"col\"><a href=\"index.php?$sortargs&amp;sort=time\">$strtime</a></th>";
a09aeee4 93 echo "</tr>";
61b00708 94
a09aeee4 95 foreach ($ratings as $rating) {
61b00708
AD
96 //Undo the aliasing of the user id column from user_picture::fields()
97 //we could clone the rating object or preserve the rating id if we needed it again
98 //but we don't
99 $rating->id = $rating->uid;
c538a23d 100
7ac928a7 101 echo '<tr class="ratingitemheader">';
a09aeee4 102 echo "<td>";
2d10c085
AD
103 if($course && $course->id) {
104 echo $OUTPUT->user_picture($rating, array('courseid'=>$course->id));
a09aeee4
AD
105 } else {
106 echo $OUTPUT->user_picture($rating);
107 }
108 echo '</td><td>'.fullname($rating).'</td>';
109 echo '<td style="white-space:nowrap" align="center" class="rating">'.$scalemenu[$rating->rating]."</td>";
110 echo '<td style="white-space:nowrap" align="center" class="time">'.userdate($rating->timemodified)."</td>";
111 echo "</tr>\n";
112 }
113 echo "</table>";
114 echo "<br />";
115}
116
63e87951
AD
117if ($popup) {
118 echo $OUTPUT->close_window_button();
119}
a09aeee4 120echo $OUTPUT->footer();