MDL-64820 forum: fix blog discussion list ordering
authorRyan Wyllie <ryan@moodle.com>
Wed, 20 Mar 2019 06:46:15 +0000 (14:46 +0800)
committerRyan Wyllie <ryan@moodle.com>
Fri, 22 Mar 2019 01:31:21 +0000 (09:31 +0800)
mod/forum/classes/local/vaults/discussion_list.php
mod/forum/view.php

index 17c138b..19c6087 100644 (file)
@@ -61,6 +61,8 @@ class discussion_list extends db_table_vault {
     public const SORTORDER_NEWEST_FIRST = 1;
     /** Sort by oldest first */
     public const SORTORDER_OLDEST_FIRST = 2;
+    /** Sort by created desc */
+    public const SORTORDER_CREATED_DESC = 3;
 
     /**
      * Get the table alias.
@@ -180,19 +182,24 @@ class discussion_list extends db_table_vault {
 
         $alias = $this->get_table_alias();
 
-        // TODO consider user favourites...
-        $keyfield = "{$alias}.timemodified";
-        $direction = "DESC";
+        if ($sortmethod == self::SORTORDER_CREATED_DESC) {
+            $keyfield = "fp.created";
+            $direction = "DESC";
+        } else {
+            // TODO consider user favourites...
+            $keyfield = "{$alias}.timemodified";
+            $direction = "DESC";
 
-        if ($sortmethod == self::SORTORDER_OLDEST_FIRST) {
-            $direction = "ASC";
-        }
+            if ($sortmethod == self::SORTORDER_OLDEST_FIRST) {
+                $direction = "ASC";
+            }
 
-        if (!empty($CFG->forum_enabletimedposts)) {
-            $keyfield = "CASE WHEN {$keyfield} < {$alias}.timestart THEN {$alias}.timestart ELSE {$keyfield} END";
+            if (!empty($CFG->forum_enabletimedposts)) {
+                $keyfield = "CASE WHEN {$keyfield} < {$alias}.timestart THEN {$alias}.timestart ELSE {$keyfield} END";
+            }
         }
 
-        return "{$alias}.pinned DESC, {$keyfield} {$direction}";
+        return "{$alias}.pinned DESC, {$keyfield} {$direction}, {$alias}.id DESC";
     }
 
     /**
index 008d03c..6a025e5 100644 (file)
@@ -128,7 +128,9 @@ switch ($forum->get_type()) {
         break;
     case 'blog':
         $discussionsrenderer = $rendererfactory->get_blog_discussion_list_renderer($forum);
-        echo $discussionsrenderer->render($USER, $cm, $groupid, $sortorder, $pageno, $pagesize);
+        $discussionlistvault = $vaultfactory->get_discussions_in_forum_vault();
+        // Blog forums always show discussions newest first.
+        echo $discussionsrenderer->render($USER, $cm, $groupid, $discussionlistvault::SORTORDER_CREATED_DESC, $pageno, $pagesize);
         break;
     default:
         $discussionsrenderer = $rendererfactory->get_discussion_list_renderer($forum);