ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilAdvancedMDRecordObjectOrderings.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3
11{
15 private $db;
16
21
25 public function __construct()
26 {
27 global $DIC;
28
29 $this->db = $DIC->database();
30 }
31
32
39 public function deleteByObjId(int $obj_id)
40 {
41 $query = 'DELETE FROM adv_md_record_obj_ord ' .
42 'WHERE obj_id = ' . $this->db->quote($obj_id, 'integer');
43 $this->db->manipulate($query);
44 }
45
46
51 public function sortRecords(array $records, int $obj_id = null)
52 {
53 // if local custom meta is not enabled use global sorting
54 $use_global = true;
55 if ($obj_id) {
57 $obj_id,
59 false
60 )) {
61 $use_global = false;
62 }
63 }
64 if ($use_global) {
65 usort(
67 [
68 __CLASS__,
69 'compareRecords'
70 ]
71 );
72 return $records;
73 } else {
74 $this->readPositionsForObject($obj_id);
75 usort(
77 [
78 __CLASS__,
79 'compareLocalRecords'
80 ]
81 );
82 }
83
84 return $records;
85 }
86
93 {
94 if ($a->getGlobalPosition() == null) {
95 $a->setGlobalPosition(999);
96 }
97 if ($b->getGlobalPosition() == null) {
98 $b->setGlobalPosition(999);
99 }
100
101 if ($a->getGlobalPosition() == $b->getGlobalPosition()) {
102 return 0;
103 }
104 if ($a->getGlobalPosition() < $b->getGlobalPosition()) {
105 return -1;
106 }
107 if ($a->getGlobalPosition() > $b->getGlobalPosition()) {
108 return 1;
109 }
110 }
111
118 {
119 $local_pos_a = isset($this->record_position_map[$a->getRecordId()]) ?
120 $this->record_position_map[$a->getRecordId()] :
121 (
122 $a->getGlobalPosition() ?
123 $a->getGlobalPosition() :
124 999
125 );
126 $local_pos_b = isset($this->record_position_map[$b->getRecordId()]) ?
127 $this->record_position_map[$b->getRecordId()] :
128 (
129 $b->getGlobalPosition() ?
130 $b->getGlobalPosition() :
131 999
132 );
133 if ($local_pos_a == $local_pos_b) {
134 return 0;
135 }
136 if ($local_pos_a < $local_pos_b) {
137 return -1;
138 }
139 if ($local_pos_a > $local_pos_b) {
140 return 1;
141 }
142 }
143
149 protected function readPositionsForObject(int $obj_id)
150 {
151 $query = 'SELECT record_id, position FROM adv_md_record_obj_ord ' .
152 'WHERE obj_id = ' . $this->db->quote($obj_id, 'integer');
153 $res = $this->db->query($query);
154
155 $this->record_position_map = [];
156 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
157 $this->record_position_map[$row->record_id] = $row->position;
158 }
159 }
160}
An exception for terminatinating execution or to throw for unit testing.
readPositionsForObject(int $obj_id)
Read local positions for object.
compareRecords(\ilAdvancedMDRecord $a, \ilAdvancedMDRecord $b)
compareLocalRecords(\ilAdvancedMDRecord $a, \ilAdvancedMDRecord $b)
deleteByObjId(int $obj_id)
Delete entries by obj_id.
__construct()
ilAdvancedMDRecordObjectOrderings constructor.
setGlobalPosition(int $position)
Set global position.
static _lookupContainerSetting($a_id, $a_keyword, $a_default_value=null)
Lookup a container setting.
$row
$query
global $DIC
Definition: saml.php:7
foreach($_POST as $key=> $value) $res
$records
Definition: simple_test.php:22