ILIAS  release_8 Revision v8.23
class.ilTestSessionDynamicQuestionSet.php
Go to the documentation of this file.
1 <?php
2 
28 {
33 
34  public function __construct()
35  {
37 
38  $this->questionSetFilterSelection = new ilTestDynamicQuestionSetFilterSelection();
39  }
40 
45  {
47  }
48 
49  public function loadFromDb($active_id)
50  {
51  global $DIC;
52  $ilDB = $DIC['ilDB'];
53  $result = $ilDB->queryF(
54  "SELECT * FROM tst_active WHERE active_id = %s",
55  array('integer'),
56  array($active_id)
57  );
58  if ($result->numRows()) {
59  $row = $ilDB->fetchAssoc($result);
60  $this->active_id = $row["active_id"];
61  $this->user_id = $row["user_fi"];
62  $this->anonymous_id = $row["anonymous_id"];
63  $this->test_id = $row["test_fi"];
64  $this->lastsequence = $row["lastindex"];
65  $this->submitted = ($row["submitted"]) ? true : false;
66  $this->submittedTimestamp = $row["submittimestamp"];
67  $this->tstamp = $row["tstamp"];
68 
69  $this->questionSetFilterSelection->setTaxonomySelection(unserialize($row['taxfilter']));
70  $this->questionSetFilterSelection->setAnswerStatusSelection($row['answerstatusfilter']);
71  $this->questionSetFilterSelection->setAnswerStatusActiveId($row['active_id']);
72  }
73  }
74 
75  public function loadTestSession($test_id, $user_id = "", $anonymous_id = ""): void
76  {
77  global $DIC;
78  $ilDB = $DIC['ilDB'];
79  $ilUser = $DIC['ilUser'];
80 
81  if (!$user_id) {
82  $user_id = $ilUser->getId();
83  }
84  if (($GLOBALS['DIC']['ilUser']->getId() == ANONYMOUS_USER_ID) && $this->doesAccessCodeInSessionExists()) {
85  $result = $ilDB->queryF(
86  "SELECT * FROM tst_active WHERE user_fi = %s AND test_fi = %s AND anonymous_id = %s",
87  array('integer','integer','text'),
89  );
90  } elseif (strlen($anonymous_id)) {
91  $result = $ilDB->queryF(
92  "SELECT * FROM tst_active WHERE user_fi = %s AND test_fi = %s AND anonymous_id = %s",
93  array('integer','integer','text'),
95  );
96  } else {
97  if ($GLOBALS['DIC']['ilUser']->getId() == ANONYMOUS_USER_ID) {
98  return;
99  }
100  $result = $ilDB->queryF(
101  "SELECT * FROM tst_active WHERE user_fi = %s AND test_fi = %s",
102  array('integer','integer'),
103  array($user_id, $test_id)
104  );
105  }
106 
107  // TODO bheyser: Refactor
108  $this->user_id = $user_id;
109 
110  if ($result->numRows()) {
111  $row = $ilDB->fetchAssoc($result);
112  $this->active_id = $row["active_id"];
113  $this->user_id = $row["user_fi"];
114  $this->anonymous_id = $row["anonymous_id"];
115  $this->test_id = $row["test_fi"];
116  $this->lastsequence = $row["lastindex"];
117  $this->submitted = ($row["submitted"]) ? true : false;
118  $this->submittedTimestamp = $row["submittimestamp"];
119  $this->tstamp = $row["tstamp"];
120 
121  $this->questionSetFilterSelection->setTaxonomySelection(unserialize($row['taxfilter']));
122  $this->questionSetFilterSelection->setAnswerStatusSelection($row['answerstatusfilter']);
123  $this->questionSetFilterSelection->setAnswerStatusActiveId($row['active_id']);
124  } elseif ($this->doesAccessCodeInSessionExists()) {
125  $this->unsetAccessCodeInSession();
126  }
127  }
128 
129  public function saveToDb(): void
130  {
131  global $DIC;
132  $ilDB = $DIC['ilDB'];
133  $ilLog = $DIC['ilLog'];
134 
135  $submitted = ($this->isSubmitted()) ? 1 : 0;
136  if ($this->active_id > 0) {
137  $affectedRows = $ilDB->update(
138  'tst_active',
139  array(
140  'lastindex' => array('integer', $this->getLastSequence()),
141  'tries' => array('integer', $this->getPass()),
142  'submitted' => array('integer', $submitted),
143  'submittimestamp' => array('timestamp', (strlen($this->getSubmittedTimestamp())) ? $this->getSubmittedTimestamp() : null),
144  'tstamp' => array('integer', time() - 10),
145  'taxfilter' => array('text', serialize($this->getQuestionSetFilterSelection()->getTaxonomySelection())),
146  'answerstatusfilter' => array('text', $this->getQuestionSetFilterSelection()->getAnswerStatusSelection())
147  ),
148  array(
149  'active_id' => array('integer', $this->getActiveId())
150  )
151  );
152 
153  // update learning progress
154  include_once("./Modules/Test/classes/class.ilObjTestAccess.php");
155  include_once("./Services/Tracking/classes/class.ilLPStatusWrapper.php");
159  );
160  } else {
161  if (!$this->activeIDExists($this->getUserId(), $this->getTestId())) {
162  $anonymous_id = ($this->getAnonymousId()) ?: null;
163 
164  $next_id = $ilDB->nextId('tst_active');
165  $affectedRows = $ilDB->insert(
166  'tst_active',
167  array(
168  'active_id' => array('integer', $next_id),
169  'user_fi' => array('integer', $this->getUserId()),
170  'anonymous_id' => array('text', $anonymous_id),
171  'test_fi' => array('integer', $this->getTestId()),
172  'lastindex' => array('integer', $this->getLastSequence()),
173  'tries' => array('integer', $this->getPass()),
174  'submitted' => array('integer', $submitted),
175  'submittimestamp' => array('timestamp', (strlen($this->getSubmittedTimestamp())) ? $this->getSubmittedTimestamp() : null),
176  'tstamp' => array('integer', time() - 10),
177  'taxfilter' => array('text', serialize($this->getQuestionSetFilterSelection()->getTaxonomySelection())),
178  'answerstatusfilter' => array('text', $this->getQuestionSetFilterSelection()->getAnswerStatusSelection())
179  )
180  );
181  $this->active_id = $next_id;
182 
183  // update learning progress
184  include_once("./Modules/Test/classes/class.ilObjTestAccess.php");
185  include_once("./Services/Tracking/classes/class.ilLPStatusWrapper.php");
188  $this->getUserId()
189  );
190  }
191  }
192 
193  include_once("./Services/Tracking/classes/class.ilLearningProgress.php");
195  $this->getUserId(),
197  $this->getRefId(),
198  'tst'
199  );
200  }
201 
202  public function getCurrentQuestionId(): int
203  {
204  return $this->getLastSequence();
205  }
206 
207  public function setCurrentQuestionId($currentQuestionId)
208  {
209  $this->setLastSequence((int) $currentQuestionId);
210  }
211 }
const ANONYMOUS_USER_ID
Definition: constants.php:27
static _getParticipantId($active_id)
Get user id for active id.
static _tracProgress(int $a_user_id, int $a_obj_id, int $a_ref_id, string $a_obj_type='')
global $DIC
Definition: feed.php:28
loadTestSession($test_id, $user_id="", $anonymous_id="")
static _lookupObjIdForTestId($a_test_id)
Lookup object id for test id.
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
__construct(Container $dic, ilPlugin $plugin)
$ilUser
Definition: imgupload.php:34
setLastSequence($lastsequence)
activeIDExists($user_id, $test_id)
static _updateStatus(int $a_obj_id, int $a_usr_id, ?object $a_obj=null, bool $a_percentage=false, bool $a_force_raise=false)