ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 
33 {
34  protected static $instances = array();
35 
36  protected $obj_id;
37  protected $db;
38 
39  protected $manual_sort_enabled = false;
40  protected $sorting_mode = 0;
41 
49  private function __construct($a_obj_id)
50  {
51  global $ilDB;
52 
53  $this->db = $ilDB;
54  $this->obj_id = $a_obj_id;
55 
56  $this->read();
57  }
58 
66  public function getSortMode()
67  {
68  return $this->sorting_mode;
69  }
70 
71 
80  public static function _getInstance($a_obj_id)
81  {
82  if(isset(self::$instances[$a_obj_id]))
83  {
84  return self::$instances[$a_obj_id];
85  }
86  return self::$instances[$a_obj_id] = new ilContainerSorting($a_obj_id);
87  }
88 
94  public static function lookupPositions($a_obj_id)
95  {
96  global $ilDB;
97 
98  $query = "SELECT * FROM container_sorting WHERE ".
99  "obj_id = ".$ilDB->quote($a_obj_id,'integer');
100  $res = $ilDB->query($query);
101  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
102  {
103  $sorted[$row->child_id] = $row->position;
104  }
105  return $sorted ? $sorted : array();
106  }
107 
114  public function cloneSorting($a_target_id,$a_copy_id)
115  {
116  global $ilDB;
117  global $ilLog;
118 
119  $ilLog->write(__METHOD__.': Cloning container sorting.');
120 
121  $target_obj_id = ilObject::_lookupObjId($a_target_id);
122 
123  include_once('./Services/CopyWizard/classes/class.ilCopyWizardOptions.php');
124  $mappings = ilCopyWizardOptions::_getInstance($a_copy_id)->getMappings();
125 
126  $query = "SELECT * FROM container_sorting ".
127  "WHERE obj_id = ".$ilDB->quote($this->obj_id, 'integer');
128 
129  $res = $ilDB->query($query);
130 
131  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
132  {
133  if(!isset($mappings[$row->child_id]) or !$mappings[$row->child_id])
134  {
135  #$ilLog->write(__METHOD__.': No mapping found for:'.$row->child_id);
136  continue;
137  }
138 
139  if($row->parent_id and (!isset($mappings[$row->parent_id]) or !$mappings[$row->parent_id]))
140  {
141  continue;
142  }
143 
144  $query = "DELETE FROM container_sorting ".
145  "WHERE obj_id = ".$ilDB->quote($target_obj_id,'integer')." ".
146  "AND child_id = ".$ilDB->quote($mappings[$row->child_id],'integer')." ".
147  "AND parent_type = ".$ilDB->quote($row->parent_type,'text').' '.
148  "AND parent_id = ".$ilDB->quote((int) $mappings[$row->parent_id],'integer');
149  $ilDB->manipulate($query);
150 
151  // Add new value
152  $query = "INSERT INTO container_sorting (obj_id,child_id,position,parent_type,parent_id) ".
153  "VALUES( ".
154  $ilDB->quote($target_obj_id ,'integer').", ".
155  $ilDB->quote($mappings[$row->child_id] ,'integer').", ".
156  $ilDB->quote($row->position,'integer').", ".
157  $ilDB->quote($row->parent_type,'text').", ".
158  $ilDB->quote((int) $mappings[$row->parent_id],'integer').
159  ")";
160  $ilDB->manipulate($query);
161  }
162  return true;
163  }
164 
165 
166 
174  public function sortItems($a_items)
175  {
176  $sorted = array();
177  if(!$this->manual_sort_enabled)
178  {
179  switch($this->getSortMode())
180  {
182  foreach((array) $a_items as $type => $data)
183  {
184 // $sorted[$type] = ilUtil::sortArray((array) $data,'title','asc',false);
185  $sorted[$type] = $data;
186  }
187  return $sorted ? $sorted : array();
188 
190  foreach((array) $a_items as $type => $data)
191  {
192  $sorted[$type] = ilUtil::sortArray((array) $data,'start','asc',true);
193  }
194  return $sorted ? $sorted : array();
195  }
196  return $a_items;
197  }
198  if(!count($a_items))
199  {
200  return $a_items;
201  }
202  foreach($a_items as $type => $data)
203  {
204  // Add position
205  $items = array();
206  foreach($data as $key => $item)
207  {
208  $items[$key] = $item;
209  $items[$key]['position'] = isset($this->sorting['all'][$item['child']]) ? $this->sorting['all'][$item['child']] : 9999;
210  }
211 
212  switch($type)
213  {
214  case '_all':
215  $sorted[$type] = ilUtil::sortArray((array) $items,'position','asc',true);
216  break;
217 
218  case '_non_sess':
219  $sorted[$type] = ilUtil::sortArray((array) $items,'position','asc',true);
220  break;
221 
222  default:
223  $sorted[$type] = ilUtil::sortArray((array) $items,'position','asc',true);
224  break;
225  }
226  }
227  return $sorted ? $sorted : array();
228  }
229 
237  public function sortSubItems($a_parent_type,$a_parent_id,$a_items)
238  {
239  switch($this->getSortMode())
240  {
242  // Add position
243  $items = array();
244  foreach($a_items as $key => $item)
245  {
246  $items[$key] = $item;
247  $items[$key]['position'] = isset($this->sorting[$a_parent_type][$a_parent_id][$item['child']]) ?
248  $this->sorting[$a_parent_type][$a_parent_id][$item['child']] : 9999;
249  }
250  return ilUtil::sortArray((array) $items,'position','asc',true);
251 
252 
254  default:
255  return ilUtil::sortArray((array) $a_items,'title','asc',true);
256  }
257 
258  }
259 
260 
261 
262 
269  public function isManualSortingEnabled()
270  {
271  return (bool) $this->manual_sort_enabled;
272  }
273 
281  public function savePost($a_type_positions)
282  {
283  global $ilLog;
284 
285  if(!is_array($a_type_positions))
286  {
287  return false;
288  }
289  foreach($a_type_positions as $key => $position)
290  {
291  if(!is_array($position))
292  {
293  $items[$key] = $position * 100;
294  }
295  else
296  {
297  $ilLog->write(__METHOD__.': Deprecated call');
298  foreach($position as $parent_id => $items)
299  {
300  $this->saveSubItems($key,$parent_id,$items ? $items : array());
301  }
302  }
303  }
304  $this->saveItems($items ? $items : array());
305  }
306 
307 
317  protected function saveItems($a_items)
318  {
319  global $ilDB;
320 
321  foreach($a_items as $child_id => $position)
322  {
323  $ilDB->replace(
324  'container_sorting',
325  array(
326  'obj_id' => array('integer',$this->obj_id),
327  'child_id' => array('integer',$child_id),
328  'parent_id' => array('integer',0)
329  ),
330  array(
331  'parent_type' => array('text',''),
332  'position' => array('integer',$position)
333  )
334  );
335  }
336  return true;
337  }
338 
346  protected function saveSubItems($a_parent_type,$a_parent_id,$a_items)
347  {
348  global $ilDB;
349 
350  foreach($a_items as $child_id => $position)
351  {
352  $ilDB->replace(
353  'container_sorting',
354  array(
355  'obj_id' => array('integer',$this->obj_id),
356  'child_id' => array('integer',$child_id),
357  'parent_id' => array('integer',$a_parent_id)
358  ),
359  array(
360  'parent_type' => array('text',$a_parent_type),
361  'position' => array('integer',$position)
362  )
363  );
364  }
365  return true;
366 
367  }
368 
369 
376  private function read()
377  {
378  if(!$this->obj_id)
379  {
380  return true;
381  }
382 
383  include_once('Services/Container/classes/class.ilContainerSortingSettings.php');
384  $this->manual_sort_enabled = ilContainerSortingSettings::_isManualSortingEnabled($this->obj_id);
385  $this->sorting_mode = ilContainerSortingSettings::_lookupSortMode($this->obj_id);
386 
387  $query = "SELECT * FROM container_sorting ".
388  "WHERE obj_id = ".$this->db->quote($this->obj_id ,'integer')." ORDER BY position";
389  $res = $this->db->query($query);
390  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
391  {
392  if($row->parent_id)
393  {
394  $this->sorting[$row->parent_type][$row->parent_id][$row->child_id] = $row->position;
395  }
396  else
397  {
398  $this->sorting['all'][$row->child_id] = $row->position;
399  }
400  }
401  return true;
402  }
403 }
404 
405 
406 ?>