ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilRegistrationCode.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
27 {
28  protected const DB_TABLE = 'reg_registration_codes';
29  public const CODE_LENGTH = 10;
30 
31  public static function create(
32  int $role,
33  int $stamp,
34  array $local_roles,
35  ?string $limit,
36  ?string $limit_date,
37  bool $reg_type,
38  bool $ext_type
39  ): int {
40  global $DIC;
41 
42  $ilDB = $DIC->database();
43  $id = $ilDB->nextId(self::DB_TABLE);
44 
45  // create unique code
46  $found = true;
47  $code = '';
48  while ($found) {
49  $code = self::generateRandomCode();
50  $chk = $ilDB->queryF(
51  "SELECT code_id FROM " . self::DB_TABLE . " WHERE code = %s",
52  ["text"],
53  [$code]
54  );
55  $found = (bool) $ilDB->numRows($chk);
56  }
57 
58  $data = [
59  'code_id' => ['integer', $id],
60  'code' => ['text', $code],
61  'generated_on' => ['integer', $stamp],
62  'role' => ['integer', $role],
63  'role_local' => ['text', implode(";", $local_roles)],
64  'alimit' => ['text', $limit],
65  'alimitdt' => ['text', $limit_date],
66  'reg_enabled' => ['integer', $reg_type],
67  'ext_enabled' => ['integer', $ext_type]
68  ];
69 
70  $ilDB->insert(self::DB_TABLE, $data);
71  return $id;
72  }
73 
74  protected static function generateRandomCode(): string
75  {
76  // missing : 01iloO
77  $map = "23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
78 
79  $code = "";
80  $max = strlen($map) - 1;
81  for ($loop = 1; $loop <= self::CODE_LENGTH; $loop++) {
82  $code .= $map[random_int(0, $max)];
83  }
84  return $code;
85  }
86 
87  public static function getCodesData(
88  string $order_field,
89  string $order_direction,
90  int $offset,
91  int $limit,
92  string $filter_code,
93  int $filter_role,
94  string $filter_generated,
95  string $filter_access_limitation
96  ): array {
97  global $DIC;
98 
99  $ilDB = $DIC->database();
100 
101  // filter
102  $where = self::filterToSQL($filter_code, $filter_role, $filter_generated, $filter_access_limitation);
103 
104  // count query
105  $set = $ilDB->query("SELECT COUNT(*) AS cnt FROM " . self::DB_TABLE . $where);
106  $cnt = 0;
107  if ($rec = $ilDB->fetchAssoc($set)) {
108  $cnt = $rec["cnt"];
109  }
110 
111  $sql = "SELECT * FROM " . self::DB_TABLE . $where;
112  if ($order_field) {
113  if ($order_field === 'generated') {
114  $order_field = 'generated_on';
115  }
116  $sql .= " ORDER BY " . $order_field . " " . $order_direction;
117  }
118 
119  // set query
120  $ilDB->setLimit($limit, $offset);
121  $set = $ilDB->query($sql);
122  $result = [];
123  while ($rec = $ilDB->fetchAssoc($set)) {
124  $rec['generated'] = $rec['generated_on'];
125  $result[] = $rec;
126  }
127  return ["cnt" => $cnt, "set" => $result];
128  }
129 
130  public static function loadCodesByIds(array $ids): array
131  {
132  global $DIC;
133 
134  $ilDB = $DIC->database();
135 
136  $set = $ilDB->query("SELECT * FROM " . self::DB_TABLE . " WHERE " . $ilDB->in(
137  "code_id",
138  $ids,
139  false,
140  "integer"
141  ));
142  $result = [];
143  while ($rec = $ilDB->fetchAssoc($set)) {
144  $result[] = $rec;
145  }
146  return $result;
147  }
148 
149  public static function deleteCodes(array $ids): bool
150  {
151  global $DIC;
152 
153  $ilDB = $DIC->database();
154  if (count($ids)) {
155  return (bool) $ilDB->manipulate("DELETE FROM " . self::DB_TABLE . " WHERE " . $ilDB->in(
156  "code_id",
157  $ids,
158  false,
159  "integer"
160  ));
161  }
162  return false;
163  }
164 
165  public static function getGenerationDates(): array
166  {
167  global $DIC;
168 
169  $ilDB = $DIC->database();
170 
171  $set = $ilDB->query("SELECT DISTINCT(generated_on) genr FROM " . self::DB_TABLE . " ORDER BY genr");
172  $result = [];
173  while ($rec = $ilDB->fetchAssoc($set)) {
174  $result[] = $rec["genr"];
175  }
176  return $result;
177  }
178 
179  private static function filterToSQL(
180  string $filter_code,
181  ?int $filter_role,
182  string $filter_generated,
183  string $filter_access_limitation
184  ): string {
185  global $DIC;
186 
187  $ilDB = $DIC['ilDB'];
188 
189  $where = [];
190  if ($filter_code) {
191  $where[] = $ilDB->like("code", "text", "%" . $filter_code . "%");
192  }
193  if ($filter_role) {
194  $where[] = "role = " . $ilDB->quote($filter_role, "integer");
195  }
196  if ($filter_generated) {
197  $where[] = "generated_on = " . $ilDB->quote($filter_generated, "text");
198  }
199  if ($filter_access_limitation) {
200  $where[] = "alimit = " . $ilDB->quote($filter_access_limitation, "text");
201  }
202  if (count($where)) {
203  return " WHERE " . implode(" AND ", $where);
204  }
205 
206  return "";
207  }
208 
209  public static function getCodesForExport(
210  string $filter_code,
211  ?int $filter_role,
212  string $filter_generated,
213  string $filter_access_limitation
214  ): array {
215  global $DIC;
216 
217  $ilDB = $DIC->database();
218 
219  // filter
220  $where = self::filterToSQL($filter_code, $filter_role, $filter_generated, $filter_access_limitation);
221 
222  // set query
223  $set = $ilDB->query("SELECT code FROM " . self::DB_TABLE . $where . " ORDER BY code_id");
224  $result = [];
225  while ($rec = $ilDB->fetchAssoc($set)) {
226  $result[] = $rec["code"];
227  }
228  return $result;
229  }
230 
231  public static function isUnusedCode(string $code): bool
232  {
233  global $DIC;
234 
235  $ilDB = $DIC['ilDB'];
236 
237  $set = $ilDB->query("SELECT used FROM " . self::DB_TABLE . " WHERE code = " . $ilDB->quote($code, "text"));
238  $set = $ilDB->fetchAssoc($set);
239  return $set && !$set["used"];
240  }
241 
242  public static function isValidRegistrationCode(string $a_code): bool
243  {
244  global $DIC;
245 
246  $ilDB = $DIC->database();
247 
248  $query = 'SELECT alimit, alimitdt FROM reg_registration_codes ' .
249  'WHERE used = ' . $ilDB->quote(0, 'integer') . ' ' .
250  'AND reg_enabled = ' . $ilDB->quote(1, 'integer') . ' ' .
251  'AND code = ' . $ilDB->quote($a_code, 'text');
252  $res = $ilDB->query($query);
253  if ($ilDB->numRows($res) !== 1) {
254  return false;
255  }
256 
257  $is_valid = true;
258 
259  $row = $ilDB->fetchAssoc($res);
260  if ($row['alimit'] === 'absolute') {
261  $clock_factory = (new \ILIAS\Data\Factory())->clock();
262  $right_interval = new DateTimeImmutable(
263  $row['alimitdt'],
264  $clock_factory->system()->now()->getTimezone()
265  );
266 
267  $is_valid = $right_interval >= $clock_factory->system()->now();
268  }
269 
270  return $is_valid;
271  }
272 
273  public static function getCodeValidUntil(string $code): string
274  {
275  $code_data = self::getCodeData($code);
276 
277  if ($code_data["alimit"]) {
278  switch ($code_data["alimit"]) {
279  case "absolute":
280  return $code_data['alimitdt'];
281  }
282  }
283  return "0";
284  }
285 
286  public static function useCode(string $code): bool
287  {
288  global $DIC;
289 
290  $ilDB = $DIC->database();
291  return (bool) $ilDB->update(
292  self::DB_TABLE,
293  ["used" => ["timestamp", time()]],
294  ["code" => ["text", $code]]
295  );
296  }
297 
298  public static function getCodeRole(string $code): int
299  {
300  global $DIC;
301 
302  $ilDB = $DIC->database();
303  $set = $ilDB->query("SELECT role FROM " . self::DB_TABLE . " WHERE code = " . $ilDB->quote($code, "text"));
304  $row = $ilDB->fetchAssoc($set);
305  if (isset($row["role"])) {
306  return (int) $row["role"];
307  }
308  return 0;
309  }
310 
311  public static function getCodeData(string $code): array
312  {
313  global $DIC;
314 
315  $ilDB = $DIC->database();
316  $set = $ilDB->query("SELECT role, role_local, alimit, alimitdt, reg_enabled, ext_enabled" .
317  " FROM " . self::DB_TABLE .
318  " WHERE code = " . $ilDB->quote($code, "text"));
319  return $ilDB->fetchAssoc($set);
320  }
321 
322  public static function applyRoleAssignments(
323  ilObjUser $user,
324  string $code
325  ): bool {
326  $recommended_content_manager = new ilRecommendedContentManager();
327 
328  $grole = self::getCodeRole($code);
329  if ($grole) {
330  $GLOBALS['DIC']['rbacadmin']->assignUser($grole, $user->getId());
331  }
332  $code_data = self::getCodeData($code);
333  if ($code_data["role_local"]) {
334  $code_local_roles = explode(";", $code_data["role_local"]);
335  foreach ($code_local_roles as $role_id) {
336  $GLOBALS['DIC']['rbacadmin']->assignUser($role_id, $user->getId());
337 
338  // patch to remove for 45 due to mantis 21953
339  $role_obj = $GLOBALS['DIC']['rbacreview']->getObjectOfRole($role_id);
340  switch (ilObject::_lookupType($role_obj)) {
341  case 'crs':
342  case 'grp':
343  $role_refs = ilObject::_getAllReferences($role_obj);
344  $role_ref = end($role_refs);
345  // deactivated for now, see discussion at
346  // https://docu.ilias.de/goto_docu_wiki_wpage_5620_1357.html
347  //$recommended_content_manager->addObjectRecommendation($user->getId(), $role_ref);
348  break;
349  }
350  }
351  }
352  return true;
353  }
354 
355  public static function applyAccessLimits(
356  ilObjUser $user,
357  string $code
358  ): void {
359  $code_data = self::getCodeData($code);
360 
361  if ($code_data["alimit"]) {
362  switch ($code_data["alimit"]) {
363  case "absolute":
364  $end = new ilDateTime($code_data['alimitdt'], IL_CAL_DATE);
365  //$user->setTimeLimitFrom(time());
366  $user->setTimeLimitUntil($end->get(IL_CAL_UNIX));
367  $user->setTimeLimitUnlimited(false);
368  break;
369 
370  case "relative":
371 
372  $rel = unserialize($code_data["alimitdt"], ["allowed_classes" => false]);
373 
374  $end = new ilDateTime(time(), IL_CAL_UNIX);
375 
376  if ($rel['y'] > 0) {
377  $end->increment(IL_CAL_YEAR, (int) $rel['y']);
378  }
379 
380  if ($rel['m'] > 0) {
381  $end->increment(IL_CAL_MONTH, (int) $rel['m']);
382  }
383 
384  if ($rel['d'] > 0) {
385  $end->increment(IL_CAL_DAY, (int) $rel['d']);
386  }
387 
388  //$user->setTimeLimitFrom(time());
389  $user->setTimeLimitUntil($end->get(IL_CAL_UNIX));
390  $user->setTimeLimitUnlimited(false);
391  break;
392 
393  case 'unlimited':
394  $user->setTimeLimitUnlimited(true);
395  break;
396  }
397  } else {
398  $user->setTimeLimitUnlimited(true);
399  }
400  }
401 }
setTimeLimitUnlimited(bool $a_unlimited)
$res
Definition: ltiservices.php:69
static getCodeRole(string $code)
static getCodeValidUntil(string $code)
static deleteCodes(array $ids)
static _getAllReferences(int $id)
get all reference ids for object ID
const IL_CAL_MONTH
const IL_CAL_UNIX
setTimeLimitUntil(?int $a_until)
static getCodeData(string $code)
const IL_CAL_DAY
$GLOBALS["DIC"]
Definition: wac.php:30
static loadCodesByIds(array $ids)
static isValidRegistrationCode(string $a_code)
Class ilRegistrationCode.
static isUnusedCode(string $code)
global $DIC
Definition: shib_login.php:25
static getCodesData(string $order_field, string $order_direction, int $offset, int $limit, string $filter_code, int $filter_role, string $filter_generated, string $filter_access_limitation)
static getCodesForExport(string $filter_code, ?int $filter_role, string $filter_generated, string $filter_access_limitation)
const IL_CAL_DATE
static create(int $role, int $stamp, array $local_roles, ?string $limit, ?string $limit_date, bool $reg_type, bool $ext_type)
static useCode(string $code)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:24
static applyRoleAssignments(ilObjUser $user, string $code)
static _lookupType(int $id, bool $reference=false)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static applyAccessLimits(ilObjUser $user, string $code)
const IL_CAL_YEAR
static filterToSQL(string $filter_code, ?int $filter_role, string $filter_generated, string $filter_access_limitation)