ILIAS  release_8 Revision v8.24
class.ilStudyProgrammeAutoMembershipsDBRepository.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
27{
28 private const TABLE = 'prg_auto_membership';
29 private const FIELD_PRG_OBJ_ID = 'prg_obj_id';
30 private const FIELD_SOURCE_TYPE = 'source_type';
31 private const FIELD_SOURCE_ID = 'source_id';
32 private const FIELD_ENABLED = 'enabled';
33 private const FIELD_EDITOR_ID = 'last_usr_id';
34 private const FIELD_LAST_EDITED = 'last_edited';
35
36 protected ilDBInterface $db;
38
39 public function __construct(ilDBInterface $db, int $current_usr_id)
40 {
41 $this->db = $db;
42 $this->current_usr_id = $current_usr_id;
43 }
44
48 public function getFor(int $prg_obj_id): array
49 {
50 $query = 'SELECT '
51 . self::FIELD_PRG_OBJ_ID . ','
52 . self::FIELD_SOURCE_TYPE . ','
53 . self::FIELD_SOURCE_ID . ','
54 . self::FIELD_ENABLED . ','
55 . self::FIELD_EDITOR_ID . ','
56 . self::FIELD_LAST_EDITED
57 . PHP_EOL . 'FROM ' . self::TABLE
58 . PHP_EOL . 'WHERE ' . self::FIELD_PRG_OBJ_ID . ' = '
59 . $this->db->quote($prg_obj_id, 'integer');
60 $res = $this->db->query($query);
61 $ret = [];
62 while ($rec = $this->db->fetchAssoc($res)) {
63 $ret[] = $this->create(
64 (int) $rec[self::FIELD_PRG_OBJ_ID],
65 $rec[self::FIELD_SOURCE_TYPE],
66 (int) $rec[self::FIELD_SOURCE_ID],
67 (bool) $rec[self::FIELD_ENABLED],
68 (int) $rec[self::FIELD_EDITOR_ID],
69 new DateTimeImmutable($rec[self::FIELD_LAST_EDITED])
70 );
71 }
72 return $ret;
73 }
74
75 public function create(
76 int $prg_obj_id,
77 string $source_type,
78 int $source_id,
79 bool $enabled,
80 int $last_edited_usr_id = null,
81 DateTimeImmutable $last_edited = null
83 if (is_null($last_edited_usr_id)) {
84 $last_edited_usr_id = $this->current_usr_id;
85 }
86 if (is_null($last_edited)) {
87 $last_edited = new DateTimeImmutable();
88 }
90 $prg_obj_id,
91 $source_type,
92 $source_id,
94 $last_edited_usr_id,
95 $last_edited
96 );
97 }
98
103 {
104 $ilAtomQuery = $this->db->buildAtomQuery();
105 $ilAtomQuery->addTableLock(self::TABLE);
106 $current_usr_id = $this->current_usr_id;
107 $ilAtomQuery->addQueryCallable(
108 function (ilDBInterface $db) use ($ams, $current_usr_id) {
109 $query = 'DELETE FROM ' . self::TABLE
110 . PHP_EOL . 'WHERE prg_obj_id = ' . $ams->getPrgObjId()
111 . PHP_EOL . 'AND ' . self::FIELD_SOURCE_TYPE . ' = ' . $this->db->quote($ams->getSourceType(), 'string')
112 . PHP_EOL . 'AND ' . self::FIELD_SOURCE_ID . ' = ' . $ams->getSourceId();
113 $db->manipulate($query);
114 $now = new DateTimeImmutable();
115 $now = $now->format('Y-m-d H:i:s');
116 $db->insert(
117 self::TABLE,
118 [
119 self::FIELD_PRG_OBJ_ID => ['integer', $ams->getPrgObjId()],
120 self::FIELD_SOURCE_TYPE => ['text', $ams->getSourceType()],
121 self::FIELD_SOURCE_ID => ['integer', $ams->getSourceId()],
122 self::FIELD_ENABLED => ['integer', $ams->isEnabled()],
123 self::FIELD_EDITOR_ID => ['integer', $current_usr_id],
124 self::FIELD_LAST_EDITED => ['timestamp', $now]
125 ]
126 );
127 }
128 );
129 $ilAtomQuery->run();
130 }
131
135 public function delete(int $prg_obj_id, string $source_type, int $source_id): void
136 {
137 $query = 'DELETE FROM ' . self::TABLE
138 . PHP_EOL . 'WHERE prg_obj_id = ' . $this->db->quote($prg_obj_id, 'integer')
139 . PHP_EOL . 'AND ' . self::FIELD_SOURCE_TYPE . ' = ' . $this->db->quote($source_type, 'string')
140 . PHP_EOL . 'AND ' . self::FIELD_SOURCE_ID . ' = ' . $this->db->quote($source_id, 'integer');
141
142 $this->db->manipulate($query);
143 }
144
148 public function deleteFor(int $prg_obj_id): void
149 {
150 $query = 'DELETE FROM ' . self::TABLE
151 . PHP_EOL . 'WHERE prg_obj_id = ' . $this->db->quote($prg_obj_id, 'integer');
152 $this->db->manipulate($query);
153 }
154
158 public static function getProgrammesFor(string $source_type, int $source_id): array
159 {
160 global $ilDB;
161 $query = 'SELECT ' . self::FIELD_PRG_OBJ_ID
162 . PHP_EOL . 'FROM ' . self::TABLE . ' prgs'
163 . PHP_EOL . 'INNER JOIN object_reference oref ON '
164 . 'prgs.' . self::FIELD_PRG_OBJ_ID . ' = oref.obj_id'
165 . PHP_EOL . 'WHERE ' . self::FIELD_SOURCE_TYPE . ' = ' . $ilDB->quote($source_type, 'text')
166 . PHP_EOL . 'AND ' . self::FIELD_SOURCE_ID . ' = ' . $ilDB->quote($source_id, 'integer')
167 . PHP_EOL . 'AND ' . self::FIELD_ENABLED . ' = 1'
168 . PHP_EOL . 'AND oref.deleted IS NULL';
169
170 $res = $ilDB->query($query);
171 return $ilDB->fetchAll($res);
172 }
173}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
deleteFor(int $prg_obj_id)
Delete all auto-membership sources of a programme.
create(int $prg_obj_id, string $source_type, int $source_id, bool $enabled, int $last_edited_usr_id=null, DateTimeImmutable $last_edited=null)
Build an auto-membership source.
static getProgrammesFor(string $source_type, int $source_id)
Get all programmes' obj_ids monitoring the given source.int[]
update(ilStudyProgrammeAutoMembershipSource $ams)
Update an auto-membership source.
getFor(int $prg_obj_id)
Read auto-membership sources of programme.ilStudyProgrammeAutoMembershipSource[]
if(!file_exists(getcwd() . '/ilias.ini.php'))
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: confirmReg.php:20
Interface ilDBInterface.
insert(string $table_name, array $values)
manipulate(string $query)
Run a (write) Query on the database.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$res
Definition: ltiservices.php:69
bool $enabled
Whether the system instance is enabled to accept connection requests.
Definition: System.php:123
$query