ILIAS  trunk Revision v12.0_alpha-1329-g1094ddb0c33
class.ilBlogPosting.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
23
30{
31 protected string $title = "";
32 protected ?ilDateTime $created = null;
33 protected int $blog_node_id = 0;
34 protected bool $blog_node_is_wsp = false;
35 protected int $author = 0;
36 protected bool $approved = false;
37 protected ?ilDateTime $withdrawn = null;
40
41 public function afterConstructor(): void
42 {
43 global $DIC;
44 $this->internal_data = $DIC->blog()->internal()->data();
45 $this->posting_manager = $DIC->blog()->internal()->domain()->posting();
46 }
47
48 public function getParentType(): string
49 {
50 return "blp";
51 }
52
53 public function setTitle(string $a_title): void
54 {
55 $this->title = $a_title;
56 }
57
58 public function getTitle(): string
59 {
60 return $this->title;
61 }
62
63 public function setBlogId(int $a_id): void
64 {
65 $this->setParentId($a_id);
66 }
67
68 public function getBlogId(): int
69 {
70 return $this->getParentId();
71 }
72
73 public function setCreated(ilDateTime $a_date): void
74 {
75 $this->created = $a_date;
76 }
77
78 public function getCreated(): ilDateTime
79 {
80 return $this->created;
81 }
82
83 public function setAuthor(int $a_id): void
84 {
85 $this->author = $a_id;
86 }
87
88 public function getAuthor(): int
89 {
90 return $this->author;
91 }
92
93 public function setApproved(bool $a_status): void
94 {
95 $this->approved = $a_status;
96 }
97
98 public function isApproved(): bool
99 {
100 return $this->approved;
101 }
102
106 public function setWithdrawn(
107 ilDateTime $a_date
108 ): void {
109 $this->withdrawn = $a_date;
110 }
111
115 public function getWithdrawn(): ?ilDateTime
116 {
117 return $this->withdrawn;
118 }
119
123 public function create(
124 bool $a_import = false
125 ): void {
126 $data = $this->internal_data;
127
128 if (!$a_import) {
129 $created = ilUtil::now();
130 } else {
131 $created = $this->getCreated()->get(IL_CAL_DATETIME);
132 }
133
134 $post = $data->posting(
135 0,
136 $this->getBlogId(),
137 $this->getTitle(),
138 new ilDateTime($created, IL_CAL_DATETIME),
139 $this->getAuthor(),
140 $this->isApproved(),
141 $this->getWithdrawn()
142 );
143 $id = $this->posting_manager->create($post);
144 $this->setId($id);
145
146 if (!$a_import) {
147 parent::create($a_import);
148 }
149 }
150
151 public function update(
152 bool $a_validate = true,
153 bool $a_no_history = false,
154 bool $a_notify = true,
155 string $a_notify_action = "update"
156 ): array|bool {
157 $data = $this->internal_data;
158 $post = $data->posting(
159 $this->getId(),
160 $this->getBlogId(),
161 $this->getTitle(),
162 $this->getCreated(),
163 $this->getAuthor(),
164 $this->isApproved(),
165 $this->getWithdrawn()
166 );
167 $this->posting_manager->update($post);
168
169 $ret = parent::update($a_validate, $a_no_history);
170
171 if ($a_notify && $this->getActive()) {
173 $a_notify_action,
174 $this->blog_node_is_wsp,
175 $this->blog_node_id,
176 $this->getId()
177 );
178 }
179
180 return $ret;
181 }
182
186 public function read(): void
187 {
188 $ilDB = $this->db;
189
190 $query = "SELECT * FROM il_blog_posting" .
191 " WHERE id = " . $ilDB->quote($this->getId(), "integer");
192 $set = $ilDB->query($query);
193 $rec = $ilDB->fetchAssoc($set);
194
195 $this->setTitle($rec["title"]);
196 $this->setBlogId($rec["blog_id"]);
197 $this->setCreated(new ilDateTime($rec["created"], IL_CAL_DATETIME));
198 $this->setAuthor($rec["author"]);
199 if ($rec["approved"]) {
200 $this->setApproved(true);
201 }
202 $this->setWithdrawn(new ilDateTime($rec["last_withdrawn"], IL_CAL_DATETIME));
203
204 // when posting is deactivated it should loose the approval
205 $this->addUpdateListener($this, "checkApproval");
206
207 parent::read();
208 }
209
210 public function checkApproval(): void
211 {
212 if (!$this->getActive() && $this->isApproved()) {
213 $this->approved = false;
214 $this->update();
215 }
216 }
217
221 public function delete(): void
222 {
223 $ilDB = $this->db;
224
226 $this->getBlogId(),
227 "blog",
228 $this->getId(),
229 $this->getParentType()
230 );
231
232 $this->posting_manager->delete($this->getId());
233
234 parent::delete();
235 }
236
240 public function unpublish(): void
241 {
242 $this->setApproved(false);
243 $this->setActive(false);
244 $this->setWithdrawn(new ilDateTime(ilUtil::now(), IL_CAL_DATETIME));
245 $this->update(true, false, false);
246
248 $this->getBlogId(),
249 "blog",
250 $this->getId(),
251 $this->getParentType()
252 );
253 }
254
258 public static function deleteAllBlogPostings(
259 int $a_blog_id
260 ): void {
261 global $DIC;
262
263 $lom_services = $DIC->learningObjectMetadata();
264 $mgr = $DIC->blog()->internal()->domain()->posting();
265 foreach ($mgr->getAllByBlog($a_blog_id, 0) as $posting) {
266 $lom_services->deleteAll($a_blog_id, $posting->getId(), "blp");
267 $post = new ilBlogPosting($posting->getId());
268 $post->delete();
269 }
270 }
271
272 public static function lookupBlogId(
273 int $a_posting_id
274 ): ?int {
275 global $DIC;
276 return $DIC->blog()->internal()->domain()->posting()->lookupBlogId($a_posting_id);
277 }
278
282 public static function getAllPostings(
283 int $a_blog_id,
284 int $a_limit = 1000,
285 int $a_offset = 0
286 ): array {
287 global $DIC;
288
289 $pages = parent::getAllPages("blp", $a_blog_id);
290 $posts = [];
291 foreach ($DIC->blog()->internal()->domain()->posting()->getAllByBlog(
292 $a_blog_id,
293 $a_limit,
294 $a_offset
295 ) as $posting) {
296 $id = $posting->getId();
297 if (isset($pages[$id])) {
298 $posts[$id] = $pages[$id];
299 $posts[$id]["title"] = $posting->getTitle();
300 $posts[$id]["created"] = $posting->getCreated();
301 $posts[$id]["author"] = $posting->getAuthor();
302 $posts[$id]["approved"] = $posting->isApproved();
303 $posts[$id]["last_withdrawn"] = $posting->getLastWithdrawn();
304
305 foreach (self::getPageContributors("blp", $id) as $editor) {
306 if ($editor["user_id"] != $posting->getAuthor()) {
307 $posts[$id]["editors"][] = $editor["user_id"];
308 }
309 }
310 }
311 }
312
313 return $posts;
314 }
315
319 public static function exists(
320 int $a_blog_id,
321 int $a_posting_id
322 ): bool {
323 global $DIC;
324 return $DIC->blog()->internal()->domain()->posting()->exists(
325 $a_blog_id,
326 $a_posting_id
327 );
328 }
329
333 public static function getLastPost(
334 int $a_blog_id
335 ): int {
336 global $DIC;
337 return $DIC->blog()->internal()->domain()->posting()->getLastPost($a_blog_id);
338 }
339
343 public function setBlogNodeId(
344 int $a_id,
345 bool $a_is_in_workspace = false
346 ): void {
347 $this->blog_node_id = $a_id;
348 $this->blog_node_is_wsp = $a_is_in_workspace;
349 }
350
351 public function getNotificationAbstract(): string
352 {
353 $snippet = ilBlogPostingGUI::getSnippet($this->getId(), true);
354
355 // making things more readable
356 $snippet = str_replace(array('<br/>', '<br />', '</p>', '</div>'), "\n", $snippet);
357
358 return trim(strip_tags($snippet));
359 }
360
361 // keywords
362 public function updateKeywords(
363 array $keywords
364 ): void {
365 $this->lom_services->manipulate($this->getBlogId(), $this->getId(), "blp")
366 ->prepareDelete($this->lom_services->paths()->keywords())
367 ->prepareCreateOrUpdate($this->lom_services->paths()->keywords(), ...$keywords)
368 ->execute();
369 }
370
371 public static function getKeywords(
372 int $a_obj_id,
373 int $a_posting_id
374 ): array {
375 global $DIC;
376
377 $lom_services = $DIC->learningObjectMetadata();
378
379 $result = [];
380 $keywords = $lom_services->read($a_obj_id, $a_posting_id, "blp")
381 ->allData($lom_services->paths()->keywords());
382 foreach ($keywords as $keyword) {
383 if ($keyword->value() !== "") {
384 $result[] = $keyword->value();
385 }
386 }
387
388 return $result;
389 }
390}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
const IL_CAL_DATETIME
static getSnippet(int $a_id, bool $a_truncate=false, int $a_truncate_length=500, string $a_truncate_sign="...", bool $a_include_picture=false, int $a_picture_width=144, int $a_picture_height=144, ?string $a_export_directory=null)
Get first text paragraph of page.
Class ilBlogPosting.
static exists(int $a_blog_id, int $a_posting_id)
Checks whether a posting exists.
update(bool $a_validate=true, bool $a_no_history=false, bool $a_notify=true, string $a_notify_action="update")
create(bool $a_import=false)
Create new blog posting.
setApproved(bool $a_status)
setBlogNodeId(int $a_id, bool $a_is_in_workspace=false)
Set blog node id (needed for notification)
static getAllPostings(int $a_blog_id, int $a_limit=1000, int $a_offset=0)
Get all postings of blog.
updateKeywords(array $keywords)
static lookupBlogId(int $a_posting_id)
static getKeywords(int $a_obj_id, int $a_posting_id)
static getLastPost(int $a_blog_id)
Get newest posting for blog.
PostingManager $posting_manager
InternalDataService $internal_data
setCreated(ilDateTime $a_date)
setWithdrawn(ilDateTime $a_date)
Set last withdrawal date.
static deleteAllBlogPostings(int $a_blog_id)
Delete all postings for blog.
setTitle(string $a_title)
getWithdrawn()
Get last withdrawal date.
read()
Read blog posting.
@classDescription Date and time handling
get(int $a_format, string $a_format_str='', string $a_tz='')
get formatted date
static deleteNewsOfContext(int $a_context_obj_id, string $a_context_obj_type, int $a_context_sub_obj_id=0, string $a_context_sub_obj_type="")
Delete all news of a context.
static sendNotification(string $a_action, bool $a_in_wsp, int $a_blog_node_id, int $a_posting_id, ?string $a_comment=null)
Class ilPageObject Handles PageObjects of ILIAS Learning Modules (see ILIAS DTD)
setParentId(int $a_id)
static now()
Return current timestamp in Y-m-d H:i:s format.
$post
Definition: ltitoken.php:46
global $DIC
Definition: shib_login.php:26