ILIAS  trunk Revision v11.0_alpha-1769-g99a433fe2dc
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilItemGroupItems.php
Go to the documentation of this file.
1 <?php
2 
26 {
27  protected ilDBInterface $db;
30  protected ilLogger $log;
31  public ilTree $tree;
32  public ilLanguage $lng;
33  public int $item_group_id = 0;
34  public int $item_group_ref_id = 0;
35  public array $items = array();
36 
37  public function __construct(
38  int $a_item_group_ref_id = 0
39  ) {
40  global $DIC;
41 
42  $this->obj_data_cache = $DIC["ilObjDataCache"];
43  $this->log = $DIC["ilLog"];
44  $this->db = $DIC->database();
45  $this->lng = $DIC->language();
46  $this->tree = $DIC->repositoryTree();
47  $this->obj_def = $DIC["objDefinition"];
48 
49  $this->setItemGroupRefId($a_item_group_ref_id);
50  if ($this->getItemGroupRefId() > 0) {
51  $this->setItemGroupId(ilObject::_lookupObjId($a_item_group_ref_id));
52  }
53 
54  if ($this->getItemGroupId() > 0) {
55  $this->read();
56  }
57  }
58 
59  public function setItemGroupId(int $a_val): void
60  {
61  $this->item_group_id = $a_val;
62  }
63 
64  public function getItemGroupId(): int
65  {
66  return $this->item_group_id;
67  }
68 
69  public function setItemGroupRefId(int $a_val): void
70  {
71  $this->item_group_ref_id = $a_val;
72  }
73 
74  public function getItemGroupRefId(): int
75  {
77  }
78 
82  public function setItems(array $a_val): void
83  {
84  $this->items = $a_val;
85  }
86 
87  public function getItems(): array
88  {
89  return $this->items;
90  }
91 
92  public function addItem(int $a_item_ref_id): void
93  {
94  if (!in_array($a_item_ref_id, $this->items)) {
95  $this->items[] = $a_item_ref_id;
96  }
97  }
98 
99  public function delete(): void
100  {
101  $query = "DELETE FROM item_group_item " .
102  "WHERE item_group_id = " . $this->db->quote($this->getItemGroupId(), 'integer');
103  $this->db->manipulate($query);
104  }
105 
106  public function update(): void
107  {
108  $this->delete();
109 
110  foreach ($this->items as $item) {
111  $query = "INSERT INTO item_group_item (item_group_id,item_ref_id) " .
112  "VALUES( " .
113  $this->db->quote($this->getItemGroupId(), 'integer') . ", " .
114  $this->db->quote($item, 'integer') . " " .
115  ")";
116  $this->db->manipulate($query);
117  }
118  }
119 
120  public function read(): void
121  {
122  $this->items = array();
123  $set = $this->db->query(
124  "SELECT * FROM item_group_item " .
125  " WHERE item_group_id = " . $this->db->quote($this->getItemGroupId(), "integer")
126  );
127  while ($rec = $this->db->fetchAssoc($set)) {
128  $this->items[] = $rec["item_ref_id"];
129  }
130  }
131 
132  public function getAssignableItems(): array
133  {
134  $objDefinition = $this->obj_def;
135 
136  if ($this->getItemGroupRefId() <= 0) {
137  return array();
138  }
139 
140  $parent_node = $this->tree->getNodeData(
141  $this->tree->getParentId($this->getItemGroupRefId())
142  );
143 
144  $materials = array();
145  $nodes = $this->tree->getChilds($parent_node["child"]);
146 
147  foreach ($nodes as $node) {
148  // filter side blocks and session, item groups and role folder
149  if ($node['child'] == $parent_node["child"] ||
150  $this->obj_def->isSideBlock($node['type']) ||
151  in_array($node['type'], array('sess', 'itgr', 'rolf', 'adm'))) {
152  continue;
153  }
154 
155  // filter hidden files
156  // see http://www.ilias.de/mantis/view.php?id=10269
157  if ($node['type'] == "file" &&
158  ilObjFileAccess::_isFileHidden((string) $node['title'])) {
159  continue;
160  }
161 
162  if ($objDefinition->isInactivePlugin((string) $node['type'])) {
163  continue;
164  }
165 
166  $materials[] = $node;
167  }
168 
169  $materials = ilArrayUtil::sortArray($materials, "title", "asc");
170 
171  return $materials;
172  }
173 
174  public function getValidItems(): array
175  {
176  $items = $this->getItems();
177  $ass_items = $this->getAssignableItems();
178  $valid_items = array();
179  foreach ($ass_items as $aitem) {
180  if (in_array($aitem["ref_id"], $items)) {
181  $valid_items[] = $aitem["ref_id"];
182  }
183  }
184  return $valid_items;
185  }
186 
187  public function cloneItems(
188  int $a_source_id,
189  int $a_copy_id
190  ): void {
191  $ilLog = $this->log;
192 
193  $ilLog->write(__METHOD__ . ': Begin cloning item group materials ... -' . $a_source_id . '-');
194 
195  $cwo = ilCopyWizardOptions::_getInstance($a_copy_id);
196  $mappings = $cwo->getMappings();
197 
198  $new_items = array();
199  // check: is this a ref id!?
200  $source_ig = new ilItemGroupItems($a_source_id);
201  foreach ($source_ig->getItems() as $item_ref_id) {
202  if (isset($mappings[$item_ref_id]) and $mappings[$item_ref_id]) {
203  $ilLog->write(__METHOD__ . ': Clone item group item nr. ' . $item_ref_id);
204  $new_items[] = $mappings[$item_ref_id];
205  } else {
206  $ilLog->write(__METHOD__ . ': No mapping found for item group item nr. ' . $item_ref_id);
207  }
208  }
209  $this->setItems($new_items);
210  $this->update();
211  $ilLog->write(__METHOD__ . ': Finished cloning item group items ...');
212  }
213 
214  public static function _getItemsOfContainer(int $a_ref_id): array
215  {
216  global $DIC;
217 
218  $ilDB = $DIC->database();
219  $tree = $DIC->repositoryTree();
220 
221  $itgr_ids = [];
222  $itgr_nodes = $tree->getChildsByType($a_ref_id, 'itgr');
223  foreach ($itgr_nodes as $node) {
224  $itgr_ids[] = $node['obj_id'];
225  }
226  $query = "SELECT item_ref_id FROM item_group_item " .
227  "WHERE " . $ilDB->in('item_group_id', $itgr_ids, false, 'integer');
228 
229 
230  $res = $ilDB->query($query);
231  $items = [];
232  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
233  $items[] = $row->item_ref_id;
234  }
235  return $items;
236  }
237 }
$res
Definition: ltiservices.php:66
write(string $message, $level=ilLogLevel::INFO, array $context=[])
write log message
static _isFileHidden(string $a_file_name)
Returns true, if a file with the specified name, is usually hidden from the user. ...
static _lookupObjId(int $ref_id)
getChildsByType(int $a_node_id, string $a_type)
get child nodes of given node by object type
addItem(int $a_item_ref_id)
static _getItemsOfContainer(int $a_ref_id)
global $DIC
Definition: shib_login.php:22
ilObjectDataCache $obj_data_cache
ilObjectDefinition $obj_def
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(int $a_item_group_ref_id=0)
cloneItems(int $a_source_id, int $a_copy_id)
static _getInstance(int $a_copy_id)
static sortArray(array $array, string $a_array_sortby_key, string $a_array_sortorder="asc", bool $a_numeric=false, bool $a_keep_keys=false)