ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 
113  public function getInheritedSettings($a_container_obj_id)
114  {
115  $tree = $this->tree;
116 
117  if (!$a_container_obj_id) {
118  $a_container_obj_id = $this->obj_id;
119  }
120 
121  $ref_ids = ilObject::_getAllReferences($a_container_obj_id);
122  $ref_id = current($ref_ids);
123 
124  if ($cont_ref_id = $tree->checkForParentType($ref_id, 'grp', true)) {
125  $parent_obj_id = ilObject::_lookupObjId($cont_ref_id);
126  $parent_settings = self::getInstanceByObjId($parent_obj_id);
127 
128  if ($parent_settings->getSortMode() == ilContainer::SORT_INHERIT) {
129  return $this->getInheritedSettings($parent_obj_id);
130  }
131  return $parent_settings;
132  }
133 
134  if ($cont_ref_id = $tree->checkForParentType($ref_id, 'crs', true)) {
135  $parent_obj_id = ilObject::_lookupObjId($cont_ref_id);
136  $parent_settings = self::getInstanceByObjId($parent_obj_id);
137  return $parent_settings;
138  }
139  // no parent settings found => return current settings
140  return $this;
141  }
142 
143 
144  public static function _readSortMode($a_obj_id)
145  {
146  global $DIC;
147 
148  $ilDB = $DIC->database();
149 
150  $query = "SELECT * FROM container_sorting_set " .
151  "WHERE obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " ";
152  $res = $ilDB->query($query);
153 
154  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
155  return $row->sort_mode;
156  }
158  }
159 
160 
169  public static function _lookupSortMode($a_obj_id)
170  {
171  global $DIC;
172 
173  $tree = $DIC->repositoryTree();
174  $ilDB = $DIC->database();
175  $objDefinition = $DIC["objDefinition"];
176 
177  // Try to read from table
178  $query = "SELECT * FROM container_sorting_set " .
179  "WHERE obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " ";
180  $res = $ilDB->query($query);
181 
182  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
183  if ($row->sort_mode != ilContainer::SORT_INHERIT) {
184  return $row->sort_mode;
185  }
186  }
187  return self::lookupSortModeFromParentContainer($a_obj_id);
188  }
189 
195  public static function lookupSortModeFromParentContainer($a_obj_id)
196  {
197  $settings = self::getInstanceByObjId($a_obj_id);
198  $inherited_settings = $settings->getInheritedSettings($a_obj_id);
199  return $inherited_settings->getSortMode();
200  }
201 
211  public static function _cloneSettings($a_old_id, $a_new_id)
212  {
213  global $DIC;
214 
215  $ilDB = $DIC->database();
216 
217  $query = "SELECT sort_mode,sort_direction,new_items_position,new_items_order " .
218  "FROM container_sorting_set " .
219  "WHERE obj_id = " . $ilDB->quote($a_old_id, 'integer') . " ";
220  $res = $ilDB->query($query);
221  while ($row = $ilDB->fetchAssoc($res)) {
222  $query = "DELETE FROM container_sorting_set " .
223  "WHERE obj_id = " . $ilDB->quote($a_new_id) . " ";
224  $ilDB->manipulate($query);
225 
226  $query = "INSERT INTO container_sorting_set " .
227  "(obj_id,sort_mode, sort_direction, new_items_position, new_items_order) " .
228  "VALUES( " .
229  $ilDB->quote($a_new_id, 'integer') . ", " .
230  $ilDB->quote($row["sort_mode"], 'integer') . ", " .
231  $ilDB->quote($row["sort_direction"], 'integer') . ', ' .
232  $ilDB->quote($row["new_items_position"], 'integer') . ', ' .
233  $ilDB->quote($row["new_items_order"], 'integer') . ' ' .
234  ")";
235  $ilDB->manipulate($query);
236  }
237  return true;
238  }
239 
246  public function getSortMode()
247  {
248  return $this->sort_mode ? $this->sort_mode : 0;
249  }
250 
255  public function getSortDirection()
256  {
257  return $this->sort_direction ? $this->sort_direction : ilContainer::SORT_DIRECTION_ASC;
258  }
259 
264  public function getSortNewItemsPosition()
265  {
267  }
268 
273  public function getSortNewItemsOrder()
274  {
275  return $this->new_items_order;
276  }
277 
285  public function setSortMode($a_mode)
286  {
287  $this->sort_mode = (int) $a_mode;
288  }
289 
294  public function setSortDirection($a_direction)
295  {
296  $this->sort_direction = (int) $a_direction;
297  }
298 
303  public function setSortNewItemsPosition($a_position)
304  {
305  $this->new_items_position = (int) $a_position;
306  }
307 
312  public function setSortNewItemsOrder($a_order)
313  {
314  $this->new_items_order = (int) $a_order;
315  }
316 
323  public function update()
324  {
325  $ilDB = $this->db;
326 
327  $query = "DELETE FROM container_sorting_set " .
328  "WHERE obj_id = " . $ilDB->quote($this->obj_id, 'integer');
329  $res = $ilDB->manipulate($query);
330 
331  $this->save();
332  }
333 
340  public function save()
341  {
342  $ilDB = $this->db;
343 
344  $query = "INSERT INTO container_sorting_set " .
345  "(obj_id,sort_mode, sort_direction, new_items_position, new_items_order) " .
346  "VALUES ( " .
347  $this->db->quote($this->obj_id, 'integer') . ", " .
348  $this->db->quote($this->sort_mode, 'integer') . ", " .
349  $this->db->quote($this->sort_direction, 'integer') . ', ' .
350  $this->db->quote($this->new_items_position, 'integer') . ', ' .
351  $this->db->quote($this->new_items_order, 'integer') . ' ' .
352  ")";
353  $res = $ilDB->manipulate($query);
354  }
355 
360  public function delete()
361  {
362  $ilDB = $this->db;
363 
364  $query = 'DELETE FROM container_sorting_set WHERE obj_id = ' . $ilDB->quote($this->obj_id, 'integer');
365  $ilDB->query($query);
366  }
367 
375  protected function read()
376  {
377  if (!$this->obj_id) {
378  return true;
379  }
380 
381  $query = "SELECT * FROM container_sorting_set " .
382  "WHERE obj_id = " . $this->db->quote($this->obj_id, 'integer') . " ";
383 
384  $res = $this->db->query($query);
385  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
386  $this->sort_mode = $row->sort_mode;
387  $this->sort_direction = $row->sort_direction;
388  $this->new_items_position = $row->new_items_position;
389  $this->new_items_order = $row->new_items_order;
390  return true;
391  }
392  }
393 
399  public static function sortModeToString($a_sort_mode)
400  {
401  global $DIC;
402 
403  $lng = $DIC->language();
404 
405  $lng->loadLanguageModule('crs');
406  switch ($a_sort_mode) {
408  return $lng->txt('crs_sort_activation');
409 
411  return $lng->txt('crs_sort_manual');
412 
414  return $lng->txt('crs_sort_title');
415 
417  return $lng->txt('sorting_creation_header');
418  }
419  return '';
420  }
421 
429  {
430  $settings = self::getInstanceByObjId($obj_id);
431 
432  $attr = array();
433  switch ($settings->getSortMode()) {
435  switch ($settings->getSortNewItemsOrder()) {
437  $order = 'Activation';
438  break;
440  $order = 'Creation';
441  break;
443  $order = 'Title';
444  break;
445  }
446 
447  $attr = array(
448  'direction' => $settings->getSortDirection() == ilContainer::SORT_DIRECTION_ASC ? "ASC" : "DESC",
449  'position' => $settings->getSortNewItemsPosition() == ilContainer::SORT_NEW_ITEMS_POSITION_BOTTOM ? "Bottom" : "Top",
450  'order' => $order,
451  'type' => 'Manual'
452  );
453 
454  break;
455 
457  $attr = array(
458  'direction' => $settings->getSortDirection() == ilContainer::SORT_DIRECTION_ASC ? "ASC" : "DESC",
459  'type' => 'Creation'
460  );
461  break;
462 
464  $attr = array(
465  'direction' => $settings->getSortDirection() == ilContainer::SORT_DIRECTION_ASC ? "ASC" : "DESC",
466  'type' => 'Title'
467  );
468  break;
470  $attr = array(
471  'direction' => $settings->getSortDirection() == ilContainer::SORT_DIRECTION_ASC ? "ASC" : "DESC",
472  'type' => 'Activation'
473  );
474  break;
476  $attr = array(
477  'type' => 'Inherit'
478  );
479  }
480  $xml->xmlElement('Sort', $attr);
481  }
482 
489  public static function _importContainerSortingSettings($attibs, $obj_id)
490  {
491  $settings = self::getInstanceByObjId($obj_id);
492 
493  switch ($attibs['type']) {
494  case 'Manual':
495  $settings->setSortMode(ilContainer::SORT_MANUAL);
496  break;
497  case 'Creation':
498  $settings->setSortMode(ilContainer::SORT_CREATION);
499  break;
500  case 'Title':
501  $settings->setSortMode(ilContainer::SORT_TITLE);
502  break;
503  case 'Activation':
504  $settings->setSortMode(ilContainer::SORT_ACTIVATION);
505  break;
506  }
507 
508  switch ($attibs['direction']) {
509  case 'ASC':
510  $settings->setSortDirection(ilContainer::SORT_DIRECTION_ASC);
511  break;
512  case 'DESC':
513  $settings->setSortDirection(ilContainer::SORT_DIRECTION_DESC);
514  break;
515  }
516 
517  switch ($attibs['position']) {
518  case "Top":
519  $settings->setSortNewItemsPosition(ilContainer::SORT_NEW_ITEMS_POSITION_TOP);
520  break;
521  case "Bottom":
522  $settings->setSortNewItemsPosition(ilContainer::SORT_NEW_ITEMS_POSITION_BOTTOM);
523  break;
524  }
525 
526  switch ($attibs['order']) {
527  case 'Creation':
528  $settings->setSortNewItemsOrder(ilContainer::SORT_NEW_ITEMS_ORDER_CREATION);
529  break;
530  case 'Title':
531  $settings->setSortNewItemsOrder(ilContainer::SORT_NEW_ITEMS_ORDER_TITLE);
532  break;
533  case 'Activation':
534  $settings->setSortNewItemsOrder(ilContainer::SORT_NEW_ITEMS_ORDER_ACTIVATION);
535  }
536 
537  $settings->update();
538  }
539 }
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.
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
const SORT_NEW_ITEMS_POSITION_BOTTOM
foreach($_POST as $key=> $value) $res
$lng
const SORT_NEW_ITEMS_ORDER_CREATION
static _lookupObjId($a_id)
static lookupSortModeFromParentContainer($a_obj_id)
Lookup sort mode from parent container.
$query
static sortModeToString($a_sort_mode)
get String representation of sort mode
$row
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 $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