ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilOrgUnitSimpleUserImport.php
Go to the documentation of this file.
1 <?php
18 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
19 
27 {
31  public function simpleUserImport($file_path)
32  {
33  $this->stats = array('created' => 0, 'removed' => 0);
34  $a = file_get_contents($file_path, 'r');
35  $xml = new SimpleXMLElement($a);
36 
37  if (!count($xml->children())) {
38  $this->addError('no_assignment', null, null);
39 
40  return;
41  }
42 
43  foreach ($xml->children() as $a) {
45  }
46  }
47 
51  public function simpleUserImportElement(SimpleXMLElement $a)
52  {
53  $attributes = $a->attributes();
54  $action = $attributes->action;
55  $user_id_type = $a->User->attributes()->id_type;
56  $user_id = (string) $a->User;
57  $org_unit_id_type = $a->OrgUnit->attributes()->id_type;
58  $org_unit_id = (string) $a->OrgUnit;
59  $role = (string) $a->Role;
60 
61  if (!$user_id = $this->buildUserId($user_id, $user_id_type)) {
62  $this->addError('user_not_found', $a->User);
63 
64  return;
65  }
66 
67  if (!$org_unit_id = $this->buildRef($org_unit_id, $org_unit_id_type)) {
68  $this->addError('org_unit_not_found', $a->OrgUnit);
69 
70  return;
71  }
72  $org_unit = new ilObjOrgUnit($org_unit_id);
73 
74  if ($role === 'employee') {
76  } elseif ($role === 'superior') {
78  } else {
79  //if passed a custom position.
80  $position = ilOrgUnitPosition::where(['title' => $role])->first();
81  if ($position instanceof ilOrgUnitPosition) {
82  $position_id = $position->getId();
83  } else {
84  $this->addError('not_a_valid_role', $user_id);
85  return;
86  }
87  }
88 
89  if ($action == 'add') {
90  $assignment = ilOrgUnitUserAssignment::findOrCreateAssignment($user_id, $position_id, $org_unit_id);
91  $assignment->store();
92 
93  $this->stats['created']++;
94  } elseif ($action == 'remove') {
95  $assignment = ilOrgUnitUserAssignment::findOrCreateAssignment($user_id, $position_id, $org_unit_id);
96  $assignment->delete();
97  $this->stats['removed']++;
98  } else {
99  $this->addError('not_a_valid_action', $user_id);
100  }
101  }
102 
108  private function buildUserId($id, $type)
109  {
110  global $DIC;
111  $ilDB = $DIC['ilDB'];
112  if ($type == 'ilias_login') {
113  $user_id = ilObjUser::_lookupId($id);
114 
115  return $user_id ? $user_id : false;
116  } elseif ($type == 'external_id') {
118 
119  return $user_id ? $user_id : false;
120  } elseif ($type == 'email') {
121  $q = 'SELECT usr_id FROM usr_data WHERE email = ' . $ilDB->quote($id, 'text');
122  $set = $ilDB->query($q);
123  $user_id = $ilDB->fetchAssoc($set);
124 
125  return $user_id ? $user_id : false;
126  } elseif ($type == 'user_id') {
127  return $id;
128  } else {
129  return false;
130  }
131  }
132 }
static _lookupObjIdByImportId(string $import_id)
Get (latest) object id for an import id.
$attributes
Definition: metadata.php:248
static findOrCreateAssignment(int $user_id, int $position_id, int $orgu_id)
$type
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _lookupId($a_user_str)
static where($where, $operator=null)
global $DIC
Definition: feed.php:28
buildRef($id, string $type)
$xml
Definition: metadata.php:351
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:23
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
addError(string $lang_var, string $import_id, ?string $action=null)