You are here

function activity_send_email_cron in Open Social 10.1.x

Same name and namespace in other branches
  1. 8.9 modules/custom/activity_send/modules/activity_send_email/activity_send_email.module \activity_send_email_cron()
  2. 8 modules/custom/activity_send/modules/activity_send_email/activity_send_email.module \activity_send_email_cron()
  3. 8.2 modules/custom/activity_send/modules/activity_send_email/activity_send_email.module \activity_send_email_cron()
  4. 8.3 modules/custom/activity_send/modules/activity_send_email/activity_send_email.module \activity_send_email_cron()
  5. 8.4 modules/custom/activity_send/modules/activity_send_email/activity_send_email.module \activity_send_email_cron()
  6. 8.5 modules/custom/activity_send/modules/activity_send_email/activity_send_email.module \activity_send_email_cron()
  7. 8.6 modules/custom/activity_send/modules/activity_send_email/activity_send_email.module \activity_send_email_cron()
  8. 8.7 modules/custom/activity_send/modules/activity_send_email/activity_send_email.module \activity_send_email_cron()
  9. 8.8 modules/custom/activity_send/modules/activity_send_email/activity_send_email.module \activity_send_email_cron()
  10. 10.3.x modules/custom/activity_send/modules/activity_send_email/activity_send_email.module \activity_send_email_cron()
  11. 10.0.x modules/custom/activity_send/modules/activity_send_email/activity_send_email.module \activity_send_email_cron()
  12. 10.2.x modules/custom/activity_send/modules/activity_send_email/activity_send_email.module \activity_send_email_cron()

Implements hook_cron().

This cronjob handles creating digest queue items to process.

2 string references to 'activity_send_email_cron'
social_advanced_queue_install in modules/custom/social_advanced_queue/social_advanced_queue.install
Implements hook_install().
social_advanced_queue_update_10201 in modules/custom/social_advanced_queue/social_advanced_queue.install
Update default cron settings.

File

modules/custom/activity_send/modules/activity_send_email/activity_send_email.module, line 26
Contains activity_basics.module..

Code

function activity_send_email_cron() {

  // Get plugins and their intervals.
  $emailfrequencymanager = \Drupal::service('plugin.manager.emailfrequency');
  $plugins = $emailfrequencymanager
    ->getDefinitions();

  // Fill a sortable array with the data about the email frequency.
  foreach ($plugins as $frequency) {
    $instance = $emailfrequencymanager
      ->createInstance($frequency['id']);

    /** @var \Drupal\activity_send_email\EmailFrequencyInterface $instance */
    $interval = $instance
      ->getInterval();

    // If the interval of the EmailFrequency plugin is more than 0, we should
    // consider it to be a digest email.
    if ($interval > 0) {

      // Get last run time.
      $last_run = \Drupal::state()
        ->get('digest.' . $frequency['id'] . '.last_run', 0);

      // If interval of frequency passed since last time, try to create queue
      // items.
      if (time() - $last_run > $interval) {

        // Query to get the data to process per user per frequency. And we make
        // sure to check only for items that need to be sent.
        $db = Database::getConnection();
        $query = $db
          ->select('user_activity_digest', 'uad')
          ->fields('uad', [
          'uid',
          'activity',
        ])
          ->condition('uad.frequency', $frequency['id'])
          ->orderBy('uad.timestamp', 'DESC');
        $activitities = $query
          ->execute()
          ->fetchAll();
        if (!empty($activitities)) {
          $queue_items = [];

          // Fill the queue items.
          foreach ($activitities as $activitity) {
            $queue_items[$activitity->uid]['uid'] = $activitity->uid;
            $queue_items[$activitity->uid]['frequency'] = $frequency['id'];
            $queue_items[$activitity->uid]['activities'][] = $activitity->activity;
          }

          // Add queue items to the queue worker.
          foreach ($queue_items as $queue_item) {

            // Add the item to the queue.
            $queue = \Drupal::queue('activity_digest_worker');
            $queue
              ->createItem($queue_item);

            // Remove activities from digest table that we just put it in the
            // queue.
            $db
              ->delete('user_activity_digest')
              ->condition('uid', $queue_item['uid'])
              ->condition('activity', $queue_item['activities'], 'IN')
              ->execute();
          }
        }

        // Update last run.
        \Drupal::state()
          ->set('digest.' . $frequency['id'] . '.last_run', time());
      }
    }
  }
}