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.

One Session - One View

edited June 2012 in Vanilla 2.0 - 2.8

\forum\applications\vanilla\models\class.discussionmodel.php

How to modify this function to add one view on the one session?

    * @since 2.0.0
    * @access public
    *
    * @param int $DiscussionID Unique ID of discussion to get +1 view.
    */
    public function AddView($DiscussionID, $Views = 0) {
      $Views++;
      if (C('Vanilla.Views.Denormalize', FALSE) && Gdn::Cache()->ActiveEnabled()) {
         $CacheKey = "QueryCache.Discussion.{$DiscussionID}.CountViews";

         // Increment. If not success, create key.
         $Incremented = Gdn::Cache()->Increment($CacheKey);
         if ($Incremented === Gdn_Cache::CACHEOP_FAILURE)
            Gdn::Cache()->Store($CacheKey, $Views);

         // Every X views, writeback to Discussions
         if (($Views % C('Vanilla.Views.DenormalizeWriteback',100)) == 0) {
            Gdn::Database()->Query("UPDATE {$this->Database->DatabasePrefix}Discussion 
            SET CountViews={$Views}
            WHERE DiscussionID={$DiscussionID}");
         }
      } else {
         $this->SQL
            ->Update('Discussion')
            ->Set('CountViews', 'CountViews + 1', FALSE)
            ->Where('DiscussionID', $DiscussionID)
            ->Put();
      }
    } 

Answers

  • Is this a question or an answer??????????????

    If its a question. What are you trying to do?

    If it is an answer, what did you do?

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

  • 422422 Developer MVP

    Lol

    There was an error rendering this rich post.

  • edited June 2012

    It is a question.

    I found this function in the core file class.discussionmodel.php

    It increments a view by one when called.

    The question is how to modify so that incrementing of discussion is done only one time for the current session. So that a user could view so many times as he wants without incrementing the counter but only one time a session.

    So modify - that "views" doesn't show the discussion count when move between pages and refreshing the page with F5

    but really shows a quantity of users (sessions) which viewed the discussion.

    How to do so?

  • Well, the most simple way is

              $cname="View".$DiscussionID;
    
              if (isset($_COOKIE[$cname]))
              {
    
              } else {
    
                 setcookie($cname, $DiscussionID, time()+3600);  
    
                 $this->SQL
                    ->Update('Discussion')
                    ->Set('CountViews', 'CountViews + 1', FALSE)
                    ->Where('DiscussionID', $DiscussionID)
                    ->Put();
              }
    

    Now I had to find out of double hit

  • edited June 2012

    Thnx to all )))))))))

            public function AddView($DiscussionID, $Views = 0) {    
              session_start();
              $cname="DiscussionViewed".$DiscussionID;      
              if (!isset($_SESSION[$cname]))
              {
                 $_SESSION[$cname]='';         
                 $this->SQL
                    ->Update('Discussion')
                    ->Set('CountViews', 'CountViews + 1', FALSE)
                    ->Where('DiscussionID', $DiscussionID)
                    ->Put();     
              }
            }
    
Sign In or Register to comment.