MDL-40308: Fix javascript for lti mod_form
[moodle.git] / rating / rate.php
CommitLineData
a09aeee4
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/**
b1721f67 19 * This page receives non-ajax rating submissions
a09aeee4 20 *
b1721f67 21 * It is similar to rate_ajax.php. Unlike rate_ajax.php a return url is required.
a09aeee4 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
a09aeee4
AD
27 */
28
29require_once('../config.php');
2b04c41c
SH
30require_once($CFG->dirroot.'/rating/lib.php');
31
32$contextid = required_param('contextid', PARAM_INT);
aff24313
PS
33$component = required_param('component', PARAM_COMPONENT);
34$ratingarea = required_param('ratingarea', PARAM_AREA);
2b04c41c
SH
35$itemid = required_param('itemid', PARAM_INT);
36$scaleid = required_param('scaleid', PARAM_INT);
37$userrating = required_param('rating', PARAM_INT);
63e87951 38$rateduserid = required_param('rateduserid', PARAM_INT);//which user is being rated. Required to update their grade
2b04c41c 39$returnurl = required_param('returnurl', PARAM_LOCALURL);//required for non-ajax requests
a09aeee4 40
a09aeee4
AD
41$result = new stdClass;
42
f1f6a755
AD
43list($context, $course, $cm) = get_context_info_array($contextid);
44require_login($course, false, $cm);
a09aeee4 45
3180bc2c 46$contextid = null;//now we have a context object throw away the id from the user
2c2ff8d5 47$PAGE->set_context($context);
2b04c41c 48$PAGE->set_url('/rating/rate.php', array('contextid' => $context->id));
3180bc2c 49
778361c3 50if (!confirm_sesskey() || !has_capability('moodle/rating:rate',$context)) {
3180bc2c 51 echo $OUTPUT->header();
5bda2d6a 52 echo get_string('ratepermissiondenied', 'rating');
3180bc2c
AD
53 echo $OUTPUT->footer();
54 die();
55}
56
59b0f797 57$rm = new rating_manager();
58
d251b259 59//check the module rating permissions
2c2ff8d5 60//doing this check here rather than within rating_manager::get_ratings() so we can return a json error response
2b04c41c 61$pluginpermissionsarray = $rm->get_plugin_permissions_array($context->id, $component, $ratingarea);
d251b259 62
2c2ff8d5
AD
63if (!$pluginpermissionsarray['rate']) {
64 $result->error = get_string('ratepermissiondenied', 'rating');
65 echo json_encode($result);
a09aeee4 66 die();
2c2ff8d5
AD
67} else {
68 $params = array(
2b04c41c
SH
69 'context' => $context,
70 'component' => $component,
71 'ratingarea' => $ratingarea,
72 'itemid' => $itemid,
73 'scaleid' => $scaleid,
74 'rating' => $userrating,
75 'rateduserid' => $rateduserid
76 );
77 if (!$rm->check_rating_is_valid($params)) {
2c2ff8d5
AD
78 echo $OUTPUT->header();
79 echo get_string('ratinginvalid', 'rating');
80 echo $OUTPUT->footer();
81 die();
82 }
a09aeee4
AD
83}
84
279fcfcf 85if ($userrating != RATING_UNSET_RATING) {
2b04c41c 86 $ratingoptions = new stdClass;
279fcfcf 87 $ratingoptions->context = $context;
2c2ff8d5 88 $ratingoptions->component = $component;
2b04c41c 89 $ratingoptions->ratingarea = $ratingarea;
279fcfcf
AD
90 $ratingoptions->itemid = $itemid;
91 $ratingoptions->scaleid = $scaleid;
92 $ratingoptions->userid = $USER->id;
93
94 $rating = new rating($ratingoptions);
95 $rating->update_rating($userrating);
96} else { //delete the rating if the user set to Rate...
2b04c41c 97 $options = new stdClass;
279fcfcf 98 $options->contextid = $context->id;
2c2ff8d5 99 $options->component = $component;
2b04c41c 100 $options->ratingarea = $ratingarea;
279fcfcf
AD
101 $options->userid = $USER->id;
102 $options->itemid = $itemid;
103
104 $rm->delete_ratings($options);
105}
a09aeee4 106
63e87951
AD
107//todo add a setting to turn grade updating off for those who don't want them in gradebook
108//note that this needs to be done in both rate.php and rate_ajax.php
2b04c41c 109if (!empty($cm) && $context->contextlevel == CONTEXT_MODULE) {
63e87951 110 //tell the module that its grades have changed
2b04c41c 111 $modinstance = $DB->get_record($cm->modname, array('id' => $cm->instance), '*', MUST_EXIST);
63e87951
AD
112 $modinstance->cmidnumber = $cm->id; //MDL-12961
113 $functionname = $cm->modname.'_update_grades';
114 require_once($CFG->dirroot."/mod/{$cm->modname}/lib.php");
2b04c41c 115 if (function_exists($functionname)) {
63e87951
AD
116 $functionname($modinstance, $rateduserid);
117 }
118}
119
120redirect($returnurl);