|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00029 require_once('../config.php'); 00030 require_once($CFG->dirroot.'/rating/lib.php'); 00031 00032 $contextid = required_param('contextid', PARAM_INT); 00033 $component = required_param('component', PARAM_COMPONENT); 00034 $ratingarea = required_param('ratingarea', PARAM_AREA); 00035 $itemid = required_param('itemid', PARAM_INT); 00036 $scaleid = required_param('scaleid', PARAM_INT); 00037 $userrating = required_param('rating', PARAM_INT); 00038 $rateduserid = required_param('rateduserid', PARAM_INT);//which user is being rated. Required to update their grade 00039 $returnurl = required_param('returnurl', PARAM_LOCALURL);//required for non-ajax requests 00040 00041 $result = new stdClass; 00042 00043 list($context, $course, $cm) = get_context_info_array($contextid); 00044 require_login($course, false, $cm); 00045 00046 $contextid = null;//now we have a context object throw away the id from the user 00047 $PAGE->set_context($context); 00048 $PAGE->set_url('/rating/rate.php', array('contextid' => $context->id)); 00049 00050 if (!confirm_sesskey() || !has_capability('moodle/rating:rate',$context)) { 00051 echo $OUTPUT->header(); 00052 echo get_string('ratepermissiondenied', 'rating'); 00053 echo $OUTPUT->footer(); 00054 die(); 00055 } 00056 00057 $rm = new rating_manager(); 00058 00059 //check the module rating permissions 00060 //doing this check here rather than within rating_manager::get_ratings() so we can return a json error response 00061 $pluginpermissionsarray = $rm->get_plugin_permissions_array($context->id, $component, $ratingarea); 00062 00063 if (!$pluginpermissionsarray['rate']) { 00064 $result->error = get_string('ratepermissiondenied', 'rating'); 00065 echo json_encode($result); 00066 die(); 00067 } else { 00068 $params = array( 00069 'context' => $context, 00070 'component' => $component, 00071 'ratingarea' => $ratingarea, 00072 'itemid' => $itemid, 00073 'scaleid' => $scaleid, 00074 'rating' => $userrating, 00075 'rateduserid' => $rateduserid 00076 ); 00077 if (!$rm->check_rating_is_valid($params)) { 00078 echo $OUTPUT->header(); 00079 echo get_string('ratinginvalid', 'rating'); 00080 echo $OUTPUT->footer(); 00081 die(); 00082 } 00083 } 00084 00085 if ($userrating != RATING_UNSET_RATING) { 00086 $ratingoptions = new stdClass; 00087 $ratingoptions->context = $context; 00088 $ratingoptions->component = $component; 00089 $ratingoptions->ratingarea = $ratingarea; 00090 $ratingoptions->itemid = $itemid; 00091 $ratingoptions->scaleid = $scaleid; 00092 $ratingoptions->userid = $USER->id; 00093 00094 $rating = new rating($ratingoptions); 00095 $rating->update_rating($userrating); 00096 } else { //delete the rating if the user set to Rate... 00097 $options = new stdClass; 00098 $options->contextid = $context->id; 00099 $options->component = $component; 00100 $options->ratingarea = $ratingarea; 00101 $options->userid = $USER->id; 00102 $options->itemid = $itemid; 00103 00104 $rm->delete_ratings($options); 00105 } 00106 00107 //todo add a setting to turn grade updating off for those who don't want them in gradebook 00108 //note that this needs to be done in both rate.php and rate_ajax.php 00109 if (!empty($cm) && $context->contextlevel == CONTEXT_MODULE) { 00110 //tell the module that its grades have changed 00111 $modinstance = $DB->get_record($cm->modname, array('id' => $cm->instance), '*', MUST_EXIST); 00112 $modinstance->cmidnumber = $cm->id; //MDL-12961 00113 $functionname = $cm->modname.'_update_grades'; 00114 require_once($CFG->dirroot."/mod/{$cm->modname}/lib.php"); 00115 if (function_exists($functionname)) { 00116 $functionname($modinstance, $rateduserid); 00117 } 00118 } 00119 00120 redirect($returnurl);