Jump to content

New articles to users -- with cookie?


Manaus
 Share

Recommended Posts

Ok, gotcha. I did this on a site a while back. I'll dig up the code and get back to you in a minute. Actually I did this on the very first PW site I built, so there might be a better way, but this still works.

Create a new datetime field called last_page_load and add it to the user template (remember to "Show System Templates" so you have access to it).

Put this code somewhere that is included on every page of the site: 

if ($user->isLoggedin()) { 
    $user->last_page_load = date('Y-m-d H:i:s'); 
    $user->of(false); 
    $user->save(); 
}

Then you can do this:

foreach($articles as $article){ 
    if($user->isLoggedin() && $article->modified > $user->last_page_load){
        // echo article here
    }
}

Let me know how you go with that. I don't think I have missed any components in making this work, and maybe there is a better approach, but one advantage of this over cookies is that it won't matter whether the user clears their cookies or not, or if they are visiting from different devices.

EDIT: Thinking through this - in my case I had already limited $articles with a find. You may want to do the same as foreach'ing through 100's/1000's of articles won't be a good idea, so you could use something like this:

$new_articles = $pages->find("template=articles,modified>{$user->last_page_load}"); 
  • Like 7
Link to comment
Share on other sites

Adrian, remember to restore the output formatting state of $user, i.e. $user->of(true); unless you sure you won't be outputting anything from user after that block of code. But safer just to restore it to of(true) after you've saved. 

  • Like 2
Link to comment
Share on other sites

  • 1 month later...

I stumbled upon this issue again, but it's getting curious developments...

It might happen that posts are often modified [adding or removing users from them], so the posts are now appearing 'new' quite often.

Is there another way to use a per-user state of the posts?

Just thinking: I might add a list of this-user-has-read linked pages to each post, and everytime the user reads the post, flag the checkbox.

Thanks for any suggestion...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...