From a50cc6cba87c1ff25abc1d84032437e4248c1ef6 Mon Sep 17 00:00:00 2001 From: Paolo Date: Tue, 25 Feb 2020 16:41:27 +0100 Subject: [PATCH] - jobs/MailJob->sendAllMessageEmailNotices changed since parameter default value - Moved ALT_OE from MessageLib to CL/Messages_model - MessageLib->_setSentError renamed to _updatedRecipientNoticeEmailInfo - MessageLib->_sendNoticeEmail changed to have more information about a not sent notice email - Recipient_model->getNotSentMessages now does not retrieve messages with notice email send failure --- application/controllers/jobs/MailJob.php | 3 +- application/libraries/MessageLib.php | 33 +++++++++++++++---- application/models/CL/Messages_model.php | 4 ++- application/models/system/Recipient_model.php | 3 +- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/application/controllers/jobs/MailJob.php b/application/controllers/jobs/MailJob.php index b4b772f78..f459d4d3e 100644 --- a/application/controllers/jobs/MailJob.php +++ b/application/controllers/jobs/MailJob.php @@ -19,12 +19,13 @@ class MailJob extends JOB_Controller * Send all the NOT sent notice emails for messaging system * The parameters are all not mandatory, they could be used to overrides the configs for testing, debug or one shot purposes */ - public function sendAllMessageEmailNotices($since = null, $numberToSent = null, $numberPerTimeRange = null, $emailTimeRange = null, $emailFromSystem = null) + public function sendAllMessageEmailNotices($since = '1970-01-01', $numberToSent = null, $numberPerTimeRange = null, $emailTimeRange = null, $emailFromSystem = null) { $this->logInfo('Send all message email notices started'); // Send them all! $sendAllEmailNotices = $this->messagelib->sendAllEmailNotices($since, $numberToSent, $numberPerTimeRange, $emailTimeRange, $emailFromSystem); + if (isError($sendAllEmailNotices)) { $optionalParameters = new stdClass(); diff --git a/application/libraries/MessageLib.php b/application/libraries/MessageLib.php index cfd450f98..f2ede9cef 100644 --- a/application/libraries/MessageLib.php +++ b/application/libraries/MessageLib.php @@ -26,8 +26,6 @@ class MessageLib const EMAIL_KONTAKT_TYPE = 'email'; // Email kontakt type const SENT_INFO_NEWLINE = '\n'; // tbl_msg_recipient->sentInfo separator - const ALT_OE = 'infocenter'; // alternative organisation unit when no one is found for a presetudent - private $_ci; /** @@ -498,7 +496,7 @@ class MessageLib * Stores the type of error in 'sentinfo' column keeping en eventual previous error * sent column is set to null */ - private function _setSentError($message_id, $receiver_id, $sentInfo, $prevSentInfo) + private function _updatedRecipientNoticeEmailInfo($message_id, $receiver_id, $sentInfo, $prevSentInfo) { if (!isEmptyString($prevSentInfo)) { @@ -763,15 +761,15 @@ class MessageLib if (!$sent) { // Set in database why this email is NOT going to be send - $sse = $this->_setSentError( + $sse = $this->_updatedRecipientNoticeEmailInfo( $messageData->message_id, $messageData->receiver_id, - 'An error occurred while sending the notice email', - $messageData->sentinfo + 'An error occurred while sending the notice email', // current info + $messageData->sentinfo // previous info ); // If database error occurred then return it, otherwise return a logic error - return isError($sse) ? $sse : error('An error occurred while sending the notice email'); + return isError($sse) ? $sse : error('An error occurred while updating the recipient notice email info'); } else // success! { @@ -780,6 +778,27 @@ class MessageLib if (isError($sss)) return $sss; // If database error occurred then return it } } + else // Because was not possible to find a valid contact + { + $reason = 'Was not possible to find a valid contact for this user'; // default reason + + // In case that the organisation unit does not receive any email notices + if (!isEmptyString($messageData->receiver_ou)) $reason = 'This organization unit does not receive email notices'; + + // In case that a degree program sent a message to a user without a valid contact or UID + if (!isEmptyString($messageData->sender_ou)) $reason = 'Sent from a degree program to a user that does not have a valid UID or a valid contact'; + + // Set in database why this email is NOT going to be send + $sse = $this->_updatedRecipientNoticeEmailInfo( + $messageData->message_id, + $messageData->receiver_id, + $reason, // current info + $messageData->sentinfo // previous info + ); + + // If database error occurred then return it + if (isError($sse)) return $sse; + } } return success('Notice emails sent successfully'); diff --git a/application/models/CL/Messages_model.php b/application/models/CL/Messages_model.php index 9cc4a76da..cbb42543f 100644 --- a/application/models/CL/Messages_model.php +++ b/application/models/CL/Messages_model.php @@ -26,6 +26,8 @@ class Messages_model extends CI_Model const TYPE_PERSONS = 'persons'; const TYPE_PRESTUDENTS = 'prestudents'; + const ALT_OE = 'infocenter'; // alternative organisation unit when no one is found for a presetudent + /** * Constructor */ @@ -106,7 +108,7 @@ class Messages_model extends CI_Model { $ouOptions .= sprintf( "\n".'', - is_numeric($ou->prestudent_id) ? $ou->oe_kurzbz : MessageLib::ALT_OE, + is_numeric($ou->prestudent_id) ? $ou->oe_kurzbz : self::ALT_OE, $ou->bezeichnung . (is_numeric($ou->prestudent_id) ? '' : ' *') ); } diff --git a/application/models/system/Recipient_model.php b/application/models/system/Recipient_model.php index ad1c1e850..d74d03243 100644 --- a/application/models/system/Recipient_model.php +++ b/application/models/system/Recipient_model.php @@ -205,12 +205,13 @@ class Recipient_model extends DB_Model * @param limit specifies the max number of messages to get * @param since specifies from which date messages have to be retrieved */ - public function getNotSentMessages($limit, $since = '1970-01-01') + public function getNotSentMessages($limit, $since) { $query = 'SELECT mm.message_id FROM public.tbl_msg_recipient mr JOIN public.tbl_msg_message mm USING (message_id) WHERE mr.sent IS NULL + AND mr.sentinfo IS NULL AND mm.insertamum > ? ORDER BY mr.insertamum ASC LIMIT ?';