Skip to content

Commit

Permalink
MDL-82359 mod_forum: Move user posts links logic into module
Browse files Browse the repository at this point in the history
The logic about adding links to view user posts in the forum module
was previously in core code, but there is an existing callback that
means this can be implemented entirely within the forum module.

Co-authored-by: sam marshall <[email protected]>
  • Loading branch information
andrewnicols and sammarshallou committed Aug 21, 2024
1 parent cdd8cbb commit 553480e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 21 deletions.
20 changes: 0 additions & 20 deletions lib/navigationlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2589,16 +2589,6 @@ protected function load_for_user($user=null, $forceforcontext=false) {
}
}

if (!empty($CFG->navadduserpostslinks)) {
// Add nodes for forum posts and discussions if the user can view either or both
// There are no capability checks here as the content of the page is based
// purely on the forums the current user has access too.
$forumtab = $usernode->add(get_string('forumposts', 'forum'));
$forumtab->add(get_string('posts', 'forum'), new moodle_url('/mod/forum/user.php', $baseargs));
$forumtab->add(get_string('discussions', 'forum'), new moodle_url('/mod/forum/user.php',
array_merge($baseargs, array('mode' => 'discussions'))));
}

// Add blog nodes.
if (!empty($CFG->enableblogs)) {
if (!$this->cache->cached('userblogoptions'.$user->id)) {
Expand Down Expand Up @@ -5201,16 +5191,6 @@ protected function generate_user_settings($courseid, $userid, $gstitle='usercurr
$profilenode = $mainpage->add(get_string('profile'), new moodle_url('/user/profile.php',
array('id' => $user->id)), self::TYPE_SETTING, null, 'myprofile');

if (!empty($CFG->navadduserpostslinks)) {
// Add nodes for forum posts and discussions if the user can view either or both
// There are no capability checks here as the content of the page is based
// purely on the forums the current user has access too.
$forumtab = $profilenode->add(get_string('forumposts', 'forum'));
$forumtab->add(get_string('posts', 'forum'), new moodle_url('/mod/forum/user.php', $baseargs), null, 'myposts');
$forumtab->add(get_string('discussions', 'forum'), new moodle_url('/mod/forum/user.php',
array_merge($baseargs, array('mode' => 'discussions'))), null, 'mydiscussions');
}

// Add blog nodes.
if (!empty($CFG->enableblogs)) {
if (!$this->cache->cached('userblogoptions'.$user->id)) {
Expand Down
47 changes: 47 additions & 0 deletions mod/forum/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -6912,3 +6912,50 @@ function forum_refresh_events(int $courseid, stdClass $instance, stdClass $cm):

forum_update_calendar($instance, $cm->id);
}

/**
* Callback adds navigation to view user posts if the navadduserpostslinks config is on.
*
* @param navigation_node $usernode User node within navigation
* @param stdClass $user User object
* @param \core\context\user $usercontext User context
* @param stdClass $course Current course
* @param \core\context $coursecontext Course context
*/
function mod_forum_extend_navigation_user(
navigation_node $usernode,
stdClass $user,
\core\context\user $usercontext,
stdClass $course,
\core\context $coursecontext,
): void {
global $CFG;
if (!empty($CFG->navadduserpostslinks) && $coursecontext instanceof \core\context\system) {
$baseargs = ['id' => $user->id];

// Add nodes for forum posts and discussions if the user can view either or both
// There are no capability checks here as the content of the page is based
// purely on the forums the current user has access too.
$forumtab = \navigation_node::create(get_string('forumposts', 'forum'));
$forumtab->add(
get_string('posts', 'forum'),
new moodle_url('/mod/forum/user.php', $baseargs),
);
$forumtab->add(
get_string('discussions', 'forum'),
new moodle_url('/mod/forum/user.php',
array_merge($baseargs, ['mode' => 'discussions']),
),
);

// We add the forum link either immediately after the 'viewuserdetails' link, or as the first item in the list.
foreach ($usernode->children as $child) {
if ($child->key === 'viewuserdetails') {
continue;
}
$addbefore = $child;
break;
}
$usernode->add_node($forumtab, $addbefore->key);
}
}
2 changes: 1 addition & 1 deletion mod/forum/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2024042200; // The current module version (Date: YYYYMMDDXX).
$plugin->version = 2024082100; // The current module version (Date: YYYYMMDDXX).
$plugin->requires = 2024041600; // Requires this Moodle version.
$plugin->component = 'mod_forum'; // Full name of the plugin (used for diagnostics)

0 comments on commit 553480e

Please sign in to comment.