From 553480e044a9bc8cea53564c2228b6debf601099 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Tue, 9 Jul 2024 10:11:09 +0100 Subject: [PATCH] MDL-82359 mod_forum: Move user posts links logic into module 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 --- lib/navigationlib.php | 20 ------------------ mod/forum/lib.php | 47 +++++++++++++++++++++++++++++++++++++++++++ mod/forum/version.php | 2 +- 3 files changed, 48 insertions(+), 21 deletions(-) diff --git a/lib/navigationlib.php b/lib/navigationlib.php index 24a175bb321..80d9953d3e5 100644 --- a/lib/navigationlib.php +++ b/lib/navigationlib.php @@ -2589,16 +2589,6 @@ class global_navigation extends navigation_node { } } - 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)) { @@ -5201,16 +5191,6 @@ class settings_navigation extends navigation_node { $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)) { diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 849b18c06d9..ecfaea1d91a 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -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); + } +} diff --git a/mod/forum/version.php b/mod/forum/version.php index 2a5cac776fa..06b343adf5f 100644 --- a/mod/forum/version.php +++ b/mod/forum/version.php @@ -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)