Merge branch 'MDL-64012' of https://github.com/timhunt/moodle
[moodle.git] / rating / rate_ajax.php
blob859824270b29ad0fe9204ac9440cb57f3f48bc37
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();
66 $result = $rm->add_rating($cm, $context, $component, $ratingarea, $itemid, $scaleid, $userrating, $rateduserid, $aggregationmethod);
68 // Return translated error.
69 if (!empty($result->error)) {
70 $result->error = get_string($result->error, 'rating');
73 echo json_encode($result);