ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ILIAS\Survey\Code\CodeDBRepo Class Reference

DB survey codes (table. More...

+ Collaboration diagram for ILIAS\Survey\Code\CodeDBRepo:

Public Member Functions

 __construct (InternalDataService $data, \ilDBInterface $db)
 
 deleteAll (int $survey_id)
 Delete all codes of a survey. More...
 
 delete (int $survey_id, string $code)
 Delete single code. More...
 
 exists (int $survey_id, string $code)
 Does code exist in survey? More...
 
 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. More...
 
 addCodes (int $survey_id, int $nr)
 Add multiple codes. More...
 
 updateExternalData (int $code_id, string $email, string $last_name, string $first_name, int $sent)
 Update external data of a code. More...
 
 getAll (int $survey_id)
 Get all access keys of a survey. More...
 
 getAllData (int $survey_id)
 Get all codes of a survey. More...
 
 getByUserKey (int $survey_id, string $survey_key)
 
 bindUser (int $survey_id, string $code, int $user_id)
 Bind registered user to a code. More...
 
 getByUserId (int $survey_id, int $user_id)
 Get code for a registered user. More...
 
 getByCodeId (int $survey_id, int $code_id)
 

Protected Member Functions

 getNew (int $survey_id)
 Get a new unique code. More...
 
 getUserKey (int $user_id)
 Get user key for id. More...
 

Protected Attributes

ilDBInterface $db
 
InternalDataService $data
 

Detailed Description

DB survey codes (table.

Author
killi.nosp@m.ng@l.nosp@m.eifos.nosp@m..de

Definition at line 29 of file class.CodeDBRepo.php.

Constructor & Destructor Documentation

◆ __construct()

ILIAS\Survey\Code\CodeDBRepo::__construct ( InternalDataService  $data,
\ilDBInterface  $db 
)

Definition at line 35 of file class.CodeDBRepo.php.

38 {
39 $this->db = $db;
40 $this->data = $data;
41 }

References ILIAS\Survey\Code\CodeDBRepo\$data, and ILIAS\Survey\Code\CodeDBRepo\$db.

Member Function Documentation

◆ add()

ILIAS\Survey\Code\CodeDBRepo::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.

Returns
int new id
Exceptions

ilSurveyException

Definition at line 128 of file class.CodeDBRepo.php.

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 }
updateExternalData(int $code_id, string $email, string $last_name, string $first_name, int $sent)
Update external data of a code.
getUserKey(int $user_id)
Get user key for id.
getNew(int $survey_id)
Get a new unique code.
exists(int $survey_id, string $code)
Does code exist in survey?
insert(string $table_name, array $values)
nextId(string $table_name)

◆ addCodes()

ILIAS\Survey\Code\CodeDBRepo::addCodes ( int  $survey_id,
int  $nr 
)

Add multiple codes.

Parameters
int$nrnumber of codes that should be generated/added
Returns
int[]
Exceptions

ilSurveyException

Definition at line 182 of file class.CodeDBRepo.php.

185 : array {
186 $ids = [];
187 while ($nr-- > 0) {
188 $ids[] = $this->add($survey_id);
189 }
190 return $ids;
191 }
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.

References ILIAS\UI\examples\Symbol\Glyph\Add\add().

+ Here is the call graph for this function:

◆ bindUser()

ILIAS\Survey\Code\CodeDBRepo::bindUser ( int  $survey_id,
string  $code,
int  $user_id 
)

Bind registered user to a code.

Definition at line 314 of file class.CodeDBRepo.php.

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 }
update(string $table_name, array $values, array $where)
@description $where MUST contain existing columns only.

◆ delete()

ILIAS\Survey\Code\CodeDBRepo::delete ( int  $survey_id,
string  $code 
)

Delete single code.

Definition at line 61 of file class.CodeDBRepo.php.

61 : void
62 {
63 $db = $this->db;
64
65 if ($code !== "") {
67 "DELETE FROM svy_anonymous WHERE " .
68 " survey_fi = %s AND survey_key = %s",
69 ["integer", "text"],
70 [$survey_id, $code]
71 );
72 }
73 }
manipulateF(string $query, array $types, array $values)

References ILIAS\Survey\Code\CodeDBRepo\$db, and ilDBInterface\manipulateF().

Referenced by ILIAS\Survey\Code\CodeManager\delete().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ deleteAll()

ILIAS\Survey\Code\CodeDBRepo::deleteAll ( int  $survey_id)

Delete all codes of a survey.

Definition at line 46 of file class.CodeDBRepo.php.

46 : void
47 {
48 $db = $this->db;
49
51 "DELETE FROM svy_anonymous WHERE " .
52 " survey_fi = %s",
53 ["integer"],
54 [$survey_id]
55 );
56 }

References ILIAS\Survey\Code\CodeDBRepo\$db, and ilDBInterface\manipulateF().

Referenced by ILIAS\Survey\Code\CodeManager\deleteAll().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ exists()

ILIAS\Survey\Code\CodeDBRepo::exists ( int  $survey_id,
string  $code 
)

Does code exist in survey?

Definition at line 98 of file class.CodeDBRepo.php.

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 }
queryF(string $query, array $types, array $values)

Referenced by ILIAS\Survey\Code\CodeManager\exists(), and ILIAS\Survey\Code\CodeDBRepo\getNew().

+ Here is the caller graph for this function:

◆ getAll()

ILIAS\Survey\Code\CodeDBRepo::getAll ( int  $survey_id)

Get all access keys of a survey.

Returns
string[]

Definition at line 233 of file class.CodeDBRepo.php.

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 }
fetchAssoc(ilDBStatement $statement)

◆ getAllData()

ILIAS\Survey\Code\CodeDBRepo::getAllData ( int  $survey_id)

Get all codes of a survey.

Returns
Code[]

Definition at line 255 of file class.CodeDBRepo.php.

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 }

◆ getByCodeId()

ILIAS\Survey\Code\CodeDBRepo::getByCodeId ( int  $survey_id,
int  $code_id 
)

Definition at line 356 of file class.CodeDBRepo.php.

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 }

◆ getByUserId()

ILIAS\Survey\Code\CodeDBRepo::getByUserId ( int  $survey_id,
int  $user_id 
)

Get code for a registered user.

Definition at line 338 of file class.CodeDBRepo.php.

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 }

◆ getByUserKey()

ILIAS\Survey\Code\CodeDBRepo::getByUserKey ( int  $survey_id,
string  $survey_key 
)

Definition at line 282 of file class.CodeDBRepo.php.

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 }

◆ getNew()

ILIAS\Survey\Code\CodeDBRepo::getNew ( int  $survey_id)
protected

Get a new unique code.

Definition at line 78 of file class.CodeDBRepo.php.

78 : 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 }

References ILIAS\Survey\Code\CodeDBRepo\exists(), and ILIAS\Survey\Code\CodeDBRepo\getNew().

Referenced by ILIAS\Survey\Code\CodeDBRepo\getNew().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getUserKey()

ILIAS\Survey\Code\CodeDBRepo::getUserKey ( int  $user_id)
protected

Get user key for id.

Definition at line 115 of file class.CodeDBRepo.php.

115 : ?string
116 {
117 $user_key = ($user_id > 0)
118 ? md5((string) $user_id)
119 : null;
120 return $user_key;
121 }

References $user_id.

◆ updateExternalData()

ILIAS\Survey\Code\CodeDBRepo::updateExternalData ( int  $code_id,
string  $email,
string  $last_name,
string  $first_name,
int  $sent 
)

Update external data of a code.

Definition at line 196 of file class.CodeDBRepo.php.

202 : bool {
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 }
static is_email(string $a_email, ?ilMailRfc822AddressParserFactory $mailAddressParserFactory=null)
This preg-based function checks whether an e-mail address is formally valid.

Field Documentation

◆ $data

InternalDataService ILIAS\Survey\Code\CodeDBRepo::$data
protected

Definition at line 32 of file class.CodeDBRepo.php.

Referenced by ILIAS\Survey\Code\CodeDBRepo\__construct().

◆ $db


The documentation for this class was generated from the following file: