You are here

function opigno_moxtra_cronjob_meeting_notification in Opigno Moxtra 3.x

Same name and namespace in other branches
  1. 8 opigno_moxtra.module \opigno_moxtra_cronjob_meeting_notification()

Cron job callback for opigno_moxtra_meeting_notification.

1 string reference to 'opigno_moxtra_cronjob_meeting_notification'
ultimate_cron.job.opigno_moxtra_meeting_notification.yml in config/install/ultimate_cron.job.opigno_moxtra_meeting_notification.yml
config/install/ultimate_cron.job.opigno_moxtra_meeting_notification.yml

File

./opigno_moxtra.module, line 259
Contains opigno_moxtra.module.

Code

function opigno_moxtra_cronjob_meeting_notification() {
  $org_id = _opigno_moxtra_get_organization_id();
  if (empty($org_id)) {
    return;
  }

  // Send the email notifications for the upcoming live meetings.

  /** @var \Drupal\Core\Mail\MailManagerInterface $mail_service */
  $mail_service = \Drupal::service('plugin.manager.mail');
  $timestamp = \Drupal::time()
    ->getRequestTime();
  $date_min = DrupalDateTime::createFromTimestamp($timestamp);
  $date_max = clone $date_min;
  $date_max
    ->add(new DateInterval('P1D'));
  $date_min_str = $date_min
    ->format(DrupalDateTime::FORMAT);
  $date_max_str = $date_max
    ->format(DrupalDateTime::FORMAT);
  $meetings_ids = \Drupal::entityTypeManager()
    ->getStorage('opigno_moxtra_meeting')
    ->getQuery()
    ->condition('date__value', [
    $date_min_str,
    $date_max_str,
  ], 'BETWEEN')
    ->execute();

  /** @var \Drupal\opigno_moxtra\MeetingInterface[] $meetings */
  $meetings = Meeting::loadMultiple($meetings_ids);
  foreach ($meetings as $meeting) {
    $members = $meeting
      ->getMembers();
    if (empty($members)) {
      $training = $meeting
        ->getTraining();
      if ($training !== NULL) {
        $members = array_map(function ($member) {

          /** @var \Drupal\group\GroupMembership $member */
          return $member
            ->getUser();
        }, $training
          ->getMembers());
      }
    }
    $notified = $meeting
      ->getNotifiedMembers();

    /** @var \Drupal\user\UserInterface[] $not_notified */
    $not_notified = array_udiff($members, $notified, function ($user1, $user2) {

      /** @var \Drupal\user\UserInterface $user1 */

      /** @var \Drupal\user\UserInterface $user2 */
      return $user2
        ->id() - $user1
        ->id();
    });
    $params['subject'] = $params['message'] = t('The Live Meeting %meeting starts in less than 24 hours', [
      '%meeting' => $meeting
        ->getTitle(),
    ]);
    if (\Drupal::hasService('opigno_calendar_event.iCal')) {
      $params['attachments'] = opigno_moxtra_ical_prepare($meeting);
    }
    $module = 'opigno_moxtra';
    $key = 'upcoming_meeting_notify';
    foreach ($not_notified as $user) {
      $to = $user
        ->getEmail();
      $langcode = $user
        ->getPreferredLangcode();
      $mail_service
        ->mail($module, $key, $to, $langcode, $params, NULL, TRUE);
      $meeting
        ->addNotifiedMember($user
        ->id());
      $meeting
        ->save();
    }
  }
}