ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
TodoRepository.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24
26{
27 protected ilDBInterface $db;
28
29 public function __construct(ilDBInterface $db)
30 {
31 $this->db = $db;
32 }
33
38 public function getItemsOfUser(int $user_id): array
39 {
40 $items = [];
41
42 $query = "SELECT * FROM todo_items WHERE user_id = %s";
43
44 $result = $this->db->queryF($query, ['integer'], [$user_id]);
45
46 while ($row = $this->db->fetchAssoc($result)) {
47 $items[] = new TodoItem(
48 $row['item_id'],
49 $row['user_id'],
50 $row['title'],
51 $row['description'] ?? null,
52 $row['deadline'] ?? null
53 );
54 }
55 return $items;
56 }
57
62 public function createItem(TodoItem $item): TodoItem
63 {
64 $todo_id = $this->db->nextId('todo_items');
65
66 $this->db->insert('todo_items', [
67 'todo_id' => ['integer', $todo_id],
68 'user_id' => ['integer', $item->getUserId()],
69 'title' => ['text', $item->getTitle()],
70 'description' => ['clob', $item->getDescription()],
71 'deadline' => ['date', $item->getDeadline()]
72 ]);
73
74 return $item->widthTodoId($todo_id);
75 }
76
80 public function updateItem(TodoItem $item): void
81 {
82 $this->db->update('todo_items', [
83 'user_id' => ['integer', $item->getUserId()],
84 'title' => ['text', $item->getTitle()],
85 'description' => ['clob', $item->getDescription()],
86 'deadline' => ['date', $item->getDeadline()]
87 ], [
88 'todo_id' => ['integer', $item->getTodoId()]
89 ]);
90
91 }
92
93
94 public function deleteItem(ToDoItem $item): void
95 {
96 $query = "DELETE FROM todo_items WHERE todo_id = %s";
97
98 $this->db->manipulateF($query, ['integer'], [$item->getTodoId()]);
99 }
100}
getUserId()
Get the id of the user to which the item belongs.
Definition: TodoItem.php:45
getDescription()
Get a description of the item (optional)
Definition: TodoItem.php:61
getDeadline()
Get the deadline of the item (optional)
Definition: TodoItem.php:69
widthTodoId(int $todo_id)
Get a clone with a new id.
Definition: TodoItem.php:77
getTitle()
Get the title of the item.
Definition: TodoItem.php:53
getTodoId()
Get the id of the item.
Definition: TodoItem.php:37
createItem(TodoItem $item)
Create an item in the database The returned item has an automatically created id.
getItemsOfUser(int $user_id)
Get the items of a user.
updateItem(TodoItem $item)
Update an item in the database.
Interface ilDBInterface.