ILIAS  release_8 Revision v8.24
class.ilLOTestAssignments.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=0);
25{
26 private static array $instances = [];
27
28 private int $container_id = 0;
32 private array $assignments = [];
34
35 protected ilDBInterface $db;
36
37 public function __construct(int $a_container_id)
38 {
39 global $DIC;
40
41 $this->db = $DIC->database();
42
43 $this->container_id = $a_container_id;
44 $this->settings = ilLOSettings::getInstanceByObjId($a_container_id);
45 $this->readTestAssignments();
46 }
47
48 public static function getInstance(int $a_container_id): self
49 {
50 if (isset(self::$instances[$a_container_id])) {
51 return self::$instances[$a_container_id];
52 }
53 return self::$instances[$a_container_id] = new self($a_container_id);
54 }
55
56 public static function lookupContainerForTest(int $a_test_ref_id): int
57 {
58 global $DIC;
59
60 $ilDB = $DIC['ilDB'];
61
62 $query = 'SELECT container_id FROM loc_tst_assignments ' .
63 'WHERE tst_ref_id = ' . $ilDB->quote($a_test_ref_id, 'integer');
64 $res = $ilDB->query($query);
65 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
66 return $row->container_id;
67 }
68 return 0;
69 }
70
71 public function getContainerId(): int
72 {
74 }
75
76 public function getSettings(): ilLOSettings
77 {
78 return $this->settings;
79 }
80
85 public function getAssignments(): array
86 {
87 return $this->assignments;
88 }
89
90 public static function deleteByContainer(int $a_container_id): void
91 {
92 global $DIC;
93
94 $ilDB = $DIC->database();
95 $query = 'DELETE FROM loc_tst_assignments ' .
96 'WHERE container_id = ' . $ilDB->quote($a_container_id, 'integer');
97 $ilDB->manipulate($query);
98 }
99
104 public function getAssignmentsByType(int $a_type): array
105 {
106 $by_type = array();
107 foreach ($this->assignments as $assignment) {
108 if ($assignment->getAssignmentType() === $a_type) {
109 $by_type[] = $assignment;
110 }
111 }
112 return $by_type;
113 }
114
118 public function getTests(): array
119 {
120 $tests = array();
121 if ($this->getSettings()->getInitialTest()) {
122 $tests[] = $this->getSettings()->getInitialTest();
123 }
124 if ($this->getSettings()->getQualifiedTest()) {
125 $tests[] = $this->getSettings()->getQualifiedTest();
126 }
127 foreach ($this->assignments as $assignment) {
128 $tests[] = $assignment->getTestRefId();
129 }
130 return $tests;
131 }
132
133 public function getTestByObjective(int $a_objective_id, int $a_type): int
134 {
135 switch ($a_type) {
137 if (!$this->getSettings()->hasSeparateInitialTests()) {
138 return $this->getSettings()->getInitialTest();
139 }
140 break;
141
143 if (!$this->getSettings()->hasSeparateQualifiedTests()) {
144 return $this->getSettings()->getQualifiedTest();
145 }
146 break;
147 }
148
149 $assignment = $this->getAssignmentByObjective($a_objective_id, $a_type);
150 if ($assignment) {
151 return $assignment->getTestRefId();
152 }
153 return 0;
154 }
155
156 public function isSeparateTest(int $a_test_ref_id): bool
157 {
158 if (!$this->getSettings()->hasSeparateInitialTests()) {
159 if ($this->getSettings()->getInitialTest() == $a_test_ref_id) {
160 return false;
161 }
162 }
163 if (!$this->getSettings()->hasSeparateQualifiedTests()) {
164 if ($this->getSettings()->getQualifiedTest() == $a_test_ref_id) {
165 return false;
166 }
167 }
168 return true;
169 }
170
171 public function getTypeByTest(int $a_test_ref_id): int
172 {
173 if ($this->getSettings()->worksWithInitialTest() && !$this->getSettings()->hasSeparateInitialTests()) {
174 if ($this->getSettings()->getInitialTest() == $a_test_ref_id) {
176 }
177 } elseif ($this->getSettings()->worksWithInitialTest()) {
178 foreach ($this->assignments as $assignment) {
179 if ($assignment->getTestRefId() == $a_test_ref_id) {
181 }
182 }
183 }
184 if (!$this->getSettings()->hasSeparateQualifiedTests()) {
185 if ($this->getSettings()->getQualifiedTest() == $a_test_ref_id) {
187 }
188 } else {
189 foreach ($this->assignments as $assignment) {
190 if ($assignment->getTestRefId() == $a_test_ref_id) {
192 }
193 }
194 }
196 }
197
198 public function getAssignmentByObjective(int $a_objective_id, int $a_type): ?ilLOTestAssignment
199 {
200 foreach ($this->assignments as $assignment) {
201 if (
202 ($assignment->getObjectiveId() === $a_objective_id) &&
203 ($assignment->getAssignmentType() === $a_type)
204 ) {
205 return $assignment;
206 }
207 }
208 return null;
209 }
210
211 protected function readTestAssignments(): void
212 {
213 $query = 'SELECT assignment_id FROM loc_tst_assignments ' .
214 'WHERE container_id = ' . $this->db->quote($this->getContainerId(), 'integer');
215 $res = $this->db->query($query);
216 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
217 $assignment = new ilLOTestAssignment($row->assignment_id);
218
219 $this->assignments[] = $assignment;
220 }
221 }
222
223 public function toXml(ilXmlWriter $writer, int $a_objective_id): void
224 {
225 foreach ($this->getAssignments() as $assignment) {
226 if ($assignment->getObjectiveId() != $a_objective_id) {
227 continue;
228 }
229 $writer->xmlElement(
230 'Test',
231 array(
233 'refId' => $assignment->getTestRefId(),
234 'testType' => $assignment->getAssignmentType()
235 )
236 );
237 }
238 }
239
240 public static function lookupObjectivesForTest(int $a_test_ref_id): array
241 {
242 global $DIC;
243
244 $ilDB = $DIC->database();
245 $objectives = [];
246 $query = 'SELECT objective_id FROM loc_tst_assignments ' .
247 'WHERE tst_ref_id = ' . $ilDB->quote($a_test_ref_id, 'integer');
248 $res = $ilDB->query($query);
249 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
250 $objectives[] = $row->objective_id;
251 }
252 return $objectives;
253 }
254}
Settings for LO courses.
static getInstanceByObjId(int $a_obj_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(int $a_container_id)
isSeparateTest(int $a_test_ref_id)
toXml(ilXmlWriter $writer, int $a_objective_id)
static lookupContainerForTest(int $a_test_ref_id)
getTestByObjective(int $a_objective_id, int $a_type)
getAssignmentByObjective(int $a_objective_id, int $a_type)
static deleteByContainer(int $a_container_id)
getAssignments()
Get assignments.
getTypeByTest(int $a_test_ref_id)
getAssignmentsByType(int $a_type)
Get assignments by type.
static lookupObjectivesForTest(int $a_test_ref_id)
static getInstance(int $a_container_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
xmlElement(string $tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
global $DIC
Definition: feed.php:28
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
$query
$objectives