Check if a user has ever read a topic?

Started by samborabora, May 27, 2015, 08:17:35 AM

Previous topic - Next topic

margarett

* margarett apologizes for a much needed cleanup

@samborabora, what you want to do is now perfectly clear. I am just sorry that I don't have, right now, the required time to implement that (because I can't code it out of my head, I need to try, fail and debug).
The principle that I outlined should be more or less correct, but it still needs to be coded. And it is a bit heavy for my skills so it requires time :(

QuoteLet's try again. I'd like a variable, say $context[user][seenpost] that
You don't need that, the way I see it. After you have fetched the "seenpost" information, you just need to loop the $context['topics'] variable and clean the "new" item of any item which as not "seen".
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

samborabora

Quote from: margarett on May 29, 2015, 11:27:16 AM
* margarett apologizes for a much needed cleanup

@samborabora, what you want to do is now perfectly clear. I am just sorry that I don't have, right now, the required time to implement that (because I can't code it out of my head, I need to try, fail and debug).
The principle that I outlined should be more or less correct, but it still needs to be coded. And it is a bit heavy for my skills so it requires time :(

QuoteLet's try again. I'd like a variable, say $context[user][seenpost] that
You don't need that, the way I see it. After you have fetched the "seenpost" information, you just need to loop the $context['topics'] variable and clean the "new" item of any item which as not "seen".

Thank you, margarett, I appreciate you taking the time to consider this, and hope I have described it accurately enough! Okay, back to the original plan, any ideas about Topic View Mod being useful for this?

samborabora

Still looks like that mod would require a lot of extra stuff I don't need, which is really just a variable to check if the user has ever seen the topic before, and it's a bit weird trying to figure out how to download that mod. If anyone can come up with any suggestions on what code to use to do this, I'd be most grateful!

samborabora

Okay, I installed Topic View Mod, here's how the mod is building the topic views list:


function tvl_actions(&$actionArray) //Action!
{
$actionArray['topicviewlog'] = array('TopicViewLog.php', 'TopicViewLog');
}

function tvl_permissions(&$permissionGroups, &$permissionList)
{
global $context;

loadLanguage('TopicViewLog');
$context['non_guest_permissions'][] = 'tvl_view';
$permissionList['board']['tvl_view'] = array(true, 'topic', 'moderate');
}

function tvl_display_buttons(&$normal_buttons)
{
global $scripturl, $context;

loadLanguage('TopicViewLog');
$context['can_view_topic_log'] = allowedTo('tvl_view_any') || (allowedTo('tvl_view_own') && $context['user']['started']);

if (!empty($context['can_view_topic_log']))
{
$normal_buttons[] = array(
'text' => 'tvl_title',
'image' => 'topiclog.gif',
'lang' => true,
'url' => $scripturl . '?action=topicviewlog;topic=' . $context['current_topic'] . '.0'
);
}
}

function tvl_load_theme()
{
global $context;

if (!empty($_REQUEST['topic']) && empty($context['current_action']))
tvl_log();
}

function TopicViewLog()
{
global $smcFunc, $context, $user_info, $scripturl, $sourcedir, $txt, $topic;

loadLanguage('TopicViewLog');

if (empty($topic))
fatal_lang_error('no_board', false);

$request = $smcFunc['db_query']('', '
SELECT id_member_started
FROM {db_prefix}topics
WHERE id_topic = {int:topic} LIMIT 1',
array(
'topic' => $topic,
)
);
list ($starter) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);

if (!allowedTo('tvl_view_any') && $user_info['id'] == $starter)
isAllowedTo('tvl_view_own');
else
isAllowedTo('tvl_view_any');

$context['page_title'] = $txt['tvl_title'];

require_once($sourcedir . '/Subs-List.php');

$listOptions = array(
'id' => 'tvl_list',
'items_per_page' => 30,
'base_href' => $scripturl . '?action=topicviewlog;topic=' . $topic,
'default_sort_col' => 'time',
'get_items' => array(
'function' => 'list_get_tvl_members',
),
'get_count' => array(
'function' => 'list_get_tvl_num_members',
),
'columns' => array(
'name' => array(
'header' => array(
'value' => $txt['name'],
),
'data' => array(
'sprintf' => array(
'format' => '<a href="' . strtr($scripturl, array('%' => '%%')) . '?action=profile;u=%1$d">%2$s</a>',
'params' => array(
'id_member' => false,
'real_name' => false,
),
),
'style' => 'width: 25%;',
),
'sort' => array(
'default' => 'real_name',
'reverse' => 'real_name DESC',
),
),
'group' => array(
'header' => array(
'value' => $txt['position'],
),
'data' => array(
'db' => 'group_name',
'style' => 'width: 25%;',
),
'sort' =>  array(
'default' => 'group_name',
'reverse' => 'group_name DESC',
),
),
'posts' => array(
'header' => array(
'value' => $txt['posts'],
),
'data' => array(
'db' => 'topic_posts',
'style' => 'width: 10%; text-align: center;',
),
'sort' =>  array(
'default' => 'topic_posts',
'reverse' => 'topic_posts DESC',
),
),
'views' => array(
'header' => array(
'value' => $txt['views'],
),
'data' => array(
'db' => 'views',
'style' => 'width: 10%; text-align: center;',
),
'sort' =>  array(
'default' => 'views',
'reverse' => 'views DESC',
),
),
'time' => array(
'header' => array(
'value' => $txt['tvl_times'],
),
'data' => array(
'function' => create_function('$rows', '
return timeformat($rows[\'time\']);
'),
'style' => 'width: 30%;',
),
'sort' =>  array(
'default' => 'time',
'reverse' => 'time DESC',
),
),
),
);

createList($listOptions);

$context['sub_template'] = 'show_list';
$context['default_list'] = 'tvl_list';
}

function list_get_tvl_members($start, $items_per_page, $sort)
{
global $smcFunc, $topic;

$request = $smcFunc['db_query']('', '
SELECT
mem.id_member, mem.member_name, mem.real_name, mg.group_name,
tvl.views, tvl.time, COUNT(m.id_msg) AS topic_posts
FROM {db_prefix}log_topic_view AS tvl
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = tvl.id_member)
LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = CASE WHEN mem.id_group = {int:regular_id_group} THEN mem.id_post_group ELSE mem.id_group END)
LEFT JOIN {db_prefix}messages AS m ON (m.id_member = mem.id_member AND m.id_topic = tvl.id_topic)
WHERE tvl.id_topic = {int:topic}
GROUP BY mem.id_member
ORDER BY {raw:sort}
LIMIT {int:start}, {int:per_page}',
array(
'sort' => $sort,
'start' => $start,
'per_page' => $items_per_page,
'topic' => $topic,
'regular_id_group' => 0,
)
);

$rows = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
$rows[] = $row;
$smcFunc['db_free_result']($request);

return $rows;
}

function list_get_tvl_num_members()
{
global $smcFunc, $topic;

$request = $smcFunc['db_query']('', '
SELECT COUNT(*)
FROM {db_prefix}log_topic_view
WHERE id_topic = {int:topic}',
array(
'topic' => $topic,
)
);
list ($num_rows) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);

return $num_rows;
}

function tvl_log()
{
global $smcFunc, $user_info, $topic;

if (empty($topic) || $user_info['is_guest'])
return false;

$smcFunc['db_query']('', '
UPDATE {db_prefix}log_topic_view
SET views = views + {int:views}, time = {int:time}
WHERE id_member = {int:member}
AND id_topic = {int:topic}
LIMIT 1',
array(
'member' => $user_info['id'],
'topic' => $topic,
'views' => !empty($_SESSION['last_read_topic']) && $_SESSION['last_read_topic'] == $topic ? 0 : 1,
'time' => time(),
)
);

if ($smcFunc['db_affected_rows']() == 0)
{
$smcFunc['db_insert']('ignore',
'{db_prefix}log_topic_view',
array('id_member' => 'int', 'id_topic' => 'int', 'views' => 'int', 'time' => 'int'),
array($user_info['id'], $topic, 1, time()),
array('id_member', 'id_topic')
);
}
}

function tvl_Buffer($buffer)
{
global $forum_copyright, $context, $sourcedir;

require_once($sourcedir . '/QueryString.php');
ob_sessrewrite($buffer);

if(empty($context['deletforum']))
{
$context['deletforum'] = base64_decode('IHwgPGEgc3R5bGU9ImZvbnQtc2l6ZToxMHB4OyIgaHJlZj0iaHR0cDovL3d3dy5zbWZzaW1wbGUuY29tIiB0aXRsZT0iVG9kbyBwYXJhIHR1IGZvcm8gU01GIj5Nb2RzIGJ5IFNNRlNpbXBsZS5jb208L2E+');
$buffer = str_replace($forum_copyright, $forum_copyright.$context['deletforum'],$buffer);
}
return $buffer;
}


So, can this be used to grab a view count from the db for each topic on MessageIndex, then just have a variable that if the views count is not empty, then display "new"?

samborabora

Quote from: margarett on May 29, 2015, 11:27:16 AM
@samborabora, what you want to do is now perfectly clear. I am just sorry that I don't have, right now, the required time to implement that (because I can't code it out of my head, I need to try, fail and debug).
The principle that I outlined should be more or less correct, but it still needs to be coded. And it is a bit heavy for my skills so it requires time :(

Is the Topic View source I posted of any use? With that, I'm hoping to just do a per topic on MessageIndex check against the table of views, if there is 0 views, then the variable isn't satisfied, if there is more than 0, then the variable is. I'm just terrible with direct database call coding!

samborabora

Quote from: samborabora on May 31, 2015, 04:34:09 PM


function tvl_actions(&$actionArray) //Action!
{
$actionArray['topicviewlog'] = array('TopicViewLog.php', 'TopicViewLog');
}

function tvl_permissions(&$permissionGroups, &$permissionList)
{
global $context;

loadLanguage('TopicViewLog');
$context['non_guest_permissions'][] = 'tvl_view';
$permissionList['board']['tvl_view'] = array(true, 'topic', 'moderate');
}

function tvl_display_buttons(&$normal_buttons)
{
global $scripturl, $context;

loadLanguage('TopicViewLog');
$context['can_view_topic_log'] = allowedTo('tvl_view_any') || (allowedTo('tvl_view_own') && $context['user']['started']);

if (!empty($context['can_view_topic_log']))
{
$normal_buttons[] = array(
'text' => 'tvl_title',
'image' => 'topiclog.gif',
'lang' => true,
'url' => $scripturl . '?action=topicviewlog;topic=' . $context['current_topic'] . '.0'
);
}
}

function tvl_load_theme()
{
global $context;

if (!empty($_REQUEST['topic']) && empty($context['current_action']))
tvl_log();
}

function TopicViewLog()
{
global $smcFunc, $context, $user_info, $scripturl, $sourcedir, $txt, $topic;

loadLanguage('TopicViewLog');

if (empty($topic))
fatal_lang_error('no_board', false);

$request = $smcFunc['db_query']('', '
SELECT id_member_started
FROM {db_prefix}topics
WHERE id_topic = {int:topic} LIMIT 1',
array(
'topic' => $topic,
)
);
list ($starter) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);

if (!allowedTo('tvl_view_any') && $user_info['id'] == $starter)
isAllowedTo('tvl_view_own');
else
isAllowedTo('tvl_view_any');

$context['page_title'] = $txt['tvl_title'];

require_once($sourcedir . '/Subs-List.php');

$listOptions = array(
'id' => 'tvl_list',
'items_per_page' => 30,
'base_href' => $scripturl . '?action=topicviewlog;topic=' . $topic,
'default_sort_col' => 'time',
'get_items' => array(
'function' => 'list_get_tvl_members',
),
'get_count' => array(
'function' => 'list_get_tvl_num_members',
),
'columns' => array(
'name' => array(
'header' => array(
'value' => $txt['name'],
),
'data' => array(
'sprintf' => array(
'format' => '<a href="' . strtr($scripturl, array('%' => '%%')) . '?action=profile;u=%1$d">%2$s</a>',
'params' => array(
'id_member' => false,
'real_name' => false,
),
),
'style' => 'width: 25%;',
),
'sort' => array(
'default' => 'real_name',
'reverse' => 'real_name DESC',
),
),
'group' => array(
'header' => array(
'value' => $txt['position'],
),
'data' => array(
'db' => 'group_name',
'style' => 'width: 25%;',
),
'sort' =>  array(
'default' => 'group_name',
'reverse' => 'group_name DESC',
),
),
'posts' => array(
'header' => array(
'value' => $txt['posts'],
),
'data' => array(
'db' => 'topic_posts',
'style' => 'width: 10%; text-align: center;',
),
'sort' =>  array(
'default' => 'topic_posts',
'reverse' => 'topic_posts DESC',
),
),
'views' => array(
'header' => array(
'value' => $txt['views'],
),
'data' => array(
'db' => 'views',
'style' => 'width: 10%; text-align: center;',
),
'sort' =>  array(
'default' => 'views',
'reverse' => 'views DESC',
),
),
'time' => array(
'header' => array(
'value' => $txt['tvl_times'],
),
'data' => array(
'function' => create_function('$rows', '
return timeformat($rows[\'time\']);
'),
'style' => 'width: 30%;',
),
'sort' =>  array(
'default' => 'time',
'reverse' => 'time DESC',
),
),
),
);

createList($listOptions);

$context['sub_template'] = 'show_list';
$context['default_list'] = 'tvl_list';
}

function list_get_tvl_members($start, $items_per_page, $sort)
{
global $smcFunc, $topic;

$request = $smcFunc['db_query']('', '
SELECT
mem.id_member, mem.member_name, mem.real_name, mg.group_name,
tvl.views, tvl.time, COUNT(m.id_msg) AS topic_posts
FROM {db_prefix}log_topic_view AS tvl
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = tvl.id_member)
LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = CASE WHEN mem.id_group = {int:regular_id_group} THEN mem.id_post_group ELSE mem.id_group END)
LEFT JOIN {db_prefix}messages AS m ON (m.id_member = mem.id_member AND m.id_topic = tvl.id_topic)
WHERE tvl.id_topic = {int:topic}
GROUP BY mem.id_member
ORDER BY {raw:sort}
LIMIT {int:start}, {int:per_page}',
array(
'sort' => $sort,
'start' => $start,
'per_page' => $items_per_page,
'topic' => $topic,
'regular_id_group' => 0,
)
);

$rows = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
$rows[] = $row;
$smcFunc['db_free_result']($request);

return $rows;
}

function list_get_tvl_num_members()
{
global $smcFunc, $topic;

$request = $smcFunc['db_query']('', '
SELECT COUNT(*)
FROM {db_prefix}log_topic_view
WHERE id_topic = {int:topic}',
array(
'topic' => $topic,
)
);
list ($num_rows) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);

return $num_rows;
}

function tvl_log()
{
global $smcFunc, $user_info, $topic;

if (empty($topic) || $user_info['is_guest'])
return false;

$smcFunc['db_query']('', '
UPDATE {db_prefix}log_topic_view
SET views = views + {int:views}, time = {int:time}
WHERE id_member = {int:member}
AND id_topic = {int:topic}
LIMIT 1',
array(
'member' => $user_info['id'],
'topic' => $topic,
'views' => !empty($_SESSION['last_read_topic']) && $_SESSION['last_read_topic'] == $topic ? 0 : 1,
'time' => time(),
)
);

if ($smcFunc['db_affected_rows']() == 0)
{
$smcFunc['db_insert']('ignore',
'{db_prefix}log_topic_view',
array('id_member' => 'int', 'id_topic' => 'int', 'views' => 'int', 'time' => 'int'),
array($user_info['id'], $topic, 1, time()),
array('id_member', 'id_topic')
);
}
}

function tvl_Buffer($buffer)
{
global $forum_copyright, $context, $sourcedir;

require_once($sourcedir . '/QueryString.php');
ob_sessrewrite($buffer);

if(empty($context['deletforum']))
{
$context['deletforum'] = base64_decode('IHwgPGEgc3R5bGU9ImZvbnQtc2l6ZToxMHB4OyIgaHJlZj0iaHR0cDovL3d3dy5zbWZzaW1wbGUuY29tIiB0aXRsZT0iVG9kbyBwYXJhIHR1IGZvcm8gU01GIj5Nb2RzIGJ5IFNNRlNpbXBsZS5jb208L2E+');
$buffer = str_replace($forum_copyright, $forum_copyright.$context['deletforum'],$buffer);
}
return $buffer;
}



Is there enough detail in this code snippet to be able to work out what sql calls need to be made?


frakster

I know this is an old'ish topic but you could show 2 images. One for New  then a different one for read.

if ($topic['new'] && $context['user']['is_logged'])
echo '
<img src="/new.png" alt="New" />'; 
else echo '<img src="/old.png" alt="old" />';


Warning this is a completely untested example. Just tossing the idea out there.

Advertisement: