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