/** * Update group and membership info in underlying 'course-' group (group type 4) * * Method is called anytime after a course is saved * * @param $course - course object */ public function onAfterStoreCourse($course) { // Get a new group object $group = new \Hubzero\User\Group(); // If the course doesn't have a group id set, then we need to create a new group if (!$course->get('group_id')) { // Set some group info $group->set('cn', 'course-' . $course->cn); $group->create(); $group->set('type', 4); // group type 4 = course // Set the new group gidNumber as the group_id in the course and update $course->set('group_id', $group->get('gidNumber')); $course->update(); } else { $group->read($course->get('group_id')); } // Set the group description (in case it's been changed) $group->set('description', $course->get('description')); // Get all of the course members that are not yet group members (i.e. they need to be added to the group) $add = array_diff($course->get('members'), $group->get('members')); foreach ($add as $a) { $group->add('members', $a); } // Get all of the group members that are not members of the course (i.e. they need to be removed from the group) $remove = array_diff($group->get('members'), $course->get('members')); foreach ($remove as $r) { $group->remove('members', $r); } // Finally, update the group $group->update(); }