ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilAccountCode.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
4
14{
15 const DB_TABLE = 'usr_account_codes';
16 const CODE_LENGTH = 10;
17
18 public static function create($valid_until, $stamp)
19 {
20 global $DIC;
21
22 $ilDB = $DIC['ilDB'];
23
24 $id = $ilDB->nextId(self::DB_TABLE);
25
26 // create unique code
27 $found = true;
28 while ($found) {
30 $chk = $ilDB->queryF("SELECT code_id FROM " . self::DB_TABLE . " WHERE code = %s", array("text"), array($code));
31 $found = (bool) $ilDB->numRows($chk);
32 }
33
34 $data = array(
35 'code_id' => array('integer', $id),
36 'code' => array('text', $code),
37 'generated' => array('integer', $stamp),
38 'valid_until' => array('text', $valid_until)
39 );
40
41 $ilDB->insert(self::DB_TABLE, $data);
42 return $id;
43 }
44
45 protected static function generateRandomCode()
46 {
47 // missing : 01iloO
48 $map = "23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
49
50 $code = "";
51 $max = strlen($map) - 1;
52 for ($loop = 1; $loop <= self::CODE_LENGTH; $loop++) {
53 $code .= $map[mt_rand(0, $max)];
54 }
55 return $code;
56 }
57
58 public static function getCodesData($order_field, $order_direction, $offset, $limit, $filter_code, $filter_valid_until, $filter_generated)
59 {
60 global $DIC;
61
62 $ilDB = $DIC['ilDB'];
63
64 // filter
65 $where = self::filterToSQL($filter_code, $filter_valid_until, $filter_generated);
66
67 // count query
68 $set = $ilDB->query("SELECT COUNT(*) AS cnt FROM " . self::DB_TABLE . $where);
69 $cnt = 0;
70 if ($rec = $ilDB->fetchAssoc($set)) {
71 $cnt = $rec["cnt"];
72 }
73
74 $sql = "SELECT * FROM " . self::DB_TABLE . $where;
75 if ($order_field) {
76 $sql .= " ORDER BY " . $order_field . " " . $order_direction;
77 }
78
79 // set query
80 $ilDB->setLimit((int) $limit, (int) $offset);
81 $set = $ilDB->query($sql);
82 $result = array();
83 while ($rec = $ilDB->fetchAssoc($set)) {
84 $result[] = $rec;
85 }
86 return array("cnt" => $cnt, "set" => $result);
87 }
88
89 public static function loadCodesByIds(array $ids)
90 {
91 global $DIC;
92
93 $ilDB = $DIC['ilDB'];
94
95 $set = $ilDB->query("SELECT * FROM " . self::DB_TABLE . " WHERE " . $ilDB->in("code_id", $ids, false, "integer"));
96 $result = array();
97 while ($rec = $ilDB->fetchAssoc($set)) {
98 $result[] = $rec;
99 }
100 return $result;
101 }
102
103 public static function deleteCodes(array $ids)
104 {
105 global $DIC;
106
107 $ilDB = $DIC['ilDB'];
108
109 if (sizeof($ids)) {
110 return $ilDB->manipulate("DELETE FROM " . self::DB_TABLE . " WHERE " . $ilDB->in("code_id", $ids, false, "integer"));
111 }
112 return false;
113 }
114
115 public static function getGenerationDates()
116 {
117 global $DIC;
118
119 $ilDB = $DIC['ilDB'];
120
121 $set = $ilDB->query("SELECT DISTINCT(generated) AS generated FROM " . self::DB_TABLE . " ORDER BY generated");
122 $result = array();
123 while ($rec = $ilDB->fetchAssoc($set)) {
124 $result[] = $rec["generated"];
125 }
126 return $result;
127 }
128
129 protected static function filterToSQL($filter_code, $filter_valid_until, $filter_generated)
130 {
131 global $DIC;
132
133 $ilDB = $DIC['ilDB'];
134
135 $where = array();
136 if ($filter_code) {
137 $where[] = $ilDB->like("code", "text", "%" . $filter_code . "%");
138 }
139 if ($filter_valid_until) {
140 $where[] = "valid_until = " . $ilDB->quote($filter_valid_until, "text");
141 }
142 if ($filter_generated) {
143 $where[] = "generated = " . $ilDB->quote($filter_generated, "text");
144 }
145 if (sizeof($where)) {
146 return " WHERE " . implode(" AND ", $where);
147 } else {
148 return "";
149 }
150 }
151
152 public static function getCodesForExport($filter_code, $filter_valid_until, $filter_generated)
153 {
154 global $DIC;
155
156 $ilDB = $DIC['ilDB'];
157
158 // filter
159 $where = self::filterToSQL($filter_code, $filter_valid_until, $filter_generated);
160
161 // set query
162 $set = $ilDB->query("SELECT code FROM " . self::DB_TABLE . $where . " ORDER BY code_id");
163 $result = array();
164 while ($rec = $ilDB->fetchAssoc($set)) {
165 $result[] = $rec["code"];
166 }
167 return $result;
168 }
169
170 public static function isUnusedCode($code)
171 {
172 global $DIC;
173
174 $ilDB = $DIC['ilDB'];
175
176 include_once './Services/Registration/classes/class.ilRegistrationCode.php';
178
179
180 $set = $ilDB->query("SELECT used FROM " . self::DB_TABLE . " WHERE code = " . $ilDB->quote($code, "text"));
181 $set = $ilDB->fetchAssoc($set);
182 if ($set && !$set["used"]) {
183 return true;
184 }
185 return false;
186 }
187
188 public static function useCode($code)
189 {
190 global $DIC;
191
192 $ilDB = $DIC['ilDB'];
193
194 include_once './Services/Registration/classes/class.ilRegistrationCode.php';
195 return (bool) ilRegistrationCode::useCode($code);
196
197 return (bool) $ilDB->update(self::DB_TABLE, array("used" => array("timestamp", time())), array("code" => array("text", $code)));
198 }
199
200 public static function getCodeValidUntil($code)
201 {
202 global $DIC;
203
204 $ilDB = $DIC['ilDB'];
205
206 include_once './Services/Registration/classes/class.ilRegistrationCode.php';
208
209 if ($code_data["alimit"]) {
210 switch ($code_data["alimit"]) {
211 case "absolute":
212 return $code_data['alimitdt'];
213 }
214 }
215 return "0";
216
217 $set = $ilDB->query("SELECT valid_until FROM " . self::DB_TABLE . " WHERE code = " . $ilDB->quote($code, "text"));
218 $row = $ilDB->fetchAssoc($set);
219 if (isset($row["valid_until"])) {
220 return $row["valid_until"];
221 }
222 }
223
224 public static function applyRoleAssignments(ilObjUser $user, $code)
225 {
226 include_once './Services/Registration/classes/class.ilRegistrationCode.php';
227
229 if ($grole) {
230 $GLOBALS['DIC']['rbacadmin']->assignUser($grole, $user->getId());
231 }
233 if ($code_data["role_local"]) {
234 $code_local_roles = explode(";", $code_data["role_local"]);
235 foreach ((array) $code_local_roles as $role_id) {
236 $GLOBALS['DIC']['rbacadmin']->assignUser($role_id, $user->getId());
237
238 // patch to remove for 45 due to mantis 21953
239 $role_obj = $GLOBALS['DIC']['rbacreview']->getObjectOfRole($role_id);
240 switch (ilObject::_lookupType($role_obj)) {
241 case 'crs':
242 case 'grp':
243 $role_refs = ilObject::_getAllReferences($role_obj);
244 $role_ref = end($role_refs);
245 ilObjUser::_addDesktopItem($user->getId(), $role_ref, ilObject::_lookupType($role_obj));
246 break;
247 }
248 }
249 }
250 return true;
251 }
252
253 public static function applyAccessLimits(ilObjUser $user, $code)
254 {
255 include_once './Services/Registration/classes/class.ilRegistrationCode.php';
257
258 if ($code_data["alimit"]) {
259 switch ($code_data["alimit"]) {
260 case "absolute":
261 $end = new ilDateTime($code_data['alimitdt'], IL_CAL_DATE);
262 //$user->setTimeLimitFrom(time());
263 $user->setTimeLimitUntil($end->get(IL_CAL_UNIX));
264 $user->setTimeLimitUnlimited(0);
265 break;
266
267 case "relative":
268
269 $rel = unserialize($code_data["alimitdt"]);
270
271 include_once './Services/Calendar/classes/class.ilDateTime.php';
272 $end = new ilDateTime(time(), IL_CAL_UNIX);
273
274 if ($rel['y'] > 0) {
275 $end->increment(IL_CAL_YEAR, $rel['y']);
276 }
277
278 if ($rel['m'] > 0) {
279 $end->increment(IL_CAL_MONTH, $rel['m']);
280 }
281
282 if ($rel['d'] > 0) {
283 $end->increment(IL_CAL_DAY, $rel['d']);
284 }
285
286 //$user->setTimeLimitFrom(time());
287 $user->setTimeLimitUntil($end->get(IL_CAL_UNIX));
288 $user->setTimeLimitUnlimited(0);
289 break;
290
291 case 'unlimited':
292 $user->setTimeLimitUnlimited(1);
293 break;
294
295 }
296 } else {
297 $user->setTimeLimitUnlimited(1);
298 }
299 }
300}
$result
An exception for terminatinating execution or to throw for unit testing.
const IL_CAL_DATE
const IL_CAL_UNIX
const IL_CAL_YEAR
const IL_CAL_MONTH
const IL_CAL_DAY
Class ilAccountCode.
static isUnusedCode($code)
static create($valid_until, $stamp)
static getCodeValidUntil($code)
static applyRoleAssignments(ilObjUser $user, $code)
static applyAccessLimits(ilObjUser $user, $code)
static getCodesData($order_field, $order_direction, $offset, $limit, $filter_code, $filter_valid_until, $filter_generated)
static generateRandomCode()
static loadCodesByIds(array $ids)
static useCode($code)
static filterToSQL($filter_code, $filter_valid_until, $filter_generated)
static deleteCodes(array $ids)
static getCodesForExport($filter_code, $filter_valid_until, $filter_generated)
@classDescription Date and time handling
static _addDesktopItem($a_usr_id, $a_item_id, $a_type, $a_par="")
add an item to user's personal desktop
static _getAllReferences($a_id)
get all reference ids of object
static _lookupType($a_id, $a_reference=false)
lookup object type
static isUnusedCode($code)
Check if code has been used already @global type $ilDB.
$code
Definition: example_050.php:99
if(!array_key_exists('StateId', $_REQUEST)) $id
$user
Definition: migrateto20.php:57
$row
$GLOBALS['JPEG_Segment_Names']
Global Variable: XMP_tag_captions.
global $DIC
Definition: saml.php:7
global $ilDB
$data
Definition: bench.php:6