diff --git a/classes/booking_manager.php b/classes/booking_manager.php index af97e53..f58f40a 100644 --- a/classes/booking_manager.php +++ b/classes/booking_manager.php @@ -168,7 +168,10 @@ public function validate($timenow = null): array { global $DB; $errors = []; $sessioncapacitycache = []; - $timenow ??= time(); + + if ($timenow == null) { + $timenow = time(); + } // Break into rows and validate the multiple interdependant fields together. foreach ($this->get_iterator() as $index => $entry) { diff --git a/lib.php b/lib.php index ed41045..ae53d86 100644 --- a/lib.php +++ b/lib.php @@ -1123,7 +1123,11 @@ function facetoface_get_future_sessions(int $id): array { $now = time(); return array_filter( facetoface_get_sessions($id), - fn (stdClass $s): bool => !empty(array_filter($s->sessiondates, fn (stdClass $d): bool => $d->timestart > $now)) + function(stdClass $s) use ($now) { + return !empty(array_filter($s->sessiondates, function(stdClass $d) use ($now) { + return $d->timestart > $now; + })); + } ); } @@ -2238,6 +2242,12 @@ function facetoface_send_notice($postsubject, $posttext, $posttextmgrheading, } } + // Define a simple reusable function so we don't have to copy + // and paste a huge function call multiple times. + $substitute = function($text, $session) use ($facetoface, $user) { + return facetoface_email_substitutions($text, format_string($facetoface->name), $facetoface->reminderperiod, $user, $session, $session->id); + }; + // Do iCal attachement stuff. $icalattachments = []; if ($notificationtype & MDL_F2F_ICAL) { @@ -2249,28 +2259,11 @@ function facetoface_send_notice($postsubject, $posttext, $posttextmgrheading, $session->sessiondates = [$sessiondate]; // One day at a time. $filename = facetoface_get_ical_attachment($notificationtype, $facetoface, $session, $user); - $subject = facetoface_email_substitutions( - $postsubject, - format_string($facetoface->name), - $facetoface->reminderperiod, - $user, - $session, - $session->id - ); - $body = facetoface_email_substitutions($posttext, format_string($facetoface->name), $facetoface->reminderperiod, - $user, $session, $session->id); - $htmlmessage = facetoface_email_substitutions( - $posttext, - $facetoface->name, - $facetoface->reminderperiod, - $user, - $session, - $session->id - ); - $htmlbody = $htmlmessage; + $subject = $substitute($postsubject, $session); + $body = $substitute($posttext, $session); $icalattachments[] = [ 'filename' => $filename, 'subject' => $subject, - 'body' => $body, 'htmlbody' => $htmlbody, + 'body' => $body, ]; } @@ -2278,42 +2271,20 @@ function facetoface_send_notice($postsubject, $posttext, $posttextmgrheading, $session->sessiondates = $sessiondates; } else { $filename = facetoface_get_ical_attachment($notificationtype, $facetoface, $session, $user); - $subject = facetoface_email_substitutions($postsubject, format_string($facetoface->name), $facetoface->reminderperiod, - $user, $session, $session->id); - $body = facetoface_email_substitutions($posttext, format_string($facetoface->name), $facetoface->reminderperiod, - $user, $session, $session->id); - $htmlmessage = facetoface_email_substitutions( - $posttext, - $facetoface->name, - $facetoface->reminderperiod, - $user, - $session, - $session->id - ); - $htmlbody = $htmlmessage; + $subject = $substitute($postsubject, $session); + $body = $substitute($posttext, $session); $icalattachments[] = [ 'filename' => $filename, 'subject' => $subject, - 'body' => $body, 'htmlbody' => $htmlbody, + 'body' => $body, ]; } } // Fill-in the email placeholders. - $postsubject = facetoface_email_substitutions($postsubject, format_string($facetoface->name), $facetoface->reminderperiod, - $user, $session, $session->id); - $posttext = facetoface_email_substitutions($posttext, format_string($facetoface->name), $facetoface->reminderperiod, - $user, $session, $session->id); - - $posttextmgrheading = facetoface_email_substitutions( - $posttextmgrheading, - format_string($facetoface->name), - $facetoface->reminderperiod, - $user, - $session, - $session->id - ); + $postsubject = $substitute($postsubject, $session); + $posttext = $substitute($posttext, $session); + $posttextmgrheading = $substitute($posttextmgrheading, $session); - $posthtml = ''; // FIXME. if ($fromaddress = get_config('facetoface', 'fromaddress')) { $from = new stdClass(); $from->maildisplay = true; @@ -2326,7 +2297,7 @@ function facetoface_send_notice($postsubject, $posttext, $posttextmgrheading, if ($notificationtype & MDL_F2F_ICAL) { foreach ($icalattachments as $attachment) { if (!email_to_user($user, $from, $attachment['subject'], $attachment['body'], - $attachment['htmlbody'], $attachment['filename'], $attachmentfilename)) { + '', $attachment['filename'], $attachmentfilename)) { return 'error:cannotsendconfirmationuser'; } unlink($CFG->dataroot . '/' . $attachment['filename']); @@ -2335,7 +2306,7 @@ function facetoface_send_notice($postsubject, $posttext, $posttextmgrheading, // Send plain text email. if ($notificationtype & MDL_F2F_TEXT - && !email_to_user($user, $from, $postsubject, $posttext, $posthtml)) { + && !email_to_user($user, $from, $postsubject, $posttext)) { return 'error:cannotsendconfirmationuser'; } @@ -2347,7 +2318,7 @@ function facetoface_send_notice($postsubject, $posttext, $posttextmgrheading, $manager->email = $manageremail; // Leave out the ical attachments in the managers notification. - if (!email_to_user($manager, $from, $postsubject, $managertext, $posthtml)) { + if (!email_to_user($manager, $from, $postsubject, $managertext)) { return 'error:cannotsendconfirmationmanager'; } } @@ -2361,7 +2332,7 @@ function facetoface_send_notice($postsubject, $posttext, $posttextmgrheading, $thirdparty->email = trim($recipient); // Leave out the ical attachments in the 3rd parties notification. - if (!email_to_user($thirdparty, $from, $postsubject, $posttext, $posthtml)) { + if (!email_to_user($thirdparty, $from, $postsubject, $posttext)) { return 'error:cannotsendconfirmationthirdparty'; } } diff --git a/tests/session_test.php b/tests/session_test.php index 83ba463..42b7ebd 100644 --- a/tests/session_test.php +++ b/tests/session_test.php @@ -18,6 +18,10 @@ use core_date; +defined('MOODLE_INTERNAL') || die(); +global $CFG; +require_once("$CFG->dirroot/mod/facetoface/lib.php"); + /** * Test the session helper class. * @@ -112,4 +116,216 @@ public function test_get_readable_session_time_with_users_timezone() { $expectedstring = "1 January 2030, 9:00 AM - 4 January 2030, 5:00 PM (time zone: $expectedtimezone)"; $this->assertEquals($expectedstring, session::get_readable_session_datetime($date)); } + + /** + * Provides values to test_email_notification + * @return array + */ + public static function email_notification_provider(): array { + $htmlconfirmmessage = " +
This is the confirm message
+Details:
+ [details] + "; + + $htmldetails = " +This is a html message
+This is the confirm message
Details:
This is a html messageThis is the confirm message
Details:
This is a plain text detail