Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add groupings synchronization #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ Any future amendments to groups in 'child' courses will be reflected in linked m

Author
------
Paul Holden ([email protected])
Paul Holden ([email protected]) Extended by François Lumineau

- Updates: https://moodle.org/plugins/view.php?plugin=local_metagroups
- Latest code: https://github.com/paulholden/moodle-local_metagroups

Changes
-------
Release 1.2 (build 2014042500)
- Autocreation of groupings when creating a group and asign the group in the grouping.
- Synchronization of groupings on meta courses

Release 1.1 (build 2014031300)
- Prevent synchronized group memberships being removed.

Expand Down
109 changes: 107 additions & 2 deletions classes/observers.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,86 @@
require_once($CFG->dirroot . '/local/metagroups/locallib.php');

class observers {
/**
* grouping created
*
* @param \core\event\grouping_created $event
* @return void
*/
public static function grouping_created(\core\event\grouping_created $event) {
global $DB;

$grouping = $event->get_record_snapshot('groupings', $event->objectid);

$courseids = local_metagroups_parent_courses($grouping->courseid);
foreach ($courseids as $courseid) {
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);

if (! $DB->record_exists('groupings', array('courseid' => $course->id, 'name' => $grouping->name))) {
$metagrouping = new \stdClass();
$metagrouping->courseid = $course->id;
$metagrouping->idnumber = $grouping->id;
$metagrouping->name = $grouping->name;

groups_create_grouping($metagrouping);
}
}
}

/**
* grouping deleted
*
* @param \core\event\grouping_deleted $event
* @return void
*/
public static function grouping_deleted(\core\event\grouping_deleted $event) {
global $DB;

$grouping = $event->get_record_snapshot('groupings', $event->objectid);

$courseids = local_metagroups_parent_courses($grouping->courseid);

foreach ($courseids as $courseid) {
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);

if ($metagrouping = $DB->get_record('groupings', array('courseid' => $course->id, 'name' => $grouping->name))) {
groups_delete_grouping($metagrouping);
}
}
}

/**
* Grouping updated
*
* @param \core\event\grouping_updated $event
* @return void
*/
public static function grouping_updated(\core\event\grouping_updated $event) {
global $DB;

$grouping = $event->get_record_snapshot('groupings', $event->objectid);

$courseids = local_metagroups_parent_courses($grouping->courseid);
foreach ($courseids as $courseid) {
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);

// Update if the grouping was created with autocreation method
if ($metagrouping = $DB->get_record('groupings', array('courseid' => $course->id, 'idnumber' => $grouping->id))) {
$metagrouping->name = $grouping->name;
groups_update_grouping($metagrouping);
}
// Update if the grouping was created with meta method
else {
if ($metagroup = $DB->get_record('groups', array('courseid' => $course->id, 'idnumber' => $grouping->id))) {
if ($metagrouping = $DB->get_record('groupings', array('courseid' => $course->id, 'idnumber' => $metagroup->id))) {
$metagrouping->name = $grouping->name;
groups_update_grouping($metagrouping);
}
}
}
}
}

/**
* Group created
*
Expand All @@ -38,7 +117,8 @@ public static function group_created(\core\event\group_created $event) {
global $DB;

$group = $event->get_record_snapshot('groups', $event->objectid);


//Réplication on parent courses
$courseids = local_metagroups_parent_courses($group->courseid);
foreach ($courseids as $courseid) {
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
Expand All @@ -52,6 +132,21 @@ public static function group_created(\core\event\group_created $event) {
groups_create_group($metagroup, false, false);
}
}

// Grouping creation if necessary

if (! $DB->record_exists('groupings', array('courseid' => $group->courseid, 'idnumber' => $group->id))) {

$autogrouping = new \stdClass();
$autogrouping->courseid = $group->courseid;
$autogrouping->idnumber = $group->id;
$autogrouping->name = $group->name;

groups_create_grouping($autogrouping);
}

//Assign group to grouping
groups_assign_grouping($autogrouping->id,$group->id);
}

/**
Expand All @@ -75,6 +170,12 @@ public static function group_updated(\core\event\group_updated $event) {
groups_update_group($metagroup, false, false);
}
}

if ($metagrouping = $DB->get_record('groupings', array('courseid' => $group->courseid, 'idnumber' => $group->id))) {
$metagrouping->name = $group->name;

groups_update_grouping($metagrouping);
}
}

/**
Expand All @@ -87,7 +188,7 @@ public static function group_deleted(\core\event\group_deleted $event) {
global $DB;

$group = $event->get_record_snapshot('groups', $event->objectid);

$courseids = local_metagroups_parent_courses($group->courseid);
foreach ($courseids as $courseid) {
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
Expand All @@ -96,6 +197,10 @@ public static function group_deleted(\core\event\group_deleted $event) {
groups_delete_group($metagroup);
}
}

if ($metagrouping = $DB->get_record('groupings', array('courseid' => $group->courseid, 'idnumber' => $group->id))) {
groups_delete_grouping($metagrouping);
}
}

/**
Expand Down
15 changes: 15 additions & 0 deletions db/events.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@
defined('MOODLE_INTERNAL') || die();

$observers = array(
array(
'eventname' => '\core\event\grouping_created',
'callback' => '\local_metagroups\observers::grouping_created',
),

array(
'eventname' => '\core\event\grouping_updated',
'callback' => '\local_metagroups\observers::grouping_updated',
),

array(
'eventname' => '\core\event\grouping_deleted',
'callback' => '\local_metagroups\observers::grouping_deleted',
),

array(
'eventname' => '\core\event\group_created',
'callback' => '\local_metagroups\observers::group_created',
Expand Down
84 changes: 76 additions & 8 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,42 @@ function local_metagroups_child_courses($courseid) {
return $DB->get_records_menu('enrol', array('enrol' => 'meta', 'courseid' => $courseid, 'status' => ENROL_INSTANCE_ENABLED), 'sortorder', 'id, customint1');
}

/**
* Returns the groups in the specified grouping.
*
* @param int $groupingid The groupingid to get the groups for
* @param string $fields The fields to return
* @param string $sort optional sorting of returned users
* @return array|bool Returns an array of the groups for the specified
* group or false if no groups or an error returned.
*/
function local_metagroups_get_grouping_groups($groupingid, $fields='g.*', $sort='name ASC') {
global $DB;

return $DB->get_records_sql("SELECT $fields
FROM {groups} g, {groupings_groups} gg
WHERE g.id = gg.groupid AND gg.groupingid = ?
ORDER BY $sort", array($groupingid));
}

/**
* Returns the group id of a group in a parent course matching the child course.
*
* @param int $groupid The groupid to get the group for
* @param int $courseid The course "parent" id
* @return array|bool Returns an array of the id for the specified
* course or false if no group or an error returned.
*/
function local_metagroups_group_match($groupid, $courseid) {
global $DB;

return $DB->get_records_sql("SELECT g.id
FROM {groups} g
WHERE g.idnumber = $groupid AND g.courseid=$courseid");
}



/**
* Run synchronization process
*
Expand All @@ -62,18 +98,18 @@ function local_metagroups_child_courses($courseid) {
function local_metagroups_sync(progress_trace $trace) {
global $DB;

$courseids = local_metagroups_parent_courses();
foreach (array_unique($courseids) as $courseid) {
$courseids = local_metagroups_parent_courses(); // Get of all parent courses in the platform
foreach (array_unique($courseids) as $courseid) { // browse parent courses ID
$parent = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
$trace->output($parent->fullname, 1);

$children = local_metagroups_child_courses($parent->id);
foreach ($children as $childid) {
$children = local_metagroups_child_courses($parent->id); // Get of all child courses ID for current parretn course
foreach ($children as $childid) { // browse child courses ID
$child = $DB->get_record('course', array('id' => $childid), '*', MUST_EXIST);
$trace->output($child->fullname, 2);

$groups = groups_get_all_groups($child->id);
foreach ($groups as $group) {
$childgroups = groups_get_all_groups($child->id); // Get child course groups
foreach ($childgroups as $group) { // Browse child group course and create in parent course if necessary
if (! $metagroup = $DB->get_record('groups', array('courseid' => $parent->id, 'idnumber' => $group->id))) {
$metagroup = new stdClass();
$metagroup->courseid = $parent->id;
Expand All @@ -85,11 +121,43 @@ function local_metagroups_sync(progress_trace $trace) {

$trace->output($metagroup->name, 3);

$users = groups_get_members($group->id);
foreach ($users as $user) {
$users = groups_get_members($group->id); // Get members from child course group
foreach ($users as $user) { // Browse for adding in parent course group
groups_add_member($metagroup, $user->id, 'local_metagroups', $group->id);
}
}

$parentgroups = groups_get_all_groups($parent->id); // Get members from parent course groups
$groupings = groups_get_all_groupings($child->id); // Get groupings from child course

foreach ($groupings as $grouping) { // Browse child course groupings and create in parent course if necessary
if (! $metagrouping = $DB->get_record('groupings', array('courseid' => $parent->id, 'name' => $grouping->name))) {
$metagrouping = new stdClass();
$metagrouping->courseid = $parent->id;
$metagrouping->idnumber = $grouping->id;
$metagrouping->name = $grouping->name;
$metagrouping->id = groups_create_grouping($metagrouping);
}

$trace->output($metagrouping->name, 3);

$groupinggroupsChild = local_metagroups_get_grouping_groups($grouping->id); // Get groups of current grouping (child course)
$groupinggroupsParent = local_metagroups_get_grouping_groups($metagrouping->id); // Get groups of current grouping (parent course)

foreach ($groupinggroupsChild as $groupinggroup) {
$targetgroupsid=local_metagroups_group_match($groupinggroup->id,$parent->id); // Should be one group
foreach ($targetgroupsid as $targetgroupid) {
groups_assign_grouping($metagrouping->id, $targetgroupid->id);
unset($groupinggroupsParent[$targetgroupid->id]); // Unset from group for unassign afterward
}
}

foreach ($groupinggroupsParent as $groupinggroup) { // Unassign in parent course
groups_unassign_grouping($metagrouping->id, $groupinggroup->id);
}

}

}
}
}
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
defined('MOODLE_INTERNAL') || die();

$plugin->component = 'local_metagroups';
$plugin->release = '1.1';
$plugin->version = 2014031300;
$plugin->release = '1.2';
$plugin->version = 2014042500;
$plugin->requires = 2013111800; // Moodle 2.6 onwards.
$plugin->maturity = MATURITY_STABLE;

Expand Down