plugins.py 2.77 KB
Newer Older
1 2 3 4 5 6 7 8 9
"""
Platform plugins to support the course experience.

This includes any locally defined CourseTools.
"""
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext as _

from course_tools import CourseTool
10
from courseware.courses import get_course_by_id
11
from student.models import CourseEnrollment
12 13 14 15
from views.course_reviews import CourseReviewsModuleFragmentView
from views.course_updates import CourseUpdatesFragmentView

from . import SHOW_REVIEWS_TOOL_FLAG, UNIFIED_COURSE_TAB_FLAG
16 17 18 19 20 21 22


class CourseUpdatesTool(CourseTool):
    """
    The course updates tool.
    """
    @classmethod
23 24 25 26 27 28 29
    def analytics_id(cls):
        """
        Returns an analytics id for this tool, used for eventing.
        """
        return 'edx.updates'

    @classmethod
30 31 32 33 34 35 36 37 38 39 40 41 42 43
    def title(cls):
        """
        Returns the title of this tool.
        """
        return _('Updates')

    @classmethod
    def icon_classes(cls):
        """
        Returns icon classes needed to represent this tool.
        """
        return 'fa fa-newspaper-o'

    @classmethod
44
    def is_enabled(cls, request, course_key):
45
        """
46
        Returns True if the user should be shown course updates for this course.
47
        """
48 49 50 51
        if not UNIFIED_COURSE_TAB_FLAG.is_enabled(course_key):
            return False
        if not CourseEnrollment.is_enrolled(request.user, course_key):
            return False
52
        course = get_course_by_id(course_key)
53
        return CourseUpdatesFragmentView.has_updates(request, course)
54 55 56 57 58 59 60 61 62 63 64 65 66 67

    @classmethod
    def url(cls, course_key):
        """
        Returns the URL for this tool for the specified course key.
        """
        return reverse('openedx.course_experience.course_updates', args=[course_key])


class CourseReviewsTool(CourseTool):
    """
    The course reviews tool.
    """
    @classmethod
68 69 70 71 72 73 74
    def analytics_id(cls):
        """
        Returns an id to uniquely identify this tool in analytics events.
        """
        return 'edx.reviews'

    @classmethod
75 76 77 78 79 80 81 82 83 84 85 86 87 88
    def title(cls):
        """
        Returns the title of this tool.
        """
        return _('Reviews')

    @classmethod
    def icon_classes(cls):
        """
        Returns icon classes needed to represent this tool.
        """
        return 'fa fa-star'

    @classmethod
89
    def is_enabled(cls, request, course_key):
90 91 92
        """
        Returns True if this tool is enabled for the specified course key.
        """
93 94 95
        if not SHOW_REVIEWS_TOOL_FLAG.is_enabled(course_key):
            return False
        return CourseReviewsModuleFragmentView.is_configured()
96 97 98 99 100 101 102

    @classmethod
    def url(cls, course_key):
        """
        Returns the URL for this tool for the specified course key.
        """
        return reverse('openedx.course_experience.course_reviews', args=[course_key])