ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilAdvancedMDRecordObjectOrderings.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
5 
11 {
12  private ilDBInterface $db;
13  private array $record_position_map = [];
14 
15  public function __construct()
16  {
17  global $DIC;
18 
19  $this->db = $DIC->database();
20  }
21 
25  public function deleteByObjId(int $obj_id): void
26  {
27  $query = 'DELETE FROM adv_md_record_obj_ord ' .
28  'WHERE obj_id = ' . $this->db->quote($obj_id, 'integer');
29  $this->db->manipulate($query);
30  }
31 
37  public function sortRecords(array $records, int $obj_id = null): array
38  {
39  // if local custom meta is not enabled use global sorting
40  $use_global = true;
41  if ($obj_id) {
43  $obj_id,
45  ''
46  )) {
47  $use_global = false;
48  }
49  }
50  if ($use_global) {
51  usort(
52  $records,
53  [
54  __CLASS__,
55  'compareRecords'
56  ]
57  );
58  return $records;
59  } else {
60  $this->readPositionsForObject($obj_id);
61  usort(
62  $records,
63  [
64  __CLASS__,
65  'compareLocalRecords'
66  ]
67  );
68  }
69  return $records;
70  }
71 
78  {
79  if ($a->getGlobalPosition() == null) {
80  $a->setGlobalPosition(999);
81  }
82  if ($b->getGlobalPosition() == null) {
83  $b->setGlobalPosition(999);
84  }
85 
86  if ($a->getGlobalPosition() < $b->getGlobalPosition()) {
87  return -1;
88  }
89  if ($a->getGlobalPosition() > $b->getGlobalPosition()) {
90  return 1;
91  }
92  return 0;
93  }
94 
101  {
102  $local_pos_a = $this->record_position_map[$a->getRecordId()] ??
103  ($a->getGlobalPosition() ?: 999);
104  $local_pos_b = $this->record_position_map[$b->getRecordId()] ??
105  ($b->getGlobalPosition() ?: 999);
106  if ($local_pos_a < $local_pos_b) {
107  return -1;
108  }
109  if ($local_pos_a > $local_pos_b) {
110  return 1;
111  }
112  return 0;
113  }
114 
118  protected function readPositionsForObject(int $obj_id): void
119  {
120  $query = 'SELECT record_id, position FROM adv_md_record_obj_ord ' .
121  'WHERE obj_id = ' . $this->db->quote($obj_id, 'integer');
122  $res = $this->db->query($query);
123 
124  $this->record_position_map = [];
125  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
126  $this->record_position_map[(int) $row->record_id] = (int) $row->position;
127  }
128  }
129 }
$res
Definition: ltiservices.php:69
compareRecords(ilAdvancedMDRecord $a, ilAdvancedMDRecord $b)
static _lookupContainerSetting(int $a_id, string $a_keyword, string $a_default_value=null)
global $DIC
Definition: feed.php:28
readPositionsForObject(int $obj_id)
Read local positions for object.
$query
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
deleteByObjId(int $obj_id)
Delete entries by obj_id.
compareLocalRecords(ilAdvancedMDRecord $a, ilAdvancedMDRecord $b)