ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
class.ilContainerSorting.php
Go to the documentation of this file.
1<?php
2/*
3 +-----------------------------------------------------------------------------+
4 | ILIAS open source |
5 +-----------------------------------------------------------------------------+
6 | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7 | |
8 | This program is free software; you can redistribute it and/or |
9 | modify it under the terms of the GNU General Public License |
10 | as published by the Free Software Foundation; either version 2 |
11 | of the License, or (at your option) any later version. |
12 | |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software |
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21 +-----------------------------------------------------------------------------+
22*/
23
24include_once './Services/Container/classes/class.ilContainer.php';
25include_once('Services/Container/classes/class.ilContainerSortingSettings.php');
26
36{
37 protected static $instances = array();
38
39 protected $obj_id;
40 protected $db;
41
42 protected $sorting_settings = null;
43 const ORDER_DEFAULT = 999999;
44
52 private function __construct($a_obj_id)
53 {
54 global $ilDB;
55
56 $this->db = $ilDB;
57 $this->obj_id = $a_obj_id;
58
59 $this->read();
60 }
61
66 public function getSortingSettings()
67 {
69 }
70
79 public static function _getInstance($a_obj_id)
80 {
81 if(isset(self::$instances[$a_obj_id]))
82 {
83 return self::$instances[$a_obj_id];
84 }
85 return self::$instances[$a_obj_id] = new ilContainerSorting($a_obj_id);
86 }
87
93 public static function lookupPositions($a_obj_id)
94 {
95 global $ilDB;
96
97 $query = "SELECT * FROM container_sorting WHERE ".
98 "obj_id = ".$ilDB->quote($a_obj_id,'integer');
99 $res = $ilDB->query($query);
100 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
101 {
102 $sorted[$row->child_id] = $row->position;
103 }
104 return $sorted ? $sorted : array();
105 }
106
113 public function cloneSorting($a_target_id,$a_copy_id)
114 {
115 global $ilDB;
116 global $ilLog;
117
118 $ilLog->write(__METHOD__.': Cloning container sorting.');
119
120 $target_obj_id = ilObject::_lookupObjId($a_target_id);
121
122 include_once('./Services/CopyWizard/classes/class.ilCopyWizardOptions.php');
123 $mappings = ilCopyWizardOptions::_getInstance($a_copy_id)->getMappings();
124
125 $query = "SELECT * FROM container_sorting ".
126 "WHERE obj_id = ".$ilDB->quote($this->obj_id, 'integer');
127
128 $res = $ilDB->query($query);
129
130 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
131 {
132 if(!isset($mappings[$row->child_id]) or !$mappings[$row->child_id])
133 {
134 #$ilLog->write(__METHOD__.': No mapping found for:'.$row->child_id);
135 continue;
136 }
137
138 if($row->parent_id and (!isset($mappings[$row->parent_id]) or !$mappings[$row->parent_id]))
139 {
140 continue;
141 }
142
143 $query = "DELETE FROM container_sorting ".
144 "WHERE obj_id = ".$ilDB->quote($target_obj_id,'integer')." ".
145 "AND child_id = ".$ilDB->quote($mappings[$row->child_id],'integer')." ".
146 "AND parent_type = ".$ilDB->quote($row->parent_type,'text').' '.
147 "AND parent_id = ".$ilDB->quote((int) $mappings[$row->parent_id],'integer');
148 $ilDB->manipulate($query);
149
150 // Add new value
151 $query = "INSERT INTO container_sorting (obj_id,child_id,position,parent_type,parent_id) ".
152 "VALUES( ".
153 $ilDB->quote($target_obj_id ,'integer').", ".
154 $ilDB->quote($mappings[$row->child_id] ,'integer').", ".
155 $ilDB->quote($row->position,'integer').", ".
156 $ilDB->quote($row->parent_type,'text').", ".
157 $ilDB->quote((int) $mappings[$row->parent_id],'integer').
158 ")";
159 $ilDB->manipulate($query);
160 }
161 return true;
162 }
163
164
165
173 public function sortItems($a_items)
174 {
175 $sorted = array();
176 if($this->getSortingSettings()->getSortMode() != ilContainer::SORT_MANUAL)
177 {
178 switch($this->getSortingSettings()->getSortMode())
179 {
181 foreach((array) $a_items as $type => $data)
182 {
183 // #16311 - sorting will remove keys (prev/next)
184 if($type == 'sess_link')
185 {
186 $sorted[$type] = $data;
187 continue;
188 }
189
190 // this line used until #4389 has been fixed (3.10.6)
191 // reanimated with 4.4.0
192 $sorted[$type] = ilUtil::sortArray(
193 (array) $data,
194 'title',
195 ($this->getSortingSettings()->getSortDirection() == ilContainer::SORT_DIRECTION_ASC) ? 'asc' : 'desc',
196 FALSE
197 );
198
199 // the next line tried to use db sorting and has replaced sortArray due to bug #4389
200 // but leads to bug #12165. PHP should be able to do a proper sorting, if the locale
201 // is set correctly, so we witch back to sortArray (with 4.4.0) and see what
202 // feedback we get
203 // (next line has been used from 3.10.6 to 4.3.x)
204// $sorted[$type] = $data;
205 }
206 return $sorted ? $sorted : array();
207
209 foreach((array) $a_items as $type => $data)
210 {
211 // #16311 - sorting will remove keys (prev/next)
212 if($type == 'sess_link')
213 {
214 $sorted[$type] = $data;
215 continue;
216 }
217
218 $sorted[$type] = ilUtil::sortArray(
219 (array) $data,
220 'start',
221 ($this->getSortingSettings()->getSortDirection() == ilContainer::SORT_DIRECTION_ASC) ? 'asc' : 'desc',
222 TRUE
223 );
224
225 }
226 return $sorted ? $sorted : array();
227
228
230 foreach((array) $a_items as $type => $data)
231 {
232 // #16311 - sorting will remove keys (prev/next)
233 if($type == 'sess_link')
234 {
235 $sorted[$type] = $data;
236 continue;
237 }
238
239 $sorted[$type] = ilUtil::sortArray(
240 (array) $data,
241 'create_date',
242 ($this->getSortingSettings()->getSortDirection() == ilContainer::SORT_DIRECTION_ASC) ? 'asc' : 'desc',
243 TRUE
244 );
245 }
246 return $sorted ? $sorted : array();
247 }
248 return $a_items;
249 }
250 if(!count($a_items))
251 {
252 return $a_items;
253 }
254 $sorted = array();
255 foreach((array) $a_items as $type => $data)
256 {
257 if($type == 'sess_link')
258 {
259 $sorted[$type] = $data;
260 continue;
261 }
262
263 // Add position
264 $items = array();
265 foreach((array) $data as $key => $item)
266 {
267 $items[$key] = $item;
268 if(is_array($this->sorting['all']) and isset($this->sorting['all'][$item['child']]))
269 {
270 $items[$key]['position'] = $this->sorting['all'][$item['child']];
271 }
272 else
273 {
274 $items[$key]['position'] = self::ORDER_DEFAULT;
275 }
276 }
277
278 $items = $this->sortOrderDefault($items);
279
280 switch($type)
281 {
282 case '_all':
283 $sorted[$type] = ilUtil::sortArray((array) $items,'position','asc',true);
284 break;
285
286 case '_non_sess':
287 $sorted[$type] = ilUtil::sortArray((array) $items,'position','asc',true);
288 break;
289
290 default:
291 $sorted[$type] = ilUtil::sortArray((array) $items,'position','asc',true);
292 break;
293 }
294 }
295 return $sorted ? $sorted : array();
296 }
297
305 public function sortSubItems($a_parent_type,$a_parent_id,$a_items)
306 {
307 switch($this->getSortingSettings()->getSortMode())
308 {
310 $items = array();
311 foreach($a_items as $key => $item)
312 {
313 $items[$key] = $item;
314 $items[$key]['position'] = isset($this->sorting[$a_parent_type][$a_parent_id][$item['child']]) ?
315 $this->sorting[$a_parent_type][$a_parent_id][$item['child']] : self::ORDER_DEFAULT;
316 }
317
318 $items = $this->sortOrderDefault($items);
319 return ilUtil::sortArray((array) $items,'position','asc',TRUE);
320
321
323 return ilUtil::sortArray(
324 (array) $a_items,
325 'start',
326 ($this->getSortingSettings()->getSortDirection() == ilContainer::SORT_DIRECTION_ASC) ? 'asc' : 'desc',
327 TRUE
328 );
329
331 return ilUtil::sortArray(
332 (array) $a_items,
333 'create_date',
334 ($this->getSortingSettings()->getSortDirection() == ilContainer::SORT_DIRECTION_ASC) ? 'asc' : 'desc',
335 TRUE
336 );
337
338 default:
340 return ilUtil::sortArray(
341 (array) $a_items,
342 'title',
343 ($this->getSortingSettings()->getSortDirection() == ilContainer::SORT_DIRECTION_ASC) ? 'asc' : 'desc',
344 FALSE
345 );
346 }
347
348 }
349
357 public function savePost($a_type_positions)
358 {
359 global $ilLog;
360
361 if(!is_array($a_type_positions))
362 {
363 return false;
364 }
365 foreach($a_type_positions as $key => $position)
366 {
367 if($key == "blocks")
368 {
369 $this->saveBlockPositions($position);
370 }
371 else if(!is_array($position))
372 {
373 $items[$key] = $position * 100;
374 }
375 else
376 {
377 $ilLog->write(__METHOD__.': Deprecated call');
378 foreach($position as $parent_id => $sub_items)
379 {
380 $this->saveSubItems($key,$parent_id,$sub_items ? $sub_items : array());
381 }
382 }
383 }
384 $this->saveItems($items ? $items : array());
385 }
386
387
397 protected function saveItems($a_items)
398 {
399 global $ilDB;
400
401 foreach($a_items as $child_id => $position)
402 {
403 $ilDB->replace(
404 'container_sorting',
405 array(
406 'obj_id' => array('integer',$this->obj_id),
407 'child_id' => array('integer',$child_id),
408 'parent_id' => array('integer',0)
409 ),
410 array(
411 'parent_type' => array('text',''),
412 'position' => array('integer',$position)
413 )
414 );
415 }
416 return true;
417 }
418
426 protected function saveSubItems($a_parent_type,$a_parent_id,$a_items)
427 {
428 global $ilDB;
429
430 foreach($a_items as $child_id => $position)
431 {
432 $ilDB->replace(
433 'container_sorting',
434 array(
435 'obj_id' => array('integer',$this->obj_id),
436 'child_id' => array('integer',$child_id),
437 'parent_id' => array('integer',$a_parent_id)
438 ),
439 array(
440 'parent_type' => array('text',$a_parent_type),
441 'position' => array('integer',$position)
442 )
443 );
444 }
445 return true;
446
447 }
448
454 protected function saveBlockPositions(array $a_values)
455 {
456 global $ilDB;
457
458 asort($a_values);
459
460 $ilDB->replace(
461 'container_sorting_bl',
462 array(
463 'obj_id' => array('integer',$this->obj_id)
464 ),
465 array(
466 'block_ids' => array('text', implode(";", array_keys($a_values)))
467 )
468 );
469 }
470
476 public function getBlockPositions()
477 {
478 global $ilDB;
479
480 $set = $ilDB->query("SELECT block_ids".
481 " FROM container_sorting_bl".
482 " WHERE obj_id = ".$ilDB->quote($this->obj_id, "integer"));
483 $row = $ilDB->fetchAssoc($set);
484 if($row["block_ids"])
485 {
486 return explode(";", $row["block_ids"]);
487 }
488 }
489
490
497 private function read()
498 {
499 global $tree;
500
501 if(!$this->obj_id)
502 {
503 $this->sorting_settings = new ilContainerSortingSettings();
504 return true;
505 }
506
507 $this->sorting_settings = ilContainerSortingSettings::getInstanceByObjId($this->obj_id);
508 if($this->getSortingSettings()->getSortMode() == ilContainer::SORT_INHERIT)
509 {
510 // lookup settings of parent course
511 $ref_ids = ilObject::_getAllReferences($this->obj_id);
512 $ref_id = end($ref_ids);
513 $crs_ref_id = $tree->checkForParentType($ref_id,'crs');
514 $crs_obj_id = ilObject::_lookupObjId($crs_ref_id);
515
516 $crs_settings = ilContainerSortingSettings::getInstanceByObjId($crs_obj_id);
517 $this->sorting_settings = clone $crs_settings;
518
519 }
520 $query = "SELECT * FROM container_sorting ".
521 "WHERE obj_id = ".$this->db->quote($this->obj_id ,'integer')." ORDER BY position";
522 $res = $this->db->query($query);
523 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
524 {
525 if($row->parent_id)
526 {
527 $this->sorting[$row->parent_type][$row->parent_id][$row->child_id] = $row->position;
528 }
529 else
530 {
531 $this->sorting['all'][$row->child_id] = $row->position;
532 }
533 }
534 return true;
535 }
536
543 private function sortOrderDefault($items)
544 {
545 $no_position = array();
546
547 foreach($items as $key => $item)
548 {
549 if($item["position"] == self::ORDER_DEFAULT)
550 {
551 $no_position[]= array("key" => $key, "title" => $item["title"], "create_date" => $item["create_date"],
552 "start" => $item["start"]);
553 }
554
555 }
556
557 if(!count($no_position))
558 {
559 return $items;
560 }
561
562 switch($this->getSortingSettings()->getSortNewItemsOrder())
563 {
565 $no_position = ilUtil::sortArray( (array) $no_position,
566 'title',
567 ($this->getSortingSettings()->getSortDirection() == ilContainer::SORT_DIRECTION_ASC) ? 'asc' : 'desc',
568 TRUE);
569 break;
571 $no_position = ilUtil::sortArray((array) $no_position,
572 'create_date',
573 ($this->getSortingSettings()->getSortDirection() == ilContainer::SORT_DIRECTION_ASC) ? 'asc' : 'desc',
574 TRUE);
575 break;
577 $no_position = ilUtil::sortArray((array) $no_position,
578 'start',
579 ($this->getSortingSettings()->getSortDirection() == ilContainer::SORT_DIRECTION_ASC) ? 'asc' : 'desc',
580 TRUE);
581
582 }
583 $count = $this->getSortingSettings()->getSortNewItemsPosition()
585
586 foreach($no_position as $values)
587 {
588 $items[$values["key"]]["position"] = $count;
589 $count++;
590 }
591 return $items;
592 }
593}
594
595
596?>
const DB_FETCHMODE_OBJECT
Definition: class.ilDB.php:11
static getInstanceByObjId($a_obj_id)
Get singleton instance.
cloneSorting($a_target_id, $a_copy_id)
clone sorting
sortItems($a_items)
sort subitems
getBlockPositions()
Read block custom positions (for current object id)
__construct($a_obj_id)
Constructor.
saveBlockPositions(array $a_values)
Save block custom positions (for current object id)
sortOrderDefault($items)
Position and order sort order for new object without position in manual sorting type.
saveItems($a_items)
save items
saveSubItems($a_parent_type, $a_parent_id, $a_items)
Save subitem ordering (sessions, learning objectives)
static lookupPositions($a_obj_id)
Get positions of subitems.
static _getInstance($a_obj_id)
get instance by obj_id
savePost($a_type_positions)
Save post.
sortSubItems($a_parent_type, $a_parent_id, $a_items)
sort subitems (items of sessions or learning objectives)
getSortingSettings()
Get sorting settings.
const SORT_NEW_ITEMS_ORDER_ACTIVATION
const SORT_NEW_ITEMS_ORDER_TITLE
const SORT_NEW_ITEMS_ORDER_CREATION
const SORT_NEW_ITEMS_POSITION_TOP
const SORT_DIRECTION_ASC
static _getInstance($a_copy_id)
Get instance of copy wizard options.
static _lookupObjId($a_id)
static _getAllReferences($a_id)
get all reference ids of object
static sortArray($array, $a_array_sortby, $a_array_sortorder=0, $a_numeric=false, $a_keep_keys=false)
sortArray
$ref_id
Definition: sahs_server.php:39
global $ilDB