Example #1
0
def add_or_edit_exercise(request, module_id, exercise_id=None):
    """ 
    This page can be used by teachers to add new exercises and edit existing ones.
    """
    module = get_object_or_404(CourseModule, id=module_id)
    course_instance = module.course_instance

    has_permission = course_instance.is_teacher(request.user.get_profile())

    if not has_permission:
        return HttpResponseForbidden(
            "You are not allowed to access this view.")

    if exercise_id != None:
        exercise = get_object_or_404(module.learning_objects,
                                     id=exercise_id).as_leaf_class()
    else:
        exercise = BaseExercise(course_module=module)

    if request.method == "POST":
        form = BaseExerciseForm(request.POST, instance=exercise)
        if form.is_valid():
            exercise = form.save()
            messages.success(request,
                             _('The exercise was saved successfully.'))
    else:
        form = BaseExerciseForm(instance=exercise)

    return render_to_response(
        "exercise/edit_exercise.html",
        CourseContext(request,
                      course_instance=course_instance,
                      exercise=exercise,
                      form=form))
Example #2
0
 def get_completed_percentage(self):
     max_points = BaseExercise.get_course_instance_max_points(
         self.course_instance)
     if max_points == 0:
         return 0
     else:
         return int(round(100.0 * self.get_total_points() / max_points))
Example #3
0
def fetch_exercise_metadata(request):
    exercise_url    = request.GET.get("exercise_url", None)
    metadata = {"success": False}
    
    validate        = URLValidator(verify_exists=True)
    
    try:
        validate(exercise_url)
        
        exercise            = BaseExercise(service_url=exercise_url)
        exercise_page       = exercise.get_page("")
        metadata["name"]    = exercise_page.meta["title"]
        metadata["description"] = exercise_page.meta["description"]
        metadata["success"] = True
    except ValidationError as e:
        metadata["message"] = " ".join(e.messages)
    except Exception as e:
        metadata["message"] = "No metadata found."
    
    return HttpResponse(simplejson.dumps(metadata), content_type="application/json")
Example #4
0
def fetch_exercise_metadata(request):
    exercise_url = request.GET.get("exercise_url", None)
    metadata = {"success": False}

    validate = URLValidator(verify_exists=True)

    try:
        validate(exercise_url)

        exercise = BaseExercise(service_url=exercise_url)
        exercise_page = exercise.get_page("")
        metadata["name"] = exercise_page.meta["title"]
        metadata["description"] = exercise_page.meta["description"]
        metadata["success"] = True
    except ValidationError as e:
        metadata["message"] = " ".join(e.messages)
    except Exception as e:
        metadata["message"] = "No metadata found."

    return HttpResponse(simplejson.dumps(metadata),
                        content_type="application/json")
Example #5
0
def view_my_page(request, course_url, instance_url):
    """ 
    Renders a personalized page for a student on the course. The page is intended to show 
    how well the student is doing on the course and shortcuts to the latest submissions.
    
    @param request: the Django HttpRequest object
    @param course_url: the url value of a Course object
    @param instance_url: the url value of a CourseInstance object 
    """
    
    course_instance = _get_course_instance(course_url, instance_url)

    if not course_instance.is_visible_to(request.user.get_profile()):
        return HttpResponseForbidden("You are not allowed "
                                     "to access this view.")

    course_summary  = UserCourseSummary(course_instance, request.user)
    submissions     = request.user.get_profile().submissions.filter(exercise__course_module__course_instance=course_instance).order_by("-id")

    course_instance_max_points = BaseExercise.get_course_instance_max_points(
        course_instance)

    unread_notifications = request.user.get_profile().received_notifications.filter(course_instance=course_instance, seen=False)
    older_notifications = request.user.get_profile().received_notifications.filter(course_instance=course_instance, seen=True)
    for notification in unread_notifications:
        notification.mark_as_seen()

    return render_to_response("course/view_my_page.html", 
                              CourseContext(request, 
                                            course_instance=course_instance,
                                            course_summary=course_summary,
                                            submissions=submissions,
                                            course_instance_max_points=
                                            course_instance_max_points,
                                            unread_notifications=unread_notifications,
                                            older_notifications=older_notifications,
                                            ))
Example #6
0
def view_instance(request, course_url, instance_url):
    """
    Renders the home page for a course instance showing the current student's
    progress on the course.

    On the page, all the exercises of the course instance are organized as a
    schedule. They are primarily organized in a list of course modules which
    are primarily ordered according to their closing times and secondarily to
    their opening times. Inside the course modules the exercises are ordered
    according to their order attribute but in the same time, they are also
    grouped to their categories.

    The home page also contains a summary of the student's progress for the
    whole course instance, course modules, categories and each exercise.
        
    @param request: the Django HttpRequest object
    @param course_url: the url value of a Course object
    @param instance_url: the url value of a CourseInstance object
    """
    
    course_instance = _get_course_instance(course_url, instance_url)
    user_profile = request.user.get_profile()

    if not course_instance.is_visible_to(user_profile):
        return HttpResponseForbidden("You are not allowed "
                                     "to access this view.")

    # In the following code, we are going to construct a special data structure
    # to be used in the view_instance.html. We refer to the structure as the
    # exercise_tree. The main idea is to provide all the data for the template
    # in a way that makes the template as simple as possible.
    #
    # This is how the structure should be used in the template:
    #
    #   {% for course_module, round_summary, uncategorized_exercise_level, category_level in exercise_tree %}
    #
    #       ...
    #
    #       {% for exercise, exercise_summary in uncategorized_exercise_level %}
    #           ...
    #       {% endfor %}
    #
    #       ...
    #
    #       {% for category, category_summary, categorized_exercise_level in category_level %}
    #
    #           ...
    #
    #           {% for exercise, exercise_summary in categorized_exercise_level %}
    #               ...
    #           {% endfor %}
    #
    #           ...
    #
    #       {% endfor %}
    #
    #       ...
    #
    #   {% endfor %}
    #
    # Notice that all the nodes of the tree are tuples (all the lists contain
    # tuples). The tuples are of course formatted the same way in a particular
    # tree level (list).
    #
    # The CourseModule objects are ordered chronologically. The exercises are
    # ordered by their order attribute. The order of the categories is
    # determined by the order of the exercises. For example, if the first two
    # exercises belong to category A and then the following two exercises
    # belong to category B, our list of categories will begin like [A, B, ...].
    # Note that the category A may be in the list later too if there is more
    # exercises that belong to the category A. For example, if the fifth
    # exercises would belong to category A, our list of categories would be
    # [A, B, A, ...].
    #
    # The view_instance.html template renders the CourseModules objects so that
    # in addition to the categorized exercise list, there is an additional
    # small list of hotlinks for the exercises. For this reason, the exercise
    # tree separates to two different list on the second level--the list
    # containing all the exercises of the CourseModule (for the hotlinks) and
    # the list containing the LearningObjectCategory objects. The latter of
    # these then has a third level which finally contains lists of exercises
    # (grouped by the LearningObjectCategory objects of their parent node that
    # is).
    #
    # Also notice the summaries in the tuples. Those alway correspond to the
    # main object of the tuple.
    #

    course_summary = UserCourseSummary(course_instance, request.user)

    visible_categories = LearningObjectCategory.objects.filter(
        course_instance=course_instance).exclude(hidden_to=user_profile)
    visible_exercises = (BaseExercise.objects.filter(
        course_module__course_instance=course_instance,
        category__in=visible_categories)
        .select_related("course_module", "category").order_by("order"))

    visible_exercises_by_course_modules = defaultdict(list)
    for exercise in visible_exercises:
        (visible_exercises_by_course_modules[exercise.course_module]
         .append(exercise))

    visible_exercises_by_course_modules = sorted(
        visible_exercises_by_course_modules.items(),
        key=lambda t: (t[0].closing_time, t[0].opening_time))

    exercise_tree = [
        (course_module,
         course_summary.get_exercise_round_summary(course_module),
         [(exercise, course_summary.get_exercise_summary(exercise))
          for exercise in exercises], [])
        for course_module, exercises in visible_exercises_by_course_modules]

    for course_module, round_summary, exercises_and_summaries,\
            exercise_tree_category_level in exercise_tree:
        for exercise, exercise_summary in exercises_and_summaries:
            if (len(exercise_tree_category_level) == 0
                    or exercise_tree_category_level[-1][0]
                    != exercise.category):
                exercise_tree_category_level.append(
                    (exercise.category,
                     course_summary.get_exercise_round_summary(
                         exercise.category),
                     []))
            exercise_tree_category_level[-1][2].append((exercise,
                                                        exercise_summary))

    # Finished constructing the exercise_tree.

    course_instance_max_points = BaseExercise.get_course_instance_max_points(
        course_instance)

    plugin_renderers = build_plugin_renderers(
        plugins=course_instance.plugins.all(),
        view_name="course_instance",
        user_profile=user_profile,
        course_instance=course_instance)
    
    return render_to_response("course/view_instance.html", 
                              CourseContext(request, 
                                            course_instance=course_instance, 
                                            course_summary=course_summary,
                                            plugin_renderers=plugin_renderers,
                                            exercise_tree=exercise_tree,
                                            course_instance_max_points=
                                            course_instance_max_points,
                                            ))
Example #7
0
def main():
    from django.contrib.auth.models import User
    from course.models import Course, CourseInstance, CourseModule
    from course.models import Enrollment, StudentGroup, LearningObjectCategory
    from exercise.exercise_models import BaseExercise
    from exercise.submission_models import Submission

    now = timezone.now()
    year_later = now + timedelta(days=365)

    user0 = User(id=500)
    user0.username = '******'
    user0.first_name = 'Perry'
    user0.last_name = 'Cash'
    user0.email = '*****@*****.**'
    user0.set_password('percash0')
    user0.save()

    user1 = User(id=501)
    user1.username = '******'
    user1.first_name = 'Zorita'
    user1.last_name = 'Alston'
    user1.email = '*****@*****.**'
    user1.set_password('zoralst1')
    user1.save()

    user2 = User(id=502)
    user2.username = '******'
    user2.first_name = 'Cameron'
    user2.last_name = 'Stein'
    user2.email = '*****@*****.**'
    user2.set_password('camstei2')
    user2.save()

    user3 = User(id=503)
    user3.username = '******'
    user3.first_name = 'Brynne'
    user3.last_name = 'Pollard'
    user3.email = '*****@*****.**'
    user3.set_password('brypoll3')
    user3.save()

    user4 = User(id=504)
    user4.username = '******'
    user4.first_name = 'Allistair'
    user4.last_name = 'Blackburn'
    user4.email = '*****@*****.**'
    user4.set_password('allblac4')
    user4.save()

    user5 = User(id=505)
    user5.username = '******'
    user5.first_name = 'Zachary'
    user5.last_name = 'Bolton'
    user5.email = '*****@*****.**'
    user5.set_password('zacbolt5')
    user5.save()

    user6 = User(id=506)
    user6.username = '******'
    user6.first_name = 'Kelsie'
    user6.last_name = 'Wolf'
    user6.email = '*****@*****.**'
    user6.set_password('kelwolf6')
    user6.save()

    user7 = User(id=507)
    user7.username = '******'
    user7.first_name = 'John'
    user7.last_name = 'McCarty'
    user7.email = '*****@*****.**'
    user7.set_password('johmcca7')
    user7.save()

    user8 = User(id=508)
    user8.username = '******'
    user8.first_name = 'Sheila'
    user8.last_name = 'Rodriquez'
    user8.email = '*****@*****.**'
    user8.set_password('sherodr8')
    user8.save()

    user9 = User(id=509)
    user9.username = '******'
    user9.first_name = 'Cassady'
    user9.last_name = 'Stanley'
    user9.email = '*****@*****.**'
    user9.set_password('casstan9')
    user9.save()

    course0 = Course()
    course0.name = 'Test Course'
    course0.url = 'test-course'
    course0.save()

    instance0 = CourseInstance(id=100, course=course0)
    instance0.instance_name = 'Test Instance'
    instance0.url = 'test-instance'
    instance0.starting_time = now
    instance0.ending_time = year_later
    instance0.save()

    Enrollment.objects.create(course_instance=instance0, user_profile=user0.userprofile)
    Enrollment.objects.create(course_instance=instance0, user_profile=user1.userprofile)
    Enrollment.objects.create(course_instance=instance0, user_profile=user2.userprofile)
    Enrollment.objects.create(course_instance=instance0, user_profile=user3.userprofile)
    Enrollment.objects.create(course_instance=instance0, user_profile=user4.userprofile)
    Enrollment.objects.create(course_instance=instance0, user_profile=user5.userprofile)
    Enrollment.objects.create(course_instance=instance0, user_profile=user6.userprofile)
    Enrollment.objects.create(course_instance=instance0, user_profile=user7.userprofile)
    Enrollment.objects.create(course_instance=instance0, user_profile=user8.userprofile)
    Enrollment.objects.create(course_instance=instance0, user_profile=user9.userprofile)

    group0 = StudentGroup.objects.create(id=200, course_instance=instance0)
    group0.members.add(user0.userprofile)
    group0.members.add(user1.userprofile)
    group0.save()

    group1 = StudentGroup.objects.create(id=201, course_instance=instance0)
    group1.members.add(user0.userprofile)
    group1.members.add(user2.userprofile)
    group1.members.add(user3.userprofile)
    group1.save()

    group2 = StudentGroup.objects.create(id=202, course_instance=instance0)
    group2.members.add(user1.userprofile)
    group2.members.add(user4.userprofile)
    group2.save()

    group3 = StudentGroup.objects.create(id=203, course_instance=instance0)
    group3.members.add(user5.userprofile)
    group3.members.add(user6.userprofile)
    group3.members.add(user7.userprofile)
    group3.members.add(user8.userprofile)
    group3.save()

    module0 = CourseModule(course_instance=instance0)
    module0.name = "First module"
    module0.url = "first-module"
    module0.opening_time = now
    module0.closing_time = year_later
    module0.save()

    module1 = CourseModule(course_instance=instance0)
    module1.name = "Second module"
    module1.url = "second-module"
    module1.opening_time = now
    module1.closing_time = year_later
    module1.save()

    category0 = LearningObjectCategory(course_instance=instance0)
    category0.name = "Some category"
    category0.save()

    exercise0 = BaseExercise(id=300, course_module=module0, category=category0)
    exercise0.name = "Easy exercise"
    exercise0.url = 'easy-exercise'
    exercise0.max_submissions = 10
    exercise0.max_group_size = 4
    exercise0.max_points = 100
    exercise0.points_to_pass = 50
    exercise0.save()

    exercise1 = BaseExercise(id=301, course_module=module0, category=category0)
    exercise1.name = "Hard exercise"
    exercise1.url = 'hard-exercise'
    exercise1.max_submissions = 5
    exercise0.max_group_size = 2
    exercise1.max_points = 100
    exercise1.points_to_pass = 100
    exercise1.save()

    exercise2 = BaseExercise(id=302, course_module=module1, category=category0)
    exercise2.name = "Nice exercise"
    exercise2.url = 'nice-exercise'
    exercise2.max_submissions = 0
    exercise2.max_points = 10
    exercise2.points_to_pass = 0
    exercise2.save()

    submission0 = Submission.objects.create(id=400, exercise=exercise0)
    submission0.submitters.add(user0.userprofile)
    submission0.submitters.add(user1.userprofile)
    submission0.feedback = '<html><body>Not bad.</body></html>'
    submission0.set_points(40, 100)
    submission0.set_ready()
    submission0.save()

    submission1 = Submission.objects.create(id=401, exercise=exercise0)
    submission1.submitters.add(user0.userprofile)
    submission1.submitters.add(user1.userprofile)
    submission1.feedback = '<html><body>Good.</body></html>'
    submission1.set_points(60, 100)
    submission1.set_ready()
    submission1.save()

    submission2 = Submission.objects.create(id=402, exercise=exercise0)
    submission2.submitters.add(user1.userprofile)
    submission2.submitters.add(user4.userprofile)
    submission2.feedback = '<html><body>Good.</body></html>'
    submission2.set_points(50, 100)
    submission2.set_ready()
    submission2.save()

    submission3 = Submission.objects.create(id=403, exercise=exercise2)
    submission3.submitters.add(user0.userprofile)
    submission3.feedback = '<html><body>Excellent.</body></html>'
    submission3.set_points(10, 10)
    submission3.set_ready()
    submission3.save()