Permalink
Browse files

MDL-34398 groups: implemented caching of group information.

  • Loading branch information...
1 parent 11fbebb commit e17dbeeb1a0eeada466350a621ce0adc2a03b596 Sam Hemelryk committed Dec 20, 2012
View
6 backup/moodle2/restore_stepslib.php
@@ -778,6 +778,8 @@ public function process_group($data) {
}
// Save the id mapping
$this->set_mapping('group', $oldid, $newitemid, $restorefiles);
+ // Invalidate the course group data cache just in case.
+ cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($data->courseid));
}
public function process_grouping($data) {
@@ -821,6 +823,8 @@ public function process_grouping($data) {
}
// Save the id mapping
$this->set_mapping('grouping', $oldid, $newitemid, $restorefiles);
+ // Invalidate the course group data cache just in case.
+ cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($data->courseid));
}
public function process_grouping_group($data) {
@@ -838,6 +842,8 @@ protected function after_execute() {
$this->add_related_files('group', 'description', 'group');
// Add grouping related files, matching with "grouping" mappings
$this->add_related_files('grouping', 'description', 'grouping');
+ // Invalidate the course group data.
+ cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($this->get_courseid()));
}
}
View
2 enrol/imsenterprise/lib.php
@@ -704,6 +704,8 @@ function process_membership_tag($tagcontents){
$this->log_line('Added a new group for this course: '.$group->name);
$groupids[$member->groupname] = $groupid; // Store ID in cache
$member->groupid = $groupid;
+ // Invalidate the course group data cache just in case.
+ cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($ship->courseid));
}
}
// Add the user-to-group association if it doesn't already exist
View
10 group/assign.php
@@ -53,13 +53,19 @@
} else if (isset($frm->add) and !empty($frm->addselect)) {
foreach ($frm->addselect as $groupid) {
- groups_assign_grouping($grouping->id, (int)$groupid);
+ // Ask this method not to purge the cache, we'll do it ourselves afterwards.
+ groups_assign_grouping($grouping->id, (int)$groupid, null, false);
}
+ // Invalidate the course groups cache seeing as we've changed it.
+ cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($courseid));
} else if (isset($frm->remove) and !empty($frm->removeselect)) {
foreach ($frm->removeselect as $groupid) {
- groups_unassign_grouping($grouping->id, (int)$groupid);
+ // Ask this method not to purge the cache, we'll do it ourselves afterwards.
+ groups_unassign_grouping($grouping->id, (int)$groupid, false);
}
+ // Invalidate the course groups cache seeing as we've changed it.
+ cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($courseid));
}
}
View
8 group/autogroup.php
@@ -208,10 +208,14 @@
groups_add_member($groupid, $user->id);
}
if ($grouping) {
- groups_assign_grouping($grouping->id, $groupid);
+ // Ask this function not to invalidate the cache, we'll do that manually once at the end.
+ groups_assign_grouping($grouping->id, $groupid, null, false);
}
}
+ // Invalidate the course groups cache seeing as we've changed it.
+ cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($courseid));
+
if ($failed) {
foreach ($createdgroups as $groupid) {
groups_delete_group($groupid);
@@ -248,4 +252,4 @@
echo $preview;
}
-echo $OUTPUT->footer();
+echo $OUTPUT->footer();
View
50 group/lib.php
@@ -254,6 +254,9 @@ function groups_create_group($data, $editform = false, $editoroptions = false) {
groups_update_group_icon($group, $data, $editform);
}
+ // Invalidate the grouping cache for the course
+ cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($course->id));
+
//trigger groups events
events_trigger('groups_group_created', $group);
@@ -298,6 +301,9 @@ function groups_create_grouping($data, $editoroptions=null) {
$DB->update_record('groupings', $description);
}
+ // Invalidate the grouping cache for the course
+ cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($data->courseid));
+
events_trigger('groups_grouping_created', $data);
return $id;
@@ -329,6 +335,8 @@ function groups_update_group_icon($group, $data, $editform) {
$group->picture = 0;
}
@unlink($iconfile);
+ // Invalidate the group data as we've updated the group record.
+ cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($group->courseid));
}
}
}
@@ -361,6 +369,9 @@ function groups_update_group($data, $editform = false, $editoroptions = false) {
$DB->update_record('groups', $data);
+ // Invalidate the group data.
+ cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($data->courseid));
+
$group = $DB->get_record('groups', array('id'=>$data->id));
if ($editform) {
@@ -395,6 +406,10 @@ function groups_update_grouping($data, $editoroptions=null) {
$data = file_postupdate_standard_editor($data, 'description', $editoroptions, $editoroptions['context'], 'grouping', 'description', $data->id);
}
$DB->update_record('groupings', $data);
+
+ // Invalidate the group data.
+ cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($data->courseid));
+
//trigger groups events
events_trigger('groups_grouping_updated', $data);
@@ -438,6 +453,9 @@ function groups_delete_group($grouporid) {
$fs->delete_area_files($context->id, 'group', 'description', $groupid);
$fs->delete_area_files($context->id, 'group', 'icon', $groupid);
+ // Invalidate the grouping cache for the course
+ cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($group->courseid));
+
//trigger groups events
events_trigger('groups_group_deleted', $group);
@@ -480,6 +498,9 @@ function groups_delete_grouping($groupingorid) {
$file->delete();
}
+ // Invalidate the grouping cache for the course
+ cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($grouping->courseid));
+
//trigger groups events
events_trigger('groups_grouping_deleted', $grouping);
@@ -540,6 +561,9 @@ function groups_delete_groupings_groups($courseid, $showfeedback=false) {
$groupssql = "SELECT id FROM {groups} g WHERE g.courseid = ?";
$DB->delete_records_select('groupings_groups', "groupid IN ($groupssql)", array($courseid));
+ // Invalidate the grouping cache for the course
+ cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($courseid));
+
//trigger groups events
events_trigger('groups_groupings_groups_removed', $courseid);
@@ -578,6 +602,9 @@ function groups_delete_groups($courseid, $showfeedback=false) {
$DB->delete_records('groups', array('courseid'=>$courseid));
+ // Invalidate the grouping cache for the course
+ cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($courseid));
+
// trigger groups events
events_trigger('groups_groups_deleted', $courseid);
@@ -615,6 +642,9 @@ function groups_delete_groupings($courseid, $showfeedback=false) {
$DB->delete_records('groupings', array('courseid'=>$courseid));
+ // Invalidate the grouping cache for the course
+ cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($courseid));
+
// trigger groups events
events_trigger('groups_groupings_deleted', $courseid);
@@ -714,9 +744,10 @@ function groups_parse_name($format, $groupnumber) {
* @param int groupingid
* @param int groupid
* @param int $timeadded The time the group was added to the grouping.
+ * @param bool $invalidatecache If set to true the course group cache will be invalidated as well.
* @return bool true or exception
*/
-function groups_assign_grouping($groupingid, $groupid, $timeadded = null) {
+function groups_assign_grouping($groupingid, $groupid, $timeadded = null, $invalidatecache = true) {
global $DB;
if ($DB->record_exists('groupings_groups', array('groupingid'=>$groupingid, 'groupid'=>$groupid))) {
@@ -732,20 +763,33 @@ function groups_assign_grouping($groupingid, $groupid, $timeadded = null) {
}
$DB->insert_record('groupings_groups', $assign);
+ if ($invalidatecache) {
+ // Invalidate the grouping cache for the course
+ $courseid = $DB->get_field('groupings', 'courseid', array('id' => $groupingid));
+ cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($courseid));
+ }
+
return true;
}
/**
- * Unassigns group grom grouping
+ * Unassigns group from grouping
*
* @param int groupingid
* @param int groupid
+ * @param bool $invalidatecache If set to true the course group cache will be invalidated as well.
* @return bool success
*/
-function groups_unassign_grouping($groupingid, $groupid) {
+function groups_unassign_grouping($groupingid, $groupid, $invalidatecache = true) {
global $DB;
$DB->delete_records('groupings_groups', array('groupingid'=>$groupingid, 'groupid'=>$groupid));
+ if ($invalidatecache) {
+ // Invalidate the grouping cache for the course
+ $courseid = $DB->get_field('groupings', 'courseid', array('id' => $groupingid));
+ cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($courseid));
+ }
+
return true;
}
View
1 lang/en/cache.php
@@ -37,6 +37,7 @@
$string['cachedef_config'] = 'Config settings';
$string['cachedef_databasemeta'] = 'Database meta information';
$string['cachedef_eventinvalidation'] = 'Event invalidation';
+$string['cachedef_groupdata'] = 'Course group information';
$string['cachedef_htmlpurifier'] = 'HTML Purifier - cleaned content';
$string['cachedef_locking'] = 'Locking';
$string['cachedef_questiondata'] = 'Question definitions';
View
11 lib/db/caches.php
@@ -94,4 +94,15 @@
'persistent' => true,
'simpledata' => true
),
+
+ // Groupings belonging to a course.
+ // A simple cache designed to replace $GROUPLIB_CACHE->groupings.
+ // Items are organised by course id and are essentially course records.
+ 'groupdata' => array(
+ 'mode' => cache_store::MODE_APPLICATION,
+ 'simplekeys' => true, // The course id the groupings exist for.
+ 'simpledata' => true, // Array of stdClass objects containing only strings.
+ 'persist' => true, // Likely there will be a couple of calls to this.
+ 'persistmaxsize' => 2, // The original cache used 1, we've increased that to two.
+ )
);
View
159 lib/grouplib.php
@@ -86,9 +86,11 @@ function groups_get_grouping_name($groupingid) {
* @return int $groupid
*/
function groups_get_group_by_name($courseid, $name) {
- global $DB;
- if ($groups = $DB->get_records('groups', array('courseid'=>$courseid, 'name'=>$name))) {
- return key($groups);
+ $data = groups_get_course_data($courseid);
+ foreach ($data->groups as $group) {
+ if ($group->name == $name) {
+ return $group->id;
+ }
}
return false;
}
@@ -103,11 +105,14 @@ function groups_get_group_by_name($courseid, $name) {
* @return group object
*/
function groups_get_group_by_idnumber($courseid, $idnumber) {
- global $DB;
if (empty($idnumber)) {
return false;
- } else if ($group = $DB->get_record('groups', array('courseid' => $courseid, 'idnumber' => $idnumber))) {
- return $group;
+ }
+ $data = groups_get_course_data($courseid);
+ foreach ($data->groups as $group) {
+ if ($group->idnumber == $idnumber) {
+ return $group;
+ }
}
return false;
}
@@ -122,9 +127,11 @@ function groups_get_group_by_idnumber($courseid, $idnumber) {
* @return int $groupid
*/
function groups_get_grouping_by_name($courseid, $name) {
- global $DB;
- if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid, 'name'=>$name))) {
- return key($groupings);
+ $data = groups_get_course_data($courseid);
+ foreach ($data->groupings as $grouping) {
+ if ($grouping->name == $name) {
+ return $grouping->id;
+ }
}
return false;
}
@@ -139,11 +146,14 @@ function groups_get_grouping_by_name($courseid, $name) {
* @return grouping object
*/
function groups_get_grouping_by_idnumber($courseid, $idnumber) {
- global $DB;
if (empty($idnumber)) {
return false;
- } else if ($grouping = $DB->get_record('groupings', array('courseid' => $courseid, 'idnumber' => $idnumber))) {
- return $grouping;
+ }
+ $data = groups_get_course_data($courseid);
+ foreach ($data->groupings as $grouping) {
+ if ($grouping->idnumber == $idnumber) {
+ return $grouping;
+ }
}
return false;
}
@@ -189,6 +199,42 @@ function groups_get_grouping($groupingid, $fields='*', $strictness=IGNORE_MISSIN
function groups_get_all_groups($courseid, $userid=0, $groupingid=0, $fields='g.*') {
global $DB;
+ // We need to check that we each field in the fields list belongs to the group table and that it has not being
+ // aliased. If its something else we need to avoid the cache and run the query as who knows whats going on.
+ $knownfields = true;
+ if ($fields !== 'g.*') {
+ $fieldbits = explode(',', $fields);
+ foreach ($fieldbits as $bit) {
+ $bit = trim($bit);
+ if (strpos($bit, 'g.') !== 0 or stripos($bit, ' AS ') !== false) {
+ $knownfields = false;
+ break;
+ }
+ }
+ }
+
+ if (empty($userid) && $knownfields) {
+ // We can use the cache.
+ $data = groups_get_course_data($courseid);
+ if (empty($groupingid)) {
+ // All groups.. Easy!
+ $groups = $data->groups;
+ } else {
+ $groups = array();
+ foreach ($data->mappings as $mapping) {
+ if ($mapping->groupingid != $groupingid) {
+ continue;
+ }
+ if (isset($data->groups[$mapping->groupid])) {
+ $groups[$mapping->groupid] = $data->groups[$mapping->groupid];
+ }
+ }
+ }
+ // Yay! We could use the cache. One more query saved.
+ return $groups;
+ }
+
+
if (empty($userid)) {
$userfrom = "";
$userwhere = "";
@@ -276,22 +322,8 @@ function groups_get_user_groups($courseid, $userid=0) {
* @return array Returns an array of the grouping objects (empty if none)
*/
function groups_get_all_groupings($courseid) {
- global $CFG, $DB, $GROUPLIB_CACHE;
-
- // Use cached data if available. (Note: We only cache a single request, so
- // as not to waste memory if processing is happening for multiple courses.)
- if (!empty($GROUPLIB_CACHE->groupings) &&
- $GROUPLIB_CACHE->groupings->courseid == $courseid) {
- return $GROUPLIB_CACHE->groupings->result;
- }
- if (empty($GROUPLIB_CACHE)) {
- $GROUPLIB_CACHE = new stdClass();
- }
- $GROUPLIB_CACHE->groupings = new stdClass();
- $GROUPLIB_CACHE->groupings->courseid = $courseid;
- $GROUPLIB_CACHE->groupings->result =
- $DB->get_records('groupings', array('courseid' => $courseid), 'name ASC');
- return $GROUPLIB_CACHE->groupings->result;
+ $data = groups_get_course_data($courseid);
+ return $data->groupings;
}
/**
@@ -810,3 +842,74 @@ function _group_verify_activegroup($courseid, $groupmode, $groupingid, array $al
}
}
}
+
+/**
+ * Caches group data for a particular course to speed up subsequent requests.
+ *
+ * @param int $courseid The course id to cache data for.
+ * @param cache $cache The cache if it has already been initialised. If not a new one will be created.
+ * @return stdClass A data object containing groups, groupings, and mappings.
+ */
+function groups_cache_groupdata($courseid, cache $cache = null) {
+ global $DB;
+
+ if ($cache === null) {
+ // Initialise a cache if we wern't given one.
+ $cache = cache::make('core', 'groupdata');
+ }
+
+ // Get the groups that belong to the course.
+ $groups = $DB->get_records('groups', array('courseid' => $courseid), 'name ASC');
+ // Get the groupings that belong to the course.
+ $groupings = $DB->get_records('groupings', array('courseid' => $courseid), 'name ASC');
+
+ if (!is_array($groups)) {
+ $groups = array();
+ }
+
+ if (!is_array($groupings)) {
+ $groupings = array();
+ }
+
+ if (!empty($groupings)) {
+ // Finally get the mappings between the two.
+ $mappings = $DB->get_records_list('groupings_groups', 'groupingid', array_keys($groupings), '', 'id,groupingid,groupid');
+ } else {
+ $mappings = array();
+ }
+
+ // Prepare the data array.
+ $data = new stdClass;
+ $data->groups = $groups;
+ $data->groupings = $groupings;
+ $data->mappings = $mappings;
+ // Cache the data.
+ $cache->set($courseid, $data);
+ // Finally return it so it can be used if desired.
+ return $data;
+}
+
+/**
+ * Gets group data for a course.
+ *
+ * This returns an object with the following properties:
+ * - groups : An array of all the groups in the course.
+ * - groupings : An array of all the groupings within the course.
+ * - mappings : An array of group to grouping mappings.
+ *
+ * @param int $courseid The course id to get data for.
+ * @param cache $cache The cache if it has already been initialised. If not a new one will be created.
+ * @return stdClass
+ */
+function groups_get_course_data($courseid, cache $cache = null) {
+ if ($cache === null) {
+ // Initialise a cache if we wern't given one.
+ $cache = cache::make('core', 'groupdata');
+ }
+ // Try to retrieve it from the cache.
+ $data = $cache->get($courseid);
+ if ($data === false) {
+ $data = groups_cache_groupdata($courseid, $cache);
+ }
+ return $data;
+}
View
3 lib/phpunit/classes/util.php
@@ -554,7 +554,7 @@ public static function reset_dataroot() {
* @return void
*/
public static function reset_all_data($logchanges = false) {
- global $DB, $CFG, $USER, $SITE, $COURSE, $PAGE, $OUTPUT, $SESSION, $GROUPLIB_CACHE;
+ global $DB, $CFG, $USER, $SITE, $COURSE, $PAGE, $OUTPUT, $SESSION;
// Stop any message redirection.
phpunit_util::stop_message_redirection();
@@ -638,7 +638,6 @@ public static function reset_all_data($logchanges = false) {
if (class_exists('repository')) {
repository::reset_caches();
}
- $GROUPLIB_CACHE = null;
//TODO MDL-25290: add more resets here and probably refactor them to new core function
// Reset course and module caches.
View
8 lib/setup.php
@@ -353,14 +353,6 @@
global $OUTPUT;
/**
- * Cache used within grouplib to cache data within current request only.
- *
- * @global object $GROUPLLIB_CACHE
- * @name $GROUPLIB_CACHE
- */
-global $GROUPLIB_CACHE;
-
-/**
* Full script path including all params, slash arguments, scheme and host.
*
* Note: Do NOT use for getting of current page URL or detection of https,
View
234 lib/tests/grouplib_test.php
@@ -171,4 +171,238 @@ public function test_groups_get_grouping_by_idnumber() {
$grouping = $generator->create_grouping(array('courseid' => $course->id, 'idnumber' => $idnumber2));
$this->assertEquals(groups_get_grouping_by_idnumber($course->id, $idnumber2), $grouping);
}
+
+ public function test_groups_get_group_by_name() {
+ $this->resetAfterTest(true);
+
+ $generator = $this->getDataGenerator();
+
+ // Create a course category and course
+ $cat = $generator->create_category(array('parent' => 0));
+ $course = $generator->create_course(array('category' => $cat->id));
+
+ $name1 = 'Name 1';
+ $name2 = 'Name 2';
+
+ // Test with an empty and a null idnumber
+ $this->assertFalse(groups_get_group_by_name($course->id, ''));
+ $this->assertFalse(groups_get_group_by_name($course->id, null));
+
+ // Even when a group exists.
+ $generator->create_group(array('courseid' => $course->id));
+ $this->assertFalse(groups_get_group_by_name($course->id, ''));
+ $this->assertFalse(groups_get_group_by_name($course->id, null));
+
+ // Test with a valid name, but one that doesn't exist yet.
+ $this->assertFalse(groups_get_group_by_name($course->id, $name1));
+ $this->assertFalse(groups_get_group_by_name($course->id, $name2));
+
+ // We should now have a valid group returned by the name search.
+ $group1 = $generator->create_group(array('courseid' => $course->id, 'name' => $name1));
+ $this->assertEquals(groups_get_group_by_name($course->id, $name1), $group1->id);
+ $this->assertFalse(groups_get_group_by_name($course->id, $name2));
+
+ // We should now have a two valid groups returned by the name search.
+ $group2 = $generator->create_group(array('courseid' => $course->id, 'name' => $name2));
+ $this->assertEquals(groups_get_group_by_name($course->id, $name1), $group1->id);
+ $this->assertEquals(groups_get_group_by_name($course->id, $name2), $group2->id);
+
+ // Delete a group.
+ $this->assertTrue(groups_delete_group($group1));
+ $this->assertFalse(groups_get_group_by_name($course->id, $name1));
+ $this->assertEquals(groups_get_group_by_name($course->id, $name2), $group2->id);
+
+ /**
+ * Group idnumbers are unique within a course so test that we don't
+ * retrieve groups for the first course
+ */
+
+ // Create a second course
+ $course = $generator->create_course(array('category' => $cat->id));
+
+ // An empty name should always return a false value
+ $this->assertFalse(groups_get_group_by_name($course->id, ''));
+ $this->assertFalse(groups_get_group_by_name($course->id, null));
+
+ // Our existing names shouldn't be returned here as we're in a different course
+ $this->assertFalse(groups_get_group_by_name($course->id, $name1));
+ $this->assertFalse(groups_get_group_by_name($course->id, $name2));
+
+ // We should be able to reuse the idnumbers again since this is a different course
+ $group1 = $generator->create_group(array('courseid' => $course->id, 'name' => $name1));
+ $this->assertEquals(groups_get_group_by_name($course->id, $name1), $group1->id);
+
+ $group2 = $generator->create_group(array('courseid' => $course->id, 'name' => $name2));
+ $this->assertEquals(groups_get_group_by_name($course->id, $name2), $group2->id);
+ }
+
+ public function test_groups_get_grouping() {
+ $this->resetAfterTest(true);
+
+ $generator = $this->getDataGenerator();
+
+ // Create a course category and course
+ $cat = $generator->create_category(array('parent' => 0));
+ $course = $generator->create_course(array('category' => $cat->id));
+
+ $name1 = 'Grouping 1';
+ $name2 = 'Grouping 2';
+
+ // Test with an empty and a null idnumber
+ $this->assertFalse(groups_get_grouping_by_name($course->id, ''));
+ $this->assertFalse(groups_get_grouping_by_name($course->id, null));
+
+ // Even when a group exists.
+ $generator->create_group(array('courseid' => $course->id));
+ $this->assertFalse(groups_get_grouping_by_name($course->id, ''));
+ $this->assertFalse(groups_get_grouping_by_name($course->id, null));
+
+ // Test with a valid name, but one that doesn't exist yet.
+ $this->assertFalse(groups_get_grouping_by_name($course->id, $name1));
+ $this->assertFalse(groups_get_grouping_by_name($course->id, $name2));
+
+ // We should now have a valid group returned by the name search.
+ $group1 = $generator->create_grouping(array('courseid' => $course->id, 'name' => $name1));
+ $this->assertEquals(groups_get_grouping_by_name($course->id, $name1), $group1->id);
+ $this->assertFalse(groups_get_grouping_by_name($course->id, $name2));
+
+ // We should now have a two valid groups returned by the name search.
+ $group2 = $generator->create_grouping(array('courseid' => $course->id, 'name' => $name2));
+ $this->assertEquals(groups_get_grouping_by_name($course->id, $name1), $group1->id);
+ $this->assertEquals(groups_get_grouping_by_name($course->id, $name2), $group2->id);
+
+ // Delete a group.
+ $this->assertTrue(groups_delete_grouping($group1));
+ $this->assertFalse(groups_get_grouping_by_name($course->id, $name1));
+ $this->assertEquals(groups_get_grouping_by_name($course->id, $name2), $group2->id);
+
+ /**
+ * Group idnumbers are unique within a course so test that we don't
+ * retrieve groups for the first course
+ */
+
+ // Create a second course
+ $course = $generator->create_course(array('category' => $cat->id));
+
+ // An empty name should always return a false value
+ $this->assertFalse(groups_get_grouping_by_name($course->id, ''));
+ $this->assertFalse(groups_get_grouping_by_name($course->id, null));
+
+ // Our existing names shouldn't be returned here as we're in a different course
+ $this->assertFalse(groups_get_grouping_by_name($course->id, $name1));
+ $this->assertFalse(groups_get_grouping_by_name($course->id, $name2));
+
+ // We should be able to reuse the idnumbers again since this is a different course
+ $group1 = $generator->create_grouping(array('courseid' => $course->id, 'name' => $name1));
+ $this->assertEquals(groups_get_grouping_by_name($course->id, $name1), $group1->id);
+
+ $group2 = $generator->create_grouping(array('courseid' => $course->id, 'name' => $name2));
+ $this->assertEquals(groups_get_grouping_by_name($course->id, $name2), $group2->id);
+ }
+
+ public function test_groups_get_course_data() {
+ $this->resetAfterTest(true);
+
+ $generator = $this->getDataGenerator();
+
+ // Create a course category and course
+ $cat = $generator->create_category(array('parent' => 0));
+ $course = $generator->create_course(array('category' => $cat->id));
+ $grouping1 = $generator->create_grouping(array('courseid' => $course->id, 'name' => 'Grouping 1'));
+ $grouping2 = $generator->create_grouping(array('courseid' => $course->id, 'name' => 'Grouping 2'));
+ $group1 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 1'));
+ $group2 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 2'));
+ $group3 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 3'));
+ $group4 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 4'));
+
+ // Assign the groups to groupings.
+ $this->assertTrue(groups_assign_grouping($grouping1->id, $group1->id));
+ $this->assertTrue(groups_assign_grouping($grouping1->id, $group2->id));
+ $this->assertTrue(groups_assign_grouping($grouping2->id, $group3->id));
+ $this->assertTrue(groups_assign_grouping($grouping2->id, $group4->id));
+
+ // Get the data.
+ $data = groups_get_course_data($course->id);
+ $this->assertInstanceOf('stdClass', $data);
+ $this->assertObjectHasAttribute('groups', $data);
+ $this->assertObjectHasAttribute('groupings', $data);
+ $this->assertObjectHasAttribute('mappings', $data);
+
+ // Test we have the expected items returns.
+ $this->assertCount(4, $data->groups);
+ $this->assertCount(2, $data->groupings);
+ $this->assertCount(4, $data->mappings);
+
+ // Check we have the expected groups.
+ $this->assertContains($group1->id, array_keys($data->groups));
+ $this->assertContains($group2->id, array_keys($data->groups));
+ $this->assertContains($group3->id, array_keys($data->groups));
+ $this->assertContains($group4->id, array_keys($data->groups));
+
+ // Test a group-id is mapped correctly.
+ $this->assertEquals($group3->name, $data->groups[$group3->id]->name);
+
+ // Check we have the expected number of groupings.
+ $this->assertContains($grouping1->id, array_keys($data->groupings));
+ $this->assertContains($grouping2->id, array_keys($data->groupings));
+
+ // Test a grouping-id is mapped correctly.
+ $this->assertEquals($grouping2->name, $data->groupings[$grouping2->id]->name);
+
+ // Test that all of the mappings are correct.
+ $grouping1maps = 0;
+ $grouping2maps = 0;
+ $group1maps = 0;
+ $group2maps = 0;
+ $group3maps = 0;
+ $group4maps = 0;
+ foreach ($data->mappings as $mapping) {
+ if ($mapping->groupingid === $grouping1->id) {
+ $grouping1maps++;
+ $this->assertContains($mapping->groupid, array($group1->id, $group2->id));
+ } else if ($mapping->groupingid === $grouping2->id) {
+ $grouping2maps++;
+ $this->assertContains($mapping->groupid, array($group3->id, $group4->id));
+ } else {
+ $this->fail('Unexpected groupingid');
+ }
+ switch ($mapping->groupid) {
+ case $group1->id : $group1maps++; break;
+ case $group2->id : $group2maps++; break;
+ case $group3->id : $group3maps++; break;
+ case $group4->id : $group4maps++; break;
+ }
+ }
+ $this->assertEquals(2, $grouping1maps);
+ $this->assertEquals(2, $grouping2maps);
+ $this->assertEquals(1, $group1maps);
+ $this->assertEquals(1, $group2maps);
+ $this->assertEquals(1, $group3maps);
+ $this->assertEquals(1, $group4maps);
+
+ // Test the groups_get_all_groups which uses this functionality.
+ $groups = groups_get_all_groups($course->id);
+ $groupkeys = array_keys($groups);
+ $this->assertCount(4, $groups);
+ $this->assertContains($group1->id, $groupkeys);
+ $this->assertContains($group2->id, $groupkeys);
+ $this->assertContains($group3->id, $groupkeys);
+ $this->assertContains($group4->id, $groupkeys);
+
+ $groups = groups_get_all_groups($course->id, null, $grouping1->id);
+ $groupkeys = array_keys($groups);
+ $this->assertCount(2, $groups);
+ $this->assertContains($group1->id, $groupkeys);
+ $this->assertContains($group2->id, $groupkeys);
+ $this->assertNotContains($group3->id, $groupkeys);
+ $this->assertNotContains($group4->id, $groupkeys);
+
+ $groups = groups_get_all_groups($course->id, null, $grouping2->id);
+ $groupkeys = array_keys($groups);
+ $this->assertCount(2, $groups);
+ $this->assertNotContains($group1->id, $groupkeys);
+ $this->assertNotContains($group2->id, $groupkeys);
+ $this->assertContains($group3->id, $groupkeys);
+ $this->assertContains($group4->id, $groupkeys);
+ }
}

0 comments on commit e17dbee

Please sign in to comment.