Commit f3068aab authored by Richard Mansfield's avatar Richard Mansfield

Log page hits on view pages and add them to the db once a day

Signed-off-by: default avatarRichard Mansfield <richardm@catalyst.net.nz>
parent 2e53cfc1
......@@ -578,6 +578,7 @@
<FIELD NAME="template" TYPE="int" LENGTH="1" DEFAULT="0" NOTNULL="true" />
<FIELD NAME="copynewuser" TYPE="int" LENGTH="1" DEFAULT="0" NOTNULL="true" />
<FIELD NAME="type" TYPE="char" LENGTH="50" NOTNULL="true" />
<FIELD NAME="visits" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" />
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" />
......@@ -727,6 +728,18 @@
<KEY NAME="primary" TYPE="primary" FIELDS="token" />
</KEYS>
</TABLE>
<TABLE NAME="view_visit">
<FIELDS>
<FIELD NAME="view" TYPE="int" LENGTH="10" NOTNULL="true" />
<FIELD NAME="ctime" TYPE="datetime" NOTNULL="true" />
</FIELDS>
<KEYS>
<KEY NAME="viewfk" TYPE="foreign" FIELDS="view" REFTABLE="view" REFFIELDS="id" />
</KEYS>
<INDEXES>
<INDEX NAME="ctimeix" UNIQUE="false" FIELDS="ctime"/>
</INDEXES>
</TABLE>
<!-- site content stuff -->
<!--
<TABLE NAME="site_file">
......
......@@ -1320,10 +1320,6 @@ function xmldb_core_upgrade($oldversion=0) {
add_field($table, $field);
}
// @TODO: Stats upgrade:
// Daily data: count of logged-in users
// Add visits column to view table, updated daily from log on filesystem
if ($oldversion < 2009122200) {
// Table for collection of historical stats
$table = new XMLDBTable('site_data');
......@@ -1360,6 +1356,20 @@ function xmldb_core_upgrade($oldversion=0) {
$field = new XMLDBField('ctime');
$field->setAttributes(XMLDB_TYPE_DATETIME, null, null);
add_field($table, $field);
// Add visits column to view table
$table = new XMLDBTable('view');
$field = new XMLDBField('visits');
$field->setAttributes(XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, 0);
add_field($table, $field);
// Add table to store daily view visits
$table = new XMLDBTable('view_visit');
$table->addFieldInfo('ctime', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL);
$table->addFieldInfo('view', XMLDB_TYPE_INTEGER, 10, false, XMLDB_NOTNULL);
$table->addKeyInfo('viewfk', XMLDB_KEY_FOREIGN, array('view'), 'view', array('id'));
$table->addIndexInfo('ctimeix', XMLDB_INDEX_NOTUNIQUE, array('ctime'));
create_table($table);
}
return $status;
......
......@@ -2262,6 +2262,48 @@ function cron_site_data_daily() {
'type' => 'loggedin-users-daily',
'value' => count_records_select('usr', $where),
));
// Process log file containing view visits
if ($fh = @fopen(get_config('dataroot') . 'views.log', 'r')) {
// Read the new stuff out of the file
$latest = get_field_sql("SELECT MAX(ctime) FROM {view_visit}");
$visits = array();
while (!feof($fh)) {
$line = fgets($fh, 1024);
if (preg_match('/^\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\] (\d+)$/', $line, $m) && $m[1] > $latest) {
$visits[] = (object) array('ctime' => $m[1], 'view' => $m[2]);
}
}
fclose($fh);
// Get per-view counts for the view table.
$visitcounts = array();
foreach ($visits as &$v) {
if (!isset($visitcounts[$v->view])) {
$visitcounts[$v->view] = 0;
}
$visitcounts[$v->view]++;
}
// Add visit records to view_visit
foreach ($visits as &$v) {
if (record_exists('view', 'id', $v->view)) {
insert_record('view_visit', $v);
}
}
// Delete view_visit records > 1 week old
delete_records_select(
'view_visit',
'ctime < CURRENT_DATE - INTERVAL ' . (is_postgres() ? "'1 week'" : '1 WEEK')
);
// Update view counts
foreach ($visitcounts as $viewid => $newvisits) {
execute_sql("UPDATE {view} SET visits = visits + ? WHERE id = ?", array($newvisits, $viewid));
}
}
}
function random_string($length=15) {
......
......@@ -61,8 +61,9 @@ $is_friend = is_friend($userid, $loggedinid);
$userobj = new User();
$userobj->find_by_id($userid);
$view = $userobj->get_profile_view();
$viewid = $view->get('id');
# access will either be logged in (always) or public as well
if (!$view || !can_view_view($view->get('id'))) {
if (!$view || !can_view_view($viewid)) {
throw new AccessDeniedException(get_string('youcannotviewthisusersprofile'));
}
......@@ -307,7 +308,7 @@ $smarty->assign('canmessage', can_send_message($loggedinid, $userid));
$smarty->assign('NAME',$name);
$smarty->assign('USERID', $userid);
$smarty->assign('userdisplayname', display_name($USER, null, true));
$smarty->assign('viewid', $view->get('id'));
$smarty->assign('viewid', $viewid);
$smarty->assign('viewtitle', get_string('usersprofile', 'mahara', display_name($user, null, true)));
$smarty->assign('viewtype', 'profile');
if ($loggedinid && $loggedinid == $userid) {
......@@ -332,6 +333,9 @@ $smarty->assign('viewcontent', $view->build_columns());
$smarty->assign('PAGEHEADING', hsc(TITLE));
$smarty->display('user/view.tpl');
// Log view visits
error_log('[' . date("Y-m-d h:i:s") . "] $viewid\n", 3, get_config('dataroot') . 'views.log');
// Send an invitation to the user to join a group
function invite_submit(Pieform $form, $values) {
global $userid;
......
......@@ -271,4 +271,6 @@ $smarty->assign('viewbeingwatched', $viewbeingwatched);
$smarty->display('view/view.tpl');
// Log view visits
error_log('[' . date("Y-m-d h:i:s") . "] $viewid\n", 3, get_config('dataroot') . 'views.log');
?>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment