forked from jeapostrophe/grade-samurai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rkt
1762 lines (1640 loc) · 60.9 KB
/
app.rkt
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
#lang racket/base
(require racket/contract
unstable/debug
web-server/servlet-env
unstable/contract
web-server/http
web-server/http/bindings
web-server/dispatchers/dispatch
web-server/dispatch
web-server/servlet/web
web-server/formlets
racket/file
racket/list
racket/path
racket/match
racket/date
racket/runtime-path
racket/string
racket/function
file/md5
(only-in srfi/13 string-trim-both)
"model.rkt"
"../m8b/id-cookie.rkt")
(define DEBUG? #f)
(define (format-% v)
(format "~a%" (real->decimal-string (* 100 v) 2)))
(define (string->lines s)
(string-split s "\n"))
(define (contains-greater-than-80-char-line? file-content)
(for/or ([l (in-list (string->lines (bytes->string/utf-8 file-content)))])
((string-length l) . > . 80)))
(module+ test
(require rackunit)
(check-equal? (contains-greater-than-80-char-line? #"
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklm
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklm
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklm
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklm
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklm
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklm
") #t)
(check-equal? (contains-greater-than-80-char-line? #"
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklm
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklm
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklm
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklm
") #f))
(define (make-parent-directory* p)
(define parent (path-only p))
(make-directory* parent))
(define (letter-grade ng)
(cond
[(> ng 0.93) "A"]
[(> ng 0.90) "A-"]
[(> ng 0.86) "B+"]
[(> ng 0.83) "B"]
[(> ng 0.80) "B-"]
[(> ng 0.76) "C+"]
[(> ng 0.73) "C"]
[(> ng 0.70) "C-"]
[(> ng 0.66) "D+"]
[(> ng 0.63) "D"]
[(> ng 0.60) "D-"]
[else "F"]))
(define (path->last-part f)
(define-values (base name must-be-dir?)
(split-path f))
(path->string name))
(define (directory-list* pth)
(if (directory-exists? pth)
(sort (map path->last-part (directory-list pth))
string-ci<=?)
empty))
(define-runtime-path source-dir ".")
(define (samurai-go!
#:db db-path
#:port port
#:assignments pre-assignments
#:authenticate authenticate-users
#:username-request-text login-formlet-un-text
#:password-request-text login-formlet-pw-text)
(define (display-to-file* v pth)
(GRADE-CACHE-CLEAR!)
(make-parent-directory* pth)
(display-to-file v pth #:exists 'replace))
(define (write-to-file* v pth)
(GRADE-CACHE-CLEAR!)
(make-parent-directory* pth)
(write-to-file v pth #:exists 'replace))
(define assignments
(sort pre-assignments
<=
#:key assignment-due-secs))
(define secret-salt-path (build-path db-path "secret-salt"))
(define (id->assignment a-id)
(findf (λ (a) (string=? a-id (assignment-id a))) assignments))
(define secret-salt
(begin
(unless (file-exists? secret-salt-path)
(display-to-file*
(list->bytes (build-list 128 (λ (i) (random 256))))
secret-salt-path))
(file->bytes secret-salt-path)))
(define (is-admin?)
(eq? 'admin (current-user-type)))
(define (page/root req)
(send/back
(if (is-admin?)
(redirect-to (main-url page/admin/students))
(redirect-to (main-url page/main)))))
(define (page/login req [last-error #f])
(define login-formlet
(formlet
(table
(tr (td ,login-formlet-un-text)
(td ,{(to-string (required (text-input))) . => . username}))
(tr (td ,login-formlet-pw-text)
(td ,{(to-string (required (password-input))) . => . password})))
(values username password)))
(define log-req
(send/suspend
(λ (k-url)
(template
#:breadcrumb (list (cons "Home" (main-url page/root))
(cons "Login" #f))
`(div ([id "login"])
(form ([action ,k-url] [method "post"])
,@(formlet-display login-formlet)
(input ([type "submit"] [value "Log in"])))
,@(if last-error
`((h1 ([class "error"]) ,last-error))
'()))))))
(define-values (username password)
(formlet-process login-formlet log-req))
(define authenticated?
(authenticate-users username password))
(cond
[authenticated?
(redirect-to (parameterize ([current-user username]
[current-user-type authenticated?])
(main-url page/root))
#:headers
(list (cookie->header
(make-id-cookie secret-salt
(format "~a:~a"
authenticated?
username)))))]
[else (page/login
req
(format "Invalid password for user (~S)" username))]))
(define (default-text-input default-string)
(to-string (default (string->bytes/utf-8 default-string)
(text-input #:value (string->bytes/utf-8 default-string)))))
(define (page/account req)
(define existing-info
(student-info (current-user)))
(define account-formlet
(formlet
(div ([id "form-inputs"])
(table
(tr (td "Legal First Name: ")
(td ,{(default-text-input (student-firstname existing-info))
. => . first-name}))
(tr (td "Last Name: ")
(td ,{(default-text-input (student-lastname existing-info))
. => . last-name}))
(tr (td "I Prefer to be Known As: ")
(td ,{(default-text-input (student-nickname existing-info))
. => . nick-name}))
(tr (td "Email Address: ")
(td ,{(default-text-input (student-email existing-info))
. => . email}))
(tr (td "Picture I can be recognized by: ")
(td ,{(file-upload) . => . photo}))))
(values first-name last-name nick-name email photo)))
(define account-form
(send/suspend
(λ (k-url)
(template
#:breadcrumb (list (cons "Home" (main-url page/main))
(cons (current-user) #f)
(cons "Details" #f))
`(div
([id "account"])
(form ([action ,k-url]
[method "post"]
[enctype "multipart/form-data"])
,@(formlet-display account-formlet)
(p "Instead of this: "
(img
([src ,(main-url page/student/photo (current-user))]
[height "160"])))
(input ([type "submit"] [value "Update Info"]))))))))
(define-values (first-name last-name nick-name email photo)
(formlet-process account-formlet account-form))
(write-to-file* (student nick-name first-name last-name email)
(user-info-path))
(when (binding:file? photo)
(display-to-file* (binding:file-content photo)
(user-image-path)))
(redirect-to (main-url page/root)))
(define-values (main-dispatch main-url main-applies?)
(dispatch-rules+applies
[("")
page/root]
[("main")
page/main]
[("admin" "students")
page/admin/students]
[("admin" "assignments")
page/admin/assignments]
[("admin" "assignments" (string-arg))
page/admin/assignments/view]
[("admin" "grade-next")
page/admin/grade-next]
[("login")
page/login]
[("logout")
page/logout]
[("account")
page/account]
[("student" (string-arg) "photo.jpg")
page/student/photo]
[("assignment" (string-arg) "files")
page/assignment/files]
[("assignment" (string-arg) "files" "delete" (string-arg))
page/assignment/files/delete]
[("assignment" (string-arg) "self" "edit")
page/assignment/self/edit]
[("assignment" (string-arg) "self")
page/assignment/self]
[("assignment" (string-arg) "peer" "edit")
page/assignment/peer/edit]
[("assignment" (string-arg) "peer")
page/assignment/peer]))
(define default-peer
"The Spanish Inquisition")
(define (assignment-peer id)
(if (file-exists? (assignment-peer-path id))
(file->string (assignment-peer-path id))
default-peer))
(define (assignment-co-peer id)
(or (for/or ([u (in-list (users))])
(and (equal? (current-user)
(parameterize ([current-user u])
(assignment-peer id)))
u))
default-peer))
(define (users-path)
(build-path db-path "users"))
(define (users)
(directory-list* (users-path)))
(define (sorted-users)
(sort (users)
string-ci<=?
#:key (compose student-lastname student-info)))
(define (user-path)
(build-path (users-path) (current-user)))
(define (user-info-path)
(build-path (user-path) "info.rktd"))
(define (user-image-path)
(build-path (user-path) "photo.jpg"))
(define (assignment-path id)
(build-path (user-path) "assignments" id))
(define (assignment-file-path id)
(build-path (assignment-path id) "files"))
(define (assignment-files id)
(directory-list* (assignment-file-path id)))
(define (assignment-peer-path id)
(build-path (assignment-path id) "peer"))
(define (assignment-question-student-grade-path id i)
(build-path (assignment-path id) "self-eval" (number->string i)))
(define (assignment-question-student-grade-path/peer id i)
(build-path (assignment-path id) "peer-eval" (number->string i)))
(define (assignment-question-prof-grade-path id i)
(build-path (assignment-path id) "prof-eval" (number->string i)))
(define ((make-assignment-question-student-grade
assignment-question-student-grade-path)
id i)
(define p (assignment-question-student-grade-path id i))
(and (file-exists? p) (file->value p)))
(define assignment-question-student-grade
(make-assignment-question-student-grade
assignment-question-student-grade-path))
(define assignment-question-student-grade/peer
(make-assignment-question-student-grade
assignment-question-student-grade-path/peer))
(define assignment-question-prof-grade
(make-assignment-question-student-grade
assignment-question-prof-grade-path))
(define (assignment-question-peer-grade id i)
(define co-peer (assignment-co-peer id))
(parameterize ([current-user co-peer])
(assignment-question-student-grade/peer id i)))
(define (assignment-question-peer-grade-path id i)
(define co-peer (assignment-co-peer id))
(parameterize ([current-user co-peer])
(assignment-question-student-grade-path/peer id i)))
;; XXX CLEANUP this
(define (assignment-question-student-bool-grade id i)
(define v (assignment-question-student-grade id i))
(if v
(answer:bool-value v)
'n/a))
(define (assignment-question-student-bool-grade/peer id i)
(define v (assignment-question-student-grade/peer id i))
(if v
(answer:bool-value v)
'n/a))
(define (assignment-question-student-numeric-grade/peer id i)
(define v (assignment-question-student-grade/peer id i))
(if v
(answer:numeric-value v)
#f))
(define (assignment-question-prof-bool-grade id i)
(define v (assignment-question-prof-grade id i))
(if v
(answer:bool-value v)
'n/a))
(define (assignment-question-prof-bool-grade/peer id i)
(define peer (assignment-peer id))
(parameterize ([current-user peer])
(assignment-question-prof-bool-grade id i)))
(define (assignment-question-prof-numeric-grade id i)
(define v (assignment-question-prof-grade id i))
(if v
(answer:numeric-value v)
#f))
(define (assignment-question-prof-numeric-grade/peer id i)
(define peer (assignment-peer id))
(parameterize ([current-user peer])
(assignment-question-prof-numeric-grade id i)))
(define (is-optional-enabled?)
(for/and ([a (in-list assignments)]
#:when (zero? (assignment-normal-weight a))
#:when (zero? (assignment-optional-weight a)))
(define a-id (assignment-id a))
(or
;; It is before the self assessment date
(< (current-seconds) (assignment-eval-secs a))
;; It is after and...
(and
;; they've evaluated themselves...
(self-eval-completed? a)
;; and so have I...
(prof-eval-completed? a)
;; and I said they did do it
(assignment-question-prof-bool-grade a-id 0)))))
(define (compute-question-grade optional? default-grade id i q)
(match-define (question nw ow _ t) q)
(define ow-p (if optional? ow 0))
(define ps
(match t
['numeric
(or (assignment-question-prof-numeric-grade id i)
default-grade)]
['bool
(define student-correct?
(assignment-question-student-bool-grade id i))
(define prof-correct?
(assignment-question-prof-bool-grade id i))
(match* (student-correct? prof-correct?)
[( #t #t) 10/10]
[( #t #f) -1/10]
[( #f #t) 1/10]
[( #f #f) 0/10]
[('n/a _) default-grade]
[( _ 'n/a) default-grade])]))
(* (+ nw ow-p) ps))
(define (compute-peer-grade optional? default-grade id i q)
(match-define (question nw ow _ t) q)
(define ow-p (if optional? ow 0))
(define ps
(match t
['numeric
(define prof
(assignment-question-prof-numeric-grade/peer id i))
(define student
(assignment-question-student-numeric-grade/peer id i))
(if (and prof student)
(- 1 (abs (- prof student)))
default-grade)]
['bool
(define prof (assignment-question-prof-bool-grade/peer id i))
(define student (assignment-question-student-bool-grade/peer id i))
(cond
[(or (eq? 'n/a prof)
(eq? 'n/a student))
default-grade]
[(equal? prof student)
1]
[else
0])]))
(* (+ nw ow-p) ps))
(define ((make-compute-question-grades compute-question-grade)
optional? default-grade id qs)
(for/sum
([q (in-list qs)]
[i (in-naturals)])
(compute-question-grade optional? default-grade id i q)))
(define compute-question-grades
(make-compute-question-grades compute-question-grade))
(define compute-peer-grades
(make-compute-question-grades compute-peer-grade))
(define (compute-assignment-grade* a default-grade)
(match-define (assignment nw ow id ds es ps qs) a)
(define optional-enable?
(is-optional-enabled?))
(define ow-p
(if optional-enable? ow 0))
(define self-pts
(compute-question-grades
optional-enable? default-grade
id qs))
(define peer-pts
(compute-peer-grades
optional-enable? default-grade
id qs))
(define pre-score
(if (number? ps)
(* (+ ow-p nw)
(+ (* 9/10 self-pts)
(* 1/10 peer-pts)))
(* (+ ow-p nw) self-pts)))
(if (zero? nw)
(max 0 pre-score)
pre-score))
(define (compute-assignment-grade^ a default-grade)
(define optional-enable?
(is-optional-enabled?))
(define base
(compute-assignment-grade*
a
(if (< (current-seconds) (assignment-due-secs a))
default-grade
0)))
(if optional-enable?
base
(min 1 base)))
(define (compute-assignment-grade a default-grade)
(GRADE-CACHE-USE
(list a default-grade)
(λ () (compute-assignment-grade^ a default-grade))))
(define (compute-assignment-grade/id a-id default-grade)
(define a (id->assignment a-id))
(compute-assignment-grade a default-grade))
(define (compute-grade* default-grade)
(for/sum ([a (in-list assignments)])
(compute-assignment-grade a default-grade)))
(define (maximum-grade-so-far)
(define now (current-seconds))
(for/sum ([a (in-list assignments)])
(match-define (assignment nw ow id ds es ps qs) a)
(if (< ds now)
nw
0)))
(define GRADE-CACHE (make-hash))
(define GRADE-CACHE-T
(thread
(λ ()
(let loop ()
(define now (current-seconds))
(define all-due
(append-map (λ (a)
(list (assignment-due-secs a)
(assignment-eval-secs a)
(assignment-peer-secs a)))
assignments))
(define still-due
(filter (λ (t)
(and t
(< now t)))
all-due))
(define sorted-due
(sort still-due <))
(match sorted-due
[(list)
(void)]
[(list* some-due _)
(sleep (- some-due now))
(for ([k (in-hash-keys GRADE-CACHE)])
(hash-remove! GRADE-CACHE k))
(loop)])))))
(define (GRADE-CACHE-CLEAR!)
(hash-remove! GRADE-CACHE (current-user)))
(define (GRADE-CACHE-USE path t)
(define user-ht
(hash-ref! GRADE-CACHE (current-user)
(λ () (make-hash))))
(hash-ref! user-ht path t))
(define (compute-grade dg)
(GRADE-CACHE-USE
(list #f dg)
(λ () (compute-grade* dg))))
(define (assignment-file-display a-id)
(define-values (html end-line-number)
(for/fold ([html empty]
[line-offset 1])
([file (in-list (assignment-files a-id))])
(define-values (table new-offset)
(file->html-table
a-id
(build-path (assignment-file-path a-id) file)
line-offset))
(values (append html (list table)) new-offset)))
`(div ([class "files"])
,@html))
(define (side-by-side-render a-id rhs #:sticky? [sticky? #f])
`(div ([class ,(format "side-by-side~a"
(if sticky?
" sticky"
""))])
(div ([class "left"]) (div ([class "side-by-side-inner"])
,(assignment-file-display a-id)))
(div ([class "right"]) (div ([class "side-by-side-inner"])
,@rhs))))
(define (format-grade default-grade)
(show-grade
(compute-grade default-grade)))
(define (show-grade g)
(define l (letter-grade g))
`(span ([class ,(substring l 0 1)])
,(format "~a (~a)"
(format-% g)
l)))
(define (page/assignment/self/edit req a-id)
(define assignment (id->assignment a-id))
(define the-breadcrumb
(list (cons "Home" (main-url page/main))
(cons "Assignments" #f)
(cons a-id #f)
(cons "Self Evaluation" #f)
(cons "Edit" #f)))
(define (overdue-or thunk)
(if (<= (assignment-eval-secs assignment) (current-seconds))
(send/back
(template
#:breadcrumb the-breadcrumb
"Self evaluation past due."))
(thunk)))
(overdue-or
(λ ()
(for ([question (assignment-questions assignment)]
[i (in-naturals)])
(unless
(file-exists? (assignment-question-student-grade-path a-id i))
(define answer
(grade-question
(current-user) a-id question i
#:breadcrumb the-breadcrumb
#:last? #t
#:their? #f))
(overdue-or
(λ ()
(write-to-file*
answer
(assignment-question-student-grade-path a-id i))))))
;; XXX redirect to self eval view (and in the other place too)
(template
#:breadcrumb the-breadcrumb
"Self evaluation completed."))))
(define (string->linked-html s)
(define positions
(regexp-match-positions* #px"[l|L]\\d+" s))
(define-values (html pos)
(for/fold ([html empty] [pos 0])
([pos-pair positions])
(values
(append
html
(list
(substring s pos (car pos-pair))
`(a ([class "line-link"]
[href
,(format "#LC~a"
(substring
(string-upcase
(substring s
(car pos-pair)
(cdr pos-pair)))
1))])
,(substring s (car pos-pair) (cdr pos-pair)))))
(cdr pos-pair))))
`(p ([class "comment"])
,@html ,(substring s pos (string-length s))))
(define (format-answer which ans
#:delete? [delete? #f])
(cond
[ans
`(div ([class ,(format "answer ~a" which)])
(p ,(format "~a evaluation is: ~a"
which
(match ans
[(answer:bool _ _ completed?)
(if completed?
"Yes"
"No")]
[(answer:numeric _ _ value)
(format-% value)]))
,(if delete?
`(span " "
(a ([href ,delete?])
"(rescind)"))
""))
,(string->linked-html (answer-comments ans)))]
[else
`(div ([class ,(format "answer incomplete ~a" which)])
(p ,(format "~a evaluation is not completed." which)))]))
(define (page/assignment/generalized/html embed/url a-id #:peer [peer #f])
(define assignment (id->assignment a-id))
(parameterize ([current-user (or peer (current-user))])
(define optional-enable? (is-optional-enabled?))
(side-by-side-render
a-id
(for/list ([q (in-list (assignment-questions assignment))]
[i (in-naturals)])
(define (delete-file-url pth)
(define (okay?)
(and
(< (current-seconds)
(if peer
(assignment-peer-secs assignment)
(assignment-eval-secs assignment)))
(file-exists? pth)
(not
(file-exists?
(assignment-question-prof-grade-path a-id i)))))
(and
(okay?)
(embed/url
(λ (req)
(and
(okay?)
(delete-file pth)
(redirect-to
(if peer
(main-url page/assignment/peer/edit a-id)
(main-url page/assignment/self/edit a-id))))))))
(define different?
(answer-different? (assignment-question-prof-grade a-id i)
(if peer
(assignment-question-peer-grade a-id i)
(assignment-question-student-grade a-id i))))
(match-define (question nw ow prompt type) q)
(define ow-p (if optional-enable? ow 0))
`(div ([class ,(format "answers~a"
(if different?
" diff"
""))])
(p (span ([class "weight"])
,(format-% (+ nw ow-p)))
,prompt
,(format " (~a)" i))
,(format-answer
#:delete? (and (not peer)
(delete-file-url (assignment-question-student-grade-path a-id i)))
(if peer "Peer's Self" "Self")
(assignment-question-student-grade a-id i))
,(format-answer
(if peer "Peer's Professor" "Professor")
(assignment-question-prof-grade a-id i))
,(format-answer
#:delete? (and peer (delete-file-url (assignment-question-peer-grade-path a-id i)))
(if peer "Your" "Peer's")
(assignment-question-peer-grade a-id i))
(a ([href ,(format "mailto:~a?subject=~a&body=~a"
"jay.mccarthy@gmail.com"
(format
"330 - Dispute - ~a - ~a (~a) - ~a"
a-id
prompt
i
(if peer
"Peer"
"Self"))
"You jerk! I disagree with your evaluation of this question! Here's why:\n\n[FILL IN YOUR OBJECTION HERE]")])
"Dispute!"))))))
(define (answer-different? x y)
(match* (x y)
[((answer:bool _ _ x) (answer:bool _ _ y))
(not (equal? x y))]
[((answer:numeric _ _ x) (answer:numeric _ _ y))
(not (equal? x y))]
[(#f _)
#f]
[(_ #f)
#f]
[(_ _)
#t]))
(define (page/assignment/self req a-id)
(define assignment (id->assignment a-id))
(send/suspend/dispatch
(λ (embed/url)
(template
#:breadcrumb
(list (cons "Home" (main-url page/main))
(cons "Assignments" #f)
(cons a-id #f)
(cons "Self Evaluation" #f))
(page/assignment/generalized/html embed/url a-id)))))
(define (page/assignment/peer req a-id)
(define assignment (id->assignment a-id))
(define peer (assignment-peer a-id))
(define the-breadcrumb
(list (cons "Home" (main-url page/main))
(cons "Assignments" #f)
(cons a-id #f)
(cons "Peer Evaluation" #f)))
(cond
[(equal? default-peer peer)
(template
#:breadcrumb the-breadcrumb
`(div ([class "notice"]) "Your peer has not been assigned."))]
[(not (peer-eval-completed? assignment))
(template
#:breadcrumb the-breadcrumb
`(div ([class "notice"]) "Your peer eval is incomplete."))]
[else
(send/suspend/dispatch
(λ (embed/url)
(template
#:breadcrumb the-breadcrumb
(page/assignment/generalized/html embed/url a-id #:peer peer))))]))
(define (page/assignment/peer/edit req a-id)
(define assignment (id->assignment a-id))
(define the-breadcrumb
(list (cons "Home" (main-url page/main))
(cons "Assignments" #f)
(cons a-id #f)
(cons "Peer Evaluation" #f)
(cons "Edit" #f)))
(define (pick-a-person a-id)
(define student-ids (users))
(define finished-self-eval
(remove (current-user)
(filter (λ (student-id)
(parameterize ([current-user student-id])
(self-eval-completed? assignment)))
student-ids)))
(define already-assigned
(map (λ (student-id)
(parameterize ([current-user student-id])
(assignment-peer a-id)))
student-ids))
(define candidates
(remove* already-assigned
finished-self-eval))
(define the-peer
(match (shuffle candidates)
[(list* peer _)
peer]
[(list)
(match (shuffle finished-self-eval)
[(list* peer _)
peer]
[(list)
(send/back
(template
#:breadcrumb the-breadcrumb
"Peer evaluation is impossible, as no peers are available."))])]))
(display-to-file* the-peer (assignment-peer-path a-id))
the-peer)
(define peer-id
(if (file-exists? (assignment-peer-path a-id))
(assignment-peer a-id)
(pick-a-person a-id)))
(define (overdue-or thunk)
(if (<= (assignment-peer-secs assignment) (current-seconds))
(send/back
(template
#:breadcrumb the-breadcrumb
"Peer evaluation past due."))
(thunk)))
(overdue-or
(λ ()
(for ([question (assignment-questions assignment)]
[i (in-naturals)])
(when
(and
;; I have not yet graded
(not
(file-exists?
(assignment-question-student-grade-path/peer a-id i)))
;; They have grade
(file-exists?
(parameterize ([current-user peer-id])
(assignment-question-student-grade-path a-id i))))
(define grade
(grade-question
peer-id a-id question i
#:peer? #t
#:breadcrumb
(list (cons "Home" (main-url page/root))
(cons "Assignments" #f)
(cons a-id #f)
(cons "Peer Evaluation" #f)
(cons "Edit" #f))))
(overdue-or
(λ ()
(write-to-file*
grade
(assignment-question-student-grade-path/peer a-id i))))))
(template
#:breadcrumb the-breadcrumb
"Peer evaluation completed, or not available (as peer has not finished their grading.)"))))
(define (grade-question stu a-id question i
#:breadcrumb bc
#:their? [their? #t]
#:last? [last? #f]
#:peer? [peer? #f]
#:extra [extra empty])
(define last-i
(and last?
(for/or ([j (in-range i -1 -1)])
(define g
(parameterize ([current-user stu])
(assignment-question-student-grade a-id j)))
(and g
(not (string=? "" (answer-comments g)))
j))))
(define last
(and last-i
(parameterize ([current-user stu])
(assignment-question-student-grade a-id last-i))))
(match (parameterize ([current-user stu])
(assignment-question-student-grade a-id i))
[(answer:bool _ _ #f)
(answer:bool (current-seconds) "" #f)]
[_
(define boolean-formlet
(formlet
(p ,{(radio-group '(#t #f)
#:checked? (λ (x) x)
#:display (λ (x) (if x "Yes" "No")))
. => . credit})
credit))
(define request
(send/suspend
(λ (k-url)
(template
#:breadcrumb bc
(parameterize ([current-user stu])
(side-by-side-render
#:sticky? #t
a-id
(append
extra
(list
(if last
(format-answer
"Last answer"
(parameterize ([current-user stu])
(assignment-question-student-grade a-id last-i)))
"")
`(p ,(question-prompt question)
,(format " (~a)" i))
(if their?
(format-answer
"Their"
(parameterize ([current-user stu])
(assignment-question-student-grade a-id i)))
"")
(if peer?
(format-answer
"Peer"
(parameterize ([current-user stu])
(assignment-question-peer-grade a-id i)))
"")
(if their?
`(p "What do you think they earned?")
"")
`(form ([action ,k-url] [method "post"])
,(match (question-type question)
['bool
`(p (button ([name "submit"]
[type "submit"]
[value "bool_Y"])
"Yes")
(button ([name "submit"]
[type "submit"]
[value "bool_N"])
"No"))]
['numeric
`(p (button ([name "submit"]
[type "submit"]
[value "num_0"])
"0")
(button ([name "submit"]
[type "submit"]
[value "num_0.5"])
"0.5")
(button ([name "submit"]
[type "submit"]
[value "num_1"])
"1")
(input ([name "numeric"]
[type "text"]
[value "[0, 1]"]))
(button ([name "submit"]
[type "submit"]
[value "num_input"])
"Other"))])
(p "Provide evidence to justify that score.")
(textarea ([name "comments"]
[rows "8"]
[cols "60"])
,(if last
(answer-comments last)
""))
(p "(If you need to refer to line numbers, prefix a number with L. For example, use L32 or l32 to refer to line 32)"))))))))))
(define bs
(request-bindings/raw request))
(define comment
(bytes->string/utf-8
(binding:form-value
(bindings-assq #"comments" bs))))
(define score
(match
(binding:form-value