diff --git a/application/controllers/jobs/MailJob.php b/application/controllers/jobs/MailJob.php index 721db0068..b4b772f78 100644 --- a/application/controllers/jobs/MailJob.php +++ b/application/controllers/jobs/MailJob.php @@ -1,20 +1,8 @@ messagelib->sendAllNotices($numberToSent, $numberPerTimeRange, $emailTimeRange, $emailFromSystem); + $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(); + $optionalParameters->$since = $since; + $optionalParameters->$numberToSent = $numberToSent; + $optionalParameters->$numberPerTimeRange = $numberPerTimeRange; + $optionalParameters->$emailTimeRange = $emailTimeRange; + $optionalParameters->$emailFromSystem = $emailFromSystem; + + $this->logError($sendAllEmailNotices->retval, $optionalParameters); + } + elseif (!hasData($sendAllEmailNotices)) + { + $this->logInfo('There were no unsent messages'); + } + + $this->logInfo('Send all message email notices ended'); } } diff --git a/application/libraries/MessageLib.php b/application/libraries/MessageLib.php index 3a8f133cf..cfd450f98 100644 --- a/application/libraries/MessageLib.php +++ b/application/libraries/MessageLib.php @@ -139,27 +139,31 @@ class MessageLib // Public methods called by a job /** - * Gets all NOT sent messages from DB and sends for each of them the notice email - * Does not return anything, it logs info and errors on CI logs and in tbl_msg_recipient table + * Gets all messages for which notice emails are still not sent from DB and sends for each of them the notice email * Wrapper for _sendNoticeEmail. */ - public function sendAllEmailNotices($numberToSent, $numberPerTimeRange, $emailTimeRange, $emailFromSystem) + public function sendAllEmailNotices($since, $numberToSent, $numberPerTimeRange, $emailTimeRange, $emailFromSystem) { // Overrides MailLib configs with the given parameters $this->_ci->maillib->overrideConfigs($numberToSent, $numberPerTimeRange, $emailTimeRange, $emailFromSystem); - // Retrieves a certain amount of NOT sent messages, the amount is given by maillib->email_number_to_sent - $messagesResult = $this->_ci->RecipientModel->getMessages( - self::EMAIL_KONTAKT_TYPE, - null, - $this->_ci->maillib->getEmailNumberToSent() + // Retrieves a certain amount of NOT sent messages + $messagesResult = $this->_ci->RecipientModel->getNotSentMessages( + $this->_ci->maillib->getEmailNumberToSent(), + $since ); - if (isError($messagesResult)) terminateWithError(getData($messagesResult)); // If an error occurred then log it and terminate + if (isError($messagesResult) || !hasData($messagesResult)) return $messagesResult; - $sendNotice = $this->_sendNoticeEmails(getData($messagesResult)); + // Collects all the message ids in an array + $messageIds = array(); + foreach (getData($messagesResult) as $message) + { + $messageIds[] = $message->message_id; + } - if (isError($sendNotice)) terminateWithError(getData($sendNotice)); // If an error occurred then log it and terminate + // Send'em all + return $this->_sendNoticeEmails($messageIds); } //------------------------------------------------------------------------------------------------------------------ diff --git a/application/models/CL/Messages_model.php b/application/models/CL/Messages_model.php index de6ec5a10..9cc4a76da 100644 --- a/application/models/CL/Messages_model.php +++ b/application/models/CL/Messages_model.php @@ -509,7 +509,8 @@ class Messages_model extends CI_Model if (!hasData($message)) return error('No messages were saved in database'); // Write log entry - $personLog = $this->_personLog($sender_id, $receiver_id, getData($message)[0]); + // NOTE: $receiver_id and $sender_id are switched!!! Currently this is a workaround + $personLog = $this->_personLog($receiver_id, $sender_id, getData($message)[0]); if (isError($personLog)) return $personLog; return success('Messages sent successfully'); diff --git a/application/models/system/Recipient_model.php b/application/models/system/Recipient_model.php index cea11a7a7..ad1c1e850 100644 --- a/application/models/system/Recipient_model.php +++ b/application/models/system/Recipient_model.php @@ -199,66 +199,23 @@ class Recipient_model extends DB_Model } /** - * getMessages + * Gets all messages for which notice emails are still not sent * - * Gets all the messages to be sent - * - * @param kontaktType specifies the type of the kontakt to get - * @param sent specifies the status of the messages to get (NULL never sent, otherwise the shipping date) - * @param limit specifies the number of messages to get - * @param message_id specifies a single message + * @param kontaktType specifies the type of the kontakt to get (email,...) + * @param limit specifies the max number of messages to get + * @param since specifies from which date messages have to be retrieved */ - public function getMessages($kontaktType, $message_id = null, $limit = 1) + public function getNotSentMessages($limit, $since = '1970-01-01') { - $query = 'SELECT mm.message_id, - ks.kontakt as sender, - kr.kontakt as receiver, - mu.mitarbeiter_uid as employeeContact, - ms.mitarbeiter_uid as senderemployeeContact, - mr.person_id as receiver_id, - mr.token, - mm.subject, - mm.body, - mr.sentinfo, - mr.oe_kurzbz - FROM public.tbl_msg_recipient mr INNER JOIN public.tbl_msg_message mm USING (message_id) - LEFT JOIN ( - SELECT person_id, kontakt FROM public.tbl_kontakt WHERE zustellung = true AND kontakttyp = ? - ) ks ON (ks.person_id = mm.person_id) - LEFT JOIN ( - SELECT person_id, kontakt FROM public.tbl_kontakt WHERE zustellung = true AND kontakttyp = ? - ) kr ON (kr.person_id = mr.person_id) - LEFT JOIN ( - SELECT b.person_id, - m.mitarbeiter_uid - FROM public.tbl_benutzer b INNER JOIN public.tbl_mitarbeiter m ON(b.uid = m.mitarbeiter_uid) - WHERE b.aktiv = TRUE - ) mu ON (mu.person_id = mr.person_id) - LEFT JOIN ( - SELECT b.person_id, - m.mitarbeiter_uid - FROM public.tbl_benutzer b INNER JOIN public.tbl_mitarbeiter m ON(b.uid = m.mitarbeiter_uid) - WHERE b.aktiv = TRUE - ) ms ON (ms.person_id = mm.person_id) - WHERE mr.sent IS NULL'; + $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 mm.insertamum > ? + ORDER BY mr.insertamum ASC + LIMIT ?'; - $parametersArray = array($kontaktType, $kontaktType); - - if (is_numeric($message_id)) - { - array_push($parametersArray, $message_id); - $query .= ' AND mm.message_id = ?'; - } - - $query .= ' ORDER BY mr.insertamum ASC'; - - if (is_numeric($limit)) - { - $query .= ' LIMIT ?'; - array_push($parametersArray, $limit); - } - - return $this->execQuery($query, $parametersArray); + return $this->execQuery($query, array($since, $limit)); } /**