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