Merge branch 'MDL-51771' of git://github.com/timhunt/moodle
[moodle.git] / rating / rate_ajax.php
blob993d773f2dfae8ec9d8c7029d781d7828399bb02
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * This page receives ajax rating submissions
20 * It is similar to rate.php. Unlike rate.php a return url is NOT required.
22 * @package core_rating
23 * @category rating
24 * @copyright 2010 Andrew Davis
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 define('AJAX_SCRIPT', true);
30 require_once('../config.php');
31 require_once($CFG->dirroot.'/rating/lib.php');
33 $contextid = required_param('contextid', PARAM_INT);
34 $component = required_param('component', PARAM_COMPONENT);
35 $ratingarea = required_param('ratingarea', PARAM_AREA);
36 $itemid = required_param('itemid', PARAM_INT);
37 $scaleid = required_param('scaleid', PARAM_INT);
38 $userrating = required_param('rating', PARAM_INT);
39 $rateduserid = required_param('rateduserid', PARAM_INT); // The user being rated. Required to update their grade.
40 $aggregationmethod = optional_param('aggregation', RATING_AGGREGATE_NONE, PARAM_INT); // Used to calculate the aggregate to return.
42 $result = new stdClass;
44 // If session has expired and its an ajax request so we cant do a page redirect.
45 if (!isloggedin()) {
46 $result->error = get_string('sessionerroruser', 'error');
47 echo json_encode($result);
48 die();
51 list($context, $course, $cm) = get_context_info_array($contextid);
52 require_login($course, false, $cm);
54 $contextid = null; // Now we have a context object, throw away the id from the user.
55 $PAGE->set_context($context);
56 $PAGE->set_url('/rating/rate_ajax.php', array('contextid' => $context->id));
58 if (!confirm_sesskey() || !has_capability('moodle/rating:rate', $context)) {
59 echo $OUTPUT->header();
60 echo get_string('ratepermissiondenied', 'rating');
61 echo $OUTPUT->footer();
62 die();
65 $rm = new rating_manager();
67 // Check the module rating permissions.
68 // Doing this check here rather than within rating_manager::get_ratings() so we can return a json error response.
69 $pluginpermissionsarray = $rm->get_plugin_permissions_array($context->id, $component, $ratingarea);
71 if (!$pluginpermissionsarray['rate']) {
72 $result->error = get_string('ratepermissiondenied', 'rating');
73 echo json_encode($result);
74 die();
75 } else {
76 $params = array(
77 'context' => $context,
78 'component' => $component,
79 'ratingarea' => $ratingarea,
80 'itemid' => $itemid,
81 'scaleid' => $scaleid,
82 'rating' => $userrating,
83 'rateduserid' => $rateduserid,
84 'aggregation' => $aggregationmethod
86 if (!$rm->check_rating_is_valid($params)) {
87 $result->error = get_string('ratinginvalid', 'rating');
88 echo json_encode($result);
89 die();
93 // Rating options used to update the rating then retrieve the aggregate.
94 $ratingoptions = new stdClass;
95 $ratingoptions->context = $context;
96 $ratingoptions->ratingarea = $ratingarea;
97 $ratingoptions->component = $component;
98 $ratingoptions->itemid = $itemid;
99 $ratingoptions->scaleid = $scaleid;
100 $ratingoptions->userid = $USER->id;
102 if ($userrating != RATING_UNSET_RATING) {
103 $rating = new rating($ratingoptions);
104 $rating->update_rating($userrating);
105 } else { // Delete the rating if the user set to "Rate..."
106 $options = new stdClass;
107 $options->contextid = $context->id;
108 $options->component = $component;
109 $options->ratingarea = $ratingarea;
110 $options->userid = $USER->id;
111 $options->itemid = $itemid;
113 $rm->delete_ratings($options);
116 // Future possible enhancement: add a setting to turn grade updating off for those who don't want them in gradebook.
117 // Note that this would need to be done in both rate.php and rate_ajax.php.
118 if ($context->contextlevel == CONTEXT_MODULE) {
119 // Tell the module that its grades have changed.
120 $modinstance = $DB->get_record($cm->modname, array('id' => $cm->instance));
121 if ($modinstance) {
122 $modinstance->cmidnumber = $cm->id; // MDL-12961.
123 $functionname = $cm->modname.'_update_grades';
124 require_once($CFG->dirroot."/mod/{$cm->modname}/lib.php");
125 if (function_exists($functionname)) {
126 $functionname($modinstance, $rateduserid);
131 // Object to return to client as JSON.
132 $result->success = true;
134 // Need to retrieve the updated item to get its new aggregate value.
135 $item = new stdClass;
136 $item->id = $itemid;
138 // Most of $ratingoptions variables were previously set.
139 $ratingoptions->items = array($item);
140 $ratingoptions->aggregate = $aggregationmethod;
142 $items = $rm->get_ratings($ratingoptions);
143 $firstrating = $items[0]->rating;
145 // See if the user has permission to see the rating aggregate.
146 if ($firstrating->user_can_view_aggregate()) {
148 // For custom scales return text not the value.
149 // This scales weirdness will go away when scales are refactored.
150 $scalearray = null;
151 $aggregatetoreturn = round($firstrating->aggregate, 1);
153 // Output a dash if aggregation method == COUNT as the count is output next to the aggregate anyway.
154 if ($firstrating->settings->aggregationmethod == RATING_AGGREGATE_COUNT or $firstrating->count == 0) {
155 $aggregatetoreturn = ' - ';
156 } else if ($firstrating->settings->scale->id < 0) { // If its non-numeric scale.
157 // Dont use the scale item if the aggregation method is sum as adding items from a custom scale makes no sense.
158 if ($firstrating->settings->aggregationmethod != RATING_AGGREGATE_SUM) {
159 $scalerecord = $DB->get_record('scale', array('id' => -$firstrating->settings->scale->id));
160 if ($scalerecord) {
161 $scalearray = explode(',', $scalerecord->scale);
162 $aggregatetoreturn = $scalearray[$aggregatetoreturn - 1];
167 $result->aggregate = $aggregatetoreturn;
168 $result->count = $firstrating->count;
169 $result->itemid = $itemid;
172 echo json_encode($result);