Skip to content

Commit

Permalink
Move profile and cohort updating to a new function
Browse files Browse the repository at this point in the history
  • Loading branch information
rhell4 committed Nov 25, 2024
1 parent a648df4 commit 4fe1e9e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 29 deletions.
19 changes: 4 additions & 15 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 @@ -251,21 +253,8 @@ public function enrol_user(string $enrolkey, bool $notify = true) : array {
}
}

if ($availableenrolids) {
// 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)) {
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);
}
}
}
14 changes: 2 additions & 12 deletions unsuspend.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,13 @@
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);

// 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);
}
utility::update_user($user, $availableenrolids);

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

Expand Down

0 comments on commit 4fe1e9e

Please sign in to comment.