From 654e77e74d8130b0b78e0a65f179ca843620640f Mon Sep 17 00:00:00 2001 From: Dan Poltawski Date: Tue, 7 Jun 2016 10:44:27 +0100 Subject: [PATCH] MDL-54729 mod_forum: clarify static variable usage After some time discussing with integrators, we decided that this slightly yukky solution probably fits safely, so just making its usage better explained and speciifc. --- mod/forum/lib.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/mod/forum/lib.php b/mod/forum/lib.php index ed16ec9aa14..4816808aa21 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -3078,9 +3078,12 @@ function forum_print_post($post, $discussion, $forum, &$cm, $course, $ownpost=fa // String cache static $str; - // As we should only have one element with the id of unread we keep track of whether this post is the first - // unread post. - static $firstunread = true; + // This is an extremely hacky way to ensure we only print the 'unread' anchor + // the first time we encounter an unread post on a page. Ideally this would + // be moved into the caller somehow, and be better testable. But at the time + // of dealing with this bug, this static workaround was the most surgical and + // it fits together with only printing th unread anchor id once on a given page. + static $firstunreadanchorprinted = false; $modcontext = context_module::instance($cm->id); @@ -3296,10 +3299,10 @@ function forum_print_post($post, $discussion, $forum, &$cm, $course, $ownpost=fa $forumpostclass = ' read'; } else { $forumpostclass = ' unread'; - // If this is the first unread post then give it an anchor and id of unread. - if ($firstunread) { + // If this is the first unread post printed then give it an anchor and id of unread. + if (!$firstunreadanchorprinted) { $output .= html_writer::tag('a', '', array('id' => 'unread')); - $firstunread = false; + $firstunreadanchorprinted = true; } } } else { -- 2.43.0