ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 
20 class ilObjOrgUnit extends ilContainer {
21 
22  const TABLE_NAME = 'orgu_data';
23  protected static $root_ref_id;
24  protected static $root_id;
25  protected $employee_role;
26  protected $superior_role;
32  protected static $icons_cache;
38  protected $orgu_type_id = 0;
44  protected $amd_data;
45 
46 
51  public function __construct($a_id = 0, $a_call_by_reference = true) {
52  $this->type = "orgu";
53  $this->ilContainer($a_id, $a_call_by_reference);
54  }
55 
56 
57  public function read() {
58  global $ilDB;
59  parent::read();
61  $sql = 'SELECT * FROM ' . self::TABLE_NAME . ' WHERE orgu_id = ' . $ilDB->quote($this->getId(), 'integer');
62  $set = $ilDB->query($sql);
63  if ($ilDB->numRows($set)) {
64  $rec = $ilDB->fetchObject($set);
65  $this->setOrgUnitTypeId($rec->orgu_type_id);
66  }
67  }
68 
69 
70  public function create() {
71  global $ilDB;
73  $ilDB->insert(self::TABLE_NAME, array(
74  'orgu_type_id' => array( 'integer', $this->getOrgUnitTypeId() ),
75  'orgu_id' => array( 'integer', $this->getId() ),
76  ));
77  }
78 
79 
80  public function update() {
81  global $ilDB;
83  $sql = 'SELECT * FROM ' . self::TABLE_NAME . ' WHERE orgu_id = ' . $ilDB->quote($this->getId(), 'integer');
84  $set = $ilDB->query($sql);
85  if ($ilDB->numRows($set)) {
86  $ilDB->update(self::TABLE_NAME, array(
87  'orgu_type_id' => array( 'integer', $this->getOrgUnitTypeId() ),
88  ), array(
89  'orgu_id' => array( 'integer', $this->getId() ),
90  ));
91  } else {
92  $ilDB->insert(self::TABLE_NAME, array(
93  'orgu_type_id' => array( 'integer', $this->getOrgUnitTypeId() ),
94  'orgu_id' => array( 'integer', $this->getId() ),
95  ));
96  }
97  // Update selection for advanced meta data of the type
98  if ($this->getOrgUnitTypeId()) {
99  ilAdvancedMDRecord::saveObjRecSelection($this->getId(), 'orgu_type', $this->getOrgUnitType()->getAssignedAdvancedMDRecordIds());
100  } else {
101  // If no type is assigned, delete relations by passing an empty array
102  ilAdvancedMDRecord::saveObjRecSelection($this->getId(), 'orgu_type', array());
103  }
104  }
105 
106 
110  public function getOrgUnitTypeId() {
111  return $this->orgu_type_id;
112  }
113 
114 
118  public function getOrgUnitType() {
120  }
121 
122 
126  public function setOrgUnitTypeId($a_id) {
127  $this->orgu_type_id = $a_id;
128  }
129 
130 
140  public function getAdvancedMDValues($a_record_id = 0) {
141  if (!$this->getOrgUnitTypeId()) {
142  return array();
143  }
144  // Serve from cache?
145  if (is_array($this->amd_data)) {
146  if ($a_record_id) {
147  return (isset($this->amd_data[$a_record_id])) ? $this->amd_data[$a_record_id] : array();
148  } else {
149  return $this->amd_data;
150  }
151  }
153  foreach (ilAdvancedMDValues::getInstancesForObjectId($this->getId(), 'orgu') as $record_id => $amd_values) {
154  $amd_values = new ilAdvancedMDValues($record_id, $this->getId(), 'orgu_type', $this->getOrgUnitTypeId());
155  $amd_values->read();
156  $this->amd_data[$record_id] = $amd_values->getADTGroup()->getElements();
157  }
158  if ($a_record_id) {
159  return (isset($this->amd_data[$a_record_id])) ? $this->amd_data[$a_record_id] : array();
160  } else {
161  return $this->amd_data;
162  }
163  }
164 
165 
173  public static function getIconsCache() {
174  if (is_array(self::$icons_cache)) {
175  return self::$icons_cache;
176  }
177  global $ilDB;
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)) {
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 
196  public static function getRootOrgRefId() {
198 
199  return self::$root_ref_id;
200  }
201 
202 
203  public static function getRootOrgId() {
205 
206  return self::$root_id;
207  }
208 
209 
210  private static function loadRootOrgRefIdAndId() {
211  if (self::$root_ref_id === NULL || self::$root_id === NULL) {
212  global $ilDB;
213  $q = "SELECT o.obj_id, r.ref_id FROM object_data o
214  INNER JOIN object_reference r ON r.obj_id = o.obj_id
215  WHERE title = " . $ilDB->quote('__OrgUnitAdministration', 'text') . "";
216  $set = $ilDB->query($q);
217  $res = $ilDB->fetchAssoc($set);
218  self::$root_id = $res["obj_id"];
219  self::$root_ref_id = $res["ref_id"];
220  }
221  }
222 
223 
224  private function loadRoles() {
225  global $ilLog;
226  if (!$this->employee_role || !$this->superior_role) {
227  $this->doLoadRoles();
228  }
229 
230  if (!$this->employee_role || !$this->superior_role) {
231  $this->initDefaultRoles();
232  $this->doLoadRoles();
233  if (!$this->employee_role || !$this->superior_role) {
234  throw new Exception("The standard roles the orgu object with id: " . $this->getId()
235  . " aren't initialized or have been deleted, newly creating them didn't work!");
236  } else {
237  $ilLog->write("[" . __FILE__ . ":" . __LINE__ . "] The standard roles for the orgu obj with id: " . $this->getId()
238  . " were newly created as they couldnt be found.");
239  }
240  }
241  }
242 
243 
244  private function doLoadRoles() {
245  global $ilDB;
246  if (!$this->employee_role || !$this->superior_role) {
247  $q = "SELECT obj_id, title FROM object_data WHERE title LIKE 'il_orgu_employee_" . $ilDB->quote($this->getRefId(), "integer")
248  . "' OR title LIKE 'il_orgu_superior_" . $ilDB->quote($this->getRefId(), "integer") . "'";
249  $set = $ilDB->query($q);
250  while ($res = $ilDB->fetchAssoc($set)) {
251  if ($res["title"] == "il_orgu_employee_" . $this->getRefId()) {
252  $this->employee_role = $res["obj_id"];
253  } elseif ($res["title"] == "il_orgu_superior_" . $this->getRefId()) {
254  $this->superior_role = $res["obj_id"];
255  }
256  }
257 
258  if (!$this->employee_role || !$this->superior_role) {
259  throw new Exception("The standard roles the orgu object with id: " . $this->getId() . " aren't initialized or have been deleted!");
260  }
261  }
262  }
263 
264 
265  public function assignUsersToEmployeeRole($user_ids) {
266  global $rbacadmin, $ilAppEventHandler;
267  foreach ($user_ids as $user_id) {
268  $rbacadmin->assignUser($this->getEmployeeRole(), $user_id);
269 
270  $ilAppEventHandler->raise('Modules/OrgUnit', 'assignUsersToEmployeeRole', array(
271  'object' => $this,
272  'obj_id' => $this->getId(),
273  'ref_id' => $this->getRefId(),
274  'role_id' => $this->getEmployeeRole(),
275  'user_id' => $user_id
276  ));
277  }
278  }
279 
280 
281  public function assignUsersToSuperiorRole($user_ids) {
282  global $rbacadmin, $ilAppEventHandler;
283  foreach ($user_ids as $user_id) {
284  $rbacadmin->assignUser($this->getSuperiorRole(), $user_id);
285 
286  $ilAppEventHandler->raise('Modules/OrgUnit', 'assignUsersToSuperiorRole', array(
287  'object' => $this,
288  'obj_id' => $this->getId(),
289  'ref_id' => $this->getRefId(),
290  'role_id' => $this->getSuperiorRole(),
291  'user_id' => $user_id
292  ));
293  }
294  }
295 
296 
297  public function deassignUserFromEmployeeRole($user_id) {
298  global $rbacadmin, $ilAppEventHandler;
299  $rbacadmin->deassignUser($this->getEmployeeRole(), $user_id);
300 
301  $ilAppEventHandler->raise('Modules/OrgUnit', 'deassignUserFromEmployeeRole', array(
302  'object' => $this,
303  'obj_id' => $this->getId(),
304  'ref_id' => $this->getRefId(),
305  'role_id' => $this->getEmployeeRole(),
306  'user_id' => $user_id
307  ));
308  }
309 
310 
311  public function deassignUserFromSuperiorRole($user_id) {
312  global $rbacadmin, $ilAppEventHandler;
313  $rbacadmin->deassignUser($this->getSuperiorRole(), $user_id);
314 
315  $ilAppEventHandler->raise('Modules/OrgUnit', 'deassignUserFromSuperiorRole', array(
316  'object' => $this,
317  'obj_id' => $this->getId(),
318  'ref_id' => $this->getRefId(),
319  'role_id' => $this->getSuperiorRole(),
320  'user_id' => $user_id
321  ));
322  }
323 
324 
332  public function assignUserToLocalRole($role_id, $user_id)
333  {
334  global $rbacreview, $rbacadmin, $ilAppEventHandler;
335 
336  $arrLocalRoles = $rbacreview->getLocalRoles($this->getRefId());
337  if ( ! in_array($role_id, $arrLocalRoles)) {
338  return false;
339  }
340 
341  $return = $rbacadmin->assignUser($role_id, $user_id);
342 
343  $ilAppEventHandler->raise('Modules/OrgUnit',
344  'assignUserToLocalRole',
345  array('object' => $this,
346  'obj_id' => $this->getId(),
347  'ref_id' => $this->getRefId(),
348  'role_id' => $role_id,
349  'user_id' => $user_id));
350 
351  return $return;
352  }
353 
354 
362  public function deassignUserFromLocalRole($role_id, $user_id)
363  {
364  global $rbacreview, $rbacadmin, $ilAppEventHandler;
365 
366  $arrLocalRoles = $rbacreview->getLocalRoles($this->getRefId());
367  if ( ! in_array($role_id, $arrLocalRoles)) {
368  return false;
369  }
370 
371  $return = $rbacadmin->deassignUser($role_id, $user_id);
372 
373  $ilAppEventHandler->raise('Modules/OrgUnit',
374  'deassignUserFromLocalRole',
375  array('object' => $this,
376  'obj_id' => $this->getId(),
377  'ref_id' => $this->getRefId(),
378  'role_id' => $role_id,
379  'user_id' => $user_id));
380 
381  return $return;
382  }
383 
387  public function setEmployeeRole($employee_role) {
388  $this->employee_role = $employee_role;
389  }
390 
391 
392  public static function _exists($a_id, $a_reference = false) {
393  return parent::_exists($a_id, $a_reference, "orgu");
394  }
395 
396 
400  public function getEmployeeRole() {
401  $this->loadRoles();
402 
403  return $this->employee_role;
404  }
405 
406 
410  public function setSuperiorRole($superior_role) {
411  $this->superior_role = $superior_role;
412  }
413 
414 
418  public function getSuperiorRole() {
419  $this->loadRoles();
420 
421  return $this->superior_role;
422  }
423 
424 
425  public function initDefaultRoles() {
426  global $rbacadmin, $rbacreview, $ilAppEventHandler;
427  include_once './Services/AccessControl/classes/class.ilObjRole.php';
428  $role = new ilObjRole();
429  $role->setTitle("il_orgu_employee_" . $this->getRefId());
430  $role->setDescription("Emplyee of org unit obj_no." . $this->getId());
431  $role->create();
432 
433  $GLOBALS['rbacadmin']->assignRoleToFolder($role->getId(), $this->getRefId(), 'y');
434 
435  include_once './Services/AccessControl/classes/class.ilObjRole.php';
436  $role_sup = ilObjRole::createDefaultRole('il_orgu_superior_' . $this->getRefId(), "Superior of org unit obj_no."
437  . $this->getId(), 'il_orgu_superior', $this->getRefId());
438 
439  $ilAppEventHandler->raise('Modules/OrgUnit', 'initDefaultRoles', array(
440  'object' => $this,
441  'obj_id' => $this->getId(),
442  'ref_id' => $this->getRefId(),
443  'role_superior_id' => $role->getId(),
444  'role_employee_id' => $role_sup->getId()
445  ));
446  }
447 
448 
453  public function getTitle() {
454  if (parent::getTitle() != "__OrgUnitAdministration") {
455  return parent::getTitle();
456  } else {
457  return $this->lng->txt("objs_orgu");
458  }
459  }
460 
461 
468  public function getLongDescription() {
469  if (parent::getTitle() == "__OrgUnitAdministration") {
470  return $this->lng->txt("obj_orgu_description");
471  } else {
473  }
474  }
475 
476 
480  public function getTranslations() {
481  global $lng, $ilDB;
482 
483  $translations = array();
484 
485  $q = "SELECT * FROM object_translation WHERE obj_id = " . $ilDB->quote($this->getId(), 'integer') . " ORDER BY lang_default DESC";
486  $r = $this->ilias->db->query($q);
487 
488  $num = 0;
489 
490  while ($row = $r->fetchRow(DB_FETCHMODE_OBJECT)) {
491  $data["Fobject"][$num] = array(
492  "title" => $row->title,
493  "desc" => $row->description,
494  "lang" => $row->lang_code,
495  'lang_default' => $row->lang_default,
496  );
497  $num ++;
498  }
499 
500  $translations = $data;
501 
502  if (!count($translations["Fobject"])) {
503  $this->addTranslation($this->getTitle(), "", $lng->getDefaultLanguage(), true);
504  $translations["Fobject"][] = array(
505  "title" => $this->getTitle(),
506  "desc" => "",
507  "lang" => $lng->getDefaultLanguage()
508  );
509  }
510 
511  return $translations;
512  }
513 
514 
521  function delete() {
522  global $ilDB, $ilAppEventHandler;
523 
524  // always call parent delete function first!!
525  if (!parent::delete()) {
526  return false;
527  }
528 
529  // put here category specific stuff
530  include_once('./Services/User/classes/class.ilObjUserFolder.php');
532 
533  $query = "DELETE FROM object_translation WHERE obj_id = " . $ilDB->quote($this->getId(), 'integer');
534  $res = $ilDB->manipulate($query);
535 
536  $ilAppEventHandler->raise('Modules/OrgUnit', 'delete', array(
537  'object' => $this,
538  'obj_id' => $this->getId()
539  ));
540 
541  $sql = 'DELETE FROM ' . self::TABLE_NAME . ' WHERE orgu_id = ' . $ilDB->quote($this->getId(), 'integer');
542  $ilDB->manipulate($sql);
543 
544  return true;
545  }
546 
547 
548  // remove all Translations of current OrgUnit
549  function removeTranslations() {
550  global $ilDB;
551 
552  $query = "DELETE FROM object_translation WHERE obj_id= " . $ilDB->quote($this->getId(), 'integer');
553  $res = $ilDB->manipulate($query);
554  }
555 
556 
557  // remove translations of current OrgUnit
558  function deleteTranslation($a_lang) {
559  global $ilDB;
560 
561  $query = "DELETE FROM object_translation WHERE obj_id= " . $ilDB->quote($this->getId(), 'integer') . " AND lang_code = "
562  . $ilDB->quote($a_lang, 'text');
563  $res = $ilDB->manipulate($query);
564  }
565 
566 
567  // add a new translation to current OrgUnit
568  function addTranslation($a_title, $a_desc, $a_lang, $a_lang_default) {
569  global $ilDB;
570 
571  if (empty($a_title)) {
572  $a_title = "NO TITLE";
573  }
574 
575  $query = "INSERT INTO object_translation " . "(obj_id,title,description,lang_code,lang_default) " . "VALUES " . "("
576  . $ilDB->quote($this->getId(), 'integer') . "," . $ilDB->quote($a_title, 'text') . "," . $ilDB->quote($a_desc, 'text') . ","
577  . $ilDB->quote($a_lang, 'text') . "," . $ilDB->quote($a_lang_default, 'integer') . ")";
578  $res = $ilDB->manipulate($query);
579 
580  return true;
581  }
582 
583 
584  // update a translation to current OrgUnit
585  function updateTranslation($a_title, $a_desc, $a_lang, $a_lang_default) {
586  global $ilDB, $ilLog;
587 
588  if (empty($a_title)) {
589  $a_title = "NO TITLE";
590  }
591 
592  $query = "UPDATE object_translation SET ";
593 
594  $query .= " title = " . $ilDB->quote($a_title, 'text');
595 
596  if ($a_desc != "") {
597  $query .= ", description = " . $ilDB->quote($a_desc, 'text') . " ";
598  }
599 
600  if ($a_lang_default) {
601  $query .= ", lang_default = " . $ilDB->quote($a_lang_default, 'integer') . " ";
602  }
603 
604  $query .= " WHERE obj_id = " . $ilDB->quote($this->getId(), 'integer') . " AND lang_code = " . $ilDB->quote($a_lang, 'text');
605  $res = $ilDB->manipulate($query);
606 
607  return true;
608  }
609 }
610 
611 ?>