ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilObjOrgUnit.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 require_once "./Services/Container/classes/class.ilContainer.php";
4 require_once("./Modules/OrgUnit/classes/class.ilOrgUnitImporter.php");
5 require_once('./Modules/OrgUnit/classes/Types/class.ilOrgUnitType.php');
6 require_once('./Services/AdvancedMetaData/classes/class.ilAdvancedMDValues.php');
7 require_once('./Services/AdvancedMetaData/classes/class.ilAdvancedMDRecord.php');
8 require_once('./Modules/OrgUnit/classes/PathStorage/class.ilOrgUnitPathStorage.php');
9 
21 class ilObjOrgUnit extends ilContainer {
22 
23  const TABLE_NAME = 'orgu_data';
27  protected static $root_ref_id;
31  protected static $root_id;
35  protected $employee_role;
39  protected $superior_role;
45  protected static $icons_cache;
51  protected $orgu_type_id = 0;
57  protected $amd_data;
58 
59 
64  public function __construct($a_id = 0, $a_call_by_reference = true) {
65  $this->type = "orgu";
66  parent::__construct($a_id, $a_call_by_reference);
67  }
68 
69 
70  public function read() {
71  global $DIC;
72  $ilDB = $DIC['ilDB'];
73  parent::read();
75  $sql = 'SELECT * FROM ' . self::TABLE_NAME . ' WHERE orgu_id = '
76  . $ilDB->quote($this->getId(), 'integer');
77  $set = $ilDB->query($sql);
78  if ($ilDB->numRows($set)) {
79  $rec = $ilDB->fetchObject($set);
80  $this->setOrgUnitTypeId($rec->orgu_type_id);
81  }
82  }
83 
84 
85  public function create() {
86  global $DIC;
87  $ilDB = $DIC['ilDB'];
88  parent::create();
89  $ilDB->insert(self::TABLE_NAME, array(
90  'orgu_type_id' => array( 'integer', $this->getOrgUnitTypeId() ),
91  'orgu_id' => array( 'integer', $this->getId() ),
92  ));
93  }
94 
95 
96  public function update() {
97  global $DIC;
98  $ilDB = $DIC['ilDB'];
99  parent::update();
100  $sql = 'SELECT * FROM ' . self::TABLE_NAME . ' WHERE orgu_id = '
101  . $ilDB->quote($this->getId(), 'integer');
102  $set = $ilDB->query($sql);
103  if ($ilDB->numRows($set)) {
104  $ilDB->update(self::TABLE_NAME, array(
105  'orgu_type_id' => array( 'integer', $this->getOrgUnitTypeId() ),
106  ), array(
107  'orgu_id' => array( 'integer', $this->getId() ),
108  ));
109  } else {
110  $ilDB->insert(self::TABLE_NAME, array(
111  'orgu_type_id' => array( 'integer', $this->getOrgUnitTypeId() ),
112  'orgu_id' => array( 'integer', $this->getId() ),
113  ));
114  }
115  // Update selection for advanced meta data of the type
116  if ($this->getOrgUnitTypeId()) {
117  ilAdvancedMDRecord::saveObjRecSelection($this->getId(), 'orgu_type', $this->getOrgUnitType()
118  ->getAssignedAdvancedMDRecordIds());
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 
125 
129  public function getOrgUnitTypeId() {
130  return $this->orgu_type_id;
131  }
132 
133 
137  public function getOrgUnitType() {
139  }
140 
141 
145  public function setOrgUnitTypeId($a_id) {
146  $this->orgu_type_id = $a_id;
147  }
148 
149 
160  public function getAdvancedMDValues($a_record_id = 0) {
161  if (!$this->getOrgUnitTypeId()) {
162  return array();
163  }
164  // Serve from cache?
165  if (is_array($this->amd_data)) {
166  if ($a_record_id) {
167  return (isset($this->amd_data[$a_record_id])) ? $this->amd_data[$a_record_id] : array();
168  } else {
169  return $this->amd_data;
170  }
171  }
173  foreach (ilAdvancedMDValues::getInstancesForObjectId($this->getId(), 'orgu') as $record_id => $amd_values) {
174  $amd_values = new ilAdvancedMDValues($record_id, $this->getId(), 'orgu_type', $this->getOrgUnitTypeId());
175  $amd_values->read();
176  $this->amd_data[$record_id] = $amd_values->getADTGroup()->getElements();
177  }
178  if ($a_record_id) {
179  return (isset($this->amd_data[$a_record_id])) ? $this->amd_data[$a_record_id] : array();
180  } else {
181  return $this->amd_data;
182  }
183  }
184 
185 
193  public static function getIconsCache() {
194  if (is_array(self::$icons_cache)) {
195  return self::$icons_cache;
196  }
197  global $DIC;
198  $ilDB = $DIC['ilDB'];
200  $sql = 'SELECT orgu_id, ot.id AS type_id FROM orgu_data
201  INNER JOIN orgu_types AS ot ON (ot.id = orgu_data.orgu_type_id)
202  WHERE ot.icon IS NOT NULL';
203  $set = $ilDB->query($sql);
204  $icons_cache = array();
205  while ($row = $ilDB->fetchObject($set)) {
207  if ($type && is_file($type->getIconPath(true))) {
208  $icons_cache[$row->orgu_id] = $type->getIconPath(true);
209  }
210  }
211  self::$icons_cache = $icons_cache;
212 
213  return $icons_cache;
214  }
215 
216 
220  public static function getRootOrgRefId() {
221  self::loadRootOrgRefIdAndId();
222 
223  return self::$root_ref_id;
224  }
225 
226 
230  public static function getRootOrgId() {
231  self::loadRootOrgRefIdAndId();
232 
233  return self::$root_id;
234  }
235 
236 
237  private static function loadRootOrgRefIdAndId() {
238  if (self::$root_ref_id === null || self::$root_id === null) {
239  global $DIC;
240  $ilDB = $DIC['ilDB'];
241  $q = "SELECT o.obj_id, r.ref_id FROM object_data o
242  INNER JOIN object_reference r ON r.obj_id = o.obj_id
243  WHERE title = " . $ilDB->quote('__OrgUnitAdministration', 'text') . "";
244  $set = $ilDB->query($q);
245  $res = $ilDB->fetchAssoc($set);
246  self::$root_id = $res["obj_id"];
247  self::$root_ref_id = $res["ref_id"];
248  }
249  }
250 
251 
252  private function loadRoles() {
253  global $DIC;
254  $ilLog = $DIC['ilLog'];
255  if (!$this->employee_role || !$this->superior_role) {
256  $this->doLoadRoles();
257  }
258 
259  if (!$this->employee_role || !$this->superior_role) {
260  $this->initDefaultRoles();
261  $this->doLoadRoles();
262  if (!$this->employee_role || !$this->superior_role) {
263  throw new Exception("The standard roles the orgu object with id: " . $this->getId()
264  . " aren't initialized or have been deleted, newly creating them didn't work!");
265  } else {
266  $ilLog->write("[" . __FILE__ . ":" . __LINE__
267  . "] The standard roles for the orgu obj with id: " . $this->getId()
268  . " were newly created as they couldnt be found.");
269  }
270  }
271  }
272 
273 
274  private function doLoadRoles() {
275  global $DIC;
276  $ilDB = $DIC['ilDB'];
277  if (!$this->employee_role || !$this->superior_role) {
278  $q = "SELECT obj_id, title FROM object_data WHERE title = 'il_orgu_employee_"
279  . $ilDB->quote($this->getRefId(), "integer") . "' OR title = 'il_orgu_superior_"
280  . $ilDB->quote($this->getRefId(), "integer") . "'";
281  $set = $ilDB->query($q);
282  while ($res = $ilDB->fetchAssoc($set)) {
283  if ($res["title"] == "il_orgu_employee_" . $this->getRefId()) {
284  $this->employee_role = $res["obj_id"];
285  } elseif ($res["title"] == "il_orgu_superior_" . $this->getRefId()) {
286  $this->superior_role = $res["obj_id"];
287  }
288  }
289 
290  if (!$this->employee_role || !$this->superior_role) {
291  throw new Exception("The standard roles the orgu object with id: " . $this->getId()
292  . " aren't initialized or have been deleted!");
293  }
294  }
295  }
296 
297 
298  public function assignUsersToEmployeeRole($user_ids) {
299  global $DIC;
300  $rbacadmin = $DIC['rbacadmin'];
301  $ilAppEventHandler = $DIC['ilAppEventHandler'];
302  foreach ($user_ids as $user_id) {
303  $rbacadmin->assignUser($this->getEmployeeRole(), $user_id);
304 
305  $ilAppEventHandler->raise('Modules/OrgUnit', 'assignUsersToEmployeeRole', array(
306  'object' => $this,
307  'obj_id' => $this->getId(),
308  'ref_id' => $this->getRefId(),
309  'role_id' => $this->getEmployeeRole(),
310  'user_id' => $user_id,
311  ));
312  }
313  }
314 
315 
316  public function assignUsersToSuperiorRole($user_ids) {
317  global $DIC;
318  $rbacadmin = $DIC['rbacadmin'];
319  $ilAppEventHandler = $DIC['ilAppEventHandler'];
320  foreach ($user_ids as $user_id) {
321  $rbacadmin->assignUser($this->getSuperiorRole(), $user_id);
322 
323  $ilAppEventHandler->raise('Modules/OrgUnit', 'assignUsersToSuperiorRole', array(
324  'object' => $this,
325  'obj_id' => $this->getId(),
326  'ref_id' => $this->getRefId(),
327  'role_id' => $this->getSuperiorRole(),
328  'user_id' => $user_id,
329  ));
330  }
331  }
332 
333 
334  public function deassignUserFromEmployeeRole($user_id) {
335  global $DIC;
336  $rbacadmin = $DIC['rbacadmin'];
337  $ilAppEventHandler = $DIC['ilAppEventHandler'];
338  $rbacadmin->deassignUser($this->getEmployeeRole(), $user_id);
339 
340  $ilAppEventHandler->raise('Modules/OrgUnit', 'deassignUserFromEmployeeRole', array(
341  'object' => $this,
342  'obj_id' => $this->getId(),
343  'ref_id' => $this->getRefId(),
344  'role_id' => $this->getEmployeeRole(),
345  'user_id' => $user_id,
346  ));
347  }
348 
349 
350  public function deassignUserFromSuperiorRole($user_id) {
351  global $DIC;
352  $rbacadmin = $DIC['rbacadmin'];
353  $ilAppEventHandler = $DIC['ilAppEventHandler'];
354  $rbacadmin->deassignUser($this->getSuperiorRole(), $user_id);
355 
356  $ilAppEventHandler->raise('Modules/OrgUnit', 'deassignUserFromSuperiorRole', array(
357  'object' => $this,
358  'obj_id' => $this->getId(),
359  'ref_id' => $this->getRefId(),
360  'role_id' => $this->getSuperiorRole(),
361  'user_id' => $user_id,
362  ));
363  }
364 
365 
373  public function assignUserToLocalRole($role_id, $user_id) {
374  global $DIC;
375  $rbacreview = $DIC['rbacreview'];
376  $rbacadmin = $DIC['rbacadmin'];
377  $ilAppEventHandler = $DIC['ilAppEventHandler'];
378 
379  $arrLocalRoles = $rbacreview->getLocalRoles($this->getRefId());
380  if (!in_array($role_id, $arrLocalRoles)) {
381  return false;
382  }
383 
384  $return = $rbacadmin->assignUser($role_id, $user_id);
385 
386  $ilAppEventHandler->raise('Modules/OrgUnit', 'assignUserToLocalRole', array(
387  'object' => $this,
388  'obj_id' => $this->getId(),
389  'ref_id' => $this->getRefId(),
390  'role_id' => $role_id,
391  'user_id' => $user_id,
392  ));
393 
394  return $return;
395  }
396 
397 
405  public function deassignUserFromLocalRole($role_id, $user_id) {
406  global $DIC;
407  $rbacreview = $DIC['rbacreview'];
408  $rbacadmin = $DIC['rbacadmin'];
409  $ilAppEventHandler = $DIC['ilAppEventHandler'];
410 
411  $arrLocalRoles = $rbacreview->getLocalRoles($this->getRefId());
412  if (!in_array($role_id, $arrLocalRoles)) {
413  return false;
414  }
415 
416  $return = $rbacadmin->deassignUser($role_id, $user_id);
417 
418  $ilAppEventHandler->raise('Modules/OrgUnit', 'deassignUserFromLocalRole', array(
419  'object' => $this,
420  'obj_id' => $this->getId(),
421  'ref_id' => $this->getRefId(),
422  'role_id' => $role_id,
423  'user_id' => $user_id,
424  ));
425 
426  return $return;
427  }
428 
429 
433  public function setEmployeeRole($employee_role) {
434  $this->employee_role = $employee_role;
435  }
436 
437 
444  public static function _exists($a_id, $a_reference = false, $type = "orgu") {
445  return parent::_exists($a_id, $a_reference, "orgu");
446  }
447 
448 
452  public function getEmployeeRole() {
453  $this->loadRoles();
454 
455  return $this->employee_role;
456  }
457 
458 
462  public function setSuperiorRole($superior_role) {
463  $this->superior_role = $superior_role;
464  }
465 
466 
470  public function getSuperiorRole() {
471  $this->loadRoles();
472 
473  return $this->superior_role;
474  }
475 
476 
477  public function initDefaultRoles() {
478  global $DIC;
479  $ilAppEventHandler = $DIC['ilAppEventHandler'];
480 
481  include_once './Services/AccessControl/classes/class.ilObjRole.php';
482  $role_emp = ilObjRole::createDefaultRole('il_orgu_employee_'
483  . $this->getRefId(), "Emplyee of org unit obj_no."
484  . $this->getId(), 'il_orgu_employee', $this->getRefId());
485 
486  $role_sup = ilObjRole::createDefaultRole('il_orgu_superior_'
487  . $this->getRefId(), "Superior of org unit obj_no."
488  . $this->getId(), 'il_orgu_superior', $this->getRefId());
489 
490  $ilAppEventHandler->raise('Modules/OrgUnit', 'initDefaultRoles', array(
491  'object' => $this,
492  'obj_id' => $this->getId(),
493  'ref_id' => $this->getRefId(),
494  'role_superior_id' => $role_sup->getId(),
495  'role_employee_id' => $role_emp->getId(),
496  ));
497  }
498 
499 
505  public function getTitle() {
506  if (parent::getTitle() != "__OrgUnitAdministration") {
507  return parent::getTitle();
508  } else {
509  return $this->lng->txt("objs_orgu");
510  }
511  }
512 
513 
520  public function getLongDescription() {
521  if (parent::getTitle() == "__OrgUnitAdministration") {
522  return $this->lng->txt("obj_orgu_description");
523  } else {
524  return parent::getLongDescription();
525  }
526  }
527 
528 
532  public function getTranslations() {
533  global $DIC;
534  $lng = $DIC['lng'];
535  $ilDB = $DIC['ilDB'];
536 
537  $translations = array();
538 
539  $q = "SELECT * FROM object_translation WHERE obj_id = "
540  . $ilDB->quote($this->getId(), 'integer') . " ORDER BY lang_default DESC";
541  $r = $this->ilias->db->query($q);
542 
543  $num = 0;
544 
545  while ($row = $r->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
546  $data["Fobject"][$num] = array(
547  "title" => $row->title,
548  "desc" => $row->description,
549  "lang" => $row->lang_code,
550  'lang_default' => $row->lang_default,
551  );
552  $num ++;
553  }
554 
555  $translations = $data;
556 
557  if (!count($translations["Fobject"])) {
558  $this->addTranslation($this->getTitle(), "", $lng->getDefaultLanguage(), true);
559  $translations["Fobject"][] = array(
560  "title" => $this->getTitle(),
561  "desc" => "",
562  "lang" => $lng->getDefaultLanguage(),
563  );
564  }
565 
566  return $translations;
567  }
568 
569 
577  public function delete() {
578  global $DIC;
579  $ilDB = $DIC['ilDB'];
580  $ilAppEventHandler = $DIC['ilAppEventHandler'];
581 
582  // always call parent delete function first!!
583  if (!parent::delete()) {
584  return false;
585  }
586 
587  // put here category specific stuff
588  include_once('./Services/User/classes/class.ilObjUserFolder.php');
590 
591  $query = "DELETE FROM object_translation WHERE obj_id = "
592  . $ilDB->quote($this->getId(), 'integer');
593  $res = $ilDB->manipulate($query);
594 
595  $ilAppEventHandler->raise('Modules/OrgUnit', 'delete', array(
596  'object' => $this,
597  'obj_id' => $this->getId(),
598  ));
599 
600  $sql = 'DELETE FROM ' . self::TABLE_NAME . ' WHERE orgu_id = '
601  . $ilDB->quote($this->getId(), 'integer');
602  $ilDB->manipulate($sql);
603 
604  $path = ilOrgUnitPathStorage::find($this->getRefId());
605  if ($path instanceof ilOrgUnitPathStorage) {
606  $path->delete();
607  }
608 
609  if (!$this->employee_role || !$this->superior_role) {
610  $this->doLoadRoles();
611  }
612  $emp = new ilObjRole($this->employee_role);
613  $emp->setParent($this->getRefId());
614  $emp->delete();
615  $sup = new ilObjRole($this->superior_role);
616  $sup->setParent($this->getRefId());
617  $sup->delete();
618 
619  return true;
620  }
621 
622 
626  public function removeTranslations() {
627  global $DIC;
628  $ilDB = $DIC['ilDB'];
629 
630  $query = "DELETE FROM object_translation WHERE obj_id= "
631  . $ilDB->quote($this->getId(), 'integer');
632  $res = $ilDB->manipulate($query);
633  }
634 
635 
641  public function deleteTranslation($a_lang) {
642  global $DIC;
643  $ilDB = $DIC['ilDB'];
644 
645  $query = "DELETE FROM object_translation WHERE obj_id= "
646  . $ilDB->quote($this->getId(), 'integer') . " AND lang_code = "
647  . $ilDB->quote($a_lang, 'text');
648  $res = $ilDB->manipulate($query);
649  }
650 
651 
661  public function addTranslation($a_title, $a_desc, $a_lang, $a_lang_default) {
662  global $DIC;
663  $ilDB = $DIC['ilDB'];
664 
665  if (empty($a_title)) {
666  $a_title = "NO TITLE";
667  }
668 
669  $query = "INSERT INTO object_translation "
670  . "(obj_id,title,description,lang_code,lang_default) " . "VALUES " . "("
671  . $ilDB->quote($this->getId(), 'integer') . "," . $ilDB->quote($a_title, 'text')
672  . "," . $ilDB->quote($a_desc, 'text') . "," . $ilDB->quote($a_lang, 'text') . ","
673  . $ilDB->quote($a_lang_default, 'integer') . ")";
674  $res = $ilDB->manipulate($query);
675 
676  return true;
677  }
678 
679 
689  public function updateTranslation($a_title, $a_desc, $a_lang, $a_lang_default) {
690  global $DIC;
691  $ilDB = $DIC['ilDB'];
692  $ilLog = $DIC['ilLog'];
693 
694  if (empty($a_title)) {
695  $a_title = "NO TITLE";
696  }
697 
698  $query = "UPDATE object_translation SET ";
699 
700  $query .= " title = " . $ilDB->quote($a_title, 'text');
701 
702  if ($a_desc != "") {
703  $query .= ", description = " . $ilDB->quote($a_desc, 'text') . " ";
704  }
705 
706  if ($a_lang_default) {
707  $query .= ", lang_default = " . $ilDB->quote($a_lang_default, 'integer') . " ";
708  }
709 
710  $query .= " WHERE obj_id = " . $ilDB->quote($this->getId(), 'integer') . " AND lang_code = "
711  . $ilDB->quote($a_lang, 'text');
712  $res = $ilDB->manipulate($query);
713 
714  return true;
715  }
716 
717 
718  public function writePath() {
719  if ($this->getRefId()) {
720  ilOrgUnitPathStorage::writePathByRefId($this->getRefId());
721  }
722  }
723 }
Class ilObjRole.
static _updateUserFolderAssignment($a_old_id, $a_new_id)
Update user folder assignment Typically called after deleting a category with local user accounts...
static getInstancesForObjectId($a_obj_id, $a_obj_type=null, $a_sub_type="-", $a_sub_id=0)
Get instances for given object id.
$path
Definition: aliased.php:25
removeTranslations()
remove all Translations of current OrgUnit
__construct($a_id=0, $a_call_by_reference=true)
deassignUserFromSuperiorRole($user_id)
assignUsersToSuperiorRole($user_ids)
static _exists($a_id, $a_reference=false, $type="orgu")
Class ilOrgUnitPathStorage.
assignUsersToEmployeeRole($user_ids)
updateTranslation($a_title, $a_desc, $a_lang, $a_lang_default)
update a translation to current OrgUnit
deassignUserFromLocalRole($role_id, $user_id)
Deassign a given user to a given local role.
static saveObjRecSelection($a_obj_id, $a_sub_type="", array $a_records=null, $a_delete_before=true)
Save repository object record selection.
static loadRootOrgRefIdAndId()
static createDefaultRole($a_title, $a_description, $a_tpl_name, $a_ref_id)
setEmployeeRole($employee_role)
$r
Definition: example_031.php:79
addTranslation($a_title, $a_desc, $a_lang, $a_lang_default)
add a new translation to current OrgUnit
getId()
get object id public
deleteTranslation($a_lang)
remove translations of current OrgUnit
setSuperiorRole($superior_role)
getTitle()
Return title.
getLongDescription()
get object long description (stored in object_description)
Class ilObjOrgUnit.
static getInstance($a_id)
Public.
redirection script todo: (a better solution should control the processing via a xml file) ...
Class ilContainer.
assignUserToLocalRole($role_id, $user_id)
Assign a given user to a given local role.
deassignUserFromEmployeeRole($user_id)
Create styles array
The data for the language used.
static getRootOrgRefId()
global $ilDB
getRefId()
get reference id public
const USER_FOLDER_ID
Class ilObjUserFolder.
global $DIC