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