Merge branch 'MDL-32012' of git://github.com/merrill-oakland/moodle
[moodle.git] / rating / index.php
blobdb41d98603d93b3e208fd3022346f3f1b19242d3
1 <?php
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.
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/>.
18 /**
19 * A page to display a list of ratings for a given item (forum post etc)
21 * @package core_rating
22 * @category rating
23 * @copyright 2010 Andrew Davis
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require_once("../config.php");
28 require_once("lib.php");
30 $contextid = required_param('contextid', PARAM_INT);
31 $component = required_param('component', PARAM_COMPONENT);
32 $ratingarea = optional_param('ratingarea', null, PARAM_AREA);
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?
38 list($context, $course, $cm) = get_context_info_array($contextid);
39 require_login($course, false, $cm);
41 $url = new moodle_url('/rating/index.php', array('contextid'=>$contextid,'itemid'=>$itemid,'scaleid'=>$scaleid));
42 if ($sort !== 0) {
43 $url->param('sort', $sort);
45 $PAGE->set_url($url);
46 $PAGE->set_context($context);
48 if ($popup) {
49 $PAGE->set_pagelayout('popup');
52 if (!has_capability('moodle/rating:view',$context)) {
53 print_error('noviewrate', 'rating');
55 if (!has_capability('moodle/rating:viewall',$context) and $USER->id != $item->userid) {
56 print_error('noviewanyrate', 'rating');
59 switch ($sort) {
60 case 'firstname': $sqlsort = "u.firstname ASC"; break;
61 case 'rating': $sqlsort = "r.rating ASC"; break;
62 default: $sqlsort = "r.timemodified ASC";
65 $scalemenu = make_grades_menu($scaleid);
67 $strrating = get_string('rating', 'rating');
68 $strname = get_string('name');
69 $strtime = get_string('time');
71 $PAGE->set_title(get_string('allratingsforitem','rating'));
72 echo $OUTPUT->header();
74 $ratingoptions = new stdClass;
75 $ratingoptions->context = $context;
76 $ratingoptions->component = $component;
77 $ratingoptions->ratingarea = $ratingarea;
78 $ratingoptions->itemid = $itemid;
79 $ratingoptions->sort = $sqlsort;
81 $rm = new rating_manager();
82 $ratings = $rm->get_all_ratings_for_item($ratingoptions);
83 if (!$ratings) {
84 $msg = get_string('noratings','rating');
85 echo html_writer::tag('div', $msg, array('class'=>'mdl-align'));
86 } else {
87 $sorturl = new moodle_url('/index.php', array('contextid' => $contextid, 'itemid' => $itemid, 'scaleid' => $scaleid));
88 if ($popup) {
89 $sorturl->param('popup', $popup);
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)
102 $table->colclasses = array('', 'firstname', 'rating', 'time');
103 $table->data = array();
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));
109 foreach ($ratings as $rating) {
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
113 $rating->id = $rating->userid;
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));
119 } else {
120 $row->cells[] = $OUTPUT->user_picture($rating);
122 $row->cells[] = fullname($rating);
123 if ($rating->rating > $maxrating) {
124 $rating->rating = $maxrating;
126 $row->cells[] = $scalemenu[$rating->rating];
127 $row->cells[] = userdate($rating->timemodified);
128 $table->data[] = $row;
130 echo html_writer::table($table);
132 if ($popup) {
133 echo $OUTPUT->close_window_button();
135 echo $OUTPUT->footer();