From c2e0f4de6c2c7c776a09de72456f12e6715eeb99 Mon Sep 17 00:00:00 2001 From: Joel Dapiawen Date: Thu, 4 Jul 2024 09:56:31 -0600 Subject: [PATCH] Added email formatting to accept emails with nickname or plus sign --- mod_form.php | 110 ++++++++++++++++++++++----------------------------- 1 file changed, 47 insertions(+), 63 deletions(-) diff --git a/mod_form.php b/mod_form.php index 8a815cc6..7bf66194 100644 --- a/mod_form.php +++ b/mod_form.php @@ -189,106 +189,93 @@ public function definition() { $context = context_course::instance($this->_course->id); if (has_capability('mod/zoom:assign', $context)) { - $teacherarray = zoom_get_course_instructors($this->_course->id); - /** - * Joel Dapiawen - * February 12, 2024 - * update June 12, 2024 - * Try to format the email address of the user to the expected format and add it to the dropdown menu - * Select the host who owns the meeting when editing the Zoom meeting. - */ + $teachersmenu = array(); - $alert_messages = array(); - $current_user_alert = ''; + $alert_messages = array(); + $current_user_alert = ''; foreach ($teacherarray as $teacher) { - // Convert the email address to lowercase - $teacher_email_lower = strtolower($teacher->email); + $teacher_email_lower = strtolower($teacher->email); + $zoom_user = zoom_webservice()->get_user($teacher_email_lower); - // Check if the user has a valid Zoom account - $zoom_user = zoom_webservice()->get_user($teacher_email_lower); - if ($zoom_user) { - // If the user has a valid Zoom account, use their current email address $teachersmenu[$teacher_email_lower] = $teacher->name; - - // Split the name into parts $name_parts = explode(' ', $teacher->name); - - // Ensure there are enough parts to split into first and last names + if (count($name_parts) >= 2) { $first_name = strtolower(array_shift($name_parts)); $last_name = strtolower(implode('', $name_parts)); - - // Construct the expected email address format + + // Construct expected email format $expected_email = $first_name . '.' . $last_name . '@uregina.ca'; - - // Check if the email matches the expected format - if (strtolower($teacher_email_lower) !== $expected_email) { + + // do not send alert message if the email is acceptable like sample.lname@uregina.ca or sample+urt00@uregina.ca + if ($teacher_email_lower !== $expected_email && !preg_match('/^[a-z]+\.[a-z]+.*|^[a-z]+\+.*@uregina\.ca$/m', $teacher_email_lower)) { // Add alert message if email does not match the expected format - $message = "User {$teacher->name}, email address ({$teacher_email_lower}) seems to be incorrectly formatted. It should be in the format: {$expected_email}. To avoid any future issues please call IT support."; + $message = "The email address ({$teacher_email_lower}) appears to be incorrectly formatted. It should be in the format: {$expected_email}. To avoid any future issues please contact IT support."; $alert_messages[] = $message; if ($teacher_email_lower == $USER->email) { $current_user_alert = $message; } } } else { - // Handle edge case where name might not split correctly + // Handle case where the name has fewer than two parts (likely a single name) $corrected_email = strtolower($teacher->name) . '@uregina.ca'; - if (strtolower($teacher_email_lower) !== $corrected_email) { - // Add alert message if email does not match the expected format - $message = "User {$teacher->name}, email address ({$teacher_email_lower}) seems to be incorrectly formatted. It should be in the format: {$corrected_email}. To avoid any future issues please call IT support."; - $alert_messages[] = $message; - if ($teacher_email_lower == $USER->email) { - $current_user_alert = $message; - } + if ($teacher_email_lower !== $corrected_email) { + // Add to teachers menu + $teachersmenu[$corrected_email] = $teacher->name; } } - } else { - // Split the name into parts $name_parts = explode(' ', $teacher->name); - - // Ensure there are enough parts to split into first and last names + if (count($name_parts) >= 2) { $first_name = strtolower(array_shift($name_parts)); $last_name = strtolower(implode('', $name_parts)); - + // Construct the expected email address format $expected_email = $first_name . '.' . $last_name . '@uregina.ca'; - - // Check if the email matches the expected format - if (strtolower($teacher_email_lower) === $expected_email) { - // If the email matches the expected format, add it to the dropdown menu - $teachersmenu[$teacher_email_lower] = $teacher->name; + + // Try to get the zoom user with the expected email + $zoom_user = zoom_webservice()->get_user($expected_email); + + // Check if the Zoom user can be found with the corrected email format + if ($zoom_user) { + // Add the teacher to the dropdown menu + $teachersmenu[$expected_email] = $teacher->name; + + // Check if the original email matches the expected format + if ($teacher_email_lower !== $expected_email) { + // Add alert message + $message = "The email address ({$teacher_email_lower}) appears to be incorrectly formatted. It should be in the format: {$expected_email}. To avoid any future issues please contact IT support."; + $alert_messages[] = $message; + if ($teacher_email_lower == $USER->email) { + $current_user_alert = $message; + } + } } else { - // If the email format is incorrect, correct it and add it to the dropdown menu - $corrected_email = $expected_email; - $teachersmenu[$corrected_email] = $teacher->name; - // Add alert message - $message = "User {$teacher->name}, email address ({$teacher_email_lower}) seems to be incorrectly formatted. It should be in the format: {$expected_email}. To avoid any future issues please call IT support."; - + // Zoom user not found, display the cannot be found message + $a = 'https://zoom.us/signin#/login'; + $message = "Unable to find your account ({$teacher->name}) on Zoom. If you are using Zoom for the first time, you must activate your Zoom account by logging into {$a}. Once you have activated your Zoom account, reload this page and continue setting up your meeting. Otherwise, make sure your email on Zoom matches your email on this system."; $alert_messages[] = $message; if ($teacher_email_lower == $USER->email) { $current_user_alert = $message; } } } else { - // Handle edge case where name might not split correctly + // Handle case where the name has fewer than two parts (likely a single name) $corrected_email = strtolower($teacher->name) . '@uregina.ca'; - $teachersmenu[$corrected_email] = $teacher->name; - // Add alert message - $message = "User {$teacher->name}, email address ({$teacher_email_lower}) seems to be incorrectly formatted. It should be in the format: {$corrected_email}. To avoid any future issues please call IT support."; - - if ($teacher_email_lower == $USER->email) { - $current_user_alert = $message; + if ($teacher_email_lower !== $corrected_email) { + // Add to teachers menu + $teachersmenu[$corrected_email] = $teacher->name; } } } } + + - // Generate Bootstrap alerts for admin support roles $alert_html = ''; if (has_capability('moodle/site:config', $context) || has_capability('moodle/site:doanything', $context)) { foreach ($alert_messages as $message) { @@ -300,7 +287,6 @@ public function definition() { "; } } else { - // Display only the current user's alert message if (!empty($current_user_alert)) { $alert_html .= "