ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilStudyProgrammeAutoMembershipsDBRepository.php
Go to the documentation of this file.
1 <?php
2 
3  declare(strict_types = 1);
4 
11 {
12  const TABLE = 'prg_auto_membership';
13  const FIELD_PRG_OBJ_ID = 'prg_obj_id';
14  const FIELD_SOURCE_TYPE = 'source_type';
15  const FIELD_SOURCE_ID = 'source_id';
16  const FIELD_ENABLED = 'enabled';
17  const FIELD_EDITOR_ID = 'last_usr_id';
18  const FIELD_LAST_EDITED = 'last_edited';
19 
23  protected $db;
24 
28  protected $current_usr_id;
29  public function __construct(
31  int $current_usr_id
32  ) {
33  $this->db = $db;
34  $this->current_usr_id = $current_usr_id;
35  }
36 
40  public function getFor(int $prg_obj_id) : array
41  {
42  $query = 'SELECT '
43  . self::FIELD_PRG_OBJ_ID . ','
44  . self::FIELD_SOURCE_TYPE . ','
45  . self::FIELD_SOURCE_ID . ','
46  . self::FIELD_ENABLED . ','
47  . self::FIELD_EDITOR_ID . ','
48  . self::FIELD_LAST_EDITED
49  . PHP_EOL . 'FROM ' . self::TABLE
50  . PHP_EOL . 'WHERE ' . self::FIELD_PRG_OBJ_ID . ' = '
51  . $this->db->quote($prg_obj_id, 'integer');
52  $res = $this->db->query($query);
53  $ret = [];
54  while ($rec = $this->db->fetchAssoc($res)) {
55  $ret[] = $this->create(
56  (int) $rec[self::FIELD_PRG_OBJ_ID],
57  $rec[self::FIELD_SOURCE_TYPE],
58  (int) $rec[self::FIELD_SOURCE_ID],
59  (bool) $rec[self::FIELD_ENABLED],
60  (int) $rec[self::FIELD_EDITOR_ID],
61  new \DateTimeImmutable($rec[self::FIELD_LAST_EDITED])
62  );
63  }
64  return $ret;
65  }
66 
67  public function create(
68  int $prg_obj_id,
69  string $source_type,
70  int $source_id,
71  bool $enabled,
72  int $last_edited_usr_id = null,
73  \DateTimeImmutable $last_edited = null
75  if (is_null($last_edited_usr_id)) {
76  $last_edited_usr_id = $this->current_usr_id;
77  }
78  if (is_null($last_edited)) {
79  $last_edited = new \DateTimeImmutable();
80  }
82  $prg_obj_id,
83  $source_type,
84  $source_id,
85  $enabled,
86  $last_edited_usr_id,
87  $last_edited
88  );
89  }
90 
95  {
96  $ilAtomQuery = $this->db->buildAtomQuery();
97  $ilAtomQuery->addTableLock(self::TABLE);
99  $ilAtomQuery->addQueryCallable(
100  function (ilDBInterface $db) use ($ams, $current_usr_id) {
101  $query = 'DELETE FROM ' . self::TABLE
102  . PHP_EOL . 'WHERE prg_obj_id = ' . $ams->getPrgObjId()
103  . PHP_EOL . 'AND ' . self::FIELD_SOURCE_TYPE . ' = ' . $this->db->quote($ams->getSourceType(), 'string')
104  . PHP_EOL . 'AND ' . self::FIELD_SOURCE_ID . ' = ' . $ams->getSourceId();
105  $db->manipulate($query);
106  $now = new \DateTimeImmutable();
107  $now = $now->format('Y-m-d H:i:s');
108  $db->insert(
109  self::TABLE,
110  [
111  self::FIELD_PRG_OBJ_ID => ['integer', $ams->getPrgObjId()],
112  self::FIELD_SOURCE_TYPE => ['text', $ams->getSourceType()],
113  self::FIELD_SOURCE_ID => ['integer', $ams->getSourceId()],
114  self::FIELD_ENABLED => ['integer', $ams->isEnabled()],
115  self::FIELD_EDITOR_ID => ['integer', $current_usr_id],
116  self::FIELD_LAST_EDITED => ['timestamp', $now]
117  ]
118  );
119  }
120  );
121  $ilAtomQuery->run();
122  }
123 
127  public function delete(int $prg_obj_id, string $source_type, int $source_id)
128  {
129  $query = 'DELETE FROM ' . self::TABLE
130  . PHP_EOL . 'WHERE prg_obj_id = ' . $this->db->quote($prg_obj_id, 'integer')
131  . PHP_EOL . 'AND ' . self::FIELD_SOURCE_TYPE . ' = ' . $this->db->quote($source_type, 'string')
132  . PHP_EOL . 'AND ' . self::FIELD_SOURCE_ID . ' = ' . $this->db->quote($source_id, 'integer');
133 
134  $this->db->manipulate($query);
135  }
136 
140  public function deleteFor(int $prg_obj_id)
141  {
142  $query = 'DELETE FROM ' . self::TABLE
143  . PHP_EOL . 'WHERE prg_obj_id = ' . $this->db->quote($prg_obj_id, 'integer');
144  $this->db->manipulate($query);
145  }
146 
150  public static function getProgrammesFor(string $source_type, int $source_id) : array
151  {
152  global $ilDB;
153  $query = 'SELECT ' . self::FIELD_PRG_OBJ_ID
154  . PHP_EOL . 'FROM ' . self::TABLE . ' prgs'
155  . PHP_EOL . 'INNER JOIN object_reference oref ON '
156  . 'prgs.' . self::FIELD_PRG_OBJ_ID . ' = oref.obj_id'
157  . PHP_EOL . 'WHERE ' . self::FIELD_SOURCE_TYPE . ' = ' . $ilDB->quote($source_type, 'text')
158  . PHP_EOL . 'AND ' . self::FIELD_SOURCE_ID . ' = ' . $ilDB->quote($source_id, 'integer')
159  . PHP_EOL . 'AND ' . self::FIELD_ENABLED . ' = 1'
160  . PHP_EOL . 'AND oref.deleted IS NULL';
161 
162  $res = $ilDB->query($query);
163  $ret = $ilDB->fetchAll($res);
164  return $ret;
165  }
166 }
static getProgrammesFor(string $source_type, int $source_id)
Get all programmes&#39; obj_ids monitoring the given source.int[]
foreach($_POST as $key=> $value) $res
Persistence of "monitored" sources for automatic membership.
deleteFor(int $prg_obj_id)
Delete all auto-membership sources of a programme.
$query
global $ilDB
$ret
Definition: parser.php:6
insert($table_name, $values)
getFor(int $prg_obj_id)
Read auto-membership sources of programme.ilStudyProgrammeAutoMembershipSource[]
update(ilStudyProgrammeAutoMembershipSource $ams)
Update an auto-membership source.ilStudyProgrammeAutoCategory[]
manipulate($query)
Run a (write) Query on the database.
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.