<commit_before>"""
Forum Leaderboard XBlock

Shows the top threads for a given discussion ID by vote.
"""
from .leaderboard import LeaderboardXBlock

from xblock.core import XBlock
from xblock.fields import Scope, String

try:
    import lms.lib.comment_client as cc
    DEV_MODE = False
except ImportError:
    # We're in the SDK, probably.
    import dummy_cc as cc
    DEV_MODE = True


class ForumLeaderboardXBlock(LeaderboardXBlock):
    STUDENT_VIEW_TEMPLATE = "forum_leaderboard.html"

    display_name = String(
        default="Forum Leaderboard", scope=Scope.settings,
        help="Display name for this block."
    )
    discussion_id = String(
        default="", scope=Scope.settings,
        help="The ID of the inline discussion to tally leading threads for.",
    )

    def get_course(self):
        """
        Gets the course, or returns a dummy value if not in an edx-platform
        environment.
        """
        if DEV_MODE:
            return 'dummy_key'
        else:
            return self.location.course_key

    def get_thread_url(self, course, discussion_id, thread_id):
        """
        Due to package structure, we can't easily import the standard
        reverse_course_url function, which is the right way to do this.
        """
        return "/courses/{0}/discussion/forum/{1}/threads/{2}".format(course, discussion_id, thread_id)

    def get_scores(self):
        """
        Compute the top threads and return them.
        """
        course = self.get_course()
        threads = cc.Thread.search({
            'course_id': unicode(course), 'commentable_id': self.discussion_id,
            'sort_key': 'votes', 'per_page': self.count - 1
        })[0]

        scored_threads = []
        for thread in threads:
            score = thread['votes']['point']  # Might be 0
            if score:
                thread['url'] = self.get_thread_url(course, self.discussion_id, thread['id'])<commit_msg>fixup! Factor out common code, add rank calculation<commit_after>"""
Forum Leaderboard XBlock

Shows the top threads for a given discussion ID by vote.
"""
from .leaderboard import LeaderboardXBlock

from xblock.core import XBlock
from xblock.fields import Scope, String

try:
    import lms.lib.comment_client as cc
    DEV_MODE = False
except ImportError:
    # We're in the SDK, probably.
    import dummy_cc as cc
    DEV_MODE = True


class ForumLeaderboardXBlock(LeaderboardXBlock):
    STUDENT_VIEW_TEMPLATE = "forum_leaderboard.html"

    display_name = String(
        default="Forum Leaderboard", scope=Scope.settings,
        help="Display name for this block."
    )
    discussion_id = String(
        default="", scope=Scope.settings,
        help="The ID of the inline discussion to tally leading threads for.",
    )

    def get_course(self):
        """
        Gets the course, or returns a dummy value if not in an edx-platform
        environment.
        """
        if DEV_MODE:
            return 'dummy_key'
        else:
            return self.location.course_key

    def get_thread_url(self, course, discussion_id, thread_id):
        """
        Due to package structure, we can't easily import the standard
        reverse_course_url function, which is the right way to do this.
        """
        return "/courses/{0}/discussion/forum/{1}/threads/{2}".format(course, discussion_id, thread_id)

    def get_scores(self):
        """
        Compute the top threads and return them.
        """
        if not self.discussion_id:
            raise RuntimeError("No discussion ID configured.")
        course = self.get_course()
        threads = cc.Thread.search({
            'course_id': unicode(course), 'commentable_id': self.discussion_id,
            'sort_key': 'votes', 'per_page': self.count - 1
        })[0]

        scored_threads = []
        for thread in threads:
            score = thread['votes']['point']  # Might be 0
            if score:
                thread['url'] = self.get_thread_url(course, self.discussion_id, thread['id'])
{'fraction_non_alphanumeric': 0.09598214285714286, 'fraction_numerical': 0.002976190476190476, 'mean_word_length': 2.4887543252595155, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4605364', 'n_tokens_mistral': 1174, 'n_tokens_neox': 1125, 'n_words': 350}