19 declare(strict_types=1);
42 public function get(
int|
string $value,
string $field): array
46 'core_identifier' =>
'integer',
49 if (!in_array($field, array_keys($fields))) {
50 throw new Exception(
"Invalid field: " . $field);
53 $query =
'SELECT id, title, description, core_position, core_identifier FROM' . PHP_EOL
55 .
' WHERE ' . self::TABLE_NAME .
'.' . $field .
' = ' . $this->db->quote($value, $fields[$field]);
56 $res = $this->db->query($query);
58 while ($rec = $this->db->fetchAssoc(
$res)) {
60 ->withTitle((
string) $rec[
'title'])
61 ->withDescription((
string) $rec[
'description'])
62 ->withCorePosition((
bool) $rec[
'core_position'])
63 ->withCoreIdentifier((
int) $rec[
'core_identifier'])
75 $pos = $this->
get($value, $field);
76 if (count($pos) === 0) {
80 return (array_shift($pos));
90 $query =
'SELECT id, title, description, core_position, core_identifier FROM' . PHP_EOL
91 . self::TABLE_NAME . PHP_EOL
93 $res = $this->db->query($query);
95 while ($rec = $this->db->fetchAssoc(
$res)) {
97 ->withTitle((
string) $rec[
'title'])
98 ->withDescription((
string) $rec[
'description'])
99 ->withCorePosition((
bool) $rec[
'core_position'])
100 ->withCoreIdentifier((
int) $rec[
'core_identifier'])
112 public function getArray(?
string $key = null, ?
string $field = null): array
114 if (!in_array(
$key, [
'id', null])) {
115 throw new Exception(
"Invalid key: " . $field);
121 'description' =>
'string',
122 'core_identifier' =>
'int',
123 'core_position' =>
'int' 125 if (!in_array($field, array_keys($fields)) && $field !== null) {
126 throw new Exception(
"Invalid field: " . $field);
129 if ($field !== null && $this->db->tableColumnExists(self::TABLE_NAME, $field)) {
130 $query =
'SELECT id, ' . $field .
' FROM' . PHP_EOL
134 $res = $this->db->query($query);
136 while ($rec = $this->db->fetchAssoc(
$res)) {
137 $value = $rec[$field];
138 if ($fields[$field] ==
'int') {
139 $value = (
int) $value;
140 } elseif ($fields[$field] ==
'string') {
141 $value = (string) $value;
144 $ret[$rec[
$key]] = $value;
150 $query =
'SELECT id, title, description, core_identifier, core_position FROM' . PHP_EOL
153 $res = $this->db->query($query);
155 while ($rec = $this->db->fetchAssoc(
$res)) {
175 $query =
'SELECT DISTINCT ' . self::TABLE_NAME .
'.id, ' . self::TABLE_NAME .
'.*' . PHP_EOL
176 .
'FROM ' . self::TABLE_NAME . PHP_EOL
177 .
'LEFT JOIN ' . self::TABLE_NAME_UA . PHP_EOL
178 .
'ON ' . self::TABLE_NAME .
'.id = ' . self::TABLE_NAME_UA .
'.position_id' . PHP_EOL
179 .
'AND ' . self::TABLE_NAME_UA .
'.orgu_id = ' . $this->db->quote($orgu_id,
'integer') . PHP_EOL
180 .
'WHERE ' . self::TABLE_NAME_UA .
'.user_id IS NOT NULL' . PHP_EOL
181 .
'OR ' . self::TABLE_NAME .
'.core_position = 1';
182 $res = $this->db->query($query);
184 while ($rec = $this->db->fetchAssoc(
$res)) {
186 ->withTitle((
string) $rec[
'title'])
187 ->withDescription((
string) $rec[
'description'])
188 ->withCorePosition((
bool) $rec[
'core_position'])
189 ->withCoreIdentifier((
int) $rec[
'core_identifier'])
206 if ($position->
getId() === 0) {
207 $position = $this->
insert($position);
214 $new_authorities = [];
215 foreach ($authorities as $authority) {
216 $auth = $this->authorityRepo->store($authority->withPositionId($position->
getId()));
217 $ids[] = $auth->getId();
218 $new_authorities[] = $auth;
222 if (count($ids) > 0) {
223 $this->authorityRepo->deleteLeftoverAuthorities($ids, $position->
getId());
225 if (count($ids) === 0) {
226 $authorities = $this->authorityRepo->get($position->
getId(),
'position_id');
227 foreach ($authorities as $authority) {
228 $this->authorityRepo->delete($authority->getId());
237 $id = $this->db->nextId(self::TABLE_NAME);
240 'id' => [
'integer',
$id],
241 'title' => [
'text', $position->
getTitle() ],
243 'core_position' => [
'integer', ($position->
isCorePosition()) ? 1 : 0 ],
247 $this->db->insert(self::TABLE_NAME, $values);
261 $where = [
'id' => [
'integer', $position->
getId() ] ];
264 'title' => [
'text', $position->
getTitle() ],
266 'core_position' => [
'integer', (($position->
isCorePosition()) ? 1 : 0) ],
270 $this->db->update(self::TABLE_NAME, $values, $where);
276 public function delete(
int $position_id):
void 278 $query =
'DELETE FROM ' . self::TABLE_NAME . PHP_EOL
279 .
' WHERE id = ' . $this->db->quote($position_id,
'integer');
281 $this->db->manipulate($query);
283 $authorities = $this->authorityRepo->get($position_id,
'position_id');
284 foreach ($authorities as $authority) {
285 $this->authorityRepo->delete($authority->getId());
288 $assignments = $this->assignmentRepo->getByPosition($position_id);
289 foreach ($assignments as $assignment) {
290 $this->assignmentRepo->delete($assignment);
300 return $this->authorityRepo->create();
302 $authority = $this->authorityRepo->get($id,
'id');
303 return array_shift($authority);
getAllPositions()
Returns all position objects.
__construct(ilDBInterface $db, ilOrgUnitAuthorityDBRepository $authorityRepo, ilOrgUnitUserAssignmentDBRepository $assignmentRepo)
getAuthority(?int $id)
Gets or creates an authority object, as the authority repo is encapsulated into this repo...
getSingle(int|string $value, string $field)
Get a single position from a filtered query (see get())
ilOrgUnitAuthorityDBRepository $authorityRepo
withAuthorities(array $authorities)
ilOrgUnitUserAssignmentDBRepository $assignmentRepo
getArray(?string $key=null, ?string $field=null)
Returns position data as an array, e.g.
getPositionsForOrgUnit(int $orgu_id)
Returns all core positions plus all positions with user assignments for a certain org unit (kept for ...
insert(ilOrgUnitPosition $position)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
update(ilOrgUnitPosition $position)
store(ilOrgUnitPosition $position)
Saves position and its authorities.