ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilOrgUnitPathStorage.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
26{
27 public const GLUE = ' > ';
28 public const GLUE_SIMPLE = ' - ';
29 public const ORG_SEPARATOR = ' | ';
30 public const TABLE_NAME = 'orgu_path_storage';
31 public const MAX_MIDDLE_PATH_LENGTH = 50;
40 protected ?int $ref_id = 0;
47 protected int $obj_id = 0;
53 protected string $path = '';
54
55 protected static array $orgu_names = array();
56
60 public static function getAllOrguRefIds(): array
61 {
62 $names = self::getAllOrguNames();
63
64 return array_keys($names);
65 }
66
67 public function store(): void
68 {
69 if (self::where(array('ref_id' => $this->getRefId()))->hasSets()) {
70 $this->update();
71 } else {
72 $this->create();
73 }
74 }
75
84 public static function getTextRepresentationOfUsersOrgUnits(
85 int $user_id,
86 string $separator = self::ORG_SEPARATOR,
87 bool $using_tmp_table = true
88 ) {
89 if ($using_tmp_table === true) {
90 global $DIC;
94 $ilDB = $DIC['ilDB'];
95 ilObjOrgUnitTree::_getInstance()->buildTempTableWithUsrAssignements();
96
97 $res = $ilDB->queryF(
98 "SELECT " . $ilDB->groupConcat(
99 "path",
100 $separator
101 ) . " AS orgus FROM orgu_usr_assignements WHERE user_id = %s GROUP BY user_id;",
102 array('integer'),
103 array($user_id)
104 );
105 $dat = $ilDB->fetchObject($res);
106
107 return (isset($dat->orgus) && $dat->orgus) ? $dat->orgus : '-';
108 } else {
109 $array_of_org_ids = ilObjOrgUnitTree::_getInstance()->getOrgUnitOfUser($user_id);
110
111 if (!$array_of_org_ids) {
112 return '-';
113 }
114 $paths = ilOrgUnitPathStorage::where(array('ref_id' => $array_of_org_ids))->getArray(null, 'path');
115
116 return implode($separator, $paths);
117 }
118 }
119
125 public static function getTextRepresentationOfOrgUnits(bool $sort_by_title = true): array
126 {
127 if ($sort_by_title) {
128 return ilOrgUnitPathStorage::orderBy('path')->getArray('ref_id', 'path');
129 }
130 return ilOrgUnitPathStorage::getArray('ref_id', 'path');
131 }
132
133 public static function writePathByRefId(int $ref_id): void
134 {
135 $original_ref_id = $ref_id;
136 $names = self::getAllOrguNames();
137 $root_ref_id = ilObjOrgUnit::getRootOrgRefId();
139 $path = array($names[$ref_id]);
140 if ($ref_id == $root_ref_id || !$ref_id) {
141 return;
142 }
143 while ($ref_id != $root_ref_id && $ref_id) {
144 $ref_id = $tree->getParent($ref_id);
145 if ($ref_id != $root_ref_id && isset($names[$ref_id]) && $names[$ref_id]) {
146 $path[] = $names[$ref_id];
147 }
148 }
149
150 if (count($path) > 2) {
151 $first = array_shift($path);
152 $last = array_pop($path);
153 $middle = implode(self::GLUE_SIMPLE, $path);
154 if (mb_strlen($middle) > self::MAX_MIDDLE_PATH_LENGTH) {
155 $middle = mb_substr($middle, 0, self::MAX_MIDDLE_PATH_LENGTH) . " ...";
156 }
157 $expression = implode(self::GLUE_SIMPLE, [$first, $middle, $last]);
158 } else {
159 $expression = implode(self::GLUE_SIMPLE, $path);
160 }
164 $ilOrgUnitPathStorage = self::findOrGetInstance($original_ref_id);
165 $ilOrgUnitPathStorage->setRefId($original_ref_id);
166 $ilOrgUnitPathStorage->setObjId(ilObject2::_lookupObjectId($original_ref_id));
167 $ilOrgUnitPathStorage->setPath($expression);
168 $ilOrgUnitPathStorage->store();
169 }
170
171 public static function clearDeleted(): void
172 {
173 global $DIC;
177 $ilDB = $DIC['ilDB'];
178 $ref_ids = self::getAllOrguRefIds();
179 $q = "DELETE FROM " . self::TABLE_NAME . " WHERE " . $ilDB->in('ref_id', $ref_ids, true, 'integer');
180 $ilDB->manipulate($q);
181 }
182
184 public static function getAllOrguNames(?array $lng_key = null): array
185 {
186 if (count(self::$orgu_names) == 0) {
187 global $DIC;
191 $ilDB = $DIC['ilDB'];
192 $res = $ilDB->queryF('SELECT * FROM object_reference
193 JOIN object_data ON object_reference.obj_id = object_data.obj_id AND deleted IS NULL
194 WHERE object_data.type = %s', array('text'), array('orgu'));
195 while ($data = $ilDB->fetchObject($res)) {
196 self::$orgu_names[$data->ref_id] = $data->title;
197 }
198 }
199
200 return self::$orgu_names;
201 }
202
203 public function getConnectorContainerName(): string
204 {
205 return self::TABLE_NAME;
206 }
207
208 public function getRefId(): int
209 {
210 return $this->ref_id;
211 }
212
213 public function setRefId(int $ref_id): void
214 {
215 $this->ref_id = $ref_id;
216 }
217
218 public function getPath(): string
219 {
220 return $this->path;
221 }
222
223 public function setPath(string $path): void
224 {
225 $this->path = $path;
226 }
227
228 public function getObjId(): int
229 {
230 return $this->obj_id;
231 }
232
233 public function setObjId(int $obj_id): void
234 {
235 $this->obj_id = $obj_id;
236 }
237}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static where($where, $operator=null)
static getArray(?string $key=null, string|array|null $values=null)
static orderBy(string $orderBy, string $orderDirection='ASC')
static findOrGetInstance($primary_key, array $add_constructor_args=[])
static getRootOrgRefId()
static _lookupObjectId(int $ref_id)
Class ilOrgUnitPathStorage.
getConnectorContainerName()
@description Return the Name of your Connector Table
static getTextRepresentationOfOrgUnits(bool $sort_by_title=true)
Get ref id path array.
$res
Definition: ltiservices.php:69
global $DIC
Definition: shib_login.php:26
$q
Definition: shib_logout.php:23