ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilTimingsCronReminder.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2015 ILIAS open source, Extended GPL, see docs/LICENSE */
3
5{
6
10 protected $log;
11
15 protected $lng;
16
20 protected $user_lang;
21
25 protected $db;
26
30 protected $obj_data_cache;
31
36
40 protected $users;
41
45 protected static $objects_information;
46
50 protected static $coaches_emails;
51
55 protected $now;
56
60 public function __construct()
61 {
62 global $DIC;
63 global $ilObjDataCache, $lng, $ilDB;
64
65 $this->log = $DIC->logger()->crs();
66 $this->lng = $DIC->language();
67 $this->lng->loadLanguageModule('crs');
68 $this->db = $DIC->database();
69 $this->obj_data_cache = $DIC['ilObjDataCache'];
70
71 self::$objects_information = array();
72 self::$coaches_emails = array();
73 $this->users_with_exceeded_timings = array();
74 $this->users = array();
75 $this->now = time();
76 }
77
81 public function getId()
82 {
83 return 'crs_timings_reminder';
84 }
85
89 public function getTitle()
90 {
91 return $this->lng->txt('timings_reminder_notifications');
92 }
93
97 public function getDescription()
98 {
99 return $this->lng->txt('timings_reminder_notifications_info');
100 }
101
105 public function getDefaultScheduleType()
106 {
108 }
109
110 public function getDefaultScheduleValue()
111 {
112 return;
113 }
114
118 public function hasAutoActivation()
119 {
120 return false;
121 }
122
126 public function hasFlexibleSchedule()
127 {
128 return false;
129 }
130
134 public function hasCustomSettings()
135 {
136 return false;
137 }
138
142 public function run()
143 {
144 $this->log->debug('Start.');
145
146 $result = new ilCronJobResult();
147
148 $this->gatherUsers();
152
154
155 $this->log->debug('End');
156
157 return $result;
158 }
159
163 protected function gatherUsers()
164 {
165 $now = time();
166 $query = $this->db->queryF(
167 'SELECT usr_id FROM usr_data WHERE
168 (active = 1 AND time_limit_unlimited = 1) OR
169 (active = 1 AND time_limit_unlimited = 0 AND time_limit_from < %s AND time_limit_until > %s)',
170 array('integer', 'integer'),
171 array($now, $now)
172 );
173 while ($row = $this->db->fetchAssoc($query)) {
174 $usr_id = (int) $row['usr_id'];
175 $this->users[$usr_id] = $usr_id;
176 }
177 $this->log->debug('Found ' . count($this->users) . ' users.');
178 }
179
184 {
185 $this->users_with_exceeded_timings = ilTimingsUser::lookupTimingsExceededByUser($this->users);
186 $this->log->debug('Found ' . count($this->users_with_exceeded_timings) . ' users with exceeded timings.');
187 }
188
192 protected function getNewExceededObjectForUser()
193 {
194 $users_with_exceeded_objects = array();
195
196 if (is_array($this->users_with_exceeded_timings) && count($this->users_with_exceeded_timings) > 0) {
197 foreach ($this->users_with_exceeded_timings as $key => $user_id) {
198 $objects = $this->getExceededObjectsForUser($user_id);
199 if (is_array($objects) && count($objects) > 0) {
200 $obj_data = array();
201 $already_notified = $this->getAlreadySentNotifications($user_id);
202 $objects = array_diff_key($objects, $already_notified);
203 foreach ($objects as $ref_id => $v) {
204 $detail_data = $this->getInformationForRefId($ref_id);
205 $obj_data[$ref_id] = $detail_data;
206 }
207 if (count($obj_data) > 0) {
208 $users_with_exceeded_objects[$user_id] = $obj_data;
209 }
210 }
211 }
212 $this->log->debug('Found ' . sizeof($users_with_exceeded_objects) . ' users with new exceeded timings.');
213
214 $this->buildExceededMails($users_with_exceeded_objects);
215 }
216 }
217
222 {
223 $users_with_new_started_object = array();
224
225 if (is_array($this->users) && count($this->users) > 0) {
226 foreach ($this->users as $key => $user_id) {
227 $objects = $this->getObjectsWithTimingsForUser($user_id);
228 if (is_array($objects) && count($objects) > 0) {
229 $obj_data = array();
230 $already_notified = $this->getAlreadySentNotifications($user_id, false);
231 $this->log->debug('User_id ' . $user_id . ' was already notified for ' . sizeof($already_notified) . ' elements ');
232 $objects = array_diff_key($objects, $already_notified);
233 foreach ($objects as $ref_id => $v) {
234 $obj_data[$ref_id] = $this->getInformationForRefId($ref_id);
235
236 if (is_array($objects[$ref_id])) {
237 if ((isset($objects[$ref_id]['end']) && isset($objects[$ref_id]['start'])) && $objects[$ref_id]['end'] > $this->now) {
238 if ($objects[$ref_id]['start'] < $this->now) {
239 $users_with_new_started_object[$user_id][$ref_id] = $obj_data[$ref_id];
240 }
241 } else {
242 $this->log->debug('End is already older than today no notification send for user_id ' . $user_id . ' on ref_id ' . $ref_id);
243 }
244 }
245 }
246 }
247 }
248 $this->log->debug('Found ' . count($users_with_new_started_object) . ' users with freshly started timings.');
249
250 $this->buildFreshlyStartedMails($users_with_new_started_object);
251 }
252 }
253
257 protected function buildExceededMails($users_with_exceeded_objects)
258 {
259 $this->log->debug('Start.');
260 if (is_array($users_with_exceeded_objects)) {
261 $this->log->debug('...found ' . count($users_with_exceeded_objects));
262 foreach ($users_with_exceeded_objects as $user_id => $exceeded_objects) {
263 $tpl = $this->buildTopMailBody($user_id, 'timings_cron_reminder_exceeded_start');
264 $has_exceeded = $this->fillObjectListForMailBody($exceeded_objects, $tpl);
265
266 if ($has_exceeded) {
267 $this->sendExceededMail($user_id, $exceeded_objects, $tpl->get());
268 $this->log->debug('start sending exceeded mail to user: ' . $user_id);
269 }
270 }
271 } else {
272 $this->log->warning('no array given.');
273 }
274
275 $this->log->debug('end.');
276 }
277
281 protected function buildFreshlyStartedMails($users_with_freshly_started_objects)
282 {
283 $this->log->debug('start.');
284 if (is_array($users_with_freshly_started_objects)) {
285 $this->log->debug('...found ' . sizeof($users_with_freshly_started_objects));
286 foreach ($users_with_freshly_started_objects as $user_id => $freshly_started_objects) {
287 $tpl = $this->buildTopMailBody($user_id, 'timings_cron_reminder_freshly_start');
288 $has_freshly_started = $this->fillObjectListForMailBody($freshly_started_objects, $tpl);
289
290 if ($has_freshly_started) {
291 $this->sendFreshlyStartedMail($user_id, $freshly_started_objects, $tpl->get());
292 }
293 }
294 } else {
295 $this->log->debug('no array given.');
296 }
297
298 $this->log->debug('end.');
299 }
300
306 protected function buildTopMailBody($user_id, $language_variable)
307 {
308 $this->log->debug('start...');
309 $tpl = new ilTemplate('tpl.crs_timings_cron_reminder_mail.html', true, true, 'Modules/Course');
310
311 $this->getUserLanguage($user_id);
312 $this->buildMailSalutation($user_id, $tpl);
313 $tpl->setVariable('START_BODY', $this->user_lang->txt($language_variable));
314 $this->log->debug('for user: ' . $user_id . ' end.');
315 return $tpl;
316 }
317
323 protected function fillObjectListForMailBody($objects, $tpl)
324 {
325 $has_elements = false;
326 foreach ($objects as $object_id => $object_details) {
327 if ($object_details['type'] == 'fold') {
328 $tpl->setCurrentBlock('items');
329 $tpl->setVariable('HREF', $object_details['url']);
330 $tpl->setVariable('ITEM_TITLE', $object_details['title']);
331 $tpl->parseCurrentBlock();
332 $has_elements = true;
333 }
334 }
335
336 $tpl->setVariable('INSTALLATION_SIGNATURE', \ilMail::_getInstallationSignature());
337
338 $this->log->debug('found elements: ' . $has_elements);
339 return $has_elements;
340 }
341
345 protected function getUserLanguage($user_id)
346 {
347 $this->log->debug('start...');
348 $this->user_lang = ilLanguageFactory::_getLanguageOfUser($user_id);
349 $this->user_lang->loadLanguageModule('crs');
350 $this->user_lang->loadLanguageModule('mail');
351 $this->log->debug('user language for user ' . $user_id . ' is ' . $this->user_lang->getLangKey() . ' end.');
352 }
353
358 protected function buildMailSalutation($user_id, $tpl)
359 {
360 $name = ilObjUser::_lookupName($user_id);
361 if (is_array($name)) {
362 $salutation = $this->user_lang->txt('mail_salutation_n') . ' ';
363 if ($name['gender'] != '') {
364 $salutation .= $this->user_lang->txt('salutation_' . $name['gender']) . ' ';
365 }
366 if ($name['title'] != '') {
367 $salutation .= $name['title'] . ' ';
368 }
369 $tpl->setVariable('SALUTATION', $salutation);
370 $tpl->setVariable('FIRSTNAME', $name['firstname']);
371 $tpl->setVariable('LASTNAME', $name['lastname']);
372 $this->log->debug('Salutation: ' . $salutation . ' Firstname: ' . $name['firstname'] . ' Lastname: ' . $name['lastname']);
373 } else {
374 $this->log->debug('did not get an array from _lookupName.');
375 }
376 }
377
383 protected function sendExceededMail($user_id, $ref_ids, $mail_body)
384 {
386 if ($login != '') {
387 $mail = new ilMail(ANONYMOUS_USER_ID);
388 if ($this->hasUserActivatedNotification($user_id)) {
389 $mail->enqueue(
390 $login,
391 '',
392 '',
393 $this->user_lang->txt('timings_cron_reminder_exceeded_subject'),
394 $mail_body,
395 [],
396 true
397 );
398 $this->log->debug('...mail send for user ' . $user_id . ' to mail ' . $login . ' has exceeded timings for ' . $mail_body);
399 $this->markExceededInDatabase($user_id, $ref_ids);
400 } else {
401 $this->log->debug('... no mail was sent because user ' . $user_id . ' has deactivated their notifications and has no coaches assigned.');
402 }
403 } else {
404 $this->log->debug('Not send. User ' . $user_id . ' has no email.');
405 }
406 }
407
408
414 protected function sendFreshlyStartedMail($user_id, $ref_ids, $mail_body)
415 {
417
418 if ($login != '' && $this->hasUserActivatedNotification($user_id)) {
419 $mail = new ilMail(ANONYMOUS_USER_ID);
420 $mail->enqueue(
421 $login,
422 '',
423 '',
424 $this->user_lang->txt('timings_cron_reminder_started_subject'),
425 $mail_body,
426 [],
427 true
428 );
429 $this->log->debug('...mail send for user ' . $user_id . ' to mail ' . $login . ' has freshly started timings for ' . $mail_body);
430 $this->markFreshlyStartedInDatabase($user_id, $ref_ids);
431 } else {
432 $this->log->debug('Not send. User ' . $user_id . ' has no email.');
433 }
434 }
435
440 protected function markExceededInDatabase($user_id, $ref_ids)
441 {
442 foreach ($ref_ids as $ref_id => $data) {
443 $this->db->manipulateF(
444 'INSERT INTO ' . ilCourseConstants::CRON_TIMINGS_EXCEEDED_TABLE . ' (user_id, ref_id, sent) VALUES ' .
445 ' (%s,%s,%s)',
446 array('integer', 'integer', 'integer'),
447 array($user_id, $ref_id, $this->now)
448 );
449
450 $this->log->debug('ilTimingsCronReminder->markExceededInDatabase: Marked exceeded in Database. User ' . $user_id . ' ref_id ' . $ref_id);
451 }
452 }
453
458 protected function markFreshlyStartedInDatabase($user_id, $ref_ids)
459 {
460 foreach ($ref_ids as $ref_id => $data) {
461 $this->db->manipulateF(
462 'INSERT INTO ' . ilCourseConstants::CRON_TIMINGS_STARTED_TABLE . ' (user_id, ref_id, sent) VALUES ' .
463 ' (%s,%s,%s)',
464 array('integer', 'integer', 'integer'),
465 array($user_id, $ref_id, $this->now)
466 );
467
468 $this->log->debug('ilTimingsCronReminder->markFreshlyStartedInDatabase: Marked freshly started in Database. User ' . $user_id . ' ref_id ' . $ref_id);
469 }
470 }
471
477 protected function getAlreadySentNotifications($user_id, $for_exceeded = true)
478 {
479 $ref_ids = array();
481
482 if (!$for_exceeded) {
484 }
485
486 $result = $this->db->queryF(
487 'SELECT * FROM ' . $table . ' WHERE ' .
488 'user_id = %s',
489 array('integer'),
490 array($user_id)
491 );
492
493 while ($record = $this->db->fetchAssoc($result)) {
494 $ref_ids[$record['ref_id']] = $record['ref_id'];
495 }
496
497 return $ref_ids;
498 }
499
504 protected function getInformationForRefId($ref_id)
505 {
506 if (!array_key_exists($ref_id, self::$objects_information)) {
507 $obj_id = $this->obj_data_cache->lookupObjId($ref_id);
508 $type = $this->obj_data_cache->lookupType($obj_id);
509 $value = array( 'title' => $this->obj_data_cache->lookupTitle($obj_id),
510 'type' => $type,
511 'url' => ilLink::_getLink($ref_id, $type),
512 'obj_id' => $obj_id
513 );
514 self::$objects_information[$ref_id] = $value;
515
516 $this->log->debug('ilTimingsCronReminder->getInformationForRefId: ...cached object information for => ' . $value['type'] . ' => ' . $value['title']);
517 }
518 return self::$objects_information[$ref_id];
519 }
520
521
526 protected function getExceededObjectsForUser($user_id)
527 {
528 $exceeded_obj_list = ilTimingsUser::lookupTimings(array($user_id), $arr = array(), true, true);
529 return $exceeded_obj_list;
530 }
531
536 protected function getObjectsWithTimingsForUser($user_id)
537 {
538 $meta = array();
539 $timings_obj_list = ilTimingsUser::lookupTimings(array($user_id), $meta, false, true);
540 $meta = $meta[$user_id];
541 return $meta;
542 }
543
544 protected function hasUserActivatedNotification($user_id)
545 {
546 return true;
547 }
548}
$result
An exception for terminatinating execution or to throw for unit testing.
Cron job result data container.
Cron job application base class.
const SCHEDULE_TYPE_DAILY
static _getLanguageOfUser($a_usr_id)
Get language object of user.
static _getInstallationSignature()
static _lookupLogin($a_user_id)
lookup login
static _lookupName($a_user_id)
lookup user name
special template class to simplify handling of ITX/PEAR
getDefaultScheduleValue()
Get schedule value.
sendFreshlyStartedMail($user_id, $ref_ids, $mail_body)
gatherUsersWithExceededTimings()
Users with exceeded timings.
markExceededInDatabase($user_id, $ref_ids)
buildFreshlyStartedMails($users_with_freshly_started_objects)
getFreshlyStartedObjectsForUser()
Get freshly started objects.
markFreshlyStartedInDatabase($user_id, $ref_ids)
getNewExceededObjectForUser()
get new exceeded objects for users
getAlreadySentNotifications($user_id, $for_exceeded=true)
buildExceededMails($users_with_exceeded_objects)
gatherUsers()
Read all active users.
buildTopMailBody($user_id, $language_variable)
sendExceededMail($user_id, $ref_ids, $mail_body)
static lookupTimingsExceededByUser(array $a_user_ids)
Check if users currently exceeded ANY object.
static lookupTimings(array $a_user_ids, array &$a_meta=null, $a_only_exceeded=true)
Lookup references, users with exceeded timings.
$login
Definition: cron.php:13
if($format !==null) $name
Definition: metadata.php:230
$query
$type
global $ilDB
$data
Definition: storeScorm.php:23
if(isset($_FILES['img_file']['size']) && $_FILES['img_file']['size'] > 0) $tpl
$DIC
Definition: xapitoken.php:46