Skip to content

Commit

Permalink
save emails #35
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuNls committed Nov 23, 2015
1 parent cbf7b1d commit b52bfd2
Showing 1 changed file with 36 additions and 16 deletions.
52 changes: 36 additions & 16 deletions application/models/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,23 @@ public function cronCheck($time = null) {
$this->checkAccuracyOneWeek($time, $emailsMeasureSent);
$this->startANewMeasure($time, $emailsWatchSent);

$this->insertAll($emailsUserSent, new MY_Model('email_user'));
$this->insertAll($emailsWatchSent, new MY_Model('email_watch'));
$this->insertAll($emailsMeasureSent, new MY_Model('email_measure'));

return array(
'users' => $emailsUserSent,
'watches' => $emailsWatchSent,
'measures' => $emailsMeasureSent
);
}

private function insertAll($array, $model){
if(is_array($array) && sizeof($array) !== 0){
$model->insert_batch($array);
}
}

private function sendMandrillEmail($subject, $content, $recipientName,
$recipientEmail, $tags, $sendAt, $attachments = null) {

Expand Down Expand Up @@ -124,11 +134,11 @@ private function whereNotAlreadySentMeasure($emailType) {
'= email_measure.measureId and emailType = '.$emailType.') = ';
}

private function addEmailToQueue(&$queue, $userId, $emailType, $time) {
private function addEmailToQueue(&$queue, $userId, $emailType, $time, $idTitle) {
array_push($queue,
array(
'userId' => $userId,
'sendTime' => $time,
$idTitle => $userId,
'sentTime' => $time,
'emailType' => $emailType,
)
);
Expand Down Expand Up @@ -157,7 +167,8 @@ private function inactiveUser($time, &$queuedEmail) {
$queuedEmail,
$user->userId,
$this->COMEBACK,
$time
$time,
'userId'
);
}
}
Expand Down Expand Up @@ -188,7 +199,8 @@ private function userWithoutWatch($time, &$queuedEmail) {
$queuedEmail,
$user->userId,
$this->ADD_FIRST_WATCH,
$time
$time,
'userId'
);
}
}
Expand All @@ -202,6 +214,7 @@ private function userWithWatchWithoutMeasure($time, &$queuedEmail) {
->where('(select count(1) from measure where watch.watchId = measure.watchId) = ', 0)
->where('creationDate <=', $time-$this->day)
->where($this->whereNotAlreadySentWatch($this->START_FIRST_MEASURE), 0, false)
->as_array()
->find_all();

if ($userWithWatchWithoutMeasure !== FALSE) {
Expand All @@ -210,6 +223,8 @@ private function userWithWatchWithoutMeasure($time, &$queuedEmail) {

foreach ($userWithWatchWithoutMeasure as $user) {

$user = (object) $user;

$this->sendMandrillEmail(
'Let’s start measuring! ⌚',
$this->load->view('email/make-first-measure', $user, true),
Expand All @@ -221,9 +236,10 @@ private function userWithWatchWithoutMeasure($time, &$queuedEmail) {

$this->addEmailToQueue(
$queuedEmail,
$user->userId,
$user->watchId,
$this->START_FIRST_MEASURE,
$time
$time,
'watchId'
);
}
}
Expand Down Expand Up @@ -261,7 +277,8 @@ private function userWithOneCompleteMeasureAndOneWatch($time, &$queuedEmail) {
$queuedEmail,
$user->userId,
$this->ADD_SECOND_WATCH,
$time
$time,
'userId'
);
}
}
Expand All @@ -270,7 +287,7 @@ private function userWithOneCompleteMeasureAndOneWatch($time, &$queuedEmail) {
private function checkAccuracy($time, &$queuedEmail) {
$measureWithoutAccuracy = $this
->measure
->select('user.userId, user.name, user.firstname, email')
->select('measure.id as measureId, user.userId, user.name, user.firstname, email')
->join('watch', 'watch.watchId = measure.watchId')
->join('user', 'watch.userId = user.userId')
->where('statusId', 1)
Expand Down Expand Up @@ -302,9 +319,10 @@ private function checkAccuracy($time, &$queuedEmail) {

$this->addEmailToQueue(
$queuedEmail,
$user->userId,
$user->measureId,
$this->CHECK_ACCURACY,
$time
$time,
'measureId'
);
}
}
Expand Down Expand Up @@ -341,9 +359,10 @@ private function checkAccuracyOneWeek($time, &$queuedEmail) {

$this->addEmailToQueue(
$queuedEmail,
$user->userId,
$user->measureId,
$this->CHECK_ACCURACY_1_WEEK,
$time
$time,
'measureId'
);
}
}
Expand All @@ -357,7 +376,7 @@ private function startANewMeasure($time, &$queuedEmail) {
->join('user', 'watch.userId = user.userId')
->where('statusId', 2)
->where('accuracyReferenceTime', $time-($this->day*30))
->where($this->whereNotAlreadySentMeasure($this->START_NEW_MEASURE), 0, false)
->where($this->whereNotAlreadySentWatch($this->START_NEW_MEASURE), 0, false)
->as_array()
->find_all();

Expand All @@ -380,9 +399,10 @@ private function startANewMeasure($time, &$queuedEmail) {

$this->addEmailToQueue(
$queuedEmail,
$user->userId,
$user->watchId,
$this->START_NEW_MEASURE,
$time
$time,
'watchId'
);
}
}
Expand Down

0 comments on commit b52bfd2

Please sign in to comment.