ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilAssQuestionPreviewSession.php
Go to the documentation of this file.
1 <?php
25 {
26  public const SESSION_BASEINDEX = 'ilAssQuestionPreviewSessions';
27 
28  public const SESSION_SUBINDEX_INSTANT_RESPONSE_ACTIVE = 'instantResponseActive';
29  public const SESSION_SUBINDEX_PARTICIPANT_SOLUTION = 'participantSolution';
30  public const SESSION_SUBINDEX_REQUESTED_HINTS = 'requestedHints';
31  public const SESSION_SUBINDEX_RANDOMIZER_SEED = 'randomizerSeed';
32 
33  private $userId;
34  private $questionId;
35 
36  public function __construct($userId, $questionId)
37  {
38  $this->userId = $userId;
39  $this->questionId = $questionId;
40  }
41 
42  public function init(): void
43  {
45  }
46 
47  public function getUserId()
48  {
49  return $this->userId;
50  }
51 
52  public function getQuestionId()
53  {
54  return $this->questionId;
55  }
56 
57  private function getSessionContextIndex(): string
58  {
59  return "u{$this->userId}::q{$this->questionId}";
60  }
61 
62  private function saveSessionValue($subIndex, $value): void
63  {
64  $val = ilSession::get(self::SESSION_BASEINDEX);
65  $val[$this->getSessionContextIndex()][$subIndex] = $value;
66  ilSession::set(self::SESSION_BASEINDEX, $val);
67  }
68 
69  private function issetSessionValue($subIndex): bool
70  {
71  $val = ilSession::get(self::SESSION_BASEINDEX);
72  return isset($val[$this->getSessionContextIndex()][$subIndex]);
73  }
74 
75  private function readSessionValue($subIndex)
76  {
77  $val = ilSession::get(self::SESSION_BASEINDEX);
78  return $val[$this->getSessionContextIndex()][$subIndex] ?? [];
79  }
80 
81  public function setInstantResponseActive($instantResponseActive): void
82  {
83  $this->saveSessionValue(self::SESSION_SUBINDEX_INSTANT_RESPONSE_ACTIVE, $instantResponseActive);
84  }
85 
86  public function isInstantResponseActive()
87  {
88  return $this->readSessionValue(self::SESSION_SUBINDEX_INSTANT_RESPONSE_ACTIVE);
89  }
90 
91  public function setParticipantsSolution($participantSolution): void
92  {
93  $this->saveSessionValue(self::SESSION_SUBINDEX_PARTICIPANT_SOLUTION, $participantSolution);
94  }
95 
96  public function getParticipantsSolution()
97  {
98  return $this->readSessionValue(self::SESSION_SUBINDEX_PARTICIPANT_SOLUTION) == [] ? null : $this->readSessionValue(self::SESSION_SUBINDEX_PARTICIPANT_SOLUTION);
99  }
100 
101  public function hasParticipantSolution(): bool
102  {
103  return $this->issetSessionValue(self::SESSION_SUBINDEX_PARTICIPANT_SOLUTION);
104  }
105 
106  public function getNumRequestedHints(): int
107  {
108  if (!$this->issetSessionValue(self::SESSION_SUBINDEX_REQUESTED_HINTS)) {
109  return 0;
110  }
111  $hints = $this->readSessionValue(self::SESSION_SUBINDEX_REQUESTED_HINTS);
112 
113  if (!is_array($hints)) {
114  return 0;
115  }
116 
117  return count($hints);
118  }
119 
120  public function isHintRequested($hintId): bool
121  {
122  if ($this->issetSessionValue(self::SESSION_SUBINDEX_REQUESTED_HINTS)) {
123  $requestedHints = $this->readSessionValue(self::SESSION_SUBINDEX_REQUESTED_HINTS);
124  return isset($requestedHints[$hintId]);
125  }
126 
127  return false;
128  }
129 
130  public function addRequestedHint($hintId): void
131  {
132  $requestedHints = $this->getRequestedHints();
133  $requestedHints[$hintId] = $hintId;
134  $this->saveSessionValue(self::SESSION_SUBINDEX_REQUESTED_HINTS, $requestedHints);
135  }
136 
137  public function getRequestedHints()
138  {
139  if ($this->issetSessionValue(self::SESSION_SUBINDEX_REQUESTED_HINTS)) {
140  return $this->readSessionValue(self::SESSION_SUBINDEX_REQUESTED_HINTS);
141  }
142 
143  return [];
144  }
145 
146  public function resetRequestedHints(): void
147  {
148  $this->saveSessionValue(self::SESSION_SUBINDEX_REQUESTED_HINTS, array());
149  }
150 
151  public function setRandomizerSeed($seed): void
152  {
153  $this->saveSessionValue(self::SESSION_SUBINDEX_RANDOMIZER_SEED, $seed);
154  }
155 
156  public function getRandomizerSeed(): ?int
157  {
158  $val = $this->readSessionValue(self::SESSION_SUBINDEX_RANDOMIZER_SEED);
159  return $val === [] ? null : $val;
160  }
161 
162  public function randomizerSeedExists(): bool
163  {
164  return ($this->getRandomizerSeed() !== null);
165  }
166 
167  private function ensureSessionStructureExists(): void
168  {
169  if (!is_array(ilSession::get(self::SESSION_BASEINDEX))) {
170  ilSession::set(self::SESSION_BASEINDEX, array());
171  }
172 
173  $baseSession = ilSession::get(self::SESSION_BASEINDEX);
174 
175  if (!isset($baseSession[$this->getSessionContextIndex()])) {
176  $baseSession[$this->getSessionContextIndex()] = array();
177  }
178 
179  $contextSession = &$baseSession[$this->getSessionContextIndex()];
180 
181  if (!isset($contextSession[self::SESSION_SUBINDEX_INSTANT_RESPONSE_ACTIVE])) {
182  $contextSession[self::SESSION_SUBINDEX_INSTANT_RESPONSE_ACTIVE] = 0;
183  }
184 
185  if (!isset($contextSession[self::SESSION_SUBINDEX_PARTICIPANT_SOLUTION])) {
186  $contextSession[self::SESSION_SUBINDEX_PARTICIPANT_SOLUTION] = null;
187  }
188 
189  if (!isset($contextSession[self::SESSION_SUBINDEX_RANDOMIZER_SEED])) {
190  $contextSession[self::SESSION_SUBINDEX_RANDOMIZER_SEED] = null;
191  }
192 
193  ilSession::set(self::SESSION_BASEINDEX, $baseSession);
194  }
195 }
static get(string $a_var)
static set(string $a_var, $a_val)
Set a value.