3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * This page receives ajax rating submissions
21 * It is similar to rate.php. Unlike rate.php a return url is NOT required.
23 * @package core_rating
25 * @copyright 2010 Andrew Davis
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 define('AJAX_SCRIPT', true);
31 require_once('../config.php');
32 require_once($CFG->dirroot
.'/rating/lib.php');
34 $contextid = required_param('contextid', PARAM_INT
);
35 $component = required_param('component', PARAM_COMPONENT
);
36 $ratingarea = required_param('ratingarea', PARAM_AREA
);
37 $itemid = required_param('itemid', PARAM_INT
);
38 $scaleid = required_param('scaleid', PARAM_INT
);
39 $userrating = required_param('rating', PARAM_INT
);
40 $rateduserid = required_param('rateduserid', PARAM_INT
);//which user is being rated. Required to update their grade
41 $aggregationmethod = optional_param('aggregation', RATING_AGGREGATE_NONE
, PARAM_INT
);//we're going to calculate the aggregate and return it to the client
43 $result = new stdClass
;
45 //if session has expired and its an ajax request so we cant do a page redirect
47 $result->error
= get_string('sessionerroruser', 'error');
48 echo json_encode($result);
52 list($context, $course, $cm) = get_context_info_array($contextid);
53 require_login($course, false, $cm);
55 $contextid = null;//now we have a context object throw away the id from the user
56 $PAGE->set_context($context);
57 $PAGE->set_url('/rating/rate_ajax.php', array('contextid'=>$context->id
));
59 if (!confirm_sesskey() ||
!has_capability('moodle/rating:rate',$context)) {
60 echo $OUTPUT->header();
61 echo get_string('ratepermissiondenied', 'rating');
62 echo $OUTPUT->footer();
66 $rm = new rating_manager();
68 //check the module rating permissions
69 //doing this check here rather than within rating_manager::get_ratings() so we can return a json error response
70 $pluginpermissionsarray = $rm->get_plugin_permissions_array($context->id
, $component, $ratingarea);
72 if (!$pluginpermissionsarray['rate']) {
73 $result->error
= get_string('ratepermissiondenied', 'rating');
74 echo json_encode($result);
78 'context' => $context,
79 'component' => $component,
80 'ratingarea' => $ratingarea,
82 'scaleid' => $scaleid,
83 'rating' => $userrating,
84 'rateduserid' => $rateduserid,
85 'aggregation' => $aggregationmethod
87 if (!$rm->check_rating_is_valid($params)) {
88 $result->error
= get_string('ratinginvalid', 'rating');
89 echo json_encode($result);
94 //rating options used to update the rating then retrieve the aggregate
95 $ratingoptions = new stdClass
;
96 $ratingoptions->context
= $context;
97 $ratingoptions->ratingarea
= $ratingarea;
98 $ratingoptions->component
= $component;
99 $ratingoptions->itemid
= $itemid;
100 $ratingoptions->scaleid
= $scaleid;
101 $ratingoptions->userid
= $USER->id
;
103 if ($userrating != RATING_UNSET_RATING
) {
104 $rating = new rating($ratingoptions);
105 $rating->update_rating($userrating);
106 } else { //delete the rating if the user set to Rate...
107 $options = new stdClass
;
108 $options->contextid
= $context->id
;
109 $options->component
= $component;
110 $options->ratingarea
= $ratingarea;
111 $options->userid
= $USER->id
;
112 $options->itemid
= $itemid;
114 $rm->delete_ratings($options);
117 // Future possible enhancement: add a setting to turn grade updating off for those who don't want them in gradebook
118 // note that this would need to be done in both rate.php and rate_ajax.php
119 if ($context->contextlevel
== CONTEXT_MODULE
) {
120 //tell the module that its grades have changed
121 $modinstance = $DB->get_record($cm->modname
, array('id' => $cm->instance
));
123 $modinstance->cmidnumber
= $cm->id
; //MDL-12961
124 $functionname = $cm->modname
.'_update_grades';
125 require_once($CFG->dirroot
."/mod/{$cm->modname}/lib.php");
126 if (function_exists($functionname)) {
127 $functionname($modinstance, $rateduserid);
132 //object to return to client as json
133 $result->success
= true;
135 //need to retrieve the updated item to get its new aggregate value
136 $item = new stdClass
;
139 //most of $ratingoptions variables were previously set
140 $ratingoptions->items
= array($item);
141 $ratingoptions->aggregate
= $aggregationmethod;
143 $items = $rm->get_ratings($ratingoptions);
144 $firstrating = $items[0]->rating
;
146 //for custom scales return text not the value
147 //this scales weirdness will go away when scales are refactored
149 $aggregatetoreturn = round($firstrating->aggregate
, 1);
151 // Output a dash if aggregation method == COUNT as the count is output next to the aggregate anyway
152 if ($firstrating->settings
->aggregationmethod
== RATING_AGGREGATE_COUNT
or $firstrating->count
== 0) {
153 $aggregatetoreturn = ' - ';
154 } else if ($firstrating->settings
->scale
->id
< 0) { //if its non-numeric scale
155 //dont use the scale item if the aggregation method is sum as adding items from a custom scale makes no sense
156 if ($firstrating->settings
->aggregationmethod
!= RATING_AGGREGATE_SUM
) {
157 $scalerecord = $DB->get_record('scale', array('id' => -$firstrating->settings
->scale
->id
));
159 $scalearray = explode(',', $scalerecord->scale
);
160 $aggregatetoreturn = $scalearray[$aggregatetoreturn-1];
165 //See if the user has permission to see the rating aggregate
166 if ($firstrating->user_can_view_aggregate()) {
167 $result->aggregate
= $aggregatetoreturn;
168 $result->count
= $firstrating->count
;
169 $result->itemid
= $itemid;
172 echo json_encode($result);