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