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.

Voting Plugin, Adding Popular week, month, etc.

edited November 2012 in Vanilla 2.0 - 2.8

I am adding the functions, Popular Weekly, Popular Monthly, and so on to my voting plugin.

Ive got it down how to expand and create new discussion controllers, but i am not positive how to limit it to posts for the week, month, etc.

Heres my new function for the Popular this week.

Also i am having trouble with my pagers not working correctly, only displays first page, seems offset isn't working right.

public function DiscussionsController_PopularThisWeek_Create($Sender) {
// if (!C('Plugins.Voting.Enabled'))
// return;

  $Sender->Title(T('Popular This Week'));  // This sets title of page
  $Sender->Head->Title($Sender->Head->Title());

  $Offset = GetValue('0', $Sender->RequestArgs, '0');

  // Get rid of announcements from this view
  if ($Sender->Head) {
     $Sender->AddJsFile('discussions.js');
     $Sender->AddJsFile('bookmark.js');
     $Sender->AddJsFile('options.js');
     $Sender->Head->AddRss($Sender->SelfUrl.'/feed.rss', $Sender->Head->Title());
  }
  if (!is_numeric($Offset) || $Offset < 0)
     $Offset = 0;

  // Add Modules
  $Sender->AddModule('NewDiscussionModule');
  $BookmarkedModule = new BookmarkedModule($Sender);
  $BookmarkedModule->GetData();
  $Sender->AddModule($BookmarkedModule);

  $Sender->SetData('Category', FALSE, TRUE);  // I think this would be where i need to limit the dates somehow.
  $Limit = C('Vanilla.Discussions.PerPage', 30);
  $DiscussionModel = new DiscussionModel();
  $CountDiscussions = $DiscussionModel->GetCount();
  $Sender->SetData('CountDiscussions', $CountDiscussions);
  $Sender->AnnounceData = FALSE;
    $Sender->SetData('Announcements', array(), TRUE);
  $DiscussionModel->SQL->OrderBy('d.Score', 'desc');
  $Sender->DiscussionData = $DiscussionModel->Get($Offset, $Limit);
  $Sender->SetData('Discussions', $Sender->DiscussionData, TRUE);        // Or possibly right here
  $Sender->SetJson('Loading', $Offset . ' to ' . $Limit);

  // Build a pager.
  $PagerFactory = new Gdn_PagerFactory();
  $Sender->Pager = $PagerFactory->GetPager('Pager', $Sender);
  $Sender->Pager->ClientID = 'Pager';
  $Sender->Pager->Configure(
     $Offset,
     $Limit,
     $CountDiscussions,
     'discussions/popular/%1$s'
  );

  // Deliver json data if necessary
  if ($Sender->DeliveryType() != DELIVERY_TYPE_ALL) {
     $Sender->SetJson('LessRow', $Sender->Pager->ToString('less'));
     $Sender->SetJson('MoreRow', $Sender->Pager->ToString('more'));
     $Sender->View = 'discussions';
  }

  // Set a definition of the user's current timezone from the db. jQuery
  // will pick this up, compare to the browser, and update the user's
  // timezone if necessary.
  $CurrentUser = Gdn::Session()->User;
  if (is_object($CurrentUser)) {
     $ClientHour = $CurrentUser->HourOffset + date('G', time());
     $Sender->AddDefinition('SetClientHour', $ClientHour);
  }

  // Render the controller
  $Sender->View = 'index';
  $Sender->Render();

}

Comments

  • edited November 2012

    So far ive got it to limit posts to a specific date and so on, Trying to make a function to create a date format the same as stored in the database, but 1 week earlier than the current timestamp.

    Heres what i got so far:

      $Whereas = array("DateInserted >" => date( 'Y-n-j g:i:s' ));
      $Sender->DiscussionData = $DiscussionModel->Get($Offset, $Limit, $Whereas);
    

    This only works because, this date is setup as 12hr cycle, instead of 24hour. So it wont work really well.

    I just need to now make a function to take that date and subtract 1 week from it, 1 day, and so on.

  • here's an example that you could modify.:

    http://vanillaforums.org/discussion/comment/168515/#Comment_168515

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • Looks like i figured out how todo it in a way.

    Set it to one week, and it works lol.

      // This is to sort posts by Discussion Score
      $DiscussionModel->SQL->OrderBy('d.Score', 'desc');
      // This is to set how old you want posts to display
      $currentdate = date( 'Y-n-j G:i:s' );
      $newdate = $newdate = strtotime ( '-1 week' , strtotime ( $currentdate ) ) ;
      $newdate = date( 'Y-n-j G:i:s', $newdate );
      // Setting date limit here
      $Whereas = array("DateInserted >" => $newdate);
      $Sender->DiscussionData = $DiscussionModel->Get($Offset, $Limit, $Whereas);
    
  • @peregrine said:
    here's an example that you could modify.:

    http://vanillaforums.org/discussion/comment/168515/#Comment_168515

    Actually thats a much cleaner method, I have updated my code as follows:

      // Setting date limit here
      $Whereas = array("DateInserted >" => Gdn_Format::ToDateTime(strtotime('-1 week')));
      $Sender->DiscussionData = $DiscussionModel->Get($Offset, $Limit, $Whereas);
      $Sender->SetData('Discussions', $Sender->DiscussionData, TRUE);
    
  • This is what i finally ended up with, it makes two more tabs, so a total of 3 tabs, Popular All Time, Popular Today, And Popular This Week. Enjoy. Trying to fix the paging bug atm still.

    /**
     * Add the "Popular Questions" tab.
     */
    public function Base_BeforeDiscussionTabs_Handler($Sender) {
    

    // if (!C('Plugins.Voting.Enabled'))
    // return;

        echo ' <li'.($Sender->RequestMethod == 'popular' ? ' class="Active"' : '').'>'
            .Anchor(T('Popular All Time'), '/discussions/popular', 'PopularDiscussions TabLink')
        .'</li>';
        echo ' <li'.($Sender->RequestMethod == 'populartoday' ? ' class="Active"' : '').'>'
            .Anchor(T('Popular Today'), '/discussions/populartoday', 'PopularDiscussions TabLink')
        .'</li>';
        echo ' <li'.($Sender->RequestMethod == 'popularthisweek' ? ' class="Active"' : '').'>'
            .Anchor(T('Popular This Week'), '/discussions/popularthisweek', 'PopularDiscussions TabLink')
        .'</li>';
    
    }
    

    // public function CategoriesController_BeforeDiscussionContent_Handler($Sender) {
    // $this->DiscussionsController_BeforeDiscussionContent_Handler($Sender);
    // }

    /**
    * Load popular discussions.
    */
    public function DiscussionsController_Popular_Create($Sender) {
    // if (!C('Plugins.Voting.Enabled'))
    // return;

      $Sender->Title(T('Popular of All Time'));
      $Sender->Head->Title($Sender->Head->Title());
    
      $Offset = GetValue('0', $Sender->RequestArgs, '0');
    
      // Get rid of announcements from this view
      if ($Sender->Head) {
         $Sender->AddJsFile('discussions.js');
         $Sender->AddJsFile('bookmark.js');
         $Sender->AddJsFile('options.js');
         $Sender->Head->AddRss($Sender->SelfUrl.'/feed.rss', $Sender->Head->Title());
      }
      if (!is_numeric($Offset) || $Offset < 0)
         $Offset = 0;
    
      // Add Modules
      $Sender->AddModule('NewDiscussionModule');
      $BookmarkedModule = new BookmarkedModule($Sender);
      $BookmarkedModule->GetData();
      $Sender->AddModule($BookmarkedModule);
    
      $Sender->SetData('Category', FALSE, TRUE);
      $Limit = C('Vanilla.Discussions.PerPage', 30);
      $DiscussionModel = new DiscussionModel();
      $CountDiscussions = $DiscussionModel->GetCount();
      $Sender->SetData('CountDiscussions', $CountDiscussions);
      $Sender->AnnounceData = FALSE;
        $Sender->SetData('Announcements', array(), TRUE);
      $DiscussionModel->SQL->OrderBy('d.Score', 'desc');
      $Sender->DiscussionData = $DiscussionModel->Get($Offset, $Limit);
      $Sender->SetData('Discussions', $Sender->DiscussionData, TRUE);
      $Sender->SetJson('Loading', $Offset . ' to ' . $Limit);
    
      // Build a pager.
      $PagerFactory = new Gdn_PagerFactory();
      $Sender->Pager = $PagerFactory->GetPager('Pager', $Sender);
      $Sender->Pager->ClientID = 'Pager';
      $Sender->Pager->Configure(
         $Offset,
         $Limit,
         $CountDiscussions,
         'discussions/popular/%1$s'
      );
    
      // Deliver json data if necessary
      if ($Sender->DeliveryType() != DELIVERY_TYPE_ALL) {
         $Sender->SetJson('LessRow', $Sender->Pager->ToString('less'));
         $Sender->SetJson('MoreRow', $Sender->Pager->ToString('more'));
         $Sender->View = 'discussions';
      }
    
      // Set a definition of the user's current timezone from the db. jQuery
      // will pick this up, compare to the browser, and update the user's
      // timezone if necessary.
      $CurrentUser = Gdn::Session()->User;
      if (is_object($CurrentUser)) {
         $ClientHour = $CurrentUser->HourOffset + date('G', time());
         $Sender->AddDefinition('SetClientHour', $ClientHour);
      }
    
      // Render the controller
      $Sender->View = 'index';
      $Sender->Render();
    

    }

    public function DiscussionsController_PopularThisWeek_Create($Sender) {
    // if (!C('Plugins.Voting.Enabled'))
    // return;

      $Sender->Title(T('Popular This Week'));
      $Sender->Head->Title($Sender->Head->Title());
    
      $Offset = GetValue('0', $Sender->RequestArgs, '0');
    
      // Get rid of announcements from this view
      if ($Sender->Head) {
         $Sender->AddJsFile('discussions.js');
         $Sender->AddJsFile('bookmark.js');
         $Sender->AddJsFile('options.js');
         $Sender->Head->AddRss($Sender->SelfUrl.'/feed.rss', $Sender->Head->Title());
      }
      if (!is_numeric($Offset) || $Offset < 0)
         $Offset = 0;
    
      // Add Modules
      $Sender->AddModule('NewDiscussionModule');
      $BookmarkedModule = new BookmarkedModule($Sender);
      $BookmarkedModule->GetData();
      $Sender->AddModule($BookmarkedModule);
    
      $Sender->SetData('Category', FALSE, TRUE);
      $Limit = C('Vanilla.Discussions.PerPage', 30);
      $DiscussionModel = new DiscussionModel();
      $CountDiscussions = $DiscussionModel->GetCount(array("DateInserted >" => Gdn_Format::ToDateTime(strtotime('-1 week'))));
      $Sender->SetData('CountDiscussions', $CountDiscussions);
      $Sender->AnnounceData = FALSE;
        $Sender->SetData('Announcements', array(), TRUE);
      // This is to sort posts by Discussion Score
      $DiscussionModel->SQL->OrderBy('d.Score', 'desc');
      // Setting date limit here
      $Whereas = array("DateInserted >" => Gdn_Format::ToDateTime(strtotime('-1 week')));
      $Sender->DiscussionData = $DiscussionModel->Get($Offset, $Limit, $Whereas);
      $Sender->SetData('Discussions', $Sender->DiscussionData, TRUE);
      $Sender->SetJson('Loading', $Offset . ' to ' . $Limit);
    
      // Build a pager.
      $PagerFactory = new Gdn_PagerFactory();
      $Sender->Pager = $PagerFactory->GetPager('Pager', $Sender);
      $Sender->Pager->ClientID = 'Pager';
      $Sender->Pager->Configure(
         $Offset,
         $Limit,
         $CountDiscussions,
         'discussions/popular/%1$s'
      );
    
      // Deliver json data if necessary
      if ($Sender->DeliveryType() != DELIVERY_TYPE_ALL) {
         $Sender->SetJson('LessRow', $Sender->Pager->ToString('less'));
         $Sender->SetJson('MoreRow', $Sender->Pager->ToString('more'));
         $Sender->View = 'discussions';
      }
    
      // Set a definition of the user's current timezone from the db. jQuery
      // will pick this up, compare to the browser, and update the user's
      // timezone if necessary.
      $CurrentUser = Gdn::Session()->User;
      if (is_object($CurrentUser)) {
         $ClientHour = $CurrentUser->HourOffset + date('G', time());
         $Sender->AddDefinition('SetClientHour', $ClientHour);
      }
    
      // Render the controller
      $Sender->View = 'index';
      $Sender->Render();
    

    }

  • public function DiscussionsController_PopularToday_Create($Sender) {
    // if (!C('Plugins.Voting.Enabled'))
    // return;

      $Sender->Title(T('Popular This Week'));
      $Sender->Head->Title($Sender->Head->Title());
    
      $Offset = GetValue('0', $Sender->RequestArgs, '0');
    
      // Get rid of announcements from this view
      if ($Sender->Head) {
         $Sender->AddJsFile('discussions.js');
         $Sender->AddJsFile('bookmark.js');
         $Sender->AddJsFile('options.js');
         $Sender->Head->AddRss($Sender->SelfUrl.'/feed.rss', $Sender->Head->Title());
      }
      if (!is_numeric($Offset) || $Offset < 0)
         $Offset = 0;
    
      // Add Modules
      $Sender->AddModule('NewDiscussionModule');
      $BookmarkedModule = new BookmarkedModule($Sender);
      $BookmarkedModule->GetData();
      $Sender->AddModule($BookmarkedModule);
    
      $Sender->SetData('Category', FALSE, TRUE);
      $Limit = C('Vanilla.Discussions.PerPage', 30);
      $DiscussionModel = new DiscussionModel();
      $CountDiscussions = $DiscussionModel->GetCount(array("DateInserted >" => Gdn_Format::ToDateTime(strtotime('-1 day'))));
      $Sender->SetData('CountDiscussions', $CountDiscussions);
      $Sender->AnnounceData = FALSE;
        $Sender->SetData('Announcements', array(), TRUE);
      // This is to sort posts by Discussion Score
      $DiscussionModel->SQL->OrderBy('d.Score', 'desc');
      // Setting date limit here
      $Whereas = array("DateInserted >" => Gdn_Format::ToDateTime(strtotime('-1 day')));
      $Sender->DiscussionData = $DiscussionModel->Get($Offset, $Limit, $Whereas);
      $Sender->SetData('Discussions', $Sender->DiscussionData, TRUE);
      $Sender->SetJson('Loading', $Offset . ' to ' . $Limit);
    
      // Build a pager.
      $PagerFactory = new Gdn_PagerFactory();
      $Sender->Pager = $PagerFactory->GetPager('Pager', $Sender);
      $Sender->Pager->ClientID = 'Pager';
      $Sender->Pager->Configure(
         $Offset,
         $Limit,
         $CountDiscussions,
         'discussions/popular/%1$s'
      );
    
      // Deliver json data if necessary
      if ($Sender->DeliveryType() != DELIVERY_TYPE_ALL) {
         $Sender->SetJson('LessRow', $Sender->Pager->ToString('less'));
         $Sender->SetJson('MoreRow', $Sender->Pager->ToString('more'));
         $Sender->View = 'discussions';
      }
    
      // Set a definition of the user's current timezone from the db. jQuery
      // will pick this up, compare to the browser, and update the user's
      // timezone if necessary.
      $CurrentUser = Gdn::Session()->User;
      if (is_object($CurrentUser)) {
         $ClientHour = $CurrentUser->HourOffset + date('G', time());
         $Sender->AddDefinition('SetClientHour', $ClientHour);
      }
    
      // Render the controller
      $Sender->View = 'index';
      $Sender->Render();
    

    }

Sign In or Register to comment.