19 declare(strict_types=1);
46 $this->log = $DIC->logger()->crs();
47 $this->
lng = $DIC->language();
48 $this->
lng->loadLanguageModule(
'crs');
49 $this->db = $DIC->database();
50 $this->obj_data_cache = $DIC[
'ilObjDataCache'];
52 self::$objects_information = [];
53 $this->users_with_exceeded_timings = [];
60 return 'crs_timings_reminder';
65 return $this->
lng->txt(
'timings_reminder_notifications');
70 return $this->
lng->txt(
'timings_reminder_notifications_info');
75 return JobScheduleType::DAILY;
100 $this->log->debug(
'Start.');
109 $result->setStatus(JobResult::STATUS_OK);
111 $this->log->debug(
'End');
122 $query = $this->db->queryF(
123 'SELECT usr_id FROM usr_data WHERE 124 (active = 1 AND time_limit_unlimited = 1) OR 125 (active = 1 AND time_limit_unlimited = 0 AND time_limit_from < %s AND time_limit_until > %s)',
126 [
'integer',
'integer'],
129 while ($row = $this->db->fetchAssoc($query)) {
130 $usr_id = (
int) $row[
'usr_id'];
131 $this->users[$usr_id] = $usr_id;
133 $this->log->debug(
'Found ' . count($this->users) .
' users.');
139 $this->log->debug(
'Found ' . count($this->users_with_exceeded_timings) .
' users with exceeded timings.');
144 $users_with_exceeded_objects = [];
146 if (is_array($this->users_with_exceeded_timings) && $this->users_with_exceeded_timings !== []) {
147 foreach ($this->users_with_exceeded_timings as $key =>
$user_id) {
149 if (is_array($objects) && $objects !== []) {
152 $objects = array_diff_key($objects, $already_notified);
153 foreach (array_keys($objects) as
$ref_id) {
155 $obj_data[
$ref_id] = $detail_data;
157 if ($obj_data !== []) {
158 $users_with_exceeded_objects[
$user_id] = $obj_data;
162 $this->log->debug(
'Found ' . count($users_with_exceeded_objects) .
' users with new exceeded timings.');
170 $users_with_new_started_object = [];
172 if (is_array($this->users) && $this->users !== []) {
173 foreach ($this->users as $key =>
$user_id) {
175 if (is_array($objects) && $objects !== []) {
178 $this->log->debug(
'User_id ' .
$user_id .
' was already notified for ' . count($already_notified) .
' elements ');
179 $objects = array_diff_key($objects, $already_notified);
180 foreach ($objects as
$ref_id => $v) {
184 if ((isset($v[
'end']) && isset($v[
'start'])) && $v[
'end'] > $this->now) {
185 if ($v[
'start'] < $this->now) {
189 $this->log->debug(
'End is already older than today no notification send for user_id ' .
$user_id .
' on ref_id ' .
$ref_id);
195 $this->log->debug(
'Found ' . count($users_with_new_started_object) .
' users with freshly started timings.');
203 $this->log->debug(
'Start.');
204 if (is_array($users_with_exceeded_objects)) {
205 $this->log->debug(
'...found ' . count($users_with_exceeded_objects));
206 foreach ($users_with_exceeded_objects as
$user_id => $exceeded_objects) {
212 $this->log->debug(
'start sending exceeded mail to user: ' .
$user_id);
216 $this->log->warning(
'no array given.');
219 $this->log->debug(
'end.');
224 $this->log->debug(
'start.');
225 if (is_array($users_with_freshly_started_objects)) {
226 $this->log->debug(
'...found ' . count($users_with_freshly_started_objects));
227 foreach ($users_with_freshly_started_objects as
$user_id => $freshly_started_objects) {
231 if ($has_freshly_started) {
236 $this->log->debug(
'no array given.');
239 $this->log->debug(
'end.');
244 $this->log->debug(
'start...');
245 $tpl =
new ilTemplate(
'tpl.crs_timings_cron_reminder_mail.html',
true,
true,
'components/ILIAS/Course');
249 $tpl->setVariable(
'START_BODY', $this->user_lang->txt($language_variable));
250 $this->log->debug(
'for user: ' . $user_id .
' end.');
256 $has_elements =
false;
257 foreach ($objects as $object_id => $object_details) {
258 if ($object_details[
'type'] !=
'fold') {
261 $tpl->
setVariable(
'ITEM_TITLE', $object_details[
'title']);
263 $has_elements =
true;
267 $this->log->debug(
'found elements: ' . $has_elements);
268 return $has_elements;
273 $this->log->debug(
'start...');
275 $this->user_lang->loadLanguageModule(
'crs');
276 $this->user_lang->loadLanguageModule(
'mail');
277 $this->log->debug(
'user language for user ' . $user_id .
' is ' . $this->user_lang->getLangKey() .
' end.');
283 if (is_array($name)) {
284 $salutation = $this->user_lang->txt(
'mail_salutation_n') .
' ';
285 if (($name[
'gender'] ??
"") !=
'') {
286 $salutation .= $this->user_lang->txt(
'salutation_' . $name[
'gender']) .
' ';
288 if ($name[
'title'] !=
'') {
289 $salutation .= $name[
'title'] .
' ';
292 $tpl->
setVariable(
'FIRSTNAME', $name[
'firstname']);
294 $this->log->debug(
'Salutation: ' . $salutation .
' Firstname: ' . $name[
'firstname'] .
' Lastname: ' . $name[
'lastname']);
296 $this->log->debug(
'did not get an array from _lookupName.');
310 $this->user_lang->txt(
'timings_cron_reminder_exceeded_subject'),
315 $this->log->debug(
'...mail send for user ' . $user_id .
' to mail ' . $login .
' has exceeded timings for ' . $mail_body);
318 $this->log->debug(
'... no mail was sent because user ' . $user_id .
' has deactivated their notifications and has no coaches assigned.');
321 $this->log->debug(
'Not send. User ' . $user_id .
' has no email.');
335 $this->user_lang->txt(
'timings_cron_reminder_started_subject'),
340 $this->log->debug(
'...mail send for user ' . $user_id .
' to mail ' . $login .
' has freshly started timings for ' . $mail_body);
343 $this->log->debug(
'Not send. User ' . $user_id .
' has no email.');
349 foreach (array_keys($ref_ids) as
$ref_id) {
350 $this->db->manipulateF(
353 [
'integer',
'integer',
'integer'],
354 [$user_id, $ref_id, $this->now]
357 $this->log->debug(
'ilTimingsCronReminder->markExceededInDatabase: Marked exceeded in Database. User ' . $user_id .
' ref_id ' . $ref_id);
363 foreach (array_keys($ref_ids) as
$ref_id) {
364 $this->db->manipulateF(
367 [
'integer',
'integer',
'integer'],
368 [$user_id, $ref_id, $this->now]
371 $this->log->debug(
'ilTimingsCronReminder->markFreshlyStartedInDatabase: Marked freshly started in Database. User ' . $user_id .
' ref_id ' . $ref_id);
380 if (!$for_exceeded) {
384 $result = $this->db->queryF(
385 'SELECT * FROM ' . $table .
' WHERE ' .
391 while ($record = $this->db->fetchAssoc($result)) {
392 $ref_ids[$record[
'ref_id']] = $record[
'ref_id'];
399 if (!array_key_exists($ref_id, self::$objects_information)) {
400 $obj_id = $this->obj_data_cache->lookupObjId($ref_id);
401 $type = $this->obj_data_cache->lookupType($obj_id);
403 'title' => $this->obj_data_cache->lookupTitle($obj_id),
405 'url' => ilLink::_getLink($ref_id, $type),
408 self::$objects_information[
$ref_id] = $value;
410 $this->log->debug(
'ilTimingsCronReminder->getInformationForRefId: ...cached object information for => ' . $value[
'type'] .
' => ' . $value[
'title']);
412 return self::$objects_information[
$ref_id];
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
getUserLanguage(int $user_id)
getExceededObjectsForUser(int $user_id)
getFreshlyStartedObjectsForUser()
buildTopMailBody(int $user_id, string $language_variable)
markExceededInDatabase(int $user_id, array $ref_ids)
getObjectsWithTimingsForUser(int $user_id)
hasUserActivatedNotification(int $user_id)
getInformationForRefId(int $ref_id)
gatherUsers()
Read all active users.
static _lookupName(int $a_user_id)
lookup user name
fillObjectListForMailBody(array $objects, ilTemplate $tpl)
buildMailSalutation(int $user_id, ilTemplate $tpl)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
buildExceededMails(array $users_with_exceeded_objects)
gatherUsersWithExceededTimings()
__construct()
Constructor.
setVariable($variable, $value='')
Sets a variable value.
static _getLanguageOfUser(int $a_usr_id)
Get language object of user.
array $users_with_exceeded_timings
static lookupTimings(array $a_user_ids, ?array &$a_meta=null, bool $a_only_exceeded=true)
Lookup references, users with exceeded timings.
sendExceededMail(int $user_id, array $ref_ids, string $mail_body)
buildFreshlyStartedMails(array $users_with_freshly_started_objects)
const CRON_TIMINGS_STARTED_TABLE
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
getNewExceededObjectForUser()
getDefaultScheduleValue()
static array $objects_information
static lookupTimingsExceededByUser(array $a_user_ids)
Check if users currently exceeded ANY object.
markFreshlyStartedInDatabase(int $user_id, array $ref_ids)
sendFreshlyStartedMail(int $user_id, array $ref_ids, string $mail_body)
getAlreadySentNotifications(int $user_id, bool $for_exceeded=true)
ilObjectDataCache $obj_data_cache
const CRON_TIMINGS_EXCEEDED_TABLE
static _getInstallationSignature()
static _lookupLogin(int $a_user_id)