utils.py 3.92 KB
Newer Older
1
from django.conf import settings
2 3
from xmodule.modulestore import Location
from xmodule.modulestore.django import modulestore
4 5
from xmodule.modulestore.exceptions import ItemNotFoundError

6 7

def get_course_location_for_item(location):
8 9 10 11 12 13
    '''
    cdodge: for a given Xmodule, return the course that it belongs to
    NOTE: This makes a lot of assumptions about the format of the course location
    Also we have to assert that this module maps to only one course item - it'll throw an
    assert if not
    '''
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
    item_loc = Location(location)

    # check to see if item is already a course, if so we can skip this
    if item_loc.category != 'course':
        # @hack! We need to find the course location however, we don't
        # know the 'name' parameter in this context, so we have
        # to assume there's only one item in this query even though we are not specifying a name
        course_search_location = ['i4x', item_loc.org, item_loc.course, 'course', None]
        courses = modulestore().get_items(course_search_location)

        # make sure we found exactly one match on this above course search
        found_cnt = len(courses)
        if found_cnt == 0:
            raise BaseException('Could not find course at {0}'.format(course_search_location))

        if found_cnt > 1:
30
            raise BaseException('Found more than one course at {0}. There should only be one!!! Dump = {1}'.format(course_search_location, courses))
31 32 33 34

        location = courses[0].location

    return location
35

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
def get_course_for_item(location):
    '''
    cdodge: for a given Xmodule, return the course that it belongs to
    NOTE: This makes a lot of assumptions about the format of the course location
    Also we have to assert that this module maps to only one course item - it'll throw an
    assert if not
    '''
    item_loc = Location(location)

    # @hack! We need to find the course location however, we don't
    # know the 'name' parameter in this context, so we have
    # to assume there's only one item in this query even though we are not specifying a name
    course_search_location = ['i4x', item_loc.org, item_loc.course, 'course', None]
    courses = modulestore().get_items(course_search_location)

    # make sure we found exactly one match on this above course search
    found_cnt = len(courses)
    if found_cnt == 0:
        raise BaseException('Could not find course at {0}'.format(course_search_location))

    if found_cnt > 1:
        raise BaseException('Found more than one course at {0}. There should only be one!!! Dump = {1}'.format(course_search_location, courses))

    return courses[0]

61

62
def get_lms_link_for_item(location, preview=False):
63
    location = Location(location)
64
    if settings.LMS_BASE is not None:
65 66
        lms_link = "//{preview}{lms_base}/courses/{course_id}/jump_to/{location}".format(
            preview='preview.' if preview else '',
67 68
            lms_base=settings.LMS_BASE,
            # TODO: These will need to be changed to point to the particular instance of this problem in the particular course
69
            course_id=modulestore().get_containing_courses(location)[0].id,
70
            location=location,
71 72 73 74 75 76
        )
    else:
        lms_link = None

    return lms_link

77

78 79 80 81 82 83
class UnitState(object):
    draft = 'draft'
    private = 'private'
    public = 'public'


84 85 86 87 88 89 90 91 92 93
def compute_unit_state(unit):
    """
    Returns whether this unit is 'draft', 'public', or 'private'.

    'draft' content is in the process of being edited, but still has a previous
        version visible in the LMS
    'public' content is locked and visible in the LMS
    'private' content is editabled and not visible in the LMS
    """

94
    if unit.metadata.get('is_draft', False):
95
        try:
96
            modulestore('direct').get_item(unit.location)
97
            return UnitState.draft
98
        except ItemNotFoundError:
99
            return UnitState.private
100
    else:
101
        return UnitState.public
Lyla Fischer committed
102 103 104 105


def get_date_display(date):
    return date.strftime("%d %B, %Y at %I:%M %p")