ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilAssQuestionUserSolutionAdopter.php
Go to the documentation of this file.
1 <?php
2 
26 {
27  protected static $preparedDeleteSolutionRecordsStatement = null;
28 
29  protected static $preparedSelectSolutionRecordsStatement = null;
30 
31  protected static $preparedInsertSolutionRecordStatement = null;
32 
33  protected static $preparedDeleteResultRecordStatement = null;
34 
35  protected static $preparedSelectResultRecordStatement = null;
36 
37  protected static $preparedInsertResultRecordStatement = null;
38 
42  protected $db;
43 
48 
52  protected $userId;
53 
57  protected $activeId;
58 
62  protected $targetPass;
63 
67  protected $questionIds;
68 
74  public function __construct(ilDBInterface $db, ilSetting $assSettings, $isAssessmentLogEnabled)
75  {
76  $this->db = $db;
77 
78  $this->userId = null;
79  $this->activeId = null;
80  $this->targetPass = null;
81  $this->questionIds = array();
82 
83  $this->processLockerFactory = new ilAssQuestionProcessLockerFactory($assSettings, $db);
84  $this->processLockerFactory->setAssessmentLogEnabled($isAssessmentLogEnabled);
85  }
86 
90  public function getUserId(): ?int
91  {
92  return $this->userId;
93  }
94 
98  public function setUserId($userId): void
99  {
100  $this->userId = $userId;
101  }
102 
106  public function getActiveId(): ?int
107  {
108  return $this->activeId;
109  }
110 
114  public function setActiveId($activeId): void
115  {
116  $this->activeId = $activeId;
117  }
118 
122  public function getTargetPass(): ?int
123  {
124  return $this->targetPass;
125  }
126 
130  public function setTargetPass($targetPass): void
131  {
132  $this->targetPass = $targetPass;
133  }
134 
138  public function getQuestionIds(): array
139  {
140  return $this->questionIds;
141  }
142 
146  public function setQuestionIds($questionIds): void
147  {
148  $this->questionIds = $questionIds;
149  }
150 
151  public function perform(): void
152  {
153  $this->processLockerFactory->setUserId($this->getUserId());
154 
155  foreach ($this->getQuestionIds() as $questionId) {
156  $this->processLockerFactory->setQuestionId($questionId);
157  $processLocker = $this->processLockerFactory->getLocker();
158 
159  $processLocker->executeUserTestResultUpdateLockOperation(function () use ($questionId) {
160  $this->adoptQuestionAnswer($questionId);
161  });
162  }
163  }
164 
165  protected function adoptQuestionAnswer($questionId): void
166  {
167  $this->resetTargetSolution($questionId);
168  $this->resetTargetResult($questionId);
169 
170  $sourcePass = $this->adoptSourceSolution($questionId);
171 
172  if ($sourcePass !== null) {
173  $this->adoptSourceResult($questionId, $sourcePass);
174  }
175  }
176 
177  protected function resetTargetSolution($questionId): void
178  {
179  $this->db->execute(
181  array($this->getActiveId(), $questionId, $this->getTargetPass())
182  );
183  }
184 
185  protected function resetTargetResult($questionId): void
186  {
187  $this->db->execute(
189  array($this->getActiveId(), $questionId, $this->getTargetPass())
190  );
191  }
192 
193  protected function adoptSourceSolution($questionId)
194  {
195  $res = $this->db->execute(
197  array($this->getActiveId(), $questionId, $this->getTargetPass())
198  );
199 
200  $sourcePass = null;
201 
202  while ($row = $this->db->fetchAssoc($res)) {
203  if ($sourcePass === null) {
204  $sourcePass = $row['pass'];
205  } elseif ($row['pass'] < $sourcePass) {
206  break;
207  }
208 
209  $solutionId = $this->db->nextId('tst_solutions');
210 
211  $this->db->execute($this->getPreparedInsertSolutionRecordStatement(), array(
212  $solutionId, $this->getActiveId(), $questionId, $this->getTargetPass(), time(),
213  $row['points'], $row['value1'], $row['value2']
214  ));
215  }
216 
217  return $sourcePass;
218  }
219 
220  protected function adoptSourceResult($questionId, $sourcePass): void
221  {
222  $res = $this->db->execute(
224  array($this->getActiveId(), $questionId, $sourcePass)
225  );
226 
227  $row = $this->db->fetchAssoc($res);
228 
229  $resultId = $this->db->nextId('tst_test_result');
230 
231  $this->db->execute($this->getPreparedInsertResultRecordStatement(), array(
232  $resultId, $this->getActiveId(), $questionId, $this->getTargetPass(), time(),
233  $row['points'], $row['manual'], $row['hint_count'], $row['hint_points'], $row['answered']
234  ));
235  }
236 
238  {
239  if (self::$preparedDeleteSolutionRecordsStatement === null) {
240  self::$preparedDeleteSolutionRecordsStatement = $this->db->prepareManip(
241  "DELETE FROM tst_solutions WHERE active_fi = ? AND question_fi = ? AND pass = ?",
242  array('integer', 'integer', 'integer')
243  );
244  }
245 
246  return self::$preparedDeleteSolutionRecordsStatement;
247  }
248 
250  {
251  if (self::$preparedSelectSolutionRecordsStatement === null) {
252  $query = "
253  SELECT pass, points, value1, value2 FROM tst_solutions
254  WHERE active_fi = ? AND question_fi = ? AND pass < ? ORDER BY pass DESC
255  ";
256 
257  self::$preparedSelectSolutionRecordsStatement = $this->db->prepare(
258  $query,
259  array('integer', 'integer', 'integer')
260  );
261  }
262 
263  return self::$preparedSelectSolutionRecordsStatement;
264  }
265 
267  {
268  if (self::$preparedInsertSolutionRecordStatement === null) {
269  $query = "
270  INSERT INTO tst_solutions (
271  solution_id, active_fi, question_fi, pass, tstamp, points, value1, value2
272  ) VALUES (
273  ?, ?, ?, ?, ?, ?, ?, ?
274  )
275  ";
276 
277  self::$preparedInsertSolutionRecordStatement = $this->db->prepareManip(
278  $query,
279  array('integer', 'integer', 'integer', 'integer', 'integer', 'integer', 'text', 'text')
280  );
281  }
282 
283  return self::$preparedInsertSolutionRecordStatement;
284  }
285 
287  {
288  if (self::$preparedDeleteResultRecordStatement === null) {
289  self::$preparedDeleteResultRecordStatement = $this->db->prepareManip(
290  "DELETE FROM tst_test_result WHERE active_fi = ? AND question_fi = ? AND pass = ?",
291  array('integer', 'integer', 'integer')
292  );
293  }
294 
295  return self::$preparedDeleteResultRecordStatement;
296  }
297 
299  {
300  if (self::$preparedSelectResultRecordStatement === null) {
301  $query = "
302  SELECT points, manual, hint_count, hint_points, answered FROM tst_test_result
303  WHERE active_fi = ? AND question_fi = ? AND pass = ?
304  ";
305 
306  self::$preparedSelectResultRecordStatement = $this->db->prepare(
307  $query,
308  array('integer', 'integer', 'integer')
309  );
310  }
311 
312  return self::$preparedSelectResultRecordStatement;
313  }
314 
316  {
317  if (self::$preparedInsertResultRecordStatement === null) {
318  $query = "
319  INSERT INTO tst_test_result (
320  test_result_id, active_fi, question_fi, pass, tstamp,
321  points, manual, hint_count, hint_points, answered
322  ) VALUES (
323  ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
324  )
325  ";
326 
327  self::$preparedInsertResultRecordStatement = $this->db->prepareManip(
328  $query,
329  array('integer', 'integer', 'integer', 'integer', 'integer', 'integer', 'integer', 'integer', 'integer', 'integer')
330  );
331  }
332 
333  return self::$preparedInsertResultRecordStatement;
334  }
335 }
$res
Definition: ltiservices.php:69
__construct(ilDBInterface $db, ilSetting $assSettings, $isAssessmentLogEnabled)