Example #1
0
/**
 * Send reminder notifications to users based on their notification settings
 * @todo if there are a *lot* of recipients we should somehow break this off into parallel threads
 * 
 * @param Event $event Event
 * @return void
 */
function send_event_reminder($event, $remindertime = null)
{
    $force_send = true;
    if ($remindertime === null) {
        $remindertime = time();
        $force_send = false;
        // default cron send
    }
    $dbprefix = elgg_get_config('dbprefix');
    $options = array('type' => 'object', 'subtype' => 'calendar', 'relationship' => Calendar::EVENT_CALENDAR_RELATIONSHIP, 'relationship_guid' => $event->guid, 'joins' => array("JOIN {$dbprefix}users_entity ue ON e.container_guid = ue.guid"), 'limit' => false);
    $calendars = new ElggBatch('elgg_get_entities_from_relationship', $options);
    $starttimestamp = $event->getNextOccurrence($remindertime);
    $endtimestamp = $starttimestamp + $event->delta;
    // prevent sending if it was in the past, unless this is a forced reminder
    if (!$force_send && $starttimestamp < strtotime('-10 minutes')) {
        return true;
    }
    $owner = $event->getOwnerEntity();
    $owner_link = elgg_view('output/url', array('text' => $owner->name, 'href' => $owner->getURL()));
    $in_group = '';
    $in_group_link = '';
    $container = $event->getContainerEntity();
    $container_link = elgg_view('output/url', array('text' => $container->name, 'href' => $container->getURL()));
    if ($container instanceof \ElggGroup) {
        $in_group = elgg_echo('events:notify:subject:ingroup', array($container->name));
        $in_group_link = elgg_echo('events:notify:subject:ingroup', array($container_link));
    }
    $event_link = elgg_view('output/url', array('text' => $event->title, 'href' => $event->getURL()));
    $notified = array();
    // users could have multiple calendars
    foreach ($calendars as $calendar) {
        $user = $calendar->getContainerEntity();
        if (in_array($user->guid, $notified)) {
            continue;
        }
        $ia = elgg_set_ignore_access(false);
        if (!has_access_to_entity($event, $user)) {
            error_log($user->username . ' does not have access to ' . $event->guid);
            // the user can't see it, lets not notify them
            $notified[] = $user->guid;
            elgg_set_ignore_access($ia);
            continue;
        }
        elgg_set_ignore_access($ia);
        $notify_self = false;
        // support for notify self
        if (is_callable('notify_self_should_notify')) {
            $notify_self = notify_self_should_notify($user);
        }
        if (elgg_get_logged_in_user_guid() == $user->guid && !$notify_self) {
            $notified[] = $user->guid;
            continue;
        }
        $methods = get_calendar_notification_methods($user, 'eventreminder');
        if (!$methods) {
            $notified[] = $user->guid;
            continue;
        }
        $timezone = Util::getClientTimezone($user);
        $dt = new DateTime(null, new DateTimeZone($timezone));
        $dt->modify("{$event->start_date} {$event->start_time}");
        $original_subject = elgg_echo('event:notify:eventreminder:subject', array($event->title, $in_group, $dt->format('D, F j g:ia T')));
        $original_message = elgg_echo('event:notify:eventreminder:message', array($event_link, $in_group_link, elgg_view('output/events_ui/date_range', array('start' => $starttimestamp, 'end' => $endtimestamp, 'timezone' => $timezone)), $event->location, $event->description));
        $params = array('event' => $event, 'entity' => $event, 'calendar' => $calendar, 'user' => $user, 'starttime' => $starttimestamp, 'endtime' => $endtimestamp);
        $subject = elgg_trigger_plugin_hook('events_ui', 'subject:eventreminder', $params, $original_subject);
        $message = elgg_trigger_plugin_hook('events_ui', 'message:eventreminder', $params, $original_message);
        notify_user($user->guid, $event->container_guid, $subject, $message, array(), $methods);
        $notified[] = $user->guid;
    }
}