From 40ce63cbb7b2f51e9b06935ae12950457c795d38 Mon Sep 17 00:00:00 2001 From: tjhunt Date: Tue, 24 Jul 2007 17:06:29 +0000 Subject: [PATCH] MDL-10588 - refactor the code for printing the 'subscribe/unsubscribe from this forum' link into one place. --- course/format/social/format.php | 10 +--- mod/forum/index.php | 85 ++++++++++++--------------------- mod/forum/lib.php | 39 +++++++++++++++ mod/forum/view.php | 11 +---- 4 files changed, 74 insertions(+), 71 deletions(-) diff --git a/course/format/social/format.php b/course/format/social/format.php index 1662888d43..794ed73038 100644 --- a/course/format/social/format.php +++ b/course/format/social/format.php @@ -43,14 +43,8 @@ echo ''; if ($forum = forum_get_course_forum($course->id, 'social')) { print_heading_block(get_string('socialheadline')); - if (forum_is_forcesubscribed($forum->id)) { - echo ''; - } else if (forum_is_subscribed($USER->id, $forum->id)) { - echo ''; - } else { - echo ''; - } - + $context = get_context_instance(CONTEXT_MODULE, get_coursemodule_from_instance('forum', $forum->id)); + echo ''; forum_print_latest_discussions($course, $forum, 10, 'plain', '', false); } else { diff --git a/mod/forum/index.php b/mod/forum/index.php index c6bf70ca00..068bd218c8 100644 --- a/mod/forum/index.php +++ b/mod/forum/index.php @@ -170,6 +170,7 @@ $groupmode = NOGROUPS; } + $cantaccessagroup = $groupmode and !has_capability('moodle/site:accessallgroups', $context) and !mygroupid($course->id); // this is potentially wrong logic. could possibly check for if user has the right to hmmm if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) { @@ -208,12 +209,18 @@ $introoptions->para=false; $forum->intro = shorten_text(trim(format_text($forum->intro, FORMAT_HTML, $introoptions)), $CFG->forum_shortpost); - if ($forum->visible) { - $forumlink = "id\">".format_string($forum->name,true).""; - $discussionlink = "id\">".$count.""; + $forumname = format_string($forum->name,true);; + if ($cantaccessagroup) { + $forumlink = $forumname; + $discussionlink = $count; } else { - $forumlink = "id\">".format_string($forum->name,true).""; - $discussionlink = "id\">".$count.""; + if ($forum->visible) { + $style = ''; + } else { + $style = 'class="dimmed"'; + } + $forumlink = "id\" $style>".format_string($forum->name,true).""; + $discussionlink = "id\" $style>".$count.""; } $row = array ($forumlink, $forum->intro, $discussionlink); @@ -223,29 +230,9 @@ } if ($can_subscribe) { - if (forum_is_forcesubscribed($forum->id)) { - $sublink = $stryes; - } else { - if ($groupmode and !has_capability('moodle/site:accessallgroups', $context) and !mygroupid($course->id)) { - $sublink = $strno; // Can't subscribe to a group forum (not in a group) - $forumlink = format_string($forum->name,true); - } else { - if (forum_is_subscribed($USER->id, $forum->id)) { - $subscribed = $stryes; - $subtitle = get_string("unsubscribe", "forum"); - } else { - $subscribed = $strno; - $subtitle = get_string("subscribe", "forum"); - } - if ($forum->forcesubscribe == FORUM_DISALLOWSUBSCRIBE - && !has_capability('mod/forum:managesubscriptions', $context)) { - $sublink = '-'; - } else { - $sublink = "id\">$subscribed"; - } - } - } - $row[] = $sublink; + $row[] = forum_get_subscribe_link($forum, $context, array('subscribed' => $stryes, + 'unsubscribed' => $strno, 'forcesubscribed' => $stryes, + 'cantsubscribe' => '-'), $cantaccessagroup); } //If this forum has RSS activated, calculate it @@ -316,6 +303,8 @@ $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id); $context = get_context_instance(CONTEXT_MODULE, $cm->id); + $cantaccessagroup = $groupmode and !has_capability('moodle/site:accessallgroups', $context) and !mygroupid($course->id); + if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) { $count = count_records("forum_discussions", "forum", "$forum->id", "groupid", $currentgroup); } else { @@ -359,12 +348,18 @@ $printsection = ""; } - if ($forum->visible) { - $forumlink = "id\">".format_string($forum->name,true).""; - $discussionlink = "id\">".$count.""; + $forumname = format_string($forum->name,true);; + if ($cantaccessagroup && $groupmode == SEPARATEGROUPS) { + $forumlink = $forumname; + $discussionlink = $count; } else { - $forumlink = "id\">".format_string($forum->name,true).""; - $discussionlink = "id\">".$count.""; + if ($forum->visible) { + $style = ''; + } else { + $style = 'class="dimmed"'; + } + $forumlink = "id\" $style>".format_string($forum->name,true).""; + $discussionlink = "id\" $style>".$count.""; } $row = array ($printsection, $forumlink, $forum->intro, $discussionlink); @@ -374,27 +369,9 @@ } if ($can_subscribe) { - if (forum_is_forcesubscribed($forum->id)) { - $sublink = $stryes; - } else { - if ($groupmode and !has_capability('moodle/site:accessallgroups', $context) - and !mygroupid($course->id)) { - $sublink = $strno; // Can't subscribe to a group forum (not in a group) - if ($groupmode == SEPARATEGROUPS) { - $forumlink = format_string($forum->name,true); - } - } else { - if (forum_is_subscribed($USER->id, $forum->id)) { - $subscribed = $stryes; - $subtitle = $strunsubscribe; - } else { - $subscribed = $strno; - $subtitle = $strsubscribe; - } - $sublink = "id\">$subscribed"; - } - } - $row[] = $sublink; + $row[] = forum_get_subscribe_link($forum, $context, array('subscribed' => $stryes, + 'unsubscribed' => $strno, 'forcesubscribed' => $stryes, + 'cantsubscribe' => '-'), $cantaccessagroup); } //If this forum has RSS activated, calculate it diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 2d1c34965b..e97fc3cecf 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -3214,6 +3214,45 @@ function forum_post_subscription($post) { return "

".get_string("nownotsubscribed", "forum", $info)."

"; } +/** + * Generate and return the subscribe or unsubscribe link for a forum. + * @param object $forum the forum. Fields used are $forum->id and $forum->forcesubscribe. + * @param object $context the context object for this forum. + * @param array $messages text used for the link in its various states + * (subscribed, unsubscribed, forcesubscribed or cantsubscribe). + * Any strings not passed in are taken from the $defaultmessages array + * at the top of the function. + * @param + */ +function forum_get_subscribe_link($forum, $context, $messages = array(), $cantaccessagroup = false) { + global $CFG, $USER; + $defaultmessages = array( + 'subscribed' => get_string('unsubscribe', 'forum'), + 'unsubscribed' => get_string('subscribe', 'forum'), + 'cantaccessgroup' => get_string('no'), + 'forcesubscribed' => get_string('everyoneissubscribed', 'forum'), + 'cantsubscribe' => get_string('disallowsubscribe','forum') + ); + $messages = $messages + $defaultmessages; + + if (forum_is_forcesubscribed($forum->id)) { + return $messages['forcesubscribed']; + } else if ($forum->forcesubscribe == FORUM_DISALLOWSUBSCRIBE && !has_capability('mod/forum:managesubscriptions', $context)) { + return $messages['cantsubscribe']; + } else if ($cantaccessagroup) { + return $messages['cantaccessgroup']; + } else { + if (forum_is_subscribed($USER->id, $forum->id)) { + $linktext = $messages['subscribed']; + $linktitle = get_string('subscribestopt', 'forum'); + } else { + $linktext = $messages['unsubscribed']; + $linktitle = get_string('subscribestart', 'forum'); + } + return '' . $linktext . ''; + } +} /** * diff --git a/mod/forum/view.php b/mod/forum/view.php index 50e8a9c4f1..f3ddb46e38 100644 --- a/mod/forum/view.php +++ b/mod/forum/view.php @@ -154,15 +154,8 @@ echo ''.$streveryonecannowchoose.''; } - if (forum_is_subscribed($USER->id, $forum->id)) { - $subtexttitle = get_string("subscribestop", "forum"); - $subtext = get_string("unsubscribe", "forum"); - } else { - $subtexttitle = get_string("subscribestart", "forum"); - $subtext = get_string("subscribe", "forum"); - } - echo "
"; - echo "id\">$subtext"; + echo '
', forum_get_subscribe_link($forum, $context, + array('forcesubscribed' => '', 'cantsubscribe' => '')), ''; } if (forum_tp_can_track_forums($forum) && ($forum->trackingtype == FORUM_TRACKING_OPTIONAL)) { -- 2.39.5