Gossamer Forum
Home : Products : DBMan : Customization :

user account stats

Quote Reply
user account stats
What I would like to to is to display some user account info on my home page.
Users are allowed a max of 5 recs, and I'm using your mod(JPD) to return the amount of used recs and what they have left, as per below.
Code:
open (DB, "<$db_file_name") or &cgierr("error in validate_records. unable to open db file:
$db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
$count = 0;
LINE: while (<DB> ) {
(/^#/) and next LINE;
(/^\s*$/) and next LINE;
$line = $_;
chomp ($line);
@data = &split_decode($line);
if ($data[$auth_user_field] eq $db_userid) {
++$count;
}
}
close DB;
$left = $db_maximum_records - $count;
What I would like to do is to collect the dates of added recs, if any. I am also going to use the autodelete mod, which I havent yet Installed. That would be set to say 30days.
I would like to return that as well, which may be as simple as deducting 30days from the rec date.
Also I have a couple of if else statements based on $left, which work well, and I tried to do the same with the add link in html_footer, but I couldn't get it to work.
ie,
Code:
if ($left==5){}
else {
print qq!| <A HREF="$db_script_link_url&add_form=1">Add</A> ! if ($per_add);}
although it would have to stay there for admin.
This is 2 questions in 1, sorry
thanks
bob
Quote Reply
Re: user account stats In reply to
I'm not sure what you want to do with the dates. If you could give me an example, I'm sure I can help you out.

Regarding the link, use

Code:
if ($left or $per_admin) {
print qq!| <A HREF="$db_script_link_url&add_form=1">Add</A> ! if ($per_add);
}

The variable $left is the number of records the user has remaining to add. So, if they have already entered 4 records, $left will be equal to 1.



------------------
JPD





Quote Reply
Re: user account stats In reply to
What I would to do with the dates is to display a summery of when there records were added or modified, and the x number of days before autodelete.
ie,
You have 3 active records,
1-added 3-July-1999 - has x days before auto delete.
2- and so on.
With the above code which does a pass through the db to see how many active records the user has, when it finds an active record, store the rec date to an array, and print it out. You would also need to display the x number of days before the record is autodeleted.
You would need to deduct the rec date(in days) from todays date(in days), and deduct the difference from the x number of days to deletion, I think...
In other words, if the record was added 10 days ago, and autodelete was set to 30 days after the recored add date, then they have 20 days to go before it gets deleted.
I hope this makes sense.
They would need to be sorted cronological if possible.
thanks
bob
Quote Reply
Re: user account stats In reply to
This is a complex one and I just got up. Smile I'm going to have to get back to you.


------------------
JPD





Quote Reply
Re: user account stats In reply to
Okay. Here's what I came up with.

Code:
open (DB, "<$db_file_name") or &cgierr("error in validate_records. unable to open db file:
$db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
$count = 0;
LINE: while (<DB> ) {
(/^#/) and next LINE;
(/^\s*$/) and next LINE;
$line = $_;
chomp ($line);
@data = &split_decode($line);
if ($data[$auth_user_field] eq $db_userid) {
push (@records,$data[5]);
}
}
close DB;
$number_of_records = scalar(@records);
$left = $db_maximum_records - $number_of_records;

print "You have $number_of_records active records.<BR>";
for ($i=0; $i<scalar(@records) ;++$i) {
$expires = int(((&date_to_unix($records[$i]) + (30 * 86400)) - time())/86400); # days left
print $i + 1;
print " - added $records[$i] - has $expires days before autodelete<BR>";
}

You'll need to change the 5 in the line

push (@records,$data[5]);

to match the number of your date field.

Also, if you have a variable for the number of days before autodelete, you can put the variable in place of the 30 in the line

$expires = int(((&date_to_unix($records[$i]) + (30 * 86400)) - time())/86400); # days left

The number of days left does not include the current day. On the day of expiration, it will say

0 days before autodelete

on the day the record was added, it will say

29 days before autodelete

If you want to include the current day, you'll need to increase the 30 to 31.

This list is not sorted. It is in the order the records were saved to the database. I haven't yet figured out how the "sort by date" works. But they should be listed in the order they were added. The only difference would be if you have the date change when a record is modified.

Quote:
I have the max recs mod installed, and these to peices of code, do the same thing, which is to pass through the db to find out how many active recs there are for the current user. Is there any way to intergrate these codes into one.

Possibly. You might be able to use a subroutine. Except that, since you want the date now, instead of just the number of records, the above code has changed.

Quote:
Secondly, there seems to be a lot of passes made through the db to gather the required user info and to to keep a track on max records, would it be better to put the user info into a seperate db and update the info when a record was added or deleted.

If you want to do that, it would be a good project to work on.



------------------
JPD





Quote Reply
Re: user account stats In reply to
Sorry about this being so complicated. If you can not understand I can try and explain it again.
A couple of questions which relate to the number active/letf mod above.
I have the max recs mod installed, and these to peices of code, do the same thing, which is to pass through the db to find out how many active recs there are for the current user. Is there any way to intergrate these codes into one.
Secondly, there seems to be a lot of passes made through the db to gather the required user info and to to keep a track on max records, would it be better to put the user info into a seperate db and update the info when a record was added or deleted.
I will have autodelete installed and that may be a problem doing it that way.
Also it would be hard to keep a track of the rec date and days to deletion this way, thats if we can solve that one.
Sorry to jump ahead of my origional question,
but I can't continue untill I solve this.
thanks
bob
Quote Reply
Re: user account stats In reply to
I'm sorry. I forgot about this. I guess I was still asleep when I answered before and it didn't get into my brain that I still had something to do.

Again, though, I'm going to have to put you off for a bit. I'll have to go offline to write the code. I'll remember this time. Smile

------------------
JPD





Quote Reply
Re: user account stats In reply to
JPD thanks for the code, I haven't had time to try it yet, but it should work fine. I wish I just had a little bit of your smarts, then I wouldn't have to ask so many questions.
I would like to continus with this seperate db idear, it's stuck in my head and I realy need to solve it.
The thing that would realy complicate this are the return and update of the record dates, so i'll forget them for now untill I explain this.
If you could keep a seperate database to keep track of the rec count, you could then dispense with the max recs mod and the above mod, remembering we have forgotten about the rec dates for the moment.
Every time a rec was added or deleted the user stats db would be updated.
This could then be referenced to keep a track of max recs and a lot of other stuff.
You could also use this as a user profile db if you wished.
This would speed things up as you would only need to pass through the smaller db and not the larger main db all the time, very handy if you had a lot of large recs per user.
A crude realtional db idea.
There were lots of things I thought of which I carn't seem to remember now.
You could use it as a reference for auto delete, which could give it a quick check, to see if there any user rec in the db before it went off to have a look for itself.
It maye be a ok way to knock out add, modify or delete, when needed with a quick reference to it. I don,t know..
Let me know If I'm barking up the wrong tree here.
I don't see any ideal way to handle the rec dates, and there may have to be a pass througt the main db every time a user enters home page, although I do have an idea, but not now.
Let me know if you think this is a good idea, and if so can we work on it, as I am stuck untill I solve this.
thanks
bob
Quote Reply
Re: user account stats In reply to
 
Quote:
thanks for the code, I haven't had time to try it yet, but it should work fine.

You're welcome. Please let me know when you do try it so I will know whether it does what you want or not.

Quote:
I wish I just had a little bit of your smarts, then I wouldn't have to ask so many questions.

I'm not really that smart. Smile I've just worked a lot with the code. Part of the reason that I know so much is that I couldn't get a lot of my questions answered when I was asking them. I had to learn it myself. The other reason is that I have answered so many questions here. I've had to study the code a lot in order to answer them. And I'm still learning more just about every day.

I understand about the advantages you see for the separate db, but what I don't understand is exactly what you envision the data to be and how it would be used.

The only thing that I could see that might work would be to write to two files when a record is added. One would be the full record and one would be the just the key field, the userid, and the date. You would also have to add code to sub delete_records and, maybe, modify_record (if you change the date when a record is modified).


------------------
JPD





Quote Reply
Re: user account stats In reply to
JPD probably the best idea here is to revert back to plan 1.
After thinking about this for awhile I realy don't think I can avoid mulitple passes througt the db to gather the info I need.
Even using two files, as you suggest, may not help as it would still have to search through them as well.
So, as it stands I still have two peices of code that bacially do the same thing, being maxrecs and the user rec stats code you did above. Can we combine the two into a seperate sub routine, that can be used to controll maxrecs, and to return the user rec stats to home page.
Maxrecs is currently in sub validate, and the user stats one in html home.
Then you may be able to help me with the next bit to this.
thanks
bob
ps,
My field is Audio, your field is Perl, I consider myself good at what I do, and I and most people who use this BBS know you are good at what you do. With out your excellent help there would be far far fewer DBman databases in the internet world.
You have tons of smarts.
Quote Reply
Re: user account stats In reply to
 Smile I have to respond to your "PS" first. Thank you!!!

You probably can put them together into a subroutine. I'd need to see the code you are using in sub validate_records first. (I know I wrote the orginal code for it, but that was a long time ago!! Smile )


------------------
JPD