diff --git a/application/controllers/jobs/MailJob.php b/application/controllers/jobs/MailJob.php index 721db0068..f459d4d3e 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..b97ef4bae 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; /** @@ -139,27 +137,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); } //------------------------------------------------------------------------------------------------------------------ @@ -221,7 +223,7 @@ class MessageLib $this->_ci->load->model('person/Benutzerfunktion_model', 'BenutzerfunktionModel'); // Retrieves organisation units for a user from database - $benutzer = $this->_ci->BenutzerfunktionModel->getByPersonId($sender_id); + $benutzer = $this->_ci->BenutzerfunktionModel->getActiveFunctionsByPersonId($sender_id); if (isSuccess($benutzer)) // if everything is ok { $ouArray = array(); @@ -494,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)) { @@ -759,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! { @@ -776,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 de6ec5a10..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) ? '' : ' *') ); } @@ -509,7 +511,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/person/Benutzerfunktion_model.php b/application/models/person/Benutzerfunktion_model.php index ceb427687..fb9b51c1a 100644 --- a/application/models/person/Benutzerfunktion_model.php +++ b/application/models/person/Benutzerfunktion_model.php @@ -11,16 +11,20 @@ class Benutzerfunktion_model extends DB_Model $this->dbTable = 'public.tbl_benutzerfunktion'; $this->pk = 'benutzerfunktion_id'; } - + /** * Get the Benutzerfunktion using the person_id */ - public function getByPersonId($person_id) + public function getActiveFunctionsByPersonId($person_id) { - // Join with the table - $this->addJoin('public.tbl_benutzer', 'uid'); - - return $this->loadWhere(array('person_id' => $person_id)); + $query = 'SELECT bf.* + FROM public.tbl_benutzerfunktion bf + JOIN public.tbl_benutzer b USING (uid) + WHERE b.person_id = ? + AND (bf.datum_von IS NULL OR bf.datum_von <= now()) + AND (bf.datum_bis IS NULL OR bf.datum_bis >= now())'; + + return $this->execQuery($query, array($person_id)); } /** diff --git a/application/models/system/Recipient_model.php b/application/models/system/Recipient_model.php index cea11a7a7..d74d03243 100644 --- a/application/models/system/Recipient_model.php +++ b/application/models/system/Recipient_model.php @@ -199,66 +199,24 @@ 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) { - $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 mr.sentinfo 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)); } /** diff --git a/application/views/system/messages/htmlWriteTemplate.php b/application/views/system/messages/htmlWriteTemplate.php index 64a8c8236..575e9ea07 100644 --- a/application/views/system/messages/htmlWriteTemplate.php +++ b/application/views/system/messages/htmlWriteTemplate.php @@ -129,34 +129,29 @@ -
+
-
-
- +
+
+
widgetlib->widget( 'Dropdown_widget', - array('elements' => success($recipientsArray), 'emptyElement' => 'Select...'), + array('elements' => success($recipientsArray), 'emptyElement' => ucfirst($this->p->t('global', 'empfaenger')).'...'), array( - 'title' => ucfirst($this->p->t('global', 'empfaenger')).': ', 'name' => 'recipients[]', 'id' => 'recipients' ) ); ?> - + + + p->t('ui', 'refresh')); ?> + + +
-

diff --git a/application/widgets/Vorlage_widget.php b/application/widgets/Vorlage_widget.php index 9c9abe430..9ec13250f 100644 --- a/application/widgets/Vorlage_widget.php +++ b/application/widgets/Vorlage_widget.php @@ -63,7 +63,7 @@ class Vorlage_widget extends DropdownWidget FROM tbl_vorlagestudiengang vs INNER JOIN tbl_vorlage v USING(vorlage_kurzbz) ) templates'; $alias = 'templates'; - $fields = array("templates.vorlage_kurzbz AS id", "UPPER(templates.oe_kurzbz) || ' - ' || templates.bezeichnung || ' - V' || templates.version AS description"); + $fields = array("templates.vorlage_kurzbz AS id", "templates.bezeichnung AS description"); $where = 'templates.aktiv = TRUE AND templates.subject IS NOT NULL AND templates.text IS NOT NULL