Skip to content

Commit 257c57d

Browse files
authored
Merge pull request #1 from lambda-solutions/preserve-manual-assignment
New Setting: Preserve manual User assignments.
2 parents e674152 + fad7703 commit 257c57d

9 files changed

+97
-11
lines changed

classes/domain/autogroup_set.php

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public function delete(\moodle_database $db, $cleanupgroups = true)
100100
//this has to be done first to prevent event handler getting in the way
101101
$db->delete_records('local_autogroup_set', array('id'=>$this->id));
102102
$db->delete_records('local_autogroup_roles', array('setid'=>$this->id));
103+
$db->delete_records('local_autogroup_manual', array('groupid'=>$this->id));
103104

104105
if($cleanupgroups){
105106
foreach($this->groups as $k => $group){

classes/domain/group.php

+10
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ public function ensure_user_is_member($userid){
9090
* @param int $userid
9191
*/
9292
public function ensure_user_is_not_member($userid){
93+
94+
// Do not allow autogroup to remove this User if they were manually assigned to group.
95+
$pluginconfig = get_config('local_autogroup');
96+
if($pluginconfig->preservemanual) {
97+
global $DB;
98+
if($DB->record_exists('local_autogroup_manual', array('userid' => $userid, 'groupid' => $this->id))) {
99+
return;
100+
}
101+
}
102+
93103
foreach($this->members as $member){
94104
if ($member == $userid) {
95105
\groups_remove_member($this->as_object(), $userid);

classes/event_handler.php

+31-8
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,23 @@ public static function group_member_added(event\group_member_added $event) {
7676
return false;
7777
}
7878

79+
global $DB;
7980
$pluginconfig = get_config('local_autogroup');
81+
82+
// Add to manually assigned list (local_autogroup_manual).
83+
$userid = (int) $event->relateduserid;
84+
$groupid = (int) $event->objectid;
85+
86+
if(!$DB->record_exists('local_autogroup_manual', array('userid' => $userid, 'groupid' => $groupid))) {
87+
$record = (object) array('userid' => $userid, 'groupid' => $groupid);
88+
$DB->insert_record('local_autogroup_manual', $record);
89+
}
90+
// End of add to manually assigned list (local_autogroup_manual)
91+
8092
if(!$pluginconfig->listenforgroupmembership){
8193
return false;
8294
}
8395

84-
global $DB;
85-
8696
$courseid = (int) $event->courseid;
8797
$userid = (int) $event->relateduserid;
8898

@@ -101,10 +111,17 @@ public static function group_member_removed(event\group_member_removed $event) {
101111
return false;
102112
}
103113

114+
global $DB, $PAGE;
104115
$pluginconfig = get_config('local_autogroup');
105116

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

107-
global $DB, $PAGE;
121+
if($DB->record_exists('local_autogroup_manual', array('userid' => $userid, 'groupid' => $groupid))) {
122+
$DB->delete_records('local_autogroup_manual', array('userid' => $userid, 'groupid' => $groupid));
123+
}
124+
// End of remove from manually assigned list (local_autogroup_manual)
108125

109126
$groupid = (int) $event->objectid;
110127
$courseid = (int) $event->courseid;
@@ -174,16 +191,22 @@ public static function group_change(event\base $event) {
174191
return false;
175192
}
176193

177-
$pluginconfig = get_config('local_autogroup');
178-
if(!$pluginconfig->listenforgroupchanges){
179-
return false;
180-
}
181-
182194
global $DB, $PAGE;
183195

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

199+
// Remove from manually assigned list (local_autogroup_manual).
200+
if ($event->eventname === '\core\event\group_deleted') {
201+
$DB->delete_records('local_autogroup_manual', array('groupid' => $groupid));
202+
}
203+
// End of remove from manually assigned list (local_autogroup_manual)
204+
205+
$pluginconfig = get_config('local_autogroup');
206+
if(!$pluginconfig->listenforgroupchanges){
207+
return false;
208+
}
209+
187210
if($DB->record_exists('groups', array('id'=>$groupid))) {
188211
$verifygroupidnumber = new usecase\verify_group_idnumber($groupid, $DB, $PAGE);
189212
$verifygroupidnumber();

composer.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "lambdasolutions/moodle-local_autogroup",
3+
"description": "Autogroup (Local Plugin)",
4+
"type": "moodle-local",
5+
"extra": {
6+
"installer-name": "autogroup"
7+
},
8+
"require": {
9+
"composer/installers": "~1.0"
10+
}
11+
}

db/install.xml

+10
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,15 @@
2929
<KEY NAME="roleid" TYPE="foreign" FIELDS="roleid" REFTABLE="role" REFFIELDS="id"/>
3030
</KEYS>
3131
</TABLE>
32+
<TABLE NAME="local_autogroup_manual" COMMENT="Tracks manually assigned Users to autogroup groups">
33+
<FIELDS>
34+
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
35+
<FIELD NAME="groupid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="Group ID of this record."/>
36+
<FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="User ID of this record." />
37+
</FIELDS>
38+
<KEYS>
39+
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
40+
</KEYS>
41+
</TABLE>
3242
</TABLES>
3343
</XMLDB>

db/upgrade.php

+23-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,27 @@ function xmldb_local_autogroup_upgrade($oldversion) {
1919
// savepoint reached.
2020
upgrade_plugin_savepoint(true, 2016062201, 'local', 'autogroup');
2121
}
22+
23+
if ($oldversion < 2018102300) {
24+
// Define table local_autogroup_manual to be created.
25+
$table = new xmldb_table('local_autogroup_manual');
26+
27+
// Adding fields to table local_autogroup_manual.
28+
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
29+
$table->add_field('groupid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
30+
$table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
31+
32+
// Adding keys to table local_autogroup_manual.
33+
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
34+
35+
// Conditionally launch create table for local_autogroup_manual.
36+
if (!$dbman->table_exists($table)) {
37+
$dbman->create_table($table);
38+
}
39+
40+
// Autogroup savepoint reached.
41+
upgrade_plugin_savepoint(true, 2018102300, 'local', 'autogroup');
42+
}
43+
2244
return true;
23-
}
45+
}

lang/en/local_autogroup.php

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
// Admin Settings
5656
$string['addtonewcourses'] = 'Add to new courses';
5757
$string['addtorestoredcourses'] = 'Add to restored courses';
58+
$string['preservemanual'] = 'Manually added Users to autogroups are not removed when groups are automatically updated.';
5859
$string['defaults'] = 'Default Settings';
5960
$string['defaultroles'] = 'Default Eligible Roles';
6061
$string['enabled'] = 'Enabled';

settings.php

+8
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@
7575
false
7676
)
7777
);
78+
$settings->add(
79+
new admin_setting_configcheckbox(
80+
'local_autogroup/preservemanual',
81+
get_string('preservemanual', 'local_autogroup'),
82+
'',
83+
1
84+
)
85+
);
7886

7987
// default settings
8088
$settings->add(

version.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030

3131
defined('MOODLE_INTERNAL') || die();
3232

33-
$plugin->version = 2018070300;
33+
$plugin->version = 2018102400;
3434
$plugin->requires = 2013111800.00; // Requires this Moodle version (2.7).
35-
$plugin->release = '2.4'; // Plugin release.
35+
$plugin->release = '2.4.1'; // Plugin release.
3636
$plugin->component = 'local_autogroup'; // Full name of the plugin (used for diagnostics).
3737
$plugin->maturity = MATURITY_STABLE;

0 commit comments

Comments
 (0)