From 20c942d78e089c4b455147403dfff76f581afce0 Mon Sep 17 00:00:00 2001 From: jmontoyaa Date: Thu, 12 Oct 2017 12:06:47 +0200 Subject: [PATCH] Allow anons to add posts (if setting is on) see BT#13516 --- main/forum/forumfunction.inc.php | 16 ++++++++++++++-- main/forum/viewthread_flat.inc.php | 25 ++++++++++++++++--------- main/forum/viewthread_nested.inc.php | 22 +++++++++++++--------- 3 files changed, 43 insertions(+), 20 deletions(-) diff --git a/main/forum/forumfunction.inc.php b/main/forum/forumfunction.inc.php index e272fc13eb..f4411a0875 100755 --- a/main/forum/forumfunction.inc.php +++ b/main/forum/forumfunction.inc.php @@ -5,6 +5,7 @@ use ChamiloSession as Session; use Doctrine\Common\Collections\Criteria; use Chamilo\CourseBundle\Entity\CForumPost; use Chamilo\CourseBundle\Entity\CForumThread; +use Chamilo\UserBundle\Entity\User; /** * These files are a complete rework of the forum. The database structure is @@ -2074,14 +2075,15 @@ function getPosts( $posterId = $post->getPosterId(); if (!empty($posterId)) { + /** @var User $user */ $user = $em->find('ChamiloUserBundle:User', $posterId); - if ($user) { $postInfo['user_id'] = $user->getUserId(); $postInfo['username'] = $user->getUsername(); $postInfo['username_canonical'] = $user->getUsernameCanonical(); $postInfo['lastname'] = $user->getLastname(); $postInfo['firstname'] = $user->getFirstname(); + $postInfo['complete_name'] = $user->getCompleteName(); } } @@ -3412,6 +3414,16 @@ function store_reply($current_forum, $values, $courseId = 0, $userId = 0) $post_date = api_get_utc_datetime(); $userId = $userId ?: api_get_user_id(); + if ($current_forum['allow_anonymous'] == 1) { + if (api_is_anonymous() && empty($userId)) { + $userId = api_get_anonymous_id(); + } + } + + if (empty($userId)) { + return false; + } + if ($current_forum['approval_direct_post'] == '1' && !api_is_allowed_to_edit(null, true) ) { @@ -3421,7 +3433,7 @@ function store_reply($current_forum, $values, $courseId = 0, $userId = 0) } $upload_ok = 1; - $return = array(); + $return = []; if ($upload_ok) { // We first store an entry in the forum_post table. diff --git a/main/forum/viewthread_flat.inc.php b/main/forum/viewthread_flat.inc.php index d4a8462b21..3c4da73c41 100755 --- a/main/forum/viewthread_flat.inc.php +++ b/main/forum/viewthread_flat.inc.php @@ -42,13 +42,20 @@ if (isset($current_thread['thread_id'])) { if (!empty($rows)) { $postCount = count($rows); foreach ($rows as $row) { - if ($row['user_id'] == '0') { + $posterId = isset($row['user_id']) ? $row['user_id'] : 0; + $name = ''; + if (empty($posterId)) { $name = prepare4display($row['poster_name']); } else { - $name = api_get_person_name($row['firstname'], $row['lastname']); + if (isset($row['complete_name'])) { + $name = $row['complete_name']; + } } - $username = sprintf(get_lang('LoginX'), $row['username']); + $username = ''; + if (isset($row['username'])) { + $username = sprintf(get_lang('LoginX'), $row['username']); + } if (($current_forum_category && $current_forum_category['locked'] == 0) && $current_forum['locked'] == 0 && @@ -58,8 +65,8 @@ if (isset($current_thread['thread_id'])) { if ($_user['user_id'] || ($current_forum['allow_anonymous'] == 1 && !$_user['user_id']) ) { - if (!api_is_anonymous() && - api_is_allowed_to_session_edit(false, true) + if ((api_is_anonymous() && $current_forum['allow_anonymous'] == 1) || + (!api_is_anonymous() && api_is_allowed_to_session_edit(false, true)) ) { $buttonReply = Display::toolbarButton( get_lang('ReplyToMessage'), @@ -120,16 +127,16 @@ if (isset($current_thread['thread_id'])) { if ($origin != 'learnpath') { if (api_get_course_setting('allow_user_image_forum')) { - $html .= '
'.display_user_image($row['user_id'], $name, $origin).'
'; + $html .= '
'.display_user_image($posterId, $name, $origin).'
'; } $html .= Display::tag( 'h4', - display_user_link($row['user_id'], $name), + display_user_link($posterId, $name), array('class' => 'title-username') ); } else { if (api_get_course_setting('allow_user_image_forum')) { - $html .= '
'.display_user_image($row['user_id'], $name, $origin).'
'; + $html .= '
'.display_user_image($posterId, $name, $origin).'
'; } $name = Display::tag('strong', "#".$postCount--, ['class' => 'text-info'])." | $name"; $html .= Display::tag( @@ -235,7 +242,7 @@ if (isset($current_thread['thread_id'])) { } } - $user_status = api_get_status_of_user_in_course($row['user_id'], api_get_course_int_id()); + $user_status = api_get_status_of_user_in_course($posterId, api_get_course_int_id()); $current_qualify_thread = showQualify('1', $row['poster_id'], $_GET['thread']); if (($current_thread['thread_peer_qualify'] == 1 || api_is_allowed_to_edit(null, true)) && $current_thread['thread_qualify_max'] > 0 && $origin != 'learnpath' diff --git a/main/forum/viewthread_nested.inc.php b/main/forum/viewthread_nested.inc.php index 28fceb2026..aa816d760b 100755 --- a/main/forum/viewthread_nested.inc.php +++ b/main/forum/viewthread_nested.inc.php @@ -37,6 +37,8 @@ $postCount = 1; $allowUserImageForum = api_get_course_setting('allow_user_image_forum'); foreach ($posts as $post) { + $posterId = isset($post['user_id']) ? $post['user_id'] : 0; + // The style depends on the status of the message: approved or not. if ($post['visible'] == '0') { $titleclass = 'forum_message_post_title_2_be_approved'; @@ -56,27 +58,29 @@ foreach ($posts as $post) { $html .= '
'; $html .= '
'; $html .= '
'; - - $username = sprintf(get_lang('LoginX'), $post['username']); - if ($post['user_id'] == '0') { + $username = ''; + if (isset($post['username'])) { + $username = sprintf(get_lang('LoginX'), $post['username']); + } + if (empty($posterId)) { $name = $post['poster_name']; } else { - $name = api_get_person_name($post['firstname'], $post['lastname']); + $name = $post['complete_name']; } if ($origin != 'learnpath') { if ($allowUserImageForum) { - $html .= '
'.display_user_image($post['user_id'], $name, $origin).'
'; + $html .= '
'.display_user_image($posterId, $name, $origin).'
'; } $html .= Display::tag( 'h4', - display_user_link($post['user_id'], $name, $origin, $username), + display_user_link($posterId, $name, $origin, $username), array('class' => 'title-username') ); } else { if ($allowUserImageForum) { - $html .= '
'.display_user_image($post['user_id'], $name, $origin).'
'; + $html .= '
'.display_user_image($posterId, $name, $origin).'
'; } $html .= Display::tag( @@ -115,7 +119,7 @@ foreach ($posts as $post) { $tutorGroup = GroupManager::is_tutor_of_group(api_get_user_id(), $groupInfo); if ((isset($groupInfo['iid']) && $tutorGroup) || - ($current_forum['allow_edit'] == 1 && $post['user_id'] == $userId) || + ($current_forum['allow_edit'] == 1 && $posterId == $userId) || (api_is_allowed_to_edit(false, true) && !(api_is_session_general_coach() && $current_forum['session_id'] != $sessionId)) ) { @@ -197,7 +201,7 @@ foreach ($posts as $post) { if ($count > 0) { $current_qualify_thread = showQualify( '1', - $post['user_id'], + $posterId, $_GET['thread'] ); if ($locked == false) {