Gossamer Forum
Home : Products : DBMan : Customization :

Already viewed record

Quote Reply
Already viewed record
Ok, here's my next issue:

I need a marker to indicate that you have already viewed a record.
Esentially the way my db works is that when you view a record it writes to a db a userid and the recordid.
So what I would like to add is a check on that db prior to listing records.

if userid=(current user) and recordid=(a record to be displayed) then display viewed image otherwise display unviewed image.

Thanks in advance!
Adam

Quote Reply
Re: Already viewed record In reply to
Is this within the current session or forever?

JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: Already viewed record In reply to
Give me about a day and I will post more details. i have been thinking more about it and somewhat mapped out an if then else type deal.

Be back after some sleep........

Adam

Quote Reply
Re: Already viewed record In reply to
OK. Now that I am awake and even more tired..... Smile

My first message wasn't very clear. Let me re-explain the scenario. When a user views a record it writes to a log script for me. The way my log script looks is:
UID|ID|Date|Time|field5|field6|field7|field8
anr|4|25-Jul-2000|20:46:09|field5|field6|field7|field8

Now what I would like to do is to have the already viewed marker only if that specific record was viewed within the last 24 hours. For instance.

If UID=(current userid) then go on (if not then not viewed)
If ID=(ID to display) then go on (if not then not viewed)
If Date=(today's date) then already viewed (if not then go on)
If Date=(Yesterday's date) then go on (if not then not viewed)
If time=(greater than current time)then already viewed (if not then not viewed)

So what we have here is a check on the userid, if it matches then we need to check the ID number, if it matches we need to check the date, if it's today we know it's been viewed within the last 24 hours, if it's yesterday then we need to check the time (which is in military format). If the current time is greater than the logged time then not viewed. If it is less than the logged time, then it's within 24 hours because we already passed the other tests...so viewed.

I hope this isn't to complicated. It sounds like a bunch of if then statments really. The hardest part is where to place the code. I need it to do a check each time a record is to be displayed. What I would like is Image1 displayed when not viewed within 24 hours and Image2 displayed when viewed within 24 hours.

I'm also hoping that this doesn't slow down the program much. Any thoughts?

Thanks for the help!
Adam

Quote Reply
Re: Already viewed record In reply to
rather than use log files - wouldn't it be far easier to use cookies ?

tom

Quote Reply
Re: Already viewed record In reply to
You would want to put the code in sub html_record, as the record is being displayed.

Depending on how many items are in your log file, it could take quite a long time to go through all of the records.

You might think about writing the necessary fields to the session file for the user. That way you wouldn't have to search through other userids.

What did we end up with when you were working on preventing users from logging in more than once per day?

JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: Already viewed record In reply to
JPD,

Users are allowed to login more than once per day, however they will keep the same session file for 24 hours, thus the random number that was generated will change only once every 24 hours. By writing my log to the session file it would make sense doing the check there....after all, I only care about a 24 hour period and the file stays active for 24 hours....No new file will be generated (even with multiple logins) until after 24 hours. BTW: This works GREAT! Smile

So what I would end up doing is needing only to write the ID to the session file, after all each user has their own file, and it's only valid for 24 hours. Good Idea!

As of now my auth file looks like this:
$in{'Random'}:$view:$add:$del:$mod:$admin:$ENV{'REMOTE_HOST'}
6:1:1::1::IPAddress

How is the best way to add to this, and then to call from this for a check?

Adam

Quote Reply
Re: Already viewed record In reply to
ego,

For some things I would rather not deal with cookies. By keeping the files on my end, someone can go from one computer to another and still have no problems. Equally, those that choose not to accept cookies would not gain from certain features.

Thanks for suggesting though!
Adam

Quote Reply
Re: Already viewed record In reply to
Let me clarify one point here. I know where the code to write to the auth has to go. This is in something else that I am using to track. I just need to know the code to do that (open auth file, add the new id number and close file --- open and close I can do Smile it's more of adding the ID number without deleting everything else that's already in there.

Then I need to know how to call that up when it's time to view (html_record you said) to determine which image to display.

Adam

Quote Reply
Re: Already viewed record In reply to
I've been pondering this for a bit to figure out the best way to do it.

It would probably be best to go through these steps:

open the file for reading
read the file into an array
close the file
see if the current record id is in there
if it is -- set a variable so you can print out the right graphic
if it isn't --
open the file for appending
write the record id to the file
close the file

Let's get down to business, then

Put this at the beginning of sub html_view_success. (That way, you only have to read the file once if there are several records returned from the search. If you're using the short/long display and this is only going to apply to the long version, you can put this in sub html_record.)

Code:

open(USER, "<$auth_dir/$db_uid") or &cgierr("unable to open auth file: $auth_dir/$uid. Reason: $!\n");
@user = <USER>;
close USER;

# Below is the new code I refer to a couple of posts down
foreach $user_rec (@user) {
chomp $user_rec;
}


In sub html_record, after

my (%rec) = @_;

add

Code:

$rec{$db_key} =~ s/<.?B>//g;
$viewed = 0;
if (grep $_ eq $rec{$db_key},@user) { # it's in there
$viewed = 1;
}
else { # it's not in there
open(USER, ">>$auth_dir/$db_uid") or &cgierr("unable to open auth file: $auth_dir/$uid. Reason: $!\n");
print USER "$rec{$db_key}\n";
close USER;
}
Then you can use the variable $viewed to determine which graphic to print out.

Of course, this is with my usual caveat -- I have not tested this, except to check it for syntax errors. Smile


JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: Already viewed record In reply to
Will this:

print USER "$rec{$db_key}\n";

print whatever is already in the db in addition to the new record id? Or only the new record id, and in that case will it cause the old info to be lost?

Adam

Quote Reply
Re: Already viewed record In reply to
If you'll notice when the file is opened, it uses the following syntax:

open(USER, ">>$auth_dir/$db_uid")

The >> means "open for appending." It will just tack on whatever is printed to the end of the file. It is the same syntax the db.cgi file uses when it adds a record.

JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: Already viewed record In reply to
Perfect! I think the best thing about your help is that I actually learn something!

And since I'm learning.....let me ask you. When it opens the file and grabs what's in it, how does it distinguish when comparing the id to view with what's in the file between an id=1 and a permission=1 and in my case random=1 or id=8 and random=8, etc.....

Adam

Quote Reply
Re: Already viewed record In reply to
Well, it doesn't really. But it looks at the whole line. Since the first line has all sorts of stuff in it, like colons and multiple numbers, it's unlikely that you're going to have a key value for a record that has all that stuff.

I could give you code that would skip that line if you think it's necessary, or if there's a problem when you give it a try.

Oops. I just thought about a couple of things.

When you write the first line to the file, be sure to end it with \n so there will be a new line.

The other thing is the new lines. Hmmmmm.

Look back at my post with the code in it. There's going to be some more code there in just a sec.

JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: Already viewed record In reply to
To use the $viewed

if ($viewed == 1) {
$image .= qq|<all the viewed image stuff>|;
}
else {
$image .= qq|<all the not viewed image stuff>|;
}


Should this work?

Thanks!
Adam

Quote Reply
Re: Already viewed record In reply to
OK. So the \n causes it to go to the next line. So each ID will then be written to a seperate line, rather than having a | or : to seperate. So then as long as it doesn't think a 10 = 1 then we are ok.

I see the new line you referred to......not sure what it does, but I see it Smile

Adam

Quote Reply
Re: Already viewed record In reply to
I don't think there will be a problem with 10=1 (or 1 = 10, for that matter). From what I understand, the grep command looks at each entry of the array as a whole. Wait a minute. Lemme test it.

It seems to work just as I expected. Smile

The new lines I added take the "newline" character off of each of the lines. Let's see if I can explain.

Your file looks like this:

6:1:1::1::IPAddress
34
87
95
147
6


The line

@user = <USER>;

puts each line of the file into an array entry. The way the script reads it is this:

$user[0] = "6:1:1::1::IPAddress\n"
$user[1] = "34\n"
$user[2] = "87\n"
$user[3] = "95\n"
$user[4] = "147\n"
$user[5] = "6\n"


Since your key value is actually 95 and not 95\n, the script would not recognize that the record had been viewed before.

The line

chomp $user_rec;

cuts off any \n character at the end of the variable. Nice thing about "chomp" is that, if there isn't a newline there, it does nothing. (This means you don't have to check to see if it's there first.)

Does this help? Smile



JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: Already viewed record In reply to
I was just starting to implement the code and realized something.

Correct me if I'm wrong....

I have my auth file scheduled to be removed 24 hours later. The problem is that the time on the file will keep changing every time it writes to it. So.....a single auth file could end up active forever (assuming a user views at least one record every 23 hours --> starting a new 24 hour period for the file with each view).

Assuming I am correct, my initial thought is instead of writing to the auth file, how about creating a new file for each user in a viewed directory with only the viewed id numbers in it. Then, when the auth file comes up for deletion, have it delete the viewed file as well.

To write it to a viewed directory I would create the directory and add to default.cfg
$viewed_dir = $db_script_path . "/viewed";

Then change in your above code
$auth_dir to $viewed_dir

What is going to happen when the file is not there initially?
If that has a problem then in the auth.pl I would just create the viewed file at the same time as it creates the auth file, simply writing either just \n or $db_uid to the file.

So now I just need to delete the viewed file when the auth file is deleted.
Again in the auth.pl
under:
unlink ("$auth_dir/$file");
add:
unlink ("$viewed_dir/$file");

I don't have the time to try it this minute, but does it all sound right?

Oh, and the code that I posted a few up on how to use the $viewed....is that correct?

Thanks!
Adam


Quote Reply
Re: Already viewed record In reply to
In Reply To:
I have my auth file scheduled to be removed 24 hours later. The problem is that the time on the file will keep changing every time it writes to it.
You are right. Darn it!! Smile And there is no statistic available that is creation time.

Your solution is very good. Yes, it would be best to create the "viewed" file at the same time as the session file. And your code for deleting the file is exactly right. You don't need to write anything to the file when it is created. It's "legal" to have an empty file.

You have really been learning this! Smile

Your code for using the $viewed variable is just fine. (Sorry I missed it before.) You could just use

if ($viewed) {

but what you have works just as well.

JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: Already viewed record In reply to
YIPEEE!!!!!! Smile

Through initial testing, it works!!! Of course I always like to run it a few days before I'm completly convinced all the bugs are gone, but as of now....it works!

On to the next subject.....my reading assignment Smile

Thanks!
Adam

Quote Reply
Re: Already viewed record In reply to
Yahooooooooo! Laugh

Excellent. I feel almost as good as you do. Smile

JPD
http://www.jpdeni.com/dbman/