You are here

function opigno_learning_path_cron in Opigno Learning path 3.x

Same name and namespace in other branches
  1. 8 opigno_learning_path.module \opigno_learning_path_cron()

Sends email notifications about training expired certification.

Implements hook_cron().

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

File

./opigno_learning_path.module, line 492
Contains opigno_learning_path.module.

Code

function opigno_learning_path_cron() {
  $config = \Drupal::config('opigno_learning_path.learning_path_settings');
  $current_timestamp = time();
  $near_timestamp = strtotime('+7 day', $current_timestamp);

  // Get users trainings with soon expiration - in 7 days.
  $db_connection = \Drupal::database();
  $results = $db_connection
    ->select('user_lp_status_expire', 'lps')
    ->fields('lps', [
    'uid',
    'gid',
  ])
    ->condition('expire', $current_timestamp, '>')
    ->condition('expire', $near_timestamp, '<')
    ->condition('notified', 0)
    ->execute()
    ->fetchAll();
  if ($results) {
    $token = \Drupal::token();
    $mail_service = \Drupal::service('plugin.manager.mail');
    $subject = t('The "[group:title]" training certification is expiring on [group:expiration_date]');
    $message = $config
      ->get('opigno_learning_path_notify_user_user_certificate_expired');
    $module = 'opigno_learning_path';
    $key = 'training_expiring_certification';
    foreach ($results as $item) {
      $group = Group::load($item->gid);
      $user = User::load($item->uid);
      if ($user && $group) {
        $params['subject'] = $token
          ->replace($subject, [
          'group' => $group,
          'uid' => $item->uid,
        ]);
        $params['message'] = $token
          ->replace($message, [
          'group' => $group,
          'uid' => $item->uid,
        ]);
        $to = $user
          ->getEmail();
        $langcode = $user
          ->getPreferredLangcode();
        $sent = $mail_service
          ->mail($module, $key, $to, $langcode, $params, NULL, TRUE);
        if ($sent["result"]) {
          LPStatus::setUserNotification($item->gid, $item->uid, 1);
        }
      }
    }
  }
}