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