ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.RunDBRepository.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
21 namespace ILIAS\Survey\Execution;
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(
41  InternalDataService $data,
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 
79  public function getUnfinishedSurveysOfUser(
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 
130  if ($code !== "") { // #15031 - should not matter if code was used by registered or anonymous (each code must be unique)
131  $set = $db->queryF(
132  "SELECT * FROM svy_finished" .
133  " WHERE survey_fi = %s AND anonymous_id = %s AND appr_id = %s",
134  array('integer', 'text', 'integer'),
135  array($survey_id, $code, $appr_id)
136  );
137  } else {
138  $set = $db->queryF(
139  "SELECT * FROM svy_finished" .
140  " WHERE survey_fi = %s AND user_fi = %s AND appr_id = %s",
141  array('integer', 'integer', 'integer'),
142  array($survey_id, $user_id, $appr_id)
143  );
144  }
145  if ($rec = $db->fetchAssoc($set)) {
146  return (int) $rec["finished_id"];
147  }
148  return null;
149  }
150 
151  public function getState(
152  int $run_id
153  ): int {
154  $db = $this->db;
155 
156  $set = $db->queryF(
157  "SELECT * FROM svy_finished" .
158  " WHERE finished_id = %s ",
159  array('integer'),
160  array($run_id)
161  );
162  if ($rec = $db->fetchAssoc($set)) {
163  return (int) $rec["state"];
164  }
165  return self::NOT_STARTED;
166  }
167 
171  public function getRunsForUser(
172  int $survey_id,
173  int $user_id,
174  string $code = ""
175  ): array {
176  $db = $this->db;
177 
178  $sql = "SELECT * FROM svy_finished" .
179  " WHERE survey_fi = " . $db->quote($survey_id, "integer");
180  // if proper user id is given, use it or current code
181  if ($user_id !== ANONYMOUS_USER_ID) {
182  $sql .= " AND (user_fi = " . $db->quote($user_id, "integer");
183  if ($code !== "") {
184  $sql .= " OR anonymous_id = " . $db->quote($code, "text");
185  }
186  $sql .= ")";
187  }
188  // use anonymous code to find finished id(s)
189  else {
190  if ($code === "") {
191  return [];
192  }
193  $sql .= " AND anonymous_id = " . $db->quote($code, "text");
194  }
195  $set = $db->query($sql);
196  $runs = [];
197  while ($row = $db->fetchAssoc($set)) {
198  $runs[$row["finished_id"]] = $this->data->run($survey_id, $user_id)
199  ->withId((int) $row["finished_id"])
200  ->withFinished((bool) $row["state"])
201  ->withCode((string) $row["anonymous_id"])
202  ->withTimestamp((int) $row["tstamp"])
203  ->withAppraiseeId((int) $row["appr_id"])
204  ->withLastPage((int) $row["lastpage"]);
205  }
206  return $runs;
207  }
208 
209  public function getById(
210  int $run_id
211  ): ?Run {
212  $db = $this->db;
213 
214  $sql = "SELECT * FROM svy_finished" .
215  " WHERE finished_id = " . $db->quote($run_id, "integer");
216  $set = $db->query($sql);
217  while ($row = $db->fetchAssoc($set)) {
218  return $this->data->run((int) $row["survey_fi"], (int) $row["user_fi"])
219  ->withId((int) $row["finished_id"])
220  ->withFinished((bool) $row["state"])
221  ->withCode((string) $row["anonymous_id"])
222  ->withTimestamp((int) $row["tstamp"])
223  ->withAppraiseeId((int) $row["appr_id"])
224  ->withLastPage((int) $row["lastpage"]);
225  }
226  return null;
227  }
228 
229 
234  public function add(
235  int $survey_id,
236  int $user_id,
237  string $code,
238  int $appraisee_id = 0
239  ): int {
240  $db = $this->db;
241 
242  $next_id = $db->nextId('svy_finished');
243  $affectedRows = $db->manipulateF(
244  "INSERT INTO svy_finished (finished_id, survey_fi, user_fi, anonymous_id, state, tstamp, appr_id) " .
245  "VALUES (%s, %s, %s, %s, %s, %s, %s)",
246  array('integer','integer','integer','text','text','integer','integer'),
247  array($next_id, $survey_id, $user_id, $code, 0, time(), $appraisee_id)
248  );
249 
250  return $next_id;
251  }
252 
256  public function addTime(int $run_id, int $time, int $first_question): void
257  {
258  $db = $this->db;
259  $id = $db->nextId('svy_times');
260  $db->manipulateF(
261  "INSERT INTO svy_times (id, finished_fi, entered_page, left_page, first_question) VALUES (%s, %s, %s, %s,%s)",
262  array('integer','integer', 'integer', 'integer', 'integer'),
263  array($id, $run_id, $time, null, $first_question)
264  );
265  }
266 
267  public function updateTime(int $run_id, int $time, int $entered_time): void
268  {
269  $db = $this->db;
270  $db->manipulateF(
271  "UPDATE svy_times SET left_page = %s WHERE finished_fi = %s AND entered_page = %s",
272  array('integer', 'integer', 'integer'),
273  array($time, $run_id, $entered_time)
274  );
275  }
276 }
manipulateF(string $query, array $types, array $values)
const ANONYMOUS_USER_ID
Definition: constants.php:27
getCurrentRunId(int $survey_id, int $user_id, string $code="", int $appr_id=0)
quote($value, string $type)
addTime(int $run_id, int $time, int $first_question)
Add time record.
global $DIC
Definition: feed.php:28
Code data class.
Definition: class.Run.php:27
nextId(string $table_name)
updateTime(int $run_id, int $time, int $entered_time)
queryF(string $query, array $types, array $values)
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.
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
getUnfinishedSurveysOfUser(int $user_id)
Get all unfinished surveys of a user.
__construct(InternalDataService $data, \ilDBInterface $db=null)
getFinishedSurveysOfUser(int $user_id)
Get all finished surveys of a user.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...