Skip to content
Permalink
Browse files
Merge branch 'MDL-57407-master' of git://github.com/jleyva/moodle
  • Loading branch information
David Monllao committed Sep 12, 2017
2 parents d887e98 + 84c50d1 commit 0500bc49f19ead29a7936b2b41dcbd7d8b52616f
@@ -542,6 +542,7 @@ public static function get_entry($entryid, $returncontents = false) {

$result = array(
'entry' => $entry,
'ratinginfo' => \core_rating\external\util::get_rating_info($database, $context, 'mod_data', 'entry', array($record)),
'warnings' => $warnings
);
// Check if we should return the entry rendered.
@@ -564,6 +565,7 @@ public static function get_entry_returns() {
array(
'entry' => record_exporter::get_read_structure(),
'entryviewcontents' => new external_value(PARAM_RAW, 'The entry as is rendered in the site.', VALUE_OPTIONAL),
'ratinginfo' => \core_rating\external\util::external_ratings_structure(),
'warnings' => new external_warnings()
)
);
@@ -1251,4 +1251,68 @@ public function test_update_entry_other_user() {
$this->expectException('moodle_exception');
mod_data_external::update_entry($entry11, []);
}

/**
* Test get_entry_rating_information.
*/
public function test_get_entry_rating_information() {
global $DB, $CFG;
require_once($CFG->dirroot . '/rating/lib.php');

$DB->set_field('data', 'assessed', RATING_AGGREGATE_SUM, array('id' => $this->database->id));
$DB->set_field('data', 'scale', 100, array('id' => $this->database->id));
list($entry11, $entry12, $entry13, $entry21) = self::populate_database_with_entries();

$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($user1->id, $this->course->id, $this->studentrole->id, 'manual');
$this->getDataGenerator()->enrol_user($user2->id, $this->course->id, $this->studentrole->id, 'manual');

// Rate the entry as user1.
$rating1 = new stdClass();
$rating1->contextid = $this->context->id;
$rating1->component = 'mod_data';
$rating1->ratingarea = 'entry';
$rating1->itemid = $entry11;
$rating1->rating = 50;
$rating1->scaleid = 100;
$rating1->userid = $user1->id;
$rating1->timecreated = time();
$rating1->timemodified = time();
$rating1->id = $DB->insert_record('rating', $rating1);

// Rate the entry as user2.
$rating2 = new stdClass();
$rating2->contextid = $this->context->id;
$rating2->component = 'mod_data';
$rating2->ratingarea = 'entry';
$rating2->itemid = $entry11;
$rating2->rating = 100;
$rating2->scaleid = 100;
$rating2->userid = $user2->id;
$rating2->timecreated = time() + 1;
$rating2->timemodified = time() + 1;
$rating2->id = $DB->insert_record('rating', $rating2);

// As student, retrieve ratings information.
$this->setUser($this->student2);
$result = mod_data_external::get_entry($entry11);
$result = external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result);
$this->assertCount(1, $result['ratinginfo']['ratings']);
$this->assertFalse($result['ratinginfo']['ratings'][0]['canviewaggregate']);
$this->assertFalse($result['ratinginfo']['canviewall']);
$this->assertFalse($result['ratinginfo']['ratings'][0]['canrate']);
$this->assertTrue(!isset($result['ratinginfo']['ratings'][0]['count']));

// Now, as teacher, I should see the info correctly.
$this->setUser($this->teacher);
$result = mod_data_external::get_entry($entry11);
$result = external_api::clean_returnvalue(mod_data_external::get_entry_returns(), $result);
$this->assertCount(1, $result['ratinginfo']['ratings']);
$this->assertTrue($result['ratinginfo']['ratings'][0]['canviewaggregate']);
$this->assertTrue($result['ratinginfo']['canviewall']);
$this->assertTrue($result['ratinginfo']['ratings'][0]['canrate']);
$this->assertEquals(2, $result['ratinginfo']['ratings'][0]['count']);
$this->assertEquals(100, $result['ratinginfo']['ratings'][0]['aggregate']); // Expect maximium scale value.
}
}
@@ -4,6 +4,7 @@ information provided here is intended especially for developers.
=== 3.4 ===
* External function mod_data_external::search_entries() now returns the maxcount field: Total count of records that the user could
see in the database (if all the search criterias were removed).
* External function get_entry now returns an additional field "ratinginfo" containing the entry rating information.

=== 3.3.2 ===
* data_refresh_events() Now takes two additional parameters to refine the update to a specific instance. This function
@@ -309,6 +309,7 @@ public static function get_forum_discussion_posts($discussionid, $sortby = "crea

$result = array();
$result['posts'] = $posts;
$result['ratinginfo'] = \core_rating\external\util::get_rating_info($forum, $modcontext, 'mod_forum', 'post', $posts);
$result['warnings'] = $warnings;
return $result;
}
@@ -349,6 +350,7 @@ public static function get_forum_discussion_posts_returns() {
), 'post'
)
),
'ratinginfo' => \core_rating\external\util::external_ratings_structure(),
'warnings' => new external_warnings()
)
);
@@ -275,6 +275,15 @@ public function test_mod_forum_get_forum_discussion_posts() {
// Create what we expect to be returned when querying the discussion.
$expectedposts = array(
'posts' => array(),
'ratinginfo' => array(
'contextid' => $forum1context->id,
'component' => 'mod_forum',
'ratingarea' => 'post',
'canviewall' => null,
'canviewany' => null,
'scales' => array(),
'ratings' => array(),
),
'warnings' => array(),
);

@@ -1066,4 +1075,95 @@ public function test_can_add_discussion() {

}

/**
* Test get forum posts discussions including rating information.
*/
public function test_mod_forum_get_forum_discussion_rating_information() {
global $DB, $CFG;
require_once($CFG->dirroot . '/rating/lib.php');

$this->resetAfterTest(true);

$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
$user3 = self::getDataGenerator()->create_user();
$teacher = self::getDataGenerator()->create_user();

// Create course to add the module.
$course = self::getDataGenerator()->create_course();

$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
$this->getDataGenerator()->enrol_user($user1->id, $course->id, $studentrole->id, 'manual');
$this->getDataGenerator()->enrol_user($user2->id, $course->id, $studentrole->id, 'manual');
$this->getDataGenerator()->enrol_user($user3->id, $course->id, $studentrole->id, 'manual');
$this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id, 'manual');

// Create the forum.
$record = new stdClass();
$record->course = $course->id;
// Set Aggregate type = Average of ratings.
$record->assessed = RATING_AGGREGATE_AVERAGE;
$record->scale = 100;
$forum = self::getDataGenerator()->create_module('forum', $record);
$context = context_module::instance($forum->cmid);

// Add discussion to the forum.
$record = new stdClass();
$record->course = $course->id;
$record->userid = $user1->id;
$record->forum = $forum->id;
$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));

// Rate the discussion as user2.
$rating1 = new stdClass();
$rating1->contextid = $context->id;
$rating1->component = 'mod_forum';
$rating1->ratingarea = 'post';
$rating1->itemid = $post->id;
$rating1->rating = 50;
$rating1->scaleid = 100;
$rating1->userid = $user2->id;
$rating1->timecreated = time();
$rating1->timemodified = time();
$rating1->id = $DB->insert_record('rating', $rating1);

// Rate the discussion as user3.
$rating2 = new stdClass();
$rating2->contextid = $context->id;
$rating2->component = 'mod_forum';
$rating2->ratingarea = 'post';
$rating2->itemid = $post->id;
$rating2->rating = 100;
$rating2->scaleid = 100;
$rating2->userid = $user3->id;
$rating2->timecreated = time() + 1;
$rating2->timemodified = time() + 1;
$rating2->id = $DB->insert_record('rating', $rating2);

// Retrieve the rating for the post as student.
$this->setUser($user1);
$posts = mod_forum_external::get_forum_discussion_posts($discussion->id);
$posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
$this->assertCount(1, $posts['ratinginfo']['ratings']);
$this->assertTrue($posts['ratinginfo']['ratings'][0]['canviewaggregate']);
$this->assertFalse($posts['ratinginfo']['canviewall']);
$this->assertFalse($posts['ratinginfo']['ratings'][0]['canrate']);
$this->assertEquals(2, $posts['ratinginfo']['ratings'][0]['count']);
$this->assertEquals(($rating1->rating + $rating2->rating) / 2, $posts['ratinginfo']['ratings'][0]['aggregate']);

// Retrieve the rating for the post as teacher.
$this->setUser($teacher);
$posts = mod_forum_external::get_forum_discussion_posts($discussion->id);
$posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
$this->assertCount(1, $posts['ratinginfo']['ratings']);
$this->assertTrue($posts['ratinginfo']['ratings'][0]['canviewaggregate']);
$this->assertTrue($posts['ratinginfo']['canviewall']);
$this->assertTrue($posts['ratinginfo']['ratings'][0]['canrate']);
$this->assertEquals(2, $posts['ratinginfo']['ratings'][0]['count']);
$this->assertEquals(($rating1->rating + $rating2->rating) / 2, $posts['ratinginfo']['ratings'][0]['aggregate']);
}
}
@@ -1,6 +1,9 @@
This files describes API changes in /mod/forum/*,
information provided here is intended especially for developers.

=== 3.4 ===
* External function get_forum_discussion_posts now returns an additional field "ratinginfo" containing rating information.

=== 3.3 ===
* External function get_forums_by_courses now returns and additional field "istracked" that indicates if the user
is tracking the related forum.
@@ -486,6 +486,7 @@ public static function get_entries_by_letter($id, $letter, $from, $limit, $optio
return array(
'count' => $count,
'entries' => $entries,
'ratinginfo' => \core_rating\external\util::get_rating_info($glossary, $context, 'mod_glossary', 'entry', $entries),
'warnings' => $warnings
);
}
@@ -502,6 +503,7 @@ public static function get_entries_by_letter_returns() {
'entries' => new external_multiple_structure(
self::get_entry_return_structure()
),
'ratinginfo' => \core_rating\external\util::external_ratings_structure(),
'warnings' => new external_warnings()
));
}
@@ -583,6 +585,7 @@ public static function get_entries_by_date($id, $order, $sort, $from, $limit, $o
return array(
'count' => $count,
'entries' => $entries,
'ratinginfo' => \core_rating\external\util::get_rating_info($glossary, $context, 'mod_glossary', 'entry', $entries),
'warnings' => $warnings
);
}
@@ -599,6 +602,7 @@ public static function get_entries_by_date_returns() {
'entries' => new external_multiple_structure(
self::get_entry_return_structure()
),
'ratinginfo' => \core_rating\external\util::external_ratings_structure(),
'warnings' => new external_warnings()
));
}
@@ -760,6 +764,7 @@ public static function get_entries_by_category($id, $categoryid, $from, $limit,
return array(
'count' => $count,
'entries' => $entries,
'ratinginfo' => \core_rating\external\util::get_rating_info($glossary, $context, 'mod_glossary', 'entry', $entries),
'warnings' => $warnings
);
}
@@ -776,6 +781,7 @@ public static function get_entries_by_category_returns() {
'entries' => new external_multiple_structure(
self::get_entry_return_structure(true)
),
'ratinginfo' => \core_rating\external\util::external_ratings_structure(),
'warnings' => new external_warnings()
));
}
@@ -954,6 +960,7 @@ public static function get_entries_by_author($id, $letter, $field, $sort, $from,
return array(
'count' => $count,
'entries' => $entries,
'ratinginfo' => \core_rating\external\util::get_rating_info($glossary, $context, 'mod_glossary', 'entry', $entries),
'warnings' => $warnings
);
}
@@ -970,6 +977,7 @@ public static function get_entries_by_author_returns() {
'entries' => new external_multiple_structure(
self::get_entry_return_structure()
),
'ratinginfo' => \core_rating\external\util::external_ratings_structure(),
'warnings' => new external_warnings()
));
}
@@ -1057,6 +1065,7 @@ public static function get_entries_by_author_id($id, $authorid, $order, $sort, $
return array(
'count' => $count,
'entries' => $entries,
'ratinginfo' => \core_rating\external\util::get_rating_info($glossary, $context, 'mod_glossary', 'entry', $entries),
'warnings' => $warnings
);
}
@@ -1073,6 +1082,7 @@ public static function get_entries_by_author_id_returns() {
'entries' => new external_multiple_structure(
self::get_entry_return_structure()
),
'ratinginfo' => \core_rating\external\util::external_ratings_structure(),
'warnings' => new external_warnings()
));
}
@@ -1158,6 +1168,7 @@ public static function get_entries_by_search($id, $query, $fullsearch, $order, $
return array(
'count' => $count,
'entries' => $entries,
'ratinginfo' => \core_rating\external\util::get_rating_info($glossary, $context, 'mod_glossary', 'entry', $entries),
'warnings' => $warnings
);
}
@@ -1174,6 +1185,7 @@ public static function get_entries_by_search_returns() {
'entries' => new external_multiple_structure(
self::get_entry_return_structure()
),
'ratinginfo' => \core_rating\external\util::external_ratings_structure(),
'warnings' => new external_warnings()
));
}
@@ -1238,6 +1250,7 @@ public static function get_entries_by_term($id, $term, $from, $limit, $options)
return array(
'count' => $count,
'entries' => $entries,
'ratinginfo' => \core_rating\external\util::get_rating_info($glossary, $context, 'mod_glossary', 'entry', $entries),
'warnings' => $warnings
);
}
@@ -1254,6 +1267,7 @@ public static function get_entries_by_term_returns() {
'entries' => new external_multiple_structure(
self::get_entry_return_structure()
),
'ratinginfo' => \core_rating\external\util::external_ratings_structure(),
'warnings' => new external_warnings()
));
}
@@ -1324,6 +1338,7 @@ public static function get_entries_to_approve($id, $letter, $order, $sort, $from
return array(
'count' => $count,
'entries' => $entries,
'ratinginfo' => \core_rating\external\util::get_rating_info($glossary, $context, 'mod_glossary', 'entry', $entries),
'warnings' => $warnings
);
}
@@ -1340,6 +1355,7 @@ public static function get_entries_to_approve_returns() {
'entries' => new external_multiple_structure(
self::get_entry_return_structure()
),
'ratinginfo' => \core_rating\external\util::external_ratings_structure(),
'warnings' => new external_warnings()
));
}
@@ -1385,6 +1401,8 @@ public static function get_entry_by_id($id) {

return array(
'entry' => $entry,
'ratinginfo' => \core_rating\external\util::get_rating_info($glossary, $context, 'mod_glossary', 'entry',
array($entry)),
'warnings' => $warnings
);
}
@@ -1398,6 +1416,7 @@ public static function get_entry_by_id($id) {
public static function get_entry_by_id_returns() {
return new external_single_structure(array(
'entry' => self::get_entry_return_structure(),
'ratinginfo' => \core_rating\external\util::external_ratings_structure(),
'warnings' => new external_warnings()
));
}
Loading

0 comments on commit 0500bc4

Please sign in to comment.