ILIAS  release_8 Revision v8.24
class.ilRegistrationCode.php
Go to the documentation of this file.
1<?php
2
20declare(strict_types=1);
21
28{
29 protected const DB_TABLE = 'reg_registration_codes';
30 public const CODE_LENGTH = 10;
31
32 public static function create(
33 int $role,
34 int $stamp,
35 array $local_roles,
36 ?string $limit,
37 ?string $limit_date,
38 bool $reg_type,
39 bool $ext_type
40 ): int {
41 global $DIC;
42
43 $ilDB = $DIC->database();
44 $id = $ilDB->nextId(self::DB_TABLE);
45
46 // create unique code
47 $found = true;
48 $code = '';
49 while ($found) {
51 $chk = $ilDB->queryF(
52 "SELECT code_id FROM " . self::DB_TABLE . " WHERE code = %s",
53 ["text"],
54 [$code]
55 );
56 $found = (bool) $ilDB->numRows($chk);
57 }
58
59 $data = [
60 'code_id' => ['integer', $id],
61 'code' => ['text', $code],
62 'generated_on' => ['integer', $stamp],
63 'role' => ['integer', $role],
64 'role_local' => ['text', implode(";", $local_roles)],
65 'alimit' => ['text', $limit],
66 'alimitdt' => ['text', $limit_date],
67 'reg_enabled' => ['integer', $reg_type],
68 'ext_enabled' => ['integer', $ext_type]
69 ];
70
71 $ilDB->insert(self::DB_TABLE, $data);
72 return $id;
73 }
74
75 protected static function generateRandomCode(): string
76 {
77 // missing : 01iloO
78 $map = "23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
79
80 $code = "";
81 $max = strlen($map) - 1;
82 for ($loop = 1; $loop <= self::CODE_LENGTH; $loop++) {
83 $code .= $map[random_int(0, $max)];
84 }
85 return $code;
86 }
87
88 public static function getCodesData(
89 string $order_field,
90 string $order_direction,
91 int $offset,
92 int $limit,
93 string $filter_code,
94 int $filter_role,
95 string $filter_generated,
96 string $filter_access_limitation
97 ): array {
98 global $DIC;
99
100 $ilDB = $DIC->database();
101
102 // filter
103 $where = self::filterToSQL($filter_code, $filter_role, $filter_generated, $filter_access_limitation);
104
105 // count query
106 $set = $ilDB->query("SELECT COUNT(*) AS cnt FROM " . self::DB_TABLE . $where);
107 $cnt = 0;
108 if ($rec = $ilDB->fetchAssoc($set)) {
109 $cnt = $rec["cnt"];
110 }
111
112 $sql = "SELECT * FROM " . self::DB_TABLE . $where;
113 if ($order_field) {
114 if ($order_field === 'generated') {
115 $order_field = 'generated_on';
116 }
117 $sql .= " ORDER BY " . $order_field . " " . $order_direction;
118 }
119
120 // set query
121 $ilDB->setLimit($limit, $offset);
122 $set = $ilDB->query($sql);
123 $result = [];
124 while ($rec = $ilDB->fetchAssoc($set)) {
125 $rec['generated'] = $rec['generated_on'];
126 $result[] = $rec;
127 }
128 return ["cnt" => $cnt, "set" => $result];
129 }
130
131 public static function loadCodesByIds(array $ids): array
132 {
133 global $DIC;
134
135 $ilDB = $DIC->database();
136
137 $set = $ilDB->query("SELECT * FROM " . self::DB_TABLE . " WHERE " . $ilDB->in(
138 "code_id",
139 $ids,
140 false,
141 "integer"
142 ));
143 $result = [];
144 while ($rec = $ilDB->fetchAssoc($set)) {
145 $result[] = $rec;
146 }
147 return $result;
148 }
149
150 public static function deleteCodes(array $ids): bool
151 {
152 global $DIC;
153
154 $ilDB = $DIC->database();
155 if (count($ids)) {
156 return (bool) $ilDB->manipulate("DELETE FROM " . self::DB_TABLE . " WHERE " . $ilDB->in(
157 "code_id",
158 $ids,
159 false,
160 "integer"
161 ));
162 }
163 return false;
164 }
165
166 public static function getGenerationDates(): array
167 {
168 global $DIC;
169
170 $ilDB = $DIC->database();
171
172 $set = $ilDB->query("SELECT DISTINCT(generated_on) genr FROM " . self::DB_TABLE . " ORDER BY genr");
173 $result = [];
174 while ($rec = $ilDB->fetchAssoc($set)) {
175 $result[] = $rec["genr"];
176 }
177 return $result;
178 }
179
180 private static function filterToSQL(
181 string $filter_code,
182 ?int $filter_role,
183 string $filter_generated,
184 string $filter_access_limitation
185 ): string {
186 global $DIC;
187
188 $ilDB = $DIC['ilDB'];
189
190 $where = [];
191 if ($filter_code) {
192 $where[] = $ilDB->like("code", "text", "%" . $filter_code . "%");
193 }
194 if ($filter_role) {
195 $where[] = "role = " . $ilDB->quote($filter_role, "integer");
196 }
197 if ($filter_generated) {
198 $where[] = "generated_on = " . $ilDB->quote($filter_generated, "text");
199 }
200 if ($filter_access_limitation) {
201 $where[] = "alimit = " . $ilDB->quote($filter_access_limitation, "text");
202 }
203 if (count($where)) {
204 return " WHERE " . implode(" AND ", $where);
205 }
206
207 return "";
208 }
209
210 public static function getCodesForExport(
211 string $filter_code,
212 ?int $filter_role,
213 string $filter_generated,
214 string $filter_access_limitation
215 ): array {
216 global $DIC;
217
218 $ilDB = $DIC->database();
219
220 // filter
221 $where = self::filterToSQL($filter_code, $filter_role, $filter_generated, $filter_access_limitation);
222
223 // set query
224 $set = $ilDB->query("SELECT code FROM " . self::DB_TABLE . $where . " ORDER BY code_id");
225 $result = [];
226 while ($rec = $ilDB->fetchAssoc($set)) {
227 $result[] = $rec["code"];
228 }
229 return $result;
230 }
231
232 public static function isUnusedCode(string $code): bool
233 {
234 global $DIC;
235
236 $ilDB = $DIC['ilDB'];
237
238 $set = $ilDB->query("SELECT used FROM " . self::DB_TABLE . " WHERE code = " . $ilDB->quote($code, "text"));
239 $set = $ilDB->fetchAssoc($set);
240 return $set && !$set["used"];
241 }
242
243 public static function isValidRegistrationCode(string $a_code): bool
244 {
245 global $DIC;
246
247 $ilDB = $DIC->database();
248
249 $query = 'SELECT alimit, alimitdt FROM reg_registration_codes ' .
250 'WHERE used = ' . $ilDB->quote(0, 'integer') . ' ' .
251 'AND reg_enabled = ' . $ilDB->quote(1, 'integer') . ' ' .
252 'AND code = ' . $ilDB->quote($a_code, 'text');
253 $res = $ilDB->query($query);
254 if ($ilDB->numRows($res) !== 1) {
255 return false;
256 }
257
258 $is_valid = true;
259
260 $row = $ilDB->fetchAssoc($res);
261 if ($row['alimit'] === 'absolute') {
262 $clock_factory = (new \ILIAS\Data\Factory())->clock();
263 $right_interval = new DateTimeImmutable(
264 $row['alimitdt'],
265 $clock_factory->system()->now()->getTimezone()
266 );
267
268 $is_valid = $right_interval >= $clock_factory->system()->now();
269 }
270
271 return $is_valid;
272 }
273
274 public static function useCode(string $code): bool
275 {
276 global $DIC;
277
278 $ilDB = $DIC->database();
279 return (bool) $ilDB->update(
280 self::DB_TABLE,
281 ["used" => ["timestamp", time()]],
282 ["code" => ["text", $code]]
283 );
284 }
285
286 public static function getCodeRole(string $code): int
287 {
288 global $DIC;
289
290 $ilDB = $DIC->database();
291 $set = $ilDB->query("SELECT role FROM " . self::DB_TABLE . " WHERE code = " . $ilDB->quote($code, "text"));
292 $row = $ilDB->fetchAssoc($set);
293 if (isset($row["role"])) {
294 return (int) $row["role"];
295 }
296 return 0;
297 }
298
299 public static function getCodeData(string $code): array
300 {
301 global $DIC;
302
303 $ilDB = $DIC->database();
304 $set = $ilDB->query("SELECT role, role_local, alimit, alimitdt, reg_enabled, ext_enabled" .
305 " FROM " . self::DB_TABLE .
306 " WHERE code = " . $ilDB->quote($code, "text"));
307 return $ilDB->fetchAssoc($set);
308 }
309}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Class ilRegistrationCode.
static deleteCodes(array $ids)
static create(int $role, int $stamp, array $local_roles, ?string $limit, ?string $limit_date, bool $reg_type, bool $ext_type)
static filterToSQL(string $filter_code, ?int $filter_role, string $filter_generated, string $filter_access_limitation)
static useCode(string $code)
static isUnusedCode(string $code)
static getCodeRole(string $code)
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)
static getCodeData(string $code)
static isValidRegistrationCode(string $a_code)
static loadCodesByIds(array $ids)
global $DIC
Definition: feed.php:28
$res
Definition: ltiservices.php:69
$query