ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilObjOrgUnit.php
Go to the documentation of this file.
1 <?php
2 
29 {
30  public const TABLE_NAME = 'orgu_data';
31  protected static int $root_ref_id = 0;
32  protected static int $root_id = 0;
39 
43  protected static ?array $icons_cache = null;
47  protected int $orgu_type_id = 0;
51  protected array $amd_data;
52 
53  public function __construct(int $a_id = 0, bool $a_call_by_reference = true)
54  {
55  global $DIC;
56 
57  $this->ilDb = $DIC->database();
58  $this->type = "orgu";
59  $this->ilAppEventHandler = $DIC->event();
60  $this->rbacreview = $DIC->rbac()->review();
61  $this->rbacadmin = $DIC->rbac()->admin();
62 
64  $this->positionRepo = $dic["repo.Positions"];
65  $this->assignmentRepo = $dic["repo.UserAssignments"];
66 
67  parent::__construct($a_id, $a_call_by_reference);
68  }
69 
70  public function read(): void
71  {
72  parent::read();
74  $sql = 'SELECT * FROM ' . self::TABLE_NAME . ' WHERE orgu_id = ' . $this->ilDb->quote($this->getId(), 'integer');
75  $set = $this->ilDb->query($sql);
76  if ($this->ilDb->numRows($set)) {
77  $rec = $this->ilDb->fetchObject($set);
78  $this->setOrgUnitTypeId($rec->orgu_type_id);
79  }
80  }
81 
82  public function create(): int
83  {
84  $id = parent::create();
85  $this->ilDb->insert(self::TABLE_NAME, array(
86  'orgu_type_id' => array('integer', $this->getOrgUnitTypeId()),
87  'orgu_id' => array('integer', $this->getId()),
88  ));
89  return $id;
90  }
91 
92  public function update(): bool
93  {
94  parent::update();
95  $sql = 'SELECT * FROM ' . self::TABLE_NAME . ' WHERE orgu_id = ' . $this->ilDb->quote(
96  $this->getId(),
97  'integer'
98  );
99  $set = $this->ilDb->query($sql);
100  if ($this->ilDb->numRows($set)) {
101  $this->ilDb->update(self::TABLE_NAME, array(
102  'orgu_type_id' => array('integer', $this->getOrgUnitTypeId()),
103  ), array(
104  'orgu_id' => array('integer', $this->getId()),
105  ));
106  } else {
107  $this->ilDb->insert(self::TABLE_NAME, array(
108  'orgu_type_id' => array('integer', $this->getOrgUnitTypeId()),
109  'orgu_id' => array('integer', $this->getId()),
110  ));
111  }
112  // Update selection for advanced meta data of the type
113  if ($this->getOrgUnitTypeId()) {
115  $this->getId(),
116  'orgu_type',
117  $this->getOrgUnitType()->getAssignedAdvancedMDRecordIds()
118  );
119  } else {
120  // If no type is assigned, delete relations by passing an empty array
121  ilAdvancedMDRecord::saveObjRecSelection($this->getId(), 'orgu_type', array());
122  }
123 
124  return true;
125  }
126 
127  public function getOrgUnitTypeId(): int
128  {
129  return $this->orgu_type_id;
130  }
131 
132  public function getOrgUnitType(): ?ilOrgUnitType
133  {
135  }
136 
137  public function setOrgUnitTypeId(int $a_id): void
138  {
139  $this->orgu_type_id = $a_id;
140  }
141 
148  public function getAdvancedMDValues(int $a_record_id = 0): array
149  {
150  if (!$this->getOrgUnitTypeId()) {
151  return array();
152  }
153  // Serve from cache?
154  if (is_array($this->amd_data)) {
155  if ($a_record_id) {
156  return $this->amd_data[$a_record_id] ?? array();
157  } else {
158  return $this->amd_data;
159  }
160  }
162  foreach (ilAdvancedMDValues::getInstancesForObjectId($this->getId(), 'orgu') as $record_id => $amd_values) {
163  $amd_values = new ilAdvancedMDValues($record_id, $this->getId(), 'orgu_type', $this->getOrgUnitTypeId());
164  $amd_values->read();
165  $this->amd_data[$record_id] = $amd_values->getADTGroup()->getElements();
166  }
167  if ($a_record_id) {
168  return $this->amd_data[$a_record_id] ?? array();
169  } else {
170  return $this->amd_data;
171  }
172  }
173 
179  public static function getIconsCache(): array
180  {
181  if (is_array(self::$icons_cache)) {
182  return self::$icons_cache;
183  }
184  global $DIC;
185  $ilDb = $DIC->database();
186  $sql = 'SELECT orgu_id, ot.id AS type_id FROM orgu_data
187  INNER JOIN orgu_types AS ot ON (ot.id = orgu_data.orgu_type_id)
188  WHERE ot.icon IS NOT NULL';
189  $set = $ilDb->query($sql);
190  $icons_cache = array();
191 
192  $irss = $DIC['resource_storage'];
193  while ($row = $ilDb->fetchObject($set)) {
194  $type = ilOrgUnitType::getInstance($row->type_id);
195  if ($type && $icon_id = $irss->manage()->find($type->getIconIdentifier())) {
196  $icons_cache[$row->orgu_id] = $irss->consume()->src($icon_id)->getSrc();
197  }
198  }
199  self::$icons_cache = $icons_cache;
200 
201  return $icons_cache;
202  }
203 
204  public static function getRootOrgRefId(): int
205  {
206  self::loadRootOrgRefIdAndId();
207 
208  return self::$root_ref_id;
209  }
210 
211  public static function getRootOrgId(): int
212  {
213  self::loadRootOrgRefIdAndId();
214 
215  return self::$root_id;
216  }
217 
218  private static function loadRootOrgRefIdAndId(): void
219  {
220  if (self::$root_ref_id === 0 || self::$root_id === 0) {
221  global $DIC;
222  $ilDb = $DIC['ilDB'];
223  $q = "SELECT o.obj_id, r.ref_id FROM object_data o
224  INNER JOIN object_reference r ON r.obj_id = o.obj_id
225  WHERE title = " . $ilDb->quote('__OrgUnitAdministration', 'text') . "";
226  $set = $ilDb->query($q);
227  $res = $ilDb->fetchAssoc($set);
228  self::$root_id = (int) $res["obj_id"];
229  self::$root_ref_id = (int) $res["ref_id"];
230  }
231  }
232 
237  public function assignUsersToEmployeeRole(array $user_ids): void
238  {
239  $position_id = $this->positionRepo
240  ->getSingle(ilOrgUnitPosition::CORE_POSITION_EMPLOYEE, 'core_identifier')
241  ->getId();
242 
243  foreach ($user_ids as $user_id) {
244  $assignment = $this->assignmentRepo->get($user_id, $position_id, $this->getRefId());
245 
246  $this->ilAppEventHandler->raise('Modules/OrgUnit', 'assignUsersToEmployeeRole', array(
247  'object' => $this,
248  'obj_id' => $this->getId(),
249  'ref_id' => $this->getRefId(),
250  'position_id' => $position_id,
251  'user_id' => $user_id
252  ));
253  }
254  }
255 
260  public function assignUsersToSuperiorRole(array $user_ids): void
261  {
262  $position_id = $this->positionRepo
263  ->getSingle(ilOrgUnitPosition::CORE_POSITION_SUPERIOR, 'core_identifier')
264  ->getId();
265 
266  foreach ($user_ids as $user_id) {
267  $assignment = $this->assignmentRepo->get($user_id, $position_id, $this->getRefId());
268 
269  $this->ilAppEventHandler->raise('Modules/OrgUnit', 'assignUsersToSuperiorRole', array(
270  'object' => $this,
271  'obj_id' => $this->getId(),
272  'ref_id' => $this->getRefId(),
273  'position_id' => $position_id,
274  'user_id' => $user_id
275  ));
276  }
277  }
278 
279  public function deassignUserFromEmployeeRole(int $user_id): void
280  {
281  $position_id = $this->positionRepo
282  ->getSingle(ilOrgUnitPosition::CORE_POSITION_EMPLOYEE, 'core_identifier')
283  ->getId();
284 
285  $assignment = $this->assignmentRepo->find($user_id, $position_id, $this->getRefId());
286  if ($assignment) {
287  $this->assignmentRepo->delete($assignment);
288  }
289 
290  $this->ilAppEventHandler->raise('Modules/OrgUnit', 'deassignUserFromEmployeeRole', array(
291  'object' => $this,
292  'obj_id' => $this->getId(),
293  'ref_id' => $this->getRefId(),
294  'position_id' => $position_id,
295  'user_id' => $user_id,
296  ));
297  }
298 
299  public function deassignUserFromSuperiorRole(int $user_id): void
300  {
301  $position_id = $this->positionRepo
302  ->getSingle(ilOrgUnitPosition::CORE_POSITION_SUPERIOR, 'core_identifier')
303  ->getId();
304 
305  $assignment = $this->assignmentRepo->find($user_id, $position_id, $this->getRefId());
306  if ($assignment) {
307  $this->assignmentRepo->delete($assignment);
308  }
309 
310  $this->ilAppEventHandler->raise('Modules/OrgUnit', 'deassignUserFromSuperiorRole', array(
311  'object' => $this,
312  'obj_id' => $this->getId(),
313  'ref_id' => $this->getRefId(),
314  'position_id' => $position_id,
315  'user_id' => $user_id,
316  ));
317  }
318 
322  public function assignUserToLocalRole(int $role_id, int $user_id): bool
323  {
324  $arrLocalRoles = $this->rbacreview->getLocalRoles($this->getRefId());
325  if (!in_array($role_id, $arrLocalRoles)) {
326  return false;
327  }
328 
329  $return = $this->rbacadmin->assignUser($role_id, $user_id);
330 
331  $this->ilAppEventHandler->raise('Modules/OrgUnit', 'assignUserToLocalRole', array(
332  'object' => $this,
333  'obj_id' => $this->getId(),
334  'ref_id' => $this->getRefId(),
335  'role_id' => $role_id,
336  'user_id' => $user_id,
337  ));
338 
339  return $return;
340  }
341 
345  public function deassignUserFromLocalRole(int $role_id, int $user_id): bool
346  {
347  $arrLocalRoles = $this->rbacreview->getLocalRoles($this->getRefId());
348  if (!in_array($role_id, $arrLocalRoles)) {
349  return false;
350  }
351 
352  $return = $this->rbacadmin->deassignUser($role_id, $user_id);
353 
354  $this->ilAppEventHandler->raise('Modules/OrgUnit', 'deassignUserFromLocalRole', array(
355  'object' => $this,
356  'obj_id' => $this->getId(),
357  'ref_id' => $this->getRefId(),
358  'role_id' => $role_id,
359  'user_id' => $user_id,
360  ));
361 
362  return $return;
363  }
364 
365  public static function _exists(int $id, bool $isReference = false, ?string $type = "orgu"): bool
366  {
367  return parent::_exists($id, $isReference, "orgu");
368  }
369 
370  public function getTitle(): string
371  {
372  if (parent::getTitle() !== "__OrgUnitAdministration") {
373  return parent::getTitle();
374  } else {
375  return $this->lng->txt("objs_orgu");
376  }
377  }
378 
382  public function getLongDescription(): string
383  {
384  if (parent::getTitle() === "__OrgUnitAdministration") {
385  return $this->lng->txt("obj_orgu_description");
386  } else {
387  return parent::getLongDescription();
388  }
389  }
390 
394  public function getTranslations()
395  {
396  $q = "SELECT * FROM object_translation WHERE obj_id = " . $this->ilDb->quote(
397  $this->getId(),
398  'integer'
399  ) . " ORDER BY lang_default DESC";
400  $r = $this->db->query($q);
401 
402  $data = [];
403  while ($row = $r->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
404  $data[$row->lang_code] = array(
405  "title" => $row->title,
406  "desc" => $row->description,
407  "lang" => $row->lang_code,
408  'default' => $row->lang_default,
409  );
410  }
411 
412  $translations = $data;
413 
414  if (!count($translations)) {
415  $this->addTranslation($this->getTitle(), "", $this->lng->getDefaultLanguage(), true);
416  $translations[$this->lng->getDefaultLanguage()] = array(
417  "title" => $this->getTitle(),
418  "desc" => "",
419  "lang" => $this->lng->getDefaultLanguage(),
420  );
421  }
422 
423  return $translations;
424  }
425 
431  public function delete(): bool
432  {
433  // always call parent delete function first!!
434  if (!parent::delete()) {
435  return false;
436  }
437 
438  // put here category specific stuff
440 
441  $query = "DELETE FROM object_translation WHERE obj_id = " . $this->ilDb->quote($this->getId(), 'integer');
442  $this->ilDb->manipulate($query);
443 
444  $this->ilAppEventHandler->raise('Modules/OrgUnit', 'delete', array(
445  'object' => $this,
446  'obj_id' => $this->getId(),
447  ));
448 
449  $sql = 'DELETE FROM ' . self::TABLE_NAME . ' WHERE orgu_id = ' . $this->ilDb->quote($this->getId(), 'integer');
450  $this->ilDb->manipulate($sql);
451 
452  $path = ilOrgUnitPathStorage::find($this->getRefId());
453  if ($path instanceof ilOrgUnitPathStorage) {
454  $path->delete();
455  }
456 
457  // Delete all position assignments to this object.
458  $assignments = $this->assignmentRepo->getByOrgUnit($this->getRefId());
459  foreach ($assignments as $assignment) {
460  $this->assignmentRepo->delete($assignment);
461  }
462 
463  return true;
464  }
465 
469  public function removeTranslations(): void
470  {
471  $query = "DELETE FROM object_translation WHERE obj_id= " . $this->ilDb->quote($this->getId(), 'integer');
472  $res = $this->ilDb->manipulate($query);
473  }
474 
479  public function deleteTranslation(string $a_lang): void
480  {
481  $query = "DELETE FROM object_translation WHERE obj_id= " . $this->quote(
482  $this->getId(),
483  'integer'
484  ) . " AND lang_code = "
485  . $this->quote($a_lang, 'text');
486  $this->ilDb->manipulate($query);
487  }
488 
492  public function addTranslation(string $a_title, string $a_desc, string $a_lang, string $a_lang_default): void
493  {
494  if (empty($a_title)) {
495  $a_title = "NO TITLE";
496  }
497 
498  $query = "INSERT INTO object_translation " . "(obj_id,title,description,lang_code,lang_default) " . "VALUES " . "("
499  . $this->ilDb->quote($this->getId(), 'integer') . "," . $this->ilDb->quote(
500  $a_title,
501  'text'
502  ) . "," . $this->ilDb->quote($a_desc, 'text') . ","
503  . $this->ilDb->quote($a_lang, 'text') . "," . $this->ilDb->quote($a_lang_default, 'integer') . ")";
504  $this->ilDb->manipulate($query);
505  }
506 
510  public function updateTranslation(string $title, string $desc, string $lang, string $lang_default): void
511  {
512  if (empty($title)) {
513  $a_title = "NO TITLE";
514  }
515 
516  $query = "UPDATE object_translation SET ";
517 
518  $query .= " title = " . $this->ilDb->quote($title, 'text');
519 
520  if ($desc !== "") {
521  $query .= ", description = " . $this->ilDb->quote($desc, 'text') . " ";
522  }
523 
524  $query .= ", lang_default = " . $this->ilDb->quote($lang_default, 'integer') . " ";
525 
526  $query .= " WHERE obj_id = " . $this->ilDb->quote(
527  $this->getId(),
528  'integer'
529  ) . " AND lang_code = " . $this->ilDb->quote($lang, 'text');
530  $this->ilDb->manipulate($query);
531  }
532 
533  public function writePath(): void
534  {
535  if ($this->getRefId()) {
536  ilOrgUnitPathStorage::writePathByRefId($this->getRefId());
537  } else {
538  throw new \LogicException('No ref_id on OrgU with id ' . $this->getId(), 1);
539  }
540  }
541 }
static getInstancesForObjectId(int $a_obj_id, ?string $a_obj_type=null, string $a_sub_type="-", int $a_sub_id=0)
string $title
$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...
ilOrgUnitPositionDBRepository $positionRepo
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.
Class ilOrgUnitPathStorage.
__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 loadRootOrgRefIdAndId()
$path
Definition: ltiservices.php:32
ilRbacReview $rbacreview
ilDBInterface $ilDb
global $DIC
Definition: feed.php:28
__construct(VocabulariesInterface $vocabularies)
ilOrgUnitUserAssignmentDBRepository $assignmentRepo
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.
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...
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.
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
$q
Definition: shib_logout.php:21
$dic
Definition: result.php:32
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.
$r