# This patch file was generated by NetBeans IDE
# This patch can be applied using context Tools: Apply Diff Patch action on respective folder.
# It uses platform neutral UTF-8 encoding.
# Above lines and this line are ignored by the patching process.
Index: moodle/make_forum
MIME: application/octet-stream; encoding: Base64; length: -1

Index: moodle/mod/forum/lib.php
--- moodle/mod/forum/lib.php Base (1.609.2.102)
+++ moodle/mod/forum/lib.php Locally Modified (Based On 1.609.2.102)
@@ -1630,7 +1630,7 @@
  *         id, type, course, cmid, cmvisible, cmgroupmode, accessallgroups,
  *         viewhiddentimedposts
  */
-function forum_get_readable_forums($userid, $courseid=0) {
+function forum_get_readable_forums($userid, $courseid=0, $limitfrom=0, $limitnum=0) {
 
     global $CFG, $USER;
     require_once($CFG->dirroot.'/course/lib.php');
@@ -1716,9 +1716,10 @@
                     }
                 }
             }
-
+            if (count($readableforums)>=$limitfrom and ( $limitnum==0 or count($readableforums)<=$limitnum)) {
             $readableforums[$forum->id] = $forum;
         }
+        }
 
         unset($modinfo);
 
@@ -1745,9 +1746,15 @@
     global $CFG, $USER;
     require_once($CFG->libdir.'/searchlib.php');
 
-    $forums = forum_get_readable_forums($USER->id, $courseid);
+    $forumcount = -1;
+    $postcount = 0;
+    $unionsql='';
+    //lets get forums one by one till we have enough posts to display for the page.
+    while ($postcount < $limitnum ) {
+        $forumcount++;
+        $forums = forum_get_readable_forums($USER->id, $courseid, $forumcount , 1 );
 
-    if (count($forums) == 0) {
+        if (count($forums) == 0 and $forumcount==0) {
         $totalcount = 0;
         return false;
     }
@@ -1869,10 +1876,16 @@
                    WHERE $selectsql
                 ORDER BY p.modified DESC";
 
-    $totalcount = count_records_sql($countsql);
-
-    return get_records_sql($searchsql, $limitfrom, $limitnum);
+        $postcount = count_records_sql($countsql);
+        $totalcount += $postcount;
+        if($postcount >= $limitnum) {
+            $unionsql = $unionsql . $searchsql;
+        } else {
+            $unionsql = $unionsql . ' UNION ' . $searchsql;
 }
+    }
+    return get_records_sql($unionsql, $limitfrom, $limitnum);
+}
 
 /**
  * Returns a list of ratings for all posts in discussion
