[TikiWiki-commits] [Git][tikiwiki/tiki][master] [FIX] Implement proper threaded forum sorting by activity when newest first is configured

Benoit Grégoire (@benoitg) via TikiWiki-cvs <[email protected]>
Newsgroups gmane.comp.cms.tiki.cvs
Message-ID <695d88d1c26b8_2c65778848691c@gitlab-sidekiq-low-urgency-cpu-bound-v2-546464f4cd-2gf5d.mail>

Benoit Grégoire pushed to branch master at Tiki Wiki CMS Groupware / Tiki


Commits:
b671a32a by Moïse Nturubika at 2026-01-06T22:03:54+00:00
[FIX] Implement proper threaded forum sorting by activity when newest first is configured
---
* [FIX] Forum sorting: calculate activity date from already-fetched reply data

* [FIX] PHPCS: camelCase methods, space after NOT, trailing whitespace

* [FIX] Sort threads by latest activity including nested replies

* [FIX] Implement proper threaded forum sorting by activity when newest first is configured

See merge request tikiwiki/tiki!7500

- - - - -


1 changed file:

- lib/comments/commentslib.php


Changes:

=====================================
lib/comments/commentslib.php
=====================================
@@ -2664,6 +2664,51 @@ class Comments extends TikiLib
             $ret[$key] = $this->maskFields($ret[$key]);
         }
 
+        // Sort forum threads by activity date (newest reply) using already-fetched data
+        if ($object[0] == "forum" && $style == 'commentStyle_threaded') {
+            // Helper to find max date in reply tree
+            $findMaxDate = function ($repliesInfo) use (&$findMaxDate) {
+                $maxDate = 0;
+                if (! empty($repliesInfo['replies'])) {
+                    foreach ($repliesInfo['replies'] as $reply) {
+                        $replyDate = $reply['commentDate'] ?? 0;
+                        if ($replyDate > $maxDate) {
+                            $maxDate = $replyDate;
+                        }
+                        // Recurse into nested replies
+                        if (! empty($reply['replies_info'])) {
+                            $nestedMax = $findMaxDate($reply['replies_info']);
+                            if ($nestedMax > $maxDate) {
+                                $maxDate = $nestedMax;
+                            }
+                        }
+                    }
+                }
+                return $maxDate;
+            };
+
+            // Calculate activity date for each thread from its reply tree
+            foreach ($ret as &$row) {
+                $ownDate = $row['commentDate'] ?? 0;
+                $replyMax = isset($row['replies_info']) ? $findMaxDate($row['replies_info']) : 0;
+                $row['lastActivityDate'] = max($ownDate, $replyMax);
+            }
+            unset($row);
+
+            // Sort by calculated activity date
+            $activitySort = (stripos($sort_mode, 'desc') !== false) ? 'DESC' : 'ASC';
+            usort($ret, function ($a, $b) use ($activitySort) {
+                $dateA = $a['lastActivityDate'] ?? 0;
+                $dateB = $b['lastActivityDate'] ?? 0;
+                if ($dateA == $dateB) {
+                    return 0;
+                }
+                return ($activitySort === 'DESC')
+                    ? (($dateA < $dateB) ? 1 : -1)
+                    : (($dateA > $dateB) ? 1 : -1);
+            });
+        }
+
         if ($old_sort_mode == 'replies_asc') {
             usort($ret, 'compare_replies');
         }
@@ -4402,6 +4447,7 @@ class Comments extends TikiLib
     }
 }
 
+
 /**
  * @param $ar1
  * @param $ar2



View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/b671a32a37d1827fbc2a814b47e03cb42d6c0b79

-- 
View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/b671a32a37d1827fbc2a814b47e03cb42d6c0b79
You're receiving this email because of your account on gitlab.com.

_______________________________________________
TikiWiki-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tikiwiki-cvs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.