ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilECSCourseMemberAssignment.php
Go to the documentation of this file.
1 <?php
2 
18 declare(strict_types=1);
24 {
25  public const STATUS_ASSIGNED = 0;
26  public const STATUS_LOCAL_DELETED = 1;
27 
28  private ilDBInterface $db;
29 
30  private int $id;
31  private int $server;
32  private int $mid;
33  private int $cms_id;
34  private ?int $cms_sub_id = null;
35  private int $obj_id;
36  private string $uid;
37  private bool $status = false;
38 
39 
43  public function __construct(int $a_id = 0)
44  {
45  global $DIC;
46 
47  $this->db = $DIC->database();
48 
49  $this->id = $a_id;
50 
51  $this->read();
52  }
53 
59  public static function lookupMissingAssignmentsOfUser(string $a_usr_id): array
60  {
61  global $DIC;
62 
63  $ilDB = $DIC['ilDB'];
64 
65  $query = 'SELECT id FROM ecs_course_assignments ' .
66  'WHERE usr_id = ' . $ilDB->quote($a_usr_id, 'text');
67  $res = $ilDB->query($query);
68 
69  $assignments = array();
70  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
71  $assignments[] = new self((int) $row->id);
72  }
73  return $assignments;
74  }
75 
79  public static function deleteByObjId(int $a_obj_id): bool
80  {
81  global $DIC;
82 
83  $ilDB = $DIC['ilDB'];
84 
85  $query = 'DELETE FROM ecs_course_assignments ' .
86  'WHERE obj_id = ' . $ilDB->quote($a_obj_id, 'integer');
87  $ilDB->manipulate($query);
88  return true;
89  }
90 
94  public static function deleteByServerId(int $a_server_id): bool
95  {
96  global $DIC;
97 
98  $ilDB = $DIC['ilDB'];
99 
100  $query = 'DELETE FROM ecs_course_assignments ' .
101  'WHERE sid = ' . $ilDB->quote($a_server_id, 'integer');
102  $ilDB->manipulate($query);
103  return true;
104  }
105 
109  public static function lookupUserIds(int $a_cms_id, ?int $a_cms_sub_id, int $a_obj_id): array
110  {
111  global $DIC;
112 
113  $ilDB = $DIC['ilDB'];
114 
115  if (is_null($a_cms_sub_id)) {
116  $cms_sub_id_query = 'AND (cms_sub_id IS NULL OR cms_sub_id = 0) ';
117  } else {
118  $cms_sub_id_query = 'AND cms_sub_id = ' . $ilDB->quote($a_cms_sub_id, 'integer') . ' ';
119  }
120 
121  $query = 'SELECT usr_id FROM ecs_course_assignments ' .
122  'WHERE cms_id = ' . $ilDB->quote($a_cms_id, 'integer') . ' ' .
123  $cms_sub_id_query .
124  'AND obj_id = ' . $ilDB->quote($a_obj_id, 'integer');
125  $res = $ilDB->query($query);
126 
127  $usr_ids = [];
128  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
129  $usr_ids[] = $row->usr_id;
130  }
131  return $usr_ids;
132  }
133 
137  public static function lookupAssignment(int $a_cms_id, ?int $a_cms_sub_id, int $a_obj_id, string $a_usr_id): ?ilECSCourseMemberAssignment
138  {
139  global $DIC;
140 
141  $ilDB = $DIC['ilDB'];
142 
143  if (is_null($a_cms_sub_id)) {
144  $cms_sub_id_query = 'AND (cms_sub_id IS NULL OR cms_sub_id = 0) ';
145  } else {
146  $cms_sub_id_query = 'AND cms_sub_id = ' . $ilDB->quote($a_cms_sub_id, 'integer') . ' ';
147  }
148 
149  $query = 'SELECT id FROM ecs_course_assignments ' .
150  'WHERE cms_id = ' . $ilDB->quote($a_cms_id, 'integer') . ' ' .
151  $cms_sub_id_query .
152  'AND obj_id = ' . $ilDB->quote($a_obj_id, 'integer') . ' ' .
153  'AND usr_id = ' . $ilDB->quote($a_usr_id, 'text');
154  $res = $ilDB->query($query);
155  if ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
156  return new ilECSCourseMemberAssignment((int) $row->id);
157  }
158  return null;
159  }
160 
161 
162  public function getId(): int
163  {
164  return $this->id;
165  }
166 
170  public function setServer(int $a_server): void
171  {
172  $this->server = $a_server;
173  }
174 
178  public function getServer(): int
179  {
180  return $this->server;
181  }
182 
183  public function setMid(int $a_mid): void
184  {
185  $this->mid = $a_mid;
186  }
187 
188  public function getMid(): int
189  {
190  return $this->mid;
191  }
192 
193  public function setCmsId(int $a_id): void
194  {
195  $this->cms_id = $a_id;
196  }
197 
198  public function getCmsId(): int
199  {
200  return $this->cms_id;
201  }
202 
203  public function setCmsSubId(?int $a_id): void
204  {
205  $this->cms_sub_id = $a_id;
206  }
207 
208  public function getCmsSubId(): ?int
209  {
210  return $this->cms_sub_id;
211  }
212 
213  public function setObjId(int $a_id): void
214  {
215  $this->obj_id = $a_id;
216  }
217 
218  public function getObjId(): int
219  {
220  return $this->obj_id;
221  }
222 
223  public function setUid(string $a_id): void
224  {
225  $this->uid = $a_id;
226  }
227 
228  public function getUid(): string
229  {
230  return $this->uid;
231  }
232 
233  public function setStatus(bool $a_status): void
234  {
235  $this->status = $a_status;
236  }
237 
238  public function getStatus(): bool
239  {
240  return $this->status;
241  }
242 
243  private function getArrayForDatabase(): array
244  {
245  return [
246  'id' => ['integer', $this->getId()],
247  'sid' => ['integer', $this->getServer()],
248  'mid' => ['integer', $this->getMid()],
249  'cms_id' => ['integer', $this->getCmsId()],
250  'cms_sub_id' => ['integer', $this->getCmsSubId()],
251  'obj_id' => ['integer', $this->getObjId()],
252  'usr_id' => ['text', $this->getUid()],
253  'status' => ['integer', $this->getStatus()],
254  ];
255  }
259  public function save(): bool
260  {
261  $this->id = $this->db->nextId('ecs_course_assignments');
262 
263  $assignment = self::lookupAssignment(
264  $this->getCmsId(),
265  $this->getCmsSubId(),
266  $this->getObjId(),
267  $this->getUid()
268  );
269  if ($assignment instanceof self) {
270  $assignment->update();
271  return true;
272  }
273  $this->db->insert('ecs_course_assignments', $this->getArrayForDatabase());
274  return true;
275  }
276 
280  public function update(): bool
281  {
282  $this->db->update('ecs_course_assignments', $this->getArrayForDatabase(), [
283  'id' => [
284  'integer',
285  $this->getId(),
286  ],
287  ]);
288  return true;
289  }
290 
294  public function delete(): bool
295  {
296  $query = 'DELETE FROM ecs_course_assignments ' .
297  'WHERE id = ' . $this->db->quote($this->getId(), 'integer');
298  $this->db->manipulate($query);
299  return true;
300  }
301 
302 
303 
307  protected function read(): bool
308  {
309  if (!$this->getId()) {
310  return false;
311  }
312  $r = $this->db->queryF(
313  "SELECT sid,mid,cms_id,cms_sub_id,obj_id,usr_id,status FROM ecs_course_assignments WHERE id = %d",
314  ['integer'],
315  [$this->getId()]
316  );
317  $row = $this->db->fetchObject($r);
318  $this->setServer((int) $row->sid);
319  $this->setMid((int) $row->mid);
320  $this->setCmsId((int) $row->cms_id);
321  $this->setCmsSubId((int) $row->cms_sub_id);
322  $this->setObjId((int) $row->obj_id);
323  $this->setUid($row->usr_id);
324  $this->setStatus((bool) $row->status);
325  return true;
326  }
327 }
$res
Definition: ltiservices.php:69
global $DIC
Definition: feed.php:28
static lookupAssignment(int $a_cms_id, ?int $a_cms_sub_id, int $a_obj_id, string $a_usr_id)
Lookup assignment of user.
static deleteByServerId(int $a_server_id)
Delete by server id.
static lookupUserIds(int $a_cms_id, ?int $a_cms_sub_id, int $a_obj_id)
Lookup user ids.
static deleteByObjId(int $a_obj_id)
Delete by obj_id.
Storage of ecs course assignments.
static lookupMissingAssignmentsOfUser(string $a_usr_id)
Lookup missing assignments;.
$r