From 279fcfcfa40a10ecb671d784e3d652999cfadf71 Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Tue, 17 Aug 2010 07:18:10 +0000 Subject: [PATCH] rating MDL-23814 made an assortment of fixes to the ratings code --- rating/lib.php | 9 +++++++++ rating/module.js | 9 +++++++-- rating/rate.php | 27 +++++++++++++++++---------- rating/rate_ajax.php | 34 ++++++++++++++++++++-------------- 4 files changed, 53 insertions(+), 26 deletions(-) diff --git a/rating/lib.php b/rating/lib.php index dbc59e801e3..60da7d7d62f 100644 --- a/rating/lib.php +++ b/rating/lib.php @@ -168,6 +168,7 @@ class rating_manager { * @param object $options { * contextid => int the context in which the ratings exist [required] * ratingid => int the id of an individual rating to delete [optional] + * userid => int delete the ratings submitted by this user. May be used in conjuction with itemid [optional] * itemid => int delete all ratings attached to this item [optional] * } * @return void @@ -179,10 +180,18 @@ class rating_manager { //delete a single rating $DB->delete_records('rating', array('contextid'=>$options->contextid, 'id'=>$options->ratingid) ); } + else if( !empty($options->itemid) && !empty($options->userid) ) { + //delete the rating for an item submitted by a particular user + $DB->delete_records('rating', array('contextid'=>$options->contextid, 'itemid'=>$options->itemid, 'userid'=>$options->userid) ); + } else if( !empty($options->itemid) ) { //delete all ratings for an item $DB->delete_records('rating', array('contextid'=>$options->contextid, 'itemid'=>$options->itemid) ); } + else if( !empty($options->userid) ) { + //delete all ratings submitted by a user + $DB->delete_records('rating', array('contextid'=>$options->contextid, 'userid'=>$options->userid) ); + } else { //delete all ratings for this context $DB->delete_records('rating', array('contextid'=>$options->contextid) ); diff --git a/rating/module.js b/rating/module.js index 78cf643fb0b..537024b8326 100644 --- a/rating/module.js +++ b/rating/module.js @@ -42,14 +42,19 @@ M.core_rating={ var data = this.Y.JSON.parse(outcome.responseText); if (data.success){ //if the user has access to the aggregate then update it - if (data.itemid && data.aggregate && data.count) { + if (data.itemid) { //do not test data.aggregate or data.count otherwise it doesn't refresh value=0 or no value var itemid = data.itemid; var node = this.Y.one('#ratingaggregate'+itemid); node.set('innerHTML',data.aggregate); + //empty the count value if no ratings var node = this.Y.one('#ratingcount'+itemid); - node.set('innerHTML',"("+data.count+")"); + if (data.count > 0) { + node.set('innerHTML',"("+data.count+")"); + } else { + node.set('innerHTML',""); + } } return true; } diff --git a/rating/rate.php b/rating/rate.php index cf33c70ee6e..fc5f0c12f04 100644 --- a/rating/rate.php +++ b/rating/rate.php @@ -72,18 +72,25 @@ if (!$pluginrateallowed || !has_capability('moodle/rating:rate',$context)) { die(); } -$PAGE->set_url('/lib/rate.php', array( - 'contextid'=>$context->id - )); +$PAGE->set_url('/lib/rate.php', array('contextid'=>$context->id)); -$ratingoptions = new stdclass; -$ratingoptions->context = $context; -$ratingoptions->itemid = $itemid; -$ratingoptions->scaleid = $scaleid; -$ratingoptions->userid = $USER->id; -$rating = new rating($ratingoptions); +if ($userrating != RATING_UNSET_RATING) { + $ratingoptions = new stdclass; + $ratingoptions->context = $context; + $ratingoptions->itemid = $itemid; + $ratingoptions->scaleid = $scaleid; + $ratingoptions->userid = $USER->id; -$rating->update_rating($userrating); + $rating = new rating($ratingoptions); + $rating->update_rating($userrating); +} else { //delete the rating if the user set to Rate... + $options = new stdClass(); + $options->contextid = $context->id; + $options->userid = $USER->id; + $options->itemid = $itemid; + + $rm->delete_ratings($options); +} //todo add a setting to turn grade updating off for those who don't want them in gradebook //note that this needs to be done in both rate.php and rate_ajax.php diff --git a/rating/rate_ajax.php b/rating/rate_ajax.php index 1c32c9df437..ab240d475d8 100644 --- a/rating/rate_ajax.php +++ b/rating/rate_ajax.php @@ -58,6 +58,8 @@ if (!confirm_sesskey() || $USER->id==$rateduserid) { die(); } +$rm = new rating_manager(); + //check the module rating permissions //doing this check here rather than within rating_manager::get_ratings so we can return a json error response $pluginrateallowed = true; @@ -65,7 +67,6 @@ $pluginpermissionsarray = null; if ($context->contextlevel==CONTEXT_MODULE) { $plugintype = 'mod'; $pluginname = $cm->modname; - $rm = new rating_manager(); $pluginpermissionsarray = $rm->get_plugin_permissions_array($context->id, $plugintype, $pluginname); $pluginrateallowed = $pluginpermissionsarray['rate']; @@ -81,19 +82,26 @@ if (!$pluginrateallowed || !has_capability('moodle/rating:rate',$context)) { die(); } -$PAGE->set_url('/lib/rate.php', array( - 'contextid'=>$context->id - )); - +$PAGE->set_url('/lib/rate.php', array('contextid'=>$context->id)); +//rating options used to update the rating then retrieve the aggregate $ratingoptions = new stdclass; $ratingoptions->context = $context; $ratingoptions->itemid = $itemid; $ratingoptions->scaleid = $scaleid; $ratingoptions->userid = $USER->id; -$rating = new rating($ratingoptions); -$rating->update_rating($userrating); +if ($userrating != RATING_UNSET_RATING) { + $rating = new rating($ratingoptions); + $rating->update_rating($userrating); +} else { //delete the rating if the user set to Rate... + $options = new stdClass(); + $options->contextid = $context->id; + $options->userid = $USER->id; + $options->itemid = $itemid; + + $rm->delete_ratings($options); +} //Future possible enhancement: add a setting to turn grade updating off for those who don't want them in gradebook //note that this would need to be done in both rate.php and rate_ajax.php @@ -115,17 +123,15 @@ if(true){ $result = new stdClass; $result->success = true; - //need to retrieve the updated item to get its new aggregate value $item = new stdclass(); -$item->id = $rating->itemid; +$item->id = $itemid; $items = array($item); -//most of $ratingoptions variables are set correctly +//most of $ratingoptions variables were previously set $ratingoptions->items = $items; $ratingoptions->aggregate = $aggregationmethod; -$rm = new rating_manager(); $items = $rm->get_ratings($ratingoptions); //for custom scales return text not the value @@ -134,10 +140,10 @@ $scalearray = null; $aggregatetoreturn = round($items[0]->rating->aggregate,1); // Output a dash if aggregation method == COUNT as the count is output next to the aggregate anyway -if ($items[0]->rating->settings->aggregationmethod==RATING_AGGREGATE_COUNT) { +if ($items[0]->rating->settings->aggregationmethod==RATING_AGGREGATE_COUNT or $items[0]->rating->count == 0) { $aggregatetoreturn = ' - '; } else if($rating->scaleid < 0) { //if its non-numeric scale - //output the numeric aggregate is aggregation method is sum + //dont use the scale item if the aggregation method is sum as adding items from a custom scale makes no sense if ($items[0]->rating->settings->aggregationmethod!= RATING_AGGREGATE_SUM) { $scalerecord = $DB->get_record('scale', array('id' => -$rating->scaleid)); if ($scalerecord) { @@ -154,7 +160,7 @@ if (($USER->id==$items[0]->rating->itemuserid && has_capability('moodle/rating:v || ($USER->id!=$items[0]->rating->itemuserid && has_capability('moodle/rating:viewany',$context) && $pluginpermissionsarray['viewany'])) { $result->aggregate = $aggregatetoreturn; $result->count = $items[0]->rating->count; - $result->itemid = $rating->itemid; + $result->itemid = $itemid; } echo json_encode($result); \ No newline at end of file