entrance_exams.py 2.15 KB
Newer Older
1 2 3 4 5
"""
This file contains all entrance exam related utils/logic.
"""

from courseware.access import has_access
6
from opaque_keys.edx.keys import UsageKey
7
from student.models import EntranceExamConfiguration
8
from util.milestones_helpers import get_required_content, is_entrance_exams_enabled
9 10 11 12 13 14 15
from xmodule.modulestore.django import modulestore


def course_has_entrance_exam(course):
    """
    Checks to see if a course is properly configured for an entrance exam
    """
16
    if not is_entrance_exams_enabled():
17 18 19 20 21 22 23 24
        return False
    if not course.entrance_exam_enabled:
        return False
    if not course.entrance_exam_id:
        return False
    return True


25
def user_can_skip_entrance_exam(user, course):
26 27 28 29 30 31 32 33 34 35 36 37
    """
    Checks all of the various override conditions for a user to skip an entrance exam
    Begin by short-circuiting if the course does not have an entrance exam
    """
    if not course_has_entrance_exam(course):
        return True
    if not user.is_authenticated():
        return False
    if has_access(user, 'staff', course):
        return True
    if EntranceExamConfiguration.user_can_skip_entrance_exam(user, course.id):
        return True
38
    if not get_entrance_exam_content(user, course):
39 40 41 42
        return True
    return False


43
def user_has_passed_entrance_exam(user, course):
44 45 46 47 48 49
    """
    Checks to see if the user has attained a sufficient score to pass the exam
    Begin by short-circuiting if the course does not have an entrance exam
    """
    if not course_has_entrance_exam(course):
        return True
50
    if not user.is_authenticated():
51
        return False
52
    return get_entrance_exam_content(user, course) is None
53 54


55
def get_entrance_exam_content(user, course):
56 57 58
    """
    Get the entrance exam content information (ie, chapter module)
    """
59
    required_content = get_required_content(course.id, user)
60 61 62

    exam_module = None
    for content in required_content:
63
        usage_key = UsageKey.from_string(content).map_into_course(course.id)
64 65 66 67 68
        module_item = modulestore().get_item(usage_key)
        if not module_item.hide_from_toc and module_item.is_entrance_exam:
            exam_module = module_item
            break
    return exam_module