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

New Setting: Preserve manual User assignments. #10

Merged
1 change: 1 addition & 0 deletions classes/domain/autogroup_set.php
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
10 changes: 10 additions & 0 deletions classes/domain/group.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ public function ensure_user_is_member($userid){
* @param int $userid
*/
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);
Expand Down
36 changes: 28 additions & 8 deletions classes/event_handler.php
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
11 changes: 11 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "lambdasolutions/moodle-local_autogroup",
"description": "Autogroup (Local Plugin)",
"type": "moodle-local",
"extra": {
"installer-name": "autogroup"
},
"require": {
"composer/installers": "~1.0"
}
}
10 changes: 10 additions & 0 deletions db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,15 @@
<KEY NAME="roleid" TYPE="foreign" FIELDS="roleid" REFTABLE="role" REFFIELDS="id"/>
</KEYS>
</TABLE>
<TABLE NAME="local_autogroup_manual" COMMENT="Tracks manually assigned Users to autogroup groups">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="groupid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="Group ID of this record."/>
<FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="User ID of this record." />
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
</TABLE>
</TABLES>
</XMLDB>
24 changes: 23 additions & 1 deletion db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,27 @@ function xmldb_local_autogroup_upgrade($oldversion) {
// savepoint reached.
upgrade_plugin_savepoint(true, 2016062201, 'local', 'autogroup');
}

if ($oldversion < 2018102300) {
// Define table local_autogroup_manual to be created.
$table = new xmldb_table('local_autogroup_manual');

// Adding fields to table local_autogroup_manual.
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('groupid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);

// Adding keys to table local_autogroup_manual.
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));

// Conditionally launch create table for local_autogroup_manual.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}

// Autogroup savepoint reached.
upgrade_plugin_savepoint(true, 2018102300, 'local', 'autogroup');
}

return true;
}
}
1 change: 1 addition & 0 deletions lang/en/local_autogroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
// Admin Settings
$string['addtonewcourses'] = 'Add to new courses';
$string['addtorestoredcourses'] = 'Add to restored courses';
$string['preservemanual'] = 'Manually added Users are not removed when autogroups are updated.';
$string['defaults'] = 'Default Settings';
$string['defaultroles'] = 'Default Eligible Roles';
$string['enabled'] = 'Enabled';
Expand Down
8 changes: 8 additions & 0 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@
false
)
);
$settings->add(
new admin_setting_configcheckbox(
'local_autogroup/preservemanual',
get_string('preservemanual', 'local_autogroup'),
'',
1
)
);

// default settings
$settings->add(
Expand Down
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@

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

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