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