Mark Posts Read Ajax problem

Extensions, styles and support for everything else about phpBB
EVO_VV
Posts: 40
Joined: 23 Jun 2017, 16:03

Re: Mark Posts Read Ajax problem

#11

Post by EVO_VV » 10 Mar 2018, 13:25

Ajax error is no more :+1: :beer:
Many thanks for your assistance.

However it still does not do what I want yet, insists on marking every Topic Read in that particular Forum.
Going through the code now to see if I can find out exactly what is happening where.
So far my normal tests of slipping in an echo "Got to Here"; or a print_r ($somevariable); do not show anything at all on the page as I am used to them doing, making it difficult to ascertain if the code is doing what I intended.

EDIT
var_dump($somevariable); Does not display anything either.

Another EDIT
Discovered why I was not getting the feedback from the echo etc.
Looks like once the data-ajax="SomeFunction" is set then any output from that function is routed to the Ajax Function and thereby either ignored or causes an Ajax error.

Removed that bit temporarily.
Now I can see what is actually going on to fix the errors in the code. :D
Wish I would have found that earlier. :(

EVO_VV
Posts: 40
Joined: 23 Jun 2017, 16:03

Re: Mark Posts Read Ajax problem

#12

Post by EVO_VV » 11 Mar 2018, 15:20

Now have it working as far as I can tell.
Still need to do more testing though but at least now it does just mark the selected topics read.
Was surprised how little actual Topic marking was in the Mark Topics part of functions.php seemed mostly mark Forums instead.
Had to copy a bunch of stuff from mark topic part and amend it to allow for topics.

EVO_VV
Posts: 40
Joined: 23 Jun 2017, 16:03

Re: Mark Posts Read Ajax problem

#13

Post by EVO_VV » 12 Mar 2018, 15:39

After copying what was in the Topic part into the Topics part and adjusting it to handle multiple topic_ids it worked as intended but I was not sure that I got all the notifications bits correct & also unsure about the Cookie part as we don't use that.
Then it struck me that if I had to use the topic bit instead of the topics bit I could simplify the whole issue and just edit the code in viewforum.php and not touch functions.php at all.
Might not be the quickest solution and not particularly elegant but it works and there is less core code changed.
Here is what I now have :-

Code: Select all

if ($mark_read == 'topics')
{
		$token = $request->variable('hash', '');
		if (check_link_hash($token, 'global'))

		if ($az != false) {
			switch ((string)$az){
				case "Other":
					$sql = "SELECT topic_id FROM " . TOPICS_TABLE . " WHERE " . $db->sql_in_set('forum_id', $forum_id) . " AND topic_title NOT REGEXP '^[a-z]'";
					break;
				case "All":
					$sql = "SELECT topic_id FROM " . TOPICS_TABLE . " WHERE " . $db->sql_in_set('forum_id', $forum_id);
					break;
				default:
					$sql = "SELECT topic_id FROM " . TOPICS_TABLE . " WHERE " . $db->sql_in_set('forum_id', $forum_id) . " AND topic_title " . $db->sql_like_expression(substr($az, 0, 1) . $db->get_any_char());
			}
			$result = $db->sql_query($sql);
			
			while ($row = $db->sql_fetchrow($result))
			{
				$topic_ids[] = $row['topic_id'];
			}
			$db->sql_freeresult($result);
			
			foreach ($topic_ids as $t_id)
			{
				markread('topic', array($forum_id), $t_id, $request->variable('mark_time', 0),0);
			}
		}else{
			{
				markread('topics', array($forum_id), false, $request->variable('mark_time', 0),0);
			}
		}
		$redirect_url = append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id);
		meta_refresh(3, $redirect_url);
		
		if ($request->is_ajax())
		{
			// Tell the ajax script what language vars and URL need to be replaced
			$data = array(
				'NO_UNREAD_POSTS'	=> $user->lang['NO_UNREAD_POSTS'],
				'UNREAD_POSTS'		=> $user->lang['UNREAD_POSTS'],
				'U_MARK_TOPICS'		=> ($user->data['is_registered'] || $config['load_anon_lastread']) ? append_sid("{$phpbb_root_path}viewforum.$phpEx", 'hash=' . generate_link_hash('global') . "&f=$forum_id&mark=topics&mark_time=" . time()) : '',
				'MESSAGE_TITLE'		=> $user->lang['INFORMATION'],
				'MESSAGE_TEXT'		=> $user->lang['TOPICS_MARKED']
			);
		}
		$json_response = new \phpbb\json_response();
		$json_response->send($data);
	
	trigger_error($user->lang['TOPICS_MARKED'] . '<br /><br />' . sprintf($user->lang['RETURN_FORUM'], '<a href="' . $redirect_url . '">', '</a>'));
}

User avatar
kasimi
mChat developer
Posts: 738
Joined: 06 Oct 2016, 09:56
Location: Germany
Contact:

Re: Mark Posts Read Ajax problem

#14

Post by kasimi » 15 Mar 2018, 17:15

Looks good!

Depending on how many topics you have in these forums, you might run into performance issues because you're marking the topics read one by one which sends many SQL queries. Maybe there's a way to use the topics mode instead. You could change this query according to your $az value: https://github.com/phpbb/phpbb/blob/rel ... #L689-L691 This will send a constant number of queries regardless of the number of topics, but looking through the code, it may break marking notifications read... 😕

EVO_VV
Posts: 40
Joined: 23 Jun 2017, 16:03

Re: Mark Posts Read Ajax problem

#15

Post by EVO_VV » 16 Mar 2018, 13:49

That was basically what I was attempting first but even when I got that part working correctly it still marked all the Topics read.
Something in the later code :-

Code: Select all

$sql = 'DELETE FROM ' . TOPICS_TRACK_TABLE . "
	WHERE user_id = {$user->data['user_id']}
		AND mark_time < $post_time
		AND " . $db->sql_in_set('forum_id', $forum_id);
$db->sql_query($sql);

$sql = 'SELECT forum_id
	FROM ' . FORUMS_TRACK_TABLE . "
	WHERE user_id = {$user->data['user_id']}
		AND " . $db->sql_in_set('forum_id', $forum_id);
$result = $db->sql_query($sql);
$sql_update = array();
while ($row = $db->sql_fetchrow($result))
{
	$sql_update[] = (int) $row['forum_id'];
}
$db->sql_freeresult($result);

if ($sql_insert = array_diff($forum_id, $sql_update))
{
	$sql_ary = array();
	foreach ($sql_insert as $f_id)
	{
		$sql_ary[] = array(
			'user_id'	=> (int) $user->data['user_id'],
			'forum_id'	=> (int) $f_id,
			'mark_time'	=> $post_time,
		);
	}
	$db->sql_multi_insert(FORUMS_TRACK_TABLE, $sql_ary);
Was causing that so that was when I attempted to try to use the topic code to try and fix it.
I did get it working but had not adjusted the code for Cookies, only Db which did not seem right to me - not complete.

That whole section is a bit of a misnomer it is called 'Topics' but it is really 'Forum(s)'

I think the largest Forum we have has about 800 topics in it but of course that is all of them and for this we are only dealing with a part of that.
So far I have not seen any performance issues as I think it is rarely used.
Server is running normally at about 12% CPU capacity and only on rare occasions does it reach 40-50% when I suspect backups are in operation.
Last edited by kasimi on 16 Mar 2018, 22:13, edited 1 time in total.
Reason: Use [code] block

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest