' . $searchterm . '<\/fgw9sdpq4>/i', $exportedpost->message)
) {
$missingterms .= " $searchterm";
}
}
$exportedpost->message = str_replace('', '', $exportedpost->message);
$exportedpost->message = str_replace('', '', $exportedpost->message);
if ($missingterms) {
$strmissingsearchterms = get_string('missingsearchterms', 'forum');
$exportedpost->message = '' . $strmissingsearchterms . ' '
. $missingterms . '
' . $exportedpost->message;
}
return $exportedpost;
},
$exportedposts
);
}
);
}
/**
* Create a posts renderer to render posts in mod/forum/user.php.
*
* @param bool $addlinkstocontext Should links to the course, forum, and discussion be included?
* @return posts_renderer
*/
public function get_user_forum_posts_report_renderer(bool $addlinkstocontext) : posts_renderer {
$urlfactory = $this->urlfactory;
return new posts_renderer(
$this->rendererbase,
$this->builderfactory->get_exported_posts_builder(),
'mod_forum/forum_posts_with_context_links',
function($exportedposts, $forumsbyid, $discussionsbyid) use ($urlfactory, $addlinkstocontext) {
return array_map(function($exportedpost) use ($forumsbyid, $discussionsbyid, $addlinkstocontext, $urlfactory) {
$discussion = $discussionsbyid[$exportedpost->discussionid];
$forum = $forumsbyid[$discussion->get_forum_id()];
$courserecord = $forum->get_course_record();
if ($addlinkstocontext) {
$viewdiscussionurl = $urlfactory->get_discussion_view_url_from_discussion($discussion);
$exportedpost->urls['viewforum'] = $urlfactory->get_forum_view_url_from_forum($forum)->out(false);
$exportedpost->urls['viewdiscussion'] = $viewdiscussionurl->out(false);
$exportedpost->urls['viewcourse'] = $urlfactory->get_course_url_from_forum($forum)->out(false);
}
$exportedpost->forumname = format_string($forum->get_name(), true);
$exportedpost->discussionname = format_string($discussion->get_name(), true);
$exportedpost->coursename = format_string($courserecord->shortname, true);
$exportedpost->showdiscussionname = $forum->get_type() != 'single';
return $exportedpost;
}, $exportedposts);
}
);
}
/**
* Create a standard type discussion list renderer.
*
* @param forum_entity $forum The forum that the discussions belong to
* @return discussion_list_renderer
*/
public function get_discussion_list_renderer(
forum_entity $forum
) : discussion_list_renderer {
$capabilitymanager = $this->managerfactory->get_capability_manager($forum);
$rendererbase = $this->rendererbase;
$notifications = [];
switch ($forum->get_type()) {
case 'news':
if (SITEID == $forum->get_course_id()) {
$template = 'mod_forum/frontpage_news_discussion_list';
} else {
$template = 'mod_forum/news_discussion_list';
}
break;
case 'qanda':
$template = 'mod_forum/qanda_discussion_list';
break;
default:
$template = 'mod_forum/discussion_list';
}
return new discussion_list_renderer(
$forum,
$rendererbase,
$this->legacydatamapperfactory,
$this->exporterfactory,
$this->vaultfactory,
$this->builderfactory,
$capabilitymanager,
$this->urlfactory,
$template,
$notifications,
function($discussions, $user, $forum) {
$exporteddiscussionsummarybuilder = $this->builderfactory->get_exported_discussion_summaries_builder();
return $exporteddiscussionsummarybuilder->build(
$user,
$forum,
$discussions
);
}
);
}
/**
* Create a discussion list renderer which shows more information about the first post.
*
* @param forum_entity $forum The forum that the discussions belong to
* @param string $template The template to use
* @return discussion_list_renderer
*/
private function get_detailed_discussion_list_renderer(
forum_entity $forum,
string $template
) : discussion_list_renderer {
$capabilitymanager = $this->managerfactory->get_capability_manager($forum);
$rendererbase = $this->rendererbase;
$notifications = [];
return new discussion_list_renderer(
$forum,
$rendererbase,
$this->legacydatamapperfactory,
$this->exporterfactory,
$this->vaultfactory,
$this->builderfactory,
$capabilitymanager,
$this->urlfactory,
$template,
$notifications,
function($discussions, $user, $forum) use ($capabilitymanager) {
$exportedpostsbuilder = $this->builderfactory->get_exported_posts_builder();
$discussionentries = [];
$postentries = [];
foreach ($discussions as $discussion) {
$discussionentries[] = $discussion->get_discussion();
$discussionentriesids[] = $discussion->get_discussion()->get_id();
$postentries[] = $discussion->get_first_post();
}
$exportedposts['posts'] = $exportedpostsbuilder->build(
$user,
[$forum],
$discussionentries,
$postentries
);
$postvault = $this->vaultfactory->get_post_vault();
$canseeanyprivatereply = $capabilitymanager->can_view_any_private_reply($user);
$discussionrepliescount = $postvault->get_reply_count_for_discussion_ids(
$user,
$discussionentriesids,
$canseeanyprivatereply
);
$forumdatamapper = $this->legacydatamapperfactory->get_forum_data_mapper();
$forumrecord = $forumdatamapper->to_legacy_object($forum);
if (forum_tp_is_tracked($forumrecord, $user)) {
$discussionunreadscount = $postvault->get_unread_count_for_discussion_ids(
$user,
$discussionentriesids,
$canseeanyprivatereply
);
} else {
$discussionunreadscount = [];
}
array_walk($exportedposts['posts'], function($post) use ($discussionrepliescount, $discussionunreadscount) {
$post->discussionrepliescount = $discussionrepliescount[$post->discussionid] ?? 0;
$post->discussionunreadscount = $discussionunreadscount[$post->discussionid] ?? 0;
// TODO: Find a better solution due to language differences when defining the singular and plural form.
$post->isreplyplural = $post->discussionrepliescount != 1 ? true : false;
$post->isunreadplural = $post->discussionunreadscount != 1 ? true : false;
});
$exportedposts['state']['hasdiscussions'] = $exportedposts['posts'] ? true : false;
return $exportedposts;
}
);
}
/**
* Create a blog type discussion list renderer.
*
* @param forum_entity $forum The forum that the discussions belong to
* @return discussion_list_renderer
*/
public function get_blog_discussion_list_renderer(
forum_entity $forum
) : discussion_list_renderer {
return $this->get_detailed_discussion_list_renderer($forum, 'mod_forum/blog_discussion_list');
}
/**
* Create a discussion list renderer for the social course format.
*
* @param forum_entity $forum The forum that the discussions belong to
* @return discussion_list_renderer
*/
public function get_social_discussion_list_renderer(
forum_entity $forum
) : discussion_list_renderer {
return $this->get_detailed_discussion_list_renderer($forum, 'mod_forum/social_discussion_list');
}
/**
* Create a discussion list renderer for the social course format.
*
* @param forum_entity $forum The forum that the discussions belong to
* @return discussion_list_renderer
*/
public function get_frontpage_news_discussion_list_renderer(
forum_entity $forum
) : discussion_list_renderer {
return $this->get_detailed_discussion_list_renderer($forum, 'mod_forum/frontpage_social_discussion_list');
}
/**
* Create a single type discussion list renderer.
*
* @param forum_entity $forum Forum the discussion belongs to
* @param discussion_entity $discussion The discussion entity
* @param bool $hasmultiplediscussions Whether the forum has multiple discussions (more than one)
* @param int $displaymode How should the posts be formatted?
* @return discussion_renderer
*/
public function get_single_discussion_list_renderer(
forum_entity $forum,
discussion_entity $discussion,
bool $hasmultiplediscussions,
int $displaymode
) : discussion_renderer {
$capabilitymanager = $this->managerfactory->get_capability_manager($forum);
$ratingmanager = $this->managerfactory->get_rating_manager();
$rendererbase = $this->rendererbase;
$cmid = $forum->get_course_module_record()->id;
$baseurl = $this->urlfactory->get_forum_view_url_from_course_module_id($cmid);
$notifications = array();
if ($hasmultiplediscussions) {
$notifications[] = (new notification(get_string('warnformorepost', 'forum')))
->set_show_closebutton(true);
}
return new discussion_renderer(
$forum,
$discussion,
$displaymode,
$rendererbase,
$this->get_single_discussion_posts_renderer($displaymode, false),
$this->page,
$this->legacydatamapperfactory,
$this->exporterfactory,
$this->vaultfactory,
$capabilitymanager,
$ratingmanager,
$this->entityfactory->get_exported_posts_sorter(),
$baseurl,
$notifications
);
}
}