Skip to content

Commit

Permalink
rating MDL-23814 made an assortment of fixes to the ratings code
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Davis committed Aug 17, 2010
1 parent c385370 commit 279fcfc
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 28 deletions.
9 changes: 9 additions & 0 deletions rating/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -179,10 +180,18 @@ public function delete_ratings($options) {
//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) );
Expand Down
9 changes: 7 additions & 2 deletions rating/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
31 changes: 19 additions & 12 deletions rating/rate.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,25 @@
die();
}

$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);

$rating->update_rating($userrating);
$PAGE->set_url('/lib/rate.php', array('contextid'=>$context->id));

if ($userrating != RATING_UNSET_RATING) {
$ratingoptions = new stdclass;
$ratingoptions->context = $context;
$ratingoptions->itemid = $itemid;
$ratingoptions->scaleid = $scaleid;
$ratingoptions->userid = $USER->id;

$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
Expand Down
34 changes: 20 additions & 14 deletions rating/rate_ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,15 @@
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;
$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'];

Expand All @@ -81,19 +82,26 @@
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
Expand All @@ -115,17 +123,15 @@
$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
Expand All @@ -134,10 +140,10 @@
$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) {
Expand All @@ -154,7 +160,7 @@
|| ($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);

0 comments on commit 279fcfc

Please sign in to comment.