ILIAS  trunk Revision v12.0_alpha-16-g3e876e53c80
StepDBRepository.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
25
27{
28 public function __construct(
29 protected ilDBInterface $db,
31 ) {
32 }
33
34 protected function getMaxOrderNr(int $tour_id): int
35 {
36 $set = $this->db->queryF(
37 "SELECT MAX(order_nr) max_order FROM help_gt_step " .
38 " WHERE tour_id = %s",
39 ["integer"],
40 [$tour_id]
41 );
42 if ($record = $this->db->fetchAssoc($set)) {
43 return (int) $record["max_order"];
44 }
45 return 0;
46 }
47
48 public function create(Step $step): int
49 {
50 $id = $this->db->nextId('help_gt_step');
51 $order_nr = $this->getMaxOrderNr($step->getTourId()) + 10;
52 $this->db->insert('help_gt_step', [
53 'id' => ['integer', $id],
54 'tour_id' => ['integer', $step->getTourId()],
55 'order_nr' => ['integer', $order_nr],
56 'type' => ['integer', $step->getType()->value],
57 'element_id' => ['text', $step->getElementId()]
58 ]);
59 return $id;
60 }
61
62 public function update(Step $step): void
63 {
64 $this->db->update('help_gt_step', [
65 'tour_id' => ['integer', $step->getTourId()],
66 'order_nr' => ['integer', $step->getOrderNr()],
67 'type' => ['integer', $step->getType()->value],
68 'element_id' => ['text', $step->getElementId()]
69 ], [
70 'id' => ['integer', $step->getId()]
71 ]);
72 }
73
74 public function delete(int $tour_id, int $step_id): void
75 {
76 $this->db->manipulateF(
77 "DELETE FROM help_gt_step WHERE " .
78 " id = %s AND tour_id = %s",
79 ["integer", "integer"],
80 [$step_id, $tour_id]
81 );
82 }
83
84 public function getById(int $id): ?Step
85 {
86 $set = $this->db->queryF(
87 'SELECT * FROM help_gt_step WHERE id = %s',
88 ['integer'],
89 [$id]
90 );
91
92 $record = $this->db->fetchAssoc($set);
93 if ($record === false) {
94 return null;
95 }
96
97 return $this->mapRecordToStep($record);
98 }
99
103 public function getStepsOfTour(int $tour_id): \Generator
104 {
105 $set = $this->db->queryF(
106 "SELECT * FROM help_gt_step " .
107 " WHERE tour_id = %s ORDER BY order_nr ASC",
108 ["integer"],
109 [$tour_id]
110 );
111 while ($record = $this->db->fetchAssoc($set)) {
112 yield $this->mapRecordToStep($record);
113 }
114 }
115
116 public function countStepsOfTour(int $tour_id): int
117 {
118 $set = $this->db->queryF(
119 "SELECT COUNT(*) cnt FROM help_gt_step " .
120 " WHERE tour_id = %s",
121 ["integer"],
122 [$tour_id]
123 );
124 if ($record = $this->db->fetchAssoc($set)) {
125 return (int) $record["cnt"];
126 }
127 return 0;
128 }
129
130 public function saveOrder(int $tour_id, array $order): void
131 {
132 $order_nr = 0;
133 foreach ($order as $step_id) {
134 $order_nr += 10;
135 $this->db->update(
136 "help_gt_step",
137 [
138 "order_nr" => ["integer", $order_nr],
139 ],
140 [ // where
141 "id" => ["integer", $step_id],
142 "tour_id" => ["integer", $tour_id],
143 ]
144 );
145 }
146 }
147
148 protected function mapRecordToStep(array $record): Step
149 {
150 return $this->data->step(
151 (int) $record['id'],
152 (int) $record['tour_id'],
153 (int) $record['order_nr'],
154 StepType::from((int) $record['type']),
155 (string) $record['element_id']
156 );
157 }
158}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
__construct(protected ilDBInterface $db, protected InternalDataService $data)
Interface ilDBInterface.