From 6214e6beeee75950719389bd1fd83471b85cf203 Mon Sep 17 00:00:00 2001 From: Ahtisham Shahid Date: Tue, 31 May 2022 13:00:41 +0500 Subject: [PATCH] feat: allow banner for discussions moderators (#30502) --- lms/djangoapps/discussion/views.py | 39 ++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/lms/djangoapps/discussion/views.py b/lms/djangoapps/discussion/views.py index 9c4b9b6630..b056c24f4b 100644 --- a/lms/djangoapps/discussion/views.py +++ b/lms/djangoapps/discussion/views.py @@ -29,6 +29,7 @@ from xmodule.modulestore.django import modulestore import lms.djangoapps.discussion.django_comment_client.utils as utils import openedx.core.djangoapps.django_comment_common.comment_client as cc from common.djangoapps.student.models import CourseEnrollment +from common.djangoapps.student.roles import CourseInstructorRole, CourseStaffRole, GlobalStaff from common.djangoapps.util.json_request import JsonResponse, expect_json from lms.djangoapps.courseware.access import has_access from lms.djangoapps.courseware.courses import get_course_with_access @@ -44,7 +45,7 @@ from lms.djangoapps.discussion.django_comment_client.utils import ( get_group_id_for_comments_service, get_group_id_for_user, is_commentable_divided, - strip_none, + strip_none ) from lms.djangoapps.discussion.exceptions import TeamDiscussionHiddenFromUserException from lms.djangoapps.discussion.toggles import ENABLE_DISCUSSIONS_MFE, ENABLE_DISCUSSIONS_MFE_FOR_EVERYONE @@ -55,13 +56,19 @@ from openedx.core.djangoapps.discussions.utils import ( available_division_schemes, get_discussion_categories_ids, get_divided_discussions, - get_group_names_by_id, + get_group_names_by_id +) +from openedx.core.djangoapps.django_comment_common.models import ( + FORUM_ROLE_ADMINISTRATOR, + FORUM_ROLE_COMMUNITY_TA, + FORUM_ROLE_GROUP_MODERATOR, + FORUM_ROLE_MODERATOR, + CourseDiscussionSettings, + Role ) -from openedx.core.djangoapps.django_comment_common.models import CourseDiscussionSettings from openedx.core.djangoapps.django_comment_common.utils import ThreadContext from openedx.core.djangoapps.plugin_api.views import EdxFragmentView from openedx.features.course_duration_limits.access import generate_course_expired_fragment -from common.djangoapps.student.roles import CourseInstructorRole, CourseStaffRole, GlobalStaff User = get_user_model() log = logging.getLogger("edx.discussions") @@ -707,7 +714,8 @@ def followed_threads(request, course_key, user_id): def _discussions_mfe_context(query_params: Dict, course_key: CourseKey, is_educator_or_staff=False, - legacy_only_view=False) -> Optional[Dict]: + legacy_only_view=False, + is_privileged=False) -> Optional[Dict]: """ Returns the context for rendering the MFE banner and MFE. @@ -740,7 +748,7 @@ def _discussions_mfe_context(query_params: Dict, "mfe_url": f"{forum_url}?discussions_experience=new", "share_feedback_url": settings.DISCUSSIONS_MFE_FEEDBACK_URL, "course_key": course_key, - "show_banner": enable_mfe, + "show_banner": enable_mfe and is_privileged, "discussions_mfe_url": mfe_url, } @@ -752,6 +760,21 @@ def is_course_staff(course_key: CourseKey, user: User): return CourseInstructorRole(course_key).has_user(user) or CourseStaffRole(course_key).has_user(user) +def is_privileged_user(course_key: CourseKey, user: User): + """ + Returns True if user has one of following course role + Administrator, Moderator, Group Moderator, Community TA + """ + forum_roles = [ + FORUM_ROLE_COMMUNITY_TA, + FORUM_ROLE_GROUP_MODERATOR, + FORUM_ROLE_MODERATOR, + FORUM_ROLE_ADMINISTRATOR + ] + has_course_role = Role.user_has_role_for_course(user, course_key, forum_roles) + return GlobalStaff().has_user(user) or is_course_staff(course_key, user) or has_course_role + + class DiscussionBoardFragmentView(EdxFragmentView): """ Component implementation of the discussion board. @@ -782,7 +805,9 @@ class DiscussionBoardFragmentView(EdxFragmentView): # Force using the legacy view if a user profile is requested or the URL contains a specific topic or thread force_legacy_view = (profile_page_context or thread_id or discussion_id) is_educator_or_staff = is_course_staff(course_key, request.user) or GlobalStaff().has_user(request.user) - mfe_context = _discussions_mfe_context(request.GET, course_key, is_educator_or_staff, force_legacy_view) + is_privileged = is_privileged_user(course_key, request.user) + mfe_context = _discussions_mfe_context(request.GET, course_key, is_educator_or_staff, force_legacy_view, + is_privileged) if mfe_context["show_mfe"]: fragment = Fragment(render_to_string('discussion/discussion_mfe_embed.html', mfe_context)) fragment.add_css(