Merge branch 'MOODLE_23_STABLE' into install_23_STABLE
[moodle.git] / rating / rate_ajax.php
CommitLineData
b1721f67
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* This page receives ajax rating submissions
20 *
21 * It is similar to rate.php. Unlike rate.php a return url is NOT required.
22 *
8c335cff
JF
23 * @package core_rating
24 * @category rating
5d354ded
PS
25 * @copyright 2010 Andrew Davis
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
b1721f67
AD
27 */
28
1bb1e5be
AD
29define('AJAX_SCRIPT', true);
30
b1721f67 31require_once('../config.php');
2b04c41c
SH
32require_once($CFG->dirroot.'/rating/lib.php');
33
34$contextid = required_param('contextid', PARAM_INT);
aff24313
PS
35$component = required_param('component', PARAM_COMPONENT);
36$ratingarea = required_param('ratingarea', PARAM_AREA);
2b04c41c
SH
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
07f05a04 41$aggregationmethod = optional_param('aggregation', RATING_AGGREGATE_NONE, PARAM_INT);//we're going to calculate the aggregate and return it to the client
b1721f67
AD
42
43$result = new stdClass;
44
45//if session has expired and its an ajax request so we cant do a page redirect
46if( !isloggedin() ){
47 $result->error = get_string('sessionerroruser', 'error');
48 echo json_encode($result);
49 die();
50}
51
52list($context, $course, $cm) = get_context_info_array($contextid);
53require_login($course, false, $cm);
54
3180bc2c 55$contextid = null;//now we have a context object throw away the id from the user
2c2ff8d5
AD
56$PAGE->set_context($context);
57$PAGE->set_url('/rating/rate_ajax.php', array('contextid'=>$context->id));
3180bc2c 58
778361c3 59if (!confirm_sesskey() || !has_capability('moodle/rating:rate',$context)) {
3180bc2c 60 echo $OUTPUT->header();
5bda2d6a 61 echo get_string('ratepermissiondenied', 'rating');
3180bc2c
AD
62 echo $OUTPUT->footer();
63 die();
64}
65
279fcfcf
AD
66$rm = new rating_manager();
67
d251b259 68//check the module rating permissions
2c2ff8d5 69//doing this check here rather than within rating_manager::get_ratings() so we can return a json error response
2b04c41c 70$pluginpermissionsarray = $rm->get_plugin_permissions_array($context->id, $component, $ratingarea);
d251b259 71
2c2ff8d5 72if (!$pluginpermissionsarray['rate']) {
5bda2d6a 73 $result->error = get_string('ratepermissiondenied', 'rating');
b1721f67
AD
74 echo json_encode($result);
75 die();
2c2ff8d5
AD
76} else {
77 $params = array(
2b04c41c
SH
78 'context' => $context,
79 'component' => $component,
80 'ratingarea' => $ratingarea,
81 'itemid' => $itemid,
82 'scaleid' => $scaleid,
83 'rating' => $userrating,
2c2ff8d5 84 'rateduserid' => $rateduserid,
2b04c41c
SH
85 'aggregation' => $aggregationmethod
86 );
87 if (!$rm->check_rating_is_valid($params)) {
2c2ff8d5
AD
88 $result->error = get_string('ratinginvalid', 'rating');
89 echo json_encode($result);
90 die();
91 }
b1721f67
AD
92}
93
279fcfcf 94//rating options used to update the rating then retrieve the aggregate
2b04c41c 95$ratingoptions = new stdClass;
b1721f67 96$ratingoptions->context = $context;
2b04c41c 97$ratingoptions->ratingarea = $ratingarea;
2c2ff8d5 98$ratingoptions->component = $component;
b1721f67
AD
99$ratingoptions->itemid = $itemid;
100$ratingoptions->scaleid = $scaleid;
3180bc2c 101$ratingoptions->userid = $USER->id;
b1721f67 102
279fcfcf
AD
103if ($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...
2b04c41c 107 $options = new stdClass;
279fcfcf 108 $options->contextid = $context->id;
2c2ff8d5 109 $options->component = $component;
2b04c41c 110 $options->ratingarea = $ratingarea;
279fcfcf
AD
111 $options->userid = $USER->id;
112 $options->itemid = $itemid;
5d354ded 113
279fcfcf
AD
114 $rm->delete_ratings($options);
115}
b1721f67 116
2b04c41c
SH
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
119if ($context->contextlevel == CONTEXT_MODULE) {
120 //tell the module that its grades have changed
121 $modinstance = $DB->get_record($cm->modname, array('id' => $cm->instance));
122 if ($modinstance) {
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);
63e87951
AD
128 }
129 }
2b04c41c 130}
63e87951 131
d251b259 132//object to return to client as json
d251b259
AD
133$result->success = true;
134
63e87951 135//need to retrieve the updated item to get its new aggregate value
2b04c41c 136$item = new stdClass;
279fcfcf 137$item->id = $itemid;
63e87951 138
279fcfcf 139//most of $ratingoptions variables were previously set
2b04c41c 140$ratingoptions->items = array($item);
63e87951
AD
141$ratingoptions->aggregate = $aggregationmethod;
142
63e87951 143$items = $rm->get_ratings($ratingoptions);
2b04c41c 144$firstrating = $items[0]->rating;
63e87951
AD
145
146//for custom scales return text not the value
147//this scales weirdness will go away when scales are refactored
148$scalearray = null;
2b04c41c 149$aggregatetoreturn = round($firstrating->aggregate, 1);
50e7d9da
AD
150
151// Output a dash if aggregation method == COUNT as the count is output next to the aggregate anyway
2b04c41c 152if ($firstrating->settings->aggregationmethod == RATING_AGGREGATE_COUNT or $firstrating->count == 0) {
50e7d9da 153 $aggregatetoreturn = ' - ';
2b04c41c 154} else if ($firstrating->settings->scale->id < 0) { //if its non-numeric scale
279fcfcf 155 //dont use the scale item if the aggregation method is sum as adding items from a custom scale makes no sense
2b04c41c
SH
156 if ($firstrating->settings->aggregationmethod != RATING_AGGREGATE_SUM) {
157 $scalerecord = $DB->get_record('scale', array('id' => -$firstrating->settings->scale->id));
50e7d9da
AD
158 if ($scalerecord) {
159 $scalearray = explode(',', $scalerecord->scale);
160 $aggregatetoreturn = $scalearray[$aggregatetoreturn-1];
161 }
63e87951 162 }
63e87951
AD
163}
164
d251b259 165//See if the user has permission to see the rating aggregate
2b04c41c 166if ($firstrating->user_can_view_aggregate()) {
d251b259 167 $result->aggregate = $aggregatetoreturn;
2b04c41c 168 $result->count = $firstrating->count;
279fcfcf 169 $result->itemid = $itemid;
d251b259
AD
170}
171
b1721f67 172echo json_encode($result);