ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilAssQuestionPreviewSession.php
Go to the documentation of this file.
1 <?php
2 
26 {
27  public const SESSION_BASEINDEX = 'ilAssQuestionPreviewSessions';
28 
29  public const SESSION_SUBINDEX_INSTANT_RESPONSE_ACTIVE = 'instantResponseActive';
30  public const SESSION_SUBINDEX_PARTICIPANT_SOLUTION = 'participantSolution';
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(): bool
87  {
88  return (bool) $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 setRandomizerSeed($seed): void
107  {
108  $this->saveSessionValue(self::SESSION_SUBINDEX_RANDOMIZER_SEED, $seed);
109  }
110 
111  public function getRandomizerSeed(): ?int
112  {
113  $val = $this->readSessionValue(self::SESSION_SUBINDEX_RANDOMIZER_SEED);
114  return $val === [] ? null : $val;
115  }
116 
117  public function randomizerSeedExists(): bool
118  {
119  return ($this->getRandomizerSeed() !== null);
120  }
121 
122  private function ensureSessionStructureExists(): void
123  {
124  if (!is_array(ilSession::get(self::SESSION_BASEINDEX))) {
125  ilSession::set(self::SESSION_BASEINDEX, []);
126  }
127 
128  $baseSession = ilSession::get(self::SESSION_BASEINDEX);
129 
130  if (!isset($baseSession[$this->getSessionContextIndex()])) {
131  $baseSession[$this->getSessionContextIndex()] = [];
132  }
133 
134  $contextSession = &$baseSession[$this->getSessionContextIndex()];
135 
136  if (!isset($contextSession[self::SESSION_SUBINDEX_INSTANT_RESPONSE_ACTIVE])) {
137  $contextSession[self::SESSION_SUBINDEX_INSTANT_RESPONSE_ACTIVE] = 0;
138  }
139 
140  if (!isset($contextSession[self::SESSION_SUBINDEX_PARTICIPANT_SOLUTION])) {
141  $contextSession[self::SESSION_SUBINDEX_PARTICIPANT_SOLUTION] = null;
142  }
143 
144  if (!isset($contextSession[self::SESSION_SUBINDEX_RANDOMIZER_SEED])) {
145  $contextSession[self::SESSION_SUBINDEX_RANDOMIZER_SEED] = null;
146  }
147 
148  ilSession::set(self::SESSION_BASEINDEX, $baseSession);
149  }
150 }
static get(string $a_var)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
static set(string $a_var, $a_val)
Set a value.