ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 $ilDB;
21
22 $id = $ilDB->nextId(self::DB_TABLE);
23
24 // create unique code
25 $found = true;
26 while ($found) {
28 $chk = $ilDB->queryF("SELECT code_id FROM " . self::DB_TABLE . " WHERE code = %s", array("text"), array($code));
29 $found = (bool) $ilDB->numRows($chk);
30 }
31
32 $data = array(
33 'code_id' => array('integer', $id),
34 'code' => array('text', $code),
35 'generated' => array('integer', $stamp),
36 'valid_until' => array('text', $valid_until)
37 );
38
39 $ilDB->insert(self::DB_TABLE, $data);
40 return $id;
41 }
42
43 protected static function generateRandomCode()
44 {
45 // missing : 01iloO
46 $map = "23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
47
48 $code = "";
49 $max = strlen($map)-1;
50 for ($loop = 1; $loop <= self::CODE_LENGTH; $loop++) {
51 $code .= $map[mt_rand(0, $max)];
52 }
53 return $code;
54 }
55
56 public static function getCodesData($order_field, $order_direction, $offset, $limit, $filter_code, $filter_valid_until, $filter_generated)
57 {
58 global $ilDB;
59
60 // filter
61 $where = self::filterToSQL($filter_code, $filter_valid_until, $filter_generated);
62
63 // count query
64 $set = $ilDB->query("SELECT COUNT(*) AS cnt FROM " . self::DB_TABLE . $where);
65 $cnt = 0;
66 if ($rec = $ilDB->fetchAssoc($set)) {
67 $cnt = $rec["cnt"];
68 }
69
70 $sql = "SELECT * FROM " . self::DB_TABLE . $where;
71 if ($order_field) {
72 $sql .= " ORDER BY " . $order_field . " " . $order_direction;
73 }
74
75 // set query
76 $ilDB->setLimit((int) $limit, (int) $offset);
77 $set = $ilDB->query($sql);
78 $result = array();
79 while ($rec = $ilDB->fetchAssoc($set)) {
80 $result[] = $rec;
81 }
82 return array("cnt" => $cnt, "set" => $result);
83 }
84
85 public static function loadCodesByIds(array $ids)
86 {
87 global $ilDB;
88
89 $set = $ilDB->query("SELECT * FROM " . self::DB_TABLE . " WHERE " . $ilDB->in("code_id", $ids, false, "integer"));
90 $result = array();
91 while ($rec = $ilDB->fetchAssoc($set)) {
92 $result[] = $rec;
93 }
94 return $result;
95 }
96
97 public static function deleteCodes(array $ids)
98 {
99 global $ilDB;
100
101 if (sizeof($ids)) {
102 return $ilDB->manipulate("DELETE FROM " . self::DB_TABLE . " WHERE " . $ilDB->in("code_id", $ids, false, "integer"));
103 }
104 return false;
105 }
106
107 public static function getGenerationDates()
108 {
109 global $ilDB;
110
111 $set = $ilDB->query("SELECT DISTINCT(generated) AS generated FROM " . self::DB_TABLE . " ORDER BY generated");
112 $result = array();
113 while ($rec = $ilDB->fetchAssoc($set)) {
114 $result[] = $rec["generated"];
115 }
116 return $result;
117 }
118
119 protected static function filterToSQL($filter_code, $filter_valid_until, $filter_generated)
120 {
121 global $ilDB;
122
123 $where = array();
124 if ($filter_code) {
125 $where[] = $ilDB->like("code", "text", "%" . $filter_code . "%");
126 }
127 if ($filter_valid_until) {
128 $where[] ="valid_until = " . $ilDB->quote($filter_valid_until, "text");
129 }
130 if ($filter_generated) {
131 $where[] ="generated = " . $ilDB->quote($filter_generated, "text");
132 }
133 if (sizeof($where)) {
134 return " WHERE " . implode(" AND ", $where);
135 } else {
136 return "";
137 }
138 }
139
140 public static function getCodesForExport($filter_code, $filter_valid_until, $filter_generated)
141 {
142 global $ilDB;
143
144 // filter
145 $where = self::filterToSQL($filter_code, $filter_valid_until, $filter_generated);
146
147 // set query
148 $set = $ilDB->query("SELECT code FROM " . self::DB_TABLE . $where . " ORDER BY code_id");
149 $result = array();
150 while ($rec = $ilDB->fetchAssoc($set)) {
151 $result[] = $rec["code"];
152 }
153 return $result;
154 }
155
156 public static function isUnusedCode($code)
157 {
158 global $ilDB;
159
160 include_once './Services/Registration/classes/class.ilRegistrationCode.php';
162
163
164 $set = $ilDB->query("SELECT used FROM " . self::DB_TABLE . " WHERE code = " . $ilDB->quote($code, "text"));
165 $set = $ilDB->fetchAssoc($set);
166 if ($set && !$set["used"]) {
167 return true;
168 }
169 return false;
170 }
171
172 public static function useCode($code)
173 {
174 global $ilDB;
175
176 include_once './Services/Registration/classes/class.ilRegistrationCode.php';
177 return (bool) ilRegistrationCode::useCode($code);
178
179 return (bool) $ilDB->update(self::DB_TABLE, array("used"=>array("timestamp", time())), array("code"=>array("text", $code)));
180 }
181
182 public static function getCodeValidUntil($code)
183 {
184 global $ilDB;
185
186 include_once './Services/Registration/classes/class.ilRegistrationCode.php';
188
189 if ($code_data["alimit"]) {
190 switch ($code_data["alimit"]) {
191 case "absolute":
192 return $code_data['alimitdt'];
193 }
194 }
195 return "0";
196
197 $set = $ilDB->query("SELECT valid_until FROM " . self::DB_TABLE . " WHERE code = " . $ilDB->quote($code, "text"));
198 $row = $ilDB->fetchAssoc($set);
199 if (isset($row["valid_until"])) {
200 return $row["valid_until"];
201 }
202 }
203
204 public static function applyRoleAssignments(ilObjUser $user, $code)
205 {
206 include_once './Services/Registration/classes/class.ilRegistrationCode.php';
207
209 if ($grole) {
210 $GLOBALS['rbacadmin']->assignUser($grole, $user->getId());
211 }
213 if ($code_data["role_local"]) {
214 $code_local_roles = explode(";", $code_data["role_local"]);
215 foreach ((array) $code_local_roles as $role_id) {
216 $GLOBALS['rbacadmin']->assignUser($role_id, $user->getId());
217
218 // patch to remove for 45 due to mantis 21953
219 $role_obj = $GLOBALS['rbacreview']->getObjectOfRole($role_id);
220 switch (ilObject::_lookupType($role_obj)) {
221 case 'crs':
222 case 'grp':
223 $role_refs = ilObject::_getAllReferences($role_obj);
224 $role_ref = end($role_refs);
225 ilObjUser::_addDesktopItem($user->getId(), $role_ref, ilObject::_lookupType($role_obj));
226 break;
227 }
228 }
229 }
230 return true;
231 }
232
233 public static function applyAccessLimits(ilObjUser $user, $code)
234 {
235 include_once './Services/Registration/classes/class.ilRegistrationCode.php';
237
238 if ($code_data["alimit"]) {
239 switch ($code_data["alimit"]) {
240 case "absolute":
241 $end = new ilDateTime($code_data['alimitdt'], IL_CAL_DATE);
242 //$user->setTimeLimitFrom(time());
243 $user->setTimeLimitUntil($end->get(IL_CAL_UNIX));
244 $user->setTimeLimitUnlimited(0);
245 break;
246
247 case "relative":
248
249 $rel = unserialize($code_data["alimitdt"]);
250
251 include_once './Services/Calendar/classes/class.ilDateTime.php';
252 $end = new ilDateTime(time(), IL_CAL_UNIX);
253
254 if ($rel['y'] > 0) {
255 $end->increment(IL_CAL_YEAR, $rel['y']);
256 }
257
258 if ($rel['m'] > 0) {
259 $end->increment(IL_CAL_MONTH, $rel['m']);
260 }
261
262 if ($rel['d'] > 0) {
263 $end->increment(IL_CAL_DAY, $rel['d']);
264 }
265
266 //$user->setTimeLimitFrom(time());
267 $user->setTimeLimitUntil($end->get(IL_CAL_UNIX));
268 $user->setTimeLimitUnlimited(0);
269 break;
270
271 case 'unlimited':
272 $user->setTimeLimitUnlimited(1);
273 break;
274
275 }
276 } else {
277 $user->setTimeLimitUnlimited(1);
278 }
279 }
280}
$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
setTimeLimitUntil($a_until)
setTimeLimitUnlimited($a_unlimited)
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
getId()
get object id @access public
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
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
$end
Definition: saml1-acs.php:18
global $ilDB