18 public static function create($valid_until, $stamp)
22 $id = $ilDB->nextId(self::DB_TABLE);
29 $chk = $ilDB->queryF(
"SELECT code_id FROM ".self::DB_TABLE.
" WHERE code = %s", array(
"text"), array($code));
30 $found = (bool)$ilDB->numRows($chk);
34 'code_id' => array(
'integer', $id),
35 'code' => array(
'text', $code),
36 'generated' => array(
'integer', $stamp),
37 'valid_until' => array(
'text', $valid_until)
40 $ilDB->insert(self::DB_TABLE, $data);
47 $map =
"23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
50 $max = strlen($map)-1;
53 $code .= $map[mt_rand(0, $max)];
58 public static function getCodesData($order_field, $order_direction, $offset, $limit, $filter_code, $filter_valid_until, $filter_generated)
66 $set = $ilDB->query(
"SELECT COUNT(*) AS cnt FROM ".self::DB_TABLE.$where);
68 if ($rec = $ilDB->fetchAssoc($set))
73 $sql =
"SELECT * FROM ".self::DB_TABLE.$where;
76 $sql .=
" ORDER BY ".$order_field.
" ".$order_direction;
80 $ilDB->setLimit((
int)$limit, (
int)$offset);
81 $set = $ilDB->query($sql);
83 while($rec = $ilDB->fetchAssoc($set))
87 return array(
"cnt" => $cnt,
"set" =>
$result);
94 $set = $ilDB->query(
"SELECT * FROM ".self::DB_TABLE.
" WHERE ".$ilDB->in(
"code_id", $ids,
false,
"integer"));
96 while($rec = $ilDB->fetchAssoc($set))
109 return $ilDB->manipulate(
"DELETE FROM ".self::DB_TABLE.
" WHERE ".$ilDB->in(
"code_id", $ids,
false,
"integer"));
118 $set = $ilDB->query(
"SELECT DISTINCT(generated) AS generated FROM ".self::DB_TABLE.
" ORDER BY generated");
120 while($rec = $ilDB->fetchAssoc($set))
127 protected static function filterToSQL($filter_code, $filter_valid_until, $filter_generated)
134 $where[] = $ilDB->like(
"code",
"text",
"%".$filter_code.
"%");
136 if($filter_valid_until)
138 $where[] =
"valid_until = ".$ilDB->quote($filter_valid_until,
"text");
140 if($filter_generated)
142 $where[] =
"generated = ".$ilDB->quote($filter_generated,
"text");
146 return " WHERE ".implode(
" AND ", $where);
159 $where =
self::filterToSQL($filter_code, $filter_valid_until, $filter_generated);
162 $set = $ilDB->query(
"SELECT code FROM ".self::DB_TABLE.$where.
" ORDER BY code_id");
164 while($rec = $ilDB->fetchAssoc($set))
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"])
188 return (
bool)$ilDB->update(self::DB_TABLE, array(
"used"=>array(
"timestamp", time())), array(
"code"=>array(
"text", $code)));
195 $set = $ilDB->query(
"SELECT valid_until FROM ".self::DB_TABLE.
" WHERE code = ".$ilDB->quote($code,
"text"));
196 $row = $ilDB->fetchAssoc($set);
197 if(isset(
$row[
"valid_until"]))
199 return $row[
"valid_until"];