ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 */
3require_once "./Services/Container/classes/class.ilContainer.php";
4require_once("./Modules/OrgUnit/classes/class.ilOrgUnitImporter.php");
5require_once('./Modules/OrgUnit/classes/Types/class.ilOrgUnitType.php');
6require_once('./Services/AdvancedMetaData/classes/class.ilAdvancedMDValues.php');
7require_once('./Services/AdvancedMetaData/classes/class.ilAdvancedMDRecord.php');
8
19
20 const TABLE_NAME = 'orgu_data';
21 protected static $root_ref_id;
22 protected static $root_id;
23 protected $employee_role;
24 protected $superior_role;
30 protected static $icons_cache;
36 protected $orgu_type_id = 0;
42 protected $amd_data;
43
44
49 public function __construct($a_id = 0, $a_call_by_reference = true) {
50 $this->type = "orgu";
51 $this->ilContainer($a_id, $a_call_by_reference);
52 }
53
54
55 public function read() {
56 global $ilDB;
57 parent::read();
59 $sql = 'SELECT * FROM ' . self::TABLE_NAME . ' WHERE orgu_id = '
60 . $ilDB->quote($this->getId(), 'integer');
61 $set = $ilDB->query($sql);
62 if ($ilDB->numRows($set)) {
63 $rec = $ilDB->fetchObject($set);
64 $this->setOrgUnitTypeId($rec->orgu_type_id);
65 }
66 }
67
68
69 public function create() {
70 global $ilDB;
71 parent::create();
72 $ilDB->insert(self::TABLE_NAME, array(
73 'orgu_type_id' => array( 'integer', $this->getOrgUnitTypeId() ),
74 'orgu_id' => array( 'integer', $this->getId() ),
75 ));
76 }
77
78
79 public function update() {
80 global $ilDB;
81 parent::update();
82 $sql = 'SELECT * FROM ' . self::TABLE_NAME . ' WHERE orgu_id = '
83 . $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()) {
100 ->getAssignedAdvancedMDRecordIds());
101 } else {
102 // If no type is assigned, delete relations by passing an empty array
103 ilAdvancedMDRecord::saveObjRecSelection($this->getId(), 'orgu_type', array());
104 }
105 }
106
107
111 public function getOrgUnitTypeId() {
112 return $this->orgu_type_id;
113 }
114
115
119 public function getOrgUnitType() {
121 }
122
123
127 public function setOrgUnitTypeId($a_id) {
128 $this->orgu_type_id = $a_id;
129 }
130
131
142 public function getAdvancedMDValues($a_record_id = 0) {
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 (isset($this->amd_data[$a_record_id])) ? $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 (isset($this->amd_data[$a_record_id])) ? $this->amd_data[$a_record_id] : array();
162 } else {
163 return $this->amd_data;
164 }
165 }
166
167
175 public static function getIconsCache() {
176 if (is_array(self::$icons_cache)) {
177 return self::$icons_cache;
178 }
179 global $ilDB;
181 $sql = 'SELECT orgu_id, ot.id AS type_id FROM orgu_data
182 INNER JOIN orgu_types AS ot ON (ot.id = orgu_data.orgu_type_id)
183 WHERE ot.icon IS NOT NULL';
184 $set = $ilDB->query($sql);
185 $icons_cache = array();
186 while ($row = $ilDB->fetchObject($set)) {
188 if ($type && is_file($type->getIconPath(true))) {
189 $icons_cache[$row->orgu_id] = $type->getIconPath(true);
190 }
191 }
192 self::$icons_cache = $icons_cache;
193
194 return $icons_cache;
195 }
196
197
198 public static function getRootOrgRefId() {
200
201 return self::$root_ref_id;
202 }
203
204
205 public static function getRootOrgId() {
207
208 return self::$root_id;
209 }
210
211
212 private static function loadRootOrgRefIdAndId() {
213 if (self::$root_ref_id === null || self::$root_id === null) {
214 global $ilDB;
215 $q = "SELECT o.obj_id, r.ref_id FROM object_data o
216 INNER JOIN object_reference r ON r.obj_id = o.obj_id
217 WHERE title = " . $ilDB->quote('__OrgUnitAdministration', 'text') . "";
218 $set = $ilDB->query($q);
219 $res = $ilDB->fetchAssoc($set);
220 self::$root_id = $res["obj_id"];
221 self::$root_ref_id = $res["ref_id"];
222 }
223 }
224
225
226 private function loadRoles() {
227 global $ilLog;
228 if (!$this->employee_role || !$this->superior_role) {
229 $this->doLoadRoles();
230 }
231
232 if (!$this->employee_role || !$this->superior_role) {
233 $this->initDefaultRoles();
234 $this->doLoadRoles();
235 if (!$this->employee_role || !$this->superior_role) {
236 throw new Exception("The standard roles the orgu object with id: " . $this->getId()
237 . " aren't initialized or have been deleted, newly creating them didn't work!");
238 } else {
239 $ilLog->write("[" . __FILE__ . ":" . __LINE__
240 . "] The standard roles for the orgu obj with id: " . $this->getId()
241 . " were newly created as they couldnt be found.");
242 }
243 }
244 }
245
246
247 private function doLoadRoles() {
248 global $ilDB;
249 if (!$this->employee_role || !$this->superior_role) {
250 $q = "SELECT obj_id, title FROM object_data WHERE title LIKE 'il_orgu_employee_"
251 . $ilDB->quote($this->getRefId(), "integer") . "' OR title LIKE 'il_orgu_superior_"
252 . $ilDB->quote($this->getRefId(), "integer") . "'";
253 $set = $ilDB->query($q);
254 while ($res = $ilDB->fetchAssoc($set)) {
255 if ($res["title"] == "il_orgu_employee_" . $this->getRefId()) {
256 $this->employee_role = $res["obj_id"];
257 } elseif ($res["title"] == "il_orgu_superior_" . $this->getRefId()) {
258 $this->superior_role = $res["obj_id"];
259 }
260 }
261
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!");
265 }
266 }
267 }
268
269
270 public function assignUsersToEmployeeRole($user_ids) {
271 global $rbacadmin, $ilAppEventHandler;
272 foreach ($user_ids as $user_id) {
273 $rbacadmin->assignUser($this->getEmployeeRole(), $user_id);
274
275 $ilAppEventHandler->raise('Modules/OrgUnit', 'assignUsersToEmployeeRole', array(
276 'object' => $this,
277 'obj_id' => $this->getId(),
278 'ref_id' => $this->getRefId(),
279 'role_id' => $this->getEmployeeRole(),
280 'user_id' => $user_id,
281 ));
282 }
283 }
284
285
286 public function assignUsersToSuperiorRole($user_ids) {
287 global $rbacadmin, $ilAppEventHandler;
288 foreach ($user_ids as $user_id) {
289 $rbacadmin->assignUser($this->getSuperiorRole(), $user_id);
290
291 $ilAppEventHandler->raise('Modules/OrgUnit', 'assignUsersToSuperiorRole', array(
292 'object' => $this,
293 'obj_id' => $this->getId(),
294 'ref_id' => $this->getRefId(),
295 'role_id' => $this->getSuperiorRole(),
296 'user_id' => $user_id,
297 ));
298 }
299 }
300
301
302 public function deassignUserFromEmployeeRole($user_id) {
303 global $rbacadmin, $ilAppEventHandler;
304 $rbacadmin->deassignUser($this->getEmployeeRole(), $user_id);
305
306 $ilAppEventHandler->raise('Modules/OrgUnit', 'deassignUserFromEmployeeRole', array(
307 'object' => $this,
308 'obj_id' => $this->getId(),
309 'ref_id' => $this->getRefId(),
310 'role_id' => $this->getEmployeeRole(),
311 'user_id' => $user_id,
312 ));
313 }
314
315
316 public function deassignUserFromSuperiorRole($user_id) {
317 global $rbacadmin, $ilAppEventHandler;
318 $rbacadmin->deassignUser($this->getSuperiorRole(), $user_id);
319
320 $ilAppEventHandler->raise('Modules/OrgUnit', 'deassignUserFromSuperiorRole', array(
321 'object' => $this,
322 'obj_id' => $this->getId(),
323 'ref_id' => $this->getRefId(),
324 'role_id' => $this->getSuperiorRole(),
325 'user_id' => $user_id,
326 ));
327 }
328
329
337 public function assignUserToLocalRole($role_id, $user_id) {
338 global $rbacreview, $rbacadmin, $ilAppEventHandler;
339
340 $arrLocalRoles = $rbacreview->getLocalRoles($this->getRefId());
341 if (!in_array($role_id, $arrLocalRoles)) {
342 return false;
343 }
344
345 $return = $rbacadmin->assignUser($role_id, $user_id);
346
347 $ilAppEventHandler->raise('Modules/OrgUnit', 'assignUserToLocalRole', array(
348 'object' => $this,
349 'obj_id' => $this->getId(),
350 'ref_id' => $this->getRefId(),
351 'role_id' => $role_id,
352 'user_id' => $user_id,
353 ));
354
355 return $return;
356 }
357
358
366 public function deassignUserFromLocalRole($role_id, $user_id) {
367 global $rbacreview, $rbacadmin, $ilAppEventHandler;
368
369 $arrLocalRoles = $rbacreview->getLocalRoles($this->getRefId());
370 if (!in_array($role_id, $arrLocalRoles)) {
371 return false;
372 }
373
374 $return = $rbacadmin->deassignUser($role_id, $user_id);
375
376 $ilAppEventHandler->raise('Modules/OrgUnit', 'deassignUserFromLocalRole', array(
377 'object' => $this,
378 'obj_id' => $this->getId(),
379 'ref_id' => $this->getRefId(),
380 'role_id' => $role_id,
381 'user_id' => $user_id,
382 ));
383
384 return $return;
385 }
386
387
392 $this->employee_role = $employee_role;
393 }
394
395
396 public static function _exists($a_id, $a_reference = false) {
397 return parent::_exists($a_id, $a_reference, "orgu");
398 }
399
400
404 public function getEmployeeRole() {
405 $this->loadRoles();
406
408 }
409
410
415 $this->superior_role = $superior_role;
416 }
417
418
422 public function getSuperiorRole() {
423 $this->loadRoles();
424
426 }
427
428
429 public function initDefaultRoles() {
430 global $ilAppEventHandler;
431
432 include_once './Services/AccessControl/classes/class.ilObjRole.php';
433 $role_emp = ilObjRole::createDefaultRole('il_orgu_employee_'
434 . $this->getRefId(), "Emplyee of org unit obj_no."
435 . $this->getId(), 'il_orgu_employee', $this->getRefId());
436
437 $role_sup = ilObjRole::createDefaultRole('il_orgu_superior_'
438 . $this->getRefId(), "Superior of org unit obj_no."
439 . $this->getId(), 'il_orgu_superior', $this->getRefId());
440
441 $ilAppEventHandler->raise('Modules/OrgUnit', 'initDefaultRoles', array(
442 'object' => $this,
443 'obj_id' => $this->getId(),
444 'ref_id' => $this->getRefId(),
445 'role_superior_id' => $role_sup->getId(),
446 'role_employee_id' => $role_emp->getId(),
447 ));
448 }
449
450
456 public function getTitle() {
457 if (parent::getTitle() != "__OrgUnitAdministration") {
458 return parent::getTitle();
459 } else {
460 return $this->lng->txt("objs_orgu");
461 }
462 }
463
464
471 public function getLongDescription() {
472 if (parent::getTitle() == "__OrgUnitAdministration") {
473 return $this->lng->txt("obj_orgu_description");
474 } else {
475 return parent::getLongDescription();
476 }
477 }
478
479
483 public function getTranslations() {
484 global $lng, $ilDB;
485
486 $q = "SELECT * FROM object_translation WHERE obj_id = "
487 . $ilDB->quote($this->getId(), 'integer') . " ORDER BY lang_default DESC";
488 $r = $this->ilias->db->query($q);
489
490 $num = 0;
491 $data = array();
492 while ($row = $r->fetchRow(DB_FETCHMODE_OBJECT)) {
493 $data["Fobject"][$num] = array(
494 "title" => $row->title,
495 "desc" => $row->description,
496 "lang" => $row->lang_code,
497 'lang_default' => $row->lang_default,
498 );
499 $num ++;
500 }
501
502 $translations = $data;
503
504 if (!count($translations["Fobject"])) {
505 $this->addTranslation($this->getTitle(), "", $lng->getDefaultLanguage(), true);
506 $translations["Fobject"][] = array(
507 "title" => $this->getTitle(),
508 "desc" => "",
509 "lang" => $lng->getDefaultLanguage(),
510 );
511 }
512
513 return $translations;
514 }
515
516
524 public function delete() {
525 global $ilDB, $ilAppEventHandler;
526
527 // always call parent delete function first!!
528 if (!parent::delete()) {
529 return false;
530 }
531
532 // put here category specific stuff
533 include_once('./Services/User/classes/class.ilObjUserFolder.php');
535
536 $query = "DELETE FROM object_translation WHERE obj_id = "
537 . $ilDB->quote($this->getId(), 'integer');
538 $ilDB->manipulate($query);
539
540 $ilAppEventHandler->raise('Modules/OrgUnit', 'delete', array(
541 'object' => $this,
542 'obj_id' => $this->getId(),
543 ));
544
545 $sql = 'DELETE FROM ' . self::TABLE_NAME . ' WHERE orgu_id = '
546 . $ilDB->quote($this->getId(), 'integer');
547 $ilDB->manipulate($sql);
548
549 if (!$this->employee_role || !$this->superior_role) {
550 $this->doLoadRoles();
551 }
552 $emp = new ilObjRole($this->employee_role);
553 $emp->setParent($this->getRefId());
554 $emp->delete();
555 $sup = new ilObjRole($this->superior_role);
556 $sup->setParent($this->getRefId());
557 $sup->delete();
558
559 return true;
560 }
561
562
563 // remove all Translations of current OrgUnit
565 global $ilDB;
566
567 $query = "DELETE FROM object_translation WHERE obj_id= "
568 . $ilDB->quote($this->getId(), 'integer');
569 $res = $ilDB->manipulate($query);
570 }
571
572
573 // remove translations of current OrgUnit
574 function deleteTranslation($a_lang) {
575 global $ilDB;
576
577 $query = "DELETE FROM object_translation WHERE obj_id= "
578 . $ilDB->quote($this->getId(), 'integer') . " AND lang_code = "
579 . $ilDB->quote($a_lang, 'text');
580 $res = $ilDB->manipulate($query);
581 }
582
583
584 // add a new translation to current OrgUnit
585 function addTranslation($a_title, $a_desc, $a_lang, $a_lang_default) {
586 global $ilDB;
587
588 if (empty($a_title)) {
589 $a_title = "NO TITLE";
590 }
591
592 $query = "INSERT INTO object_translation "
593 . "(obj_id,title,description,lang_code,lang_default) " . "VALUES " . "("
594 . $ilDB->quote($this->getId(), 'integer') . "," . $ilDB->quote($a_title, 'text')
595 . "," . $ilDB->quote($a_desc, 'text') . "," . $ilDB->quote($a_lang, 'text') . ","
596 . $ilDB->quote($a_lang_default, 'integer') . ")";
597 $res = $ilDB->manipulate($query);
598
599 return true;
600 }
601
602
603 // update a translation to current OrgUnit
604 function updateTranslation($a_title, $a_desc, $a_lang, $a_lang_default) {
605 global $ilDB, $ilLog;
606
607 if (empty($a_title)) {
608 $a_title = "NO TITLE";
609 }
610
611 $query = "UPDATE object_translation SET ";
612
613 $query .= " title = " . $ilDB->quote($a_title, 'text');
614
615 if ($a_desc != "") {
616 $query .= ", description = " . $ilDB->quote($a_desc, 'text') . " ";
617 }
618
619 if ($a_lang_default) {
620 $query .= ", lang_default = " . $ilDB->quote($a_lang_default, 'integer') . " ";
621 }
622
623 $query .= " WHERE obj_id = " . $ilDB->quote($this->getId(), 'integer') . " AND lang_code = "
624 . $ilDB->quote($a_lang, 'text');
625 $res = $ilDB->manipulate($query);
626
627 return true;
628 }
629}
630
631?>
const DB_FETCHMODE_OBJECT
Definition: class.ilDB.php:11
const USER_FOLDER_ID
Class ilObjUserFolder.
static saveObjRecSelection($a_obj_id, $a_sub_type="", array $a_records=null, $a_delete_before=true)
Save repository object record selection.
static getInstancesForObjectId($a_obj_id, $a_obj_type=null, $a_sub_type="-", $a_sub_id=0)
Get instances for given object id.
Class ilContainer.
ilContainer($a_id=0, $a_call_by_reference=true)
Constructor @access public.
Class ilObjOrgUnit.
assignUsersToSuperiorRole($user_ids)
static _exists($a_id, $a_reference=false)
static loadRootOrgRefIdAndId()
getTitle()
Return title.
deassignUserFromSuperiorRole($user_id)
addTranslation($a_title, $a_desc, $a_lang, $a_lang_default)
initDefaultRoles()
init default roles settings Purpose of this function is to create a local role folder and local roles...
deassignUserFromEmployeeRole($user_id)
deassignUserFromLocalRole($role_id, $user_id)
Deassign a given user to a given local role.
__construct($a_id=0, $a_call_by_reference=true)
setSuperiorRole($superior_role)
deleteTranslation($a_lang)
assignUserToLocalRole($role_id, $user_id)
Assign a given user to a given local role.
getLongDescription()
get object long description (stored in object_description)
assignUsersToEmployeeRole($user_ids)
static getRootOrgRefId()
updateTranslation($a_title, $a_desc, $a_lang, $a_lang_default)
setEmployeeRole($employee_role)
Class ilObjRole.
static createDefaultRole($a_title, $a_description, $a_tpl_name, $a_ref_id)
static _updateUserFolderAssignment($a_old_id, $a_new_id)
Update user folder assignment Typically called after deleting a category with local user accounts.
getRefId()
get reference id @access public
getId()
get object id @access public
static getInstance($a_id)
Public.
$data
$r
Definition: example_031.php:79
redirection script todo: (a better solution should control the processing via a xml file)
global $ilDB