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

Autogroup Bug #22

Open
wants to merge 34 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
1237b7c
issue #2: implements custom profile fields support
ak4t0sh Jan 19, 2018
e9bc84b
Merge pull request #1 from andil-elearning/issue-2
emmarichardson Feb 7, 2018
0690077
Update README.md
emmarichardson Feb 7, 2018
47d3db7
Update version.php
emmarichardson Feb 7, 2018
1e86a5e
Update version.php
emmarichardson Feb 7, 2018
86486dc
Refactor repository structure
ak4t0sh Feb 16, 2018
e134b43
Merge pull request #3 from andil-elearning/repo_refactoring
emmarichardson Mar 15, 2018
5bdb014
use standard string manager to display sort module name
ak4t0sh Feb 16, 2018
250e86f
Merge pull request #4 from andil-elearning/issue-2
emmarichardson May 22, 2018
8e87d0f
Fix memory issue on user_update event
luukverhoeven May 22, 2018
2cd8fa3
Merge pull request #6 from luukverhoeven/patch-1
emmarichardson Jul 3, 2018
4959a0b
Update version.php
emmarichardson Jul 3, 2018
6419c46
move changelogs to dedicated file
ak4t0sh Aug 5, 2018
176ae1f
Added city field to user profile sort module
andrewhancox Aug 10, 2018
4180ec9
Merge pull request #8 from andrewhancox/master
emmarichardson Aug 22, 2018
e674152
Merge pull request #7 from andil-elearning/fix_2.4_readme
emmarichardson Aug 22, 2018
191b62c
New language for setting.
gnat Oct 24, 2018
193f5be
Added new setting for manual user assignment preservation.
gnat Oct 24, 2018
4885d5f
New database table to remember if we manually assigned this user to t…
gnat Oct 24, 2018
2c2819b
Event handling logic.
gnat Oct 24, 2018
dc1af5b
New version.
gnat Oct 24, 2018
fad7703
Composer file.
gnat Oct 25, 2018
988f28c
Removed extra comments.
gnat Nov 28, 2018
e59cd75
Simplified Preserve Manual english string.
gnat Nov 28, 2018
62753ac
Simplified Preserve Manual english string.
gnat Nov 28, 2018
be83490
Revert version.php change.
gnat Nov 29, 2018
6153236
Removed composer.json
gnat Nov 29, 2018
4993414
Merge pull request #10 from lambda-solutions/preserve-manual-assignment
emmarichardson Jan 31, 2019
1ff7252
issue #11: added role_deleted event observer
ak4t0sh Jan 3, 2019
7577099
Merge pull request #12 from ak4t0sh/issue-11-fix
emmarichardson Jan 31, 2019
1c8688f
Update version.php
emmarichardson Jan 31, 2019
638f141
add missing variable
ak4t0sh Feb 1, 2019
0b29541
Autogroup Bug
sergei-porfenovich Aug 2, 2019
8eee232
Merge branch 'master' into dev
sergei-porfenovich Aug 9, 2019
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
28 changes: 28 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
## v2.4
* Fixed unnecessary processing on profile update. Thanks to Luuk Verhoeven for this addition.

## v2.3
* Added support for custom profile fields. Thanks to Arnaud Trouvé for this addition.

## v2.2
* Performance enhancement: Event listeners now check to see whether triggered by AutoGroup

## v2.1
* Switched to individual toggles for event listeners
* Minor change to settings structure for improved usability
* Fixed compatibility with Moodle 3.1 and onwards

## v2.0
* Added support for defining multiple grouping sets on one course
* Sort modules are now fully modular
* Added new sort module for Totara positional assignments

## v1.1
* Fixed a bug which would have resulted in roles being removed from all groupsets across a site
* Changed default permissions for plugin files

## v1.01
* Added event handler for course_restored and related setting

## v1.0
* Stable release. Tested for compatibility with Moodle 2.7 and 2.8
33 changes: 8 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# AutoGroup Local Plugin

Automatically assigns enrolled users on a course into groups
dependant upon information within their user profile.
dependant upon information within their user profile.

This plugin will create, update, and delete groups automatically
to match the users on your course. All behaviour is event-driven
Expand All @@ -10,33 +10,14 @@ and so will occur within page loads.
The system can also monitor manual group setting changes and
moderate them to ensure that groups are kept neat and tidy.

## Changelog
* v2.2
* Performance enhancement: Event listeners now check to see whether triggered by AutoGroup
* v2.1
* Switched to individual toggles for event listeners
* Minor change to settings structure for improved usability
* Fixed compatibility with Moodle 3.1 and onwards
* v2.0
* Added support for defining multiple grouping sets on one course
* Sort modules are now fully modular
* Added new sort module for Totara positional assignments
* v1.1
* Fixed a bug which would have resulted in roles being removed from all groupsets across a site
* Changed default permissions for plugin files
* v1.01
* Added event handler for course_restored and related setting
* v1.0
* Stable release. Tested for compatibility with Moodle 2.7 and 2.8

## Install

1. Copy the plugin directory "autogroup" into moodle\local\.
2. Check admin notifications to install.
Install the plugin, like any other plugin.
See http://docs.moodle.org/en/Installing_plugins#Installing_a_plugin for details on installing Moodle plugins.

## Maintainer

The module was authored and is being maintained by Mark Ward ([email protected]).
The module was authored by Mark Ward ([email protected]). It is now being maintained by Emma Richardson.

## Thanks to

Expand All @@ -45,11 +26,13 @@ Development for v1.0 of the plugin was funded by East Central BOCES (http://www.
Many ideas and suggestions for the functionality of the plugin were contributed
by Emma Richardson at East Central BOCES.

Thanks to Arnaud Trouvé for the addition of custom profile field support.

## Technical Support

Issue tracker can be found on [GitHub](https://github.com/markward/local_autogroup/issues). Please
Issue tracker can be found on [GitHub](https://github.com/emmarichardson/local_autogroup/issues). Please
try to give as much detail about your problem as possible and I'll do what I can to help.

## License

Released Under the GNU General Public Licence http://www.gnu.org/copyleft/gpl.html
Released Under the GNU General Public Licence http://www.gnu.org/copyleft/gpl.html
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public function delete(\moodle_database $db, $cleanupgroups = true)
//this has to be done first to prevent event handler getting in the way
$db->delete_records('local_autogroup_set', array('id'=>$this->id));
$db->delete_records('local_autogroup_roles', array('setid'=>$this->id));
$db->delete_records('local_autogroup_manual', array('groupid'=>$this->id));

if($cleanupgroups){
foreach($this->groups as $k => $group){
Expand Down Expand Up @@ -295,18 +296,27 @@ public function verify_user_group_membership(\stdclass $user, \moodle_database $

//an array of groupids which will be populated as we ensure membership
$validgroups = array();
$new_group = false;

foreach ($eligiblegroups as $eligiblegroup){
if($group = $this->get_or_create_group_by_idnumber($eligiblegroup, $db)) {
list($group, $group_created) = $this->get_or_create_group_by_idnumber($eligiblegroup, $db);
if($group) {
$validgroups[] = $group->id;
$group->ensure_user_is_member($user->id);
if ($group->courseid == $this->courseid){
if (!$new_group or $group_created){
$new_group = $group->id;
}
}
}
}

//now run through other groups and ensure user is not a member
foreach($this->groups as $key => $group){
if(!in_array($group->id,$validgroups)){
$group->ensure_user_is_not_member($user->id);
if($group->ensure_user_is_not_member($user->id) and $new_group){
$this->check_forums($user->id, $group->id, $new_group, $db);
}
}
}

Expand Down Expand Up @@ -358,7 +368,7 @@ private function get_autogroups(\moodle_database $db)
/**
* @param string $groupname
* @param \moodle_database $db
* @return bool|domain/group
* @return [bool|domain/group, bool]
*/
private function get_or_create_group_by_idnumber($group, \moodle_database $db)
{
Expand All @@ -372,7 +382,6 @@ private function get_or_create_group_by_idnumber($group, \moodle_database $db)
}

$idnumber = $this->generate_group_idnumber($groupidnumber);
$result = null;

//firstly run through existing groups and check for matches
foreach($this->groups as $group){
Expand All @@ -383,7 +392,7 @@ private function get_or_create_group_by_idnumber($group, \moodle_database $db)
$group->update();
}

return $group;
return [$group, false];
}
}

Expand All @@ -404,10 +413,10 @@ private function get_or_create_group_by_idnumber($group, \moodle_database $db)
$newgroup->create();
$this->groups[$newgroup->id] = $newgroup;
} catch (exception\invalid_group_argument $e){
return false;
return [false, false];
}

return $this->groups[$newgroup->id];
return [$this->groups[$newgroup->id], true];
}

/**
Expand Down Expand Up @@ -602,6 +611,17 @@ private function validate_object($autogroupset)
&& $autogroupset->courseid > 0;
}

/**
* @param int $user_id
* @param int $old_group_id
* @param int $new_group_id
* @param \moodle_database $db
*/
private function check_forums($user_id, $old_group_id, $new_group_id, \moodle_database $db){
$conditions = ['course' => $this->courseid, 'userid' => $user_id, 'groupid' => $old_group_id];
$db->set_field('forum_discussions', 'groupid', $new_group_id, $conditions);
}

/**
* An array of DB level attributes for an autogroup set
* used for handling stdclass object conversion.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ public function __construct ($course, \moodle_database $db)

//load autogroup groups for this course
$this->get_autogroups($db);

$this->enrolledusers = \get_enrolled_users($this->context);

}

/**
Expand All @@ -84,7 +81,8 @@ public function get_membership_counts(){
public function verify_all_group_membership(\moodle_database $db)
{
$result = true;
foreach ($this->enrolledusers as $user){
$enrolledusers = \get_enrolled_users($this->context);
foreach ($enrolledusers as $user){
$result &= $this->verify_user_group_membership($user, $db);
}
return $result;
Expand Down Expand Up @@ -150,9 +148,4 @@ private function get_autogroups(\moodle_database $db){
*/
private $context;

/**
* @var array
*/
private $enrolledusers = array();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,28 +73,42 @@ public function __construct ($group, \moodle_database $db)

/**
* @param int $userid
* @return bool
*/
public function ensure_user_is_member($userid){
foreach($this->members as $member){
if ($member == $userid) {
return;
return false;
}
}

//user was not found as a member so will now make member a user
\groups_add_member($this->as_object(), $userid, 'local_autogroup');
return;
return true;
}

/**
* @param int $userid
* @return bool
*/
public function ensure_user_is_not_member($userid){

// Do not allow autogroup to remove this User if they were manually assigned to group.
$pluginconfig = get_config('local_autogroup');
if($pluginconfig->preservemanual) {
global $DB;
if($DB->record_exists('local_autogroup_manual', array('userid' => $userid, 'groupid' => $this->id))) {
return;
}
}

foreach($this->members as $member){
if ($member == $userid) {
\groups_remove_member($this->as_object(), $userid);
return true;
}
}
return false;
}

/**
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,22 @@ public static function group_member_added(event\group_member_added $event) {
return false;
}

global $DB;
$pluginconfig = get_config('local_autogroup');

// Add to manually assigned list (local_autogroup_manual).
$userid = (int) $event->relateduserid;
$groupid = (int) $event->objectid;

if(!$DB->record_exists('local_autogroup_manual', array('userid' => $userid, 'groupid' => $groupid))) {
$record = (object) array('userid' => $userid, 'groupid' => $groupid);
$DB->insert_record('local_autogroup_manual', $record);
}

if(!$pluginconfig->listenforgroupmembership){
return false;
}

global $DB;

$courseid = (int) $event->courseid;
$userid = (int) $event->relateduserid;

Expand All @@ -101,10 +110,16 @@ public static function group_member_removed(event\group_member_removed $event) {
return false;
}

global $DB, $PAGE;
$pluginconfig = get_config('local_autogroup');

// Remove from manually assigned list (local_autogroup_manual).
$userid = (int) $event->relateduserid;
$groupid = (int) $event->objectid;

global $DB, $PAGE;
if($DB->record_exists('local_autogroup_manual', array('userid' => $userid, 'groupid' => $groupid))) {
$DB->delete_records('local_autogroup_manual', array('userid' => $userid, 'groupid' => $groupid));
}

$groupid = (int) $event->objectid;
$courseid = (int) $event->courseid;
Expand Down Expand Up @@ -174,16 +189,21 @@ public static function group_change(event\base $event) {
return false;
}

$pluginconfig = get_config('local_autogroup');
if(!$pluginconfig->listenforgroupchanges){
return false;
}

global $DB, $PAGE;

$courseid = (int) $event->courseid;
$groupid = (int) $event->objectid;

// Remove from manually assigned list (local_autogroup_manual).
if ($event->eventname === '\core\event\group_deleted') {
$DB->delete_records('local_autogroup_manual', array('groupid' => $groupid));
}

$pluginconfig = get_config('local_autogroup');
if(!$pluginconfig->listenforgroupchanges){
return false;
}

if($DB->record_exists('groups', array('id'=>$groupid))) {
$verifygroupidnumber = new usecase\verify_group_idnumber($groupid, $DB, $PAGE);
$verifygroupidnumber();
Expand Down Expand Up @@ -211,6 +231,19 @@ public static function role_change(event\base $event) {
return $usecase();
}

/**
* @param event\role_deleted $event
* @return mixed
*/
public static function role_deleted(event\role_deleted $event) {
global $DB;

$DB->delete_records('local_autogroup_roles', ['roleid' => $event->objectid]);
unset_config('eligiblerole_'.$event->objectid, 'local_autogroup');

return true;
}

/**
* @param event\course_created $event
* @return mixed
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public function get_config_options(){
'department' => get_string('department', 'local_autogroup'),
'institution' => get_string('institution', 'local_autogroup'),
'lang' => get_string('lang', 'local_autogroup'),
'city' => get_string('city', 'local_autogroup'),
);
return $options;
}
Expand Down
Loading