function opigno_ilt_cron in Opigno Instructor-led Trainings 3.x
Same name and namespace in other branches
- 8 opigno_ilt.module \opigno_ilt_cron()
Implements hook_cron().
1 string reference to 'opigno_ilt_cron'
- ultimate_cron.job.opigno_ilt_cron.yml in config/
install/ ultimate_cron.job.opigno_ilt_cron.yml - config/install/ultimate_cron.job.opigno_ilt_cron.yml
File
- ./
opigno_ilt.module, line 82 - Contains opigno_ilt.module.
Code
function opigno_ilt_cron() {
// Send the email notifications for the upcoming instructor-led training.
/** @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);
$ilts_ids = \Drupal::entityTypeManager()
->getStorage('opigno_ilt')
->getQuery()
->condition('date__value', [
$date_min_str,
$date_max_str,
], 'BETWEEN')
->execute();
/** @var \Drupal\opigno_ilt\ILTInterface[] $ilts */
$ilts = ILT::loadMultiple($ilts_ids);
foreach ($ilts as $ilt) {
$members = $ilt
->getMembers();
if (empty($members)) {
$training = $ilt
->getTraining();
if ($training !== NULL) {
$members = array_map(function ($member) {
/** @var \Drupal\group\GroupMembership $member */
return $member
->getUser();
}, $training
->getMembers());
}
}
$notified = $ilt
->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 = [];
if (\Drupal::hasService('opigno_calendar_event.iCal')) {
$params['attachments'] = opigno_ilt_ical_prepare($ilt);
}
$module = 'opigno_ilt';
$key = 'upcoming_ilt_notify';
foreach ($not_notified as $user) {
$trainer_id = $ilt
->getTrainerId();
$trainer_name = '';
if ($trainer_id) {
$trainer = \Drupal::entityTypeManager()
->getStorage('user')
->load($trainer_id);
if ($trainer) {
$trainer_name = $trainer
->getAccountName();
}
}
$date_start_str = $ilt
->getStartDate();
$date_start = DrupalDateTime::createFromFormat(DrupalDateTime::FORMAT, $date_start_str);
$date_end_str = $ilt
->getEndDate();
$date_end = DrupalDateTime::createFromFormat(DrupalDateTime::FORMAT, $date_end_str);
$site_name = \Drupal::config('system.site')
->get('name');
$params['message'] = t('Dear %username,<br /><br />
We kindly remind you that the Instructor-Led Training %ilt_title starts in less than 24 hours.<br /><br />
It will take place at: %ilt_place<br />
Start time: %ilt_start_time<br />
End time: %ilt_end_time<br />
Your trainer will be: %ilt_trainer<br /><br />
-- %site_name team', [
'%username' => $user
->getAccountName(),
'%ilt_title' => $ilt
->getTitle(),
'%ilt_place' => $ilt
->getPlace(),
'%ilt_start_time' => $date_start
->format('jS F Y - g:i A'),
'%ilt_end_time' => $date_end
->format('jS F Y - g:i A'),
'%ilt_trainer' => $trainer_name,
'%site_name' => $site_name,
]);
$params['subject'] = t('The Instructor-Led Training %ilt starts in less than 24 hours', [
'%ilt' => $ilt
->getTitle(),
]);
$to = $user
->getEmail();
$langcode = $user
->getPreferredLangcode();
$mail_service
->mail($module, $key, $to, $langcode, $params, NULL, TRUE);
$ilt
->addNotifiedMember($user
->id());
$ilt
->save();
}
}
}