diff --git a/lms/djangoapps/discussion/django_comment_client/base/tests.py b/lms/djangoapps/discussion/django_comment_client/base/tests.py index 4f7de85547..1fe5522d38 100644 --- a/lms/djangoapps/discussion/django_comment_client/base/tests.py +++ b/lms/djangoapps/discussion/django_comment_client/base/tests.py @@ -269,7 +269,9 @@ class ViewsTestCaseMixin: data = { "user_id": str(self.student.id), "closed": False, - "commentable_id": "non_team_dummy_id" + "commentable_id": "non_team_dummy_id", + "thread_id": "dummy", + "thread_type": "discussion" } if include_depth: data["depth"] = 0 @@ -1141,7 +1143,7 @@ class ViewPermissionsTestCase(ForumsEnableMixin, UrlResetMixin, SharedModuleStor def test_endorse_response_as_staff(self, mock_request): self._set_mock_request_thread_and_comment( mock_request, - {"type": "thread", "thread_type": "question", "user_id": str(self.student.id)}, + {"type": "thread", "thread_type": "question", "user_id": str(self.student.id), "commentable_id": "course"}, {"type": "comment", "thread_id": "dummy"} ) self.client.login(username=self.moderator.username, password=self.password) @@ -1153,7 +1155,8 @@ class ViewPermissionsTestCase(ForumsEnableMixin, UrlResetMixin, SharedModuleStor def test_endorse_response_as_student(self, mock_request): self._set_mock_request_thread_and_comment( mock_request, - {"type": "thread", "thread_type": "question", "user_id": str(self.moderator.id)}, + {"type": "thread", "thread_type": "question", + "user_id": str(self.moderator.id), "commentable_id": "course"}, {"type": "comment", "thread_id": "dummy"} ) self.client.login(username=self.student.username, password=self.password) @@ -1165,7 +1168,7 @@ class ViewPermissionsTestCase(ForumsEnableMixin, UrlResetMixin, SharedModuleStor def test_endorse_response_as_student_question_author(self, mock_request): self._set_mock_request_thread_and_comment( mock_request, - {"type": "thread", "thread_type": "question", "user_id": str(self.student.id)}, + {"type": "thread", "thread_type": "question", "user_id": str(self.student.id), "commentable_id": "course"}, {"type": "comment", "thread_id": "dummy"} ) self.client.login(username=self.student.username, password=self.password) diff --git a/lms/djangoapps/discussion/django_comment_client/base/views.py b/lms/djangoapps/discussion/django_comment_client/base/views.py index d88c6e063a..f7036041b3 100644 --- a/lms/djangoapps/discussion/django_comment_client/base/views.py +++ b/lms/djangoapps/discussion/django_comment_client/base/views.py @@ -349,6 +349,42 @@ def track_comment_unreported_event(request, course, comment): track_forum_event(request, event_name, course, comment, event_data) +def track_response_mark_event(request, course, comment, commentable_id, thread_type): + """ + Send analytics event for response that is marked as endorsed or answered. + """ + event_name = _EVENT_NAME_TEMPLATE.format(obj_type='response', action_name='mark') + if thread_type == 'question': + mark_type = 'Answer' + else: + mark_type = 'Endorse' + event_data = { + 'discussion': {'id': comment.thread_id}, + 'commentable_id': commentable_id, + 'mark_type': mark_type, + 'target_username': comment.get('username') + } + track_forum_event(request, event_name, course, comment, event_data) + + +def track_response_unmark_event(request, course, comment, commentable_id, thread_type): + """ + Send analytics event for response that is marked as unendorsed or unanswered. + """ + event_name = _EVENT_NAME_TEMPLATE.format(obj_type='response', action_name='unmark') + if thread_type == 'question': + mark_type = 'Answer' + else: + mark_type = 'Endorse' + event_data = { + 'discussion': {'id': comment.thread_id}, + 'commentable_id': commentable_id, + 'mark_type': mark_type, + 'target_username': comment.get('username'), + } + track_forum_event(request, event_name, course, comment, event_data) + + def track_discussion_reported_event(request, course, cc_content): """ Helper method for discussion reported events. @@ -369,6 +405,20 @@ def track_discussion_unreported_event(request, course, cc_content): track_comment_unreported_event(request, course, cc_content) +def track_forum_response_mark_event(request, course, cc_content, value): + """ + Helper method for discussions response mark event + """ + thread = cc.Thread.find(cc_content.thread_id) + commentable_id = thread.get('commentable_id') + thread_type = thread.get('thread_type') + + if value: + track_response_mark_event(request, course, cc_content, commentable_id, thread_type) + else: + track_response_unmark_event(request, course, cc_content, commentable_id, thread_type) + + def permitted(func): """ View decorator to verify the user is authorized to access this endpoint. @@ -661,11 +711,14 @@ def endorse_comment(request, course_id, comment_id): """ course_key = CourseKey.from_string(course_id) comment = cc.Comment.find(comment_id) + course = get_course_with_access(request.user, 'load', course_key) user = request.user - comment.endorsed = request.POST.get('endorsed', 'false').lower() == 'true' + endorsed = request.POST.get('endorsed', 'false').lower() == 'true' + comment.endorsed = endorsed comment.endorsement_user_id = user.id comment.save() comment_endorsed.send(sender=None, user=user, post=comment) + track_forum_response_mark_event(request, course, comment, endorsed) return JsonResponse(prepare_content(comment.to_dict(), course_key)) diff --git a/lms/djangoapps/discussion/rest_api/serializers.py b/lms/djangoapps/discussion/rest_api/serializers.py index f3e361431f..13855317ee 100644 --- a/lms/djangoapps/discussion/rest_api/serializers.py +++ b/lms/djangoapps/discussion/rest_api/serializers.py @@ -16,7 +16,7 @@ from rest_framework import serializers from common.djangoapps.student.models import get_user_by_username_or_email from common.djangoapps.student.roles import GlobalStaff from lms.djangoapps.discussion.django_comment_client.base.views import track_thread_lock_unlock_event, \ - track_thread_edited_event, track_comment_edited_event + track_thread_edited_event, track_comment_edited_event, track_forum_response_mark_event from lms.djangoapps.discussion.django_comment_client.utils import ( course_discussion_division_enabled, get_group_id_for_user, @@ -591,6 +591,7 @@ class CommentSerializer(_ContentSerializer): # endorsement_user_id on update requesting_user_id = self.context["cc_requester"]["id"] if key == "endorsed": + track_forum_response_mark_event(self.context['request'], self.context['course'], instance, val) instance["endorsement_user_id"] = requesting_user_id if key == "body" and val: instance["editing_user_id"] = requesting_user_id