ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
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)
27  {
28  $code = self::generateRandomCode();
29  $chk = $ilDB->queryF("SELECT code_id FROM ".self::DB_TABLE." WHERE code = %s", array("text"), array($code));
30  $found = (bool)$ilDB->numRows($chk);
31  }
32 
33  $data = array(
34  'code_id' => array('integer', $id),
35  'code' => array('text', $code),
36  'generated' => array('integer', $stamp),
37  'valid_until' => array('text', $valid_until)
38  );
39 
40  $ilDB->insert(self::DB_TABLE, $data);
41  return $id;
42  }
43 
44  protected static function generateRandomCode()
45  {
46  // missing : 01iloO
47  $map = "23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
48 
49  $code = "";
50  $max = strlen($map)-1;
51  for($loop = 1; $loop <= self::CODE_LENGTH; $loop++)
52  {
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 $ilDB;
61 
62  // filter
63  $where = self::filterToSQL($filter_code, $filter_valid_until, $filter_generated);
64 
65  // count query
66  $set = $ilDB->query("SELECT COUNT(*) AS cnt FROM ".self::DB_TABLE.$where);
67  $cnt = 0;
68  if ($rec = $ilDB->fetchAssoc($set))
69  {
70  $cnt = $rec["cnt"];
71  }
72 
73  $sql = "SELECT * FROM ".self::DB_TABLE.$where;
74  if($order_field)
75  {
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  {
85  $result[] = $rec;
86  }
87  return array("cnt" => $cnt, "set" => $result);
88  }
89 
90  public static function loadCodesByIds(array $ids)
91  {
92  global $ilDB;
93 
94  $set = $ilDB->query("SELECT * FROM ".self::DB_TABLE." WHERE ".$ilDB->in("code_id", $ids, false, "integer"));
95  $result = array();
96  while($rec = $ilDB->fetchAssoc($set))
97  {
98  $result[] = $rec;
99  }
100  return $result;
101  }
102 
103  public static function deleteCodes(array $ids)
104  {
105  global $ilDB;
106 
107  if(sizeof($ids))
108  {
109  return $ilDB->manipulate("DELETE FROM ".self::DB_TABLE." WHERE ".$ilDB->in("code_id", $ids, false, "integer"));
110  }
111  return false;
112  }
113 
114  public static function getGenerationDates()
115  {
116  global $ilDB;
117 
118  $set = $ilDB->query("SELECT DISTINCT(generated) AS generated FROM ".self::DB_TABLE." ORDER BY generated");
119  $result = array();
120  while($rec = $ilDB->fetchAssoc($set))
121  {
122  $result[] = $rec["generated"];
123  }
124  return $result;
125  }
126 
127  protected static function filterToSQL($filter_code, $filter_valid_until, $filter_generated)
128  {
129  global $ilDB;
130 
131  $where = array();
132  if($filter_code)
133  {
134  $where[] = $ilDB->like("code", "text", "%".$filter_code."%");
135  }
136  if($filter_valid_until)
137  {
138  $where[] ="valid_until = ".$ilDB->quote($filter_valid_until, "text");
139  }
140  if($filter_generated)
141  {
142  $where[] ="generated = ".$ilDB->quote($filter_generated, "text");
143  }
144  if(sizeof($where))
145  {
146  return " WHERE ".implode(" AND ", $where);
147  }
148  else
149  {
150  return "";
151  }
152  }
153 
154  public static function getCodesForExport($filter_code, $filter_valid_until, $filter_generated)
155  {
156  global $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  {
166  $result[] = $rec["code"];
167  }
168  return $result;
169  }
170 
171  public static function isUnusedCode($code)
172  {
173  global $ilDB;
174 
175  include_once './Services/Registration/classes/class.ilRegistrationCode.php';
176  return ilRegistrationCode::isUnusedCode($code);
177 
178 
179  $set = $ilDB->query("SELECT used FROM ".self::DB_TABLE." WHERE code = ".$ilDB->quote($code, "text"));
180  $set = $ilDB->fetchAssoc($set);
181  if($set && !$set["used"])
182  {
183  return true;
184  }
185  return false;
186  }
187 
188  public static function useCode($code)
189  {
190  global $ilDB;
191 
192  include_once './Services/Registration/classes/class.ilRegistrationCode.php';
193  return (bool) ilRegistrationCode::useCode($code);
194 
195  return (bool)$ilDB->update(self::DB_TABLE, array("used"=>array("timestamp", time())), array("code"=>array("text", $code)));
196  }
197 
198  public static function getCodeValidUntil($code)
199  {
200  global $ilDB;
201 
202  include_once './Services/Registration/classes/class.ilRegistrationCode.php';
203  $code_data = ilRegistrationCode::getCodeData($code);
204 
205  if($code_data["alimit"])
206  {
207  switch($code_data["alimit"])
208  {
209  case "absolute":
210  return $code_data['alimitdt'];
211  }
212  }
213  return "0";
214 
215  $set = $ilDB->query("SELECT valid_until FROM ".self::DB_TABLE." WHERE code = ".$ilDB->quote($code, "text"));
216  $row = $ilDB->fetchAssoc($set);
217  if(isset($row["valid_until"]))
218  {
219  return $row["valid_until"];
220  }
221  }
222 
223  public static function applyRoleAssignments(ilObjUser $user, $code)
224  {
225  include_once './Services/Registration/classes/class.ilRegistrationCode.php';
226 
227  $grole = ilRegistrationCode::getCodeRole($code);
228  if($grole)
229  {
230  $GLOBALS['rbacadmin']->assignUser($grole,$user->getId());
231  }
232  $code_data = ilRegistrationCode::getCodeData($code);
233  if($code_data["role_local"])
234  {
235  $code_local_roles = explode(";", $code_data["role_local"]);
236  foreach((array) $code_local_roles as $role_id)
237  {
238  $GLOBALS['rbacadmin']->assignUser($role_id,$user->getId());
239 
240  // patch to remove for 45 due to mantis 21953
241  $role_obj = $GLOBALS['rbacreview']->getObjectOfRole($role_id);
242  switch(ilObject::_lookupType($role_obj))
243  {
244  case 'crs':
245  case 'grp':
246  $role_refs = ilObject::_getAllReferences($role_obj);
247  $role_ref = end($role_refs);
248  ilObjUser::_addDesktopItem($user->getId(),$role_ref,ilObject::_lookupType($role_obj));
249  break;
250  }
251  }
252  }
253  return true;
254  }
255 
256  public static function applyAccessLimits(ilObjUser $user, $code)
257  {
258  include_once './Services/Registration/classes/class.ilRegistrationCode.php';
259  $code_data = ilRegistrationCode::getCodeData($code);
260 
261  if($code_data["alimit"])
262  {
263  switch($code_data["alimit"])
264  {
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  {
281  $end->increment(IL_CAL_YEAR, $rel['y']);
282  }
283 
284  if($rel['m'] > 0)
285  {
286  $end->increment(IL_CAL_MONTH, $rel['m']);
287  }
288 
289  if($rel['d'] > 0)
290  {
291  $end->increment(IL_CAL_DAY, $rel['d']);
292  }
293 
294  //$user->setTimeLimitFrom(time());
295  $user->setTimeLimitUntil($end->get(IL_CAL_UNIX));
296  $user->setTimeLimitUnlimited(0);
297  break;
298 
299  case 'unlimited':
300  $user->setTimeLimitUnlimited(1);
301  break;
302 
303  }
304  }
305  else
306  {
307  $user->setTimeLimitUnlimited(1);
308  }
309 
310  }
311 }
312 
313 ?>