ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilOrgUnitSimpleUserImport.php
Go to the documentation of this file.
1 <?php
2 
27 {
28  protected \ilOrgUnitPositionDBRepository $positionRepo;
29  protected \ilOrgUnitUserAssignmentDBRepository $assignmentRepo;
30 
31  public function __construct()
32  {
34  $this->positionRepo = $dic["repo.Positions"];
35  $this->assignmentRepo = $dic["repo.UserAssignments"];
36  }
37 
41  public function simpleUserImport($file_path)
42  {
43  $this->stats = array('created' => 0, 'removed' => 0);
44  $a = file_get_contents($file_path, 'r');
45  $xml = new SimpleXMLElement($a);
46 
47  if (!count($xml->children())) {
48  $this->addError('no_assignment', null, null);
49 
50  return;
51  }
52 
53  foreach ($xml->children() as $a) {
55  }
56  }
57 
62  {
63  $attributes = $a->attributes();
64  $action = $attributes->action;
65  $user_id_type = $a->User->attributes()->id_type;
66  $user_id = (string) $a->User;
67  $org_unit_id_type = $a->OrgUnit->attributes()->id_type;
68  $org_unit_id = (string) $a->OrgUnit;
69  $role = (string) $a->Role;
70 
71  if (!$user_id = $this->buildUserId($user_id, $user_id_type)) {
72  $this->addError('user_not_found', $a->User);
73 
74  return;
75  }
76 
77  if (!$org_unit_id = $this->buildRef($org_unit_id, $org_unit_id_type)) {
78  $this->addError('org_unit_not_found', $a->OrgUnit);
79 
80  return;
81  }
82  $org_unit = new ilObjOrgUnit($org_unit_id);
83 
84  if ($role === 'employee') {
85  $position_id = $this->positionRepo
86  ->getSingle(ilOrgUnitPosition::CORE_POSITION_EMPLOYEE, 'core_identifier')
87  ->getId();
88  } elseif ($role === 'superior') {
89  $position_id = $this->positionRepo
90  ->getSingle(ilOrgUnitPosition::CORE_POSITION_SUPERIOR, 'core_identifier')
91  ->getId();
92  } else {
93  //if passed a custom position.
94  $position = $this->positionRepo->getSingle($role, 'title');
95  if ($position instanceof ilOrgUnitPosition) {
96  $position_id = $position->getId();
97  } else {
98  $this->addError('not_a_valid_role', $user_id);
99  return;
100  }
101  }
102 
103  if ($action == 'add') {
104  $assignment = $this->assignmentRepo->get($user_id, $position_id, $org_unit_id);
105  $this->stats['created']++;
106  } elseif ($action == 'remove') {
107  $assignment = $this->assignmentRepo->find($user_id, $position_id, $org_unit_id);
108  if ($assignment) {
109  $this->assignmentRepo->delete($assignment);
110  $this->stats['removed']++;
111  }
112  } else {
113  $this->addError('not_a_valid_action', $user_id);
114  }
115  }
116 
122  private function buildUserId($id, $type)
123  {
124  global $DIC;
125  $ilDB = $DIC['ilDB'];
126  if ($type == 'ilias_login') {
128 
129  return $user_id ? $user_id : false;
130  } elseif ($type == 'external_id') {
132 
133  return $user_id ? $user_id : false;
134  } elseif ($type == 'email') {
135  $q = 'SELECT usr_id FROM usr_data WHERE email = ' . $ilDB->quote($id, 'text');
136  $set = $ilDB->query($q);
137  $user_id = $ilDB->fetchAssoc($set);
138 
139  return $user_id ? $user_id : false;
140  } elseif ($type == 'user_id') {
141  return $id;
142  } else {
143  return false;
144  }
145  }
146 }
static _lookupObjIdByImportId(string $import_id)
Get (latest) object id for an import id.
ilOrgUnitPositionDBRepository $positionRepo
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _lookupId($a_user_str)
ilOrgUnitUserAssignmentDBRepository $assignmentRepo
buildRef($id, string $type)
global $DIC
Definition: shib_login.php:25
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:24
$q
Definition: shib_logout.php:18
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
$dic
Definition: ltiresult.php:33
addError(string $lang_var, string $import_id, ?string $action=null)