Improve list thread posts - refs BT#9892 #TMI

1.10.x
Angel Fernando Quiroz Campos 10 years ago
parent 162c298c08
commit 68bba34521
  1. 95
      main/forum/forumfunction.inc.php
  2. 2
      main/forum/viewthread_flat.inc.php
  3. 3
      main/forum/viewthread_nested.inc.php
  4. 5
      main/forum/viewthread_threaded.inc.php
  5. 2
      tests/main/forum/forumfunction.inc.test.php

@ -1865,54 +1865,71 @@ function get_threads($forum_id, $course_code = null)
/**
* Retrieve all posts of a given thread
*
* @param int $threadId The thread ID
* @param string $orderDirection Optional. The direction for sort the posts
* @param boolean $recursive Optional. If the list is recursive
* @param int $postId Optional. The post ID for recursive list
* @param int $depth Optional. The depth to indicate the indent
* @return array containing all the information about the posts of a given thread
*
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
* @version february 2006, dokeos 1.8
*/
function get_posts($thread_id)
function getPosts($threadId, $orderDirection = 'ASC', $recursive = false, $postId = 0, $depth = -1)
{
$table_users = Database :: get_main_table(TABLE_MAIN_USER);
$table_posts = Database :: get_course_table(TABLE_FORUM_POST);
$list = [];
$course_id = api_get_course_int_id();
$sessionId = api_get_session_id();
$em = Database::getManager();
// note: change these SQL so that only the relevant fields of the user table are used
/*
* INNER JOIN $tableItemProperty i
ON i.ref = posts.post_id AND i.c_id = posts.c_id*
i.session_id = $sessionId
*/
if (api_is_allowed_to_edit(null, true)) {
$sql = "SELECT * FROM $table_posts posts
LEFT JOIN $table_users users
ON posts.poster_id = users.user_id
WHERE
posts.c_id = $course_id AND
posts.thread_id = ".intval($thread_id)."
$whereCondition = [
'threadId' => $threadId,
'cId' => api_get_course_int_id(),
'visible' => 1
];
ORDER BY posts.post_id ASC";
} else {
// students can only se the posts that are approved (posts.visible='1')
$sql = "SELECT * FROM $table_posts posts
LEFT JOIN $table_users users
ON posts.poster_id=users.user_id
WHERE
posts.c_id = $course_id AND
posts.thread_id = ".intval($thread_id)." AND
posts.visible='1'
ORDER BY posts.post_id ASC";
if ($recursive) {
$whereCondition['postParentId'] = $postId;
}
$result = Database::query($sql);
$posts = array();
while ($row = Database::fetch_array($result, 'ASSOC')) {
$posts[] = $row;
$posts = $em->getRepository('ChamiloCourseBundle:CForumPost')->findBy(
$whereCondition,
['postId' => $orderDirection]
);
$depth++;
foreach ($posts as $post) {
$user = $em->find('ChamiloUserBundle:User', $post->getPosterId());
$list[$post->getPostId()] = [
'c_id' => $post->getCId(),
'post_id' => $post->getPostId(),
'post_title' => $post->getPostTitle(),
'post_text' => $post->getPostText(),
'thread_id' => $post->getThreadId(),
'forum_id' => $post->getForumId(),
'poster_id' => $post->getPosterId(),
'poster_name' => $post->getPosterName(),
'post_date' => $post->getPostDate(),
'post_notification' => $post->getPostNotification(),
'post_parent_id' => $post->getPostParentId(),
'visible' => $post->getVisible(),
'indent_cnt' => $depth,
'user_id' => $user->getUserId(),
'username' => $user->getUsername(),
'username_canonical' => $user->getUsernameCanonical(),
'lastname' => $user->getLastname(),
'firstname' => $user->getFirstname(),
];
if (!$recursive) {
continue;
}
$list = array_merge(
$list,
getPosts($threadId, $orderDirection, $recursive, $post->getPostId(), $depth)
);
}
return $posts;
return $list;
}
/**
@ -3285,7 +3302,7 @@ function store_edit_post($values)
// First we check if the change affects the thread and if so we commit
// the changes (sticky and post_title=thread_title are relevant).
$posts = get_posts($values['thread_id']);
$posts = getPosts($values['thread_id']);
$first_post = null;
if (!empty($posts)) {
$first_post = $posts[0];

@ -21,7 +21,7 @@ $userId = api_get_user_id();
$groupId = api_get_group_id();
if (isset($current_thread['thread_id'])) {
$rows = get_posts($current_thread['thread_id']);
$rows = getPosts($current_thread['thread_id']);
$increment = 0;
$clean_forum_id = intval($_GET['forum']);
$clean_thread_id = intval($_GET['thread']);

@ -22,8 +22,7 @@ if (isset($_GET['action']) &&
delete_attachment(0, $_GET['id_attach']);
}
$rows = get_posts($_GET['thread']);
$rows = calculate_children($rows);
$rows = getPosts($_GET['thread'], 'ASC', true);
$count = 0;
$clean_forum_id = intval($_GET['forum']);
$clean_thread_id = intval($_GET['thread']);

@ -23,8 +23,7 @@
$forumUrl = api_get_path(WEB_CODE_PATH) . 'forum/';
$_user = api_get_user_info();
$rows = get_posts($_GET['thread']);
$rows = calculate_children($rows);
$rows = getPosts($_GET['thread'], 'ASC', true);
$sessionId = api_get_session_id();
$currentThread = get_thread_information($_GET['thread']);
$post_id = isset($_GET['post']) ? (int) $_GET['post'] : 0;
@ -263,7 +262,7 @@ if (
// Verified the post minor
$my_post = get_posts($_GET['thread']);
$my_post = getPosts($_GET['thread']);
$id_posts = array();
if (!empty($my_post) && is_array($my_post)) {

@ -601,7 +601,7 @@ class TestForumFunction extends UnitTestCase {
public function testget_posts() {
$thread_id = 1;
$res = get_posts($thread_id);
$res = getPosts($thread_id);
if(!is_null($res)){
$this->assertTrue(is_array($res));
} else {

Loading…
Cancel
Save