rating MDL-21657 first commit for rating library
[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");
27require_once("ratinglib.php");
28
29$itemcontextid = required_param('contextid', PARAM_INT);
30$itemid = required_param('itemid', PARAM_INT);
31$scaleid = required_param('scaleid', PARAM_INT);
32$sort = optional_param('sort', '', PARAM_ALPHA);
33
34$context = get_context_instance_by_id($itemcontextid);
35$cm = get_coursemodule_from_id('', $context->instanceid, 0, false, MUST_EXIST);
36$courseid = $cm->course;
37//$course = get_course_from_path($context->path);
38require_login($courseid, false, $cm);
39
40$url = new moodle_url('/rating/index.php', array('contextid'=>$itemcontextid,'itemid'=>$itemid));
41if ($sort !== 0) {
42 $url->param('sort', $sort);
43}
44$PAGE->set_url($url);
45
46$permissions = rating::get_rating_permissions(context);
47if (!$permissions[RATING_VIEW]) {
48 print_error('noviewrate', 'ratings');
49}
50if (!$permissions[RATING_VIEW_ALL] and $USER->id != $item->userid) {
51 print_error('noviewanyrate', 'ratings');
52}
53
54switch ($sort) {
55 case 'firstname': $sqlsort = "u.firstname ASC"; break;
56 case 'rating': $sqlsort = "r.rating ASC"; break;
57 default: $sqlsort = "r.timemodified ASC";
58}
59
60$scalemenu = make_grades_menu($scaleid);
61
62$strratings = get_string('ratings', 'ratings');
63$strrating = get_string('rating', 'ratings');
64$strname = get_string('name');
65$strtime = get_string('time');
66
67//Is there something more meaningful we can put in the title?
68//$PAGE->set_title("$strratings: ".format_string($post->subject));
69$PAGE->set_title("$strratings: ".format_string($itemid));
70echo $OUTPUT->header();
71
72//if (!$ratings = forum_get_ratings($post->id, $sqlsort)) {
73$ratings = rating::load_ratings_for_item($context, $itemid, $sort);
74if (!$ratings) {
75 //print_error('noresult', 'forum', '', format_string($post->subject));
76 print_error('noresult');
77} else {
78 echo "<table border=\"0\" cellpadding=\"3\" cellspacing=\"3\" class=\"generalbox\" style=\"width:100%\">";
79 echo "<tr>";
80 echo "<th class=\"header\" scope=\"col\">&nbsp;</th>";
81 echo "<th class=\"header\" scope=\"col\"><a href=\"report.php?id=$itemid&amp;sort=firstname\">$strname</a></th>";
82 echo "<th class=\"header\" scope=\"col\" style=\"width:100%\"><a href=\"report.php?id=$itemid&amp;sort=rating\">$strrating</a></th>";
83 echo "<th class=\"header\" scope=\"col\"><a href=\"report.php?id=$itemid&amp;sort=time\">$strtime</a></th>";
84 echo "</tr>";
85 foreach ($ratings as $rating) {
86 echo '<tr class="forumpostheader">';
87 echo "<td>";
88 if($courseid) {
89 echo $OUTPUT->user_picture($rating, array('courseid'=>$courseid));
90 } else {
91 echo $OUTPUT->user_picture($rating);
92 }
93 echo '</td><td>'.fullname($rating).'</td>';
94 echo '<td style="white-space:nowrap" align="center" class="rating">'.$scalemenu[$rating->rating]."</td>";
95 echo '<td style="white-space:nowrap" align="center" class="time">'.userdate($rating->timemodified)."</td>";
96 echo "</tr>\n";
97 }
98 echo "</table>";
99 echo "<br />";
100}
101
102echo $OUTPUT->close_window_button();
103echo $OUTPUT->footer();