mirror of
https://github.com/moodle/moodle.git
synced 2025-04-13 20:42:22 +02:00
rating MDL-23814 made an assortment of fixes to the ratings code
This commit is contained in:
parent
c3853702a5
commit
279fcfcfa4
@ -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) );
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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);
|
Loading…
x
Reference in New Issue
Block a user