Merge branch 'feature-3716/Messaging_inbox_outbox_user'

This commit is contained in:
Andreas Österreicher
2020-02-28 11:05:14 +01:00
7 changed files with 109 additions and 117 deletions
+26 -17
View File
@@ -1,20 +1,8 @@
<?php
/**
* FH-Complete
*
* @package FHC-API
* @author FHC-Team
* @copyright Copyright (c) 2016, fhcomplete.org
* @license GPLv3
* @link http://fhcomplete.org
* @since Version 1.0
* @filesource
*/
// ------------------------------------------------------------------------
if (!defined("BASEPATH")) exit("No direct script access allowed");
class MailJob extends CLI_Controller
class MailJob extends JOB_Controller
{
/**
* API constructor
@@ -28,11 +16,32 @@ class MailJob extends CLI_Controller
}
/**
* Send all not sent messages
* Parameters are used to overrride messages and mail configuration
* 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 sendMessages($numberToSent = null, $numberPerTimeRange = null, $emailTimeRange = null, $emailFromSystem = null)
public function sendAllMessageEmailNotices($since = '1970-01-01', $numberToSent = null, $numberPerTimeRange = null, $emailTimeRange = null, $emailFromSystem = null)
{
$this->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');
}
}
+42 -19
View File
@@ -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');
+5 -2
View File
@@ -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".'<option value="%s">%s</option>',
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');
@@ -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));
}
/**
+14 -56
View File
@@ -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));
}
/**
@@ -129,34 +129,29 @@
</label>
</div>
</div>
<div class="well">
<div class="well" id="templatePreviewDiv">
<div class="row">
<div class="col-lg-3">
<div class="form-grop form-inline">
<div class="col-sm-12" style="display: inline">
<div class="form-group form-inline">
<div class="input-group">
<?php
echo $this->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'
)
);
?>
<span class="input-group-btn">
<a class="btn btn-default" href="#templatePreviewDiv" id="refresh">
<?php echo ucfirst($this->p->t('ui', 'refresh')); ?>
</a>
</span>
</div>
</div>
</div>
<div class="col-lg-1 valign-middle">
<strong>
<a href="#" id="refresh">
<?php echo ucfirst($this->p->t('ui', 'refresh')); ?>
</a>
</strong>
</div>
</div>
<br>
<textarea id="tinymcePreview"></textarea>
+1 -1
View File
@@ -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