Skip to content

Commit

Permalink
Merge pull request #111 from catalyst/profile-cohort-update
Browse files Browse the repository at this point in the history
Update profile field and cohort in enrol_user
  • Loading branch information
matthewhilton authored Nov 26, 2024
2 parents 43a4450 + 4fe1e9e commit 8512cf4
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 21 deletions.
24 changes: 8 additions & 16 deletions auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

require_once($CFG->libdir . '/authlib.php');

use auth_enrolkey\utility;

// If totara cohort lib exists, import it.
if (file_exists($CFG->dirroot . '/totara/cohort/lib.php')) {
require_once($CFG->dirroot . '/totara/cohort/lib.php');
Expand Down Expand Up @@ -202,21 +204,6 @@ public function user_signup($user, $notify=true) {
return;
}

// New Enrolkey hook, will force/add user profile fields user based on the enrolkey used.
\auth_enrolkey\persistent\enrolkey_profile_mapping::add_fields_during_signup($user, $availableenrolids);

// New Enrolkey hook, will assign and enrol this user to corhots based on the enrolkey used.
\auth_enrolkey\persistent\enrolkey_cohort_mapping::add_cohorts_during_signup($user, $availableenrolids);

// At this point signup and enrolment is finished.
// If enabled, run a cohort sync to force dynamic cohorts to update.
if (get_config('auth_enrolkey', 'totaracohortsync') &&
function_exists('totara_cohort_check_and_update_dynamic_cohort_members')) {
$trace = new \null_progress_trace();
// This may be a perfomance hog.
totara_cohort_check_and_update_dynamic_cohort_members(null, $trace);
}

if (!empty($availableenrolids) && $user->confirmed === 0 && $user->policyagreed === 0) {
$this->email_confirmation($user->email);
}
Expand Down Expand Up @@ -247,7 +234,7 @@ function_exists('totara_cohort_check_and_update_dynamic_cohort_members')) {
* @return array
*/
public function enrol_user(string $enrolkey, bool $notify = true) : array {
global $DB;
global $DB, $USER;

/** @var enrol_self_plugin $enrol */
$enrol = enrol_get_plugin('self');
Expand All @@ -265,6 +252,11 @@ public function enrol_user(string $enrolkey, bool $notify = true) : array {
$errors[$enrolplugin->courseid] = $enrol->can_self_enrol($enrolplugin);
}
}

if (!empty($availableenrolids)) {
utility::update_user($USER, $availableenrolids);
}

return [$availableenrolids, $errors];
}

Expand Down
2 changes: 1 addition & 1 deletion classes/persistent/enrolkey_cohort_mapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static function get_records_by_enrolid($enrolid) {
/**
* During auth.php->user_signup, this adds the user to the associated cohorts.
*
* @param stdClass $user
* @param \stdClass $user
* @param array $availableenrolids
*/
public static function add_cohorts_during_signup($user, $availableenrolids) {
Expand Down
2 changes: 1 addition & 1 deletion classes/persistent/enrolkey_profile_mapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static function get_records_by_enrolid($enrolid) {
/**
* During auth.php->user_signup, this adds the forced user profile fields.
*
* @param stdClass $user
* @param \stdClass $user
* @param array $availableenrolids
*/
public static function add_fields_during_signup($user, $availableenrolids) {
Expand Down
22 changes: 22 additions & 0 deletions classes/utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,26 @@ public static function can_self_enrol(\stdClass $instance, $checkuserenrolment =

return true;
}

/**
* Updates user profile and cohort according to the set enrolkey mappings.
*
* @param \stdClass $user the user to update
* @param array $availableenrolids the enrol ids for the enrolkey used
*/
public static function update_user($user, $availableenrolids) {
// Update user profile fields based on the enrolkey used.
\auth_enrolkey\persistent\enrolkey_profile_mapping::add_fields_during_signup($user, $availableenrolids);

// Assign this user to corhots based on the enrolkey used.
\auth_enrolkey\persistent\enrolkey_cohort_mapping::add_cohorts_during_signup($user, $availableenrolids);

// If enabled, run a cohort sync to force dynamic cohorts to update.
if (get_config('auth_enrolkey', 'totaracohortsync') &&
function_exists('totara_cohort_check_and_update_dynamic_cohort_members')) {
$trace = new \null_progress_trace();
// This may be a perfomance hog.
totara_cohort_check_and_update_dynamic_cohort_members(null, $trace);
}
}
}
6 changes: 3 additions & 3 deletions unsuspend.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@
list($availableenrolids, $errors) = utility::unsuspend_and_enrol_user($data->signup_token, false);

// Only enrol a user to enrolkeys and courses which they are not already enrolled in.
if ($availableenrolids) {
if (!empty($availableenrolids)) {
complete_user_login($user);
utility::unsuspend_user($user);

// They are now unsuspended. We can actually called the real auth login function.
if (authenticate_user_login($user->username, $data->password)) {
\auth_enrolkey\persistent\enrolkey_profile_mapping::add_fields_during_signup($user, $availableenrolids);
\auth_enrolkey\persistent\enrolkey_cohort_mapping::add_cohorts_during_signup($user, $availableenrolids);
utility::update_user($user, $availableenrolids);

\auth_enrolkey\persistent\enrolkey_redirect_mapping::redirect_during_signup($availableenrolids);

// Default redirect.
Expand Down

0 comments on commit 8512cf4

Please sign in to comment.