ILIAS  trunk Revision v11.0_alpha-1753-gb21ca8c4367
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilStudyProgrammeAutoMembershipsDBRepository.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
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  private const FIELD_SEARCH_RECURSIVE = 'search_recursive';
36 
37  protected ilDBInterface $db;
38  protected int$current_usr_id;
39 
40  public function __construct(ilDBInterface $db, int $current_usr_id)
41  {
42  $this->db = $db;
43  $this->current_usr_id = $current_usr_id;
44  }
45 
49  public function getFor(int $prg_obj_id): array
50  {
51  $query = 'SELECT '
52  . self::FIELD_PRG_OBJ_ID . ','
53  . self::FIELD_SOURCE_TYPE . ','
54  . self::FIELD_SOURCE_ID . ','
55  . self::FIELD_ENABLED . ','
56  . self::FIELD_EDITOR_ID . ','
57  . self::FIELD_LAST_EDITED . ','
58  . self::FIELD_SEARCH_RECURSIVE
59  . PHP_EOL . 'FROM ' . self::TABLE
60  . PHP_EOL . 'WHERE ' . self::FIELD_PRG_OBJ_ID . ' = '
61  . $this->db->quote($prg_obj_id, 'integer');
62  $res = $this->db->query($query);
63  $ret = [];
64  while ($rec = $this->db->fetchAssoc($res)) {
65  $ret[] = $this->create(
66  (int) $rec[self::FIELD_PRG_OBJ_ID],
67  $rec[self::FIELD_SOURCE_TYPE],
68  (int) $rec[self::FIELD_SOURCE_ID],
69  (bool) $rec[self::FIELD_ENABLED],
70  (int) $rec[self::FIELD_EDITOR_ID],
71  new DateTimeImmutable($rec[self::FIELD_LAST_EDITED]),
72  (bool) $rec[self::FIELD_SEARCH_RECURSIVE]
73  );
74  }
75  return $ret;
76  }
77 
78  public function create(
79  int $prg_obj_id,
80  string $source_type,
81  int $source_id,
82  bool $enabled,
83  ?int $last_edited_usr_id = null,
84  ?DateTimeImmutable $last_edited = null,
85  bool $search_recursive = false
87  if (is_null($last_edited_usr_id)) {
88  $last_edited_usr_id = $this->current_usr_id;
89  }
90  if (is_null($last_edited)) {
91  $last_edited = new DateTimeImmutable();
92  }
94  $prg_obj_id,
95  $source_type,
96  $source_id,
97  $enabled,
98  $last_edited_usr_id,
99  $last_edited,
100  $search_recursive
101  );
102  }
103 
107  public function update(ilStudyProgrammeAutoMembershipSource $ams): void
108  {
109  $ilAtomQuery = $this->db->buildAtomQuery();
110  $ilAtomQuery->addTableLock(self::TABLE);
111  $current_usr_id = $this->current_usr_id;
112  $ilAtomQuery->addQueryCallable(
113  function (ilDBInterface $db) use ($ams, $current_usr_id) {
114  $query = 'DELETE FROM ' . self::TABLE
115  . PHP_EOL . 'WHERE prg_obj_id = ' . $ams->getPrgObjId()
116  . PHP_EOL . 'AND ' . self::FIELD_SOURCE_TYPE . ' = ' . $this->db->quote($ams->getSourceType(), 'string')
117  . PHP_EOL . 'AND ' . self::FIELD_SOURCE_ID . ' = ' . $ams->getSourceId();
118  $db->manipulate($query);
119  $now = new DateTimeImmutable();
120  $now = $now->format('Y-m-d H:i:s');
121  $db->insert(
122  self::TABLE,
123  [
124  self::FIELD_PRG_OBJ_ID => ['integer', $ams->getPrgObjId()],
125  self::FIELD_SOURCE_TYPE => ['text', $ams->getSourceType()],
126  self::FIELD_SOURCE_ID => ['integer', $ams->getSourceId()],
127  self::FIELD_ENABLED => ['integer', $ams->isEnabled()],
128  self::FIELD_EDITOR_ID => ['integer', $current_usr_id],
129  self::FIELD_LAST_EDITED => ['timestamp', $now],
130  self::FIELD_SEARCH_RECURSIVE => ['integer', $ams->isSearchRecursive()]
131  ]
132  );
133  }
134  );
135  $ilAtomQuery->run();
136  }
137 
141  public function delete(int $prg_obj_id, string $source_type, int $source_id): void
142  {
143  $query = 'DELETE FROM ' . self::TABLE
144  . PHP_EOL . 'WHERE prg_obj_id = ' . $this->db->quote($prg_obj_id, 'integer')
145  . PHP_EOL . 'AND ' . self::FIELD_SOURCE_TYPE . ' = ' . $this->db->quote($source_type, 'string')
146  . PHP_EOL . 'AND ' . self::FIELD_SOURCE_ID . ' = ' . $this->db->quote($source_id, 'integer');
147 
148  $this->db->manipulate($query);
149  }
150 
154  public function deleteFor(int $prg_obj_id): void
155  {
156  $query = 'DELETE FROM ' . self::TABLE
157  . PHP_EOL . 'WHERE prg_obj_id = ' . $this->db->quote($prg_obj_id, 'integer');
158  $this->db->manipulate($query);
159  }
160 
164  public static function getProgrammesFor(string $source_type, int $source_id): array
165  {
166  global $DIC;
167  $db = $DIC["ilDB"];
168 
169  $q = "SELECT path FROM tree WHERE child = " . $db->quote($source_id, "integer");
170  $res = $db->query($q);
171  $row = $db->fetchAssoc($res);
172  $path = explode(".", $row["path"] ?? "");
173 
174  $query = 'SELECT ' . self::FIELD_PRG_OBJ_ID . PHP_EOL
175  . 'FROM ' . self::TABLE . ' prgs' . PHP_EOL
176  . 'INNER JOIN object_reference oref ON ' . PHP_EOL
177  . 'prgs.' . self::FIELD_PRG_OBJ_ID . ' = oref.obj_id' . PHP_EOL
178  . 'WHERE ('
179  . self::FIELD_SOURCE_ID . ' = ' . $db->quote($source_id, 'integer') . PHP_EOL
180  . ' OR (' . $db->in(self::FIELD_SOURCE_ID, $path, false, 'integer') . ' AND search_recursive = 1)' . PHP_EOL
181  . ')' . PHP_EOL
182  . 'AND ' . self::FIELD_ENABLED . ' = 1' . PHP_EOL
183  . 'AND ' . self::FIELD_SOURCE_TYPE . ' = ' . $db->quote($source_type, 'text') . PHP_EOL
184  . 'AND oref.deleted IS NULL';
185 
186  $res = $db->query($query);
187  return $db->fetchAll($res);
188  }
189 }
$res
Definition: ltiservices.php:66
insert(string $table_name, array $values)
fetchAssoc(ilDBStatement $statement)
fetchAll(ilDBStatement $statement, int $fetch_mode=ilDBConstants::FETCHMODE_ASSOC)
create(int $prg_obj_id, string $source_type, int $source_id, bool $enabled, ?int $last_edited_usr_id=null, ?DateTimeImmutable $last_edited=null, bool $search_recursive=false)
quote($value, string $type)
static getProgrammesFor(string $source_type, int $source_id)
Get all programmes&#39; obj_ids monitoring the given source.int[]
$path
Definition: ltiservices.php:29
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
Persistence of "monitored" sources for automatic membership.
deleteFor(int $prg_obj_id)
Delete all auto-membership sources of a programme.
global $DIC
Definition: shib_login.php:22
query(string $query)
Run a (read-only) Query on the database.
in(string $field, array $values, bool $negate=false, string $type="")
$q
Definition: shib_logout.php:21
manipulate(string $query)
Run a (write) Query on the database.
getFor(int $prg_obj_id)
Read auto-membership sources of programme.ilStudyProgrammeAutoMembershipSource[]
update(ilStudyProgrammeAutoMembershipSource $ams)
Update an auto-membership source.