Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/rating/rate_ajax.php
Go to the documentation of this file.
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 define('AJAX_SCRIPT', true);
00030 
00031 require_once('../config.php');
00032 require_once($CFG->dirroot.'/rating/lib.php');
00033 
00034 $contextid         = required_param('contextid', PARAM_INT);
00035 $component         = required_param('component', PARAM_COMPONENT);
00036 $ratingarea        = required_param('ratingarea', PARAM_AREA);
00037 $itemid            = required_param('itemid', PARAM_INT);
00038 $scaleid           = required_param('scaleid', PARAM_INT);
00039 $userrating        = required_param('rating', PARAM_INT);
00040 $rateduserid       = required_param('rateduserid', PARAM_INT);//which user is being rated. Required to update their grade
00041 $aggregationmethod = optional_param('aggregation', RATING_AGGREGATE_NONE, PARAM_INT);//we're going to calculate the aggregate and return it to the client
00042 
00043 $result = new stdClass;
00044 
00045 //if session has expired and its an ajax request so we cant do a page redirect
00046 if( !isloggedin() ){
00047     $result->error = get_string('sessionerroruser', 'error');
00048     echo json_encode($result);
00049     die();
00050 }
00051 
00052 list($context, $course, $cm) = get_context_info_array($contextid);
00053 require_login($course, false, $cm);
00054 
00055 $contextid = null;//now we have a context object throw away the id from the user
00056 $PAGE->set_context($context);
00057 $PAGE->set_url('/rating/rate_ajax.php', array('contextid'=>$context->id));
00058 
00059 if (!confirm_sesskey() || !has_capability('moodle/rating:rate',$context)) {
00060     echo $OUTPUT->header();
00061     echo get_string('ratepermissiondenied', 'rating');
00062     echo $OUTPUT->footer();
00063     die();
00064 }
00065 
00066 $rm = new rating_manager();
00067 
00068 //check the module rating permissions
00069 //doing this check here rather than within rating_manager::get_ratings() so we can return a json error response
00070 $pluginpermissionsarray = $rm->get_plugin_permissions_array($context->id, $component, $ratingarea);
00071 
00072 if (!$pluginpermissionsarray['rate']) {
00073     $result->error = get_string('ratepermissiondenied', 'rating');
00074     echo json_encode($result);
00075     die();
00076 } else {
00077     $params = array(
00078         'context'     => $context,
00079         'component'   => $component,
00080         'ratingarea'  => $ratingarea,
00081         'itemid'      => $itemid,
00082         'scaleid'     => $scaleid,
00083         'rating'      => $userrating,
00084         'rateduserid' => $rateduserid,
00085         'aggregation' => $aggregationmethod
00086     );
00087     if (!$rm->check_rating_is_valid($params)) {
00088         $result->error = get_string('ratinginvalid', 'rating');
00089         echo json_encode($result);
00090         die();
00091     }
00092 }
00093 
00094 //rating options used to update the rating then retrieve the aggregate
00095 $ratingoptions = new stdClass;
00096 $ratingoptions->context = $context;
00097 $ratingoptions->ratingarea = $ratingarea;
00098 $ratingoptions->component = $component;
00099 $ratingoptions->itemid  = $itemid;
00100 $ratingoptions->scaleid = $scaleid;
00101 $ratingoptions->userid  = $USER->id;
00102 
00103 if ($userrating != RATING_UNSET_RATING) {
00104     $rating = new rating($ratingoptions);
00105     $rating->update_rating($userrating);
00106 } else { //delete the rating if the user set to Rate...
00107     $options = new stdClass;
00108     $options->contextid = $context->id;
00109     $options->component = $component;
00110     $options->ratingarea = $ratingarea;
00111     $options->userid = $USER->id;
00112     $options->itemid = $itemid;
00113 
00114     $rm->delete_ratings($options);
00115 }
00116 
00117 // Future possible enhancement: add a setting to turn grade updating off for those who don't want them in gradebook
00118 // note that this would need to be done in both rate.php and rate_ajax.php
00119 if ($context->contextlevel == CONTEXT_MODULE) {
00120     //tell the module that its grades have changed
00121     $modinstance = $DB->get_record($cm->modname, array('id' => $cm->instance));
00122     if ($modinstance) {
00123         $modinstance->cmidnumber = $cm->id; //MDL-12961
00124         $functionname = $cm->modname.'_update_grades';
00125         require_once($CFG->dirroot."/mod/{$cm->modname}/lib.php");
00126         if (function_exists($functionname)) {
00127             $functionname($modinstance, $rateduserid);
00128         }
00129     }
00130 }
00131 
00132 //object to return to client as json
00133 $result->success = true;
00134 
00135 //need to retrieve the updated item to get its new aggregate value
00136 $item = new stdClass;
00137 $item->id = $itemid;
00138 
00139 //most of $ratingoptions variables were previously set
00140 $ratingoptions->items = array($item);
00141 $ratingoptions->aggregate = $aggregationmethod;
00142 
00143 $items = $rm->get_ratings($ratingoptions);
00144 $firstrating = $items[0]->rating;
00145 
00146 //for custom scales return text not the value
00147 //this scales weirdness will go away when scales are refactored
00148 $scalearray = null;
00149 $aggregatetoreturn = round($firstrating->aggregate, 1);
00150 
00151 // Output a dash if aggregation method == COUNT as the count is output next to the aggregate anyway
00152 if ($firstrating->settings->aggregationmethod == RATING_AGGREGATE_COUNT or $firstrating->count == 0) {
00153     $aggregatetoreturn = ' - ';
00154 } else if ($firstrating->settings->scale->id < 0) { //if its non-numeric scale
00155     //dont use the scale item if the aggregation method is sum as adding items from a custom scale makes no sense
00156     if ($firstrating->settings->aggregationmethod != RATING_AGGREGATE_SUM) {
00157         $scalerecord = $DB->get_record('scale', array('id' => -$firstrating->settings->scale->id));
00158         if ($scalerecord) {
00159             $scalearray = explode(',', $scalerecord->scale);
00160             $aggregatetoreturn = $scalearray[$aggregatetoreturn-1];
00161         }
00162     }
00163 }
00164 
00165 //See if the user has permission to see the rating aggregate
00166 if ($firstrating->user_can_view_aggregate()) {
00167     $result->aggregate = $aggregatetoreturn;
00168     $result->count = $firstrating->count;
00169     $result->itemid = $itemid;
00170 }
00171 
00172 echo json_encode($result);
 All Data Structures Namespaces Files Functions Variables Enumerations