diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 4940d41672c..cb0bbbbcef9 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -1602,27 +1602,34 @@ function forum_print_recent_activity($course, $viewfullnames, $timestart) { } echo $OUTPUT->heading(get_string('newforumposts', 'forum').':', 3); - echo "\n\n"; + $list .= html_writer::end_tag('ul'); + echo $list; return true; } @@ -3319,53 +3326,69 @@ function forum_print_post($post, $discussion, $forum, &$cm, $course, $ownpost=fa $forumpostclass .= ' lastpost'; } + // Flag to indicate whether we should hide the author or not. + $authorhidden = forum_is_author_hidden($post, $forum); $postbyuser = new stdClass; $postbyuser->post = $post->subject; $postbyuser->user = $postuser->fullname; $discussionbyuser = get_string('postbyuser', 'forum', $postbyuser); $output .= html_writer::tag('a', '', array('id'=>'p'.$post->id)); - $output .= html_writer::start_tag('div', array('class'=>'forumpost clearfix'.$forumpostclass.$topicclass, - 'role' => 'region', - 'aria-label' => $discussionbyuser)); - $output .= html_writer::start_tag('div', array('class'=>'row header clearfix')); - $output .= html_writer::start_tag('div', array('class'=>'left picture')); - $output .= $OUTPUT->user_picture($postuser, array('courseid'=>$course->id)); - $output .= html_writer::end_tag('div'); + // Begin forum post. + $output .= html_writer::start_div('forumpost clearfix' . $forumpostclass . $topicclass, + ['role' => 'region', 'aria-label' => $discussionbyuser]); + // Begin header row. + $output .= html_writer::start_div('row header clearfix'); + // User picture. + if (!$authorhidden) { + $picture = $OUTPUT->user_picture($postuser, ['courseid' => $course->id]); + $output .= html_writer::div($picture, 'left picture'); + $topicclass = 'topic' . $topicclass; + } - $output .= html_writer::start_tag('div', array('class'=>'topic'.$topicclass)); - + // Begin topic column. + $output .= html_writer::start_div($topicclass); $postsubject = $post->subject; if (empty($post->subjectnoformat)) { $postsubject = format_string($postsubject); } - $output .= html_writer::tag('div', $postsubject, array('class'=>'subject', - 'role' => 'heading', - 'aria-level' => '2')); + $output .= html_writer::div($postsubject, 'subject', ['role' => 'heading', 'aria-level' => '2']); - $by = new stdClass(); - $by->name = html_writer::link($postuser->profilelink, $postuser->fullname); - $by->date = userdate($post->modified); - $output .= html_writer::tag('div', get_string('bynameondate', 'forum', $by), array('class'=>'author', - 'role' => 'heading', - 'aria-level' => '2')); - - $output .= html_writer::end_tag('div'); //topic - $output .= html_writer::end_tag('div'); //row - - $output .= html_writer::start_tag('div', array('class'=>'row maincontent clearfix')); - $output .= html_writer::start_tag('div', array('class'=>'left')); - - $groupoutput = ''; - if ($groups) { - $groupoutput = print_group_picture($groups, $course->id, false, true, true); + if ($authorhidden) { + $bytext = userdate($post->modified); + } else { + $by = new stdClass(); + $by->date = userdate($post->modified); + $by->name = html_writer::link($postuser->profilelink, $postuser->fullname); + $bytext = get_string('bynameondate', 'forum', $by); + } + $bytextoptions = [ + 'role' => 'heading', + 'aria-level' => '2', + ]; + $output .= html_writer::div($bytext, 'author', $bytextoptions); + // End topic column. + $output .= html_writer::end_div(); + + // End header row. + $output .= html_writer::end_div(); + + // Row with the forum post content. + $output .= html_writer::start_div('row maincontent clearfix'); + // Show if author is not hidden or we have groups. + if (!$authorhidden || $groups) { + $output .= html_writer::start_div('left'); + $groupoutput = ''; + if ($groups) { + $groupoutput = print_group_picture($groups, $course->id, false, true, true); + } + if (empty($groupoutput)) { + $groupoutput = ' '; + } + $output .= html_writer::div($groupoutput, 'grouppictures'); + $output .= html_writer::end_div(); // Left side. } - if (empty($groupoutput)) { - $groupoutput = ' '; - } - $output .= html_writer::tag('div', $groupoutput, array('class'=>'grouppictures')); - $output .= html_writer::end_tag('div'); //left side $output .= html_writer::start_tag('div', array('class'=>'no-overflow')); $output .= html_writer::start_tag('div', array('class'=>'content')); @@ -5899,6 +5922,7 @@ function forum_get_recent_mod_activity(&$activities, &$index, $timestart, $cours $tmpactivity->content->discussion = $post->discussion; $tmpactivity->content->subject = format_string($post->subject); $tmpactivity->content->parent = $post->parent; + $tmpactivity->content->forumtype = $post->forumtype; $tmpactivity->user = new stdClass(); $additionalfields = array('id' => 'userid', 'picture', 'imagealt', 'email'); @@ -5913,48 +5937,85 @@ function forum_get_recent_mod_activity(&$activities, &$index, $timestart, $cours } /** - * @todo Document this function - * @global object + * Outputs the forum post indicated by $activity. + * + * @param object $activity the activity object the forum resides in + * @param int $courseid the id of the course the forum resides in + * @param bool $detail not used, but required for compatibilty with other modules + * @param int $modnames not used, but required for compatibilty with other modules + * @param bool $viewfullnames not used, but required for compatibilty with other modules */ function forum_print_recent_mod_activity($activity, $courseid, $detail, $modnames, $viewfullnames) { - global $CFG, $OUTPUT; + global $OUTPUT; - if ($activity->content->parent) { + $content = $activity->content; + if ($content->parent) { $class = 'reply'; } else { $class = 'discussion'; } - echo ''; + $tableoptions = [ + 'border' => '0', + 'cellpadding' => '3', + 'cellspacing' => '0', + 'class' => 'forum-recent' + ]; + $output = html_writer::start_tag('table', $tableoptions); + $output .= html_writer::start_tag('tr'); - echo "
"; - echo $OUTPUT->user_picture($activity->user, array('courseid'=>$courseid)); - echo ""; + $post = (object) ['parent' => $content->parent]; + $forum = (object) ['type' => $content->forumtype]; + $authorhidden = forum_is_author_hidden($post, $forum); - if ($activity->content->parent) { + // Show user picture if author should not be hidden. + if (!$authorhidden) { + $pictureoptions = [ + 'courseid' => $courseid, + 'link' => $authorhidden, + 'alttext' => $authorhidden, + ]; + $picture = $OUTPUT->user_picture($activity->user, $pictureoptions); + $output .= html_writer::tag('td', $picture, ['class' => 'userpicture', 'valign' => 'top']); + } + + // Discussion title and author. + $output .= html_writer::start_tag('td', ['class' => $class]); + if ($content->parent) { $class = 'title'; } else { // Bold the title of new discussions so they stand out. $class = 'title bold'; } - echo "
"; + + $output .= html_writer::start_div($class); if ($detail) { $aname = s($activity->name); - echo "pix_url('icon', $activity->type) . "\" ". - "class=\"icon\" alt=\"{$aname}\" />"; + $output .= html_writer::img($OUTPUT->pix_url('icon', $activity->type), $aname, ['class' => 'icon']); } - echo "wwwroot/mod/forum/discuss.php?d={$activity->content->discussion}" - ."#p{$activity->content->id}\">{$activity->content->subject}"; - echo '
'; + $discussionurl = new moodle_url('/mod/forum/discuss.php', ['d' => $content->discussion]); + $discussionurl->set_anchor('p' . $activity->content->id); + $output .= html_writer::link($discussionurl, $content->subject); + $output .= html_writer::end_div(); - echo '
'; - $fullname = fullname($activity->user, $viewfullnames); - echo "wwwroot/user/view.php?id={$activity->user->id}&course=$courseid\">" - ."{$fullname} - ".userdate($activity->timestamp); - echo '
'; - echo "
"; + $timestamp = userdate($activity->timestamp); + if ($authorhidden) { + $authornamedate = $timestamp; + } else { + $fullname = fullname($activity->user, $viewfullnames); + $userurl = new moodle_url('/user/view.php'); + $userurl->params(['id' => $activity->user->id, 'course' => $courseid]); + $by = new stdClass(); + $by->name = html_writer::link($userurl, $fullname); + $by->date = $timestamp; + $authornamedate = get_string('bynameondate', 'forum', $by); + } + $output .= html_writer::div($authornamedate, 'user'); + $output .= html_writer::end_tag('td'); + $output .= html_writer::end_tag('tr'); + $output .= html_writer::end_tag('table'); - return; + echo $output; } /** @@ -7910,3 +7971,24 @@ function mod_forum_myprofile_navigation(core_user\output\myprofile\tree $tree, $ return true; } + +/** + * Checks whether the author's name and picture for a given post should be hidden or not. + * + * @param object $post The forum post. + * @param object $forum The forum object. + * @return bool + * @throws coding_exception + */ +function forum_is_author_hidden($post, $forum) { + if (!isset($post->parent)) { + throw new coding_exception('$post->parent must be set.'); + } + if (!isset($forum->type)) { + throw new coding_exception('$forum->type must be set.'); + } + if ($forum->type === 'single' && empty($post->parent)) { + return true; + } + return false; +} diff --git a/mod/forum/tests/lib_test.php b/mod/forum/tests/lib_test.php index cf29d4f5c8b..8e551a584d3 100644 --- a/mod/forum/tests/lib_test.php +++ b/mod/forum/tests/lib_test.php @@ -3012,6 +3012,39 @@ class mod_forum_lib_testcase extends advanced_testcase { $this->assertCount($expectedreplycount, $unmailed); } + /** + * Test for forum_is_author_hidden. + */ + public function test_forum_is_author_hidden() { + // First post, different forum type. + $post = (object) ['parent' => 0]; + $forum = (object) ['type' => 'standard']; + $this->assertFalse(forum_is_author_hidden($post, $forum)); + + // Child post, different forum type. + $post->parent = 1; + $this->assertFalse(forum_is_author_hidden($post, $forum)); + + // First post, single simple discussion forum type. + $post->parent = 0; + $forum->type = 'single'; + $this->assertTrue(forum_is_author_hidden($post, $forum)); + + // Child post, single simple discussion forum type. + $post->parent = 1; + $this->assertFalse(forum_is_author_hidden($post, $forum)); + + // Incorrect parameters: $post. + $this->setExpectedException('coding_exception', '$post->parent must be set.'); + unset($post->parent); + forum_is_author_hidden($post, $forum); + + // Incorrect parameters: $forum. + $this->setExpectedException('coding_exception', '$forum->type must be set.'); + unset($forum->type); + forum_is_author_hidden($post, $forum); + } + public function forum_get_unmailed_posts_provider() { return [ 'Untimed discussion; Single post; maxeditingtime not expired' => [