MDL-65992 travis: Migrate to Xenial distro and default MySQL service
[moodle.git] / rating / rate_ajax.php
CommitLineData
b1721f67 1<?php
b1721f67
AD
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.
13//
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/>.
16
17/**
d28a6a5f 18 * This page receives ajax rating submissions
b1721f67
AD
19 *
20 * It is similar to rate.php. Unlike rate.php a return url is NOT required.
21 *
8c335cff
JF
22 * @package core_rating
23 * @category rating
5d354ded
PS
24 * @copyright 2010 Andrew Davis
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
b1721f67
AD
26 */
27
1bb1e5be
AD
28define('AJAX_SCRIPT', true);
29
b1721f67 30require_once('../config.php');
2b04c41c
SH
31require_once($CFG->dirroot.'/rating/lib.php');
32
33$contextid = required_param('contextid', PARAM_INT);
aff24313
PS
34$component = required_param('component', PARAM_COMPONENT);
35$ratingarea = required_param('ratingarea', PARAM_AREA);
2b04c41c
SH
36$itemid = required_param('itemid', PARAM_INT);
37$scaleid = required_param('scaleid', PARAM_INT);
38$userrating = required_param('rating', PARAM_INT);
d28a6a5f
AD
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.
b1721f67
AD
41
42$result = new stdClass;
43
d28a6a5f
AD
44// If session has expired and its an ajax request so we cant do a page redirect.
45if (!isloggedin()) {
b1721f67
AD
46 $result->error = get_string('sessionerroruser', 'error');
47 echo json_encode($result);
48 die();
49}
50
51list($context, $course, $cm) = get_context_info_array($contextid);
52require_login($course, false, $cm);
53
d28a6a5f 54$contextid = null; // Now we have a context object, throw away the id from the user.
2c2ff8d5 55$PAGE->set_context($context);
d28a6a5f 56$PAGE->set_url('/rating/rate_ajax.php', array('contextid' => $context->id));
3180bc2c 57
d28a6a5f 58if (!confirm_sesskey() || !has_capability('moodle/rating:rate', $context)) {
3180bc2c 59 echo $OUTPUT->header();
5bda2d6a 60 echo get_string('ratepermissiondenied', 'rating');
3180bc2c
AD
61 echo $OUTPUT->footer();
62 die();
63}
64
279fcfcf 65$rm = new rating_manager();
a497458c 66$result = $rm->add_rating($cm, $context, $component, $ratingarea, $itemid, $scaleid, $userrating, $rateduserid, $aggregationmethod);
279fcfcf 67
a497458c
JL
68// Return translated error.
69if (!empty($result->error)) {
70 $result->error = get_string($result->error, 'rating');
d251b259
AD
71}
72
d28a6a5f 73echo json_encode($result);