MDL-65992 travis: Migrate to Xenial distro and default MySQL service
[moodle.git] / rating / rate.php
CommitLineData
a09aeee4 1<?php
a09aeee4
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/**
b1721f67 18 * This page receives non-ajax rating submissions
a09aeee4 19 *
b1721f67 20 * It is similar to rate_ajax.php. Unlike rate_ajax.php a return url is required.
a09aeee4 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
a09aeee4
AD
26 */
27
28require_once('../config.php');
2b04c41c
SH
29require_once($CFG->dirroot.'/rating/lib.php');
30
31$contextid = required_param('contextid', PARAM_INT);
aff24313
PS
32$component = required_param('component', PARAM_COMPONENT);
33$ratingarea = required_param('ratingarea', PARAM_AREA);
2b04c41c
SH
34$itemid = required_param('itemid', PARAM_INT);
35$scaleid = required_param('scaleid', PARAM_INT);
36$userrating = required_param('rating', PARAM_INT);
d28a6a5f
AD
37$rateduserid = required_param('rateduserid', PARAM_INT); // Which user is being rated. Required to update their grade.
38$returnurl = required_param('returnurl', PARAM_LOCALURL); // Required for non-ajax requests.
a09aeee4 39
a09aeee4
AD
40$result = new stdClass;
41
f1f6a755
AD
42list($context, $course, $cm) = get_context_info_array($contextid);
43require_login($course, false, $cm);
a09aeee4 44
d28a6a5f 45$contextid = null; // Now we have a context object, throw away the id from the user.
2c2ff8d5 46$PAGE->set_context($context);
2b04c41c 47$PAGE->set_url('/rating/rate.php', array('contextid' => $context->id));
3180bc2c 48
d28a6a5f 49if (!confirm_sesskey() || !has_capability('moodle/rating:rate', $context)) {
27dbf262 50 print_error('ratepermissiondenied', 'rating');
3180bc2c
AD
51}
52
59b0f797 53$rm = new rating_manager();
54
d28a6a5f 55// Check the module rating permissions.
27dbf262 56// Doing this check here rather than within rating_manager::get_ratings() so we can choose how to handle the error.
2b04c41c 57$pluginpermissionsarray = $rm->get_plugin_permissions_array($context->id, $component, $ratingarea);
d251b259 58
2c2ff8d5 59if (!$pluginpermissionsarray['rate']) {
27dbf262 60 print_error('ratepermissiondenied', 'rating');
2c2ff8d5
AD
61} else {
62 $params = array(
2b04c41c
SH
63 'context' => $context,
64 'component' => $component,
65 'ratingarea' => $ratingarea,
66 'itemid' => $itemid,
67 'scaleid' => $scaleid,
68 'rating' => $userrating,
69 'rateduserid' => $rateduserid
70 );
71 if (!$rm->check_rating_is_valid($params)) {
2c2ff8d5
AD
72 echo $OUTPUT->header();
73 echo get_string('ratinginvalid', 'rating');
74 echo $OUTPUT->footer();
75 die();
76 }
a09aeee4
AD
77}
78
279fcfcf 79if ($userrating != RATING_UNSET_RATING) {
2b04c41c 80 $ratingoptions = new stdClass;
279fcfcf 81 $ratingoptions->context = $context;
2c2ff8d5 82 $ratingoptions->component = $component;
2b04c41c 83 $ratingoptions->ratingarea = $ratingarea;
279fcfcf
AD
84 $ratingoptions->itemid = $itemid;
85 $ratingoptions->scaleid = $scaleid;
86 $ratingoptions->userid = $USER->id;
87
88 $rating = new rating($ratingoptions);
89 $rating->update_rating($userrating);
d28a6a5f 90} else { // Delete the rating if the user set to "Rate..."
2b04c41c 91 $options = new stdClass;
279fcfcf 92 $options->contextid = $context->id;
2c2ff8d5 93 $options->component = $component;
2b04c41c 94 $options->ratingarea = $ratingarea;
279fcfcf
AD
95 $options->userid = $USER->id;
96 $options->itemid = $itemid;
97
98 $rm->delete_ratings($options);
99}
a09aeee4 100
2b04c41c 101if (!empty($cm) && $context->contextlevel == CONTEXT_MODULE) {
d28a6a5f 102 // Tell the module that its grades have changed.
2b04c41c 103 $modinstance = $DB->get_record($cm->modname, array('id' => $cm->instance), '*', MUST_EXIST);
d28a6a5f 104 $modinstance->cmidnumber = $cm->id; // MDL-12961.
63e87951
AD
105 $functionname = $cm->modname.'_update_grades';
106 require_once($CFG->dirroot."/mod/{$cm->modname}/lib.php");
2b04c41c 107 if (function_exists($functionname)) {
63e87951
AD
108 $functionname($modinstance, $rateduserid);
109 }
110}
111
d28a6a5f 112redirect($returnurl);