ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilMembershipRegistrationCodeUtils.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
28{
29 protected const CODE_LENGTH = 10;
30
31 public static function handleCode(int $a_ref_id, string $a_type, string $a_code): void
32 {
33 global $DIC;
34 $main_tpl = $DIC->ui()->mainTemplate();
35
36 $lng = $DIC->language();
37 $tree = $DIC->repositoryTree();
38
39 $lng->loadLanguageModule($a_type);
40 try {
41 self::useCode($a_code, $a_ref_id);
43 $main_tpl->setOnScreenMessage('success', sprintf($lng->txt($a_type . "_admission_link_success_registration"), $title), true);
44 ilUtil::redirect(ilLink::_getLink($a_ref_id));
46 switch ($e->getCode()) {
48 $main_tpl->setOnScreenMessage('success', $e->getMessage(), true);
49 break;
51 $main_tpl->setOnScreenMessage('failure', $lng->txt($a_type . "_admission_link_failure_membership_limited"), true);
52 break;
54 $main_tpl->setOnScreenMessage('failure', $lng->txt($a_type . "_admission_link_failure_registration_period"), true);
55 break;
57 $main_tpl->setOnScreenMessage('failure', $lng->txt($a_type . "_admission_link_failure_invalid_code"), true);
58 break;
60 $main_tpl->setOnScreenMessage('failure', $lng->txt($a_type . '_admission_link_failure_offline'), true);
61 break;
63 $main_tpl->setOnScreenMessage('failure', $lng->txt($a_type . '_admission_link_failure_availability'), true);
64 break;
65 default:
66 $main_tpl->setOnScreenMessage('failure', $e->getMessage(), true);
67 break;
68 }
69 $parent_id = $tree->getParentId($a_ref_id);
70 ilUtil::redirect(ilLink::_getLink($parent_id));
71 }
72 }
73
83 protected static function useCode(string $a_code, int $a_endnode): void
84 {
85 global $DIC;
86
87 $tree = $DIC->repositoryTree();
88 $ilUser = $DIC->user();
89
90 $obj_ids = self::lookupObjectsByCode($a_code);
91
92 if (!$obj_ids) {
94 'Admission code is not valid',
96 );
97 }
98
99 foreach ($tree->getPathId($a_endnode) as $ref_id) {
100 if (in_array(ilObject::_lookupObjId($ref_id), $obj_ids)) {
101 $member_obj = ilObjectFactory::getInstanceByRefId($ref_id, false);
102 if ($member_obj instanceof ilObjCourse) {
103 $member_obj->register($ilUser->getId(), ilCourseConstants::CRS_MEMBER);
104 }
105 if ($member_obj instanceof ilObjGroup) {
106 $member_obj->register($ilUser->getId(), ilParticipants::IL_GRP_MEMBER, true);
107 }
108 }
109 }
110 }
111
115 public static function generateCode(): string
116 {
117 // missing : 01iloO
118 $map = "23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
119
120 $code = "";
121 $max = strlen($map) - 1;
122 for ($loop = 1; $loop <= self::CODE_LENGTH; $loop++) {
123 $code .= $map[random_int(0, $max)];
124 }
125 return $code;
126 }
127
132 protected static function lookupObjectsByCode(string $a_code): array
133 {
134 return array_merge(
137 );
138 }
139}
Stores registration keys for key based registration on courses and groups.
static generateCode()
Generate new registration key.
static handleCode(int $a_ref_id, string $a_type, string $a_code)
static useCode(string $a_code, int $a_endnode)
Use a registration code and assign the logged in user to the (parent) course/group that offer the cod...
static lookupObjectsByCode(string $a_code)
Get all objects with enabled access codes.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static lookupObjectsByCode(string $a_code)
Class ilObjGroup.
static lookupObjectsByCode(string $a_code)
static getInstanceByRefId(int $ref_id, bool $stop_on_error=true)
get an instance of an Ilias object by reference id
static _lookupObjectId(int $ref_id)
static _lookupObjId(int $ref_id)
static _lookupTitle(int $obj_id)
static redirect(string $a_script)
$ref_id
Definition: ltiauth.php:66
global $lng
Definition: privfeed.php:31
global $DIC
Definition: shib_login.php:26