MDL-55393 message: Add some external functions to mobile service
[moodle.git] / rating / rate.php
blob0ec08c8c624c3e772efc21f9be3b5efd5290ad10
1 <?php
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.
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/>.
17 /**
18 * This page receives non-ajax rating submissions
20 * It is similar to rate_ajax.php. Unlike rate_ajax.php a return url is required.
22 * @package core_rating
23 * @category rating
24 * @copyright 2010 Andrew Davis
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 require_once('../config.php');
29 require_once($CFG->dirroot.'/rating/lib.php');
31 $contextid = required_param('contextid', PARAM_INT);
32 $component = required_param('component', PARAM_COMPONENT);
33 $ratingarea = required_param('ratingarea', PARAM_AREA);
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 print_error('ratepermissiondenied', 'rating');
53 $rm = new rating_manager();
55 // Check the module rating permissions.
56 // Doing this check here rather than within rating_manager::get_ratings() so we can choose how to handle the error.
57 $pluginpermissionsarray = $rm->get_plugin_permissions_array($context->id, $component, $ratingarea);
59 if (!$pluginpermissionsarray['rate']) {
60 print_error('ratepermissiondenied', 'rating');
61 } else {
62 $params = array(
63 'context' => $context,
64 'component' => $component,
65 'ratingarea' => $ratingarea,
66 'itemid' => $itemid,
67 'scaleid' => $scaleid,
68 'rating' => $userrating,
69 'rateduserid' => $rateduserid
71 if (!$rm->check_rating_is_valid($params)) {
72 echo $OUTPUT->header();
73 echo get_string('ratinginvalid', 'rating');
74 echo $OUTPUT->footer();
75 die();
79 if ($userrating != RATING_UNSET_RATING) {
80 $ratingoptions = new stdClass;
81 $ratingoptions->context = $context;
82 $ratingoptions->component = $component;
83 $ratingoptions->ratingarea = $ratingarea;
84 $ratingoptions->itemid = $itemid;
85 $ratingoptions->scaleid = $scaleid;
86 $ratingoptions->userid = $USER->id;
88 $rating = new rating($ratingoptions);
89 $rating->update_rating($userrating);
90 } else { // Delete the rating if the user set to "Rate..."
91 $options = new stdClass;
92 $options->contextid = $context->id;
93 $options->component = $component;
94 $options->ratingarea = $ratingarea;
95 $options->userid = $USER->id;
96 $options->itemid = $itemid;
98 $rm->delete_ratings($options);
101 if (!empty($cm) && $context->contextlevel == CONTEXT_MODULE) {
102 // Tell the module that its grades have changed.
103 $modinstance = $DB->get_record($cm->modname, array('id' => $cm->instance), '*', MUST_EXIST);
104 $modinstance->cmidnumber = $cm->id; // MDL-12961.
105 $functionname = $cm->modname.'_update_grades';
106 require_once($CFG->dirroot."/mod/{$cm->modname}/lib.php");
107 if (function_exists($functionname)) {
108 $functionname($modinstance, $rateduserid);
112 redirect($returnurl);