Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
Options

Sorting Discussions by Date Created, rather than most recently Updated, EXCEPT in Public Forum?

DoyceTDoyceT Model Questioner ✭✭✭
edited January 2015 in Vanilla 2.0 - 2.8

Hi all,

I'm running 2.1.7, with a hacky sort of plugin that forces the forum to sort discussion posts by order created, rather than order updated. See near the end of this: http://vanillaforums.org/discussion/27892/need-to-sort-discussions-by-date-created-not-most-recent-comment/p2

The reason/need for the plugin is because the config entries that should get the forum to order in a way other than 'most recently commented on' aren't being read/acted on in core, at the moment. This was true when I posted the original question (running 2.1.3) and as near as I can tell, is still true now.

Now, aside from that issue, my users have come to me a further request: they prefer the "keep everything in the order in which it was first posted" for the classroom Categories, but would like the "order by most recently updated" for the Public areas of the forum where everyone's just talking about whatever and chatting.

Pretend, for the sake of argument, there's a single Category I'd like to order by "most recently commented", and the rest I need to keep sorting by "created at"...

Any way to do that?

Tagged:

Comments

  • Options
    hgtonighthgtonight ∞ · New Moderator

    Where there is a will, there is a way!

    All you need to do is add a check as to what is being requested against your configured category ID and not perform the hacky work around.

    public function DiscussionModel_BeforeGet_Handler($Sender) {
      $CategoryID = val('d.CategoryID', $Sender->EventArguments['Wheres'], FALSE);
      if($CategoryID == C('HackyPlugin.CategoryID')) {
        return;
      }
    
      $GetPrivateObject = function &($Object, $Item) {
        $Result = &Closure::bind(function &() use ($Item) {
          return $this->$Item;
        }, $Object, $Object)->__invoke();
        return $Result;
      };
      $OrderBy = &$GetPrivateObject($Sender->SQL, '_OrderBys');
      $OrderBy[0] = 'd.DateInserted desc';
    }
    

    Be sure to add the CategoryID to your /conf/config.php file with e.g. $Configuration['HackyPlugin']['CategoryID'] = 3;.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • Options
    DoyceTDoyceT Model Questioner ✭✭✭

    Excellent!

    I probably won't be able to implement this until next week, but will report back then.

  • Options
    hgtonighthgtonight ∞ · New Moderator

    @DoyceT said:
    Excellent!

    I probably won't be able to implement this until next week, but will report back then.

    Hopefully you don't have any issues, because that is untested code.

    Because that is how I roll at 4 pm.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • Options
    DoyceTDoyceT Model Questioner ✭✭✭

    Okay, I've added:

      $Configuration['HackyPlugin']['CategoryID'] = 20;
    

    Which is the ID for the Public Forum category.

    And I have the following in the plugin,

    <?php if (!defined('APPLICATION')) exit();
      $PluginInfo['OrderByCreationDate'] = array(
       'Name' => 'Order By Creation Date',
       'Description' => 'This plugin causes all posts in the forum to show up in the order in which they were originally posted, rather than ordered by most recent comment.',
       'Version' => '0.1',
       'Author' => "doycet",
       'AuthorEmail' => 'doyce.testerman@gmail.com',
       'AuthorUrl' => 'http://vanillaforums.org/discussion/comment/215804'
    );
    
    class OrderByCreationDatePlugin extends Gdn_Plugin {
    
    public function DiscussionModel_BeforeGet_Handler($Sender) {
        $CategoryID = val('d.CategoryID', $Sender->EventArguments['Wheres'], FALSE);
        if($CategoryID == C('HackyPlugin.CategoryID')) {
        return;
    }
    
        $GetPrivateObject = function &($Object, $Item) {
        $Result = &Closure::bind(function &() use ($Item) {
        return $this->$Item;
        }, $Object, $Object)->__invoke();
        return $Result;
        };
        $OrderBy = &$GetPrivateObject($Sender->SQL, '_OrderBys');
        $OrderBy[0] = 'd.DateInserted desc';
        }
    }
    

    The good news: it didn't break anything. Everything is still ordering by date posted.

    The bad news: the added bits don't seem to be doing anything to the display of posts in the public forum. I suspect I'm missing something.

  • Options
    hgtonighthgtonight ∞ · New Moderator

    Go ahead and add var_dump($Sender->EventArguments['Wheres']); as the first line in your function and visit a page.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • Options
    DoyceTDoyceT Model Questioner ✭✭✭
    edited January 2015

    Edit: Nevermind, that was poor placement on my part.

    It doesn't look like I'm getting any messages kicked out. :/

  • Options
    DoyceTDoyceT Model Questioner ✭✭✭

    Here we go.

    Up at the top of the forum page, above the header, I get this:

      array(1) { ["d.CategoryID"]=> array(1) { [0]=> string(2) "20" } } 
    
  • Options
    DoyceTDoyceT Model Questioner ✭✭✭
    edited January 2015

    Please see the image here:

    I flipped on debug to see what all was going on with the category in question, and it sure LOOKS like the system knows it's supposed to be doing something different with CategoryID 20.

    It just... isn't. :)

  • Options
    DoyceTDoyceT Model Questioner ✭✭✭
    edited January 2015

    However, a little bit later down, you get this:

    Which seems to be the opposite and... since it's coming up later in the debug, is perhaps the thing that runs last? Not sure where that's coming from. :P

    Looking further - is it behaving as intended for Announcements and not for 'normal' discussions?

    Answer: Yes. In CategoryID 20 Announcements, it's sorting by last comment, but for non-announcements, it's behaving like the rest of the forum and sorting by DateInserted.

    However, it looks like Announcements are only 'working' because they've always ignored my hacky little plugin.

  • Options
    hgtonighthgtonight ∞ · New Moderator
    edited January 2015

    Looks like you need to loop through the array of wheres or use in_array: http://php.net/manual/en/function.in-array.php

    $CategoryIDs = val('d.CategoryID', $Sender->EventArguments['Wheres'], array());
    if(in_array(C('HackyPlugin.CategoryID'), $CategoryIDs)) {
      return;
    }
    

    You can ditch the var dump line.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • Options
    DoyceTDoyceT Model Questioner ✭✭✭

    That got it, @hgtonight‌ - thanks for your help on this.

Sign In or Register to comment.