20 declare(strict_types=1);
29 protected const DB_TABLE =
'reg_registration_codes';
43 $ilDB = $DIC->database();
50 $code = self::generateRandomCode();
52 "SELECT code_id FROM " . self::DB_TABLE .
" WHERE code = %s",
56 $found = (bool)
$ilDB->numRows($chk);
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]
78 $map =
"23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
81 $max = strlen($map) - 1;
82 for ($loop = 1; $loop <= self::CODE_LENGTH; $loop++) {
83 $code .= $map[random_int(0, $max)];
90 string $order_direction,
95 string $filter_generated,
96 string $filter_access_limitation
100 $ilDB = $DIC->database();
103 $where = self::filterToSQL($filter_code, $filter_role, $filter_generated, $filter_access_limitation);
106 $set =
$ilDB->query(
"SELECT COUNT(*) AS cnt FROM " . self::DB_TABLE . $where);
108 if ($rec =
$ilDB->fetchAssoc($set)) {
112 $sql =
"SELECT * FROM " . self::DB_TABLE . $where;
114 if ($order_field ===
'generated') {
115 $order_field =
'generated_on';
117 $sql .=
" ORDER BY " . $order_field .
" " . $order_direction;
121 $ilDB->setLimit($limit, $offset);
122 $set =
$ilDB->query($sql);
124 while ($rec =
$ilDB->fetchAssoc($set)) {
125 $rec[
'generated'] = $rec[
'generated_on'];
128 return [
"cnt" => $cnt,
"set" => $result];
135 $ilDB = $DIC->database();
137 $set =
$ilDB->query(
"SELECT * FROM " . self::DB_TABLE .
" WHERE " .
$ilDB->in(
144 while ($rec =
$ilDB->fetchAssoc($set)) {
154 $ilDB = $DIC->database();
156 return (
bool)
$ilDB->manipulate(
"DELETE FROM " . self::DB_TABLE .
" WHERE " .
$ilDB->in(
170 $ilDB = $DIC->database();
172 $set =
$ilDB->query(
"SELECT DISTINCT(generated_on) genr FROM " . self::DB_TABLE .
" ORDER BY genr");
174 while ($rec =
$ilDB->fetchAssoc($set)) {
175 $result[] = $rec[
"genr"];
183 string $filter_generated,
184 string $filter_access_limitation
188 $ilDB = $DIC[
'ilDB'];
192 $where[] =
$ilDB->like(
"code",
"text",
"%" . $filter_code .
"%");
195 $where[] =
"role = " .
$ilDB->quote($filter_role,
"integer");
197 if ($filter_generated) {
198 $where[] =
"generated_on = " .
$ilDB->quote($filter_generated,
"text");
200 if ($filter_access_limitation) {
201 $where[] =
"alimit = " .
$ilDB->quote($filter_access_limitation,
"text");
204 return " WHERE " . implode(
" AND ", $where);
213 string $filter_generated,
214 string $filter_access_limitation
218 $ilDB = $DIC->database();
221 $where = self::filterToSQL($filter_code, $filter_role, $filter_generated, $filter_access_limitation);
224 $set =
$ilDB->query(
"SELECT code FROM " . self::DB_TABLE . $where .
" ORDER BY code_id");
226 while ($rec =
$ilDB->fetchAssoc($set)) {
227 $result[] = $rec[
"code"];
236 $ilDB = $DIC[
'ilDB'];
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"];
247 $ilDB = $DIC->database();
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');
261 if ($row[
'alimit'] ===
'absolute') {
262 $clock_factory = (new \ILIAS\Data\Factory())->clock();
265 $clock_factory->system()->now()->getTimezone()
268 $is_valid = $right_interval >= $clock_factory->system()->now();
274 public static function useCode(
string $code): bool
278 $ilDB = $DIC->database();
279 return (
bool)
$ilDB->update(
281 [
"used" => [
"timestamp", time()]],
282 [
"code" => [
"text", $code]]
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"];
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);
static getCodeRole(string $code)
static deleteCodes(array $ids)
static getGenerationDates()
static getCodeData(string $code)
static loadCodesByIds(array $ids)
static isValidRegistrationCode(string $a_code)
Class ilRegistrationCode.
static isUnusedCode(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 create(int $role, int $stamp, array $local_roles, ?string $limit, ?string $limit_date, bool $reg_type, bool $ext_type)
static useCode(string $code)
static generateRandomCode()
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
static filterToSQL(string $filter_code, ?int $filter_role, string $filter_generated, string $filter_access_limitation)