ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.PostingDBRepository.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\Blog\Posting;
22 
23 use ilDBInterface;
24 use ilDateTime;
26 
28 {
29  public function __construct(
30  protected ilDBInterface $db,
31  protected InternalDataService $data
32  ) {
33  }
34 
35  protected function getPostingFromRecord(array $rec): Posting
36  {
37  return $this->data->posting(
38  (int) $rec['id'],
39  (int) $rec['blog_id'],
40  (string) $rec['title'],
41  new ilDateTime($rec['created'], IL_CAL_DATETIME),
42  (int) $rec['author'],
43  (bool) $rec['approved'],
44  $rec['last_withdrawn'] !== null
45  ? new ilDateTime($rec['last_withdrawn'], IL_CAL_DATETIME)
46  : null
47  );
48  }
49 
50  public function create(Posting $posting): int
51  {
52  $id = $this->db->nextId('il_blog_posting');
53  $this->db->insert('il_blog_posting', [
54  'id' => ['integer', $id],
55  'blog_id' => ['integer', $posting->getBlogId()],
56  'title' => ['text', $posting->getTitle()],
57  'created' => ['timestamp', $posting->getCreated()->get(IL_CAL_DATETIME)],
58  'author' => ['integer', $posting->getAuthor()],
59  'approved' => ['integer', $posting->isApproved()],
60  'last_withdrawn' => ['timestamp', $posting->getLastWithdrawn()?->get(IL_CAL_DATETIME)],
61  ]);
62  return $id;
63  }
64 
65  public function update(Posting $posting): void
66  {
67  $this->db->update('il_blog_posting', [
68  'title' => ['text', $posting->getTitle()],
69  'created' => ['timestamp', $posting->getCreated()->get(IL_CAL_DATETIME)],
70  'approved' => ['integer', $posting->isApproved()],
71  'last_withdrawn' => ['timestamp', $posting->getLastWithdrawn()?->get(IL_CAL_DATETIME)],
72  ], [
73  'id' => ['integer', $posting->getId()],
74  ]);
75  }
76 
77  public function getById(int $id): ?Posting
78  {
79  $set = $this->db->queryF(
80  'SELECT * FROM il_blog_posting WHERE id = %s',
81  ['integer'],
82  [$id]
83  );
84  if ($rec = $this->db->fetchAssoc($set)) {
85  return $this->getPostingFromRecord($rec);
86  }
87  return null;
88  }
89 
90  public function delete(int $id): void
91  {
92  $this->db->manipulateF(
93  'DELETE FROM il_blog_posting WHERE id = %s',
94  ['integer'],
95  [$id]
96  );
97  }
98 
99  public function deleteAllBlogPostings(int $blog_id): void
100  {
101  $this->db->manipulateF(
102  'DELETE FROM il_blog_posting WHERE blog_id = %s',
103  ['integer'],
104  [$blog_id]
105  );
106  }
107 
108  public function lookupBlogId(int $posting_id): ?int
109  {
110  $set = $this->db->queryF(
111  'SELECT blog_id FROM il_blog_posting WHERE id = %s',
112  ['integer'],
113  [$posting_id]
114  );
115  if ($rec = $this->db->fetchAssoc($set)) {
116  return (int) $rec['blog_id'];
117  }
118  return null;
119  }
120 
121  public function getAllByBlog(int $blog_id, int $limit = 1000, int $offset = 0): array
122  {
123  if ($limit) {
124  $this->db->setLimit($limit, $offset);
125  }
126  $set = $this->db->queryF(
127  'SELECT * FROM il_blog_posting WHERE blog_id = %s ORDER BY created DESC',
128  ['integer'],
129  [$blog_id]
130  );
131  $posts = [];
132  while ($rec = $this->db->fetchAssoc($set)) {
133  $posts[] = $this->getPostingFromRecord($rec);
134  }
135  return $posts;
136  }
137 
138  public function exists(int $blog_id, int $posting_id): bool
139  {
140  $set = $this->db->queryF(
141  'SELECT id FROM il_blog_posting WHERE blog_id = %s AND id = %s',
142  ['integer', 'integer'],
143  [$blog_id, $posting_id]
144  );
145  return $this->db->numRows($set) > 0;
146  }
147 
148  public function getLastPost(int $blog_id): int
149  {
150  $all = $this->getAllByBlog($blog_id, 1, 0);
151  return $all ? $all[0]->getId() : 0;
152  }
153 
154  public function searchBlogsByAuthor(int $user_id): array
155  {
156  $ids = [];
157  $set = $this->db->queryF(
158  'SELECT DISTINCT(blog_id) FROM il_blog_posting WHERE author = %s',
159  ['integer'],
160  [$user_id]
161  );
162  while ($row = $this->db->fetchAssoc($set)) {
163  $ids[] = (int) $row['blog_id'];
164  }
165  return $ids;
166  }
167 }
const IL_CAL_DATETIME
__construct(protected ilDBInterface $db, protected InternalDataService $data)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
getAllByBlog(int $blog_id, int $limit=1000, int $offset=0)