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.utils.translation import (
ugettext, ugettext_lazy as _, string_concat)
from django.utils.functional import lazy
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
Andreas Klöckner
committed
from django.db.models import query # noqa
from django.utils.safestring import mark_safe
mark_safe_lazy = lazy(mark_safe, six.text_type)
from django.utils import translation
from django.conf import settings
from django.urls import reverse
from relate.utils import (
StyledForm, local_now, as_local_time,
format_datetime_local)
from django_select2.forms import Select2Widget
from course.constants import (
flow_permission,
Andreas Klöckner
committed
participation_permission as pperm,
FLOW_SESSION_EXPIRATION_MODE_CHOICES,
is_expiration_mode_allowed,
grade_aggregation_strategy,
GRADE_AGGREGATION_STRATEGY_CHOICES,
flow_session_interaction_kind
FlowSession, FlowPageData, FlowPageVisit,
FlowPageVisitGrade,
get_feedback_for_grade,
GradeChange, update_bulk_feedback)
from course.utils import (
Andreas Klöckner
committed
FlowContext,
FlowPageContext,
PageOrdinalOutOfRange,
instantiate_flow_page_with_ctx,
course_view, render_course_page,
get_session_start_rule,
get_session_access_rule,
get_session_grading_rule,
FlowSessionGradingRule,
)
from course.exam import get_login_exam_ticket
from course.page import InvalidPageData
from course.views import get_now_or_fake_time
from relate.utils import retry_transaction_decorator
Andreas Klöckner
committed
# {{{ mypy
from typing import Any, Optional, Iterable, Tuple, Text # noqa
Andreas Klöckner
committed
import datetime # noqa
from course.models import ( # noqa
Course,
Participation
)
from course.utils import ( # noqa
CoursePageContext,
FlowSessionStartRule,
Andreas Klöckner
committed
)
from course.content import ( # noqa
FlowDesc,
)
Andreas Klöckner
committed
PageBase,
PageBehavior,
Andreas Klöckner
committed
)
Andreas Klöckner
committed
# }}}
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# {{{ page data wrangling
@retry_transaction_decorator(serializable=True)
def _adjust_flow_session_page_data_inner(repo, flow_session,
course_identifier, flow_desc, commit_sha):
from course.page.base import PageContext
pctx = PageContext(
course=flow_session.course,
repo=repo,
commit_sha=commit_sha,
flow_session=flow_session,
in_sandbox=False,
page_uri=None)
from course.models import FlowPageData
def remove_page(fpd):
if fpd.ordinal is not None:
fpd.ordinal = None
fpd.save()
desc_group_ids = []
ordinal = [0]
for grp in flow_desc.groups:
desc_group_ids.append(grp.id)
shuffle = getattr(grp, "shuffle", False)
max_page_count = getattr(grp, "max_page_count", None)
available_page_ids = [page_desc.id for page_desc in grp.pages]
if max_page_count is None:
max_page_count = len(available_page_ids)
group_pages = []
# {{{ helper functions
def find_page_desc(page_id):
new_page_desc = None
for page_desc in grp.pages:
if page_desc.id == page_id:
new_page_desc = page_desc
break
assert new_page_desc is not None
return new_page_desc
def instantiate_page(page_desc):
from course.content import instantiate_flow_page
return instantiate_flow_page(
"course '%s', flow '%s', page '%s/%s'"
% (course_identifier, flow_session.flow_id,
grp.id, page_desc.id),
repo, page_desc, commit_sha)
def create_fpd(new_page_desc):
page = instantiate_page(new_page_desc)
data = page.make_page_data()
return FlowPageData(
flow_session=flow_session,
ordinal=None,
page_type=new_page_desc.type,
group_id=grp.id,
page_id=new_page_desc.id,
data=data,
title=page.title(pctx, data))
def add_page(fpd):
if fpd.ordinal != ordinal[0]:
fpd.ordinal = ordinal[0]
fpd.save()
page_desc = find_page_desc(fpd.page_id)
page = instantiate_page(page_desc)
title = page.title(pctx, fpd.data)
if fpd.title != title:
fpd.title = title
fpd.save()
ordinal[0] += 1
available_page_ids.remove(fpd.page_id)
group_pages.append(fpd)
# }}}
if shuffle:
# maintain order of existing pages as much as possible
for fpd in (FlowPageData.objects
.filter(
flow_session=flow_session,
group_id=grp.id,
ordinal__isnull=False)
.order_by("ordinal")):
if (fpd.page_id in available_page_ids
and len(group_pages) < max_page_count):
add_page(fpd)
else:
remove_page(fpd)
assert len(group_pages) <= max_page_count
from random import choice
# then add randomly chosen new pages
while len(group_pages) < max_page_count and available_page_ids:
new_page_id = choice(available_page_ids)
new_page_fpds = (FlowPageData.objects
.filter(
flow_session=flow_session,
group_id=grp.id,
page_id=new_page_id))
if new_page_fpds.count():
# We already have FlowPageData for this page, revive it
new_page_fpd, = new_page_fpds
assert new_page_fpd.page_id == new_page_id
else:
# Make a new FlowPageData instance
page_desc = find_page_desc(new_page_id)
assert page_desc.id == new_page_id
new_page_fpd = create_fpd(page_desc)
assert new_page_fpd.page_id == new_page_id
add_page(new_page_fpd)
else:
# reorder pages to order in flow
id_to_fpd = dict(
((fpd.group_id, fpd.page_id), fpd)
for fpd in FlowPageData.objects.filter(
flow_session=flow_session,
group_id=grp.id))
for page_desc in grp.pages:
key = (grp.id, page_desc.id)
if key in id_to_fpd:
fpd = id_to_fpd.pop(key)
else:
fpd = create_fpd(page_desc)
if len(group_pages) < max_page_count:
add_page(fpd)
for fpd in id_to_fpd.values():
remove_page(fpd)
# {{{ remove pages orphaned because of group renames
for fpd in (
FlowPageData.objects
.filter(
flow_session=flow_session,
ordinal__isnull=False)
.exclude(group_id__in=desc_group_ids)
):
remove_page(fpd)
# }}}
return ordinal[0] # new page count
def adjust_flow_session_page_data(repo, flow_session,
course_identifier, flow_desc=None, respect_preview=True):
# type: (Repo_ish, FlowSession, Text, Optional[FlowDesc], bool) -> None
Andreas Klöckner
committed
Andreas Klöckner
committed
"""
The caller may *not* be in a transaction that has a weaker isolation
level than *serializable*.
"""
from course.content import get_course_commit_sha, get_flow_desc
commit_sha = get_course_commit_sha(
flow_session.course,
flow_session.participation if respect_preview else None)
revision_key = "2:"+commit_sha.decode()
Andreas Klöckner
committed
if flow_desc is None:
flow_desc = get_flow_desc(repo, flow_session.course,
flow_session.flow_id, commit_sha)
if flow_session.page_data_at_revision_key == revision_key:
return
new_page_count = _adjust_flow_session_page_data_inner(
repo, flow_session, course_identifier, flow_desc,
commit_sha)
# These are idempotent, so they don't need to be guarded by a seqcst
# transaction.
flow_session.page_count = new_page_count
flow_session.page_data_at_revision_key = revision_key
flow_session.save()
# }}}
# {{{ grade page visit
def grade_page_visit(visit, visit_grade_model=FlowPageVisitGrade,
grade_data=None, respect_preview=True):
# type: (FlowPageVisit, type, Any, bool) -> None
if not visit.is_submitted_answer:
raise RuntimeError(_("cannot grade ungraded answer"))
flow_session = visit.flow_session
course = flow_session.course
page_data = visit.page_data
most_recent_grade = visit.get_most_recent_grade() # type: Optional[FlowPageVisitGrade] # noqa
if most_recent_grade is not None and grade_data is None:
grade_data = most_recent_grade.grade_data
from course.content import (
get_course_repo,
get_course_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 if respect_preview else None)
flow_desc = get_flow_desc(repo, course,
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,
assert page.expects_answer()
if not page.is_answer_gradable():
return
from course.page import PageContext
grading_page_context = PageContext(
course=course,
repo=repo,
flow_session=flow_session)
with translation.override(settings.RELATE_ADMIN_EMAIL_LOCALE):
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 = course_commit_sha.decode()
bulk_feedback_json = None
if answer_feedback is not None:
grade.correctness = answer_feedback.correctness
grade.feedback, bulk_feedback_json = answer_feedback.as_json()
grade.save()
update_bulk_feedback(page_data, grade, bulk_feedback_json)
# }}}
Andreas Klöckner
committed
def start_flow(
repo, # type: Repo_ish
course, # type: Course
participation, # type: Optional[Participation]
Andreas Klöckner
committed
user, # type: Any
flow_id, # type: Text
flow_desc, # type: FlowDesc
session_start_rule, # type: FlowSessionStartRule
Andreas Klöckner
committed
now_datetime, # type: datetime.datetime
):
# type: (...) -> FlowSession
Andreas Klöckner
committed
# This function does not need to be transactionally atomic.
# The only essential part is the creation of the session.
# The remainder of the function (opportunity creation and
# page setup) is atomic and gets retried.
from course.content import get_course_commit_sha
course_commit_sha = get_course_commit_sha(course, participation)
if participation is not None:
Andreas Klöckner
committed
assert participation.user == user
exp_mode = flow_session_expiration_mode.end
if session_start_rule.default_expiration_mode is not None:
exp_mode = session_start_rule.default_expiration_mode
session = FlowSession(
course=course,
participation=participation,
Andreas Klöckner
committed
user=user,
active_git_commit_sha=course_commit_sha.decode(),
flow_id=flow_id,
in_progress=True,
expiration_mode=exp_mode,
access_rules_tag=session_start_rule.tag_session)
session.save()
# Create flow grading opportunity. This makes the flow
# show up in the grade book.
Andreas Klöckner
committed
rules = getattr(flow_desc, "rules", None)
if rules is not None:
identifier = rules.grade_identifier
if identifier is not None:
Andreas Klöckner
committed
from course.models import get_flow_grading_opportunity
get_flow_grading_opportunity(
course, flow_id, flow_desc,
# will implicitly modify and save the session if there are changes
adjust_flow_session_page_data(repo, session,
course.identifier, flow_desc, respect_preview=True)
return session
# }}}
# {{{ finish flow
def get_multiple_flow_session_graded_answers_qset(flow_sessions):
Andreas Klöckner
committed
# type: (List[FlowSession]) -> query.QuerySet
from django.db.models import Q
qset = (FlowPageVisit.objects
.filter(flow_session__in=flow_sessions)
.filter(Q(answer__isnull=False) | Q(is_synthetic=True))
.order_by("flow_session__id"))
# 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(
(Q(flow_session__in_progress=False) & Q(is_submitted_answer=True))
| Q(flow_session__in_progress=True))
return qset
def get_flow_session_graded_answers_qset(flow_session):
Andreas Klöckner
committed
# type: (FlowSession) -> query.QuerySet
return get_multiple_flow_session_graded_answers_qset([flow_session])
def get_prev_answer_visits_qset(page_data):
Andreas Klöckner
committed
# type: (FlowPageData) -> query.QuerySet
get_flow_session_graded_answers_qset(page_data.flow_session)
.filter(page_data=page_data)
.order_by("-visit_time"))
def get_prev_answer_visit(page_data):
previous_answer_visits = get_prev_answer_visits_qset(page_data)
for prev_visit in previous_answer_visits[:1]:
return prev_visit
return None
def assemble_page_grades(flow_sessions):
# type: (List[FlowSession]) -> List[List[Optional[FlowPageVisitGrade]]]
Given a list of flow sessions, return a list of lists of FlowPageVisitGrade
objects corresponding to the most recent page grades for each page of the
flow session. If a page is not graded, the corresponding entry is None.
Note that, even if the flow sessions belong to the same flow, the length
of the lists may vary since the flow page count may vary per session.
"""
id_to_fsess_idx = {fsess.id: i for i, fsess in enumerate(flow_sessions)}
answer_visit_ids = [
[None] * fsess.page_count for fsess in flow_sessions
] # type: List[List[Optional[int]]]
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
# Get all answer visits corresponding to the sessions. The query result is
# typically very large.
all_answer_visits = (
get_multiple_flow_session_graded_answers_qset(flow_sessions)
.order_by("visit_time")
.values("id", "flow_session_id", "page_data__ordinal",
"is_submitted_answer"))
for answer_visit in all_answer_visits:
fsess_idx = id_to_fsess_idx[answer_visit["flow_session_id"]]
ordinal = answer_visit["page_data__ordinal"]
if ordinal is not None:
answer_visit_ids[fsess_idx][ordinal] = answer_visit["id"]
if not flow_sessions[fsess_idx].in_progress:
assert answer_visit["is_submitted_answer"] is True
flat_answer_visit_ids = []
for visit_id_list in answer_visit_ids:
for visit_id in visit_id_list:
if visit_id is not None:
flat_answer_visit_ids.append(visit_id)
# Get all grade visits associated with the answer visits.
grades = (FlowPageVisitGrade.objects
.filter(visit__in=flat_answer_visit_ids)
.order_by("visit__id")
.order_by("grade_time"))
grades_by_answer_visit = {}
for grade in grades:
grades_by_answer_visit[grade.visit_id] = grade
def get_grades_for_visit_group(visit_group):
# type: (List[Optional[int]]) -> List[Optional[FlowPageVisit]]
return [grades_by_answer_visit.get(visit_id)
for visit_id in visit_group]
return [get_grades_for_visit_group(group) for group in answer_visit_ids]
def assemble_answer_visits(flow_session):
Andreas Klöckner
committed
# type: (FlowSession) -> List[Optional[FlowPageVisit]]
answer_visits = [None] * flow_session.page_count # type: List[Optional[FlowPageVisit]] # noqa
answer_page_visits = (
get_flow_session_graded_answers_qset(flow_session)
.order_by("visit_time"))
for page_visit in answer_page_visits:
if page_visit.page_data.ordinal is not None:
answer_visits[page_visit.page_data.ordinal] = page_visit
if not flow_session.in_progress:
assert page_visit.is_submitted_answer is True
return answer_visits
Andreas Klöckner
committed
# type: (FlowSession) -> Iterable[FlowPageData]
.filter(
flow_session=flow_session,
ordinal__isnull=False)
.order_by("ordinal"))
Andreas Klöckner
committed
def get_interaction_kind(
fctx, # type: FlowContext
flow_session, # type: FlowSession
flow_generates_grade, # type: bool
all_page_data, # type: Iterable[FlowPageData]
):
# type: (...) -> Text
if not flow_session.in_progress:
return flow_session_interaction_kind.noninteractive
ikind = flow_session_interaction_kind.noninteractive
for i, page_data in enumerate(all_page_data):
assert i == page_data.ordinal
page = instantiate_flow_page_with_ctx(fctx, page_data)
if page.expects_answer():
if page.is_answer_gradable():
if flow_generates_grade:
return flow_session_interaction_kind.permanent_grade
else:
return flow_session_interaction_kind.practice_grade
else:
ikind = flow_session_interaction_kind.ungraded
return ikind
def get_session_answered_page_data(
fctx, # type: FlowContext
flow_session, # type: FlowSession
):
# type: (...) -> Tuple[List[FlowPageData], List[FlowPageData]]
all_page_data = get_all_page_data(flow_session)
answered_page_data_list = [] # type: List[FlowPageData]
unanswered_page_data_list = [] # type: List[FlowPageData]
for i, page_data in enumerate(all_page_data):
assert i == page_data.ordinal
avisit = answer_visits[i]
if avisit is not None:
answer_data = avisit.answer
else:
answer_data = None
page = instantiate_flow_page_with_ctx(fctx, page_data)
if page.expects_answer():
if answer_data is None:
unanswered_page_data_list.append(page_data)
answered_page_data_list.append(page_data)
return (answered_page_data_list, unanswered_page_data_list)
class GradeInfo(object):
"""An object to hold a tally of points and page counts of various types in a flow.
.. attribute:: points
The final grade, in points. May be *None* if the grade is not yet
final.
"""
Andreas Klöckner
committed
def __init__(
self,
points, # type: Optional[float]
provisional_points, # type: Optional[float]
max_points, # type: Optional[float]
max_reachable_points, # type: Optional[float]
Andreas Klöckner
committed
fully_correct_count, # type: int
partially_correct_count, # type: int
incorrect_count, # type: int
unknown_count, # type: int
):
# type: (...) -> None
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
self.unknown_count = unknown_count
# Rounding to larger than 100% will break the percent bars on the
# flow results page.
FULL_PERCENT = 99.99
# {{{ point percentages
def points_percent(self):
"""Only to be used for visualization purposes."""
if self.max_points is None or self.max_points == 0:
if self.points == 0:
return 100
else:
return 0
else:
return self.FULL_PERCENT*self.provisional_points/self.max_points
def missed_points_percent(self):
"""Only to be used for visualization purposes."""
return (self.FULL_PERCENT
- self.points_percent()
- self.unreachable_points_percent())
def unreachable_points_percent(self):
"""Only to be used for visualization purposes."""
if (self.max_points is None
or self.max_reachable_points is None
or self.max_points == 0):
return 0
else:
return self.FULL_PERCENT*(
self.max_points - self.max_reachable_points)/self.max_points
def total_points_percent(self):
return (
self.points_percent()
+ self.missed_points_percent()
+ self.unreachable_points_percent())
# }}}
# {{{ page counts
def total_count(self):
return (self.fully_correct_count
+ self.partially_correct_count
+ self.incorrect_count
+ self.unknown_count)
def fully_correct_percent(self):
"""Only to be used for visualization purposes."""
return self.FULL_PERCENT*self.fully_correct_count/self.total_count()
def partially_correct_percent(self):
"""Only to be used for visualization purposes."""
return self.FULL_PERCENT*self.partially_correct_count/self.total_count()
def incorrect_percent(self):
"""Only to be used for visualization purposes."""
return self.FULL_PERCENT*self.incorrect_count/self.total_count()
def unknown_percent(self):
"""Only to be used for visualization purposes."""
return self.FULL_PERCENT*self.unknown_count/self.total_count()
Andreas Klöckner
committed
def gather_grade_info(
fctx, # type: FlowContext
flow_session, # type: FlowSession
grading_rule, # type: FlowSessionGradingRule
answer_visits, # type: List[Optional[FlowPageVisit]]
):
# type: (...) -> GradeInfo
"""
:returns: a :class:`GradeInfo`
"""
all_page_data = get_all_page_data(flow_session)
bonus_points = grading_rule.bonus_points
points = bonus_points
provisional_points = bonus_points
max_points = bonus_points
max_reachable_points = bonus_points
fully_correct_count = 0
partially_correct_count = 0
incorrect_count = 0
unknown_count = 0
for i, page_data in enumerate(all_page_data):
page = instantiate_flow_page_with_ctx(fctx, page_data)
assert i == page_data.ordinal
Andreas Klöckner
committed
av = answer_visits[i]
if av is None:
# This is true in principle, but early code to deal with survey questions
# didn't generate synthetic answer visits for survey questions, so this
# can't actually be enforced.
# assert not page.expects_answer()
continue
if not page.is_answer_gradable():
continue
Andreas Klöckner
committed
grade = av.get_most_recent_grade()
assert grade is not None
feedback = get_feedback_for_grade(grade)
max_points += grade.max_points
if feedback is None or feedback.correctness is None:
unknown_count += 1
points = None
continue
max_reachable_points += grade.max_points
page_points = grade.max_points*feedback.correctness
if points is not None:
points += page_points
provisional_points += page_points
if grade.max_points > 0:
if feedback.correctness == 1:
fully_correct_count += 1
elif feedback.correctness == 0:
incorrect_count += 1
else:
partially_correct_count += 1
# {{{ adjust max_points if requested
if grading_rule.max_points is not None:
max_points = grading_rule.max_points
# }}}
# {{{ enforce points cap
if grading_rule.max_points_enforced_cap is not None:
max_reachable_points = min(
max_reachable_points, grading_rule.max_points_enforced_cap)
points = min(
points, grading_rule.max_points_enforced_cap)
provisional_points = min(
provisional_points, grading_rule.max_points_enforced_cap)
return GradeInfo(
points=points,
provisional_points=provisional_points,
max_points=max_points,
max_reachable_points=max_reachable_points,
fully_correct_count=fully_correct_count,
partially_correct_count=partially_correct_count,
incorrect_count=incorrect_count,
unknown_count=unknown_count)
def grade_page_visits(
fctx, # type: FlowContext
flow_session, # type: FlowSession
answer_visits, # type: List[Optional[FlowPageVisit]]
force_regrade=False, # type: bool
respect_preview=True, # type: bool
):
# type: (...) -> None
for i in range(len(answer_visits)):
answer_visit = answer_visits[i]
if answer_visit is not None:
answer_visit.is_submitted_answer = True
answer_visit.save()
else:
page_data = flow_session.page_data.get(ordinal=i)
page = instantiate_flow_page_with_ctx(fctx, page_data)
if not page.expects_answer():
continue
# Create a synthetic visit to attach a grade
new_answer_visit = FlowPageVisit()
new_answer_visit.flow_session = flow_session
new_answer_visit.page_data = page_data
new_answer_visit.is_synthetic = True
new_answer_visit.answer = None
new_answer_visit.is_submitted_answer = True
new_answer_visit.save()
answer_visits[i] = answer_visit = new_answer_visit
if not page.is_answer_gradable():
continue
if answer_visit is not None:
if not answer_visit.grades.count() or force_regrade: # type: ignore
grade_page_visit(answer_visit, respect_preview=respect_preview)
Andreas Klöckner
committed
@retry_transaction_decorator()
def finish_flow_session(fctx, flow_session, grading_rule,
force_regrade=False, now_datetime=None, respect_preview=True):
Andreas Klöckner
committed
"""
:returns: :class:`GradeInfo`
"""
# Do not be tempted to call adjust_flow_session_page_data in here.
# This function may be called from within a transaction.
if not flow_session.in_progress:
raise RuntimeError(_("Can't end a session that's already ended"))
assert isinstance(grading_rule, FlowSessionGradingRule)
Andreas Klöckner
committed
if now_datetime is None:
from django.utils.timezone import now
now_datetime = now()
answer_visits = assemble_answer_visits(flow_session)
grade_page_visits(fctx, flow_session, answer_visits,
force_regrade=force_regrade,
respect_preview=respect_preview)
# ORDERING RESTRICTION: Must grade pages before gathering grade info
# {{{ determine completion time
completion_time = now_datetime
if grading_rule.use_last_activity_as_completion_time:
last_activity = flow_session.last_activity()
if last_activity is not None:
completion_time = last_activity
flow_session.completion_time = completion_time
# }}}
flow_session.save()
return grade_flow_session(fctx, flow_session, grading_rule,
Andreas Klöckner
committed
def expire_flow_session(
fctx, # type: FlowContext
flow_session, # type: FlowSession
grading_rule, # type: FlowSessionGradingRule
now_datetime, # type: datetime.datetime
past_due_only=False, # type:bool
):
# type: (...) -> bool
Andreas Klöckner
committed
# This function does not need to be transactionally atomic.
# It only does one atomic 'thing' in each execution path.
if not flow_session.in_progress:
raise RuntimeError(_("Can't expire a session that's not in progress"))
if flow_session.participation is None:
raise RuntimeError(_("Can't expire an anonymous flow session"))
assert isinstance(grading_rule, FlowSessionGradingRule)
if (past_due_only
and grading_rule.due is not None
and now_datetime < grading_rule.due):
return False
Andreas Klöckner
committed
adjust_flow_session_page_data(fctx.repo, flow_session,
flow_session.course.identifier, fctx.flow_desc,
respect_preview=False)
Andreas Klöckner
committed
if flow_session.expiration_mode == flow_session_expiration_mode.roll_over:
session_start_rule = get_session_start_rule(
flow_session.course, flow_session.participation,
flow_session.flow_id, fctx.flow_desc, now_datetime,
for_rollover=True)
if not session_start_rule.may_start_new_session:
# No new session allowed: finish.
return finish_flow_session(fctx, flow_session, grading_rule,
now_datetime=now_datetime, respect_preview=False)
Andreas Klöckner
committed
else:
Andreas Klöckner
committed
flow_session.access_rules_tag = session_start_rule.tag_session
Andreas Klöckner
committed
# {{{ FIXME: This is weird and should probably not exist.
access_rule = get_session_access_rule(
flow_session, fctx.flow_desc, now_datetime)
if hasattr(session_start_rule, "default_expiration_mode"):
flow_session.expiration_mode = \
session_start_rule.default_expiration_mode
elif not is_expiration_mode_allowed(
Andreas Klöckner
committed
flow_session.expiration_mode, access_rule.permissions):
flow_session.expiration_mode = flow_session_expiration_mode.end
Andreas Klöckner
committed
# }}}
Andreas Klöckner
committed
flow_session.save()
Andreas Klöckner
committed
return True
elif flow_session.expiration_mode == flow_session_expiration_mode.end:
return finish_flow_session(fctx, flow_session, grading_rule,
now_datetime=now_datetime, respect_preview=False)