ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
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)
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' => 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  );
51 
52  $ilDB->insert(self::DB_TABLE, $data);
53  return $id;
54  }
55 
56  protected static function generateRandomCode()
57  {
58  // missing : 01iloO
59  $map = "23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
60 
61  $code = "";
62  $max = strlen($map)-1;
63  for($loop = 1; $loop <= self::CODE_LENGTH; $loop++)
64  {
65  $code .= $map[mt_rand(0, $max)];
66  }
67  return $code;
68  }
69 
70  public static function getCodesData($order_field, $order_direction, $offset, $limit, $filter_code, $filter_role, $filter_generated, $filter_access_limitation)
71  {
72  global $ilDB;
73 
74  // filter
75  $where = self::filterToSQL($filter_code, $filter_role, $filter_generated, $filter_access_limitation);
76 
77  // count query
78  $set = $ilDB->query("SELECT COUNT(*) AS cnt FROM ".self::DB_TABLE.$where);
79  $cnt = 0;
80  if ($rec = $ilDB->fetchAssoc($set))
81  {
82  $cnt = $rec["cnt"];
83  }
84 
85  $sql = "SELECT * FROM ".self::DB_TABLE.$where;
86  if($order_field)
87  {
88  $sql .= " ORDER BY ".$order_field." ".$order_direction;
89  }
90 
91  // set query
92  $ilDB->setLimit((int)$limit, (int)$offset);
93  $set = $ilDB->query($sql);
94  $result = array();
95  while($rec = $ilDB->fetchAssoc($set))
96  {
97  $result[] = $rec;
98  }
99  return array("cnt" => $cnt, "set" => $result);
100  }
101 
102  public static function loadCodesByIds(array $ids)
103  {
104  global $ilDB;
105 
106  $set = $ilDB->query("SELECT * FROM ".self::DB_TABLE." WHERE ".$ilDB->in("code_id", $ids, false, "integer"));
107  $result = array();
108  while($rec = $ilDB->fetchAssoc($set))
109  {
110  $result[] = $rec;
111  }
112  return $result;
113  }
114 
115  public static function deleteCodes(array $ids)
116  {
117  global $ilDB;
118 
119  if(sizeof($ids))
120  {
121  return $ilDB->manipulate("DELETE FROM ".self::DB_TABLE." WHERE ".$ilDB->in("code_id", $ids, false, "integer"));
122  }
123  return false;
124  }
125 
126  public static function getGenerationDates()
127  {
128  global $ilDB;
129 
130  $set = $ilDB->query("SELECT DISTINCT(generated) AS generated FROM ".self::DB_TABLE." ORDER BY generated");
131  $result = array();
132  while($rec = $ilDB->fetchAssoc($set))
133  {
134  $result[] = $rec["generated"];
135  }
136  return $result;
137  }
138 
139  protected static function filterToSQL($filter_code, $filter_role, $filter_generated, $filter_access_limitation)
140  {
141  global $ilDB;
142 
143  $where = array();
144  if($filter_code)
145  {
146  $where[] = $ilDB->like("code", "text", "%".$filter_code."%");
147  }
148  if($filter_role)
149  {
150  $where[] ="role = ".$ilDB->quote($filter_role, "integer");
151  }
152  if($filter_generated)
153  {
154  $where[] ="generated = ".$ilDB->quote($filter_generated, "text");
155  }
156  if($filter_access_limitation)
157  {
158  $where[] ="alimit = ".$ilDB->quote($filter_access_limitation, "text");
159  }
160  if(sizeof($where))
161  {
162  return " WHERE ".implode(" AND ", $where);
163  }
164  else
165  {
166  return "";
167  }
168  }
169 
170  public static function getCodesForExport($filter_code, $filter_role, $filter_generated, $filter_access_limitation)
171  {
172  global $ilDB;
173 
174  // filter
175  $where = self::filterToSQL($filter_code, $filter_role, $filter_generated, $filter_access_limitation);
176 
177  // set query
178  $set = $ilDB->query("SELECT code FROM ".self::DB_TABLE.$where." ORDER BY code_id");
179  $result = array();
180  while($rec = $ilDB->fetchAssoc($set))
181  {
182  $result[] = $rec["code"];
183  }
184  return $result;
185  }
186 
187  public static function isUnusedCode($code)
188  {
189  global $ilDB;
190 
191  $set = $ilDB->query("SELECT used FROM ".self::DB_TABLE." WHERE code = ".$ilDB->quote($code, "text"));
192  $set = $ilDB->fetchAssoc($set);
193  if($set && !$set["used"])
194  {
195  return true;
196  }
197  return false;
198  }
199 
200  public static function useCode($code)
201  {
202  global $ilDB;
203 
204  return (bool)$ilDB->update(self::DB_TABLE, array("used"=>array("timestamp", time())), array("code"=>array("text", $code)));
205  }
206 
207  public static function getCodeRole($code)
208  {
209  global $ilDB;
210 
211  $set = $ilDB->query("SELECT role FROM ".self::DB_TABLE." WHERE code = ".$ilDB->quote($code, "text"));
212  $row = $ilDB->fetchAssoc($set);
213  if(isset($row["role"]))
214  {
215  return $row["role"];
216  }
217  }
218 
219  public static function getCodeData($code)
220  {
221  global $ilDB;
222 
223  $set = $ilDB->query("SELECT role, role_local, alimit, alimitdt".
224  " FROM ".self::DB_TABLE.
225  " WHERE code = ".$ilDB->quote($code, "text"));
226  $row = $ilDB->fetchAssoc($set);
227  return $row;
228  }
229 }
230 ?>