ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilAccountCode.php
Go to the documentation of this file.
1 <?php
2 
23 {
24  public const DB_TABLE = 'usr_account_codes';
25  public const CODE_LENGTH = 10;
26 
27  public static function create(
28  string $valid_until,
29  int $stamp
30  ): int {
31  global $DIC;
32 
33  $ilDB = $DIC['ilDB'];
34  $code = "";
35  $id = $ilDB->nextId(self::DB_TABLE);
36 
37  // create unique code
38  $found = true;
39  while ($found) {
40  $code = self::generateRandomCode();
41  $chk = $ilDB->queryF("SELECT code_id FROM " . self::DB_TABLE . " WHERE code = %s", array("text"), array($code));
42  $found = (bool) $ilDB->numRows($chk);
43  }
44 
45  $data = array(
46  'code_id' => array('integer', $id),
47  'code' => array('text', $code),
48  'generated' => array('integer', $stamp),
49  'valid_until' => array('text', $valid_until)
50  );
51 
52  $ilDB->insert(self::DB_TABLE, $data);
53  return $id;
54  }
55 
56  protected static function generateRandomCode(): string
57  {
58  // missing : 01iloO
59  $map = "23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
60 
61  $code = "";
62  $max = strlen($map) - 1;
63  for ($loop = 1; $loop <= self::CODE_LENGTH; $loop++) {
64  $code .= $map[random_int(0, $max)];
65  }
66  return $code;
67  }
68 
69  public static function getCodesData(
70  string $order_field,
71  string $order_direction,
72  int $offset,
73  int $limit,
74  string $filter_code,
75  string $filter_valid_until,
76  string $filter_generated
77  ): array {
78  global $DIC;
79 
80  $ilDB = $DIC['ilDB'];
81 
82  // filter
83  $where = self::filterToSQL($filter_code, $filter_valid_until, $filter_generated);
84 
85  // count query
86  $set = $ilDB->query("SELECT COUNT(*) AS cnt FROM " . self::DB_TABLE . $where);
87  $cnt = 0;
88  if ($rec = $ilDB->fetchAssoc($set)) {
89  $cnt = $rec["cnt"];
90  }
91 
92  $sql = "SELECT * FROM " . self::DB_TABLE . $where;
93  if ($order_field) {
94  $sql .= " ORDER BY " . $order_field . " " . $order_direction;
95  }
96 
97  // set query
98  $ilDB->setLimit($limit, $offset);
99  $set = $ilDB->query($sql);
100  $result = array();
101  while ($rec = $ilDB->fetchAssoc($set)) {
102  $result[] = $rec;
103  }
104  return array("cnt" => $cnt, "set" => $result);
105  }
106 
110  public static function loadCodesByIds(array $ids): array
111  {
112  global $DIC;
113 
114  $ilDB = $DIC['ilDB'];
115 
116  $set = $ilDB->query("SELECT * FROM " . self::DB_TABLE . " WHERE " . $ilDB->in("code_id", $ids, false, "integer"));
117  $result = array();
118  while ($rec = $ilDB->fetchAssoc($set)) {
119  $result[] = $rec;
120  }
121  return $result;
122  }
123 
127  public static function deleteCodes(array $ids): bool
128  {
129  global $DIC;
130 
131  $ilDB = $DIC['ilDB'];
132 
133  if (count($ids)) {
134  return $ilDB->manipulate("DELETE FROM " . self::DB_TABLE . " WHERE " . $ilDB->in("code_id", $ids, false, "integer"));
135  }
136  return false;
137  }
138 
142  public static function getGenerationDates(): array
143  {
144  global $DIC;
145 
146  $ilDB = $DIC['ilDB'];
147 
148  $set = $ilDB->query("SELECT DISTINCT(generated) AS generated FROM " . self::DB_TABLE . " ORDER BY generated");
149  $result = array();
150  while ($rec = $ilDB->fetchAssoc($set)) {
151  $result[] = $rec["generated"];
152  }
153  return $result;
154  }
155 
156  protected static function filterToSQL(
157  string $filter_code,
158  string $filter_valid_until,
159  string $filter_generated
160  ): string {
161  global $DIC;
162 
163  $ilDB = $DIC['ilDB'];
164 
165  $where = array();
166  if ($filter_code) {
167  $where[] = $ilDB->like("code", "text", "%" . $filter_code . "%");
168  }
169  if ($filter_valid_until) {
170  $where[] = "valid_until = " . $ilDB->quote($filter_valid_until, "text");
171  }
172  if ($filter_generated) {
173  $where[] = "generated = " . $ilDB->quote($filter_generated, "text");
174  }
175  if (count($where)) {
176  return " WHERE " . implode(" AND ", $where);
177  } else {
178  return "";
179  }
180  }
181 
182  public static function getCodesForExport(
183  string $filter_code,
184  string $filter_valid_until,
185  string $filter_generated
186  ): array {
187  global $DIC;
188 
189  $ilDB = $DIC['ilDB'];
190 
191  // filter
192  $where = self::filterToSQL($filter_code, $filter_valid_until, $filter_generated);
193 
194  // set query
195  $set = $ilDB->query("SELECT code FROM " . self::DB_TABLE . $where . " ORDER BY code_id");
196  $result = array();
197  while ($rec = $ilDB->fetchAssoc($set)) {
198  $result[] = $rec["code"];
199  }
200  return $result;
201  }
202 
203  public static function isUnusedCode(string $code): bool
204  {
205  return ilRegistrationCode::isUnusedCode($code);
206  }
207 
208  public static function useCode(string $code): bool
209  {
210  return ilRegistrationCode::useCode($code);
211  }
212 
213  public static function getCodeValidUntil(string $code): string
214  {
215  $code_data = ilRegistrationCode::getCodeData($code);
216 
217  if ($code_data["alimit"]) {
218  switch ($code_data["alimit"]) {
219  case "absolute":
220  return $code_data['alimitdt'];
221  }
222  }
223  return "0";
224  }
225 
226  public static function applyRoleAssignments(
227  ilObjUser $user,
228  string $code
229  ): bool {
230  $recommended_content_manager = new ilRecommendedContentManager();
231 
232  $grole = ilRegistrationCode::getCodeRole($code);
233  if ($grole) {
234  $GLOBALS['DIC']['rbacadmin']->assignUser($grole, $user->getId());
235  }
236  $code_data = ilRegistrationCode::getCodeData($code);
237  if ($code_data["role_local"]) {
238  $code_local_roles = explode(";", $code_data["role_local"]);
239  foreach ($code_local_roles as $role_id) {
240  $GLOBALS['DIC']['rbacadmin']->assignUser($role_id, $user->getId());
241 
242  // patch to remove for 45 due to mantis 21953
243  $role_obj = $GLOBALS['DIC']['rbacreview']->getObjectOfRole($role_id);
244  switch (ilObject::_lookupType($role_obj)) {
245  case 'crs':
246  case 'grp':
247  $role_refs = ilObject::_getAllReferences($role_obj);
248  $role_ref = end($role_refs);
249  // deactivated for now, see discussion at
250  // https://docu.ilias.de/goto_docu_wiki_wpage_5620_1357.html
251  //$recommended_content_manager->addObjectRecommendation($user->getId(), $role_ref);
252  break;
253  }
254  }
255  }
256  return true;
257  }
258 
259  public static function applyAccessLimits(
260  ilObjUser $user,
261  string $code
262  ): void {
263  $code_data = ilRegistrationCode::getCodeData($code);
264 
265  if ($code_data["alimit"]) {
266  switch ($code_data["alimit"]) {
267  case "absolute":
268  $end = new ilDateTime($code_data['alimitdt'], IL_CAL_DATE);
269  //$user->setTimeLimitFrom(time());
270  $user->setTimeLimitUntil($end->get(IL_CAL_UNIX));
271  $user->setTimeLimitUnlimited(0);
272  break;
273 
274  case "relative":
275 
276  $rel = unserialize($code_data["alimitdt"], ["allowed_classes" => false]);
277 
278  $end = new ilDateTime(time(), IL_CAL_UNIX);
279 
280  if ($rel['y'] > 0) {
281  $end->increment(IL_CAL_YEAR, $rel['y']);
282  }
283 
284  if ($rel['m'] > 0) {
285  $end->increment(IL_CAL_MONTH, $rel['m']);
286  }
287 
288  if ($rel['d'] > 0) {
289  $end->increment(IL_CAL_DAY, $rel['d']);
290  }
291 
292  //$user->setTimeLimitFrom(time());
293  $user->setTimeLimitUntil($end->get(IL_CAL_UNIX));
294  $user->setTimeLimitUnlimited(0);
295  break;
296 
297  case 'unlimited':
298  $user->setTimeLimitUnlimited(1);
299  break;
300 
301  }
302  } else {
303  $user->setTimeLimitUnlimited(1);
304  }
305  }
306 }
setTimeLimitUnlimited(bool $a_unlimited)
static getCodeRole(string $code)
static useCode(string $code)
static applyRoleAssignments(ilObjUser $user, string $code)
static getCodeValidUntil(string $code)
static _getAllReferences(int $id)
get all reference ids for object ID
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static isUnusedCode(string $code)
static getCodesForExport(string $filter_code, string $filter_valid_until, string $filter_generated)
const IL_CAL_MONTH
const IL_CAL_UNIX
setTimeLimitUntil(?int $a_until)
static getCodeData(string $code)
global $DIC
Definition: feed.php:28
const IL_CAL_DAY
static loadCodesByIds(array $ids)
static isUnusedCode(string $code)
static deleteCodes(array $ids)
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
static getCodesData(string $order_field, string $order_direction, int $offset, int $limit, string $filter_code, string $filter_valid_until, string $filter_generated)
static filterToSQL(string $filter_code, string $filter_valid_until, string $filter_generated)
static generateRandomCode()
static create(string $valid_until, int $stamp)
const IL_CAL_DATE
static applyAccessLimits(ilObjUser $user, string $code)
static useCode(string $code)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
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...
const IL_CAL_YEAR