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