Skip to content

Commit

Permalink
Add group invitations
Browse files Browse the repository at this point in the history
  • Loading branch information
Remi Graton authored and Remi Graton committed Sep 26, 2024
1 parent 1ef7ffd commit 727045a
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 2 deletions.
17 changes: 17 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,5 +298,22 @@ function xmldb_enrol_invitation_upgrade($oldversion) {
upgrade_plugin_savepoint(true, 2021073000, 'enrol', 'invitation');
}

if ($oldversion < 2024092600) {
$table = new xmldb_table('enrol_invitation');

// Add status column.
$fields[] = new xmldb_field('groupsid',XMLDB_TYPE_TEXT, 'small');

foreach ($fields as $field) {
// Conditionally launch add field subject.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
}

upgrade_plugin_savepoint(true, 2024092600, 'enrol', 'invitation');
}


return true;
}
36 changes: 36 additions & 0 deletions invitation_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
require_once('locallib.php');
require_once($CFG->dirroot . '/lib/formslib.php');
require_once($CFG->dirroot . '/lib/enrollib.php');
require_once($CFG->dirroot . '/group/lib.php');


/**
* Class for sending invitation to enrol users in a course when the "Use invitation with default values" field is set to "Yes".
Expand Down Expand Up @@ -80,6 +82,21 @@ public function definition() {
$mform->addGroup($rolegroup, 'role_group', $label, '<br>');
$mform->addRule('role_group', get_string('norole', 'enrol_invitation'), 'required');


$groups = $this->getappropiategroups($course, $USER);

if (!empty($groups)) {
$mform->addElement('header', 'header_group', get_string('header_group', 'enrol_invitation'));
$label = get_string('assigngroup', 'enrol_invitation');

$groupselem = [];

foreach( $groups as $group) {
$groupselem[] = &$mform->createElement('checkbox', $group->id, $group->name);
}
$mform->addGroup($groupselem, 'groups', $label, '<br>');
}

// Email address field.
$mform->addElement('header', 'header_email', get_string('header_email', 'enrol_invitation'));
$mform->addElement(
Expand Down Expand Up @@ -256,6 +273,25 @@ private function getappropiateroles($course) {
return $retval;
}

/**
* Private class method to return a list of appropriate roles for given course and user.
*
* @param object $course Course record.
* @param object $user User
* @return array Returns array of roles indexed by role archetype.
*/
private function getappropiategroups($course, $user) {
$context = context_course::instance($course->id, $user->id);
if (has_capability('moodle/course:managegroups', $context)) {
$groups = groups_get_all_groups($course->id);
} else {
$groups = groups_get_all_groups($course->id, $user->id);
}
return $groups;
}


/**
* Provides custom validation rules.
* - Validating the email field here, rather than in definition, to allow multiple email addresses to be specified.
Expand Down
2 changes: 2 additions & 0 deletions lang/en/enrol_invitation.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
$string['action_revoke_invite'] = 'Revoke invite';
$string['anonymoususer'] = '(unknown)';
$string['assignrole'] = 'Assign role';
$string['assigngroup'] = 'Assign groups';
$string['customnamecourse'] = 'Custom format';
$string['customsubjectformat'] = '{$a->shortname} - {$a->fullname}';
$string['default_subject'] = 'Course invitation: {$a}';
Expand Down Expand Up @@ -91,6 +92,7 @@
$string['half_minute'] = 'half a minute';
$string['header_email'] = 'Who do you want to invite?';
$string['header_role'] = 'What role do you want to assign to the invitee?';
$string['header_group'] = 'What group do you want to assign to the invitee?';
$string['historyactions'] = 'Actions';
$string['historydateexpiration'] = 'Expiration date';
$string['historydatesent'] = 'Date sent';
Expand Down
29 changes: 29 additions & 0 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
* @copyright 2013 UC Regents
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->dirroot . '/group/lib.php');
require_once($CFG->dirroot . '/lib/enrollib.php');


class invitation_manager {
/**
* Course id.
Expand Down Expand Up @@ -136,6 +140,11 @@ public function send_invitations($data, $resend = false) {
$invitation->token = $token;
$invitation->tokenused = false;
$invitation->roleid = $resend ? $data->roleid : $data->role_group['roleid'];

$groups = filterGroups($data->courseid, $USER, $data->groups);
if (!is_null($groups) && !empty($groups)) {
$invitation->groupsid = json_encode($groups);
}
$invitation->status = null;

// Set the timesent/timeexpiration date for the invitation.
Expand Down Expand Up @@ -459,6 +468,13 @@ public function enroluser($invitation) {

$enrol = enrol_get_plugin('invitation');
$enrol->enrol_user($this->enrolinstance, $user->id, $invitation->roleid, 0, $timeend);

$groupInvitations = json_decode($invitation->groupsid);

foreach ($groupInvitations as $key => $groupInvitation) {
groups_add_member($key, $user->id);
}

}

/**
Expand Down Expand Up @@ -1029,3 +1045,16 @@ function print_page_tabs($activetab) {
// Display tabs here.
print_tabs([$tabs], $activetab);
}

function filterGroups($courseid, $user, $groups) {
$context = context_course::instance($courseid);
if (is_null($groups) || empty($groups) || has_capability('moodle/course:managegroups', $context)) {
return $groups;
} else {
$user_groups = groups_get_user_groups($courseid, $user->id);
$filtered_group = array_filter($groups, function($groupid) use ($user_groups) {
return in_array($groupid, $user_groups[0]);
});
return $filtered_group;
}
}
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@

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

$plugin->version = 2024042800;
$plugin->version = 2024092600;
$plugin->requires = 2013111800; // Moodle 3.9.
$plugin->release = '2.2.1';
$plugin->release = '2.2.2';
$plugin->component = "enrol_invitation";
$plugin->maturity = MATURITY_STABLE;

0 comments on commit 727045a

Please sign in to comment.