ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilLOTestRun.php
Go to the documentation of this file.
1<?php
24{
25 protected int $container_id = 0;
26 protected int $user_id = 0;
27 protected int $test_id = 0;
28 protected int $objective_id = 0;
29 protected int $max_points = 0;
30 protected array $questions = array();
31
32 protected ilDBInterface $db;
33
34 public function __construct(int $a_crs_id, int $a_user_id, int $a_test_id, int $a_objective_id)
35 {
36 global $DIC;
37
38 $this->db = $DIC->database();
39
40 $this->container_id = $a_crs_id;
41 $this->user_id = $a_user_id;
42 $this->test_id = $a_test_id;
43 $this->objective_id = $a_objective_id;
44
45 $this->read();
46 }
47
48 public static function lookupRunExistsForObjective(int $a_test_id, int $a_objective_id, int $a_user_id): bool
49 {
50 global $DIC;
51
52 $ilDB = $DIC->database();
53 $query = 'SELECT * FROM loc_tst_run ' .
54 'WHERE test_id = ' . $ilDB->quote($a_test_id, 'integer') . ' ' .
55 'AND objective_id = ' . $ilDB->quote($a_objective_id, 'integer') . ' ' .
56 'AND user_id = ' . $ilDB->quote($a_user_id, 'integer');
57 $res = $ilDB->query($query);
58 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
59 return true;
60 }
61 return false;
62 }
63
64 public static function deleteRuns(int $a_container_id, int $a_user_id): void
65 {
66 global $DIC;
67
68 $ilDB = $DIC['ilDB'];
69 $query = 'DELETE FROM loc_tst_run ' .
70 'WHERE container_id = ' . $ilDB->quote($a_container_id, 'integer') . ' ' .
71 'AND user_id = ' . $ilDB->quote($a_user_id, 'integer') . ' ';
72 $ilDB->manipulate($query);
73 }
74
75 public static function deleteRun(int $a_container_id, int $a_user_id, int $a_test_id): void
76 {
77 global $DIC;
78
79 $ilDB = $DIC->database();
80 $query = 'DELETE FROM loc_tst_run ' .
81 'WHERE container_id = ' . $ilDB->quote($a_container_id, 'integer') . ' ' .
82 'AND user_id = ' . $ilDB->quote($a_user_id, 'integer') . ' ' .
83 'AND test_id = ' . $ilDB->quote($a_test_id, 'integer') . ' ';
84 $ilDB->manipulate($query);
85 }
86
87 public static function lookupObjectives(int $a_container_id, int $a_user_id, int $a_test_id): array
88 {
89 global $DIC;
90
91 $ilDB = $DIC->database();
92 $query = 'SELECT objective_id FROM loc_tst_run ' .
93 'WHERE container_id = ' . $ilDB->quote($a_container_id, 'integer') . ' ' .
94 'AND user_id = ' . $ilDB->quote($a_user_id, 'integer') . ' ' .
95 'AND test_id = ' . $ilDB->quote($a_test_id, 'integer');
96 $res = $ilDB->query($query);
97 $objectives = array();
98 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
99 $objectives[] = $row->objective_id;
100 }
101 return $objectives;
102 }
103
107 public static function getRun(int $a_container_id, int $a_user_id, int $a_test_id): array
108 {
109 global $DIC;
110
111 $ilDB = $DIC->database();
112 $query = 'SELECT objective_id FROM loc_tst_run ' .
113 'WHERE container_id = ' . $ilDB->quote($a_container_id, 'integer') . ' ' .
114 'AND user_id = ' . $ilDB->quote($a_user_id, 'integer') . ' ' .
115 'AND test_id = ' . $ilDB->quote($a_test_id, 'integer') . ' ';
116 $res = $ilDB->query($query);
117
118 $run = array();
119 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
120 $run[] = new ilLOTestRun($a_container_id, $a_user_id, $a_test_id, $row->objective_id);
121 }
122 return $run;
123 }
124
125 public function getContainerId(): int
126 {
127 return $this->container_id;
128 }
129
130 public function getUserId(): int
131 {
132 return $this->user_id;
133 }
134
135 public function getTestId(): int
136 {
137 return $this->test_id;
138 }
139
140 public function getObjectiveId(): int
141 {
142 return $this->objective_id;
143 }
144
145 public function getMaxPoints(): int
146 {
147 return $this->max_points;
148 }
149
150 public function setMaxPoints(int $a_points): void
151 {
152 $this->max_points = $a_points;
153 }
154
155 public function getQuestions(): array
156 {
157 return $this->questions;
158 }
159
160 public function clearQuestions(): void
161 {
162 $this->questions = array();
163 }
164
165 public function addQuestion(int $a_id): void
166 {
167 $this->questions[$a_id] = 0;
168 }
169
170 public function questionExists(int $a_question_id): bool
171 {
172 return array_key_exists($a_question_id, $this->questions);
173 }
174
175 public function setQuestionResult(int $a_qst_id, float $a_points): void
176 {
177 $this->questions[$a_qst_id] = $a_points;
178 }
179
180 public function getResult(): array
181 {
182 $sum_points = 0;
183 foreach ($this->questions as $points) {
184 $sum_points += $points;
185 }
186
187 $percentage =
188 ($this->getMaxPoints() > 0) ?
189 round(($sum_points / $this->getMaxPoints() * 100), 0) :
190 100;
191
192 return array(
193 'max' => $this->getMaxPoints(),
194 'reached' => $sum_points,
195 'percentage' => $percentage
196 );
197 }
198
199 public function delete(): void
200 {
201 $query = 'DELETE FROM loc_tst_run ' .
202 'WHERE container_id = ' . $this->db->quote($this->getContainerId(), 'integer') . ' ' .
203 'AND user_id = ' . $this->db->quote($this->getUserId(), 'integer') . ' ' .
204 'AND test_id = ' . $this->db->quote($this->getTestId(), 'integer') . ' ' .
205 'AND objective_id = ' . $this->db->quote($this->getObjectiveId(), 'integer');
206 $this->db->manipulate($query);
207 }
208
209 public function create(): void
210 {
211 $query = 'INSERT INTO loc_tst_run ' .
212 '(container_id, user_id, test_id, objective_id,max_points,questions) ' .
213 'VALUES( ' .
214 $this->db->quote($this->getContainerId(), 'integer') . ', ' .
215 $this->db->quote($this->getUserId(), 'integer') . ', ' .
216 $this->db->quote($this->getTestId(), 'integer') . ', ' .
217 $this->db->quote($this->getObjectiveId(), 'integer') . ', ' .
218 $this->db->quote($this->getMaxPoints(), 'integer') . ', ' .
219 $this->db->quote(serialize($this->getQuestions()), 'text') . ' ' .
220 ')';
221 $this->db->manipulate($query);
222 }
223
224 public function update(): void
225 {
226 $query = 'UPDATE loc_tst_run SET ' .
227 'max_points = ' . $this->db->quote($this->getMaxPoints(), 'integer') . ', ' .
228 'questions = ' . $this->db->quote(serialize($this->getQuestions()), 'text') . ' ' .
229 'WHERE container_id = ' . $this->db->quote($this->container_id, 'integer') . ' ' .
230 'AND user_id = ' . $this->db->quote($this->getUserId(), 'integer') . ' ' .
231 'AND test_id = ' . $this->db->quote($this->getTestId(), 'integer') . ' ' .
232 'AND objective_id = ' . $this->db->quote($this->getObjectiveId(), 'integer') . ' ';
233 $this->db->manipulate($query);
234 }
235
236 public function read(): void
237 {
238 $query = 'SELECT * FROM loc_tst_run ' .
239 'WHERE container_id = ' . $this->db->quote($this->getContainerId(), 'integer') . ' ' .
240 'AND user_id = ' . $this->db->quote($this->getUserId(), 'integer') . ' ' .
241 'AND test_id = ' . $this->db->quote($this->getTestId(), 'integer') . ' ' .
242 'AND objective_id = ' . $this->db->quote($this->getObjectiveId(), 'integer');
243 $res = $this->db->query($query);
244 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
245 $this->max_points = $row->max_points;
246 if ($row->questions) {
247 $this->questions = unserialize($row->questions);
248 }
249 }
250 }
251}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
addQuestion(int $a_id)
setQuestionResult(int $a_qst_id, float $a_points)
static deleteRuns(int $a_container_id, int $a_user_id)
setMaxPoints(int $a_points)
__construct(int $a_crs_id, int $a_user_id, int $a_test_id, int $a_objective_id)
ilDBInterface $db
static deleteRun(int $a_container_id, int $a_user_id, int $a_test_id)
static lookupRunExistsForObjective(int $a_test_id, int $a_objective_id, int $a_user_id)
questionExists(int $a_question_id)
static lookupObjectives(int $a_container_id, int $a_user_id, int $a_test_id)
static getRun(int $a_container_id, int $a_user_id, int $a_test_id)
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
global $DIC
Definition: shib_login.php:26
$objectives