ILIAS  trunk Revision v11.0_alpha-1731-gff9cd7e2bd3
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilAdvancedMDRecordObjectOrderings.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
25 {
26  private ilDBInterface $db;
27  private array $record_position_map = [];
28 
29  public function __construct()
30  {
31  global $DIC;
32 
33  $this->db = $DIC->database();
34  }
35 
39  public function deleteByObjId(int $obj_id): void
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 
51  public function sortRecords(array $records, ?int $obj_id = null): array
52  {
53  // if local custom meta is not enabled use global sorting
54  $use_global = true;
55  if ($obj_id) {
57  $obj_id,
59  ''
60  )) {
61  $use_global = false;
62  }
63  }
64  if ($use_global) {
65  usort(
66  $records,
67  [
68  __CLASS__,
69  'compareRecords'
70  ]
71  );
72  return $records;
73  } else {
74  $this->readPositionsForObject($obj_id);
75  usort(
76  $records,
77  [
78  __CLASS__,
79  'compareLocalRecords'
80  ]
81  );
82  }
83  return $records;
84  }
85 
92  {
93  if ($a->getGlobalPosition() == null) {
94  $a->setGlobalPosition(999);
95  }
96  if ($b->getGlobalPosition() == null) {
97  $b->setGlobalPosition(999);
98  }
99 
100  if ($a->getGlobalPosition() < $b->getGlobalPosition()) {
101  return -1;
102  }
103  if ($a->getGlobalPosition() > $b->getGlobalPosition()) {
104  return 1;
105  }
106  return 0;
107  }
108 
115  {
116  $local_pos_a = $this->record_position_map[$a->getRecordId()] ??
117  ($a->getGlobalPosition() ?: 999);
118  $local_pos_b = $this->record_position_map[$b->getRecordId()] ??
119  ($b->getGlobalPosition() ?: 999);
120  if ($local_pos_a < $local_pos_b) {
121  return -1;
122  }
123  if ($local_pos_a > $local_pos_b) {
124  return 1;
125  }
126  return 0;
127  }
128 
132  protected function readPositionsForObject(int $obj_id): void
133  {
134  $query = 'SELECT record_id, position FROM adv_md_record_obj_ord ' .
135  'WHERE obj_id = ' . $this->db->quote($obj_id, 'integer');
136  $res = $this->db->query($query);
137 
138  $this->record_position_map = [];
139  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
140  $this->record_position_map[(int) $row->record_id] = (int) $row->position;
141  }
142  }
143 }
$res
Definition: ltiservices.php:66
compareRecords(ilAdvancedMDRecord $a, ilAdvancedMDRecord $b)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
readPositionsForObject(int $obj_id)
Read local positions for object.
global $DIC
Definition: shib_login.php:22
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
static _lookupContainerSetting(int $a_id, string $a_keyword, ?string $a_default_value=null)
deleteByObjId(int $obj_id)
Delete entries by obj_id.
compareLocalRecords(ilAdvancedMDRecord $a, ilAdvancedMDRecord $b)