ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilContainerSortingSettings.php
Go to the documentation of this file.
1<?php
2
23{
24 protected ilTree $tree;
26 private static array $instances = [];
27 protected int $obj_id;
32 protected ilDBInterface $db;
33
34 public function __construct(int $a_obj_id = 0)
35 {
36 global $DIC;
37
38 $this->tree = $DIC->repositoryTree();
39 $ilDB = $DIC->database();
40
41 $this->obj_id = $a_obj_id;
42 $this->db = $ilDB;
43
44 $this->read();
45 }
46
47 public static function getInstanceByObjId(int $a_obj_id): self
48 {
49 return self::$instances[$a_obj_id] ?? (self::$instances[$a_obj_id] = new self($a_obj_id));
50 }
51
55 public function loadEffectiveSettings(): self
56 {
57 if ($this->getSortMode() !== ilContainer::SORT_INHERIT) {
58 return $this;
59 }
60
61 $effective_settings = $this->getInheritedSettings($this->obj_id);
62 $inherited = clone $this;
63
64 if ($effective_settings->getSortMode() === ilContainer::SORT_INHERIT) {
65 $inherited->setSortMode(ilContainer::SORT_TITLE);
66 } else {
67 $inherited->setSortMode($effective_settings->getSortMode());
68 $inherited->setSortNewItemsOrder($effective_settings->getSortNewItemsOrder());
69 $inherited->setSortNewItemsPosition($effective_settings->getSortNewItemsPosition());
70 }
71 return $inherited;
72 }
73
74
75 public function getInheritedSettings(int $a_container_obj_id): self
76 {
78
79 if (!$a_container_obj_id) {
80 $a_container_obj_id = $this->obj_id;
81 }
82
83 $ref_ids = ilObject::_getAllReferences($a_container_obj_id);
84 $ref_id = current($ref_ids);
85
86 if ($cont_ref_id = $tree->checkForParentType($ref_id, 'grp', true)) {
87 $parent_obj_id = ilObject::_lookupObjId($cont_ref_id);
88 $parent_settings = self::getInstanceByObjId($parent_obj_id);
89
90 if ($parent_settings->getSortMode() === ilContainer::SORT_INHERIT) {
91 return $this->getInheritedSettings($parent_obj_id);
92 }
93 return $parent_settings;
94 }
95
96 if ($cont_ref_id = $tree->checkForParentType($ref_id, 'crs', true)) {
97 $parent_obj_id = ilObject::_lookupObjId($cont_ref_id);
98 $parent_settings = self::getInstanceByObjId($parent_obj_id);
99 return $parent_settings;
100 }
101 // no parent settings found => return current settings
102 return $this;
103 }
104
105
106 public static function _readSortMode(int $a_obj_id): int
107 {
108 global $DIC;
109
110 $ilDB = $DIC->database();
111
112 $query = "SELECT sort_mode FROM container_sorting_set " .
113 "WHERE obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " ";
114 $res = $ilDB->query($query);
115
116 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
117 return (int) $row->sort_mode;
118 }
120 }
121
122 public static function _lookupSortMode(int $a_obj_id): int
123 {
124 global $DIC;
125
126 $ilDB = $DIC->database();
127
128 // Try to read from table
129 $query = "SELECT sort_mode FROM container_sorting_set " .
130 "WHERE obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " ";
131 $res = $ilDB->query($query);
132
133 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
134 if ((int) $row->sort_mode !== ilContainer::SORT_INHERIT) {
135 return (int) $row->sort_mode;
136 }
137 }
139 }
140
141 public static function lookupSortModeFromParentContainer(int $a_obj_id): int
142 {
143 $settings = self::getInstanceByObjId($a_obj_id);
144 $inherited_settings = $settings->getInheritedSettings($a_obj_id);
145 return $inherited_settings->getSortMode();
146 }
147
148 public static function _cloneSettings(
149 int $a_old_id,
150 int $a_new_id
151 ): void {
152 global $DIC;
153
154 $ilDB = $DIC->database();
155
156 $query = "SELECT sort_mode,sort_direction,new_items_position,new_items_order " .
157 "FROM container_sorting_set " .
158 "WHERE obj_id = " . $ilDB->quote($a_old_id, 'integer') . " ";
159 $res = $ilDB->query($query);
160 while ($row = $ilDB->fetchAssoc($res)) {
161 $query = "DELETE FROM container_sorting_set " .
162 "WHERE obj_id = " . $ilDB->quote($a_new_id) . " ";
163 $ilDB->manipulate($query);
164
165 $query = "INSERT INTO container_sorting_set " .
166 "(obj_id,sort_mode, sort_direction, new_items_position, new_items_order) " .
167 "VALUES( " .
168 $ilDB->quote($a_new_id, 'integer') . ", " .
169 $ilDB->quote($row["sort_mode"], 'integer') . ", " .
170 $ilDB->quote($row["sort_direction"], 'integer') . ', ' .
171 $ilDB->quote($row["new_items_position"], 'integer') . ', ' .
172 $ilDB->quote($row["new_items_order"], 'integer') . ' ' .
173 ")";
174 $ilDB->manipulate($query);
175 }
176 }
177
178 public function getSortMode(): int
179 {
180 return $this->sort_mode ?: 0;
181 }
182
183 public function getSortDirection(): int
184 {
185 return $this->sort_direction ?: ilContainer::SORT_DIRECTION_ASC;
186 }
187
188 public function getSortNewItemsPosition(): int
189 {
190 return $this->new_items_position;
191 }
192
193 public function getSortNewItemsOrder(): int
194 {
195 return $this->new_items_order;
196 }
197
201 public function setSortMode(int $a_mode): void
202 {
203 $this->sort_mode = $a_mode;
204 }
205
206 public function setSortDirection(int $a_direction): void
207 {
208 $this->sort_direction = $a_direction;
209 }
210
211 public function setSortNewItemsPosition(int $a_position): void
212 {
213 $this->new_items_position = $a_position;
214 }
215
216 public function setSortNewItemsOrder(int $a_order): void
217 {
218 $this->new_items_order = $a_order;
219 }
220
221 public function update(): void
222 {
223 $ilDB = $this->db;
224
225 $query = "DELETE FROM container_sorting_set " .
226 "WHERE obj_id = " . $ilDB->quote($this->obj_id, 'integer');
227 $ilDB->manipulate($query);
228
229 $this->save();
230 }
231
232 public function save(): void
233 {
234 $ilDB = $this->db;
235
236 $query = "INSERT INTO container_sorting_set " .
237 "(obj_id,sort_mode, sort_direction, new_items_position, new_items_order) " .
238 "VALUES ( " .
239 $this->db->quote($this->obj_id, 'integer') . ", " .
240 $this->db->quote($this->sort_mode, 'integer') . ", " .
241 $this->db->quote($this->sort_direction, 'integer') . ', ' .
242 $this->db->quote($this->new_items_position, 'integer') . ', ' .
243 $this->db->quote($this->new_items_order, 'integer') . ' ' .
244 ")";
245 $ilDB->manipulate($query);
246 }
247
248 public function delete(): void
249 {
250 $ilDB = $this->db;
251
252 $query = 'DELETE FROM container_sorting_set WHERE obj_id = ' . $ilDB->quote($this->obj_id, 'integer');
253 $ilDB->query($query);
254 }
255
256 protected function read(): void
257 {
258 if (!$this->obj_id) {
259 return;
260 }
261
262 $query = "SELECT * FROM container_sorting_set " .
263 "WHERE obj_id = " . $this->db->quote($this->obj_id, 'integer') . " ";
264
265 $res = $this->db->query($query);
266 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
267 $this->sort_mode = (int) $row->sort_mode;
268 $this->sort_direction = (int) $row->sort_direction;
269 $this->new_items_position = (int) $row->new_items_position;
270 $this->new_items_order = (int) $row->new_items_order;
271 return;
272 }
273 }
274
278 public static function sortModeToString(int $a_sort_mode): string
279 {
280 global $DIC;
281
282 $lng = $DIC->language();
283
284 $lng->loadLanguageModule('crs');
285 switch ($a_sort_mode) {
287 return $lng->txt('crs_sort_activation');
288
290 return $lng->txt('crs_sort_manual');
291
293 return $lng->txt('crs_sort_title');
294
296 return $lng->txt('sorting_creation_header');
297 }
298 return '';
299 }
300
304 public static function _exportContainerSortingSettings(
305 ilXmlWriter $xml,
306 int $obj_id
307 ): void {
308 $settings = self::getInstanceByObjId($obj_id);
309
310 $attr = [];
311 switch ($settings->getSortMode()) {
313 $order = 'Title';
314 switch ($settings->getSortNewItemsOrder()) {
316 $order = 'Activation';
317 break;
319 $order = 'Creation';
320 break;
322 $order = 'Title';
323 break;
324 }
325
326 $attr = [
327 'direction' => $settings->getSortDirection() === ilContainer::SORT_DIRECTION_ASC ? "ASC" : "DESC",
328 'position' => $settings->getSortNewItemsPosition() === ilContainer::SORT_NEW_ITEMS_POSITION_BOTTOM ? "Bottom" : "Top",
329 'order' => $order,
330 'type' => 'Manual'
331 ];
332
333 break;
334
336 $attr = [
337 'direction' => $settings->getSortDirection() === ilContainer::SORT_DIRECTION_ASC ? "ASC" : "DESC",
338 'type' => 'Creation'
339 ];
340 break;
341
343 $attr = [
344 'direction' => $settings->getSortDirection() === ilContainer::SORT_DIRECTION_ASC ? "ASC" : "DESC",
345 'type' => 'Title'
346 ];
347 break;
349 $attr = [
350 'direction' => $settings->getSortDirection() === ilContainer::SORT_DIRECTION_ASC ? "ASC" : "DESC",
351 'type' => 'Activation'
352 ];
353 break;
355 $attr = [
356 'type' => 'Inherit'
357 ];
358 }
359 $xml->xmlElement('Sort', $attr);
360 }
361
365 public static function _importContainerSortingSettings(
366 array $attibs,
367 int $obj_id
368 ): void {
369 $settings = self::getInstanceByObjId($obj_id);
370
371 switch ($attibs['type'] ?? '') {
372 case 'Manual':
373 $settings->setSortMode(ilContainer::SORT_MANUAL);
374 break;
375 case 'Creation':
376 $settings->setSortMode(ilContainer::SORT_CREATION);
377 break;
378 case 'Title':
379 $settings->setSortMode(ilContainer::SORT_TITLE);
380 break;
381 case 'Activation':
382 $settings->setSortMode(ilContainer::SORT_ACTIVATION);
383 break;
384 }
385
386 switch ($attibs['direction'] ?? '') {
387 case 'ASC':
388 $settings->setSortDirection(ilContainer::SORT_DIRECTION_ASC);
389 break;
390 case 'DESC':
391 $settings->setSortDirection(ilContainer::SORT_DIRECTION_DESC);
392 break;
393 }
394
395 switch ($attibs['position'] ?? "") {
396 case "Top":
397 $settings->setSortNewItemsPosition(ilContainer::SORT_NEW_ITEMS_POSITION_TOP);
398 break;
399 case "Bottom":
400 $settings->setSortNewItemsPosition(ilContainer::SORT_NEW_ITEMS_POSITION_BOTTOM);
401 break;
402 }
403
404 switch ($attibs['order'] ?? "") {
405 case 'Creation':
406 $settings->setSortNewItemsOrder(ilContainer::SORT_NEW_ITEMS_ORDER_CREATION);
407 break;
408 case 'Title':
409 $settings->setSortNewItemsOrder(ilContainer::SORT_NEW_ITEMS_ORDER_TITLE);
410 break;
411 case 'Activation':
412 $settings->setSortNewItemsOrder(ilContainer::SORT_NEW_ITEMS_ORDER_ACTIVATION);
413 }
414
415 $settings->update();
416 }
417}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _importContainerSortingSettings(array $attibs, int $obj_id)
sorting import for all container objects
static lookupSortModeFromParentContainer(int $a_obj_id)
static _exportContainerSortingSettings(ilXmlWriter $xml, int $obj_id)
sorting XML-export for all container objects
static _cloneSettings(int $a_old_id, int $a_new_id)
static sortModeToString(int $a_sort_mode)
Get string representation of sort mode.
loadEffectiveSettings()
Load inherited settings.
const SORT_DIRECTION_DESC
const SORT_NEW_ITEMS_ORDER_ACTIVATION
const SORT_NEW_ITEMS_ORDER_TITLE
const SORT_NEW_ITEMS_ORDER_CREATION
const SORT_NEW_ITEMS_POSITION_BOTTOM
const SORT_NEW_ITEMS_POSITION_TOP
const SORT_DIRECTION_ASC
static _getAllReferences(int $id)
get all reference ids for object ID
static _lookupObjId(int $ref_id)
Tree class data representation in hierachical trees using the Nested Set Model with Gaps by Joe Celco...
checkForParentType(int $a_ref_id, string $a_type, bool $a_exclude_source_check=false)
Check for parent type e.g check if a folder (ref_id 3) is in a parent course obj => checkForParentTyp...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
xmlElement(string $tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
Interface ilDBInterface.
$ref_id
Definition: ltiauth.php:66
$res
Definition: ltiservices.php:69
global $lng
Definition: privfeed.php:31
global $DIC
Definition: shib_login.php:26