예제 #1
0
def create_controller_query_service():
    """
    Return an instance of a service that can query edX ORA.
    """

    return ControllerQueryService(settings.OPEN_ENDED_GRADING_INTERFACE,
                                  system)
def combined_notifications(course, user):
    """
    Show notifications to a given user for a given course.  Get notifications from the cache if possible,
    or from the grading controller server if not.
    @param course: The course object for which we are getting notifications
    @param user: The user object for which we are getting notifications
    @return: A dictionary with boolean pending_grading (true if there is pending grading), img_path (for notification
    image), and response (actual response from grading controller server).
    """
    #Set up return values so that we can return them for error cases
    pending_grading = False
    img_path = ""
    notifications = {}
    notification_dict = {
        'pending_grading': pending_grading,
        'img_path': img_path,
        'response': notifications
    }

    #We don't want to show anonymous users anything.
    if not user.is_authenticated():
        return notification_dict

    #Define a mock modulesystem
    system = LmsModuleSystem(
        static_url="/static",
        track_function=None,
        get_module=None,
        render_template=render_to_string,
        replace_urls=None,
        descriptor_runtime=None,
        services={
            'i18n': ModuleI18nService(),
        },
    )
    #Initialize controller query service using our mock system
    controller_qs = ControllerQueryService(
        settings.OPEN_ENDED_GRADING_INTERFACE, system)
    student_id = unique_id_for_user(user)
    user_is_staff = has_access(user, course, 'staff')
    course_id = course.id
    notification_type = "combined"

    #See if we have a stored value in the cache
    success, notification_dict = get_value_from_cache(student_id, course_id,
                                                      notification_type)
    if success:
        return notification_dict

    #Get the time of the last login of the user
    last_login = user.last_login
    last_time_viewed = last_login - datetime.timedelta(
        seconds=(NOTIFICATION_CACHE_TIME + 60))

    try:
        #Get the notifications from the grading controller
        controller_response = controller_qs.check_combined_notifications(
            course.id, student_id, user_is_staff, last_time_viewed)
        notifications = json.loads(controller_response)
        if notifications.get('success'):
            if (notifications.get('staff_needs_to_grade')
                    or notifications.get('student_needs_to_peer_grade')):
                pending_grading = True
    except:
        #Non catastrophic error, so no real action
        #This is a dev_facing_error
        log.exception(
            u"Problem with getting notifications from controller query service for course {0} user {1}."
            .format(course_id, student_id))

    if pending_grading:
        img_path = "/static/images/grading_notification.png"

    notification_dict = {
        'pending_grading': pending_grading,
        'img_path': img_path,
        'response': notifications
    }

    #Store the notifications in the cache
    set_value_in_cache(student_id, course_id, notification_type,
                       notification_dict)

    return notification_dict
def combined_notifications(course, user):
    """
    Show notifications to a given user for a given course.  Get notifications from the cache if possible,
    or from the grading controller server if not.
    @param course: The course object for which we are getting notifications
    @param user: The user object for which we are getting notifications
    @return: A dictionary with boolean pending_grading (true if there is pending grading), img_path (for notification
    image), and response (actual response from grading controller server).
    """
    #Set up return values so that we can return them for error cases
    pending_grading = False
    img_path = ""
    notifications = {}
    notification_dict = {'pending_grading': pending_grading, 'img_path': img_path, 'response': notifications}

    #We don't want to show anonymous users anything.
    if not user.is_authenticated():
        return notification_dict

    #Initialize controller query service using our mock system
    controller_qs = ControllerQueryService(settings.OPEN_ENDED_GRADING_INTERFACE, render_to_string)
    student_id = unique_id_for_user(user)
    user_is_staff = has_access(user, 'staff', course)
    course_id = course.id
    notification_type = "combined"

    #See if we have a stored value in the cache
    success, notification_dict = get_value_from_cache(student_id, course_id, notification_type)
    if success:
        return notification_dict

    #Get the time of the last login of the user
    last_login = user.last_login
    last_time_viewed = last_login - datetime.timedelta(seconds=(NOTIFICATION_CACHE_TIME + 60))

    try:
        #Get the notifications from the grading controller
        notifications = controller_qs.check_combined_notifications(
            course.id,
            student_id,
            user_is_staff,
            last_time_viewed,
        )
        if notifications.get('success'):
            if (notifications.get('staff_needs_to_grade') or
                    notifications.get('student_needs_to_peer_grade')):
                pending_grading = True
    except:
        #Non catastrophic error, so no real action
        #This is a dev_facing_error
        log.exception(
            u"Problem with getting notifications from controller query service for course {0} user {1}.".format(
                course_id, student_id))

    if pending_grading:
        img_path = "/static/images/grading_notification.png"

    notification_dict = {'pending_grading': pending_grading, 'img_path': img_path, 'response': notifications}

    #Store the notifications in the cache
    set_value_in_cache(student_id, course_id, notification_type, notification_dict)

    return notification_dict
예제 #4
0
def combined_notifications(course, user):
    """
    Show notifications to a given user for a given course.  Get notifications from the cache if possible,
    or from the grading controller server if not.
    @param course: The course object for which we are getting notifications
    @param user: The user object for which we are getting notifications
    @return: A dictionary with boolean pending_grading (true if there is pending grading), img_path (for notification
    image), and response (actual response from grading controller server).
    """
    #Set up return values so that we can return them for error cases
    pending_grading = False
    img_path = ""
    notifications={}
    notification_dict = {'pending_grading': pending_grading, 'img_path': img_path, 'response': notifications}

    #We don't want to show anonymous users anything.
    if not user.is_authenticated():
        return notification_dict

    #Define a mock modulesystem
    system = ModuleSystem(
        ajax_url=None,
        track_function=None,
        get_module = None,
        render_template=render_to_string,
        replace_urls=None,
        xblock_model_data= {}
    )
    #Initialize controller query service using our mock system
    controller_qs = ControllerQueryService(settings.OPEN_ENDED_GRADING_INTERFACE, system)
    student_id = unique_id_for_user(user)
    user_is_staff = has_access(user, course, 'staff')
    course_id = course.id
    notification_type = "combined"

    #See if we have a stored value in the cache
    success, notification_dict = get_value_from_cache(student_id, course_id, notification_type)
    if success:
        return notification_dict

    #Get the time of the last login of the user
    last_login = user.last_login

    #Find the modules they have seen since they logged in
    last_module_seen = StudentModule.objects.filter(student=user, course_id=course_id,
                                                    modified__gt=last_login).values('modified').order_by(
        '-modified')
    last_module_seen_count = last_module_seen.count()

    if last_module_seen_count > 0:
        #The last time they viewed an updated notification (last module seen minus how long notifications are cached)
        last_time_viewed = last_module_seen[0]['modified'] - datetime.timedelta(seconds=(NOTIFICATION_CACHE_TIME + 60))
    else:
        #If they have not seen any modules since they logged in, then don't refresh
        return {'pending_grading': False, 'img_path': img_path, 'response': notifications}

    try:
        #Get the notifications from the grading controller
        controller_response = controller_qs.check_combined_notifications(course.id, student_id, user_is_staff,
                                                                         last_time_viewed)
        notifications = json.loads(controller_response)
        if notifications['success']:
            if notifications['overall_need_to_check']:
                pending_grading = True
    except:
        #Non catastrophic error, so no real action
        #This is a dev_facing_error
        log.exception(
            "Problem with getting notifications from controller query service for course {0} user {1}.".format(
                course_id, student_id))

    if pending_grading:
        img_path = "/static/images/grading_notification.png"

    notification_dict = {'pending_grading': pending_grading, 'img_path': img_path, 'response': notifications}

    #Store the notifications in the cache
    set_value_in_cache(student_id, course_id, notification_type, notification_dict)

    return notification_dict
def combined_notifications(course, user):
    """
    Show notifications to a given user for a given course.  Get notifications from the cache if possible,
    or from the grading controller server if not.
    @param course: The course object for which we are getting notifications
    @param user: The user object for which we are getting notifications
    @return: A dictionary with boolean pending_grading (true if there is pending grading), img_path (for notification
    image), and response (actual response from grading controller server).
    """
    # Set up return values so that we can return them for error cases
    pending_grading = False
    img_path = ""
    notifications = {}
    notification_dict = {"pending_grading": pending_grading, "img_path": img_path, "response": notifications}

    # We don't want to show anonymous users anything.
    if not user.is_authenticated():
        return notification_dict

    # Define a mock modulesystem
    system = ModuleSystem(
        ajax_url=None,
        track_function=None,
        get_module=None,
        render_template=render_to_string,
        replace_urls=None,
        xmodule_field_data=DictFieldData({}),
    )
    # Initialize controller query service using our mock system
    controller_qs = ControllerQueryService(settings.OPEN_ENDED_GRADING_INTERFACE, system)
    student_id = unique_id_for_user(user)
    user_is_staff = has_access(user, course, "staff")
    course_id = course.id
    notification_type = "combined"

    # See if we have a stored value in the cache
    success, notification_dict = get_value_from_cache(student_id, course_id, notification_type)
    if success:
        return notification_dict

    # Get the time of the last login of the user
    last_login = user.last_login
    last_time_viewed = last_login - datetime.timedelta(seconds=(NOTIFICATION_CACHE_TIME + 60))

    try:
        # Get the notifications from the grading controller
        controller_response = controller_qs.check_combined_notifications(
            course.id, student_id, user_is_staff, last_time_viewed
        )
        notifications = json.loads(controller_response)
        if notifications["success"]:
            if notifications["staff_needs_to_grade"] or notifications["student_needs_to_peer_grade"]:
                pending_grading = True
    except:
        # Non catastrophic error, so no real action
        # This is a dev_facing_error
        log.exception(
            "Problem with getting notifications from controller query service for course {0} user {1}.".format(
                course_id, student_id
            )
        )

    if pending_grading:
        img_path = "/static/images/grading_notification.png"

    notification_dict = {"pending_grading": pending_grading, "img_path": img_path, "response": notifications}

    # Store the notifications in the cache
    set_value_in_cache(student_id, course_id, notification_type, notification_dict)

    return notification_dict
예제 #6
0
def combined_notifications(course, user):
    """
    Show notifications to a given user for a given course.  Get notifications from the cache if possible,
    or from the grading controller server if not.
    @param course: The course object for which we are getting notifications
    @param user: The user object for which we are getting notifications
    @return: A dictionary with boolean pending_grading (true if there is pending grading), img_path (for notification
    image), and response (actual response from grading controller server).
    """
    # Set up return values so that we can return them for error cases
    pending_grading = False
    img_path = ""
    notifications = {}
    notification_dict = {
        'pending_grading': pending_grading,
        'img_path': img_path,
        'response': notifications
    }

    # We don't want to show anonymous users anything.
    if not user.is_authenticated():
        return notification_dict

    # Define a mock modulesystem
    system = ModuleSystem(ajax_url=None,
                          track_function=None,
                          get_module=None,
                          render_template=render_to_string,
                          replace_urls=None,
                          xblock_model_data={})
    # Initialize controller query service using our mock system
    controller_qs = ControllerQueryService(
        settings.OPEN_ENDED_GRADING_INTERFACE, system)
    student_id = unique_id_for_user(user)
    user_is_staff = has_access(user, course, 'staff')
    course_id = course.id
    notification_type = "combined"

    # See if we have a stored value in the cache
    success, notification_dict = get_value_from_cache(student_id, course_id,
                                                      notification_type)
    if success:
        return notification_dict

    # Get the time of the last login of the user
    last_login = user.last_login

    # Find the modules they have seen since they logged in
    last_module_seen = StudentModule.objects.filter(
        student=user, course_id=course_id,
        modified__gt=last_login).values('modified').order_by('-modified')
    last_module_seen_count = last_module_seen.count()

    if last_module_seen_count > 0:
        # The last time they viewed an updated notification (last module seen
        # minus how long notifications are cached)
        last_time_viewed = last_module_seen[0]['modified'] - datetime.timedelta(
            seconds=(NOTIFICATION_CACHE_TIME + 60))
    else:
        # If they have not seen any modules since they logged in, then don't
        # refresh
        return {
            'pending_grading': False,
            'img_path': img_path,
            'response': notifications
        }

    try:
        # Get the notifications from the grading controller
        controller_response = controller_qs.check_combined_notifications(
            course.id, student_id, user_is_staff, last_time_viewed)
        notifications = json.loads(controller_response)
        if notifications['success']:
            if notifications['overall_need_to_check']:
                pending_grading = True
    except:
        # Non catastrophic error, so no real action
        # This is a dev_facing_error
        log.exception(
            "Problem with getting notifications from controller query service for course {0} user {1}."
            .format(course_id, student_id))

    if pending_grading:
        img_path = "/static/images/grading_notification.png"

    notification_dict = {
        'pending_grading': pending_grading,
        'img_path': img_path,
        'response': notifications
    }

    # Store the notifications in the cache
    set_value_in_cache(student_id, course_id, notification_type,
                       notification_dict)

    return notification_dict
예제 #7
0
from xmodule.modulestore import search
from xmodule.modulestore.exceptions import ItemNotFoundError

from django.http import HttpResponse, Http404, HttpResponseRedirect
from mitxmako.shortcuts import render_to_string

log = logging.getLogger(__name__)

system = ModuleSystem(ajax_url=None,
                      track_function=None,
                      get_module=None,
                      render_template=render_to_string,
                      replace_urls=None,
                      xblock_field_data={})

controller_qs = ControllerQueryService(settings.OPEN_ENDED_GRADING_INTERFACE,
                                       system)
"""
Reverses the URL from the name and the course id, and then adds a trailing slash if
it does not exist yet

"""


def _reverse_with_slash(url_name, course_id):
    ajax_url = _reverse_without_slash(url_name, course_id)
    if not ajax_url.endswith('/'):
        ajax_url += '/'
    return ajax_url


def _reverse_without_slash(url_name, course_id):