Hi toemaz, maybe I'm misunderstanding, but I can't see how to mimic the behavior as provided from the screenshot. As well, some of the code seems redundant from the user_visits module. For exampe the Latest visitors block already exists fomr user_visits and seems unnecessary.

Would you be interesetd in taking a different approach that actually mimics the behavior of the screenshot more closely?

So, remove the block for latest visitors and let user_visits deal with displaying user pictures of latest visitors. And recode this module to focus on a single function that takes an additional variable as a time period, e.g. user_visits_adv_get('history', $account->uid, 1), where 1 equal one day. Off course you could create blocks as well that serve this function, but it would be nice if users could theme the function individually.

Then e.g. the following could give the screenshot result:

<?php
$today = user_visits_adv_get('history', $account->uid, 1);
$week = user_visits_adv_get('history', $account->uid, 7);
$total = user_visits_adv_get('total', $account->uid);
?>

Then the users can specify this themselves in a function rather than one sidewide value. This is very specalative and I wouldn't know at all how to do this. But in theory it would be easier the get the result of the screen shot. OR I am missing something completely and the module does this already?

Kind regards,

Marius

CommentFileSizeAuthor
#2 cron.jpg36.39 KBmariusooms
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

toemaz’s picture

It sure does look redundant but it isn't. The user visits adv module takes the data from the user visits table, collects it and adds it to its own table per user. This is done on every cron job and the user visits table is emptied. So the user visits logic, blocks, whatever, can't be used anymore bc it does not hold data anymore.

The reason for this approach is performance. Consider a 500.000+ users Drupal website with ten thousands of hits every day. The user visits table would fill up very quickly and kill the database performance.

You might be right the code might need some work in terms of transparency, but I will not put much work in it for now. Your initial theming request can be obtained with no problems at all. The only thing you need to do is make a sum of the past 7 days in the history array.

I hope this answers your questions a bit. The code will speak for itself.

mariusooms’s picture

FileSize
36.39 KB

Hi Toemaz, thanks for the explanation. I saw that the table indeed emptied after cron. I'm also understanding the history slightly better, however I don't understand the following screenshot. Here I just ran cron and you can see that 'ryan' and ' Gatekeeper' just visited my profile. However, the history still displays as '0 0 0 2 0 0 0'. Shouldn't that be '0 0 0 2 0 0 2'? The total views count is 4 as well, so that would make sense then.

UPDATE: After 24 hours the count is added to the 1st array value. However, I think it should be added to the 2nd array value. The 1st value _should_ count visits within the first day. This would give a better count result as the $total count _also_ counts current visits. In the current case, the 'total' count will keep going up in the block while the history count lags behind 24 hours.

A print_r(array_keys($history)) shows this result:

Array ( [0] => 9057 [1] => 9058 [2] => 9059 [3] => 9060 [4] => 9061 [5] => 9062 [6] => 9063 )

From the database a recent visit is stored under key 9064, which equals today, but is not loaded in the array? Therefore I thought the following change would work, but doesn't.

<?php
function user_visits_get_history_key($time) {
  // constructed with the year [0-99] and the day [0-356]
  return (date('y', $time) * 1000) + date('z', $time) + '1'; //add 1 so it counts today
}
?>

I hope this makes sense...somehow the added 1 doesn't work and throws off the databse key value as well. Any thoughts on this?

Regards,

Marius

mariusooms’s picture

Also...I figured out the sum for a week, but outputting a specific value, namely 'today' or array value [0], does not work.

<?php
function theme_user_visits_adv_history($history, $total) {
  $week = array_sum($history); //Add up the array to a total
  $today = $history[6]; //Retrieve a specific array value
  
  $output = '<div>'. t('!total views all-time', array('!total' => $total)) .'</div>';
  $output .= '<div>'. t('!week views this week', array('!week' => $week)) .'</div>';
  $output .= '<div>'. t('!today views today', array('!today' => $today)) .'</div>';

  return $output;
}
?>

I appreciate your support ;)

Marius

mariusooms’s picture

Hi toemaz...I made some slight progress, but no working solution yet. I updated the 2 posts above ;).

I realize you are very busy, but once I'm on an issue, I can't let it go. However, I can't seem to figure it out. Once I get this I can continue the port to Drupal 6.

Thanks,

Marius

toemaz’s picture

Hi Marius, thanks for all the updates. It seems you may have found an issue so I'll look into this because it will for sure affect my own website as well. That will be tomorrow when DrupalCon is over. Keep on posting.

toemaz’s picture

Status: Active » Fixed

Hi Marius,

I found the bug in user_visits_adv_history_block()

    $history = array_fill(user_visits_get_history_key(time()) - $limit, $limit, 0);

has to be

    $history = array_fill(user_visits_get_history_key(time()) - $limit + 1, $limit, 0);

Also, if you want to get the number of visits for today, keep on using array_pop($history)

Will create another release for D5 and see whether I can fix the D6 release so you can start using the official release.

mariusooms’s picture

Ah...you see I tried:

<?php
    $history = array_fill(user_visits_get_history_key(time() + 1) - $limit, $limit, 0);
?>

but my php knowledge is read only ;) not write...

Thanks,

Marius

toemaz’s picture

The 6 dev snapshot is available now. I would propose you try to use it locally and if something does not work out, feel free to post an issue. Thanks for all the work so far. I hope it works out for you.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.