Skip to content

Commit be806fa

Browse files
Forum: Fix phpstan issues + Add draft improvements
1 parent eee5c62 commit be806fa

5 files changed

+425
-533
lines changed

components/ILIAS/Forum/classes/Actions/class.ilForumAutoSaveAsyncDraftAction.php

+42-32
Original file line numberDiff line numberDiff line change
@@ -76,73 +76,83 @@ public function executeAndGetResponseObject(): stdClass
7676

7777
if ($draftId > 0) {
7878
if ('showreply' === $this->action) {
79-
$draftObj = ilForumPostDraft::newInstanceByDraftId($draftId);
80-
$draftObj->setPostSubject($subjectFormatterCallback($inputValues['subject']));
81-
$draftObj->setPostMessage(ilRTE::_replaceMediaObjectImageSrc($inputValues['message'], 0));
82-
$draftObj->setPostUserAlias($inputValues['alias']);
83-
$draftObj->setNotificationStatus((bool) $inputValues['notify']);
84-
$draftObj->setUpdateUserId($this->actor->getId());
85-
$draftObj->setPostAuthorId($this->actor->getId());
86-
$draftObj->setPostDisplayUserId(($this->forumProperties->isAnonymized() ? 0 : $this->actor->getId()));
87-
$draftObj->updateDraft();
79+
$draft = ilForumPostDraft::newInstanceByDraftId($draftId);
80+
81+
if ($draft->getPostAuthorId() !== $this->actor->getId()) {
82+
return $response;
83+
}
84+
85+
$draft->setPostSubject($subjectFormatterCallback($inputValues['subject']));
86+
$draft->setPostMessage(ilRTE::_replaceMediaObjectImageSrc($inputValues['message'], 0));
87+
$draft->setPostUserAlias($inputValues['alias']);
88+
$draft->setNotificationStatus((bool) $inputValues['notify']);
89+
$draft->setUpdateUserId($this->actor->getId());
90+
$draft->setPostAuthorId($this->actor->getId());
91+
$draft->setPostDisplayUserId(($this->forumProperties->isAnonymized() ? 0 : $this->actor->getId()));
92+
$draft->updateDraft();
8893

8994
$uploadedObjects = ilObjMediaObject::_getMobsOfObject('frm~:html', $this->actor->getId());
90-
$oldMediaObjects = ilObjMediaObject::_getMobsOfObject('frm~d:html', $draftObj->getDraftId());
95+
$oldMediaObjects = ilObjMediaObject::_getMobsOfObject('frm~d:html', $draft->getDraftId());
9196
$curMediaObjects = ilRTE::_getMediaObjects($inputValues['message'], 0);
9297

9398
$this->handleMedia(
9499
ilForumPostDraft::MEDIAOBJECT_TYPE,
95-
$draftObj->getDraftId(),
100+
$draft->getDraftId(),
96101
$uploadedObjects,
97102
$oldMediaObjects,
98103
$curMediaObjects
99104
);
100105
} else {
101-
$draftObj = new ilForumDraftsHistory();
102-
$draftObj->setDraftId($draftId);
103-
$draftObj->setPostSubject($subjectFormatterCallback($inputValues['subject']));
104-
$draftObj->setPostMessage(ilRTE::_replaceMediaObjectImageSrc($inputValues['message'], 0));
105-
$draftObj->addDraftToHistory();
106+
$draft = ilForumPostDraft::newInstanceByDraftId($draftId);
107+
if ($draft->getPostAuthorId() !== $this->actor->getId()) {
108+
return $response;
109+
}
110+
111+
$history_entry = new ilForumDraftsHistory();
112+
$history_entry->setDraftId($draft->getDraftId());
113+
$history_entry->setPostSubject($subjectFormatterCallback($inputValues['subject']));
114+
$history_entry->setPostMessage(ilRTE::_replaceMediaObjectImageSrc($inputValues['message'], 0));
115+
$history_entry->addDraftToHistory();
106116

107117
$uploadedObjects = ilObjMediaObject::_getMobsOfObject('frm~:html', $this->actor->getId());
108-
$oldMediaObjects = ilObjMediaObject::_getMobsOfObject('frm~d:html', $draftObj->getDraftId());
118+
$oldMediaObjects = ilObjMediaObject::_getMobsOfObject('frm~d:html', $history_entry->getDraftId());
109119
$curMediaObjects = ilRTE::_getMediaObjects($inputValues['message'], 0);
110120

111121
$this->handleMedia(
112122
ilForumDraftsHistory::MEDIAOBJECT_TYPE,
113-
$draftObj->getHistoryId(),
123+
$history_entry->getHistoryId(),
114124
$uploadedObjects,
115125
$oldMediaObjects,
116126
$curMediaObjects
117127
);
118128
}
119129
} else {
120-
$draftObj = new ilForumPostDraft();
121-
$draftObj->setForumId($this->relatedForumId);
122-
$draftObj->setThreadId($this->thread->getId());
123-
$draftObj->setPostId($relatedPostId);
124-
$draftObj->setPostSubject($subjectFormatterCallback($inputValues['subject']));
125-
$draftObj->setPostMessage(ilRTE::_replaceMediaObjectImageSrc($inputValues['message'], 0));
126-
$draftObj->setPostUserAlias($inputValues['alias']);
127-
$draftObj->setNotificationStatus((bool) $inputValues['notify']);
128-
$draftObj->setPostAuthorId($this->actor->getId());
129-
$draftObj->setPostDisplayUserId(($this->forumProperties->isAnonymized() ? 0 : $this->actor->getId()));
130-
$draftObj->saveDraft();
130+
$draft = new ilForumPostDraft();
131+
$draft->setForumId($this->relatedForumId);
132+
$draft->setThreadId($this->thread->getId());
133+
$draft->setPostId($relatedPostId);
134+
$draft->setPostSubject($subjectFormatterCallback($inputValues['subject']));
135+
$draft->setPostMessage(ilRTE::_replaceMediaObjectImageSrc($inputValues['message'], 0));
136+
$draft->setPostUserAlias($inputValues['alias']);
137+
$draft->setNotificationStatus((bool) $inputValues['notify']);
138+
$draft->setPostAuthorId($this->actor->getId());
139+
$draft->setPostDisplayUserId(($this->forumProperties->isAnonymized() ? 0 : $this->actor->getId()));
140+
$draft->saveDraft();
131141

132142
$uploadedObjects = ilObjMediaObject::_getMobsOfObject('frm~:html', $this->actor->getId());
133-
$oldMediaObjects = ilObjMediaObject::_getMobsOfObject('frm~d:html', $draftObj->getDraftId());
143+
$oldMediaObjects = ilObjMediaObject::_getMobsOfObject('frm~d:html', $draft->getDraftId());
134144
$curMediaObjects = ilRTE::_getMediaObjects($inputValues['message'], 0);
135145

136146
$this->handleMedia(
137147
ilForumPostDraft::MEDIAOBJECT_TYPE,
138-
$draftObj->getDraftId(),
148+
$draft->getDraftId(),
139149
$uploadedObjects,
140150
$oldMediaObjects,
141151
$curMediaObjects
142152
);
143153
}
144154

145-
$response->draft_id = $draftObj->getDraftId();
155+
$response->draft_id = $draft->getDraftId();
146156

147157
return $response;
148158
}

components/ILIAS/Forum/classes/Thread/ForumThreadTableSessionStorage.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ class ForumThreadTableSessionStorage
4040
private Factory $refinery;
4141

4242
public function __construct(
43-
private readonly int $forum_ref_id,
43+
private readonly int $forum_ref_id,
4444
private readonly bool $is_moderator,
45-
?WrapperFactory $http_wrapper = null,
46-
?Factory $refinery = null
45+
?WrapperFactory $http_wrapper = null,
46+
?Factory $refinery = null
4747
) {
4848
global $DIC;
4949
$this->http_wrapper = $http_wrapper ?? $DIC->http()->wrapper();

components/ILIAS/Forum/classes/class.ilForumDraftsHistory.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,9 @@ public function delete(): void
158158
);
159159
}
160160

161-
public function getFirstAutosaveByDraftId(int $draft_id): void
161+
public function populateWithFirstAutosaveByDraftId(int $draft_id): void
162162
{
163+
$this->db->setLimit(1);
163164
$res = $this->db->queryF(
164165
'SELECT * FROM frm_drafts_history WHERE draft_id = %s ORDER BY history_id ASC',
165166
['integer'],
@@ -176,6 +177,7 @@ public function getFirstAutosaveByDraftId(int $draft_id): void
176177

177178
public function getLastAutosaveByDraftId(int $draft_id): void
178179
{
180+
$this->db->setLimit(1);
179181
$res = $this->db->queryF(
180182
'SELECT * FROM frm_drafts_history WHERE draft_id = %s ORDER BY history_id DESC',
181183
['integer'],

components/ILIAS/Forum/classes/class.ilForumPostDraft.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function __construct(int $user_id = 0, int $post_id = 0, int $draft_id =
6363
}
6464
}
6565

66-
protected static function populateWithDatabaseRecord(ilForumPostDraft $draft, array $row): void
66+
private static function populateWithDatabaseRecord(ilForumPostDraft $draft, array $row): void
6767
{
6868
$draft->setDraftId((int) $row['draft_id']);
6969
$draft->setForumId((int) $row['forum_id']);
@@ -261,10 +261,10 @@ protected static function readDrafts(int $user_id): void
261261
];
262262

263263
while ($row = $ilDB->fetchAssoc($res)) {
264-
$tmp_obj = new ilForumPostDraft();
265-
self::populateWithDatabaseRecord($tmp_obj, $row);
266-
self::$instances[$user_id][$row['thread_id']][$tmp_obj->getPostId()][] = $tmp_obj;
267-
self::$instances[$user_id]['draft_ids'][$tmp_obj->getDraftId()] = $tmp_obj;
264+
$draft = new ilForumPostDraft();
265+
self::populateWithDatabaseRecord($draft, $row);
266+
self::$instances[$user_id][$row['thread_id']][$draft->getPostId()][] = $draft;
267+
self::$instances[$user_id]['draft_ids'][$draft->getDraftId()] = $draft;
268268
}
269269
}
270270

@@ -314,7 +314,7 @@ public static function getSortedDrafts(
314314
}
315315

316316
/**
317-
* @return ilForumPostDraft[]
317+
* @return array<int, ilForumPostDraft>
318318
*/
319319
public static function getDraftInstancesByUserId(int $user_id): array
320320
{

0 commit comments

Comments
 (0)