Эх сурвалжийг харах

Merge pull request #719 from AngelFQC/improve-forums

Improve list thread posts - refs BT#9892 #TMI
Angel Fernando Quiroz Campos 9 жил өмнө
parent
commit
0105fd9739

+ 56 - 39
main/forum/forumfunction.inc.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];

+ 3 - 1
main/forum/viewthread_flat.inc.php

@@ -20,8 +20,10 @@ $_user = api_get_user_info();
 $userId = api_get_user_id();
 $groupId = api_get_group_id();
 
+$sortDirection = isset($_GET['posts_order']) && $_GET['posts_order'] === 'desc' ? 'DESC' : 'ASC';
+
 if (isset($current_thread['thread_id'])) {
-    $rows = get_posts($current_thread['thread_id']);
+    $rows = getPosts($current_thread['thread_id'], $sortDirection);
     $increment = 0;
     $clean_forum_id  = intval($_GET['forum']);
     $clean_thread_id = intval($_GET['thread']);

+ 3 - 2
main/forum/viewthread_nested.inc.php

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

+ 3 - 3
main/forum/viewthread_threaded.inc.php

@@ -23,8 +23,8 @@
 
 $forumUrl = api_get_path(WEB_CODE_PATH) . 'forum/';
 $_user = api_get_user_info();
-$rows = get_posts($_GET['thread']);
-$rows = calculate_children($rows);
+$sortDirection = isset($_GET['posts_order']) && $_GET['posts_order'] === 'desc' ? 'DESC' : 'ASC';
+$rows = getPosts($_GET['thread'], $sortDirection, true);
 $sessionId = api_get_session_id();
 $currentThread = get_thread_information($_GET['thread']);
 $post_id = isset($_GET['post']) ? (int) $_GET['post'] : 0;
@@ -263,7 +263,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)) {

+ 4 - 0
main/inc/lib/internationalization.lib.php

@@ -450,6 +450,10 @@ function api_get_local_time($time = null, $to_timezone = null, $from_timezone =
         $from_timezone = 'UTC';
         $time = gmdate('Y-m-d H:i:s', $time);
     }
+    if ($time instanceof DateTime) {
+        $time = $time->format('Y-m-d H:i:s');
+        $from_timezone = 'UTC';
+    }
     try {
         $date = new DateTime($time, new DateTimezone($from_timezone));
         $date->setTimezone(new DateTimeZone($to_timezone));

+ 1 - 1
tests/main/forum/forumfunction.inc.test.php

@@ -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 {