Newer
Older
# -*- coding: utf-8 -*-
from __future__ import division
__copyright__ = "Copyright (C) 2014 Andreas Kloeckner"
__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
from django.shortcuts import ( # noqa
render, get_object_or_404, redirect)
from django.contrib import messages
from django.core.exceptions import (
PermissionDenied, SuspiciousOperation,
ObjectDoesNotExist)
from django.db import transaction
from django.utils.safestring import mark_safe
from course.views import get_now_or_fake_time
import re
from course.models import (
FlowSession, FlowPageData, FlowPageVisit, FlowPageVisitGrade,
flow_permission,
from course.utils import (
FlowContext, FlowPageContext,
instantiate_flow_page_with_ctx,
course_view, render_course_page)
# {{{ grade page visit
def grade_page_visit(visit, visit_grade_model=FlowPageVisitGrade,
grade_data=None, graded_at_git_commit_sha=None):
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
if not visit.is_graded_answer:
raise RuntimeError("cannot grade ungraded answer")
flow_session = visit.flow_session
course = flow_session.course
page_data = visit.page_data
from course.content import (
get_course_repo,
get_course_commit_sha,
get_flow_commit_sha,
get_flow_desc,
get_flow_page_desc,
instantiate_flow_page)
repo = get_course_repo(course)
course_commit_sha = get_course_commit_sha(
course, flow_session.participation)
flow_desc_pre = get_flow_desc(repo, course,
flow_session.flow_id, course_commit_sha)
flow_commit_sha = get_flow_commit_sha(
course, flow_session.participation, flow_desc_pre,
visit.flow_session)
flow_desc = get_flow_desc(repo, course,
flow_session.flow_id, flow_commit_sha)
page_desc = get_flow_page_desc(
flow_session.flow_id,
flow_desc,
page_data.group_id, page_data.page_id)
page = instantiate_flow_page(
location="flow '%s', group, '%s', page '%s'"
% (flow_session.flow_id, page_data.group_id, page_data.page_id),
repo=repo, page_desc=page_desc,
commit_sha=flow_commit_sha)
from course.page import PageContext
grading_page_context = PageContext(
course=course,
repo=repo,
commit_sha=flow_commit_sha)
answer_feedback = page.grade(
grading_page_context, visit.page_data.data,
visit.answer, grade_data=grade_data)
grade = visit_grade_model()
grade.visit = visit
grade.grade_data = grade_data
grade.max_points = page.max_points(visit.page_data)
grade.graded_at_git_commit_sha = graded_at_git_commit_sha
if answer_feedback is not None:
grade.correctness = answer_feedback.correctness
grade.feedback = answer_feedback.as_json()
grade.save()
# }}}
# {{{ finish flow
def get_flow_session_graded_answers_qset(flow_session):
from django.db.models import Q
qset = (FlowPageVisit.objects
.filter(flow_session=flow_session)
.filter(Q(answer__isnull=False) | Q(is_synthetic=True)))
if not flow_session.in_progress:
# Ungraded answers *can* show up in non-in-progress flows as a result
# of a race between a 'save' and the 'end session'. If this happens,
# we'll go ahead and ignore those.
qset = qset.filter(is_graded_answer=True)
return qset
def assemble_answer_visits(flow_session):
answer_visits = [None] * flow_session.page_count
answer_page_visits = (
get_flow_session_graded_answers_qset(flow_session)
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
.order_by("visit_time"))
for page_visit in answer_page_visits:
answer_visits[page_visit.page_data.ordinal] = page_visit
if not flow_session.in_progress:
# This is redundant with the answers being marked as
# final at the end of a flow, but that's OK.
#
# Note that this change is generally not persisted.
page_visit.is_graded_answer = True
return answer_visits
def count_answered(fctx, flow_session, answer_visits):
all_page_data = (FlowPageData.objects
.filter(flow_session=flow_session)
.order_by("ordinal"))
answered_count = 0
unanswered_count = 0
for i, page_data in enumerate(all_page_data):
assert i == page_data.ordinal
if answer_visits[i] is not None:
answer_data = answer_visits[i].answer
else:
answer_data = None
page = instantiate_flow_page_with_ctx(fctx, page_data)
if page.expects_answer():
if answer_data is None:
unanswered_count += 1
else:
answered_count += 1
return (answered_count, unanswered_count)
class GradeInfo(object):
"""
.. attribute:: points
The final grade, in points. May be *None* if the grade is not yet
final.
"""
def __init__(self,
points, provisional_points, max_points, max_reachable_points,
fully_correct_count, partially_correct_count, incorrect_count,
unknown_count):
self.points = points
self.provisional_points = provisional_points
self.max_points = max_points
self.max_reachable_points = max_reachable_points
self.fully_correct_count = fully_correct_count
self.partially_correct_count = partially_correct_count
self.incorrect_count = incorrect_count
Loading
Loading full blame...