MDL-20636 fix most of the remaining codechecker issues in mod/quiz and lib/questionli...
[moodle.git] / rating / rate.php
blobf9b881a62ba56502ad34d7757cd14d612a96a44c
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
24 * @subpackage 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('lib.php');
32 $contextid = required_param('contextid', PARAM_INT);
33 $component = required_param('component', PARAM_ALPHAEXT);
34 $itemid = required_param('itemid', PARAM_INT);
35 $scaleid = required_param('scaleid', PARAM_INT);
36 $userrating = required_param('rating', PARAM_INT);
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
40 $result = new stdClass;
42 list($context, $course, $cm) = get_context_info_array($contextid);
43 require_login($course, false, $cm);
45 $contextid = null;//now we have a context object throw away the id from the user
46 $PAGE->set_context($context);
47 $PAGE->set_url('/rating/rate.php', array('contextid'=>$context->id));
49 if (!confirm_sesskey() || !has_capability('moodle/rating:rate',$context)) {
50 echo $OUTPUT->header();
51 echo get_string('ratepermissiondenied', 'rating');
52 echo $OUTPUT->footer();
53 die();
56 $rm = new rating_manager();
58 //check the module rating permissions
59 //doing this check here rather than within rating_manager::get_ratings() so we can return a json error response
60 $pluginpermissionsarray = $rm->get_plugin_permissions_array($context->id, $component);
62 if (!$pluginpermissionsarray['rate']) {
63 $result->error = get_string('ratepermissiondenied', 'rating');
64 echo json_encode($result);
65 die();
66 } else {
67 $params = array(
68 'context' => $context,
69 'itemid' => $itemid,
70 'scaleid' => $scaleid,
71 'rating' => $userrating,
72 'rateduserid' => $rateduserid);
74 if (!$rm->check_rating_is_valid($component, $params)) {
75 echo $OUTPUT->header();
76 echo get_string('ratinginvalid', 'rating');
77 echo $OUTPUT->footer();
78 die();
82 if ($userrating != RATING_UNSET_RATING) {
83 $ratingoptions = new stdClass();
84 $ratingoptions->context = $context;
85 $ratingoptions->component = $component;
86 $ratingoptions->itemid = $itemid;
87 $ratingoptions->scaleid = $scaleid;
88 $ratingoptions->userid = $USER->id;
90 $rating = new rating($ratingoptions);
91 $rating->update_rating($userrating);
92 } else { //delete the rating if the user set to Rate...
93 $options = new stdClass();
94 $options->contextid = $context->id;
95 $options->component = $component;
96 $options->userid = $USER->id;
97 $options->itemid = $itemid;
99 $rm->delete_ratings($options);
102 //todo add a setting to turn grade updating off for those who don't want them in gradebook
103 //note that this needs to be done in both rate.php and rate_ajax.php
104 if(true){
105 //tell the module that its grades have changed
106 if ( !$modinstance = $DB->get_record($cm->modname, array('id' => $cm->instance)) ) {
107 print_error('invalidid');
109 $modinstance->cmidnumber = $cm->id; //MDL-12961
110 $functionname = $cm->modname.'_update_grades';
111 require_once($CFG->dirroot."/mod/{$cm->modname}/lib.php");
112 if(function_exists($functionname)) {
113 $functionname($modinstance, $rateduserid);
117 redirect($returnurl);