ILIAS  trunk Revision v11.0_alpha-1715-g7fc467680fb
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilExcCriteriaCatalogue.php
Go to the documentation of this file.
1 <?php
2 
26 {
27  protected ilDBInterface $db;
28  protected ?int $id = null;
29  protected ?int $parent = null;
30  protected ?string $title = null;
31  protected int $pos = 0;
32 
33  public function __construct(?int $a_id = null)
34  {
35  global $DIC;
36 
37  $this->db = $DIC->database();
38  $this->read($a_id);
39  }
40 
44  public static function getInstancesByParentId(int $a_parent_id): array
45  {
46  global $DIC;
47 
48  $ilDB = $DIC->database();
49 
50  $res = array();
51 
52  $set = $ilDB->query("SELECT *" .
53  " FROM exc_crit_cat" .
54  " WHERE parent = " . $ilDB->quote($a_parent_id, "integer") .
55  " ORDER BY pos");
56  while ($row = $ilDB->fetchAssoc($set)) {
57  $obj = new self();
58  $obj->importFromDB($row);
59  $res[$obj->getId()] = $obj;
60  }
61 
62  return $res;
63  }
64 
65 
66  //
67  // properties
68  //
69 
70  public function getId(): ?int
71  {
72  return $this->id;
73  }
74 
75  protected function setId(int $a_id): void
76  {
77  $this->id = $a_id;
78  }
79 
80  public function setParent(?int $a_value): void
81  {
82  $this->parent = $a_value;
83  }
84 
85  public function getParent(): ?int
86  {
87  return $this->parent;
88  }
89 
90  public function setTitle(?string $a_value): void
91  {
92  $this->title = $a_value;
93  }
94 
95  public function getTitle(): ?string
96  {
97  return $this->title;
98  }
99 
100  public function setPosition(int $a_value): void
101  {
102  $this->pos = $a_value;
103  }
104 
105  public function getPosition(): int
106  {
107  return $this->pos;
108  }
109 
110 
111  //
112  // CRUD
113  //
114 
115  protected function importFromDB(array $a_row): void
116  {
117  $this->setId((int) $a_row["id"]);
118  $this->setParent((int) $a_row["parent"]);
119  $this->setTitle((string) $a_row["title"]);
120  $this->setPosition((int) $a_row["pos"]);
121  }
122 
123  protected function getDBProperties(): array
124  {
125  return array(
126  "title" => array("text", $this->getTitle())
127  ,"pos" => array("integer", $this->getPosition())
128  );
129  }
130 
131  protected function getLastPosition(): int
132  {
133  $ilDB = $this->db;
134 
135  if (!$this->getParent()) {
136  return 0;
137  }
138 
139  $set = $ilDB->query("SELECT MAX(pos) pos" .
140  " FROM exc_crit_cat" .
141  " WHERE parent = " . $ilDB->quote($this->getParent(), "integer"));
142  $row = $ilDB->fetchAssoc($set);
143  return (int) $row["pos"];
144  }
145 
146  protected function read(?int $a_id): void
147  {
148  $ilDB = $this->db;
149 
150  if ($a_id > 0) {
151  $set = $ilDB->query("SELECT *" .
152  " FROM exc_crit_cat" .
153  " WHERE id = " . $ilDB->quote($a_id, "integer"));
154  if ($ilDB->numRows($set) !== 0) {
155  $row = $ilDB->fetchAssoc($set);
156  $this->importFromDB($row);
157  }
158  }
159  }
160 
161  public function save(): void
162  {
163  $ilDB = $this->db;
164 
165  if ($this->id) {
166  $this->update();
167  return;
168  }
169 
170  $this->id = $ilDB->nextId("exc_crit_cat");
171 
172  $fields = $this->getDBProperties();
173  $fields["parent"] = array("integer", $this->getParent());
174  $fields["pos"] = array("integer", $this->getLastPosition() + 10);
175  $fields["id"] = array("integer", $this->id);
176 
177  $ilDB->insert("exc_crit_cat", $fields);
178  }
179 
180  public function update(): void
181  {
182  $ilDB = $this->db;
183 
184  if (!$this->id) {
185  $this->save();
186  return;
187  }
188 
189  $primary = array("id" => array("integer", $this->id));
190  $ilDB->update("exc_crit_cat", $this->getDBProperties(), $primary);
191  }
192 
193  public function delete(): void
194  {
195  $ilDB = $this->db;
196 
197  if (!$this->id) {
198  return;
199  }
200 
202 
203  $ilDB->manipulate("DELETE FROM exc_crit_cat" .
204  " WHERE id = " . $ilDB->quote($this->id, "integer"));
205  }
206 
207  public function cloneObject(int $a_target_parent_id): int
208  {
209  $new_obj = new self();
210  $new_obj->setParent($a_target_parent_id);
211  $new_obj->setTitle($this->getTitle());
212  $new_obj->setPosition($this->getPosition());
213  $new_obj->save();
214 
215  foreach (ilExcCriteria::getInstancesByParentId($this->getId()) as $crit) {
216  $crit->cloneObject($new_obj->getId());
217  }
218 
219  return $new_obj->getId();
220  }
221 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$res
Definition: ltiservices.php:66
static getInstancesByParentId(int $a_parent_id)
static getInstancesByParentId(int $a_parent_id)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
global $DIC
Definition: shib_login.php:22
static deleteByParent(int $a_parent_id)
cloneObject(int $a_target_parent_id)