ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilContainerSortingSettings.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 
24 include_once './Services/Container/classes/class.ilContainer.php';
25 
36 {
40  protected $tree;
41 
42  private static $instances = array();
43 
44  protected $obj_id;
49 
50  protected $db;
51 
59  public function __construct($a_obj_id = 0)
60  {
61  global $DIC;
62 
63  $this->tree = $DIC->repositoryTree();
64  $ilDB = $DIC->database();
65 
66  $this->obj_id = $a_obj_id;
67  $this->db = $ilDB;
68 
69  $this->read();
70  }
71 
77  public static function getInstanceByObjId($a_obj_id)
78  {
79  if (self::$instances[$a_obj_id]) {
80  return self::$instances[$a_obj_id];
81  }
82  return self::$instances[$a_obj_id] = new self($a_obj_id);
83  }
84 
89  public function loadEffectiveSettings()
90  {
91  if ($this->getSortMode() != ilContainer::SORT_INHERIT) {
92  return $this;
93  }
94 
95  $effective_settings = $this->getInheritedSettings($this->obj_id);
96  $inherited = clone $this;
97 
98  if ($effective_settings->getSortMode() == ilContainer::SORT_INHERIT) {
99  $inherited->setSortMode(ilContainer::SORT_TITLE);
100  } else {
101  $inherited->setSortMode($effective_settings->getSortMode());
102  $inherited->setSortNewItemsOrder($effective_settings->getSortNewItemsOrder());
103  $inherited->setSortNewItemsPosition($effective_settings->getSortNewItemsPosition());
104  }
105  return $inherited;
106  }
107 
108 
114  public function getInheritedSettings($a_container_obj_id)
115  {
116  $tree = $this->tree;
117 
118  if (!$a_container_obj_id) {
119  $a_container_obj_id = $this->obj_id;
120  }
121 
122  $ref_ids = ilObject::_getAllReferences($a_container_obj_id);
123  $ref_id = current($ref_ids);
124 
125  if ($cont_ref_id = $tree->checkForParentType($ref_id, 'grp', true)) {
126  $parent_obj_id = ilObject::_lookupObjId($cont_ref_id);
127  $parent_settings = self::getInstanceByObjId($parent_obj_id);
128 
129  if ($parent_settings->getSortMode() == ilContainer::SORT_INHERIT) {
130  return $this->getInheritedSettings($parent_obj_id);
131  }
132  return $parent_settings;
133  }
134 
135  if ($cont_ref_id = $tree->checkForParentType($ref_id, 'crs', true)) {
136  $parent_obj_id = ilObject::_lookupObjId($cont_ref_id);
137  $parent_settings = self::getInstanceByObjId($parent_obj_id);
138  return $parent_settings;
139  }
140  // no parent settings found => return current settings
141  return $this;
142  }
143 
144 
145  public static function _readSortMode($a_obj_id)
146  {
147  global $DIC;
148 
149  $ilDB = $DIC->database();
150 
151  $query = "SELECT * FROM container_sorting_set " .
152  "WHERE obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " ";
153  $res = $ilDB->query($query);
154 
155  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
156  return $row->sort_mode;
157  }
159  }
160 
161 
170  public static function _lookupSortMode($a_obj_id)
171  {
172  global $DIC;
173 
174  $tree = $DIC->repositoryTree();
175  $ilDB = $DIC->database();
176  $objDefinition = $DIC["objDefinition"];
177 
178  // Try to read from table
179  $query = "SELECT * FROM container_sorting_set " .
180  "WHERE obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " ";
181  $res = $ilDB->query($query);
182 
183  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
184  if ($row->sort_mode != ilContainer::SORT_INHERIT) {
185  return $row->sort_mode;
186  }
187  }
188  return self::lookupSortModeFromParentContainer($a_obj_id);
189  }
190 
196  public static function lookupSortModeFromParentContainer($a_obj_id)
197  {
198  $settings = self::getInstanceByObjId($a_obj_id);
199  $inherited_settings = $settings->getInheritedSettings($a_obj_id);
200  return $inherited_settings->getSortMode();
201  }
202 
212  public static function _cloneSettings($a_old_id, $a_new_id)
213  {
214  global $DIC;
215 
216  $ilDB = $DIC->database();
217 
218  $query = "SELECT sort_mode,sort_direction,new_items_position,new_items_order " .
219  "FROM container_sorting_set " .
220  "WHERE obj_id = " . $ilDB->quote($a_old_id, 'integer') . " ";
221  $res = $ilDB->query($query);
222  while ($row = $ilDB->fetchAssoc($res)) {
223  $query = "DELETE FROM container_sorting_set " .
224  "WHERE obj_id = " . $ilDB->quote($a_new_id) . " ";
225  $ilDB->manipulate($query);
226 
227  $query = "INSERT INTO container_sorting_set " .
228  "(obj_id,sort_mode, sort_direction, new_items_position, new_items_order) " .
229  "VALUES( " .
230  $ilDB->quote($a_new_id, 'integer') . ", " .
231  $ilDB->quote($row["sort_mode"], 'integer') . ", " .
232  $ilDB->quote($row["sort_direction"], 'integer') . ', ' .
233  $ilDB->quote($row["new_items_position"], 'integer') . ', ' .
234  $ilDB->quote($row["new_items_order"], 'integer') . ' ' .
235  ")";
236  $ilDB->manipulate($query);
237  }
238  return true;
239  }
240 
247  public function getSortMode()
248  {
249  return $this->sort_mode ? $this->sort_mode : 0;
250  }
251 
256  public function getSortDirection()
257  {
258  return $this->sort_direction ? $this->sort_direction : ilContainer::SORT_DIRECTION_ASC;
259  }
260 
265  public function getSortNewItemsPosition()
266  {
268  }
269 
274  public function getSortNewItemsOrder()
275  {
276  return $this->new_items_order;
277  }
278 
286  public function setSortMode($a_mode)
287  {
288  $this->sort_mode = (int) $a_mode;
289  }
290 
295  public function setSortDirection($a_direction)
296  {
297  $this->sort_direction = (int) $a_direction;
298  }
299 
304  public function setSortNewItemsPosition($a_position)
305  {
306  $this->new_items_position = (int) $a_position;
307  }
308 
313  public function setSortNewItemsOrder($a_order)
314  {
315  $this->new_items_order = (int) $a_order;
316  }
317 
324  public function update()
325  {
326  $ilDB = $this->db;
327 
328  $query = "DELETE FROM container_sorting_set " .
329  "WHERE obj_id = " . $ilDB->quote($this->obj_id, 'integer');
330  $res = $ilDB->manipulate($query);
331 
332  $this->save();
333  }
334 
341  public function save()
342  {
343  $ilDB = $this->db;
344 
345  $query = "INSERT INTO container_sorting_set " .
346  "(obj_id,sort_mode, sort_direction, new_items_position, new_items_order) " .
347  "VALUES ( " .
348  $this->db->quote($this->obj_id, 'integer') . ", " .
349  $this->db->quote($this->sort_mode, 'integer') . ", " .
350  $this->db->quote($this->sort_direction, 'integer') . ', ' .
351  $this->db->quote($this->new_items_position, 'integer') . ', ' .
352  $this->db->quote($this->new_items_order, 'integer') . ' ' .
353  ")";
354  $res = $ilDB->manipulate($query);
355  }
356 
361  public function delete()
362  {
363  $ilDB = $this->db;
364 
365  $query = 'DELETE FROM container_sorting_set WHERE obj_id = ' . $ilDB->quote($this->obj_id, 'integer');
366  $ilDB->query($query);
367  }
368 
376  protected function read()
377  {
378  if (!$this->obj_id) {
379  return true;
380  }
381 
382  $query = "SELECT * FROM container_sorting_set " .
383  "WHERE obj_id = " . $this->db->quote($this->obj_id, 'integer') . " ";
384 
385  $res = $this->db->query($query);
386  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
387  $this->sort_mode = $row->sort_mode;
388  $this->sort_direction = $row->sort_direction;
389  $this->new_items_position = $row->new_items_position;
390  $this->new_items_order = $row->new_items_order;
391  return true;
392  }
393  }
394 
400  public static function sortModeToString($a_sort_mode)
401  {
402  global $DIC;
403 
404  $lng = $DIC->language();
405 
406  $lng->loadLanguageModule('crs');
407  switch ($a_sort_mode) {
409  return $lng->txt('crs_sort_activation');
410 
412  return $lng->txt('crs_sort_manual');
413 
415  return $lng->txt('crs_sort_title');
416 
418  return $lng->txt('sorting_creation_header');
419  }
420  return '';
421  }
422 
430  {
431  $settings = self::getInstanceByObjId($obj_id);
432 
433  $attr = array();
434  switch ($settings->getSortMode()) {
436  switch ($settings->getSortNewItemsOrder()) {
438  $order = 'Activation';
439  break;
441  $order = 'Creation';
442  break;
444  $order = 'Title';
445  break;
446  }
447 
448  $attr = array(
449  'direction' => $settings->getSortDirection() == ilContainer::SORT_DIRECTION_ASC ? "ASC" : "DESC",
450  'position' => $settings->getSortNewItemsPosition() == ilContainer::SORT_NEW_ITEMS_POSITION_BOTTOM ? "Bottom" : "Top",
451  'order' => $order,
452  'type' => 'Manual'
453  );
454 
455  break;
456 
458  $attr = array(
459  'direction' => $settings->getSortDirection() == ilContainer::SORT_DIRECTION_ASC ? "ASC" : "DESC",
460  'type' => 'Creation'
461  );
462  break;
463 
465  $attr = array(
466  'direction' => $settings->getSortDirection() == ilContainer::SORT_DIRECTION_ASC ? "ASC" : "DESC",
467  'type' => 'Title'
468  );
469  break;
471  $attr = array(
472  'direction' => $settings->getSortDirection() == ilContainer::SORT_DIRECTION_ASC ? "ASC" : "DESC",
473  'type' => 'Activation'
474  );
475  break;
477  $attr = array(
478  'type' => 'Inherit'
479  );
480  }
481  $xml->xmlElement('Sort', $attr);
482  }
483 
490  public static function _importContainerSortingSettings($attibs, $obj_id)
491  {
492  $settings = self::getInstanceByObjId($obj_id);
493 
494  switch ($attibs['type']) {
495  case 'Manual':
496  $settings->setSortMode(ilContainer::SORT_MANUAL);
497  break;
498  case 'Creation':
499  $settings->setSortMode(ilContainer::SORT_CREATION);
500  break;
501  case 'Title':
502  $settings->setSortMode(ilContainer::SORT_TITLE);
503  break;
504  case 'Activation':
505  $settings->setSortMode(ilContainer::SORT_ACTIVATION);
506  break;
507  }
508 
509  switch ($attibs['direction']) {
510  case 'ASC':
511  $settings->setSortDirection(ilContainer::SORT_DIRECTION_ASC);
512  break;
513  case 'DESC':
514  $settings->setSortDirection(ilContainer::SORT_DIRECTION_DESC);
515  break;
516  }
517 
518  switch ($attibs['position']) {
519  case "Top":
520  $settings->setSortNewItemsPosition(ilContainer::SORT_NEW_ITEMS_POSITION_TOP);
521  break;
522  case "Bottom":
523  $settings->setSortNewItemsPosition(ilContainer::SORT_NEW_ITEMS_POSITION_BOTTOM);
524  break;
525  }
526 
527  switch ($attibs['order']) {
528  case 'Creation':
529  $settings->setSortNewItemsOrder(ilContainer::SORT_NEW_ITEMS_ORDER_CREATION);
530  break;
531  case 'Title':
532  $settings->setSortNewItemsOrder(ilContainer::SORT_NEW_ITEMS_ORDER_TITLE);
533  break;
534  case 'Activation':
535  $settings->setSortNewItemsOrder(ilContainer::SORT_NEW_ITEMS_ORDER_ACTIVATION);
536  }
537 
538  $settings->update();
539  }
540 }
setSortDirection($a_direction)
Set sort direction.
const SORT_NEW_ITEMS_POSITION_TOP
loadEffectiveSettings()
Load inherited settings.
global $DIC
Definition: saml.php:7
getInheritedSettings($a_container_obj_id)
Read inherited settings of course/group ilTree $tree.
static _importContainerSortingSettings($attibs, $obj_id)
sorting import for all container objects
XML writer class.
setSortNewItemsPosition($a_position)
SET new item position.
static _cloneSettings($a_old_id, $a_new_id)
Clone settings.
static _getAllReferences($a_id)
get all reference ids of object
$xml
Definition: metadata.php:240
const SORT_NEW_ITEMS_POSITION_BOTTOM
foreach($_POST as $key=> $value) $res
const SORT_NEW_ITEMS_ORDER_CREATION
static _lookupObjId($a_id)
static lookupSortModeFromParentContainer($a_obj_id)
Lookup sort mode from parent container.
$query
Create styles array
The data for the language used.
static sortModeToString($a_sort_mode)
get String representation of sort mode
const SORT_NEW_ITEMS_ORDER_ACTIVATION
xmlElement($tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
getSortNewItemsPosition()
GET new item position.
global $lng
Definition: privfeed.php:17
global $ilDB
static _exportContainerSortingSettings(ilXmlWriter $xml, $obj_id)
sorting XML-export for all container objects
setSortNewItemsOrder($a_order)
SET new item order.
static _lookupSortMode($a_obj_id)
lookup sort mode
static getInstanceByObjId($a_obj_id)
Get singleton instance.
const SORT_NEW_ITEMS_ORDER_TITLE