ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilOrgUnitSimpleUserImport.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3
13{
14
18 public function simpleUserImport($file_path)
19 {
20 $this->stats = array('created' => 0, 'removed' => 0);
21 $a = file_get_contents($file_path, 'r');
22 $xml = new SimpleXMLElement($a);
23
24 if (!count($xml->children())) {
25 $this->addError('no_assignment', null, null);
26
27 return;
28 }
29
30 foreach ($xml->children() as $a) {
31 $this->simpleUserImportElement($a);
32 }
33 }
34
35
39 public function simpleUserImportElement(SimpleXMLElement $a)
40 {
41 global $DIC;
42 $rbacadmin = $DIC['rbacadmin'];
43
44 $attributes = $a->attributes();
45 $action = $attributes->action;
46 $user_id_type = $a->User->attributes()->id_type;
47 $user_id = (string) $a->User;
48 $org_unit_id_type = $a->OrgUnit->attributes()->id_type;
49 $org_unit_id = (string) $a->OrgUnit;
50 $role = (string) $a->Role;
51
52 if (!$user_id = $this->buildUserId($user_id, $user_id_type)) {
53 $this->addError('user_not_found', $a->User);
54
55 return;
56 }
57
58 if (!$org_unit_id = $this->buildRef($org_unit_id, $org_unit_id_type)) {
59 $this->addError('org_unit_not_found', $a->OrgUnit);
60
61 return;
62 }
63 $org_unit = new ilObjOrgUnit($org_unit_id);
64
65 if ($role === 'employee') {
67 } elseif ($role === 'superior') {
69 } else {
70 //if passed a custom position.
71 $position = ilOrgUnitPosition::where(['title' => $role])->first();
72 if ($position instanceof ilOrgUnitPosition) {
73 $position_id = $position->getId();
74 } else {
75 $this->addError('not_a_valid_role', $user_id);
76 return;
77 }
78 }
79
80 if ($action == 'add') {
81 $assignment = ilOrgUnitUserAssignment::findOrCreateAssignment($user_id, $position_id, $org_unit_id);
82 $assignment->store();
83
84 $this->stats['created']++;
85 } elseif ($action == 'remove') {
86 $assignment = ilOrgUnitUserAssignment::findOrCreateAssignment($user_id, $position_id, $org_unit_id);
87 $assignment->delete();
88 $this->stats['removed']++;
89 } else {
90 $this->addError('not_a_valid_action', $user_id);
91 }
92 }
93
94
101 private function buildUserId($id, $type)
102 {
103 global $DIC;
104 $ilDB = $DIC['ilDB'];
105 if ($type == 'ilias_login') {
106 $user_id = ilObjUser::_lookupId($id);
107
108 return $user_id ? $user_id : false;
109 } elseif ($type == 'external_id') {
110 $user_id = ilObjUser::_lookupObjIdByImportId($id);
111
112 return $user_id ? $user_id : false;
113 } elseif ($type == 'email') {
114 $q = 'SELECT usr_id FROM usr_data WHERE email = ' . $ilDB->quote($id, 'text');
115 $set = $ilDB->query($q);
116 $user_id = $ilDB->fetchAssoc($set);
117
118 return $user_id ? $user_id : false;
119 } elseif ($type == 'user_id') {
120 return $id;
121 } else {
122 return false;
123 }
124 }
125}
static where($where, $operator=null)
An exception for terminatinating execution or to throw for unit testing.
Class ilObjOrgUnit.
static _lookupId($a_user_str)
Lookup id by login.
static _lookupObjIdByImportId($a_import_id)
Class ilOrgUnitImporter.
addError($lang_var, $import_id, $action=null)
Class ilOrgUnitPosition.
Class ilOrgUnitSimpleUserImport.
static findOrCreateAssignment($user_id, $position_id, $orgu_id)
$attributes
Definition: metadata.php:231
$xml
Definition: metadata.php:332
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
$type
global $ilDB
$DIC
Definition: xapitoken.php:46