Merge branch 'MDL-55786-master' of git://github.com/jleyva/moodle

This commit is contained in:
Dan Poltawski 2016-10-03 15:44:40 +01:00
commit 8ba8b13bd9
5 changed files with 335 additions and 153 deletions

View File

@ -746,6 +746,14 @@ $functions = array(
'capabilities' => 'moodle/rating:view',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
'core_rating_add_rating' => array(
'classname' => 'core_rating_external',
'methodname' => 'add_rating',
'description' => 'Rates an item.',
'type' => 'write',
'capabilities' => 'moodle/rating:rate',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
'core_role_assign_roles' => array(
'classname' => 'core_role_external',
'methodname' => 'assign_roles',

View File

@ -195,4 +195,107 @@ class core_rating_external extends external_api {
);
}
/**
* Returns description of add_rating parameters.
*
* @return external_function_parameters
* @since Moodle 3.2
*/
public static function add_rating_parameters() {
return new external_function_parameters (
array(
'contextlevel' => new external_value(PARAM_ALPHA, 'context level: course, module, user, etc...'),
'instanceid' => new external_value(PARAM_INT, 'the instance id of item associated with the context level'),
'component' => new external_value(PARAM_COMPONENT, 'component'),
'ratingarea' => new external_value(PARAM_AREA, 'rating area'),
'itemid' => new external_value(PARAM_INT, 'associated id'),
'scaleid' => new external_value(PARAM_INT, 'scale id'),
'rating' => new external_value(PARAM_INT, 'user rating'),
'rateduserid' => new external_value(PARAM_INT, 'rated user id'),
'aggregation' => new external_value(PARAM_INT, 'agreggation method', VALUE_DEFAULT, RATING_AGGREGATE_NONE)
)
);
}
/**
* Adds a rating to an item
*
* @param string $contextlevel course, module, user...
* @param int $instanceid the instance if for the context element
* @param string $component the name of the component
* @param string $ratingarea rating area
* @param int $itemid the item id
* @param int $scaleid the scale id
* @param int $rating the user rating
* @param int $rateduserid the rated user id
* @param int $aggregation the aggregation method
* @return array result and possible warnings
* @throws moodle_exception
* @since Moodle 3.2
*/
public static function add_rating($contextlevel, $instanceid, $component, $ratingarea, $itemid, $scaleid, $rating, $rateduserid,
$aggregation = RATING_AGGREGATE_NONE) {
$warnings = array();
$params = array(
'contextlevel' => $contextlevel,
'instanceid' => $instanceid,
'component' => $component,
'ratingarea' => $ratingarea,
'itemid' => $itemid,
'scaleid' => $scaleid,
'rating' => $rating,
'rateduserid' => $rateduserid,
'aggregation' => $aggregation,
);
// Validate and normalize parameters.
$params = self::validate_parameters(self::add_rating_parameters(), $params);
$context = self::get_context_from_params($params);
self::validate_context($context);
$cm = get_coursemodule_from_id(false, $context->instanceid, 0, false, MUST_EXIST);
require_capability('moodle/rating:rate', $context);
$rm = new rating_manager();
$result = $rm->add_rating($cm, $context, $params['component'], $params['ratingarea'], $params['itemid'], $params['scaleid'],
$params['rating'], $params['rateduserid'], $params['aggregation']);
if (!empty($result->error)) {
throw new moodle_exception($result->error, 'rating');
}
$returndata = array(
'success' => $result->success,
'warnings' => $warnings
);
if (isset($result->aggregate)) {
$returndata['aggregate'] = $result->aggregate;
$returndata['count'] = $result->count;
$returndata['itemid'] = $result->itemid;
}
return $returndata;
}
/**
* Returns description of add_rating result values.
*
* @return external_single_structure
* @since Moodle 3.2
*/
public static function add_rating_returns() {
return new external_single_structure(
array(
'success' => new external_value(PARAM_BOOL, 'Whether the rate was successfully created'),
'aggregate' => new external_value(PARAM_TEXT, 'New aggregate', VALUE_OPTIONAL),
'count' => new external_value(PARAM_INT, 'Ratings count', VALUE_OPTIONAL),
'itemid' => new external_value(PARAM_INT, 'Rating item id', VALUE_OPTIONAL),
'warnings' => new external_warnings(),
)
);
}
}

View File

@ -1051,6 +1051,129 @@ class rating_manager {
return $aggregatelabel;
}
/**
* Adds a new rating
*
* @param stdClass $cm course module object
* @param stdClass $context context object
* @param string $component component name
* @param string $ratingarea rating area
* @param int $itemid the item id
* @param int $scaleid the scale id
* @param int $userrating the user rating
* @param int $rateduserid the rated user id
* @param int $aggregationmethod the aggregation method
* @since Moodle 3.2
*/
public function add_rating($cm, $context, $component, $ratingarea, $itemid, $scaleid, $userrating, $rateduserid,
$aggregationmethod) {
global $CFG, $DB, $USER;
$result = new stdClass;
// Check the module rating permissions.
// Doing this check here rather than within rating_manager::get_ratings() so we can return a error response.
$pluginpermissionsarray = $this->get_plugin_permissions_array($context->id, $component, $ratingarea);
if (!$pluginpermissionsarray['rate']) {
$result->error = 'ratepermissiondenied';
return $result;
} else {
$params = array(
'context' => $context,
'component' => $component,
'ratingarea' => $ratingarea,
'itemid' => $itemid,
'scaleid' => $scaleid,
'rating' => $userrating,
'rateduserid' => $rateduserid,
'aggregation' => $aggregationmethod
);
if (!$this->check_rating_is_valid($params)) {
$result->error = 'ratinginvalid';
return $result;
}
}
// Rating options used to update the rating then retrieve the aggregate.
$ratingoptions = new stdClass;
$ratingoptions->context = $context;
$ratingoptions->ratingarea = $ratingarea;
$ratingoptions->component = $component;
$ratingoptions->itemid = $itemid;
$ratingoptions->scaleid = $scaleid;
$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..."
$options = new stdClass;
$options->contextid = $context->id;
$options->component = $component;
$options->ratingarea = $ratingarea;
$options->userid = $USER->id;
$options->itemid = $itemid;
$this->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.
if ($context->contextlevel == CONTEXT_MODULE) {
// 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.
$functionname = $cm->modname.'_update_grades';
require_once($CFG->dirroot."/mod/{$cm->modname}/lib.php");
if (function_exists($functionname)) {
$functionname($modinstance, $rateduserid);
}
}
}
// Object to return to client as JSON.
$result->success = true;
// 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.
$ratingoptions->items = array($item);
$ratingoptions->aggregate = $aggregationmethod;
$items = $this->get_ratings($ratingoptions);
$firstrating = $items[0]->rating;
// See if the user has permission to see the rating aggregate.
if ($firstrating->user_can_view_aggregate()) {
// 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.
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.
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];
}
}
}
$result->aggregate = $aggregatetoreturn;
$result->count = $firstrating->count;
$result->itemid = $itemid;
}
return $result;
}
} // End rating_manager class definition.
/**

View File

@ -63,110 +63,11 @@ if (!confirm_sesskey() || !has_capability('moodle/rating:rate', $context)) {
}
$rm = new rating_manager();
$result = $rm->add_rating($cm, $context, $component, $ratingarea, $itemid, $scaleid, $userrating, $rateduserid, $aggregationmethod);
// 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']) {
$result->error = get_string('ratepermissiondenied', 'rating');
echo json_encode($result);
die();
} else {
$params = array(
'context' => $context,
'component' => $component,
'ratingarea' => $ratingarea,
'itemid' => $itemid,
'scaleid' => $scaleid,
'rating' => $userrating,
'rateduserid' => $rateduserid,
'aggregation' => $aggregationmethod
);
if (!$rm->check_rating_is_valid($params)) {
$result->error = get_string('ratinginvalid', 'rating');
echo json_encode($result);
die();
}
}
// Rating options used to update the rating then retrieve the aggregate.
$ratingoptions = new stdClass;
$ratingoptions->context = $context;
$ratingoptions->ratingarea = $ratingarea;
$ratingoptions->component = $component;
$ratingoptions->itemid = $itemid;
$ratingoptions->scaleid = $scaleid;
$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..."
$options = new stdClass;
$options->contextid = $context->id;
$options->component = $component;
$options->ratingarea = $ratingarea;
$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.
if ($context->contextlevel == CONTEXT_MODULE) {
// 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.
$functionname = $cm->modname.'_update_grades';
require_once($CFG->dirroot."/mod/{$cm->modname}/lib.php");
if (function_exists($functionname)) {
$functionname($modinstance, $rateduserid);
}
}
}
// Object to return to client as JSON.
$result->success = true;
// 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.
$ratingoptions->items = array($item);
$ratingoptions->aggregate = $aggregationmethod;
$items = $rm->get_ratings($ratingoptions);
$firstrating = $items[0]->rating;
// See if the user has permission to see the rating aggregate.
if ($firstrating->user_can_view_aggregate()) {
// 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.
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.
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];
}
}
}
$result->aggregate = $aggregatetoreturn;
$result->count = $firstrating->count;
$result->itemid = $itemid;
// Return translated error.
if (!empty($result->error)) {
$result->error = get_string($result->error, 'rating');
}
echo json_encode($result);

View File

@ -40,81 +40,89 @@ require_once($CFG->dirroot . '/rating/lib.php');
*/
class core_rating_externallib_testcase extends externallib_advanced_testcase {
/**
* Test get_item_ratings
/*
* Set up for every test
*/
public function test_get_item_ratings() {
public function setUp() {
global $DB;
$this->resetAfterTest();
global $DB, $USER;
$this->course = self::getDataGenerator()->create_course();
$this->student1 = $this->getDataGenerator()->create_user();
$this->student2 = $this->getDataGenerator()->create_user();
$this->teacher1 = $this->getDataGenerator()->create_user();
$this->teacher2 = $this->getDataGenerator()->create_user();
$this->teacher3 = $this->getDataGenerator()->create_user();
$this->studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
unassign_capability('moodle/site:accessallgroups', $this->teacherrole->id);
$this->resetAfterTest(true);
$course = self::getDataGenerator()->create_course();
$student = $this->getDataGenerator()->create_user();
$teacher1 = $this->getDataGenerator()->create_user();
$teacher2 = $this->getDataGenerator()->create_user();
$teacher3 = $this->getDataGenerator()->create_user();
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
unassign_capability('moodle/site:accessallgroups', $teacherrole->id);
$this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id);
$this->getDataGenerator()->enrol_user($teacher1->id, $course->id, $teacherrole->id);
$this->getDataGenerator()->enrol_user($teacher2->id, $course->id, $teacherrole->id);
$this->getDataGenerator()->enrol_user($teacher3->id, $course->id, $teacherrole->id);
$this->getDataGenerator()->enrol_user($this->student1->id, $this->course->id, $this->studentrole->id);
$this->getDataGenerator()->enrol_user($this->student2->id, $this->course->id, $this->studentrole->id);
$this->getDataGenerator()->enrol_user($this->teacher1->id, $this->course->id, $this->teacherrole->id);
$this->getDataGenerator()->enrol_user($this->teacher2->id, $this->course->id, $this->teacherrole->id);
$this->getDataGenerator()->enrol_user($this->teacher3->id, $this->course->id, $this->teacherrole->id);
// Create the forum.
$record = new stdClass();
$record->introformat = FORMAT_HTML;
$record->course = $course->id;
$record->course = $this->course->id;
// Set Aggregate type = Average of ratings.
$record->assessed = RATING_AGGREGATE_AVERAGE;
$forum = self::getDataGenerator()->create_module('forum', $record);
$record->scale = 100;
$this->forum = self::getDataGenerator()->create_module('forum', $record);
$contextid = context_module::instance($forum->cmid)->id;
$this->contextid = context_module::instance($this->forum->cmid)->id;
// Add discussion to the forums.
$record = new stdClass();
$record->course = $course->id;
$record->userid = $student->id;
$record->forum = $forum->id;
$discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
$record->course = $this->course->id;
$record->userid = $this->student1->id;
$record->forum = $this->forum->id;
$this->discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
// Retrieve the first post.
$post = $DB->get_record('forum_posts', array('discussion' => $discussion->id));
$this->post = $DB->get_record('forum_posts', array('discussion' => $this->discussion->id));
}
/**
* Test get_item_ratings
*/
public function test_get_item_ratings() {
global $DB;
// Rete the discussion as teacher1.
$rating1 = new stdClass();
$rating1->contextid = $contextid;
$rating1->contextid = $this->contextid;
$rating1->component = 'mod_forum';
$rating1->ratingarea = 'post';
$rating1->itemid = $post->id;
$rating1->itemid = $this->post->id;
$rating1->rating = 90;
$rating1->scaleid = 100;
$rating1->userid = $teacher1->id;
$rating1->userid = $this->teacher1->id;
$rating1->timecreated = time();
$rating1->timemodified = time();
$rating1->id = $DB->insert_record('rating', $rating1);
// Rete the discussion as teacher2.
$rating2 = new stdClass();
$rating2->contextid = $contextid;
$rating2->contextid = $this->contextid;
$rating2->component = 'mod_forum';
$rating2->ratingarea = 'post';
$rating2->itemid = $post->id;
$rating2->itemid = $this->post->id;
$rating2->rating = 95;
$rating2->scaleid = 100;
$rating2->userid = $teacher2->id;
$rating2->userid = $this->teacher2->id;
$rating2->timecreated = time() + 1;
$rating2->timemodified = time() + 1;
$rating2->id = $DB->insert_record('rating', $rating2);
// Delete teacher2, we must still receive the ratings.
delete_user($teacher2);
delete_user($this->teacher2);
// Teachers can see all the ratings.
$this->setUser($teacher1);
$this->setUser($this->teacher1);
$ratings = core_rating_external::get_item_ratings('module', $forum->cmid, 'mod_forum', 'post', $post->id, 100, '');
$ratings = core_rating_external::get_item_ratings('module', $this->forum->cmid, 'mod_forum', 'post', $this->post->id, 100, '');
// We need to execute the return values cleaning process to simulate the web service server.
$ratings = external_api::clean_returnvalue(core_rating_external::get_item_ratings_returns(), $ratings);
$this->assertCount(2, $ratings['ratings']);
@ -130,16 +138,16 @@ class core_rating_externallib_testcase extends externallib_advanced_testcase {
$this->assertEquals($rating2->userid, $indexedratings[$rating2->id]['userid']);
// Student can see ratings.
$this->setUser($student);
$this->setUser($this->student1);
$ratings = core_rating_external::get_item_ratings('module', $forum->cmid, 'mod_forum', 'post', $post->id, 100, '');
$ratings = core_rating_external::get_item_ratings('module', $this->forum->cmid, 'mod_forum', 'post', $this->post->id, 100, '');
// We need to execute the return values cleaning process to simulate the web service server.
$ratings = external_api::clean_returnvalue(core_rating_external::get_item_ratings_returns(), $ratings);
$this->assertCount(2, $ratings['ratings']);
// Invalid item.
try {
$ratings = core_rating_external::get_item_ratings('module', $forum->cmid, 'mod_forum', 'post', 0, 100, '');
$ratings = core_rating_external::get_item_ratings('module', $this->forum->cmid, 'mod_forum', 'post', 0, 100, '');
$this->fail('Exception expected due invalid itemid.');
} catch (moodle_exception $e) {
$this->assertEquals('invalidrecord', $e->errorcode);
@ -147,7 +155,7 @@ class core_rating_externallib_testcase extends externallib_advanced_testcase {
// Invalid area.
try {
$ratings = core_rating_external::get_item_ratings('module', $forum->cmid, 'mod_forum', 'xyz', $post->id, 100, '');
$ratings = core_rating_external::get_item_ratings('module', $this->forum->cmid, 'mod_forum', 'xyz', $this->post->id, 100, '');
$this->fail('Exception expected due invalid rating area.');
} catch (moodle_exception $e) {
$this->assertEquals('invalidratingarea', $e->errorcode);
@ -155,34 +163,73 @@ class core_rating_externallib_testcase extends externallib_advanced_testcase {
// Invalid context. invalid_parameter_exception.
try {
$ratings = core_rating_external::get_item_ratings('module', 0, 'mod_forum', 'post', $post->id, 100, '');
$ratings = core_rating_external::get_item_ratings('module', 0, 'mod_forum', 'post', $this->post->id, 100, '');
$this->fail('Exception expected due invalid context.');
} catch (invalid_parameter_exception $e) {
$this->assertEquals('invalidparameter', $e->errorcode);
}
// Test for groupmode.
set_coursemodule_groupmode($forum->cmid, SEPARATEGROUPS);
$group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
groups_add_member($group, $teacher1);
set_coursemodule_groupmode($this->forum->cmid, SEPARATEGROUPS);
$group = $this->getDataGenerator()->create_group(array('courseid' => $this->course->id));
groups_add_member($group, $this->teacher1);
$discussion->groupid = $group->id;
$DB->update_record('forum_discussions', $discussion);
$this->discussion->groupid = $group->id;
$DB->update_record('forum_discussions', $this->discussion);
// Error for teacher3 and 2 ratings for teacher1 should be returned.
$this->setUser($teacher1);
$ratings = core_rating_external::get_item_ratings('module', $forum->cmid, 'mod_forum', 'post', $post->id, 100, '');
$this->setUser($this->teacher1);
$ratings = core_rating_external::get_item_ratings('module', $this->forum->cmid, 'mod_forum', 'post', $this->post->id, 100, '');
// We need to execute the return values cleaning process to simulate the web service server.
$ratings = external_api::clean_returnvalue(core_rating_external::get_item_ratings_returns(), $ratings);
$this->assertCount(2, $ratings['ratings']);
$this->setUser($teacher3);
$this->setUser($this->teacher3);
try {
$ratings = core_rating_external::get_item_ratings('module', $forum->cmid, 'mod_forum', 'post', $post->id, 100, '');
$ratings = core_rating_external::get_item_ratings('module', $this->forum->cmid, 'mod_forum', 'post', $this->post->id, 100, '');
$this->fail('Exception expected due invalid group permissions.');
} catch (moodle_exception $e) {
$this->assertEquals('noviewrate', $e->errorcode);
}
}
/**
* Test add_rating
*/
public function test_add_rating() {
$this->setUser($this->teacher1);
// First rating of 50.
$rating = core_rating_external::add_rating('module', $this->forum->cmid, 'mod_forum', 'post', $this->post->id, 100,
50, $this->student1->id, RATING_AGGREGATE_AVERAGE);
// We need to execute the return values cleaning process to simulate the web service server.
$rating = external_api::clean_returnvalue(core_rating_external::add_rating_returns(), $rating);
$this->assertTrue($rating['success']);
$this->assertEquals(50, $rating['aggregate']);
$this->assertEquals(1, $rating['count']);
// New different rate (it will replace the existing one).
$rating = core_rating_external::add_rating('module', $this->forum->cmid, 'mod_forum', 'post', $this->post->id, 100,
100, $this->student1->id, RATING_AGGREGATE_AVERAGE);
$rating = external_api::clean_returnvalue(core_rating_external::add_rating_returns(), $rating);
$this->assertTrue($rating['success']);
$this->assertEquals(100, $rating['aggregate']);
$this->assertEquals(1, $rating['count']);
// Rate as other user.
$this->setUser($this->teacher2);
$rating = core_rating_external::add_rating('module', $this->forum->cmid, 'mod_forum', 'post', $this->post->id, 100,
50, $this->student1->id, RATING_AGGREGATE_AVERAGE);
$rating = external_api::clean_returnvalue(core_rating_external::add_rating_returns(), $rating);
$this->assertEquals(75, $rating['aggregate']);
$this->assertEquals(2, $rating['count']);
// Try to rate my own post.
$this->setUser($this->student1);
$this->expectException('moodle_exception');
$this->expectExceptionMessage(get_string('ratepermissiondenied', 'rating'));
$rating = core_rating_external::add_rating('module', $this->forum->cmid, 'mod_forum', 'post', $this->post->id, 100,
100, $this->student1->id, RATING_AGGREGATE_AVERAGE);
}
}