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

Added email formatting to accept emails with nickname or plus sign #2

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 47 additions & 63 deletions mod_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [email protected] or [email protected]
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 href=\"{$a}\" target=\"_blank\">{$a}</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) {
Expand All @@ -300,7 +287,6 @@ public function definition() {
</div>";
}
} else {
// Display only the current user's alert message
if (!empty($current_user_alert)) {
$alert_html .= "<div class='alert alert-warning alert-dismissible fade show' role='alert'>
{$current_user_alert}
Expand All @@ -311,25 +297,23 @@ public function definition() {
}
}

// Add alert HTML to the form
if (!empty($alert_html)) {
$mform->addElement('html', $alert_html);
}

// Add an assign instructor field if the user has the capability to do so
$select = $mform->addElement('select', 'assign', get_string('assign', 'zoom'), $teachersmenu);

// Don't need to call anything, we just want to get the user who is the host of the Zoom meeting.
$zoomuser = zoom_webservice()->get_user($this->current->host_id);

if ($zoomuser) {
if (!$isnew) {
//select the host of the Zoom meeting
$select->setSelected(strtolower($zoomuser->email));
}
}
}




$mform->addElement('text', 'name', get_string('title', 'zoom'), ['size' => '64']);
// Render Bootstrap alerts
Expand Down
Loading