Sorting discussion topics in order of 'first post time'

Sorting discussion topics in order of 'first post time'

by Tatsuya Shirai -
Number of replies: 5

This patches allow you to select sorting order method of the discussion topics. In current version of Moodle1.8 the list of discussion topics is displayed in order of 'Last Post' only. We can not follow the relationship a topic and next topic.

After applying these three paches we can select three sorting methods of discussion topics. One is origginal sorting method (Last Post), and the others are sorting by 'First Post (From older)' and 'First Post (From newer)' according to date of discussion topic.


(1) mod/forum/view.php, Line199

         print_box_start('rsslink');
        rss_print_link($course->id, $userid, "forum", $forum->id, $tooltiptext);
        print_box_end(); // subscription

    }
//  Adding (From here)
    print_box_start('sortorder');
    if (!isset($_COOKIE['sortorderfirstpost'])) $sortorderfirstpost = 1;
        else $sortorderfirstpost = $_COOKIE['sortorderfirstpost'];
    echo "<span class=\"helplink\">Sort discussion topics in order of &nbsp";
    if ($sortorderfirstpost <> 1) {
        echo "<a title=\"Display discussion topics in order of Last Post\" href=\"subscribe.php?id=$forum->id&amp;sortorder=1\">(Last Post)</a>&nbsp";
    }
    if ($sortorderfirstpost <> 2) {
        echo "<a title=\"Display discussion topics in order of First Post(From Older)\" href=\"subscribe.php?id=$forum->id&amp;sortorder=2\">(First Post/Older)</a>&nbsp";
 }
 if ($sortorderfirstpost <> 3) {
        echo "<a title=\"Display discussion topics in order of First Post(From Newer)\" href=\"subscribe.php?id=$forum->id&amp;sortorder=3\">(First Post/Newer)&nbsp</a>";
 }
 echo "</span>";
    print_box_end();
//  Adding (To here)
    print_box_end(); // subscription

    print_box_end();  // forumcontrol

    print_box('&nbsp;', 'clearer');

(2) mod/forum/subscribe.php, Line73.

        notice_yesno(get_string('noguestsubscribe', 'forum').'<br /><br />'.get_string('liketologin'),
                     $wwwroot, $_SERVER['HTTP_REFERER']);
        print_footer($course);
        exit;
    }

    $returnto = forum_go_back_to("index.php?id=$course->id");

//  Adding (From here)
    $sortorder = optional_param('sortorder', 0, PARAM_INT);
    if ($sortorder > 0) {
        setcookie("sortorderfirstpost", $sortorder);
        redirect($returnto, "Sorting order of Discussion topics has changed.", 1);
    }
// Adding (To here)
    if ($force and has_capability('mod/forum:managesubscriptions', $context)) {
        if (forum_is_forcesubscribed($forum->id)) {
            forum_forcesubscribe($forum->id, 0);
            redirect($returnto, get_string("everyonecannowchoose", "forum"), 1);
        } else {
            forum_forcesubscribe($forum->id, 1);
            redirect($returnto, get_string("everyoneisnowsubscribed", "forum"), 1);
        }
    }

(3) mod/forum/lib.php, Line3486,in function forum_print_latest_discussions()

function forum_print_latest_discussions($course, $forum, $maxdiscussions=5, $displayformat='plain', $sort='',
                                        $currentgroup=-1, $groupmode=-1, $page=-1, $cm=NULL) {
    global $CFG, $USER;
   
// Adding (From here)
    if (isset($_COOKIE['sortorderfirstpost'])) {
        switch ($_COOKIE['sortorderfirstpost']) {
          case "2": $sort = "d.firstpost"; break;
          case "3": $sort = "d.firstpost DESC"; break;
          default : $sort = '';
        }
    }
// Adding (To here)

    if (!$cm) {
        if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $forum->course)) {
            error('Course Module ID was incorrect');
        }
    }  
    $context = get_context_instance(CONTEXT_MODULE, $cm->id);


Average of ratings: -
In reply to Tatsuya Shirai

Re: Sorting discussion topics in order of 'first post time'

by Tatsuya Shirai -

Using $_SESSION is more better than using $_COOKIE.

(1) mod/forum/view.php, Line199

         print_box_start('rsslink');
        rss_print_link($course->id, $userid, "forum", $forum->id, $tooltiptext);
        print_box_end(); // subscription

    }
//  Adding (From here)
    print_box_start('sortorder');
    if (!isset($_SESSION['sortorderfirstpost'])) $sortorderfirstpost = 1;
        else $sortorderfirstpost = $_SESSION['sortorderfirstpost'];
    echo "<span class=\"helplink\">Sort discussion topics in order of &nbsp";
    if ($sortorderfirstpost <> 1) {
        echo "<a title=\"Display discussion topics in order of Last Post\" href=\"subscribe.php?id=$forum->id&amp;sortorder=1\">(Last Post)</a>&nbsp";
    }
    if ($sortorderfirstpost <> 2) {
        echo "<a title=\"Display discussion topics in order of First Post(From Older)\" href=\"subscribe.php?id=$forum->id&amp;sortorder=2\">(First Post/Older)</a>&nbsp";
 }
 if ($sortorderfirstpost <> 3) {
        echo "<a title=\"Display discussion topics in order of First Post(From Newer)\" href=\"subscribe.php?id=$forum->id&amp;sortorder=3\">(First Post/Newer)&nbsp</a>";
 }
 echo "</span>";
    print_box_end();
//  Adding (To here)
    print_box_end(); // subscription

    print_box_end();  // forumcontrol

    print_box('&nbsp;', 'clearer');

(2) mod/forum/subscribe.php, Line73.

        notice_yesno(get_string('noguestsubscribe', 'forum').'<br /><br />'.get_string('liketologin'),
                     $wwwroot, $_SERVER['HTTP_REFERER']);
        print_footer($course);
        exit;
    }

    $returnto = forum_go_back_to("index.php?id=$course->id");

//  Adding (From here)
    $sortorder = optional_param('sortorder', 0, PARAM_INT);
    if ($sortorder > 0) {
//      setcookie("sortorderfirstpost", $sortorder);
        $_SESSION['sortorderfirstpost'] = $sortorder;
        redirect($returnto, "Sorting order of Discussion topics has changed.", 1);
    }
// Adding (To here)
    if ($force and has_capability('mod/forum:managesubscriptions', $context)) {
        if (forum_is_forcesubscribed($forum->id)) {
            forum_forcesubscribe($forum->id, 0);
            redirect($returnto, get_string("everyonecannowchoose", "forum"), 1);
        } else {
            forum_forcesubscribe($forum->id, 1);
            redirect($returnto, get_string("everyoneisnowsubscribed", "forum"), 1);
        }
    }

(3) mod/forum/lib.php, Line3486,in function forum_print_latest_discussions()

function forum_print_latest_discussions($course, $forum, $maxdiscussions=5, $displayformat='plain', $sort='',
                                        $currentgroup=-1, $groupmode=-1, $page=-1, $cm=NULL) {
    global $CFG, $USER;
   
// Adding (From here)
    if (isset($_SESSION['sortorderfirstpost'])) {
        switch ($_SESSION['sortorderfirstpost']) {
          case "2": $sort = "d.firstpost"; break;
          case "3": $sort = "d.firstpost DESC"; break;
          default : $sort = '';
        }
    }
// Adding (To here)

    if (!$cm) {
        if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $forum->course)) {
            error('Course Module ID was incorrect');
        }
    }  
    $context = get_context_instance(CONTEXT_MODULE, $cm->id);

In reply to Tatsuya Shirai

New version (UI improved)

by Tatsuya Shirai -

This is new version that User Interface has been changed intuitively!

(1) mod/forum/view.php
  There are no modifications.

(2) mod/forum/subscribe.php, Line 73

        notice_yesno(get_string('noguestsubscribe', 'forum').'<br /><br />'.get_string('liketologin'),
                     $wwwroot, $_SERVER['HTTP_REFERER']);
        print_footer($course);
        exit;
    }

    $returnto = forum_go_back_to("index.php?id=$course->id");

// Append from hee
    $sortorder = optional_param('sortorder', 0, PARAM_INT);
    if ($sortorder > 0) {
        $_SESSION['sortorderfirstpost'] = $sortorder;
        $get_string_sortorderischanged = 'Sorting order of discusson topics is chaned';
        redirect($returnto, $get_string_sortorderischanged, 1);
    }
// Append to here

    if ($force and has_capability('mod/forum:managesubscriptions', $context)) {
        if (forum_is_forcesubscribed($forum->id)) {
            forum_forcesubscribe($forum->id, 0);
            redirect($returnto, get_string("everyonecannowchoose", "forum"), 1);
        } else {
            forum_forcesubscribe($forum->id, 1);
            redirect($returnto, get_string("everyoneisnowsubscribed", "forum"), 1);
        }
    }

(3) mod/forum/lib.php, Line3486

function forum_print_latest_discussions($course, $forum, $maxdiscussions=5, $displayformat='plain', $sort='',
                                        $currentgroup=-1, $groupmode=-1, $page=-1, $cm=NULL) {
    global $CFG, $USER;
   
// Append from here
    if (isset($_SESSION['sortorderfirstpost'])) {
        switch ($_SESSION['sortorderfirstpost']) {
          case "2": $sort = "d.firstpost"; break;
          case "3": $sort = "d.firstpost DESC"; break;
          default : $sort = '';
        }
    }
// Apend to here
    if (!$cm) {
        if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $forum->course)) {
            error('Course Module ID was incorrect');
        }
    }  
    $context = get_context_instance(CONTEXT_MODULE, $cm->id);

(4) mod/forum/lib.php, Line3625

        if (has_capability('mod/forum:viewdiscussion', $context)) {
            echo '<th class="header replies" scope="col">'.get_string('replies', 'forum').'</th>';
            // If the forum can be tracked, display the unread column.
            if ($cantrack) {
                echo '<th class="header replies" scope="col">'.get_string('unread', 'forum');
                if ($forumtracked) {
                    echo '&nbsp;<a title="'.get_string('markallread', 'forum').
                         '" href="'.$CFG->wwwroot.'/mod/forum/markposts.php?f='.
                         $forum->id.'&amp;mark=read&amp;returnpage=view.php">'.
                         '<img src="'.$CFG->pixpath.'/t/clear.gif" class="iconsmall" alt="'.get_string('markallread', 'forum').'" /></a>';
                }
                echo '</th>';
            }
        }
// Comment out
//
      echo '<th class="header lastpost" scope="col">'.get_string('lastpost', 'forum').'</th>';
// Append from here
         $get_string_firstpost = get_string('lastpost', 'forum');
         $get_string_sortbylastpost = 'Sort by Last Post';
         $get_string_sortbyfirstpostnewer = 'Sort by First Post(Newer)';
         $get_string_sortbyfirstpostolder = 'Sort by First Post(Older)';
     if (!isset($_SESSION['sortorderfirstpost'])) $sortorderfirstpost = 1;
         else $sortorderfirstpost = $_SESSION['sortorderfirstpost'];
     $linklastpost = '<a title="'.$get_string_sortbylastpost."\" href=\"subscribe.php?id=$forum->id&amp;sortorder=1\">&#x25C0;</a>";
     $linkfirstpostolder = '<a title="'.$get_string_sortbyfirstpostolder."\" href=\"subscribe.php?id=$forum->id&amp;sortorder=2\">&#x25B2;</a>";
     $linkfirstpostnewer = '<a title="'.$get_string_sortbyfirstpostnewer."\" href=\"subscribe.php?id=$forum->id&amp;sortorder=3\">&#x25BC;</a>";
     switch ($sortorderfirstpost) {
       case 2:
         $sortordermsg = $linklastpost.'&nbsp'.$get_string_firstpost.'&nbsp(&#x25B2;/'.$linkfirstpostnewer.')';
         break;
       case 3:
         $sortordermsg = $linklastpost.'&nbsp'.$get_string_firstpost.'&nbsp('.$linkfirstpostolder.'/&#x25BC;)';
         break;
       case 1:
       default:
   $sortordermsg = get_string('lastpost', 'forum').'&nbsp('.$linkfirstpostolder.'/'.$linkfirstpostnewer.')';
         break;
     }
        echo '<th class="header lastpost" scope="col">'.$sortordermsg.'</th>';
//  Append to here
        echo '</tr>';
        echo '</thead>';
        echo '<tbody>';
    }

#Sorry, I'm japanese. The statements , coments are not good english, however your moodle site would become usefully! Try this modification!

Attachment Improvement_sort_order_of_forum_posts.jpg
Average of ratings: Useful (2)
In reply to Tatsuya Shirai

Re: New version (UI improved)

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
Thanks for the patches, but please, don't post them here in the forums. Otherwise the forums become full of old code and they clutter up the searches.

The best way is to file a new bug in the Moodle Tracker and attach them there as diffs (use a diff program to generate these automatically). Once you have a issue number for your patch, you can easily point people there from these forums using the issue number (it will automatically link) eg MDL-10000

more info: http://docs.moodle.org/en/Development:Overview#Minor_Development