MDL-38373 Use correct PEAR->loadExtension()
[moodle.git] / rating / index.php
blobddfc22487a1d1209a6595fcbcee66291bec7a0cc
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,'component'=>$component,'itemid'=>$itemid,'scaleid'=>$scaleid));
42 if (!empty($ratingarea)) {
43 $url->param('ratingarea', $ratingarea);
45 if (!empty($sort)) {
46 $url->param('sort', $sort);
48 if (!empty($popup)) {
49 $url->param('popup', $popup);
51 $PAGE->set_url($url);
52 $PAGE->set_context($context);
54 if ($popup) {
55 $PAGE->set_pagelayout('popup');
58 if (!has_capability('moodle/rating:view',$context)) {
59 print_error('noviewrate', 'rating');
61 if (!has_capability('moodle/rating:viewall',$context) and $USER->id != $item->userid) {
62 print_error('noviewanyrate', 'rating');
65 switch ($sort) {
66 case 'firstname': $sqlsort = "u.firstname ASC"; break;
67 case 'rating': $sqlsort = "r.rating ASC"; break;
68 default: $sqlsort = "r.timemodified ASC";
71 $scalemenu = make_grades_menu($scaleid);
73 $strrating = get_string('rating', 'rating');
74 $strname = get_string('name');
75 $strtime = get_string('time');
77 $PAGE->set_title(get_string('allratingsforitem','rating'));
78 echo $OUTPUT->header();
80 $ratingoptions = new stdClass;
81 $ratingoptions->context = $context;
82 $ratingoptions->component = $component;
83 $ratingoptions->ratingarea = $ratingarea;
84 $ratingoptions->itemid = $itemid;
85 $ratingoptions->sort = $sqlsort;
87 $rm = new rating_manager();
88 $ratings = $rm->get_all_ratings_for_item($ratingoptions);
89 if (!$ratings) {
90 $msg = get_string('noratings','rating');
91 echo html_writer::tag('div', $msg, array('class'=>'mdl-align'));
92 } else {
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');
97 $table = new html_table;
98 $table->cellpadding = 3;
99 $table->cellspacing = 3;
100 $table->attributes['class'] = 'generalbox ratingtable';
101 $table->head = array(
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)
107 $table->colclasses = array('', 'firstname', 'rating', 'time');
108 $table->data = array();
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));
114 foreach ($ratings as $rating) {
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
118 $rating->id = $rating->userid;
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));
124 } else {
125 $row->cells[] = $OUTPUT->user_picture($rating);
127 $row->cells[] = fullname($rating);
128 if ($rating->rating > $maxrating) {
129 $rating->rating = $maxrating;
131 $row->cells[] = $scalemenu[$rating->rating];
132 $row->cells[] = userdate($rating->timemodified);
133 $table->data[] = $row;
135 echo html_writer::table($table);
137 if ($popup) {
138 echo $OUTPUT->close_window_button();
140 echo $OUTPUT->footer();