1313use Chamilo \CoreBundle \Entity \ResourceNode ;
1414use Chamilo \CoreBundle \Entity \Session ;
1515use Chamilo \CoreBundle \Entity \User ;
16+ use Chamilo \CoreBundle \Settings \SettingsManager ;
1617use Chamilo \CourseBundle \Entity \CForum ;
1718use Chamilo \CourseBundle \Entity \CForumNotification ;
1819use Chamilo \CourseBundle \Entity \CForumPost ;
@@ -37,12 +38,16 @@ final class ForumThreadPostsStateProvider implements ProviderInterface
3738{
3839 use ForumStateHelperTrait;
3940
41+ /** @var array<int, string> */
42+ private array $ posterAvatarUrlByUserId = [];
43+
4044 public function __construct (
4145 private readonly RequestStack $ requestStack ,
4246 private readonly EntityManagerInterface $ entityManager ,
4347 private readonly CForumThreadRepository $ threadRepository ,
4448 private readonly CForumAttachmentRepository $ attachmentRepository ,
4549 private readonly Security $ security ,
50+ private readonly SettingsManager $ settingsManager ,
4651 ) {}
4752
4853 /**
@@ -144,6 +149,8 @@ public function getThreadPosts(int $threadId, Request $request): array
144149
145150 $ posts = $ postsQueryBuilder ->getQuery ()->getResult ();
146151
152+ $ showPosterAvatar = $ this ->arePosterImagesAllowed ($ course );
153+
147154 return [
148155 'forum ' => $ this ->serializeForum ($ forum ),
149156 'thread ' => $ this ->serializeThread (
@@ -157,7 +164,13 @@ public function getThreadPosts(int $threadId, Request $request): array
157164 'canReply ' => $ this ->canReply ($ forum , $ thread ),
158165 'canManageThread ' => $ canManage ,
159166 'posts ' => array_map (
160- fn (CForumPost $ post ): array => $ this ->serializePost ($ post , $ forum , $ thread , $ canManage ),
167+ fn (CForumPost $ post ): array => $ this ->serializePost (
168+ $ post ,
169+ $ forum ,
170+ $ thread ,
171+ $ canManage ,
172+ $ showPosterAvatar ,
173+ ),
161174 $ posts ,
162175 ),
163176 ];
@@ -189,6 +202,22 @@ private function areForumPostNotificationsHidden(Course $course): bool
189202 return 1 === (int ) api_get_course_setting ('hide_forum_notifications ' , $ course );
190203 }
191204
205+ private function arePosterImagesAllowed (Course $ course ): bool
206+ {
207+ if (!\function_exists ('api_get_course_setting ' )) {
208+ return false ;
209+ }
210+
211+ return 1 === (int ) api_get_course_setting ('allow_user_image_forum ' , $ course );
212+ }
213+
214+ private function shouldHideForumPostRevisionLanguage (): bool
215+ {
216+ return $ this ->isTruthySetting (
217+ $ this ->settingsManager ->getSetting ('forum.hide_forum_post_revision_language ' , true ),
218+ );
219+ }
220+
192221 private function isSubscribedToThread (Course $ course , User $ user , int $ threadId ): bool
193222 {
194223 return null !== $ this ->entityManager ->getRepository (CForumNotification::class)->findOneBy ([
@@ -254,8 +283,13 @@ private function serializeThread(
254283 /**
255284 * @return array<string, mixed>
256285 */
257- private function serializePost (CForumPost $ post , CForum $ forum , CForumThread $ thread , bool $ canManage ): array
258- {
286+ private function serializePost (
287+ CForumPost $ post ,
288+ CForum $ forum ,
289+ CForumThread $ thread ,
290+ bool $ canManage ,
291+ bool $ showPosterAvatar ,
292+ ): array {
259293 $ canEdit = $ this ->canEditPost ($ post , $ forum , $ thread , $ canManage );
260294 $ attachments = [];
261295
@@ -275,7 +309,8 @@ private function serializePost(CForumPost $post, CForum $forum, CForumThread $th
275309 $ currentUser = $ this ->security ->getUser ();
276310 $ isAuthor = $ currentUser instanceof User && $ post ->getUser ()->getId () === $ currentUser ->getId ();
277311 $ revisionRequested = $ this ->postNeedsRevision ($ post );
278- $ revisionLanguage = $ this ->getPostRevisionLanguage ($ post );
312+ $ revisionLanguage = $ this ->shouldHideForumPostRevisionLanguage () ? '' : $ this ->getPostRevisionLanguage ($ post );
313+ $ posterAvatarUrl = $ showPosterAvatar ? $ this ->getPosterAvatarUrl ($ post ->getUser ()) : '' ;
279314
280315 return [
281316 'iid ' => $ post ->getIid (),
@@ -287,6 +322,8 @@ private function serializePost(CForumPost $post, CForum $forum, CForumThread $th
287322 'status ' => $ status ,
288323 'statusLabel ' => $ this ->getPostStatusLabel ($ status ),
289324 'posterFullName ' => $ post ->getPosterFullName (),
325+ 'posterAvatarUrl ' => $ posterAvatarUrl ,
326+ 'showPosterAvatar ' => $ showPosterAvatar && '' !== $ posterAvatarUrl ,
290327 'canEdit ' => $ canEdit ,
291328 'canDelete ' => $ canEdit ,
292329 'canApprove ' => $ canManage && CForumPost::STATUS_VALIDATED !== $ status ,
@@ -328,6 +365,29 @@ private function getPostRevisionLanguage(CForumPost $post): string
328365 return (string ) ($ this ->getExtraFieldValue ('forum_post ' , (int ) $ post ->getIid (), 'revision_language ' ) ?? '' );
329366 }
330367
368+ private function getPosterAvatarUrl (?User $ user ): string
369+ {
370+ if (!$ user instanceof User || !\function_exists ('api_get_user_info ' )) {
371+ return '' ;
372+ }
373+
374+ $ userId = (int ) $ user ->getId ();
375+ if (isset ($ this ->posterAvatarUrlByUserId [$ userId ])) {
376+ return $ this ->posterAvatarUrlByUserId [$ userId ];
377+ }
378+
379+ $ userInfo = api_get_user_info ($ userId , false , false , false , false , true );
380+ if (!\is_array ($ userInfo )) {
381+ $ this ->posterAvatarUrlByUserId [$ userId ] = '' ;
382+
383+ return '' ;
384+ }
385+
386+ $ this ->posterAvatarUrlByUserId [$ userId ] = (string ) ($ userInfo ['avatar_small ' ] ?? $ userInfo ['avatar ' ] ?? '' );
387+
388+ return $ this ->posterAvatarUrlByUserId [$ userId ];
389+ }
390+
331391 private function isReportAvailableForCurrentRequest (): bool
332392 {
333393 $ request = $ this ->requestStack ->getCurrentRequest ();
0 commit comments