ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.CodeDBRepo.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
21 namespace ILIAS\Survey\Code;
22 
24 
30 {
31  protected \ilDBInterface $db;
33 
34 
35  public function __construct(
36  InternalDataService $data,
37  \ilDBInterface $db
38  ) {
39  $this->db = $db;
40  $this->data = $data;
41  }
42 
46  public function deleteAll(int $survey_id): void
47  {
48  $db = $this->db;
49 
50  $db->manipulateF(
51  "DELETE FROM svy_anonymous WHERE " .
52  " survey_fi = %s",
53  ["integer"],
54  [$survey_id]
55  );
56  }
57 
61  public function delete(int $survey_id, string $code): void
62  {
63  $db = $this->db;
64 
65  if ($code !== "") {
66  $db->manipulateF(
67  "DELETE FROM svy_anonymous WHERE " .
68  " survey_fi = %s AND survey_key = %s",
69  ["integer", "text"],
70  [$survey_id, $code]
71  );
72  }
73  }
74 
78  protected function getNew(int $survey_id): string
79  {
80  // create a 5 character code
81  $codestring = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
82  mt_srand();
83  $code = "";
84  for ($i = 1; $i <= 5; $i++) {
85  $index = random_int(0, strlen($codestring) - 1);
86  $code .= substr($codestring, $index, 1);
87  }
88  // uniqueness
89  while ($this->exists($survey_id, $code)) {
90  $code = $this->getNew($survey_id);
91  }
92  return $code;
93  }
94 
98  public function exists(
99  int $survey_id,
100  string $code
101  ): bool {
102  $db = $this->db;
103  $set = $db->queryF(
104  "SELECT anonymous_id FROM svy_anonymous " .
105  " WHERE survey_fi = %s AND survey_key = %s ",
106  ["integer", "text"],
107  [$survey_id, $code]
108  );
109  return ($set->numRows() > 0);
110  }
111 
115  protected function getUserKey(int $user_id): ?string
116  {
117  $user_key = ($user_id > 0)
118  ? md5((string) $user_id)
119  : null;
120  return $user_key;
121  }
122 
128  public function add(
129  int $survey_id,
130  string $code = "",
131  int $user_id = 0,
132  string $email = "",
133  string $last_name = "",
134  string $first_name = "",
135  int $sent = 0,
136  int $tstamp = 0
137  ): int {
138  $db = $this->db;
139 
140  if ($code === "") {
141  $code = $this->getNew($survey_id);
142  }
143  if ($this->exists($survey_id, $code)) {
144  throw new \ilSurveyException("Code $code already exists.");
145  }
146 
147  $user_key = $this->getUserKey($user_id);
148 
149  if ($tstamp === 0) {
150  $tstamp = time();
151  }
152 
153  $next_id = $db->nextId('svy_anonymous');
154 
155  $db->insert("svy_anonymous", [
156  "anonymous_id" => ["integer", $next_id],
157  "survey_key" => ["text", $code],
158  "survey_fi" => ["integer", $survey_id],
159  "user_key" => ["text", $user_key],
160  "tstamp" => ["integer", $tstamp],
161  "sent" => ["integer", $sent]
162  ]);
163 
164  if ($email !== "" || $last_name !== "" || $first_name !== "") {
165  $this->updateExternalData(
166  $next_id,
167  $email,
168  $last_name,
169  $first_name,
170  $sent
171  );
172  }
173  return $next_id;
174  }
175 
182  public function addCodes(
183  int $survey_id,
184  int $nr
185  ): array {
186  $ids = [];
187  while ($nr-- > 0) {
188  $ids[] = $this->add($survey_id);
189  }
190  return $ids;
191  }
192 
196  public function updateExternalData(
197  int $code_id,
198  string $email,
199  string $last_name,
200  string $first_name,
201  int $sent
202  ): bool {
203  $ilDB = $this->db;
204 
205  $email = trim($email);
206 
207  if ($email === "" || ($email && !\ilUtil::is_email($email))) {
208  return false;
209  }
210 
211  $data = array("email" => $email,
212  "lastname" => trim($last_name),
213  "firstname" => trim($first_name));
214 
215  $fields = array(
216  "externaldata" => array("text", serialize($data)),
217  "sent" => array("integer", $sent)
218  );
219 
220  $ilDB->update(
221  "svy_anonymous",
222  $fields,
223  array("anonymous_id" => array("integer", $code_id))
224  );
225 
226  return true;
227  }
228 
233  public function getAll(
234  int $survey_id
235  ): array {
236  $db = $this->db;
237 
238  $set = $db->queryF(
239  "SELECT survey_key FROM svy_anonymous " .
240  " WHERE survey_fi = %s ",
241  ["integer"],
242  [$survey_id]
243  );
244  $codes = [];
245  while ($rec = $db->fetchAssoc($set)) {
246  $codes[] = $rec["survey_key"];
247  }
248  return $codes;
249  }
250 
255  public function getAllData(
256  int $survey_id
257  ): array {
258  $db = $this->db;
259 
260  $set = $db->queryF(
261  "SELECT * FROM svy_anonymous " .
262  " WHERE survey_fi = %s ",
263  ["integer"],
264  [$survey_id]
265  );
266  $codes = [];
267  while ($rec = $db->fetchAssoc($set)) {
268  $codes[] = $this->data->code($rec["survey_key"])
269  ->withId((int) $rec["anonymous_id"])
270  ->withSurveyId((int) $rec["survey_fi"])
271  ->withUserKey((string) $rec["user_key"])
272  ->withTimestamp((int) $rec["tstamp"])
273  ->withSent((int) $rec["sent"])
274  ->withEmail((string) $rec["email"])
275  ->withFirstName((string) $rec["firstname"])
276  ->withLastName((string) $rec["lastname"]);
277  }
278 
279  return $codes;
280  }
281 
282  public function getByUserKey(
283  int $survey_id,
284  string $survey_key
285  ): ?Code {
286  $db = $this->db;
287 
288  $set = $db->queryF(
289  "SELECT * FROM svy_anonymous " .
290  " WHERE survey_fi = %s AND survey_key = %s",
291  ["integer", "string"],
292  [$survey_id, $survey_key]
293  );
294 
295  if ($rec = $db->fetchAssoc($set)) {
296  $ext_data = unserialize((string) $rec["externaldata"], ["allowed_classes" => false]);
297  return $this->data->code($rec["survey_key"])
298  ->withId((int) $rec["anonymous_id"])
299  ->withSurveyId((int) $rec["survey_fi"])
300  ->withUserKey((string) $rec["user_key"])
301  ->withTimestamp((int) $rec["tstamp"])
302  ->withSent((int) $rec["sent"])
303  ->withEmail((string) ($ext_data["email"] ?? ""))
304  ->withFirstName((string) ($ext_data["firstname"] ?? ""))
305  ->withLastName((string) ($ext_data["lastname"] ?? ""));
306  }
307 
308  return null;
309  }
310 
314  public function bindUser(
315  int $survey_id,
316  string $code,
317  int $user_id
318  ): void {
319  $db = $this->db;
320 
321  $user_key = $this->getUserKey($user_id);
322 
323  $db->update(
324  "svy_anonymous",
325  [
326  "user_key" => ["text", $user_key]
327  ],
328  [ // where
329  "survey_fi" => ["integer", $survey_id],
330  "survey_key" => ["text", $code]
331  ]
332  );
333  }
334 
338  public function getByUserId(
339  int $survey_id,
340  int $user_id
341  ): string {
342  $db = $this->db;
343 
344  $user_key = $this->getUserKey($user_id);
345 
346  $set = $db->queryF(
347  "SELECT survey_key FROM svy_anonymous " .
348  " WHERE survey_fi = %s AND user_key = %s ",
349  ["integer", "string"],
350  [$survey_id, $user_key]
351  );
352  $rec = $db->fetchAssoc($set);
353  return $rec["survey_key"] ?? "";
354  }
355 
356  public function getByCodeId(
357  int $survey_id,
358  int $code_id
359  ): string {
360  $db = $this->db;
361 
362  $set = $db->queryF(
363  "SELECT survey_key FROM svy_anonymous " .
364  " WHERE survey_fi = %s AND anonymous_id = %s ",
365  ["integer", "integer"],
366  [$survey_id, $code_id]
367  );
368  $rec = $db->fetchAssoc($set);
369  return $rec["survey_key"] ?? "";
370  }
371 }
bindUser(int $survey_id, string $code, int $user_id)
Bind registered user to a code.
manipulateF(string $query, array $types, array $values)
static is_email(string $a_email, ilMailRfc822AddressParserFactory $mailAddressParserFactory=null)
This preg-based function checks whether an e-mail address is formally valid.
getNew(int $survey_id)
Get a new unique code.
DB survey codes (table.
__construct(InternalDataService $data, \ilDBInterface $db)
$index
Definition: metadata.php:145
deleteAll(int $survey_id)
Delete all codes of a survey.
if($orgName !==null) if($spconfig->hasValue('contacts')) $email
Definition: metadata.php:302
getAll(int $survey_id)
Get all access keys of a survey.
queryF(string $query, array $types, array $values)
addCodes(int $survey_id, int $nr)
Add multiple codes.
getAllData(int $survey_id)
Get all codes of a survey.
exists(int $survey_id, string $code)
Does code exist in survey?
getByUserKey(int $survey_id, string $survey_key)
getUserKey(int $user_id)
Get user key for id.
add(int $survey_id, string $code="", int $user_id=0, string $email="", string $last_name="", string $first_name="", int $sent=0, int $tstamp=0)
Saves a survey access code for a registered user to the database.
updateExternalData(int $code_id, string $email, string $last_name, string $first_name, int $sent)
Update external data of a code.
getByUserId(int $survey_id, int $user_id)
Get code for a registered user.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: class.Code.php:21
getByCodeId(int $survey_id, int $code_id)
$i
Definition: metadata.php:41
Code data class.
Definition: class.Code.php:27