-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathflow.py
2964 lines (2293 loc) · 102 KB
/
flow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
109
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- 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 import six
from django.utils.translation import (
ugettext, ugettext_lazy as _)
from django.contrib.auth.decorators import login_required
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
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 import forms
from django import http
from django.conf import settings
from django.urls import reverse
from crispy_forms.helper import FormHelper
from relate.utils import (
StyledForm, local_now, as_local_time,
format_datetime_local, string_concat)
from crispy_forms.layout import Submit
from django_select2.forms import Select2Widget
from course.constants import (
flow_permission,
participation_permission as pperm,
flow_session_expiration_mode,
FLOW_SESSION_EXPIRATION_MODE_CHOICES,
is_expiration_mode_allowed,
grade_aggregation_strategy,
GRADE_AGGREGATION_STRATEGY_CHOICES,
flow_session_interaction_kind
)
from course.models import (
Participation,
Course,
FlowSession, FlowPageData, FlowPageVisit,
FlowPageVisitGrade,
get_feedback_for_grade,
GradeChange, update_bulk_feedback)
from course.utils import (
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,
LanguageOverride,
)
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
# {{{ mypy
if False:
from typing import Any, Optional, Iterable, Sequence, Tuple, Text, List, FrozenSet # noqa
import datetime # noqa
from course.models import Course # noqa
from accounts.models import User # noqa
from course.utils import ( # noqa
CoursePageContext,
FlowSessionStartRule,
)
from course.content import ( # noqa
FlowDesc,
)
from course.page.base import ( # noqa
PageBase,
PageBehavior,
AnswerFeedback
)
from relate.utils import Repo_ish # noqa
# }}}
# {{{ 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)
def remove_page(fpd):
if fpd.page_ordinal is not None:
fpd.page_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: # pragma: no branch
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.initialize_page_data(pctx)
return FlowPageData(
flow_session=flow_session,
page_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.page_ordinal != ordinal[0]:
fpd.page_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,
page_ordinal__isnull=False)
.order_by("page_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,
page_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
"""
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()
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)
with get_course_repo(course) as repo:
course_commit_sha = get_course_commit_sha(
course, flow_session.participation if respect_preview else None)
flow_desc = get_flow_desc(repo, course,
flow_session.flow_id, course_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=course_commit_sha)
assert page.expects_answer()
if not page.is_answer_gradable():
return
from course.page import PageContext
grading_page_context = PageContext(
course=course,
repo=repo,
commit_sha=course_commit_sha,
flow_session=flow_session)
with LanguageOverride(course=course):
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)
# }}}
# {{{ start flow
def start_flow(
repo, # type: Repo_ish
course, # type: Course
participation, # type: Optional[Participation]
user, # type: Any
flow_id, # type: Text
flow_desc, # type: FlowDesc
session_start_rule, # type: FlowSessionStartRule
now_datetime, # type: datetime.datetime
):
# type: (...) -> FlowSession
# 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:
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
assert exp_mode in dict(FLOW_SESSION_EXPIRATION_MODE_CHOICES)
session = FlowSession(
course=course,
participation=participation,
user=user,
active_git_commit_sha=course_commit_sha.decode(),
flow_id=flow_id,
start_time=now_datetime,
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.
rules = getattr(flow_desc, "rules", None)
if rules is not None:
identifier = rules.grade_identifier
if identifier is not None:
from course.models import get_flow_grading_opportunity
get_flow_grading_opportunity(
course, flow_id, flow_desc,
identifier,
rules.grade_aggregation_strategy)
# 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):
# 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):
# type: (FlowSession) -> query.QuerySet
return get_multiple_flow_session_graded_answers_qset([flow_session])
def get_prev_answer_visits_qset(page_data):
# type: (FlowPageData) -> query.QuerySet
return (
get_flow_session_graded_answers_qset(page_data.flow_session)
.filter(page_data=page_data)
.order_by("-visit_time"))
def get_first_from_qset(qset):
# type: (query.QuerySet) -> Optional[Any]
for item in qset[:1]:
return item
return None
def get_prev_answer_visit(page_data):
return get_first_from_qset(get_prev_answer_visits_qset(page_data))
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]]]
# 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__page_ordinal",
"is_submitted_answer"))
for answer_visit in all_answer_visits:
fsess_idx = id_to_fsess_idx[answer_visit["flow_session_id"]]
page_ordinal = answer_visit["page_data__page_ordinal"]
if page_ordinal is not None:
answer_visit_ids[fsess_idx][page_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):
# 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.page_ordinal is not None:
answer_visits[page_visit.page_data.page_ordinal] = page_visit
if not flow_session.in_progress:
assert page_visit.is_submitted_answer is True
return answer_visits
def get_all_page_data(flow_session):
# type: (FlowSession) -> Iterable[FlowPageData]
return (FlowPageData.objects
.filter(
flow_session=flow_session,
page_ordinal__isnull=False)
.order_by("page_ordinal"))
def get_interaction_kind(
fctx, # type: FlowContext
flow_session, # type: FlowSession
flow_generates_grade, # type: bool
all_page_data, # type: Iterable[FlowPageData]
):
# type: (...) -> Text
ikind = flow_session_interaction_kind.noninteractive
for i, page_data in enumerate(all_page_data):
assert i == page_data.page_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:
return flow_session_interaction_kind.ungraded
return ikind
def get_session_answered_page_data(
fctx, # type: FlowContext
flow_session, # type: FlowSession
answer_visits # type: List[Optional[FlowPageVisit]]
):
# type: (...) -> Tuple[List[FlowPageData], List[FlowPageData], bool]
all_page_data = get_all_page_data(flow_session)
answered_page_data_list = [] # type: List[FlowPageData]
unanswered_page_data_list = [] # type: List[FlowPageData]
is_interactive_flow = False # type: bool
for i, page_data in enumerate(all_page_data):
assert i == page_data.page_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():
is_interactive_flow = True
if not page.is_optional_page:
if answer_data is None:
unanswered_page_data_list.append(page_data)
else:
answered_page_data_list.append(page_data)
return (answered_page_data_list, unanswered_page_data_list, is_interactive_flow)
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.
"""
def __init__(
self,
points, # type: Optional[float]
provisional_points, # type: Optional[float]
max_points, # type: Optional[float]
max_reachable_points, # type: Optional[float]
fully_correct_count, # type: int
partially_correct_count, # type: int
incorrect_count, # type: int
unknown_count, # type: int
optional_fully_correct_count=0, # type: int
optional_partially_correct_count=0, # type: int
optional_incorrect_count=0, # type: int
optional_unknown_count=0, # 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
self.optional_fully_correct_count = optional_fully_correct_count
self.optional_partially_correct_count = optional_partially_correct_count
self.optional_incorrect_count = optional_incorrect_count
self.optional_unknown_count = optional_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()
def optional_total_count(self):
return (self.optional_fully_correct_count
+ self.optional_partially_correct_count
+ self.optional_incorrect_count
+ self.optional_unknown_count)
def optional_fully_correct_percent(self):
"""Only to be used for visualization purposes."""
return self.FULL_PERCENT * self.optional_fully_correct_count\
/ self.optional_total_count()
def optional_partially_correct_percent(self):
"""Only to be used for visualization purposes."""
return self.FULL_PERCENT * self.optional_partially_correct_count\
/ self.optional_total_count()
def optional_incorrect_percent(self):
"""Only to be used for visualization purposes."""
return self.FULL_PERCENT * self.optional_incorrect_count\
/ self.optional_total_count()
def optional_unknown_percent(self):
"""Only to be used for visualization purposes."""
return self.FULL_PERCENT * self.optional_unknown_count\
/ self.optional_total_count()
# }}}
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
optional_fully_correct_count = 0
optional_partially_correct_count = 0
optional_incorrect_count = 0
optional_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.page_ordinal
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
grade = av.get_most_recent_grade()
assert grade is not None
feedback = get_feedback_for_grade(grade)
if page.is_optional_page:
if feedback is None or feedback.correctness is None:
optional_unknown_count += 1
continue
else:
page_points = grade.max_points*feedback.correctness
if points is not None:
points += page_points
if feedback.correctness == 1:
optional_fully_correct_count += 1
elif feedback.correctness == 0:
optional_incorrect_count += 1
else:
optional_partially_correct_count += 1
else:
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)
if points is not None:
points = min(
points, grading_rule.max_points_enforced_cap)
assert provisional_points is not None
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,
optional_fully_correct_count=optional_fully_correct_count,
optional_partially_correct_count=optional_partially_correct_count,
optional_incorrect_count=optional_incorrect_count,
optional_unknown_count=optional_unknown_count)
@transaction.atomic
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(page_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
assert 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)
@retry_transaction_decorator()
def finish_flow_session(fctx, flow_session, grading_rule,
force_regrade=False, now_datetime=None, respect_preview=True):
"""
: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)
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
# }}}