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