ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilUnitConfigurationRepository.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
25{
26 protected int $consumer_id = 0;
27 protected ilLanguage $lng;
28 protected ilDBInterface $db;
30 private array $units = [];
32 private array $categorizedUnits = [];
33
34 public function __construct(int $consumer_id)
35 {
36 global $DIC;
37
38 $lng = $DIC->language();
39
40 $this->db = $DIC->database();
41 $this->consumer_id = $consumer_id;
42 $this->lng = $lng;
43 }
44
45 public function setConsumerId(int $consumer_id): void
46 {
47 $this->consumer_id = $consumer_id;
48 }
49
50 public function getConsumerId(): int
51 {
52 return $this->consumer_id;
53 }
54
55 public function isCRUDAllowed(int $category_id): bool
56 {
57 $res = $this->db->queryF(
58 'SELECT * FROM il_qpl_qst_fq_ucat WHERE category_id = %s',
59 ['integer'],
60 [$category_id]
61 );
62 $row = $this->db->fetchAssoc($res);
63 return isset($row['question_fi']) && (int) $row['question_fi'] === $this->getConsumerId();
64 }
65
66 public function copyCategory(int $category_id, int $question_fi, ?string $category_name = null): int
67 {
68 $res = $this->db->queryF(
69 'SELECT category FROM il_qpl_qst_fq_ucat WHERE category_id = %s',
70 ['integer'],
71 [$category_id]
72 );
73 $row = $this->db->fetchAssoc($res);
74
75 if (null === $category_name) {
76 $category_name = $row['category'];
77 }
78
79 $next_id = $this->db->nextId('il_qpl_qst_fq_ucat');
80 $this->db->insert(
81 'il_qpl_qst_fq_ucat',
82 [
83 'category_id' => ['integer', $next_id],
84 'category' => ['text', $category_name],
85 'question_fi' => ['integer', (int) $question_fi]
86 ]
87 );
88
89 return $next_id;
90 }
91
92 public function copyUnitsByCategories(int $from_category_id, int $to_category_id, int $qustion_fi): void
93 {
94 $res = $this->db->queryF(
95 'SELECT * FROM il_qpl_qst_fq_unit WHERE category_fi = %s',
96 ['integer'],
97 [$from_category_id]
98 );
99 $i = 0;
100 $units = [];
101 while ($row = $this->db->fetchAssoc($res)) {
102 $next_id = $this->db->nextId('il_qpl_qst_fq_unit');
103
104 $units[$i]['old_unit_id'] = $row['unit_id'];
105 $units[$i]['new_unit_id'] = $next_id;
106
107 $this->db->insert(
108 'il_qpl_qst_fq_unit',
109 [
110 'unit_id' => ['integer', $next_id],
111 'unit' => ['text', $row['unit']],
112 'factor' => ['float', $row['factor']],
113 'baseunit_fi' => ['integer', (int) $row['baseunit_fi']],
114 'category_fi' => ['integer', (int) $to_category_id],
115 'sequence' => ['integer', (int) $row['sequence']],
116 'question_fi' => ['integer', (int) $qustion_fi]
117 ]
118 );
119 $i++;
120 }
121
122 foreach ($units as $unit) {
123 //update unit : baseunit_fi
124 $this->db->update(
125 'il_qpl_qst_fq_unit',
126 ['baseunit_fi' => ['integer', (int) $unit['new_unit_id']]],
127 [
128 'baseunit_fi' => ['integer', $unit['old_unit_id']],
129 'category_fi' => ['integer', $to_category_id]
130 ]
131 );
132
133 //update var : unit_fi
134 $this->db->update(
135 'il_qpl_qst_fq_var',
136 ['unit_fi' => ['integer', (int) $unit['new_unit_id']]],
137 [
138 'unit_fi' => ['integer', $unit['old_unit_id']],
139 'question_fi' => ['integer', $qustion_fi]
140 ]
141 );
142
143 //update res : unit_fi
144 $this->db->update(
145 'il_qpl_qst_fq_res',
146 ['unit_fi' => ['integer', (int) $unit['new_unit_id']]],
147 [
148 'unit_fi' => ['integer', $unit['old_unit_id']],
149 'question_fi' => ['integer', $qustion_fi]
150 ]
151 );
152
153 //update res_unit : unit_fi
154 $this->db->update(
155 'il_qpl_qst_fq_res_unit',
156 ['unit_fi' => ['integer', (int) $unit['new_unit_id']]],
157 [
158 'unit_fi' => ['integer', $unit['old_unit_id']],
159 'question_fi' => ['integer', $qustion_fi]
160 ]
161 );
162 }
163 }
164
165 public function getCategoryUnitCount(int $id): int
166 {
167 $result = $this->db->queryF(
168 "SELECT * FROM il_qpl_qst_fq_unit WHERE category_fi = %s",
169 ['integer'],
170 [$id]
171 );
172
173 return $this->db->numRows($result);
174 }
175
176 public function isUnitInUse(int $id): bool
177 {
178 $result_1 = $this->db->queryF(
179 "SELECT unit_fi FROM il_qpl_qst_fq_res_unit WHERE unit_fi = %s",
180 ['integer'],
181 [$id]
182 );
183
184 $result_2 = $this->db->queryF(
185 "SELECT unit_fi FROM il_qpl_qst_fq_var WHERE unit_fi = %s",
186 ['integer'],
187 [$id]
188 );
189 $result_3 = $this->db->queryF(
190 "SELECT unit_fi FROM il_qpl_qst_fq_res WHERE unit_fi = %s",
191 ['integer'],
192 [$id]
193 );
194
195 $cnt_1 = $this->db->numRows($result_1);
196 $cnt_2 = $this->db->numRows($result_2);
197 $cnt_3 = $this->db->numRows($result_3);
198
199 return $cnt_1 > 0 || $cnt_2 > 0 || $cnt_3 > 0;
200 }
201
202 public function checkDeleteCategory(int $id): ?string
203 {
204 $res = $this->db->queryF(
205 'SELECT unit_id FROM il_qpl_qst_fq_unit WHERE category_fi = %s',
206 ['integer'],
207 [$id]
208 );
209
210 if ($this->db->numRows($res)) {
211 while ($row = $this->db->fetchAssoc($res)) {
212 $unit_res = $this->checkDeleteUnit((int) $row['unit_id'], $id);
213 if (!is_null($unit_res)) {
214 return $unit_res;
215 }
216 }
217 }
218
219 return null;
220 }
221
222 public function deleteUnit(int $id): ?string
223 {
224 $res = $this->checkDeleteUnit($id);
225 if (!is_null($res)) {
226 return $res;
227 }
228
229 $affectedRows = $this->db->manipulateF(
230 "DELETE FROM il_qpl_qst_fq_unit WHERE unit_id = %s",
231 ['integer'],
232 [$id]
233 );
234
235 if ($affectedRows > 0) {
236 $this->clearUnits();
237 }
238
239 return null;
240 }
241
242 protected function loadUnits(): void
243 {
244 $result = $this->db->query(
245 "
246 SELECT units.*, il_qpl_qst_fq_ucat.category, baseunits.unit baseunit_title
247 FROM il_qpl_qst_fq_unit units
248 INNER JOIN il_qpl_qst_fq_ucat ON il_qpl_qst_fq_ucat.category_id = units.category_fi
249 LEFT JOIN il_qpl_qst_fq_unit baseunits ON baseunits.unit_id = units.baseunit_fi
250 ORDER BY il_qpl_qst_fq_ucat.category, units.sequence"
251 );
252
253 if ($this->db->numRows($result)) {
254 while ($row = $this->db->fetchAssoc($result)) {
255 $unit = new assFormulaQuestionUnit();
256 $unit->initFormArray($row);
257 $this->addUnit($unit);
258 }
259 }
260 }
261
265 public function getCategorizedUnits(): array
266 {
267 if (count($this->categorizedUnits) === 0) {
268 $result = $this->db->queryF(
269 "
270 SELECT units.*, il_qpl_qst_fq_ucat.category, il_qpl_qst_fq_ucat.question_fi, baseunits.unit baseunit_title
271 FROM il_qpl_qst_fq_unit units
272 INNER JOIN il_qpl_qst_fq_ucat ON il_qpl_qst_fq_ucat.category_id = units.category_fi
273 LEFT JOIN il_qpl_qst_fq_unit baseunits ON baseunits.unit_id = units.baseunit_fi
274 WHERE units.question_fi = %s
275 ORDER BY il_qpl_qst_fq_ucat.category, units.sequence",
276 ['integer'],
277 [$this->getConsumerId()]
278 );
279
280 if ($this->db->numRows($result) > 0) {
281 $category = 0;
282 while ($row = $this->db->fetchAssoc($result)) {
283 $unit = new assFormulaQuestionUnit();
284 $unit->initFormArray($row);
285
286 if ($category !== $unit->getCategory()) {
288 $cat->initFormArray([
289 'category_id' => (int) $row['category_fi'],
290 'category' => $row['category'],
291 'question_fi' => (int) $row['question_fi'],
292 ]);
293 $this->categorizedUnits[] = $cat;
294 $category = $unit->getCategory();
295 }
296
297 $this->categorizedUnits[] = $unit;
298 }
299 }
300 }
301
303 }
304
305 protected function clearUnits(): void
306 {
307 $this->units = [];
308 }
309
310 protected function addUnit(assFormulaQuestionUnit $unit): void
311 {
312 $this->units[$unit->getId()] = $unit;
313 }
314
318 public function getUnits(): array
319 {
320 if (count($this->units) === 0) {
321 $this->loadUnits();
322 }
323 return $this->units;
324 }
325
330 public function loadUnitsForCategory(int $category): array
331 {
332 global $DIC;
333 $ilDB = $DIC['ilDB'];
334
335 $units = [];
336 $result = $ilDB->queryF(
337 "SELECT units.*, baseunits.unit baseunit_title, il_qpl_qst_fq_ucat.category
338 FROM il_qpl_qst_fq_unit units
339 INNER JOIN il_qpl_qst_fq_ucat ON il_qpl_qst_fq_ucat.category_id = units.category_fi
340 LEFT JOIN il_qpl_qst_fq_unit baseunits ON baseunits.unit_id = units.baseunit_fi
341 WHERE il_qpl_qst_fq_ucat.category_id = %s
342 ORDER BY units.sequence",
343 ['integer'],
344 [$category]
345 );
346
347 if ($result->numRows() > 0) {
348 while ($row = $ilDB->fetchAssoc($result)) {
349 $unit = new assFormulaQuestionUnit();
350 $unit->initFormArray($row);
351 $units[] = $unit;
352 }
353 }
354
355 return $units;
356 }
357
362 public function getUnit(int $id): ?assFormulaQuestionUnit
363 {
364 if (count($this->units) === 0) {
365 $this->loadUnits();
366 }
367
368 if (array_key_exists($id, $this->units)) {
369 return $this->units[$id];
370 }
371
372 // Maybe this is a new unit, reload $this->units
373
374 $this->loadUnits();
375
376 return $this->units[$id] ?? null;
377 }
378
382 public function getUnitCategories(): array
383 {
384 $categories = [];
385 $result = $this->db->queryF(
386 "SELECT * FROM il_qpl_qst_fq_ucat WHERE question_fi > %s ORDER BY category",
387 ['integer'],
388 [0]
389 );
390
391 if ($this->db->numRows($result)) {
392 while ($row = $this->db->fetchAssoc($result)) {
393 $value = strcmp('-qpl_qst_formulaquestion_' . $row['category'] . '-', $this->lng->txt($row['category'])) === 0
394 ? $row['category']
395 : $this->lng->txt($row['category']);
396
397 if (trim($row['category']) !== '') {
398 $cat = [
399 'value' => (int) $row['category_id'],
400 'text' => $value,
401 'qst_id' => (int) $row['question_fi']
402 ];
403 $categories[(int) $row['category_id']] = $cat;
404 }
405 }
406 }
407
408 return $categories;
409 }
410
414 public function getAdminUnitCategories(): array
415 {
416 $categories = [];
417
418 $result = $this->db->queryF(
419 "SELECT * FROM il_qpl_qst_fq_ucat WHERE question_fi = %s ORDER BY category",
420 ['integer'],
421 [0]
422 );
423
424 if ($result = $this->db->numRows($result)) {
425 while ($row = $this->db->fetchAssoc($result)) {
426 $value = strcmp('-qpl_qst_formulaquestion_' . $row['category'] . '-', $this->lng->txt($row['category'])) === 0
427 ? $row['category']
428 : $this->lng->txt($row['category']);
429
430 if (trim($row['category']) !== '') {
431 $cat = [
432 'value' => (int) $row['category_id'],
433 'text' => $value,
434 'qst_id' => (int) $row['question_fi']
435 ];
436 $categories[(int) $row['category_id']] = $cat;
437 }
438 }
439 }
440
441 return $categories;
442 }
443
444 public function saveUnitOrder(int $unit_id, int $sequence): void
445 {
446 $this->db->manipulateF(
447 'UPDATE il_qpl_qst_fq_unit SET sequence = %s WHERE unit_id = %s AND question_fi = %s',
448 ['integer', 'integer', 'integer'],
449 [$sequence, $unit_id, $this->getConsumerId()]
450 );
451 }
452
453 public function checkDeleteUnit(int $id, ?int $category_id = null): ?string
454 {
455 $result = $this->db->queryF(
456 "SELECT * FROM il_qpl_qst_fq_var WHERE unit_fi = %s",
457 ['integer'],
458 [$id]
459 );
460 if ($this->db->numRows($result) > 0) {
461 return $this->lng->txt("err_unit_in_variables");
462 }
463
464 $result = $this->db->queryF(
465 "SELECT * FROM il_qpl_qst_fq_res WHERE unit_fi = %s",
466 ['integer'],
467 [$id]
468 );
469 if ($this->db->numRows($result) > 0) {
470 return $this->lng->txt("err_unit_in_results");
471 }
472
473 if (!is_null($category_id)) {
474 $result = $this->db->queryF(
475 "SELECT * FROM il_qpl_qst_fq_unit WHERE baseunit_fi = %s AND category_fi != %s",
476 ['integer', 'integer', 'integer'],
477 [$id, $id, $category_id]
478 );
479 } else {
480 $result = $this->db->queryF(
481 "SELECT * FROM il_qpl_qst_fq_unit WHERE baseunit_fi = %s AND unit_id != %s",
482 ['integer', 'integer'],
483 [$id, $id]
484 );
485 }
486
487 if ($this->db->numRows($result) > 0) {
488 return $this->lng->txt("err_unit_is_baseunit");
489 }
490
491 return null;
492 }
493
495 {
496 $query = 'SELECT * FROM il_qpl_qst_fq_ucat WHERE category_id = ' . $this->db->quote($id, 'integer');
497 $res = $this->db->query($query);
498 if (!$this->db->numRows($res)) {
499 throw new ilException('un_category_not_exist');
500 }
501
502 $row = $this->db->fetchAssoc($res);
503 $category = new assFormulaQuestionUnitCategory();
504 $category->initFormArray($row);
505 return $category;
506 }
507
508 public function saveCategory(assFormulaQuestionUnitCategory $category): void
509 {
510 $res = $this->db->queryF(
511 'SELECT * FROM il_qpl_qst_fq_ucat WHERE category = %s AND question_fi = %s AND category_id != %s',
512 ['text', 'integer', 'integer'],
513 [$category->getCategory(), $this->getConsumerId(), $category->getId()]
514 );
515 if ($this->db->numRows($res)) {
516 throw new ilException('err_wrong_categoryname');
517 }
518
519 $this->db->manipulateF(
520 'UPDATE il_qpl_qst_fq_ucat SET category = %s WHERE question_fi = %s AND category_id = %s',
521 ['text', 'integer', 'integer'],
522 [$category->getCategory(), $this->getConsumerId(), $category->getId()]
523 );
524 }
525
527 {
528 $res = $this->db->queryF(
529 'SELECT category FROM il_qpl_qst_fq_ucat WHERE category = %s AND question_fi = %s',
530 ['text', 'integer'],
531 [$category->getCategory(), $this->getConsumerId()]
532 );
533 if ($this->db->numRows($res)) {
534 throw new ilException('err_wrong_categoryname');
535 }
536
537 $next_id = $this->db->nextId('il_qpl_qst_fq_ucat');
538 $this->db->manipulateF(
539 "INSERT INTO il_qpl_qst_fq_ucat (category_id, category, question_fi) VALUES (%s, %s, %s)",
540 ['integer', 'text', 'integer'],
541 [
542 $next_id,
543 $category->getCategory(),
544 $this->getConsumerId()
545 ]
546 );
547 $category->setId($next_id);
548 }
549
553 public function getAllUnitCategories(): array
554 {
555 $categories = [];
556 $result = $this->db->queryF(
557 "SELECT * FROM il_qpl_qst_fq_ucat WHERE question_fi = %s OR question_fi = %s ORDER BY category",
558 ['integer', 'integer'],
559 [$this->getConsumerId(), 0]
560 );
561
562 if ($result->numRows() > 0) {
563 while ($row = $this->db->fetchAssoc($result)) {
564 $category = new assFormulaQuestionUnitCategory();
565 $category->initFormArray($row);
566 $categories[] = $category;
567 }
568 }
569 return $categories;
570 }
571
572 public function deleteCategory(int $id): ?string
573 {
574 $res = $this->checkDeleteCategory($id);
575 if (!is_null($res)) {
576 return $this->lng->txt('err_category_in_use');
577 }
578
579 $res = $this->db->queryF(
580 'SELECT * FROM il_qpl_qst_fq_unit WHERE category_fi = %s',
581 ['integer'],
582 [$id]
583 );
584 while ($row = $this->db->fetchAssoc($res)) {
585 $this->deleteUnit((int) $row['unit_id']);
586 }
587
588 $ar = $this->db->manipulateF(
589 'DELETE FROM il_qpl_qst_fq_ucat WHERE category_id = %s',
590 ['integer'],
591 [$id]
592 );
593
594 if ($ar > 0) {
595 $this->clearUnits();
596 }
597
598 return null;
599 }
600
601 public function createNewUnit(assFormulaQuestionUnit $unit): void
602 {
603 $next_id = $this->db->nextId('il_qpl_qst_fq_unit');
604 $this->db->manipulateF(
605 'INSERT INTO il_qpl_qst_fq_unit (unit_id, unit, factor, baseunit_fi, category_fi, sequence, question_fi) VALUES (%s, %s, %s, %s, %s, %s, %s)',
606 ['integer', 'text', 'float', 'integer', 'integer', 'integer', 'integer'],
607 [
608 $next_id,
609 $unit->getUnit(),
610 1,
611 0,
612 $unit->getCategory(),
613 0,
614 $this->getConsumerId()
615 ]
616 );
617 $unit->setId($next_id);
618 $unit->setFactor(1.0);
619 $unit->setBaseUnit(0);
620 $unit->setSequence(0);
621
622 $this->clearUnits();
623 }
624
625 public function saveUnit(assFormulaQuestionUnit $unit): void
626 {
627 $res = $this->db->queryF(
628 'SELECT unit_id FROM il_qpl_qst_fq_unit WHERE unit_id = %s',
629 ['integer'],
630 [$unit->getId()]
631 );
632 if ($this->db->numRows($res)) {
633 $row = $this->db->fetchAssoc($res);
634
635 if ($unit->getBaseUnit() === 0 || $unit->getBaseUnit() === $unit->getId()) {
636 $unit->setFactor(1);
637 }
638
639 $ar = $this->db->manipulateF(
640 'UPDATE il_qpl_qst_fq_unit SET unit = %s, factor = %s, baseunit_fi = %s, category_fi = %s, sequence = %s WHERE unit_id = %s AND question_fi = %s',
641 ['text', 'float', 'integer', 'integer', 'integer', 'integer', 'integer'],
642 [
643 $unit->getUnit(), $unit->getFactor(), (int) $unit->getBaseUnit(),
644 $unit->getCategory(),
645 $unit->getSequence(),
646 $unit->getId(),
647 $this->getConsumerId()
648 ]
649 );
650 if ($ar > 0) {
651 $this->clearUnits();
652 }
653 }
654 }
655
656 public function cloneUnits(int $from_consumer_id, int $to_consumer_id): void
657 {
658 $category_mapping = [];
659
660 $res = $this->db->queryF("SELECT * FROM il_qpl_qst_fq_ucat WHERE question_fi = %s", ['integer'], [$from_consumer_id]);
661 while ($row = $this->db->fetchAssoc($res)) {
662 $new_category_id = $this->copyCategory((int) $row['category_id'], $to_consumer_id);
663 $category_mapping[$row['category_id']] = $new_category_id;
664 }
665
666 foreach ($category_mapping as $old_category_id => $new_category_id) {
667 $res = $this->db->queryF(
668 'SELECT * FROM il_qpl_qst_fq_unit WHERE category_fi = %s',
669 ['integer'],
670 [$old_category_id]
671 );
672
673 $i = 0;
674 $units = [];
675 while ($row = $this->db->fetchAssoc($res)) {
676 $next_id = $this->db->nextId('il_qpl_qst_fq_unit');
677
678 $units[$i]['old_unit_id'] = $row['unit_id'];
679 $units[$i]['new_unit_id'] = $next_id;
680
681 $this->db->insert(
682 'il_qpl_qst_fq_unit',
683 [
684 'unit_id' => ['integer', $next_id],
685 'unit' => ['text', $row['unit']],
686 'factor' => ['float', $row['factor']],
687 'baseunit_fi' => ['integer', (int) $row['baseunit_fi']],
688 'category_fi' => ['integer', (int) $new_category_id],
689 'sequence' => ['integer', (int) $row['sequence']],
690 'question_fi' => ['integer', $to_consumer_id]
691 ]
692 );
693 $i++;
694 }
695
696 foreach ($units as $unit) {
697 //update unit : baseunit_fi
698 $this->db->update(
699 'il_qpl_qst_fq_unit',
700 ['baseunit_fi' => ['integer', (int) $unit['new_unit_id']]],
701 [
702 'baseunit_fi' => ['integer', (int) $unit['old_unit_id']],
703 'question_fi' => ['integer', $to_consumer_id]
704 ]
705 );
706
707 //update var : unit_fi
708 $this->db->update(
709 'il_qpl_qst_fq_var',
710 ['unit_fi' => ['integer', (int) $unit['new_unit_id']]],
711 [
712 'unit_fi' => ['integer', (int) $unit['old_unit_id']],
713 'question_fi' => ['integer', $to_consumer_id]
714 ]
715 );
716
717 //update res : unit_fi
718 $this->db->update(
719 'il_qpl_qst_fq_res',
720 ['unit_fi' => ['integer', (int) $unit['new_unit_id']]],
721 [
722 'unit_fi' => ['integer', (int) $unit['old_unit_id']],
723 'question_fi' => ['integer', $to_consumer_id]
724 ]
725 );
726
727 //update res_unit : unit_fi
728 $this->db->update(
729 'il_qpl_qst_fq_res_unit',
730 ['unit_fi' => ['integer', (int) $unit['new_unit_id']]],
731 [
732 'unit_fi' => ['integer', (int) $unit['old_unit_id']],
733 'question_fi' => ['integer', $to_consumer_id]
734 ]
735 );
736 }
737 }
738 }
739}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Base class for ILIAS Exception handling.
language handling
Class ilUnitConfigurationRepository.
cloneUnits(int $from_consumer_id, int $to_consumer_id)
copyCategory(int $category_id, int $question_fi, ?string $category_name=null)
saveNewUnitCategory(assFormulaQuestionUnitCategory $category)
copyUnitsByCategories(int $from_category_id, int $to_category_id, int $qustion_fi)
saveCategory(assFormulaQuestionUnitCategory $category)
checkDeleteUnit(int $id, ?int $category_id=null)
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
global $DIC
Definition: shib_login.php:26