Skip to content

Instantly share code, notes, and snippets.

@chrisarusso
Created November 11, 2014 21:50
Show Gist options
  • Save chrisarusso/c4af2e1970199097d677 to your computer and use it in GitHub Desktop.
Save chrisarusso/c4af2e1970199097d677 to your computer and use it in GitHub Desktop.
/**
* Function to queue up text and email reminders from cron runs.
*/
function compost_customizations_queue_reminders() {
$now = time();
$tomorrow_timestamp = $now + 60 * 60 * 24;
$tomorrow = date('l', $tomorrow_timestamp);
// Somewhat arbitrary. Reminders ought to be once a week, but on certain
// weeks, like holidays, the gap will be larger or smaller. Three days seems
// safe that it'll never be less than
$three_days_ago = $now - 3 * (60 * 60 * 24);
$hour_of_the_day = date('G', $now);
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'user')
->fieldCondition('field_reminders', 'value', 0, '>')
->fieldCondition('field_active', 'value', 1)
->fieldCondition('field_collection_day', 'value', $tomorrow)
->fieldCondition('field_last_reminded', 'value', $three_days_ago, '<')
->fieldCondition('field_reminder_time', 'value', $hour_of_the_day, '<=');
$result = $query->execute();
if (!empty($result['user'])) {
$text_queue = DrupalQueue::get('text_reminders');
$email_queue = DrupalQueue::get('email_reminders');
$user_uids = array_keys($result['user']);
$users = entity_load('user', $user_uids);
foreach($users as $user) {
if (!empty($user->field_reminders)) {
foreach($user->field_reminders[LANGUAGE_NONE] as $reminder) {
if ($reminder['value'] == 1) {
$email_queue->createItem($user);
}
elseif ($reminder['value'] == 2) {
$text_queue->createItem($user);
}
}
// Mark them as reminded here. If the users is not marked here, and
// rather when the message is sent, they would be queued twice
$user->field_last_reminded[LANGUAGE_NONE][0]['value'] = $_SERVER['REQUEST_TIME'];
field_attach_update('user', $user);
}
}
}
}
@chrisarusso
Copy link
Author

The basic gist (pun intended) of this function is to send automated weekly email and text reminders to subscribers of a service based on fields they edit on their user profile. They may opt into an email and/or text reminder, and may set the time of day they would like that reminder to be triggered. This function is called by cron.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment