diff --git a/main/inc/lib/social.lib.php b/main/inc/lib/social.lib.php index 0c33a0caef..f31685384b 100755 --- a/main/inc/lib/social.lib.php +++ b/main/inc/lib/social.lib.php @@ -1640,13 +1640,12 @@ class SocialManager extends UserManager "; if ($getCount) { - $select = ' SELECT count(id) count '; + $select = ' SELECT count(id) as count_items '; } - $sql = "$select - FROM $tblMessage m - WHERE - msg_status <> ".MESSAGE_STATUS_WALL_DELETE.' AND '; + $sqlBase = "$select FROM $tblMessage m WHERE "; + $sql = []; + $sql[1] = $sqlBase."msg_status <> ".MESSAGE_STATUS_WALL_DELETE.' AND '; // Get my own posts $userReceiverCondition = ' ( @@ -1655,22 +1654,25 @@ class SocialManager extends UserManager parent_id = '.$parentId.' )'; - $sql .= $userReceiverCondition; + $sql[1] .= $userReceiverCondition; - $sql .= ' OR ( msg_status = '.MESSAGE_STATUS_PROMOTED.' ) '; + $sql[2] = $sqlBase.' msg_status = '.MESSAGE_STATUS_PROMOTED.' '; // Get my group posts $groupCondition = ''; if (!empty($groupId)) { if (is_array($groupId)) { $groupId = array_map('intval', $groupId); - $groupId = implode("','", $groupId); - $groupCondition = " OR ( group_id IN ('$groupId') "; + $groupId = implode(",", $groupId); + $groupCondition = " ( group_id IN ($groupId) "; } else { $groupId = (int) $groupId; - $groupCondition = " OR ( group_id = '$groupId' "; + $groupCondition = " ( group_id = $groupId "; } - $groupCondition .= ' AND msg_status IN ('.MESSAGE_STATUS_NEW.', '.MESSAGE_STATUS_UNREAD.')) '; + $groupCondition .= ' AND (msg_status = '.MESSAGE_STATUS_NEW.' OR msg_status = '.MESSAGE_STATUS_UNREAD.')) '; + } + if (!empty($groupCondition)) { + $sql[3] = $sqlBase.$groupCondition; } // Get my friend posts @@ -1678,17 +1680,16 @@ class SocialManager extends UserManager if (!empty($friendId)) { if (is_array($friendId)) { $friendId = array_map('intval', $friendId); - $friendId = implode("','", $friendId); - $friendCondition = " OR ( user_receiver_id IN ('$friendId') "; + $friendId = implode(",", $friendId); + $friendCondition = " ( user_receiver_id IN ($friendId) "; } else { $friendId = (int) $friendId; - $friendCondition = " OR ( user_receiver_id = '$friendId' "; + $friendCondition = " ( user_receiver_id = $friendId "; } $friendCondition .= ' AND msg_status = '.MESSAGE_STATUS_WALL_POST.' AND parent_id = 0) '; } - - if (!empty($groupCondition) || !empty($friendCondition)) { - $sql .= " $groupCondition $friendCondition "; + if (!empty($friendCondition)) { + $sql[4] = $sqlBase.$friendCondition; } if (!empty($threadList)) { @@ -1713,76 +1714,90 @@ class SocialManager extends UserManager $threadList = array_map('intval', $threadList); $threadList = implode("','", $threadList); $condition = " thread_id IN ('$threadList') "; - $sql .= " - UNION ( - $select + $sql[5] = "$select FROM c_forum_post WHERE $condition - ) "; } if ($getCount) { - $res = Database::query($sql); - $row = Database::fetch_array($res); - - return (int) $row['count']; + $count = 0; + foreach ($sql as $oneQuery) { + if (!empty($oneQuery)) { + $res = Database::query($oneQuery); + $row = Database::fetch_array($res); + $count += (int) $row['count_items']; + } + } + return $count; } - $sql .= ' ORDER BY send_date DESC '; - $sql .= " LIMIT $start, $length "; - + $sqlOrder = ' ORDER BY send_date DESC '; + $sqlLimit = " LIMIT $start, $length "; $messages = []; - $res = Database::query($sql); - $em = Database::getManager(); - if (Database::num_rows($res) > 0) { - $repo = $em->getRepository('ChamiloCourseBundle:CForumPost'); - $repoThread = $em->getRepository('ChamiloCourseBundle:CForumThread'); - $groups = []; - $userGroup = new UserGroup(); - $urlGroup = api_get_path(WEB_CODE_PATH).'social/group_view.php?id='; - while ($row = Database::fetch_array($res, 'ASSOC')) { - $row['group_info'] = []; - if (!empty($row['group_id'])) { - if (!in_array($row['group_id'], $groups)) { - $group = $userGroup->get($row['group_id']); - $group['url'] = $urlGroup.$group['id']; - $groups[$row['group_id']] = $group; - $row['group_info'] = $group; - } else { - $row['group_info'] = $groups[$row['group_id']]; + foreach ($sql as $index => $oneQuery) { + if ($index === 5) { + // Exception only for the forum query above (field name change) + $oneQuery .= ' ORDER BY post_date DESC '.$sqlLimit; + } else { + $oneQuery .= $sqlOrder.$sqlLimit; + } + $res = Database::query($oneQuery); + $em = Database::getManager(); + if (Database::num_rows($res) > 0) { + $repo = $em->getRepository('ChamiloCourseBundle:CForumPost'); + $repoThread = $em->getRepository('ChamiloCourseBundle:CForumThread'); + $groups = []; + $userGroup = new UserGroup(); + $urlGroup = api_get_path(WEB_CODE_PATH).'social/group_view.php?id='; + while ($row = Database::fetch_array($res, 'ASSOC')) { + $row['group_info'] = []; + if (!empty($row['group_id'])) { + if (!in_array($row['group_id'], $groups)) { + $group = $userGroup->get($row['group_id']); + $group['url'] = $urlGroup.$group['id']; + $groups[$row['group_id']] = $group; + $row['group_info'] = $group; + } else { + $row['group_info'] = $groups[$row['group_id']]; + } } - } - // Forums - $row['post_title'] = ''; - $row['forum_title'] = ''; - $row['thread_url'] = ''; - if ($row['msg_status'] == MESSAGE_STATUS_FORUM) { - /** @var CForumPost $post */ - $post = $repo->find($row['id']); - /** @var CForumThread $thread */ - $thread = $repoThread->find($row['thread_id']); - if ($post && $thread) { - $courseInfo = api_get_course_info_by_id($post->getCId()); - $row['post_title'] = $post->getForumId(); - $row['forum_title'] = $thread->getThreadTitle(); - $row['thread_url'] = api_get_path(WEB_CODE_PATH).'forum/viewthread.php?'.http_build_query([ - 'cidReq' => $courseInfo['code'], - 'forum' => $post->getForumId(), - 'thread' => $post->getThreadId(), - 'post_id' => $post->getIid(), - ]).'#post_id_'.$post->getIid(); + // Forums + $row['post_title'] = ''; + $row['forum_title'] = ''; + $row['thread_url'] = ''; + if ($row['msg_status'] == MESSAGE_STATUS_FORUM) { + /** @var CForumPost $post */ + $post = $repo->find($row['id']); + /** @var CForumThread $thread */ + $thread = $repoThread->find($row['thread_id']); + if ($post && $thread) { + $courseInfo = api_get_course_info_by_id($post->getCId()); + $row['post_title'] = $post->getForumId(); + $row['forum_title'] = $thread->getThreadTitle(); + $row['thread_url'] = api_get_path(WEB_CODE_PATH).'forum/viewthread.php?'.http_build_query([ + 'cidReq' => $courseInfo['code'], + 'forum' => $post->getForumId(), + 'thread' => $post->getThreadId(), + 'post_id' => $post->getIid(), + ]).'#post_id_'.$post->getIid(); + } } - } - $messages[] = $row; + $messages[$row['id']] = $row; + } } } + // Reordering messages by ID (reverse order) is enough to have the + // latest first, as there is currently no option to edit messages + // afterwards + krsort($messages); return $messages; } + /** * Gets all messages from someone's wall (within specific limits), formatted. *