ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilRegistrationCode.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 = 'reg_registration_codes';
16  const CODE_LENGTH = 10;
17 
18  public static function create($role, $stamp, $local_roles, $limit, $limit_date, $reg_type, $ext_type)
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  if(is_array($local_roles))
34  {
35  $local_roles = implode(";", $local_roles);
36  }
37  if($limit == "relative" && is_array($limit_date))
38  {
39  $limit_date = serialize($limit_date);
40  }
41 
42  $data = array(
43  'code_id' => array('integer', $id),
44  'code' => array('text', $code),
45  'generated_on' => array('integer', $stamp),
46  'role' => array('integer', $role),
47  'role_local' => array('text', $local_roles),
48  'alimit' => array('text', $limit),
49  'alimitdt' => array('text', $limit_date),
50  'reg_enabled' => array('integer',$reg_type),
51  'ext_enabled' => array('integer',$ext_type)
52  );
53 
54  $ilDB->insert(self::DB_TABLE, $data);
55  return $id;
56  }
57 
58  protected static function generateRandomCode()
59  {
60  // missing : 01iloO
61  $map = "23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
62 
63  $code = "";
64  $max = strlen($map)-1;
65  for($loop = 1; $loop <= self::CODE_LENGTH; $loop++)
66  {
67  $code .= $map[mt_rand(0, $max)];
68  }
69  return $code;
70  }
71 
72  public static function getCodesData($order_field, $order_direction, $offset, $limit, $filter_code, $filter_role, $filter_generated, $filter_access_limitation)
73  {
74  global $ilDB;
75 
76  // filter
77  $where = self::filterToSQL($filter_code, $filter_role, $filter_generated, $filter_access_limitation);
78 
79  // count query
80  $set = $ilDB->query("SELECT COUNT(*) AS cnt FROM ".self::DB_TABLE.$where);
81  $cnt = 0;
82  if ($rec = $ilDB->fetchAssoc($set))
83  {
84  $cnt = $rec["cnt"];
85  }
86 
87  $sql = "SELECT * FROM ".self::DB_TABLE.$where;
88  if($order_field)
89  {
90  if($order_field == 'generated')
91  {
92  $order_field = 'generated_on';
93  }
94  $sql .= " ORDER BY ".$order_field." ".$order_direction;
95  }
96 
97  // set query
98  $ilDB->setLimit((int)$limit, (int)$offset);
99  $set = $ilDB->query($sql);
100  $result = array();
101  while($rec = $ilDB->fetchAssoc($set))
102  {
103  $rec['generated'] = $rec['generated_on'];
104  $result[] = $rec;
105  }
106  return array("cnt" => $cnt, "set" => $result);
107  }
108 
109  public static function loadCodesByIds(array $ids)
110  {
111  global $ilDB;
112 
113  $set = $ilDB->query("SELECT * FROM ".self::DB_TABLE." WHERE ".$ilDB->in("code_id", $ids, false, "integer"));
114  $result = array();
115  while($rec = $ilDB->fetchAssoc($set))
116  {
117  $result[] = $rec;
118  }
119  return $result;
120  }
121 
122  public static function deleteCodes(array $ids)
123  {
124  global $ilDB;
125 
126  if(sizeof($ids))
127  {
128  return $ilDB->manipulate("DELETE FROM ".self::DB_TABLE." WHERE ".$ilDB->in("code_id", $ids, false, "integer"));
129  }
130  return false;
131  }
132 
133  public static function getGenerationDates()
134  {
135  global $ilDB;
136 
137  $set = $ilDB->query("SELECT DISTINCT(generated_on) genr FROM ".self::DB_TABLE." ORDER BY genr");
138  $result = array();
139  while($rec = $ilDB->fetchAssoc($set))
140  {
141  $result[] = $rec["genr"];
142  }
143  return $result;
144  }
145 
146  protected static function filterToSQL($filter_code, $filter_role, $filter_generated, $filter_access_limitation)
147  {
148  global $ilDB;
149 
150  $where = array();
151  if($filter_code)
152  {
153  $where[] = $ilDB->like("code", "text", "%".$filter_code."%");
154  }
155  if($filter_role)
156  {
157  $where[] ="role = ".$ilDB->quote($filter_role, "integer");
158  }
159  if($filter_generated)
160  {
161  $where[] ="generated_on = ".$ilDB->quote($filter_generated, "text");
162  }
163  if($filter_access_limitation)
164  {
165  $where[] ="alimit = ".$ilDB->quote($filter_access_limitation, "text");
166  }
167  if(sizeof($where))
168  {
169  return " WHERE ".implode(" AND ", $where);
170  }
171  else
172  {
173  return "";
174  }
175  }
176 
177  public static function getCodesForExport($filter_code, $filter_role, $filter_generated, $filter_access_limitation)
178  {
179  global $ilDB;
180 
181  // filter
182  $where = self::filterToSQL($filter_code, $filter_role, $filter_generated, $filter_access_limitation);
183 
184  // set query
185  $set = $ilDB->query("SELECT code FROM ".self::DB_TABLE.$where." ORDER BY code_id");
186  $result = array();
187  while($rec = $ilDB->fetchAssoc($set))
188  {
189  $result[] = $rec["code"];
190  }
191  return $result;
192  }
193 
200  public static function isUnusedCode($code)
201  {
202  global $ilDB;
203 
204  $set = $ilDB->query("SELECT used FROM ".self::DB_TABLE." WHERE code = ".$ilDB->quote($code, "text"));
205  $set = $ilDB->fetchAssoc($set);
206  if($set && !$set["used"])
207  {
208  return true;
209  }
210  return false;
211  }
212 
218  public static function isValidRegistrationCode($a_code)
219  {
220  global $ilDB;
221 
222  $query = 'SELECT code_id FROM reg_registration_codes '.
223  'WHERE used = '.$ilDB->quote(0,'integer').' '.
224  'AND reg_enabled = '.$ilDB->quote(1,'integer').' '.
225  'AND code = '.$ilDB->quote($a_code,'text');
226  $res = $ilDB->query($query);
227 
228  return $res->numRows() ? true : false;
229  }
230 
231  public static function useCode($code)
232  {
233  global $ilDB;
234 
235  return (bool)$ilDB->update(self::DB_TABLE, array("used"=>array("timestamp", time())), array("code"=>array("text", $code)));
236  }
237 
238  public static function getCodeRole($code)
239  {
240  global $ilDB;
241 
242  $set = $ilDB->query("SELECT role FROM ".self::DB_TABLE." WHERE code = ".$ilDB->quote($code, "text"));
243  $row = $ilDB->fetchAssoc($set);
244  if(isset($row["role"]))
245  {
246  return $row["role"];
247  }
248  }
249 
250  public static function getCodeData($code)
251  {
252  global $ilDB;
253 
254  $set = $ilDB->query("SELECT role, role_local, alimit, alimitdt, reg_enabled, ext_enabled".
255  " FROM ".self::DB_TABLE.
256  " WHERE code = ".$ilDB->quote($code, "text"));
257  $row = $ilDB->fetchAssoc($set);
258  return $row;
259  }
260 }
261 ?>
$result
static getCodesData($order_field, $order_direction, $offset, $limit, $filter_code, $filter_role, $filter_generated, $filter_access_limitation)
static deleteCodes(array $ids)
$code
Definition: example_050.php:99
static filterToSQL($filter_code, $filter_role, $filter_generated, $filter_access_limitation)
static create($role, $stamp, $local_roles, $limit, $limit_date, $reg_type, $ext_type)
static isValidRegistrationCode($a_code)
Check if given code is a valid registration code.
static loadCodesByIds(array $ids)
Class ilRegistrationCode.
Create styles array
The data for the language used.
global $ilDB
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
static getCodesForExport($filter_code, $filter_role, $filter_generated, $filter_access_limitation)
static isUnusedCode($code)
Check if code has been used already type $ilDB.