ILIAS  trunk Revision v11.0_alpha-1843-g9e1fad99175
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilLOSettings.php
Go to the documentation of this file.
1 <?php
18 declare(strict_types=0);
19 
26 {
27  // new settings 5.1
28  public const QST_PASSED_FLAG = 1;
29  public const QST_PASSED_HIDE = 2;
30 
31  public const TYPE_INITIAL_PLACEMENT_ALL = 1;
33  public const TYPE_INITIAL_QUALIFYING_ALL = 3;
35  public const TYPE_INITIAL_NONE = 5;
36 
37  public const TYPE_QUALIFYING_ALL = 1;
38  public const TYPE_QUALIFYING_SELECTED = 2;
39 
40  // end new settings
41 
42  public const TYPE_TEST_UNDEFINED = 0;
43  public const TYPE_TEST_INITIAL = 1;
44  public const TYPE_TEST_QUALIFIED = 2;
45 
46  public const QT_VISIBLE_ALL = 0;
47  public const QT_VISIBLE_OBJECTIVE = 1;
48 
49  public const LOC_INITIAL_ALL = 1;
50  public const LOC_INITIAL_SEL = 2;
51  public const LOC_QUALIFIED = 3;
52  public const LOC_PRACTISE = 4;
53 
54  public const HIDE_PASSED_OBJECTIVE_QST = 1;
55  public const MARK_PASSED_OBJECTIVE_QST = 2;
56 
57  private static array $instances = [];
58 
59  private int $it_type = self::TYPE_INITIAL_PLACEMENT_ALL;
60  private int $qt_type = self::TYPE_QUALIFYING_ALL;
61 
62  private bool $it_start = false;
63  private bool $qt_start = false;
64  private int $container_id = 0;
65  private int $type = 0;
66  private int $initial_test = 0;
67  private int $qualified_test = 0;
68  private bool $reset_results = true;
69  private int $passed_objective_mode = self::HIDE_PASSED_OBJECTIVE_QST;
70 
71  private bool $entry_exists = false;
72 
73  private ilLogger $logger;
74  protected ilDBInterface $db;
75  protected ilTree $tree;
76 
77  protected function __construct(int $a_cont_id)
78  {
79  global $DIC;
80 
81  $this->logger = $DIC->logger()->crs();
82  $this->db = $DIC->database();
83  $this->tree = $DIC->repositoryTree();
84  $this->container_id = $a_cont_id;
85  $this->read();
86  }
87 
88  public static function getInstanceByObjId(int $a_obj_id): self
89  {
90  if (isset(self::$instances[$a_obj_id])) {
91  return self::$instances[$a_obj_id];
92  }
93  return self::$instances[$a_obj_id] = new ilLOSettings($a_obj_id);
94  }
95 
96  public function setInitialTestType(int $a_type): void
97  {
98  $this->it_type = $a_type;
99  }
100 
101  public function getInitialTestType(): int
102  {
103  return $this->it_type;
104  }
105 
106  public function getQualifyingTestType(): int
107  {
108  return $this->qt_type;
109  }
110 
111  public function setQualifyingTestType(int $a_type): void
112  {
113  $this->qt_type = $a_type;
114  }
115 
116  public function setInitialTestAsStart(bool $a_type): void
117  {
118  $this->it_start = $a_type;
119  }
120 
121  public function isInitialTestStart(): bool
122  {
123  return $this->it_start;
124  }
125 
126  public function setQualifyingTestAsStart(bool $a_type): void
127  {
128  $this->qt_start = $a_type;
129  }
130 
131  public function isQualifyingTestStart(): bool
132  {
133  return $this->qt_start;
134  }
135 
139  public function hasSeparateInitialTests(): bool
140  {
141  return $this->getInitialTestType() == self::TYPE_INITIAL_PLACEMENT_SELECTED || $this->getInitialTestType() == self::TYPE_INITIAL_QUALIFYING_SELECTED;
142  }
143 
147  public function hasSeparateQualifiedTests(): bool
148  {
149  return $this->getQualifyingTestType() == self::TYPE_QUALIFYING_SELECTED;
150  }
151 
155  public function isInitialTestQualifying(): bool
156  {
157  return $this->getInitialTestType() == self::TYPE_INITIAL_QUALIFYING_ALL || $this->getInitialTestType() == self::TYPE_INITIAL_QUALIFYING_SELECTED;
158  }
159 
163  public static function isObjectiveTest(int $a_trst_ref_id): bool
164  {
165  global $DIC;
166 
167  $ilDB = $DIC->database();
168  // Check for direct assignment
169  $query = 'SELECT obj_id FROM loc_settings ' .
170  'WHERE itest = ' . $ilDB->quote($a_trst_ref_id, 'integer') . ' ' .
171  'OR qtest = ' . $ilDB->quote($a_trst_ref_id, 'integer');
172  $res = $ilDB->query($query);
173  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
174  return $row->obj_id;
175  }
176  return (bool) ilLOTestAssignments::lookupContainerForTest($a_trst_ref_id);
177  }
178 
179  public static function cloneSettings(int $a_copy_id, int $a_container_id, int $a_new_container_id): void
180  {
181  $options = ilCopyWizardOptions::_getInstance($a_copy_id);
182  $mappings = $options->getMappings();
183 
184  $settings = self::getInstanceByObjId($a_container_id);
185  $new_settings = self::getInstanceByObjId($a_new_container_id);
186 
187  $new_settings->setType($settings->getType());
188  $new_settings->setInitialTestType($settings->getInitialTestType());
189  $new_settings->setQualifyingTestType($settings->getQualifyingTestType());
190  $new_settings->resetResults($settings->isResetResultsEnabled());
191  $new_settings->setPassedObjectiveMode($settings->getPassedObjectiveMode());
192 
193  if ($settings->getInitialTest() && array_key_exists($settings->getInitialTest(), $mappings)) {
194  $new_settings->setInitialTest($mappings[$settings->getInitialTest()]);
195  $new_settings->setInitialTestAsStart($new_settings->isInitialTestStart());
196  }
197 
198  if ($settings->getQualifiedTest() && array_key_exists($settings->getQualifiedTest(), $mappings)) {
199  $new_settings->setQualifiedTest($mappings[$settings->getQualifiedTest()]);
200  $new_settings->setQualifyingTestAsStart($settings->isQualifyingTestStart());
201  }
202 
203  // update calls create in case of no entry exists.
204  $new_settings->update();
205  }
206 
210  public function worksWithStartObjects(): bool
211  {
212  return $this->isInitialTestStart() || $this->isQualifyingTestStart();
213  }
214 
218  public function worksWithInitialTest(): bool
219  {
220  return $this->getInitialTestType() !== self::TYPE_INITIAL_NONE;
221  }
222 
223  public function isGeneralQualifiedTestVisible(): bool
224  {
225  return $this->getQualifyingTestType() === self::TYPE_QUALIFYING_ALL;
226  }
227 
228  public function getPassedObjectiveMode(): int
229  {
231  }
232 
233  public function setPassedObjectiveMode(int $a_mode): void
234  {
235  $this->passed_objective_mode = $a_mode;
236  }
237 
238  public function isGeneralInitialTestVisible(): bool
239  {
240  return
241  $this->getInitialTestType() === self::TYPE_INITIAL_PLACEMENT_ALL ||
242  $this->getInitialTestType() === self::TYPE_INITIAL_QUALIFYING_ALL;
243  }
244 
245  public function settingsExist(): bool
246  {
247  return $this->entry_exists;
248  }
249 
250  public function getObjId(): int
251  {
252  return $this->container_id;
253  }
254 
255  public function setType(int $a_type): void
256  {
257  $this->type = $a_type;
258  }
259 
260  public function getType(): int
261  {
262  return $this->type;
263  }
264 
265  public function getTestByType(int $a_type): int
266  {
267  switch ($a_type) {
268  case self::TYPE_TEST_INITIAL:
269  return $this->getInitialTest();
270 
271  case self::TYPE_TEST_QUALIFIED:
272  return $this->getQualifiedTest();
273  }
274  return 0;
275  }
276 
277  public function getTests(): array
278  {
279  $tests = array();
280  if ($this->getInitialTest()) {
281  $tests[] = $this->getInitialTest();
282  }
283  if ($this->getQualifiedTest()) {
284  $tests[] = $this->getQualifiedTest();
285  }
286  return $tests;
287  }
288 
289  public function isRandomTestType(int $a_type): bool
290  {
291  $tst = $this->getTestByType($a_type);
293  }
294 
295  public function setInitialTest(int $a_id): void
296  {
297  $this->initial_test = $a_id;
298  }
299 
300  public function getInitialTest(): int
301  {
302  return $this->initial_test;
303  }
304 
305  public function setQualifiedTest(int $a_id): void
306  {
307  $this->qualified_test = $a_id;
308  }
309 
310  public function getQualifiedTest(): int
311  {
312  return $this->qualified_test;
313  }
314 
315  public function resetResults(bool $a_status): void
316  {
317  $this->reset_results = $a_status;
318  }
319 
320  public function isResetResultsEnabled(): bool
321  {
322  return $this->reset_results;
323  }
324 
325  public function create(): void
326  {
327  $query = 'INSERT INTO loc_settings ' .
328  '(obj_id, it_type,itest,qtest,it_start,qt_type,qt_start,reset_results,passed_obj_mode) VALUES ( ' .
329  $this->db->quote($this->getObjId(), 'integer') . ', ' .
330  $this->db->quote($this->getInitialTestType(), 'integer') . ', ' .
331  $this->db->quote($this->getInitialTest(), 'integer') . ', ' .
332  $this->db->quote($this->getQualifiedTest(), 'integer') . ', ' .
333  $this->db->quote($this->isInitialTestStart(), 'integer') . ', ' .
334  $this->db->quote($this->getQualifyingTestType(), 'integer') . ', ' .
335  $this->db->quote($this->isQualifyingTestStart(), 'integer') . ', ' .
336  $this->db->quote($this->isResetResultsEnabled(), 'integer') . ', ' .
337  $this->db->quote($this->getPassedObjectiveMode(), 'integer') . ' ' .
338  ') ';
339  $this->db->manipulate($query);
340  $this->entry_exists = true;
341  }
342 
343  public function update(): void
344  {
345  if (!$this->entry_exists) {
346  $this->create();
347  return;
348  }
349 
350  $query = 'UPDATE loc_settings ' . ' ' .
351  'SET it_type = ' . $this->db->quote($this->getInitialTestType(), 'integer') . ', ' .
352  'itest = ' . $this->db->quote($this->getInitialTest(), 'integer') . ', ' .
353  'qtest = ' . $this->db->quote($this->getQualifiedTest(), 'integer') . ', ' .
354  'it_start = ' . $this->db->quote($this->isInitialTestStart(), 'integer') . ', ' .
355  'qt_type = ' . $this->db->quote($this->getQualifyingTestType(), 'integer') . ', ' .
356  'qt_start = ' . $this->db->quote($this->isQualifyingTestStart(), 'integer') . ', ' .
357  'reset_results = ' . $this->db->quote($this->isResetResultsEnabled(), 'integer') . ', ' .
358  'passed_obj_mode = ' . $this->db->quote($this->getPassedObjectiveMode(), 'integer') . ' ' .
359  'WHERE obj_id = ' . $this->db->quote($this->getObjId(), 'integer');
360 
361  $this->db->manipulate($query);
362  }
363 
369  public function updateStartObjects(ilContainerStartObjects $start): void
370  {
371  if ($this->getInitialTestType() != self::TYPE_INITIAL_NONE) {
372  if ($start->exists($this->getQualifiedTest())) {
373  $start->deleteItem($this->getQualifiedTest());
374  }
375  }
376 
377  switch ($this->getInitialTestType()) {
378  case self::TYPE_INITIAL_PLACEMENT_ALL:
379  case self::TYPE_INITIAL_QUALIFYING_ALL:
380 
381  if ($this->isInitialTestStart()) {
382  if (!$start->exists($this->getInitialTest())) {
383  $start->add($this->getInitialTest());
384  }
385  } else {
386  if ($start->exists($this->getInitialTest())) {
387  $start->deleteItem($this->getInitialTest());
388  }
389  }
390  break;
391 
392  case self::TYPE_INITIAL_NONE:
393 
394  if ($start->exists($this->getInitialTest())) {
395  $start->deleteItem($this->getInitialTest());
396  }
397  break;
398 
399  default:
400 
401  $this->logger->debug('Type initial default');
402  if ($start->exists($this->getInitialTest())) {
403  $this->logger->debug('Old start object exists. Trying to delete');
404  $start->deleteItem($this->getInitialTest());
405  }
406  break;
407  }
408 
409  switch ($this->getQualifyingTestType()) {
410  case self::TYPE_QUALIFYING_ALL:
411 
412  if ($this->isQualifyingTestStart()) {
413  if (!$start->exists($this->getQualifiedTest())) {
414  $start->add($this->getQualifiedTest());
415  }
416  }
417  break;
418 
419  default:
420  if ($start->exists($this->getQualifiedTest())) {
421  $start->deleteItem($this->getQualifiedTest());
422  }
423  break;
424  }
425  }
426 
430  protected function read(): void
431  {
432  $query = 'SELECT * FROM loc_settings ' .
433  'WHERE obj_id = ' . $this->db->quote($this->getObjId(), 'integer');
434  $res = $this->db->query($query);
435  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
436  $this->entry_exists = true;
437 
438  $this->setInitialTestType((int) $row->it_type);
439  $this->setInitialTestAsStart((bool) $row->it_start);
440  $this->setQualifyingTestType((int) $row->qt_type);
441  $this->setQualifyingTestAsStart((bool) $row->qt_start);
442  $this->setInitialTest((int) $row->itest);
443  $this->setQualifiedTest((int) $row->qtest);
444  $this->resetResults((bool) $row->reset_results);
445  $this->setPassedObjectiveMode((int) $row->passed_obj_mode);
446  }
447 
448  if ($this->tree->isDeleted($this->getInitialTest())) {
449  $this->setInitialTest(0);
450  }
451  if ($this->tree->isDeleted($this->getQualifiedTest())) {
452  $this->setQualifiedTest(0);
453  }
454  $this->purgeReferences();
455  }
456 
457  protected function purgeReferences(): void
458  {
459  if ($this->getInitialTest() > 0) {
460  $obj = ilObjectFactory::getInstanceByRefId($this->getInitialTest(), false);
461  if (!$obj instanceof ilObjTest) {
462  $this->setInitialTest(0);
463  $this->update();
464  }
465  }
466  if ($this->getQualifiedTest() > 0) {
468  if (!$obj instanceof ilObjTest) {
469  $this->setQualifiedTest(0);
470  $this->update();
471  }
472  }
473  }
474 
475 
476  public function toXml(ilXmlWriter $writer): void
477  {
478  $writer->xmlElement(
479  'Settings',
480  array(
481  'initialTestType' => $this->getInitialTestType(),
482  'initialTestStart' => (int) $this->isInitialTestStart(),
483  'qualifyingTestType' => $this->getQualifyingTestType(),
484  'qualifyingTestStart' => (int) $this->isQualifyingTestStart(),
485  'resetResults' => (int) $this->isResetResultsEnabled(),
486  'passedObjectivesMode' => $this->getPassedObjectiveMode(),
487  'iTest' => $this->getInitialTest(),
488  'qTest' => $this->getQualifiedTest()
489  )
490  );
491  }
492 }
toXml(ilXmlWriter $writer)
worksWithInitialTest()
Check if the loc is configured for initial tests.
$res
Definition: ltiservices.php:66
setInitialTestAsStart(bool $a_type)
const TYPE_INITIAL_QUALIFYING_SELECTED
getTestByType(int $a_type)
updateStartObjects(ilContainerStartObjects $start)
Update start objects Depends on course objective settings.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Settings for LO courses.
setQualifyingTestType(int $a_type)
setInitialTest(int $a_id)
static _lookupRandomTest(int $obj_id)
setType(int $a_type)
const HIDE_PASSED_OBJECTIVE_QST
static _lookupObjId(int $ref_id)
hasSeparateInitialTests()
Check if separate initial test are configured.
__construct(int $a_cont_id)
static array $instances
const MARK_PASSED_OBJECTIVE_QST
static getInstanceByRefId(int $ref_id, bool $stop_on_error=true)
get an instance of an Ilias object by reference id
global $DIC
Definition: shib_login.php:22
isInitialTestQualifying()
Check if initial test is qualifying*.
resetResults(bool $a_status)
static isObjectiveTest(int $a_trst_ref_id)
Check if test ref_id is used in an objective course.
static getInstanceByObjId(int $a_obj_id)
setPassedObjectiveMode(int $a_mode)
ilDBInterface $db
hasSeparateQualifiedTests()
Check if separate qualified tests are configured.
setQualifyingTestAsStart(bool $a_type)
const TYPE_INITIAL_QUALIFYING_ALL
const TYPE_INITIAL_PLACEMENT_ALL
static lookupContainerForTest(int $a_test_ref_id)
static _getInstance(int $a_copy_id)
const TYPE_QUALIFYING_SELECTED
xmlElement(string $tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
isRandomTestType(int $a_type)
worksWithStartObjects()
Check if start objects are enabled.
static cloneSettings(int $a_copy_id, int $a_container_id, int $a_new_container_id)
setInitialTestType(int $a_type)
setQualifiedTest(int $a_id)
const TYPE_INITIAL_PLACEMENT_SELECTED