MDL-46446 core_rating: fixed coding style problems
This commit is contained in:
+24
-17
@@ -1,5 +1,4 @@
|
||||
<?php
|
||||
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
@@ -33,12 +32,15 @@ $ratingarea = optional_param('ratingarea', null, PARAM_AREA);
|
||||
$itemid = required_param('itemid', PARAM_INT);
|
||||
$scaleid = required_param('scaleid', PARAM_INT);
|
||||
$sort = optional_param('sort', '', PARAM_ALPHA);
|
||||
$popup = optional_param('popup', 0, PARAM_INT); //==1 if in a popup window?
|
||||
$popup = optional_param('popup', 0, PARAM_INT); // Any non-zero value if in a popup window.
|
||||
|
||||
list($context, $course, $cm) = get_context_info_array($contextid);
|
||||
require_login($course, false, $cm);
|
||||
|
||||
$url = new moodle_url('/rating/index.php', array('contextid'=>$contextid,'component'=>$component,'itemid'=>$itemid,'scaleid'=>$scaleid));
|
||||
$url = new moodle_url('/rating/index.php', array('contextid' => $contextid,
|
||||
'component' => $component,
|
||||
'itemid' => $itemid,
|
||||
'scaleid' => $scaleid));
|
||||
if (!empty($ratingarea)) {
|
||||
$url->param('ratingarea', $ratingarea);
|
||||
}
|
||||
@@ -55,17 +57,22 @@ if ($popup) {
|
||||
$PAGE->set_pagelayout('popup');
|
||||
}
|
||||
|
||||
if (!has_capability('moodle/rating:view',$context)) {
|
||||
if (!has_capability('moodle/rating:view', $context)) {
|
||||
print_error('noviewrate', 'rating');
|
||||
}
|
||||
if (!has_capability('moodle/rating:viewall',$context) and $USER->id != $item->userid) {
|
||||
if (!has_capability('moodle/rating:viewall', $context) and $USER->id != $item->userid) {
|
||||
print_error('noviewanyrate', 'rating');
|
||||
}
|
||||
|
||||
switch ($sort) {
|
||||
case 'firstname': $sqlsort = "u.firstname ASC"; break;
|
||||
case 'rating': $sqlsort = "r.rating ASC"; break;
|
||||
default: $sqlsort = "r.timemodified ASC";
|
||||
case 'firstname':
|
||||
$sqlsort = "u.firstname ASC";
|
||||
break;
|
||||
case 'rating':
|
||||
$sqlsort = "r.rating ASC";
|
||||
break;
|
||||
default:
|
||||
$sqlsort = "r.timemodified ASC";
|
||||
}
|
||||
|
||||
$scalemenu = make_grades_menu($scaleid);
|
||||
@@ -74,7 +81,7 @@ $strrating = get_string('rating', 'rating');
|
||||
$strname = get_string('name');
|
||||
$strtime = get_string('time');
|
||||
|
||||
$PAGE->set_title(get_string('allratingsforitem','rating'));
|
||||
$PAGE->set_title(get_string('allratingsforitem', 'rating'));
|
||||
echo $OUTPUT->header();
|
||||
|
||||
$ratingoptions = new stdClass;
|
||||
@@ -87,10 +94,10 @@ $ratingoptions->sort = $sqlsort;
|
||||
$rm = new rating_manager();
|
||||
$ratings = $rm->get_all_ratings_for_item($ratingoptions);
|
||||
if (!$ratings) {
|
||||
$msg = get_string('noratings','rating');
|
||||
echo html_writer::tag('div', $msg, array('class'=>'mdl-align'));
|
||||
$msg = get_string('noratings', 'rating');
|
||||
echo html_writer::tag('div', $msg, array('class' => 'mdl-align'));
|
||||
} else {
|
||||
// To get the sort URL, copy the current URL and remove any previous sort
|
||||
// To get the sort URL, copy the current URL and remove any previous sort.
|
||||
$sorturl = new moodle_url($url);
|
||||
$sorturl->remove_params('sort');
|
||||
|
||||
@@ -107,14 +114,14 @@ if (!$ratings) {
|
||||
$table->colclasses = array('', 'firstname', 'rating', 'time');
|
||||
$table->data = array();
|
||||
|
||||
// If the scale was changed after ratings were submitted some ratings may have a value above the current maximum
|
||||
// We can't just do count($scalemenu) - 1 as custom scales start at index 1, not 0
|
||||
// If the scale was changed after ratings were submitted some ratings may have a value above the current maximum.
|
||||
// We can't just do count($scalemenu) - 1 as custom scales start at index 1, not 0.
|
||||
$maxrating = max(array_keys($scalemenu));
|
||||
|
||||
foreach ($ratings as $rating) {
|
||||
//Undo the aliasing of the user id column from user_picture::fields()
|
||||
//we could clone the rating object or preserve the rating id if we needed it again
|
||||
//but we don't
|
||||
// Undo the aliasing of the user id column from user_picture::fields().
|
||||
// We could clone the rating object or preserve the rating id if we needed it again
|
||||
// but we don't.
|
||||
$rating->id = $rating->userid;
|
||||
|
||||
$row = new html_table_row();
|
||||
|
||||
+139
-125
@@ -25,7 +25,7 @@
|
||||
|
||||
define('RATING_UNSET_RATING', -999);
|
||||
|
||||
define ('RATING_AGGREGATE_NONE', 0); //no ratings
|
||||
define ('RATING_AGGREGATE_NONE', 0); // No ratings.
|
||||
define ('RATING_AGGREGATE_AVERAGE', 1);
|
||||
define ('RATING_AGGREGATE_COUNT', 2);
|
||||
define ('RATING_AGGREGATE_MAXIMUM', 3);
|
||||
@@ -129,12 +129,12 @@ class rating implements renderable {
|
||||
* }
|
||||
*/
|
||||
public function __construct($options) {
|
||||
$this->context = $options->context;
|
||||
$this->component = $options->component;
|
||||
$this->context = $options->context;
|
||||
$this->component = $options->component;
|
||||
$this->ratingarea = $options->ratingarea;
|
||||
$this->itemid = $options->itemid;
|
||||
$this->scaleid = $options->scaleid;
|
||||
$this->userid = $options->userid;
|
||||
$this->itemid = $options->itemid;
|
||||
$this->scaleid = $options->scaleid;
|
||||
$this->userid = $options->userid;
|
||||
|
||||
if (isset($options->settings)) {
|
||||
$this->settings = $options->settings;
|
||||
@@ -176,7 +176,7 @@ class rating implements renderable {
|
||||
$ratingoptions->component = $this->component;
|
||||
$ratingoptions->ratingarea = $this->ratingarea;
|
||||
$ratingoptions->items = $items;
|
||||
$ratingoptions->aggregate = RATING_AGGREGATE_AVERAGE;//we dont actually care what aggregation method is applied
|
||||
$ratingoptions->aggregate = RATING_AGGREGATE_AVERAGE; // We dont actually care what aggregation method is applied.
|
||||
$ratingoptions->scaleid = $this->scaleid;
|
||||
$ratingoptions->userid = $this->userid;
|
||||
|
||||
@@ -185,7 +185,7 @@ class rating implements renderable {
|
||||
$firstitem = $items[0]->rating;
|
||||
|
||||
if (empty($firstitem->id)) {
|
||||
// Insert a new rating
|
||||
// Insert a new rating.
|
||||
$data->contextid = $this->context->id;
|
||||
$data->component = $this->component;
|
||||
$data->ratingarea = $this->ratingarea;
|
||||
@@ -197,7 +197,7 @@ class rating implements renderable {
|
||||
$data->timemodified = $time;
|
||||
$DB->insert_record('rating', $data);
|
||||
} else {
|
||||
// Update the rating
|
||||
// Update the rating.
|
||||
$data->id = $firstitem->id;
|
||||
$DB->update_record('rating', $data);
|
||||
}
|
||||
@@ -222,12 +222,14 @@ class rating implements renderable {
|
||||
$aggregate = $this->aggregate;
|
||||
$method = $this->settings->aggregationmethod;
|
||||
|
||||
// only display aggregate if aggregation method isn't COUNT
|
||||
// Only display aggregate if aggregation method isn't COUNT.
|
||||
$aggregatestr = '';
|
||||
if ($aggregate && $method != RATING_AGGREGATE_COUNT) {
|
||||
if ($method != RATING_AGGREGATE_SUM && !$this->settings->scale->isnumeric) {
|
||||
$aggregatestr .= $this->settings->scale->scaleitems[round($aggregate)]; //round aggregate as we're using it as an index
|
||||
} else { // aggregation is SUM or the scale is numeric
|
||||
|
||||
// Round aggregate as we're using it as an index.
|
||||
$aggregatestr .= $this->settings->scale->scaleitems[round($aggregate)];
|
||||
} else { // Aggregation is SUM or the scale is numeric.
|
||||
$aggregatestr .= round($aggregate, 1);
|
||||
}
|
||||
}
|
||||
@@ -246,20 +248,20 @@ class rating implements renderable {
|
||||
global $USER;
|
||||
$userid = $USER->id;
|
||||
}
|
||||
// You can't rate your item
|
||||
// You can't rate your item.
|
||||
if ($this->itemuserid == $userid) {
|
||||
return false;
|
||||
}
|
||||
// You can't rate if you don't have the system cap
|
||||
// You can't rate if you don't have the system cap.
|
||||
if (!$this->settings->permissions->rate) {
|
||||
return false;
|
||||
}
|
||||
// You can't rate if you don't have the plugin cap
|
||||
// You can't rate if you don't have the plugin cap.
|
||||
if (!$this->settings->pluginpermissions->rate) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// You can't rate if the item was outside of the assessment times
|
||||
// You can't rate if the item was outside of the assessment times.
|
||||
$timestart = $this->settings->assesstimestart;
|
||||
$timefinish = $this->settings->assesstimefinish;
|
||||
$timecreated = $this->itemtimecreated;
|
||||
@@ -281,14 +283,20 @@ class rating implements renderable {
|
||||
$userid = $USER->id;
|
||||
}
|
||||
|
||||
// if the item doesnt belong to anyone or its another user's items and they can see the aggregate on items they don't own
|
||||
// Note that viewany doesnt mean you can see the aggregate or ratings of your own items
|
||||
if ((empty($this->itemuserid) or $this->itemuserid != $userid) && $this->settings->permissions->viewany && $this->settings->pluginpermissions->viewany ) {
|
||||
// If the item doesnt belong to anyone or its another user's items and they can see the aggregate on items they don't own.
|
||||
// Note that viewany doesnt mean you can see the aggregate or ratings of your own items.
|
||||
if ((empty($this->itemuserid) or $this->itemuserid != $userid)
|
||||
&& $this->settings->permissions->viewany
|
||||
&& $this->settings->pluginpermissions->viewany ) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// if its the current user's item and they have permission to view the aggregate on their own items
|
||||
if ($this->itemuserid == $userid && $this->settings->permissions->view && $this->settings->pluginpermissions->view) {
|
||||
// If its the current user's item and they have permission to view the aggregate on their own items.
|
||||
if ($this->itemuserid == $userid
|
||||
&& $this->settings->permissions->view
|
||||
&& $this->settings->pluginpermissions->view) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -352,14 +360,7 @@ class rating implements renderable {
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove this rating from the database
|
||||
* @return void
|
||||
*/
|
||||
//public function delete_rating() {
|
||||
//todo implement this if its actually needed
|
||||
//}
|
||||
} //end rating class definition
|
||||
} // End rating class definition.
|
||||
|
||||
/**
|
||||
* The rating_manager class provides the ability to retrieve sets of ratings from the database
|
||||
@@ -414,7 +415,9 @@ class rating_manager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of ratings for a given item (forum post, glossary entry etc). This returns all users ratings for a single item
|
||||
* Returns an array of ratings for a given item (forum post, glossary entry etc).
|
||||
*
|
||||
* This returns all users ratings for a single item
|
||||
*
|
||||
* @param stdClass $options {
|
||||
* context => context the context in which the ratings exists [required]
|
||||
@@ -442,7 +445,7 @@ class rating_manager {
|
||||
}
|
||||
|
||||
$sortclause = '';
|
||||
if( !empty($options->sort) ) {
|
||||
if (!empty($options->sort)) {
|
||||
$sortclause = "ORDER BY $options->sort";
|
||||
}
|
||||
|
||||
@@ -469,16 +472,16 @@ class rating_manager {
|
||||
* Adds rating objects to an array of items (forum posts, glossary entries etc). Rating objects are available at $item->rating
|
||||
*
|
||||
* @param stdClass $options {
|
||||
* context => context the context in which the ratings exists [required]
|
||||
* component => the component name ie mod_forum [required]
|
||||
* ratingarea => the ratingarea we are interested in [required]
|
||||
* items => array an array of items such as forum posts or glossary items. They must have an 'id' member ie $items[0]->id[required]
|
||||
* aggregate => int what aggregation method should be applied. RATING_AGGREGATE_AVERAGE, RATING_AGGREGATE_MAXIMUM etc [required]
|
||||
* scaleid => int the scale from which the user can select a rating [required]
|
||||
* userid => int the id of the current user [optional]
|
||||
* returnurl => string the url to return the user to after submitting a rating. Can be left null for ajax requests [optional]
|
||||
* assesstimestart => int only allow rating of items created after this timestamp [optional]
|
||||
* assesstimefinish => int only allow rating of items created before this timestamp [optional]
|
||||
* context => context the context in which the ratings exists [required]
|
||||
* component => the component name ie mod_forum [required]
|
||||
* ratingarea => the ratingarea we are interested in [required]
|
||||
* items => array items like forum posts or glossary items. Each item needs an 'id' ie $items[0]->id [required]
|
||||
* aggregate => int aggregation method to apply. RATING_AGGREGATE_AVERAGE, RATING_AGGREGATE_MAXIMUM etc [required]
|
||||
* scaleid => int the scale from which the user can select a rating [required]
|
||||
* userid => int the id of the current user [optional]
|
||||
* returnurl => string the url to return the user to after submitting a rating. Null for ajax requests [optional]
|
||||
* assesstimestart => int only allow rating of items created after this timestamp [optional]
|
||||
* assesstimefinish => int only allow rating of items created before this timestamp [optional]
|
||||
* @return array the array of items with their ratings attached at $items[0]->rating
|
||||
*/
|
||||
public function get_ratings($options) {
|
||||
@@ -509,12 +512,12 @@ class rating_manager {
|
||||
if (!isset($options->aggregate)) {
|
||||
throw new coding_exception('The aggregate option is a required option when getting ratings.');
|
||||
} else if ($options->aggregate == RATING_AGGREGATE_NONE) {
|
||||
// Ratings arn't enabled.
|
||||
// Ratings are not enabled.
|
||||
return $options->items;
|
||||
}
|
||||
$aggregatestr = $this->get_aggregation_method($options->aggregate);
|
||||
|
||||
// Default the userid to the current user if it is not set
|
||||
// Default the userid to the current user if it is not set.
|
||||
if (empty($options->userid)) {
|
||||
$userid = $USER->id;
|
||||
} else {
|
||||
@@ -525,15 +528,20 @@ class rating_manager {
|
||||
// from the related component.
|
||||
list($type, $name) = core_component::normalize_component($options->component);
|
||||
$default = array(null, 'id', 'userid');
|
||||
list($itemtablename, $itemidcol, $itemuseridcol) = plugin_callback($type, $name, 'rating', 'get_item_fields', array($options), $default);
|
||||
list($itemtablename, $itemidcol, $itemuseridcol) = plugin_callback($type,
|
||||
$name,
|
||||
'rating',
|
||||
'get_item_fields',
|
||||
array($options),
|
||||
$default);
|
||||
|
||||
// Create an array of item ids
|
||||
// Create an array of item IDs.
|
||||
$itemids = array();
|
||||
foreach ($options->items as $item) {
|
||||
$itemids[] = $item->{$itemidcol};
|
||||
}
|
||||
|
||||
// get the items from the database
|
||||
// Get the items from the database.
|
||||
list($itemidtest, $params) = $DB->get_in_or_equal($itemids, SQL_PARAMS_NAMED);
|
||||
$params['contextid'] = $options->context->id;
|
||||
$params['userid'] = $userid;
|
||||
@@ -567,11 +575,11 @@ class rating_manager {
|
||||
$ratingoptions->settings = $this->generate_rating_settings_object($options);
|
||||
foreach ($options->items as $item) {
|
||||
$founduserrating = false;
|
||||
foreach($userratings as $userrating) {
|
||||
//look for an existing rating from this user of this item
|
||||
foreach ($userratings as $userrating) {
|
||||
// Look for an existing rating from this user of this item.
|
||||
if ($item->{$itemidcol} == $userrating->itemid) {
|
||||
// Note: rec->scaleid = the id of scale at the time the rating was submitted
|
||||
// may be different from the current scale id
|
||||
// Note: rec->scaleid = the id of scale at the time the rating was submitted.
|
||||
// It may be different from the current scale id.
|
||||
$ratingoptions->scaleid = $userrating->scaleid;
|
||||
$ratingoptions->userid = $userrating->userid;
|
||||
$ratingoptions->id = $userrating->id;
|
||||
@@ -585,7 +593,7 @@ class rating_manager {
|
||||
$ratingoptions->scaleid = null;
|
||||
$ratingoptions->userid = null;
|
||||
$ratingoptions->id = null;
|
||||
$ratingoptions->rating = null;
|
||||
$ratingoptions->rating = null;
|
||||
}
|
||||
|
||||
if (array_key_exists($item->{$itemidcol}, $aggregateratings)) {
|
||||
@@ -617,9 +625,9 @@ class rating_manager {
|
||||
* context => context the context in which the ratings exists [required]
|
||||
* component => string The component the items belong to [required]
|
||||
* ratingarea => string The ratingarea the items belong to [required]
|
||||
* aggregate => int what aggregation method should be applied. RATING_AGGREGATE_AVERAGE, RATING_AGGREGATE_MAXIMUM etc [required]
|
||||
* aggregate => int Aggregation method to apply. RATING_AGGREGATE_AVERAGE, RATING_AGGREGATE_MAXIMUM etc [required]
|
||||
* scaleid => int the scale from which the user can select a rating [required]
|
||||
* returnurl => string the url to return the user to after submitting a rating. Can be left null for ajax requests [optional]
|
||||
* returnurl => string the url to return the user to after submitting a rating. Null for ajax requests [optional]
|
||||
* assesstimestart => int only allow rating of items created after this timestamp [optional]
|
||||
* assesstimefinish => int only allow rating of items created before this timestamp [optional]
|
||||
* plugintype => string plugin type ie 'mod' Used to find the permissions callback [optional]
|
||||
@@ -645,14 +653,14 @@ class rating_manager {
|
||||
throw new coding_exception('The scaleid option is now a required option when generating a rating settings object.');
|
||||
}
|
||||
|
||||
// settings that are common to all ratings objects in this context
|
||||
// Settings that are common to all ratings objects in this context.
|
||||
$settings = new stdClass;
|
||||
$settings->scale = $this->generate_rating_scale_object($options->scaleid); // the scale to use now
|
||||
$settings->scale = $this->generate_rating_scale_object($options->scaleid); // The scale to use now.
|
||||
$settings->aggregationmethod = $options->aggregate;
|
||||
$settings->assesstimestart = null;
|
||||
$settings->assesstimefinish = null;
|
||||
|
||||
// Collect options into the settings object
|
||||
// Collect options into the settings object.
|
||||
if (!empty($options->assesstimestart)) {
|
||||
$settings->assesstimestart = $options->assesstimestart;
|
||||
}
|
||||
@@ -663,15 +671,22 @@ class rating_manager {
|
||||
$settings->returnurl = $options->returnurl;
|
||||
}
|
||||
|
||||
// check site capabilities
|
||||
// Check site capabilities.
|
||||
$settings->permissions = new stdClass;
|
||||
$settings->permissions->view = has_capability('moodle/rating:view', $options->context); // can view the aggregate of ratings of their own items
|
||||
$settings->permissions->viewany = has_capability('moodle/rating:viewany', $options->context); // can view the aggregate of ratings of other people's items
|
||||
$settings->permissions->viewall = has_capability('moodle/rating:viewall', $options->context); // can view individual ratings
|
||||
$settings->permissions->rate = has_capability('moodle/rating:rate', $options->context); // can submit ratings
|
||||
// Can view the aggregate of ratings of their own items.
|
||||
$settings->permissions->view = has_capability('moodle/rating:view', $options->context);
|
||||
// Can view the aggregate of ratings of other people's items.
|
||||
$settings->permissions->viewany = has_capability('moodle/rating:viewany', $options->context);
|
||||
// Can view individual ratings.
|
||||
$settings->permissions->viewall = has_capability('moodle/rating:viewall', $options->context);
|
||||
// Can submit ratings.
|
||||
$settings->permissions->rate = has_capability('moodle/rating:rate', $options->context);
|
||||
|
||||
// check module capabilities (mostly for backwards compatability with old modules that previously implemented their own ratings)
|
||||
$pluginpermissionsarray = $this->get_plugin_permissions_array($options->context->id, $options->component, $options->ratingarea);
|
||||
// Check module capabilities
|
||||
// This is mostly for backwards compatability with old modules that previously implemented their own ratings.
|
||||
$pluginpermissionsarray = $this->get_plugin_permissions_array($options->context->id,
|
||||
$options->component,
|
||||
$options->ratingarea);
|
||||
$settings->pluginpermissions = new stdClass;
|
||||
$settings->pluginpermissions->view = $pluginpermissionsarray['view'];
|
||||
$settings->pluginpermissions->viewany = $pluginpermissionsarray['viewany'];
|
||||
@@ -700,25 +715,25 @@ class rating_manager {
|
||||
$scale->max = $scaleid;
|
||||
|
||||
if ($scaleid < 0) {
|
||||
// It is a proper scale (not numeric)
|
||||
// It is a proper scale (not numeric).
|
||||
$scalerecord = $DB->get_record('scale', array('id' => abs($scaleid)));
|
||||
if ($scalerecord) {
|
||||
// We need to generate an array with string keys starting at 1
|
||||
// We need to generate an array with string keys starting at 1.
|
||||
$scalearray = explode(',', $scalerecord->scale);
|
||||
$c = count($scalearray);
|
||||
for ($i = 0; $i < $c; $i++) {
|
||||
// treat index as a string to allow sorting without changing the value
|
||||
// Treat index as a string to allow sorting without changing the value.
|
||||
$scale->scaleitems[(string)($i + 1)] = $scalearray[$i];
|
||||
}
|
||||
krsort($scale->scaleitems); // have the highest grade scale item appear first
|
||||
krsort($scale->scaleitems); // Have the highest grade scale item appear first.
|
||||
$scale->isnumeric = false;
|
||||
$scale->name = $scalerecord->name;
|
||||
$scale->courseid = $scalerecord->courseid;
|
||||
$scale->max = count($scale->scaleitems);
|
||||
}
|
||||
} else {
|
||||
//generate an array of values for numeric scales
|
||||
for($i = 0; $i <= (int)$scaleid; $i++) {
|
||||
// Generate an array of values for numeric scales.
|
||||
for ($i = 0; $i <= (int)$scaleid; $i++) {
|
||||
$scale->scaleitems[(string)$i] = $i;
|
||||
}
|
||||
}
|
||||
@@ -738,13 +753,11 @@ class rating_manager {
|
||||
* @return int|null return null if the created time is unavailable, otherwise return a timestamp
|
||||
*/
|
||||
protected function get_item_time_created($item) {
|
||||
if( !empty($item->created) ) {
|
||||
return $item->created;//the forum_posts table has created instead of timecreated
|
||||
}
|
||||
else if(!empty($item->timecreated)) {
|
||||
if (!empty($item->created)) {
|
||||
return $item->created; // The forum_posts table has created instead of timecreated.
|
||||
} else if (!empty($item->timecreated)) {
|
||||
return $item->timecreated;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -753,16 +766,16 @@ class rating_manager {
|
||||
* Returns an array of grades calculated by aggregating item ratings.
|
||||
*
|
||||
* @param stdClass $options {
|
||||
* userid => int the id of the user whose items have been rated. NOT the user who submitted the ratings. 0 to update all. [required]
|
||||
* aggregationmethod => int the aggregation method to apply when calculating grades ie RATING_AGGREGATE_AVERAGE [required]
|
||||
* scaleid => int the scale from which the user can select a rating. Used for bounds checking. [required]
|
||||
* itemtable => int the table containing the items [required]
|
||||
* itemtableusercolum => int the column of the user table containing the item owner's user id [required]
|
||||
* component => The component for the ratings [required]
|
||||
* ratingarea => The ratingarea for the ratings [required]
|
||||
* contextid => int the context in which the rated items exist [optional]
|
||||
* modulename => string the name of the module [optional]
|
||||
* moduleid => int the id of the module instance [optional]
|
||||
* userid => int the id of the user whose items were rated, NOT the user who submitted ratings. 0 to update all. [required]
|
||||
* aggregationmethod => int the aggregation method to apply when calculating grades ie RATING_AGGREGATE_AVERAGE [required]
|
||||
* scaleid => int the scale from which the user can select a rating. Used for bounds checking. [required]
|
||||
* itemtable => int the table containing the items [required]
|
||||
* itemtableusercolum => int the column of the user table containing the item owner's user id [required]
|
||||
* component => The component for the ratings [required]
|
||||
* ratingarea => The ratingarea for the ratings [required]
|
||||
* contextid => int the context in which the rated items exist [optional]
|
||||
* modulename => string the name of the module [optional]
|
||||
* moduleid => int the id of the module instance [optional]
|
||||
* }
|
||||
* @return array the array of the user's grades
|
||||
*/
|
||||
@@ -778,16 +791,10 @@ class rating_manager {
|
||||
throw new coding_exception('The ratingarea option is now a required option when getting user grades from ratings.');
|
||||
}
|
||||
|
||||
//if the calling code doesn't supply a context id we'll have to figure it out
|
||||
if( !empty($options->contextid) ) {
|
||||
// If the calling code doesn't supply a context id we'll have to figure it out.
|
||||
if (!empty($options->contextid)) {
|
||||
$contextid = $options->contextid;
|
||||
}
|
||||
else if( !empty($options->cmid) ) {
|
||||
//not implemented as not currently used although cmid is potentially available (the forum supplies it)
|
||||
//Is there a convenient way to get a context id from a cm id?
|
||||
//$cmidnumber = $options->cmidnumber;
|
||||
}
|
||||
else if ( !empty($options->modulename) && !empty($options->moduleid) ) {
|
||||
} else if (!empty($options->modulename) && !empty($options->moduleid)) {
|
||||
$modulename = $options->modulename;
|
||||
$moduleid = intval($options->moduleid);
|
||||
|
||||
@@ -814,16 +821,16 @@ class rating_manager {
|
||||
$scaleid = $options->scaleid;
|
||||
$aggregationstring = $this->get_aggregation_method($options->aggregationmethod);
|
||||
|
||||
//if userid is not 0 we only want the grade for a single user
|
||||
// If userid is not 0 we only want the grade for a single user.
|
||||
$singleuserwhere = '';
|
||||
if ($options->userid != 0) {
|
||||
$params['userid1'] = intval($options->userid);
|
||||
$singleuserwhere = "AND i.{$itemtableusercolumn} = :userid1";
|
||||
}
|
||||
|
||||
//MDL-24648 The where line used to be "WHERE (r.contextid is null or r.contextid=:contextid)"
|
||||
//r.contextid will be null for users who haven't been rated yet
|
||||
//no longer including users who haven't been rated to reduce memory requirements
|
||||
// MDL-24648 The where line used to be "WHERE (r.contextid is null or r.contextid=:contextid)".
|
||||
// r.contextid will be null for users who haven't been rated yet.
|
||||
// No longer including users who haven't been rated to reduce memory requirements.
|
||||
$sql = "SELECT u.id as id, u.id AS userid, $aggregationstring(r.rating) AS rawgrade
|
||||
FROM {user} u
|
||||
LEFT JOIN {{$itemtable}} i ON u.id=i.{$itemtableusercolumn}
|
||||
@@ -840,10 +847,10 @@ class rating_manager {
|
||||
$scale = null;
|
||||
$max = 0;
|
||||
if ($options->scaleid >= 0) {
|
||||
//numeric
|
||||
// Numeric.
|
||||
$max = $options->scaleid;
|
||||
} else {
|
||||
//custom scales
|
||||
// Custom scales.
|
||||
$scale = $DB->get_record('scale', array('id' => -$options->scaleid));
|
||||
if ($scale) {
|
||||
$scale = explode(',', $scale->scale);
|
||||
@@ -853,16 +860,17 @@ class rating_manager {
|
||||
}
|
||||
}
|
||||
|
||||
// it could throw off the grading if count and sum returned a rawgrade higher than scale
|
||||
// so to prevent it we review the results and ensure that rawgrade does not exceed the scale, if it does we set rawgrade = scale (i.e. full credit)
|
||||
foreach ($results as $rid=>$result) {
|
||||
// It could throw off the grading if count and sum returned a rawgrade higher than scale
|
||||
// so to prevent it we review the results and ensure that rawgrade does not exceed the scale.
|
||||
// If it does we set rawgrade = scale (i.e. full credit).
|
||||
foreach ($results as $rid => $result) {
|
||||
if ($options->scaleid >= 0) {
|
||||
//numeric
|
||||
// Numeric.
|
||||
if ($result->rawgrade > $options->scaleid) {
|
||||
$results[$rid]->rawgrade = $options->scaleid;
|
||||
}
|
||||
} else {
|
||||
//scales
|
||||
// Scales.
|
||||
if (!empty($scale) && $result->rawgrade > $max) {
|
||||
$results[$rid]->rawgrade = $max;
|
||||
}
|
||||
@@ -912,8 +920,8 @@ class rating_manager {
|
||||
$aggregatestr = 'SUM';
|
||||
break;
|
||||
default:
|
||||
$aggregatestr = 'AVG'; // Default to this to avoid real breakage - MDL-22270
|
||||
debugging('Incorrect call to get_aggregation_method(), was called with incorrect aggregate method ' . $aggregate, DEBUG_DEVELOPER);
|
||||
$aggregatestr = 'AVG'; // Default to this to avoid real breakage - MDL-22270.
|
||||
debugging('Incorrect call to get_aggregation_method(), incorrect aggregate method ' . $aggregate, DEBUG_DEVELOPER);
|
||||
}
|
||||
return $aggregatestr;
|
||||
}
|
||||
@@ -928,10 +936,16 @@ class rating_manager {
|
||||
*/
|
||||
public function get_plugin_permissions_array($contextid, $component, $ratingarea) {
|
||||
$pluginpermissionsarray = null;
|
||||
$defaultpluginpermissions = array('rate'=>false,'view'=>false,'viewany'=>false,'viewall'=>false);//deny by default
|
||||
// Deny by default.
|
||||
$defaultpluginpermissions = array('rate' => false, 'view' => false, 'viewany' => false, 'viewall' => false);
|
||||
if (!empty($component)) {
|
||||
list($type, $name) = core_component::normalize_component($component);
|
||||
$pluginpermissionsarray = plugin_callback($type, $name, 'rating', 'permissions', array($contextid, $component, $ratingarea), $defaultpluginpermissions);
|
||||
$pluginpermissionsarray = plugin_callback($type,
|
||||
$name,
|
||||
'rating',
|
||||
'permissions',
|
||||
array($contextid, $component, $ratingarea),
|
||||
$defaultpluginpermissions);
|
||||
} else {
|
||||
$pluginpermissionsarray = $defaultpluginpermissions;
|
||||
}
|
||||
@@ -942,15 +956,15 @@ class rating_manager {
|
||||
* Validates a submitted rating
|
||||
*
|
||||
* @param array $params submitted data
|
||||
* context => object the context in which the rated items exists [required]
|
||||
* component => The component the rating belongs to [required]
|
||||
* ratingarea => The ratingarea the rating is associated with [required]
|
||||
* itemid => int the ID of the object being rated [required]
|
||||
* scaleid => int the scale from which the user can select a rating. Used for bounds checking. [required]
|
||||
* rating => int the submitted rating
|
||||
* rateduserid => int the id of the user whose items have been rated. NOT the user who submitted the ratings. 0 to update all. [required]
|
||||
* aggregation => int the aggregation method to apply when calculating grades ie RATING_AGGREGATE_AVERAGE [optional]
|
||||
* @return boolean true if the rating is valid. False if callback wasnt found and will throw rating_exception if rating is invalid
|
||||
* context => object the context in which the rated items exists [required]
|
||||
* component => The component the rating belongs to [required]
|
||||
* ratingarea => The ratingarea the rating is associated with [required]
|
||||
* itemid => int the ID of the object being rated [required]
|
||||
* scaleid => int the scale from which the user can select a rating. Used for bounds checking. [required]
|
||||
* rating => int the submitted rating
|
||||
* rateduserid => int the id of the user whose items have been rated. 0 to update all. [required]
|
||||
* aggregation => int the aggregation method to apply when calculating grades ie RATING_AGGREGATE_AVERAGE [optional]
|
||||
* @return boolean true if the rating is valid, false if callback not found, throws rating_exception if rating is invalid
|
||||
*/
|
||||
public function check_rating_is_valid($params) {
|
||||
|
||||
@@ -975,11 +989,11 @@ class rating_manager {
|
||||
|
||||
list($plugintype, $pluginname) = core_component::normalize_component($params['component']);
|
||||
|
||||
//this looks for a function like forum_rating_validate() in mod_forum lib.php
|
||||
//wrapping the params array in another array as call_user_func_array() expands arrays into multiple arguments
|
||||
// This looks for a function like forum_rating_validate() in mod_forum lib.php
|
||||
// wrapping the params array in another array as call_user_func_array() expands arrays into multiple arguments.
|
||||
$isvalid = plugin_callback($plugintype, $pluginname, 'rating', 'validate', array($params), null);
|
||||
|
||||
//if null then the callback doesn't exist
|
||||
// If null then the callback does not exist.
|
||||
if ($isvalid === null) {
|
||||
$isvalid = false;
|
||||
debugging('rating validation callback not found for component '. clean_param($component, PARAM_ALPHANUMEXT));
|
||||
@@ -996,7 +1010,7 @@ class rating_manager {
|
||||
public function initialise_rating_javascript(moodle_page $page) {
|
||||
global $CFG;
|
||||
|
||||
//only needs to be initialized once
|
||||
// Only needs to be initialized once.
|
||||
static $done = false;
|
||||
if ($done) {
|
||||
return true;
|
||||
@@ -1037,10 +1051,10 @@ class rating_manager {
|
||||
return $aggregatelabel;
|
||||
}
|
||||
|
||||
}//end rating_manager class definition
|
||||
} // End rating_manager class definition.
|
||||
|
||||
/**
|
||||
* The rating_exception class provides the ability to generate exceptions that can be easily identified as coming from the ratings system
|
||||
* The rating_exception class for exceptions specific to the ratings system
|
||||
*
|
||||
* @package core_rating
|
||||
* @category rating
|
||||
@@ -1058,7 +1072,7 @@ class rating_exception extends moodle_exception {
|
||||
*
|
||||
* @param string $errorcode the error code to generate
|
||||
*/
|
||||
function __construct($errorcode) {
|
||||
public function __construct($errorcode) {
|
||||
$this->errorcode = $errorcode;
|
||||
$this->message = get_string($errorcode, 'error');
|
||||
}
|
||||
|
||||
+12
-13
@@ -1,7 +1,7 @@
|
||||
M.core_rating={
|
||||
M.core_rating = {
|
||||
|
||||
Y : null,
|
||||
api: M.cfg.wwwroot+'/rating/rate_ajax.php',
|
||||
api: M.cfg.wwwroot + '/rating/rate_ajax.php',
|
||||
|
||||
init : function(Y){
|
||||
this.Y = Y;
|
||||
@@ -20,9 +20,8 @@ M.core_rating={
|
||||
var thedata = [];
|
||||
|
||||
var inputssize = theinputs.size();
|
||||
for ( var i=0; i<inputssize; i++ )
|
||||
{
|
||||
if(theinputs.item(i).get("name")!="returnurl") {//dont include return url for ajax requests
|
||||
for (var i = 0; i < inputssize; i++) {
|
||||
if(theinputs.item(i).get("name") != "returnurl") { // Dont include return url for ajax requests.
|
||||
thedata[theinputs.item(i).get("name")] = theinputs.item(i).get("value");
|
||||
}
|
||||
}
|
||||
@@ -44,24 +43,24 @@ M.core_rating={
|
||||
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 = scope.Y.one('#ratingaggregate'+itemid);
|
||||
var node = scope.Y.one('#ratingaggregate' + itemid);
|
||||
node.set('innerHTML',data.aggregate);
|
||||
|
||||
//empty the count value if no ratings
|
||||
var node = scope.Y.one('#ratingcount'+itemid);
|
||||
// Empty the count value if no ratings.
|
||||
var node = scope.Y.one('#ratingcount' + itemid);
|
||||
if (data.count > 0) {
|
||||
node.set('innerHTML',"("+data.count+")");
|
||||
node.set('innerHTML', "(" + data.count + ")");
|
||||
} else {
|
||||
node.set('innerHTML',"");
|
||||
node.set('innerHTML', "");
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else if (data.error){
|
||||
else if (data.error) {
|
||||
alert(data.error);
|
||||
}
|
||||
} catch(e) {
|
||||
alert(e.message+" "+outcome.responseText);
|
||||
alert(e.message + " " + outcome.responseText);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -76,4 +75,4 @@ M.core_rating={
|
||||
this.Y.io(this.api, cfg);
|
||||
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
+10
-13
@@ -1,5 +1,4 @@
|
||||
<?php
|
||||
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
@@ -35,19 +34,19 @@ $ratingarea = required_param('ratingarea', PARAM_AREA);
|
||||
$itemid = required_param('itemid', PARAM_INT);
|
||||
$scaleid = required_param('scaleid', PARAM_INT);
|
||||
$userrating = required_param('rating', PARAM_INT);
|
||||
$rateduserid = required_param('rateduserid', PARAM_INT);//which user is being rated. Required to update their grade
|
||||
$returnurl = required_param('returnurl', PARAM_LOCALURL);//required for non-ajax requests
|
||||
$rateduserid = required_param('rateduserid', PARAM_INT); // Which user is being rated. Required to update their grade.
|
||||
$returnurl = required_param('returnurl', PARAM_LOCALURL); // Required for non-ajax requests.
|
||||
|
||||
$result = new stdClass;
|
||||
|
||||
list($context, $course, $cm) = get_context_info_array($contextid);
|
||||
require_login($course, false, $cm);
|
||||
|
||||
$contextid = null;//now we have a context object throw away the id from the user
|
||||
$contextid = null; // Now we have a context object, throw away the id from the user.
|
||||
$PAGE->set_context($context);
|
||||
$PAGE->set_url('/rating/rate.php', array('contextid' => $context->id));
|
||||
|
||||
if (!confirm_sesskey() || !has_capability('moodle/rating:rate',$context)) {
|
||||
if (!confirm_sesskey() || !has_capability('moodle/rating:rate', $context)) {
|
||||
echo $OUTPUT->header();
|
||||
echo get_string('ratepermissiondenied', 'rating');
|
||||
echo $OUTPUT->footer();
|
||||
@@ -56,8 +55,8 @@ if (!confirm_sesskey() || !has_capability('moodle/rating:rate',$context)) {
|
||||
|
||||
$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
|
||||
// Check the module rating permissions.
|
||||
// Doing this check here rather than within rating_manager::get_ratings() so we can return a json error response.
|
||||
$pluginpermissionsarray = $rm->get_plugin_permissions_array($context->id, $component, $ratingarea);
|
||||
|
||||
if (!$pluginpermissionsarray['rate']) {
|
||||
@@ -93,7 +92,7 @@ if ($userrating != RATING_UNSET_RATING) {
|
||||
|
||||
$rating = new rating($ratingoptions);
|
||||
$rating->update_rating($userrating);
|
||||
} else { //delete the rating if the user set to Rate...
|
||||
} else { // Delete the rating if the user set to "Rate..."
|
||||
$options = new stdClass;
|
||||
$options->contextid = $context->id;
|
||||
$options->component = $component;
|
||||
@@ -104,12 +103,10 @@ if ($userrating != RATING_UNSET_RATING) {
|
||||
$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
|
||||
if (!empty($cm) && $context->contextlevel == CONTEXT_MODULE) {
|
||||
//tell the module that its grades have changed
|
||||
// Tell the module that its grades have changed.
|
||||
$modinstance = $DB->get_record($cm->modname, array('id' => $cm->instance), '*', MUST_EXIST);
|
||||
$modinstance->cmidnumber = $cm->id; //MDL-12961
|
||||
$modinstance->cmidnumber = $cm->id; // MDL-12961.
|
||||
$functionname = $cm->modname.'_update_grades';
|
||||
require_once($CFG->dirroot."/mod/{$cm->modname}/lib.php");
|
||||
if (function_exists($functionname)) {
|
||||
@@ -117,4 +114,4 @@ if (!empty($cm) && $context->contextlevel == CONTEXT_MODULE) {
|
||||
}
|
||||
}
|
||||
|
||||
redirect($returnurl);
|
||||
redirect($returnurl);
|
||||
|
||||
+27
-28
@@ -1,5 +1,4 @@
|
||||
<?php
|
||||
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
@@ -16,7 +15,7 @@
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* This page receives ajax rating submissions
|
||||
* This page receives ajax rating submissions
|
||||
*
|
||||
* It is similar to rate.php. Unlike rate.php a return url is NOT required.
|
||||
*
|
||||
@@ -37,13 +36,13 @@ $ratingarea = required_param('ratingarea', PARAM_AREA);
|
||||
$itemid = required_param('itemid', PARAM_INT);
|
||||
$scaleid = required_param('scaleid', PARAM_INT);
|
||||
$userrating = required_param('rating', PARAM_INT);
|
||||
$rateduserid = required_param('rateduserid', PARAM_INT);//which user is being rated. Required to update their grade
|
||||
$aggregationmethod = optional_param('aggregation', RATING_AGGREGATE_NONE, PARAM_INT);//we're going to calculate the aggregate and return it to the client
|
||||
$rateduserid = required_param('rateduserid', PARAM_INT); // The user being rated. Required to update their grade.
|
||||
$aggregationmethod = optional_param('aggregation', RATING_AGGREGATE_NONE, PARAM_INT); // Used to calculate the aggregate to return.
|
||||
|
||||
$result = new stdClass;
|
||||
|
||||
//if session has expired and its an ajax request so we cant do a page redirect
|
||||
if( !isloggedin() ){
|
||||
// If session has expired and its an ajax request so we cant do a page redirect.
|
||||
if (!isloggedin()) {
|
||||
$result->error = get_string('sessionerroruser', 'error');
|
||||
echo json_encode($result);
|
||||
die();
|
||||
@@ -52,11 +51,11 @@ if( !isloggedin() ){
|
||||
list($context, $course, $cm) = get_context_info_array($contextid);
|
||||
require_login($course, false, $cm);
|
||||
|
||||
$contextid = null;//now we have a context object throw away the id from the user
|
||||
$contextid = null; // Now we have a context object, throw away the id from the user.
|
||||
$PAGE->set_context($context);
|
||||
$PAGE->set_url('/rating/rate_ajax.php', array('contextid'=>$context->id));
|
||||
$PAGE->set_url('/rating/rate_ajax.php', array('contextid' => $context->id));
|
||||
|
||||
if (!confirm_sesskey() || !has_capability('moodle/rating:rate',$context)) {
|
||||
if (!confirm_sesskey() || !has_capability('moodle/rating:rate', $context)) {
|
||||
echo $OUTPUT->header();
|
||||
echo get_string('ratepermissiondenied', 'rating');
|
||||
echo $OUTPUT->footer();
|
||||
@@ -65,8 +64,8 @@ if (!confirm_sesskey() || !has_capability('moodle/rating:rate',$context)) {
|
||||
|
||||
$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
|
||||
// Check the module rating permissions.
|
||||
// Doing this check here rather than within rating_manager::get_ratings() so we can return a json error response.
|
||||
$pluginpermissionsarray = $rm->get_plugin_permissions_array($context->id, $component, $ratingarea);
|
||||
|
||||
if (!$pluginpermissionsarray['rate']) {
|
||||
@@ -91,7 +90,7 @@ if (!$pluginpermissionsarray['rate']) {
|
||||
}
|
||||
}
|
||||
|
||||
//rating options used to update the rating then retrieve the aggregate
|
||||
// Rating options used to update the rating then retrieve the aggregate.
|
||||
$ratingoptions = new stdClass;
|
||||
$ratingoptions->context = $context;
|
||||
$ratingoptions->ratingarea = $ratingarea;
|
||||
@@ -103,7 +102,7 @@ $ratingoptions->userid = $USER->id;
|
||||
if ($userrating != RATING_UNSET_RATING) {
|
||||
$rating = new rating($ratingoptions);
|
||||
$rating->update_rating($userrating);
|
||||
} else { //delete the rating if the user set to Rate...
|
||||
} else { // Delete the rating if the user set to "Rate..."
|
||||
$options = new stdClass;
|
||||
$options->contextid = $context->id;
|
||||
$options->component = $component;
|
||||
@@ -114,13 +113,13 @@ if ($userrating != RATING_UNSET_RATING) {
|
||||
$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
|
||||
// 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.
|
||||
if ($context->contextlevel == CONTEXT_MODULE) {
|
||||
//tell the module that its grades have changed
|
||||
// Tell the module that its grades have changed.
|
||||
$modinstance = $DB->get_record($cm->modname, array('id' => $cm->instance));
|
||||
if ($modinstance) {
|
||||
$modinstance->cmidnumber = $cm->id; //MDL-12961
|
||||
$modinstance->cmidnumber = $cm->id; // MDL-12961.
|
||||
$functionname = $cm->modname.'_update_grades';
|
||||
require_once($CFG->dirroot."/mod/{$cm->modname}/lib.php");
|
||||
if (function_exists($functionname)) {
|
||||
@@ -129,44 +128,44 @@ if ($context->contextlevel == CONTEXT_MODULE) {
|
||||
}
|
||||
}
|
||||
|
||||
//object to return to client as json
|
||||
// Object to return to client as JSON.
|
||||
$result->success = true;
|
||||
|
||||
//need to retrieve the updated item to get its new aggregate value
|
||||
// Need to retrieve the updated item to get its new aggregate value.
|
||||
$item = new stdClass;
|
||||
$item->id = $itemid;
|
||||
|
||||
//most of $ratingoptions variables were previously set
|
||||
// Most of $ratingoptions variables were previously set.
|
||||
$ratingoptions->items = array($item);
|
||||
$ratingoptions->aggregate = $aggregationmethod;
|
||||
|
||||
$items = $rm->get_ratings($ratingoptions);
|
||||
$firstrating = $items[0]->rating;
|
||||
|
||||
//for custom scales return text not the value
|
||||
//this scales weirdness will go away when scales are refactored
|
||||
// For custom scales return text not the value.
|
||||
// This scales weirdness will go away when scales are refactored.
|
||||
$scalearray = null;
|
||||
$aggregatetoreturn = round($firstrating->aggregate, 1);
|
||||
|
||||
// Output a dash if aggregation method == COUNT as the count is output next to the aggregate anyway
|
||||
// Output a dash if aggregation method == COUNT as the count is output next to the aggregate anyway.
|
||||
if ($firstrating->settings->aggregationmethod == RATING_AGGREGATE_COUNT or $firstrating->count == 0) {
|
||||
$aggregatetoreturn = ' - ';
|
||||
} else if ($firstrating->settings->scale->id < 0) { //if its non-numeric scale
|
||||
//dont use the scale item if the aggregation method is sum as adding items from a custom scale makes no sense
|
||||
} else if ($firstrating->settings->scale->id < 0) { // If its non-numeric scale.
|
||||
// Dont use the scale item if the aggregation method is sum as adding items from a custom scale makes no sense.
|
||||
if ($firstrating->settings->aggregationmethod != RATING_AGGREGATE_SUM) {
|
||||
$scalerecord = $DB->get_record('scale', array('id' => -$firstrating->settings->scale->id));
|
||||
if ($scalerecord) {
|
||||
$scalearray = explode(',', $scalerecord->scale);
|
||||
$aggregatetoreturn = $scalearray[$aggregatetoreturn-1];
|
||||
$aggregatetoreturn = $scalearray[$aggregatetoreturn - 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//See if the user has permission to see the rating aggregate
|
||||
// See if the user has permission to see the rating aggregate.
|
||||
if ($firstrating->user_can_view_aggregate()) {
|
||||
$result->aggregate = $aggregatetoreturn;
|
||||
$result->count = $firstrating->count;
|
||||
$result->itemid = $itemid;
|
||||
}
|
||||
|
||||
echo json_encode($result);
|
||||
echo json_encode($result);
|
||||
|
||||
+107
-55
@@ -25,7 +25,7 @@
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
// Include all the needed stuff
|
||||
// Include all the needed stuff.
|
||||
global $CFG;
|
||||
require_once($CFG->dirroot . '/rating/lib.php');
|
||||
|
||||
@@ -51,38 +51,90 @@ class core_rating_testcase extends advanced_testcase {
|
||||
/**
|
||||
* Test the current get_ratings method main sql
|
||||
*/
|
||||
function test_get_ratings_sql() {
|
||||
public function test_get_ratings_sql() {
|
||||
global $DB;
|
||||
|
||||
// We load 3 items. Each is rated twice. For simplicity itemid == user id of the item owner
|
||||
// We load 3 items. Each is rated twice. For simplicity itemid == user id of the item owner.
|
||||
$ctxid = context_system::instance()->id;
|
||||
$ratings = array(
|
||||
//user 1's items. Average == 2
|
||||
array('contextid'=>$ctxid ,'component'=>'mod_forum','ratingarea'=>'post','itemid'=>1 ,'scaleid'=>10 ,'rating'=>1 ,'userid'=>2 ,'timecreated'=>1 ,'timemodified'=>1),
|
||||
array('contextid'=>$ctxid ,'component'=>'mod_forum','ratingarea'=>'post','itemid'=>1 ,'scaleid'=>10 ,'rating'=>3 ,'userid'=>3 ,'timecreated'=>1 ,'timemodified'=>1),
|
||||
//user 2's items. Average == 3
|
||||
array('contextid'=>$ctxid ,'component'=>'mod_forum','ratingarea'=>'post','itemid'=>2 ,'scaleid'=>10 ,'rating'=>1 ,'userid'=>1 ,'timecreated'=>1 ,'timemodified'=>1),
|
||||
array('contextid'=>$ctxid ,'component'=>'mod_forum','ratingarea'=>'post','itemid'=>2 ,'scaleid'=>10 ,'rating'=>5 ,'userid'=>3 ,'timecreated'=>1 ,'timemodified'=>1),
|
||||
//user 3's items. Average == 4
|
||||
array('contextid'=>$ctxid ,'component'=>'mod_forum','ratingarea'=>'post','itemid'=>3 ,'scaleid'=>10 ,'rating'=>3 ,'userid'=>1 ,'timecreated'=>1 ,'timemodified'=>1),
|
||||
array('contextid'=>$ctxid ,'component'=>'mod_forum','ratingarea'=>'post','itemid'=>3 ,'scaleid'=>10 ,'rating'=>5 ,'userid'=>2 ,'timecreated'=>1 ,'timemodified'=>1)
|
||||
// User 1's items. Average == 2.
|
||||
array('contextid' => $ctxid,
|
||||
'component' => 'mod_forum',
|
||||
'ratingarea' => 'post',
|
||||
'itemid' => 1,
|
||||
'scaleid' => 10,
|
||||
'rating' => 1,
|
||||
'userid' => 2,
|
||||
'timecreated' => 1,
|
||||
'timemodified' => 1),
|
||||
|
||||
array('contextid' => $ctxid,
|
||||
'component' => 'mod_forum',
|
||||
'ratingarea' => 'post',
|
||||
'itemid' => 1,
|
||||
'scaleid' => 10,
|
||||
'rating' => 3,
|
||||
'userid' => 3,
|
||||
'timecreated' => 1,
|
||||
'timemodified' => 1),
|
||||
|
||||
// User 2's items. Average == 3.
|
||||
array('contextid' => $ctxid,
|
||||
'component' => 'mod_forum',
|
||||
'ratingarea' => 'post',
|
||||
'itemid' => 2,
|
||||
'scaleid' => 10,
|
||||
'rating' => 1,
|
||||
'userid' => 1,
|
||||
'timecreated' => 1,
|
||||
'timemodified' => 1),
|
||||
|
||||
array('contextid' => $ctxid,
|
||||
'component' => 'mod_forum',
|
||||
'ratingarea' => 'post',
|
||||
'itemid' => 2,
|
||||
'scaleid' => 10,
|
||||
'rating' => 5,
|
||||
'userid' => 3,
|
||||
'timecreated' => 1,
|
||||
'timemodified' => 1),
|
||||
|
||||
// User 3's items. Average == 4.
|
||||
array('contextid' => $ctxid,
|
||||
'component' => 'mod_forum',
|
||||
'ratingarea' => 'post',
|
||||
'itemid' => 3,
|
||||
'scaleid' => 10,
|
||||
'rating' => 3,
|
||||
'userid' => 1,
|
||||
'timecreated' => 1,
|
||||
'timemodified' => 1),
|
||||
|
||||
array('contextid' => $ctxid,
|
||||
'component' => 'mod_forum',
|
||||
'ratingarea' => 'post',
|
||||
'itemid' => 3,
|
||||
'scaleid' => 10,
|
||||
'rating' => 5,
|
||||
'userid' => 2,
|
||||
'timecreated' => 1,
|
||||
'timemodified' => 1)
|
||||
);
|
||||
foreach ($ratings as $rating) {
|
||||
$DB->insert_record('rating', $rating);
|
||||
}
|
||||
|
||||
|
||||
// a post (item) by user 1 (rated above by user 2 and 3 with average = 2)
|
||||
// A post (item) by user 1 (rated above by user 2 and 3 with average = 2).
|
||||
$user1posts = array(
|
||||
(object)array('id' => 1, 'userid' => 1, 'message' => 'hello'));
|
||||
// a post (item) by user 2 (rated above by user 1 and 3 with average = 3)
|
||||
// A post (item) by user 2 (rated above by user 1 and 3 with average = 3).
|
||||
$user2posts = array(
|
||||
(object)array('id' => 2, 'userid' => 2, 'message' => 'world'));
|
||||
// a post (item) by user 3 (rated above by user 1 and 2 with average = 4)
|
||||
// A post (item) by user 3 (rated above by user 1 and 2 with average = 4).
|
||||
$user3posts = array(
|
||||
(object)array('id' => 3, 'userid' => 3, 'message' => 'moodle'));
|
||||
|
||||
// Prepare the default options
|
||||
// Prepare the default options.
|
||||
$defaultoptions = array (
|
||||
'context' => context_system::instance(),
|
||||
'component' => 'mod_forum',
|
||||
@@ -92,9 +144,9 @@ class core_rating_testcase extends advanced_testcase {
|
||||
|
||||
$rm = new mockup_rating_manager();
|
||||
|
||||
// STEP 1: Retreive ratings using the current user
|
||||
// STEP 1: Retreive ratings using the current user.
|
||||
|
||||
// Get results for user 1's item (expected average 1 + 3 / 2 = 2)
|
||||
// Get results for user 1's item (expected average 1 + 3 / 2 = 2).
|
||||
$toptions = (object)array_merge($defaultoptions, array('items' => $user1posts));
|
||||
$result = $rm->get_ratings($toptions);
|
||||
$this->assertEquals(count($result), count($user1posts));
|
||||
@@ -103,10 +155,10 @@ class core_rating_testcase extends advanced_testcase {
|
||||
$this->assertEquals($result[0]->message, $user1posts[0]->message);
|
||||
$this->assertEquals($result[0]->rating->count, 2);
|
||||
$this->assertEquals($result[0]->rating->aggregate, 2);
|
||||
// Note that $result[0]->rating->rating is somewhat random
|
||||
// We didn't supply a user ID so $USER was used which will vary depending on who runs the tests
|
||||
// Note that $result[0]->rating->rating is somewhat random.
|
||||
// We didn't supply a user ID so $USER was used which will vary depending on who runs the tests.
|
||||
|
||||
// Get results for items of user 2 (expected average 1 + 5 / 2 = 3)
|
||||
// Get results for items of user 2 (expected average 1 + 5 / 2 = 3).
|
||||
$toptions = (object)array_merge($defaultoptions, array('items' => $user2posts));
|
||||
$result = $rm->get_ratings($toptions);
|
||||
$this->assertEquals(count($result), count($user2posts));
|
||||
@@ -115,10 +167,10 @@ class core_rating_testcase extends advanced_testcase {
|
||||
$this->assertEquals($result[0]->message, $user2posts[0]->message);
|
||||
$this->assertEquals($result[0]->rating->count, 2);
|
||||
$this->assertEquals($result[0]->rating->aggregate, 3);
|
||||
// Note that $result[0]->rating->rating is somewhat random
|
||||
// We didn't supply a user ID so $USER was used which will vary depending on who runs the tests
|
||||
// Note that $result[0]->rating->rating is somewhat random.
|
||||
// We didn't supply a user ID so $USER was used which will vary depending on who runs the tests.
|
||||
|
||||
// Get results for items of user 3 (expected average 3 + 5 / 2 = 4)
|
||||
// Get results for items of user 3 (expected average 3 + 5 / 2 = 4).
|
||||
$toptions = (object)array_merge($defaultoptions, array('items' => $user3posts));
|
||||
$result = $rm->get_ratings($toptions);
|
||||
$this->assertEquals(count($result), count($user3posts));
|
||||
@@ -127,10 +179,10 @@ class core_rating_testcase extends advanced_testcase {
|
||||
$this->assertEquals($result[0]->message, $user3posts[0]->message);
|
||||
$this->assertEquals($result[0]->rating->count, 2);
|
||||
$this->assertEquals($result[0]->rating->aggregate, 4);
|
||||
// Note that $result[0]->rating->rating is somewhat random
|
||||
// We didn't supply a user ID so $USER was used which will vary depending on who runs the tests
|
||||
// Note that $result[0]->rating->rating is somewhat random.
|
||||
// We didn't supply a user ID so $USER was used which will vary depending on who runs the tests.
|
||||
|
||||
// Get results for items of user 1 & 2 together (expected averages are 2 and 3, as tested above)
|
||||
// Get results for items of user 1 & 2 together (expected averages are 2 and 3, as tested above).
|
||||
$posts = array_merge($user1posts, $user2posts);
|
||||
$toptions = (object)array_merge($defaultoptions, array('items' => $posts));
|
||||
$result = $rm->get_ratings($toptions);
|
||||
@@ -140,21 +192,21 @@ class core_rating_testcase extends advanced_testcase {
|
||||
$this->assertEquals($result[0]->message, $posts[0]->message);
|
||||
$this->assertEquals($result[0]->rating->count, 2);
|
||||
$this->assertEquals($result[0]->rating->aggregate, 2);
|
||||
// Note that $result[0]->rating->rating is somewhat random
|
||||
// We didn't supply a user ID so $USER was used which will vary depending on who runs the tests
|
||||
// Note that $result[0]->rating->rating is somewhat random.
|
||||
// We didn't supply a user ID so $USER was used which will vary depending on who runs the tests.
|
||||
|
||||
$this->assertEquals($result[1]->id, $posts[1]->id);
|
||||
$this->assertEquals($result[1]->userid, $posts[1]->userid);
|
||||
$this->assertEquals($result[1]->message, $posts[1]->message);
|
||||
$this->assertEquals($result[1]->rating->count, 2);
|
||||
$this->assertEquals($result[1]->rating->aggregate, 3);
|
||||
// Note that $result[0]->rating->rating is somewhat random
|
||||
// We didn't supply a user ID so $USER was used which will vary depending on who runs the tests
|
||||
// Note that $result[0]->rating->rating is somewhat random.
|
||||
// We didn't supply a user ID so $USER was used which will vary depending on who runs the tests.
|
||||
|
||||
// STEP 2: Retrieve ratings by a specified user
|
||||
// We still expect complete aggregations and counts
|
||||
// STEP 2: Retrieve ratings by a specified user.
|
||||
// We still expect complete aggregations and counts.
|
||||
|
||||
// Get results for items of user 1 rated by user 2 (avg 2, rating 1)
|
||||
// Get results for items of user 1 rated by user 2 (avg 2, rating 1).
|
||||
$toptions = (object)array_merge($defaultoptions, array('items' => $user1posts, 'userid' => 2));
|
||||
$result = $rm->get_ratings($toptions);
|
||||
$this->assertEquals(count($result), count($user1posts));
|
||||
@@ -163,10 +215,10 @@ class core_rating_testcase extends advanced_testcase {
|
||||
$this->assertEquals($result[0]->message, $user1posts[0]->message);
|
||||
$this->assertEquals($result[0]->rating->count, 2);
|
||||
$this->assertEquals($result[0]->rating->aggregate, 2);
|
||||
$this->assertEquals($result[0]->rating->rating, 1); //user 2 rated user 1 "1"
|
||||
$this->assertEquals($result[0]->rating->userid, $toptions->userid); // Must be the passed userid
|
||||
$this->assertEquals($result[0]->rating->rating, 1); // User 2 rated user 1 "1".
|
||||
$this->assertEquals($result[0]->rating->userid, $toptions->userid); // Must be the passed userid.
|
||||
|
||||
// Get results for items of user 1 rated by user 3
|
||||
// Get results for items of user 1 rated by user 3.
|
||||
$toptions = (object)array_merge($defaultoptions, array('items' => $user1posts, 'userid' => 3));
|
||||
$result = $rm->get_ratings($toptions);
|
||||
$this->assertEquals(count($result), count($user1posts));
|
||||
@@ -175,10 +227,10 @@ class core_rating_testcase extends advanced_testcase {
|
||||
$this->assertEquals($result[0]->message, $user1posts[0]->message);
|
||||
$this->assertEquals($result[0]->rating->count, 2);
|
||||
$this->assertEquals($result[0]->rating->aggregate, 2);
|
||||
$this->assertEquals($result[0]->rating->rating, 3); //user 3 rated user 1 "3"
|
||||
$this->assertEquals($result[0]->rating->userid, $toptions->userid); // Must be the passed userid
|
||||
$this->assertEquals($result[0]->rating->rating, 3); // User 3 rated user 1 "3".
|
||||
$this->assertEquals($result[0]->rating->userid, $toptions->userid); // Must be the passed userid.
|
||||
|
||||
// Get results for items of user 1 & 2 together rated by user 3
|
||||
// Get results for items of user 1 & 2 together rated by user 3.
|
||||
$posts = array_merge($user1posts, $user2posts);
|
||||
$toptions = (object)array_merge($defaultoptions, array('items' => $posts, 'userid' => 3));
|
||||
$result = $rm->get_ratings($toptions);
|
||||
@@ -188,37 +240,37 @@ class core_rating_testcase extends advanced_testcase {
|
||||
$this->assertEquals($result[0]->message, $posts[0]->message);
|
||||
$this->assertEquals($result[0]->rating->count, 2);
|
||||
$this->assertEquals($result[0]->rating->aggregate, 2);
|
||||
$this->assertEquals($result[0]->rating->rating, 3); //user 3 rated user 1 "3"
|
||||
$this->assertEquals($result[0]->rating->userid, $toptions->userid); // Must be the passed userid
|
||||
$this->assertEquals($result[0]->rating->rating, 3); // User 3 rated user 1 "3".
|
||||
$this->assertEquals($result[0]->rating->userid, $toptions->userid); // Must be the passed userid.
|
||||
|
||||
$this->assertEquals($result[1]->id, $posts[1]->id);
|
||||
$this->assertEquals($result[1]->userid, $posts[1]->userid);
|
||||
$this->assertEquals($result[1]->message, $posts[1]->message);
|
||||
$this->assertEquals($result[1]->rating->count, 2);
|
||||
$this->assertEquals($result[1]->rating->aggregate, 3);
|
||||
$this->assertEquals($result[0]->rating->rating, 3); //user 3 rated user 2 "5"
|
||||
$this->assertEquals($result[1]->rating->userid, $toptions->userid); // Must be the passed userid
|
||||
$this->assertEquals($result[0]->rating->rating, 3); // User 3 rated user 2 "5".
|
||||
$this->assertEquals($result[1]->rating->userid, $toptions->userid); // Must be the passed userid.
|
||||
|
||||
// STEP 3: Some special cases
|
||||
// STEP 3: Some special cases.
|
||||
|
||||
// Get results for user 1's items (expected average 1 + 3 / 2 = 2)
|
||||
// supplying a non-existent user id so no rating from that user should be found
|
||||
// Get results for user 1's items (expected average 1 + 3 / 2 = 2).
|
||||
// Supplying a non-existent user id so no rating from that user should be found.
|
||||
$toptions = (object)array_merge($defaultoptions, array('items' => $user1posts));
|
||||
$toptions->userid = 123456; //non-existent user
|
||||
$toptions->userid = 123456; // Non-existent user.
|
||||
$result = $rm->get_ratings($toptions);
|
||||
$this->assertNull($result[0]->rating->userid);
|
||||
$this->assertNull($result[0]->rating->rating);
|
||||
$this->assertEquals($result[0]->rating->aggregate, 2);//should still get the aggregate
|
||||
$this->assertEquals($result[0]->rating->aggregate, 2); // Should still get the aggregate.
|
||||
|
||||
// Get results for items of user 2 (expected average 1 + 5 / 2 = 3)
|
||||
// Supplying the user id of the user who owns the items so no rating should be found
|
||||
// Get results for items of user 2 (expected average 1 + 5 / 2 = 3).
|
||||
// Supplying the user id of the user who owns the items so no rating should be found.
|
||||
$toptions = (object)array_merge($defaultoptions, array('items' => $user2posts));
|
||||
$toptions->userid = 2; //user 2 viewing the ratings of their own item
|
||||
$toptions->userid = 2; // User 2 viewing the ratings of their own item.
|
||||
$result = $rm->get_ratings($toptions);
|
||||
//these should be null as the user is viewing their own item and thus cannot rate
|
||||
// These should be null as the user is viewing their own item and thus cannot rate.
|
||||
$this->assertNull($result[0]->rating->userid);
|
||||
$this->assertNull($result[0]->rating->rating);
|
||||
$this->assertEquals($result[0]->rating->aggregate, 3);//should still get the aggregate
|
||||
$this->assertEquals($result[0]->rating->aggregate, 3); // Should still get the aggregate.
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user