MDL-50154 behat: Resize pdf window so toolbar is always visible
[moodle.git] / rating / rate.php
blob6d5956333f1d4d059a812194e43d78ace7b527ae
1 <?php
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.
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/>.
18 /**
19 * This page receives non-ajax rating submissions
21 * It is similar to rate_ajax.php. Unlike rate_ajax.php a return url is required.
23 * @package core_rating
24 * @category rating
25 * @copyright 2010 Andrew Davis
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 require_once('../config.php');
30 require_once($CFG->dirroot.'/rating/lib.php');
32 $contextid = required_param('contextid', PARAM_INT);
33 $component = required_param('component', PARAM_COMPONENT);
34 $ratingarea = required_param('ratingarea', PARAM_AREA);
35 $itemid = required_param('itemid', PARAM_INT);
36 $scaleid = required_param('scaleid', PARAM_INT);
37 $userrating = required_param('rating', PARAM_INT);
38 $rateduserid = required_param('rateduserid', PARAM_INT);//which user is being rated. Required to update their grade
39 $returnurl = required_param('returnurl', PARAM_LOCALURL);//required for non-ajax requests
41 $result = new stdClass;
43 list($context, $course, $cm) = get_context_info_array($contextid);
44 require_login($course, false, $cm);
46 $contextid = null;//now we have a context object throw away the id from the user
47 $PAGE->set_context($context);
48 $PAGE->set_url('/rating/rate.php', array('contextid' => $context->id));
50 if (!confirm_sesskey() || !has_capability('moodle/rating:rate',$context)) {
51 echo $OUTPUT->header();
52 echo get_string('ratepermissiondenied', 'rating');
53 echo $OUTPUT->footer();
54 die();
57 $rm = new rating_manager();
59 //check the module rating permissions
60 //doing this check here rather than within rating_manager::get_ratings() so we can return a json error response
61 $pluginpermissionsarray = $rm->get_plugin_permissions_array($context->id, $component, $ratingarea);
63 if (!$pluginpermissionsarray['rate']) {
64 $result->error = get_string('ratepermissiondenied', 'rating');
65 echo json_encode($result);
66 die();
67 } else {
68 $params = array(
69 'context' => $context,
70 'component' => $component,
71 'ratingarea' => $ratingarea,
72 'itemid' => $itemid,
73 'scaleid' => $scaleid,
74 'rating' => $userrating,
75 'rateduserid' => $rateduserid
77 if (!$rm->check_rating_is_valid($params)) {
78 echo $OUTPUT->header();
79 echo get_string('ratinginvalid', 'rating');
80 echo $OUTPUT->footer();
81 die();
85 if ($userrating != RATING_UNSET_RATING) {
86 $ratingoptions = new stdClass;
87 $ratingoptions->context = $context;
88 $ratingoptions->component = $component;
89 $ratingoptions->ratingarea = $ratingarea;
90 $ratingoptions->itemid = $itemid;
91 $ratingoptions->scaleid = $scaleid;
92 $ratingoptions->userid = $USER->id;
94 $rating = new rating($ratingoptions);
95 $rating->update_rating($userrating);
96 } else { //delete the rating if the user set to Rate...
97 $options = new stdClass;
98 $options->contextid = $context->id;
99 $options->component = $component;
100 $options->ratingarea = $ratingarea;
101 $options->userid = $USER->id;
102 $options->itemid = $itemid;
104 $rm->delete_ratings($options);
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
109 if (!empty($cm) && $context->contextlevel == CONTEXT_MODULE) {
110 //tell the module that its grades have changed
111 $modinstance = $DB->get_record($cm->modname, array('id' => $cm->instance), '*', MUST_EXIST);
112 $modinstance->cmidnumber = $cm->id; //MDL-12961
113 $functionname = $cm->modname.'_update_grades';
114 require_once($CFG->dirroot."/mod/{$cm->modname}/lib.php");
115 if (function_exists($functionname)) {
116 $functionname($modinstance, $rateduserid);
120 redirect($returnurl);