2010-03-19 06:55:47 +00:00
|
|
|
<?php
|
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
/**
|
2014-07-20 10:14:46 +08:00
|
|
|
* This page receives ajax rating submissions
|
2010-03-19 06:55:47 +00:00
|
|
|
*
|
|
|
|
* It is similar to rate.php. Unlike rate.php a return url is NOT required.
|
|
|
|
*
|
2012-01-20 11:14:47 +08:00
|
|
|
* @package core_rating
|
|
|
|
* @category rating
|
2010-09-06 11:17:43 +00:00
|
|
|
* @copyright 2010 Andrew Davis
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2010-03-19 06:55:47 +00:00
|
|
|
*/
|
|
|
|
|
2010-08-15 06:26:16 +00:00
|
|
|
define('AJAX_SCRIPT', true);
|
|
|
|
|
2010-03-19 06:55:47 +00:00
|
|
|
require_once('../config.php');
|
2011-05-11 13:32:36 +08:00
|
|
|
require_once($CFG->dirroot.'/rating/lib.php');
|
|
|
|
|
|
|
|
$contextid = required_param('contextid', PARAM_INT);
|
2011-09-24 15:07:27 +02:00
|
|
|
$component = required_param('component', PARAM_COMPONENT);
|
|
|
|
$ratingarea = required_param('ratingarea', PARAM_AREA);
|
2011-05-11 13:32:36 +08:00
|
|
|
$itemid = required_param('itemid', PARAM_INT);
|
|
|
|
$scaleid = required_param('scaleid', PARAM_INT);
|
|
|
|
$userrating = required_param('rating', PARAM_INT);
|
2014-07-20 10:14:46 +08:00
|
|
|
$rateduserid = required_param('rateduserid', PARAM_INT); // The user being rated. Required to update their grade.
|
|
|
|
$aggregationmethod = optional_param('aggregation', RATING_AGGREGATE_NONE, PARAM_INT); // Used to calculate the aggregate to return.
|
2010-03-19 06:55:47 +00:00
|
|
|
|
|
|
|
$result = new stdClass;
|
|
|
|
|
2014-07-20 10:14:46 +08:00
|
|
|
// If session has expired and its an ajax request so we cant do a page redirect.
|
|
|
|
if (!isloggedin()) {
|
2010-03-19 06:55:47 +00:00
|
|
|
$result->error = get_string('sessionerroruser', 'error');
|
|
|
|
echo json_encode($result);
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
|
|
|
|
list($context, $course, $cm) = get_context_info_array($contextid);
|
|
|
|
require_login($course, false, $cm);
|
|
|
|
|
2014-07-20 10:14:46 +08:00
|
|
|
$contextid = null; // Now we have a context object, throw away the id from the user.
|
2011-04-01 17:20:33 +08:00
|
|
|
$PAGE->set_context($context);
|
2014-07-20 10:14:46 +08:00
|
|
|
$PAGE->set_url('/rating/rate_ajax.php', array('contextid' => $context->id));
|
2010-05-21 03:43:45 +00:00
|
|
|
|
2014-07-20 10:14:46 +08:00
|
|
|
if (!confirm_sesskey() || !has_capability('moodle/rating:rate', $context)) {
|
2010-05-21 03:43:45 +00:00
|
|
|
echo $OUTPUT->header();
|
2010-10-26 14:31:09 +00:00
|
|
|
echo get_string('ratepermissiondenied', 'rating');
|
2010-05-21 03:43:45 +00:00
|
|
|
echo $OUTPUT->footer();
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
|
2010-08-17 07:18:10 +00:00
|
|
|
$rm = new rating_manager();
|
2016-09-08 13:52:19 +01:00
|
|
|
$result = $rm->add_rating($cm, $context, $component, $ratingarea, $itemid, $scaleid, $userrating, $rateduserid, $aggregationmethod);
|
2010-08-17 07:18:10 +00:00
|
|
|
|
2016-09-08 13:52:19 +01:00
|
|
|
// Return translated error.
|
|
|
|
if (!empty($result->error)) {
|
|
|
|
$result->error = get_string($result->error, 'rating');
|
2010-04-23 09:44:19 +00:00
|
|
|
}
|
|
|
|
|
2014-07-20 10:14:46 +08:00
|
|
|
echo json_encode($result);
|