ILIAS  eassessment Revision 61809
 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)
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  'role' => array('integer', $role)
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_role, $filter_generated)
59  {
60  global $ilDB;
61 
62  // filter
63  $where = self::filterToSQL($filter_code, $filter_role, $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_role, $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_role)
137  {
138  $where[] ="role = ".$ilDB->quote($filter_role, "integer");
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_role, $filter_generated)
155  {
156  global $ilDB;
157 
158  // filter
159  $where = self::filterToSQL($filter_code, $filter_role, $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  $set = $ilDB->query("SELECT used FROM ".self::DB_TABLE." WHERE code = ".$ilDB->quote($code, "text"));
176  $set = $ilDB->fetchAssoc($set);
177  if($set && !$set["used"])
178  {
179  return true;
180  }
181  return false;
182  }
183 
184  public static function useCode($code)
185  {
186  global $ilDB;
187 
188  return (bool)$ilDB->update(self::DB_TABLE, array("used"=>array("timestamp", time())), array("code"=>array("text", $code)));
189  }
190 
191  public static function getCodeRole($code)
192  {
193  global $ilDB;
194 
195  $set = $ilDB->query("SELECT role FROM ".self::DB_TABLE." WHERE code = ".$ilDB->quote($code, "text"));
196  $row = $ilDB->fetchAssoc($set);
197  if(isset($row["role"]))
198  {
199  return $row["role"];
200  }
201  }
202 }
203 ?>