ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.RunDBRepository.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24
32{
33 public const NOT_STARTED = -1;
34 public const STARTED_NOT_FINISHED = 0;
35 public const FINISHED = 1;
36
37 protected \ilDBInterface $db;
39
40 public function __construct(
42 ?\ilDBInterface $db = null
43 ) {
44 global $DIC;
45
46 $this->data = $data;
47 $this->db = (is_null($db))
48 ? $DIC->database()
49 : $db;
50 }
51
52
57 public function getFinishedSurveysOfUser(
58 int $user_id
59 ): array {
60 $db = $this->db;
61
62 $set = $db->queryF(
63 "SELECT survey_fi FROM svy_finished " .
64 " WHERE user_fi = %s AND state = %s",
65 ["integer", "integer"],
66 [$user_id, 1]
67 );
68 $items = [];
69 while ($rec = $db->fetchAssoc($set)) {
70 $items[] = (int) $rec["survey_fi"];
71 }
72 return $items;
73 }
74
80 int $user_id
81 ): array {
82 $db = $this->db;
83
84 $set = $db->queryF(
85 "SELECT survey_fi FROM svy_finished " .
86 " WHERE user_fi = %s AND state = %s",
87 ["integer", "integer"],
88 [$user_id, 0]
89 );
90 $items = [];
91 while ($rec = $db->fetchAssoc($set)) {
92 $items[] = (int) $rec["survey_fi"];
93 }
94 return $items;
95 }
96
102 int $rater_id
103 ): array {
104 $db = $this->db;
105
106 $set = $db->queryF(
107 "SELECT survey_fi, appr_id FROM svy_finished " .
108 " WHERE user_fi = %s AND state = %s",
109 ["integer", "integer"],
110 [$rater_id, 1]
111 );
112 $appraisee = [];
113 while ($rec = $db->fetchAssoc($set)) {
114 $appraisee[] = [
115 "survey_id" => (int) $rec["survey_fi"],
116 "appr_id" => (int) $rec["appr_id"]
117 ];
118 }
119 return $appraisee;
120 }
121
122 public function getCurrentRunId(
123 int $survey_id,
124 int $user_id,
125 string $code = "",
126 int $appr_id = 0
127 ): ?int {
128 $db = $this->db;
129 if ($code !== "") { // #15031 - should not matter if code was used by registered or anonymous (each code must be unique)
130 $set = $db->queryF(
131 "SELECT * FROM svy_finished" .
132 " WHERE survey_fi = %s AND anonymous_id = %s AND appr_id = %s",
133 array('integer', 'text', 'integer'),
134 array($survey_id, $code, $appr_id)
135 );
136 } else {
137 if ($user_id === ANONYMOUS_USER_ID) {
138 return null;
139 }
140 $set = $db->queryF(
141 "SELECT * FROM svy_finished" .
142 " WHERE survey_fi = %s AND user_fi = %s AND appr_id = %s",
143 array('integer', 'integer', 'integer'),
144 array($survey_id, $user_id, $appr_id)
145 );
146 }
147 if ($rec = $db->fetchAssoc($set)) {
148 return (int) $rec["finished_id"];
149 }
150 return null;
151 }
152
153 public function getState(
154 int $run_id
155 ): int {
156 $db = $this->db;
157
158 $set = $db->queryF(
159 "SELECT * FROM svy_finished" .
160 " WHERE finished_id = %s ",
161 array('integer'),
162 array($run_id)
163 );
164 if ($rec = $db->fetchAssoc($set)) {
165 return (int) $rec["state"];
166 }
167 return self::NOT_STARTED;
168 }
169
173 public function getRunsForUser(
174 int $survey_id,
175 int $user_id,
176 string $code = ""
177 ): array {
178 $db = $this->db;
179
180 $sql = "SELECT * FROM svy_finished" .
181 " WHERE survey_fi = " . $db->quote($survey_id, "integer");
182 // if proper user id is given, use it or current code
183 if ($user_id !== ANONYMOUS_USER_ID) {
184 $sql .= " AND (user_fi = " . $db->quote($user_id, "integer");
185 if ($code !== "") {
186 $sql .= " OR anonymous_id = " . $db->quote($code, "text");
187 }
188 $sql .= ")";
189 }
190 // use anonymous code to find finished id(s)
191 else {
192 if ($code === "") {
193 return [];
194 }
195 $sql .= " AND anonymous_id = " . $db->quote($code, "text");
196 }
197 $set = $db->query($sql);
198 $runs = [];
199 while ($row = $db->fetchAssoc($set)) {
200 $runs[$row["finished_id"]] = $this->data->run($survey_id, $user_id)
201 ->withId((int) $row["finished_id"])
202 ->withFinished((bool) $row["state"])
203 ->withCode((string) $row["anonymous_id"])
204 ->withTimestamp((int) $row["tstamp"])
205 ->withAppraiseeId((int) $row["appr_id"])
206 ->withLastPage((int) $row["lastpage"]);
207 }
208 return $runs;
209 }
210
211 public function getById(
212 int $run_id
213 ): ?Run {
214 $db = $this->db;
215
216 $sql = "SELECT * FROM svy_finished" .
217 " WHERE finished_id = " . $db->quote($run_id, "integer");
218 $set = $db->query($sql);
219 while ($row = $db->fetchAssoc($set)) {
220 return $this->data->run((int) $row["survey_fi"], (int) $row["user_fi"])
221 ->withId((int) $row["finished_id"])
222 ->withFinished((bool) $row["state"])
223 ->withCode((string) $row["anonymous_id"])
224 ->withTimestamp((int) $row["tstamp"])
225 ->withAppraiseeId((int) $row["appr_id"])
226 ->withLastPage((int) $row["lastpage"]);
227 }
228 return null;
229 }
230
231
236 public function add(
237 int $survey_id,
238 int $user_id,
239 string $code,
240 int $appraisee_id = 0
241 ): int {
242 $db = $this->db;
243
244 $next_id = $db->nextId('svy_finished');
245 $affectedRows = $db->manipulateF(
246 "INSERT INTO svy_finished (finished_id, survey_fi, user_fi, anonymous_id, state, tstamp, appr_id) " .
247 "VALUES (%s, %s, %s, %s, %s, %s, %s)",
248 array('integer','integer','integer','text','text','integer','integer'),
249 array($next_id, $survey_id, $user_id, $code, 0, time(), $appraisee_id)
250 );
251
252 return $next_id;
253 }
254
258 public function addTime(int $run_id, int $time, int $first_question): void
259 {
260 $db = $this->db;
261 $id = $db->nextId('svy_times');
262 $db->manipulateF(
263 "INSERT INTO svy_times (id, finished_fi, entered_page, left_page, first_question) VALUES (%s, %s, %s, %s,%s)",
264 array('integer','integer', 'integer', 'integer', 'integer'),
265 array($id, $run_id, $time, null, $first_question)
266 );
267 }
268
269 public function updateTime(int $run_id, int $time, int $entered_time): void
270 {
271 $db = $this->db;
272 $db->manipulateF(
273 "UPDATE svy_times SET left_page = %s WHERE finished_fi = %s AND entered_page = %s",
274 array('integer', 'integer', 'integer'),
275 array($time, $run_id, $entered_time)
276 );
277 }
278}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
getUnfinishedSurveysOfUser(int $user_id)
Get all unfinished surveys of a user.
updateTime(int $run_id, int $time, int $entered_time)
getCurrentRunId(int $survey_id, int $user_id, string $code="", int $appr_id=0)
getFinishedSurveysOfUser(int $user_id)
Get all finished surveys of a user.
__construct(InternalDataService $data, ?\ilDBInterface $db=null)
addTime(int $run_id, int $time, int $first_question)
Add time record.
getRunsForUser(int $survey_id, int $user_id, string $code="")
add(int $survey_id, int $user_id, string $code, int $appraisee_id=0)
Add new run.
Code data class.
Definition: class.Run.php:28
const ANONYMOUS_USER_ID
Definition: constants.php:27
Interface ilDBInterface.
fetchAssoc(ilDBStatement $statement)
queryF(string $query, array $types, array $values)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: shib_login.php:26