ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilObjOrgUnit.php
Go to the documentation of this file.
1 <?php
2 
28 {
29  public const TABLE_NAME = 'orgu_data';
30  protected static int $root_ref_id = 0;
31  protected static int $root_id = 0;
36 
40  protected static ?array $icons_cache = null;
44  protected int $orgu_type_id = 0;
48  protected array $amd_data;
49 
50  public function __construct(int $a_id = 0, bool $a_call_by_reference = true)
51  {
52  global $DIC;
53 
54  $this->ilDb = $DIC->database();
55  $this->type = "orgu";
56  $this->ilAppEventHandler = $DIC->event();
57  $this->rbacreview = $DIC->rbac()->review();
58  $this->rbacadmin = $DIC->rbac()->admin();
59 
60  parent::__construct($a_id, $a_call_by_reference);
61  }
62 
63  public function read(): void
64  {
65  parent::read();
67  $sql = 'SELECT * FROM ' . self::TABLE_NAME . ' WHERE orgu_id = ' . $this->ilDb->quote($this->getId(), 'integer');
68  $set = $this->ilDb->query($sql);
69  if ($this->ilDb->numRows($set)) {
70  $rec = $this->ilDb->fetchObject($set);
71  $this->setOrgUnitTypeId($rec->orgu_type_id);
72  }
73  }
74 
75  public function create(): int
76  {
77  $id = parent::create();
78  $this->ilDb->insert(self::TABLE_NAME, array(
79  'orgu_type_id' => array('integer', $this->getOrgUnitTypeId()),
80  'orgu_id' => array('integer', $this->getId()),
81  ));
82  return $id;
83  }
84 
85  public function update(): bool
86  {
87  parent::update();
88  $sql = 'SELECT * FROM ' . self::TABLE_NAME . ' WHERE orgu_id = ' . $this->ilDb->quote(
89  $this->getId(),
90  'integer'
91  );
92  $set = $this->ilDb->query($sql);
93  if ($this->ilDb->numRows($set)) {
94  $this->ilDb->update(self::TABLE_NAME, array(
95  'orgu_type_id' => array('integer', $this->getOrgUnitTypeId()),
96  ), array(
97  'orgu_id' => array('integer', $this->getId()),
98  ));
99  } else {
100  $this->ilDb->insert(self::TABLE_NAME, array(
101  'orgu_type_id' => array('integer', $this->getOrgUnitTypeId()),
102  'orgu_id' => array('integer', $this->getId()),
103  ));
104  }
105  // Update selection for advanced meta data of the type
106  if ($this->getOrgUnitTypeId()) {
108  $this->getId(),
109  'orgu_type',
110  $this->getOrgUnitType()->getAssignedAdvancedMDRecordIds()
111  );
112  } else {
113  // If no type is assigned, delete relations by passing an empty array
114  ilAdvancedMDRecord::saveObjRecSelection($this->getId(), 'orgu_type', array());
115  }
116 
117  return true;
118  }
119 
120  public function getOrgUnitTypeId(): int
121  {
122  return $this->orgu_type_id;
123  }
124 
125  public function getOrgUnitType(): ?ilOrgUnitType
126  {
128  }
129 
130  public function setOrgUnitTypeId(int $a_id): void
131  {
132  $this->orgu_type_id = $a_id;
133  }
134 
141  public function getAdvancedMDValues(int $a_record_id = 0): array
142  {
143  if (!$this->getOrgUnitTypeId()) {
144  return array();
145  }
146  // Serve from cache?
147  if (is_array($this->amd_data)) {
148  if ($a_record_id) {
149  return $this->amd_data[$a_record_id] ?? array();
150  } else {
151  return $this->amd_data;
152  }
153  }
155  foreach (ilAdvancedMDValues::getInstancesForObjectId($this->getId(), 'orgu') as $record_id => $amd_values) {
156  $amd_values = new ilAdvancedMDValues($record_id, $this->getId(), 'orgu_type', $this->getOrgUnitTypeId());
157  $amd_values->read();
158  $this->amd_data[$record_id] = $amd_values->getADTGroup()->getElements();
159  }
160  if ($a_record_id) {
161  return $this->amd_data[$a_record_id] ?? array();
162  } else {
163  return $this->amd_data;
164  }
165  }
166 
172  public static function getIconsCache(): array
173  {
174  if (is_array(self::$icons_cache)) {
175  return self::$icons_cache;
176  }
177  global $DIC;
178  $ilDb = $DIC->database();
179  $sql = 'SELECT orgu_id, ot.id AS type_id FROM orgu_data
180  INNER JOIN orgu_types AS ot ON (ot.id = orgu_data.orgu_type_id)
181  WHERE ot.icon IS NOT NULL';
182  $set = $ilDb->query($sql);
183  $icons_cache = array();
184  while ($row = $ilDb->fetchObject($set)) {
185  $type = ilOrgUnitType::getInstance($row->type_id);
186  if ($type && is_file($type->getIconPath(true))) {
187  $icons_cache[$row->orgu_id] = $type->getIconPath(true);
188  }
189  }
190  self::$icons_cache = $icons_cache;
191 
192  return $icons_cache;
193  }
194 
195  public static function getRootOrgRefId(): int
196  {
197  self::loadRootOrgRefIdAndId();
198 
199  return self::$root_ref_id;
200  }
201 
202  public static function getRootOrgId(): int
203  {
204  self::loadRootOrgRefIdAndId();
205 
206  return self::$root_id;
207  }
208 
209  private static function loadRootOrgRefIdAndId(): void
210  {
211  if (self::$root_ref_id === 0 || self::$root_id === 0) {
212  global $DIC;
213  $ilDb = $DIC['ilDB'];
214  $q = "SELECT o.obj_id, r.ref_id FROM object_data o
215  INNER JOIN object_reference r ON r.obj_id = o.obj_id
216  WHERE title = " . $ilDb->quote('__OrgUnitAdministration', 'text') . "";
217  $set = $ilDb->query($q);
218  $res = $ilDb->fetchAssoc($set);
219  self::$root_id = (int) $res["obj_id"];
220  self::$root_ref_id = (int) $res["ref_id"];
221  }
222  }
223 
228  public function assignUsersToEmployeeRole(array $user_ids): void
229  {
231 
232  foreach ($user_ids as $user_id) {
233  ilOrgUnitUserAssignment::findOrCreateAssignment($user_id, $position_id, $this->getRefId());
234 
235  $this->ilAppEventHandler->raise('Modules/OrgUnit', 'assignUsersToEmployeeRole', array(
236  'object' => $this,
237  'obj_id' => $this->getId(),
238  'ref_id' => $this->getRefId(),
239  'position_id' => $position_id,
240  'user_id' => $user_id,
241  ));
242  }
243  }
244 
249  public function assignUsersToSuperiorRole(array $user_ids): void
250  {
252 
253  foreach ($user_ids as $user_id) {
254  ilOrgUnitUserAssignment::findOrCreateAssignment($user_id, $position_id, $this->getRefId());
255 
256  $this->ilAppEventHandler->raise('Modules/OrgUnit', 'assignUsersToSuperiorRole', array(
257  'object' => $this,
258  'obj_id' => $this->getId(),
259  'ref_id' => $this->getRefId(),
260  'position_id' => $position_id,
261  'user_id' => $user_id,
262  ));
263  }
264  }
265 
266  public function deassignUserFromEmployeeRole(int $user_id): void
267  {
269  ilOrgUnitUserAssignment::findOrCreateAssignment($user_id, $position_id, $this->getRefId())->delete();
270 
271  $this->ilAppEventHandler->raise('Modules/OrgUnit', 'deassignUserFromEmployeeRole', array(
272  'object' => $this,
273  'obj_id' => $this->getId(),
274  'ref_id' => $this->getRefId(),
275  'position_id' => $position_id,
276  'user_id' => $user_id,
277  ));
278  }
279 
280  public function deassignUserFromSuperiorRole(int $user_id): void
281  {
283  ilOrgUnitUserAssignment::findOrCreateAssignment($user_id, $position_id, $this->getRefId())->delete();
284 
285  $this->ilAppEventHandler->raise('Modules/OrgUnit', 'deassignUserFromSuperiorRole', array(
286  'object' => $this,
287  'obj_id' => $this->getId(),
288  'ref_id' => $this->getRefId(),
289  'position_id' => $position_id,
290  'user_id' => $user_id,
291  ));
292  }
293 
297  public function assignUserToLocalRole(int $role_id, int $user_id): bool
298  {
299  $arrLocalRoles = $this->rbacreview->getLocalRoles($this->getRefId());
300  if (!in_array($role_id, $arrLocalRoles)) {
301  return false;
302  }
303 
304  $return = $this->rbacadmin->assignUser($role_id, $user_id);
305 
306  $this->ilAppEventHandler->raise('Modules/OrgUnit', 'assignUserToLocalRole', array(
307  'object' => $this,
308  'obj_id' => $this->getId(),
309  'ref_id' => $this->getRefId(),
310  'role_id' => $role_id,
311  'user_id' => $user_id,
312  ));
313 
314  return $return;
315  }
316 
320  public function deassignUserFromLocalRole(int $role_id, int $user_id): bool
321  {
322  $arrLocalRoles = $this->rbacreview->getLocalRoles($this->getRefId());
323  if (!in_array($role_id, $arrLocalRoles)) {
324  return false;
325  }
326 
327  $return = $this->rbacadmin->deassignUser($role_id, $user_id);
328 
329  $this->ilAppEventHandler->raise('Modules/OrgUnit', 'deassignUserFromLocalRole', array(
330  'object' => $this,
331  'obj_id' => $this->getId(),
332  'ref_id' => $this->getRefId(),
333  'role_id' => $role_id,
334  'user_id' => $user_id,
335  ));
336 
337  return $return;
338  }
339 
340  public static function _exists(int $id, bool $isReference = false, ?string $type = "orgu"): bool
341  {
342  return parent::_exists($id, $isReference, "orgu");
343  }
344 
345  public function getTitle(): string
346  {
347  if (parent::getTitle() !== "__OrgUnitAdministration") {
348  return parent::getTitle();
349  } else {
350  return $this->lng->txt("objs_orgu");
351  }
352  }
353 
357  public function getLongDescription(): string
358  {
359  if (parent::getTitle() === "__OrgUnitAdministration") {
360  return $this->lng->txt("obj_orgu_description");
361  } else {
362  return parent::getLongDescription();
363  }
364  }
365 
369  public function getTranslations()
370  {
371  $q = "SELECT * FROM object_translation WHERE obj_id = " . $this->ilDb->quote(
372  $this->getId(),
373  'integer'
374  ) . " ORDER BY lang_default DESC";
375  $r = $this->db->query($q);
376 
377  $data = [];
378  while ($row = $r->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
379  $data[$row->lang_code] = array(
380  "title" => $row->title,
381  "desc" => $row->description,
382  "lang" => $row->lang_code,
383  'default' => $row->lang_default,
384  );
385  }
386 
387  $translations = $data;
388 
389  if (!count($translations)) {
390  $this->addTranslation($this->getTitle(), "", $this->lng->getDefaultLanguage(), true);
391  $translations[$this->lng->getDefaultLanguage()] = array(
392  "title" => $this->getTitle(),
393  "desc" => "",
394  "lang" => $this->lng->getDefaultLanguage(),
395  );
396  }
397 
398  return $translations;
399  }
400 
406  public function delete(): bool
407  {
408  // always call parent delete function first!!
409  if (!parent::delete()) {
410  return false;
411  }
412 
413  // put here category specific stuff
415 
416  $query = "DELETE FROM object_translation WHERE obj_id = " . $this->ilDb->quote($this->getId(), 'integer');
417  $this->ilDb->manipulate($query);
418 
419  $this->ilAppEventHandler->raise('Modules/OrgUnit', 'delete', array(
420  'object' => $this,
421  'obj_id' => $this->getId(),
422  ));
423 
424  $sql = 'DELETE FROM ' . self::TABLE_NAME . ' WHERE orgu_id = ' . $this->ilDb->quote($this->getId(), 'integer');
425  $this->ilDb->manipulate($sql);
426 
427  $path = ilOrgUnitPathStorage::find($this->getRefId());
428  if ($path instanceof ilOrgUnitPathStorage) {
429  $path->delete();
430  }
431 
432  // Delete all position assignments to this object.
433  $assignments = ilOrgUnitUserAssignment::where(array(
434  'orgu_id' => $this->getRefId(),
435  ))->get();
436  foreach ($assignments as $assignment) {
437  $assignment->delete();
438  }
439 
440  return true;
441  }
442 
446  public function removeTranslations(): void
447  {
448  $query = "DELETE FROM object_translation WHERE obj_id= " . $this->ilDb->quote($this->getId(), 'integer');
449  $res = $this->ilDb->manipulate($query);
450  }
451 
456  public function deleteTranslation(string $a_lang): void
457  {
458  $query = "DELETE FROM object_translation WHERE obj_id= " . $this->quote(
459  $this->getId(),
460  'integer'
461  ) . " AND lang_code = "
462  . $this->quote($a_lang, 'text');
463  $this->ilDb->manipulate($query);
464  }
465 
469  public function addTranslation(string $a_title, string $a_desc, string $a_lang, string $a_lang_default): void
470  {
471  if (empty($a_title)) {
472  $a_title = "NO TITLE";
473  }
474 
475  $query = "INSERT INTO object_translation " . "(obj_id,title,description,lang_code,lang_default) " . "VALUES " . "("
476  . $this->ilDb->quote($this->getId(), 'integer') . "," . $this->ilDb->quote(
477  $a_title,
478  'text'
479  ) . "," . $this->ilDb->quote($a_desc, 'text') . ","
480  . $this->ilDb->quote($a_lang, 'text') . "," . $this->ilDb->quote($a_lang_default, 'integer') . ")";
481  $this->ilDb->manipulate($query);
482  }
483 
487  public function updateTranslation(string $title, string $desc, string $lang, string $lang_default): void
488  {
489  if (empty($title)) {
490  $a_title = "NO TITLE";
491  }
492 
493  $query = "UPDATE object_translation SET ";
494 
495  $query .= " title = " . $this->ilDb->quote($title, 'text');
496 
497  if ($desc !== "") {
498  $query .= ", description = " . $this->ilDb->quote($desc, 'text') . " ";
499  }
500 
501  $query .= ", lang_default = " . $this->ilDb->quote($lang_default, 'integer') . " ";
502 
503  $query .= " WHERE obj_id = " . $this->ilDb->quote(
504  $this->getId(),
505  'integer'
506  ) . " AND lang_code = " . $this->ilDb->quote($lang, 'text');
507  $this->ilDb->manipulate($query);
508  }
509 
510  public function writePath(): void
511  {
512  if ($this->getRefId()) {
513  ilOrgUnitPathStorage::writePathByRefId($this->getRefId());
514  } else {
515  throw new \LogicException('No ref_id on OrgU with id ' . $this->getId(), 1);
516  }
517  }
518 }
static getInstancesForObjectId(int $a_obj_id, ?string $a_obj_type=null, string $a_sub_type="-", int $a_sub_id=0)
string $title
static findOrCreateAssignment(int $user_id, int $position_id, int $orgu_id)
$res
Definition: ltiservices.php:69
Global event handler.
string $type
static int $root_ref_id
setOrgUnitTypeId(int $a_id)
static getIconsCache()
Returns an array that maps from OrgUnit object IDs to its icon defined by the assigned OrgUnit type...
addTranslation(string $a_title, string $a_desc, string $a_lang, string $a_lang_default)
add a new translation to current OrgUnit
removeTranslations()
remove all Translations of current OrgUnit
static array $icons_cache
Cache storing OrgUnit objects that have OrgUnit types with custom icons assigned. ...
ilRbacAdmin $rbacadmin
assignUsersToSuperiorRole(array $user_ids)
Adds the user ids to the position superior.
const USER_FOLDER_ID
Definition: constants.php:33
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _updateUserFolderAssignment(int $a_old_id, int $a_new_id)
Update user folder assignment Typically called after deleting a category with local user accounts...
fetchAssoc(ilDBStatement $statement)
string $desc
updateTranslation(string $title, string $desc, string $lang, string $lang_default)
update a translation to current OrgUnit
array $amd_data
Advanced Metadata Values for this OrgUnit.
static saveObjRecSelection(int $a_obj_id, string $a_sub_type="", array $a_records=null, bool $a_delete_before=true)
Save repository object record selection.
__construct(int $a_id=0, bool $a_call_by_reference=true)
quote($value, string $type)
ilAppEventHandler $ilAppEventHandler
static _exists(int $id, bool $isReference=false, ?string $type="orgu")
static where($where, $operator=null)
static loadRootOrgRefIdAndId()
$path
Definition: ltiservices.php:32
ilRbacReview $rbacreview
ilDBInterface $ilDb
global $DIC
Definition: feed.php:28
getLongDescription()
get object long description (stored in object_description)
deassignUserFromLocalRole(int $role_id, int $user_id)
Deassign a given user to a given local role.
static getCorePositionId(int $core_identifier)
fetchObject(ilDBStatement $query_result)
query(string $query)
Run a (read-only) Query on the database.
deleteTranslation(string $a_lang)
remove translations of current OrgUnit
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$query
assignUserToLocalRole(int $role_id, int $user_id)
Assign a given user to a given local role.
static getRootOrgRefId()
$lang
Definition: xapiexit.php:26
int $orgu_type_id
ID of assigned OrgUnit type.
__construct(Container $dic, ilPlugin $plugin)
static int $root_id
Class ilRbacAdmin Core functions for role based access control.
deassignUserFromEmployeeRole(int $user_id)
deassignUserFromSuperiorRole(int $user_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
raise(string $a_component, string $a_event, array $a_parameter=[])
Raise an event.
assignUsersToEmployeeRole(array $user_ids)
Adds the user ids to the position employee.
static getInstance(int $a_id)
Get instance of an ilOrgUnitType object Returns object from cache or from database, returns null if no object was found.