ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilUserCronCheckAccounts.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
28 {
29  protected int $counter = 0;
30 
31  private ilDBInterface $db;
32  private ilLanguage $lng;
34 
35  public function __construct()
36  {
38  global $DIC;
39 
40  if (isset($DIC['ilDB'])) {
41  $this->db = $DIC['ilDB'];
42  }
43 
44  if (isset($DIC['lng'])) {
45  $this->lng = $DIC['lng'];
46  }
47 
48  if (isset($DIC['ilDB'])) {
49  $this->log = $DIC['ilLog'];
50  }
51  }
52 
53  public function getId(): string
54  {
55  return 'user_check_accounts';
56  }
57 
58  public function getTitle(): string
59  {
60  return $this->lng->txt('check_user_accounts');
61  }
62 
63  public function getDescription(): string
64  {
65  return $this->lng->txt('check_user_accounts_desc');
66  }
67 
69  {
70  return CronJobScheduleType::SCHEDULE_TYPE_DAILY;
71  }
72 
73  public function getDefaultScheduleValue(): ?int
74  {
75  return null;
76  }
77 
78  public function hasAutoActivation(): bool
79  {
80  return false;
81  }
82 
83  public function hasFlexibleSchedule(): bool
84  {
85  return false;
86  }
87 
88  public function run(): ilCronJobResult
89  {
91 
92  $now = time();
93  $two_weeks_in_seconds = $now + (60 * 60 * 24 * 14); // #14630
94 
95  // all users who are currently active and expire in the next 2 weeks
96  $query = 'SELECT usr_id, login, time_limit_until ' .
97  'FROM usr_data ' .
98  'WHERE time_limit_message = "0" ' .
99  'AND time_limit_unlimited = "0" ' .
100  'AND time_limit_from < ' . $this->db->quote($now, 'integer') . ' ' .
101  'AND time_limit_until > ' . $this->db->quote($now, 'integer') . ' ' .
102  'AND time_limit_until < ' . $this->db->quote($two_weeks_in_seconds, 'integer');
103 
104  $res = $this->db->query($query);
105 
106  while ($row = $this->db->fetchObject($res)) {
107  $expires = $row->time_limit_until;
108  $login = $row->login;
109  $usr_id = $row->usr_id;
110 
112  $lng->loadLanguageModule('mail');
113 
114  $salutation = ilMail::getSalutation($usr_id, $lng);
115 
116  $body = $salutation . "\n\n";
117  $body .= sprintf(
118  $lng->txt('account_expires_body'),
119  $login,
120  ILIAS_HTTP_PATH,
121  date('Y-m-d H:i', $expires)
122  );
123 
124  $body .= "\n\n" . ilMail::_getAutoGeneratedMessageString($lng);
125 
126  // force email
127  $mail = new ilMail(ANONYMOUS_USER_ID);
128  $mail->enqueue(
129  ilObjUser::_lookupEmail($usr_id),
130  '',
131  '',
132  $lng->txt('account_expires_subject'),
133  $body,
134  []
135  );
136 
137  // set status 'mail sent'
138  $query = 'UPDATE usr_data SET time_limit_message = "1" WHERE usr_id = "' . $usr_id . '"';
139  $this->db->query($query);
140 
141  // Send log message
142  $this->log->write('Cron: (checkUserAccounts()) sent message to ' . $login . '.');
143 
144  $this->counter++;
145  }
146 
148 
149  if ($this->counter) {
150  $status = ilCronJobResult::STATUS_OK;
151  }
152  $result = new ilCronJobResult();
153  $result->setStatus($status);
154  return $result;
155  }
156 
157  protected function checkNotConfirmedUserAccounts(): void
158  {
159  $registration_settings = new ilRegistrationSettings();
160 
161  $query = 'SELECT usr_id FROM usr_data '
162  . 'WHERE (reg_hash IS NOT NULL AND reg_hash != %s)'
163  . 'AND active = %s '
164  . 'AND create_date < %s';
165  $res = $this->db->queryF(
166  $query,
167  ['text', 'integer', 'timestamp'],
168  ['', 0, date('Y-m-d H:i:s', time() - $registration_settings->getRegistrationHashLifetime())]
169  );
170  while ($row = $this->db->fetchAssoc($res)) {
171  $user = ilObjectFactory::getInstanceByObjId((int) $row['usr_id']);
172  $user->delete();
173  $this->log->write('Cron: Deleted ' . $user->getLogin() . ' [' . $user->getId() . '] ' . __METHOD__);
174 
175  $this->counter++;
176  }
177  }
178 }
$res
Definition: ltiservices.php:69
const ANONYMOUS_USER_ID
Definition: constants.php:27
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
Component logger with individual log levels by component id.
loadLanguageModule(string $a_module)
Load language module.
static _getLanguageOfUser(int $a_usr_id)
Get language object of user.
final const STATUS_NO_ACTION
static getSalutation(int $a_usr_id, ?ilLanguage $a_language=null)
global $DIC
Definition: shib_login.php:25
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
Class ilObjAuthSettingsGUI.
This cron send notifications about expiring user accounts.
__construct(Container $dic, ilPlugin $plugin)
static _getAutoGeneratedMessageString(ilLanguage $lang=null)
static _lookupEmail(int $a_user_id)