-
-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathProblem.pm
More file actions
1505 lines (1288 loc) · 51.5 KB
/
Problem.pm
File metadata and controls
1505 lines (1288 loc) · 51.5 KB
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
package WeBWorK::ContentGenerator::Problem;
use Mojo::Base 'WeBWorK::ContentGenerator', -signatures, -async_await;
=head1 NAME
WeBWorK::ContentGenerator::Problem - Allow a student to interact with a problem.
=cut
use WeBWorK::Debug;
use WeBWorK::Utils qw(decodeAnswers wwRound);
use WeBWorK::Utils::DateTime qw(before between after);
use WeBWorK::Utils::Files qw(path_is_subdir);
use WeBWorK::Utils::JITAR qw(seq_to_jitar_id jitar_id_to_seq is_jitar_problem_hidden is_jitar_problem_closed
jitar_problem_finished jitar_problem_adjusted_status);
use WeBWorK::Utils::LanguageAndDirection qw(get_problem_lang_and_dir);
use WeBWorK::Utils::ProblemProcessing qw(process_and_log_answer jitar_send_warning_email compute_reduced_score
compute_unreduced_score);
use WeBWorK::Utils::Rendering qw(getTranslatorDebuggingOptions renderPG);
use WeBWorK::Utils::Sets qw(is_restricted format_set_name_display);
use WeBWorK::AchievementEvaluator qw(checkForAchievements);
use WeBWorK::DB::Utils qw(global2user fake_set fake_problem);
use WeBWorK::Localize;
use WeBWorK::AchievementEvaluator;
use WeBWorK::HTML::SingleProblemGrader;
use WeBWorK::HTML::StudentNav qw(studentNav);
# GET/POST Parameters for this module
#
# Standard params:
#
# user - user ID of real user
# effectiveUser - user ID of effective user
#
# Integration with PGProblemEditor:
#
# editMode - if set, indicates alternate problem source location.
# can be "temporaryFile" or "savedFile".
#
# sourceFilePath - path to file to be edited
# problemSeed - force problem seed to value
# success - success message to display
# failure - failure message to display
#
# Rendering options:
#
# displayMode - name of display mode to use
#
# showOldAnswers - request that last entered answer be shown (if allowed)
# showCorrectAnswers - request that correct answers be shown (if allowed)
# showHints - request that hints be shown (if allowed)
# showSolutions - request that solutions be shown (if allowed)
#
# Problem interaction:
#
# AnSwEr# - answer blanks in problem
#
# redisplay - name of the "Redisplay Problem" button
# submitAnswers - name of "Submit Answers" button
# checkAnswers - name of the "Check Answers" button
# showMeAnother - name of the "Show me another" button
# previewAnswers - name of the "Preview Answers" button
# "can" methods
# Subroutines to determine if a user "can" perform an action. Each subroutine is
# called with the following arguments:
# ($c, $user, $effectiveUser, $set, $problem)
# In addition can_recordAnswers and can_showMeAnother have the argument
# $submitAnswers that is used to distinguish between this submission and the
# next.
sub can_showOldAnswers ($c, $user, $effectiveUser, $set, $problem) {
return $c->authz->hasPermissions($user->user_id, 'can_show_old_answers');
}
sub can_showCorrectAnswers ($c, $user, $effectiveUser, $set, $problem) {
return after($set->answer_date, $c->submitTime)
|| $c->authz->hasPermissions($user->user_id, 'show_correct_answers_before_answer_date');
}
sub can_showProblemGrader ($c, $user, $effectiveUser, $set, $problem) {
my $authz = $c->authz;
return ($authz->hasPermissions($user->user_id, 'access_instructor_tools')
&& $authz->hasPermissions($user->user_id, 'problem_grader')
&& $set->set_id ne 'Undefined_Set'
&& !$c->{invalidSet});
}
sub can_showAnsGroupInfo ($c, $user, $effectiveUser, $set, $problem) {
return $c->authz->hasPermissions($user->user_id, 'show_answer_group_info');
}
sub can_showAnsHashInfo ($c, $user, $effectiveUser, $set, $problem) {
return $c->authz->hasPermissions($user->user_id, 'show_answer_hash_info');
}
sub can_showPGInfo ($c, $user, $effectiveUser, $set, $problem) {
return $c->authz->hasPermissions($user->user_id, 'show_pg_info');
}
sub can_showResourceInfo ($c, $user, $effectiveUser, $set, $problem) {
return $c->authz->hasPermissions($user->user_id, 'show_resource_info');
}
sub can_showHints ($c, $user, $effectiveUser, $set, $problem) {
return 1 if $c->authz->hasPermissions($user->user_id, 'always_show_hint');
my $showHintsAfter =
$set->hide_hint ? -1
: $problem->showHintsAfter > -2 ? $problem->showHintsAfter
: $c->ce->{pg}{options}{showHintsAfter};
return $showHintsAfter > -1
&& $showHintsAfter <= $problem->num_correct + $problem->num_incorrect + ($c->{submitAnswers} ? 1 : 0);
}
sub can_showSolutions ($c, $user, $effectiveUser, $set, $problem) {
my $authz = $c->authz;
return
$authz->hasPermissions($user->user_id, 'always_show_solutions')
|| after($set->answer_date, $c->submitTime)
|| $authz->hasPermissions($user->user_id, 'show_solutions_before_answer_date');
}
sub can_recordAnswers ($c, $user, $effectiveUser, $set, $problem, $submitAnswers = 0) {
my $authz = $c->authz;
if ($user->user_id ne $effectiveUser->user_id) {
return $authz->hasPermissions($user->user_id, 'record_answers_when_acting_as_student');
}
return $authz->hasPermissions($user->user_id, 'record_answers_before_open_date')
if (before($set->open_date, $c->submitTime));
if (between($set->open_date, $set->due_date, $c->submitTime)) {
my $max_attempts = $problem->max_attempts;
my $attempts_used = $problem->num_correct + $problem->num_incorrect + ($submitAnswers ? 1 : 0);
if ($max_attempts == -1 or $attempts_used < $max_attempts) {
return $authz->hasPermissions($user->user_id, 'record_answers_after_open_date_with_attempts');
} else {
return $authz->hasPermissions($user->user_id, 'record_answers_after_open_date_without_attempts');
}
}
return $authz->hasPermissions($user->user_id, 'record_answers_after_due_date')
if (between($set->due_date, $set->answer_date, $c->submitTime));
return $authz->hasPermissions($user->user_id, 'record_answers_after_answer_date')
if (after($set->answer_date, $c->submitTime));
return 0;
}
sub can_checkAnswers ($c, $user, $effectiveUser, $set, $problem) {
my $authz = $c->authz;
# If we can record answers then we dont need to be able to check them
# unless we have that specific permission.
return 0
if ($c->can_recordAnswers($user, $effectiveUser, $set, $problem, $c->{submitAnswers})
&& !$authz->hasPermissions($user->user_id, 'can_check_and_submit_answers'));
return $authz->hasPermissions($user->user_id, 'check_answers_before_open_date')
if (before($set->open_date, $c->submitTime));
if (between($set->open_date, $set->due_date, $c->submitTime)) {
my $max_attempts = $problem->max_attempts;
my $attempts_used = $problem->num_correct + $problem->num_incorrect + ($c->{submitAnswers} ? 1 : 0);
if ($max_attempts == -1 or $attempts_used < $max_attempts) {
return $authz->hasPermissions($user->user_id, 'check_answers_after_open_date_with_attempts');
} else {
return $authz->hasPermissions($user->user_id, 'check_answers_after_open_date_without_attempts');
}
}
return $authz->hasPermissions($user->user_id, 'check_answers_after_due_date')
if (between($set->due_date, $set->answer_date, $c->submitTime));
return $authz->hasPermissions($user->user_id, 'check_answers_after_answer_date')
if (after($set->answer_date, $c->submitTime));
return 0;
}
sub can_useMathView ($c) {
return $c->ce->{pg}{specialPGEnvironmentVars}{entryAssist} eq 'MathView';
}
sub can_useMathQuill ($c) {
return $c->ce->{pg}{specialPGEnvironmentVars}{entryAssist} eq 'MathQuill';
}
# Check if the showMeAnother button should be allowed. Note that this is done *before* the check to see if
# showMeAnother is possible.
sub can_showMeAnother ($c, $user, $effectiveUser, $set, $problem, $submitAnswers = 0) {
my $ce = $c->ce;
# If the showMeAnother button isn't enabled for the course, then it can't be used.
return 0 unless $ce->{pg}{options}{enableShowMeAnother};
if (after($set->open_date, $c->submitTime)
|| $c->authz->hasPermissions($c->param('user'), 'can_use_show_me_another_early'))
{
$c->{showMeAnother}{TriesNeeded} = $ce->{pg}{options}{showMeAnotherDefault}
if $c->{showMeAnother}{TriesNeeded} == -2;
# If showMeAnother is not permitted for the problem, then it can't be used for this problem.
return 0 unless $c->{showMeAnother}{TriesNeeded} > -1;
# If the user is previewing or checking a showMeAnother problem corresponding to this set and problem then
# clearly the user can use show me another.
return 1
if $c->authen->session->{showMeAnother}
&& defined $c->authen->session->{showMeAnother}{setID}
&& $c->authen->session->{showMeAnother}{setID} eq $set->set_id
&& defined $c->authen->session->{showMeAnother}{problemID}
&& $c->authen->session->{showMeAnother}{problemID} eq $problem->problem_id
&& ($c->{checkAnswers} || $c->{previewAnswers});
# If the student has not attempted the original problem enough times yet, then showMeAnother cannot be used.
return 0
if $problem->num_correct + $problem->num_incorrect + ($submitAnswers ? 1 : 0) <
$c->{showMeAnother}{TriesNeeded};
# If the number of showMeAnother uses has been exceeded, then the user cannot use it again.
return 0 if $c->{showMeAnother}{Count} >= $c->{showMeAnother}{MaxReps} && $c->{showMeAnother}{MaxReps} > -1;
return 1;
}
return 0;
}
sub attemptResults ($c, $pg) {
return $pg->{result}{summary}
? $c->c($c->tag('h2', class => 'fs-3 mb-2', $c->maketext('Results for this submission'))
. $c->tag('div', role => 'alert', $c->b($pg->{result}{summary})))->join('')
: '';
}
async sub pre_header_initialize ($c) {
my $ce = $c->ce;
my $db = $c->db;
my $authz = $c->authz;
my $setID = $c->stash('setID');
my $problemID = $c->stash('problemID');
my $userID = $c->param('user');
my $effectiveUserID = $c->param('effectiveUser');
$c->{editMode} = $c->param('editMode');
my $user = $db->getUser($userID);
my $effectiveUser = $db->getUser($effectiveUserID);
return unless defined $user && defined $effectiveUser; # This should be impossible.
# Check that the set is valid. $c->{invalidSet} is set in checkSet called by ContentGenerator.pm.
return if $c->{invalidSet};
# Obtain the merged set for $effectiveUser
$c->{set} = $db->getMergedSet($effectiveUserID, $setID);
# Determine if the set should be considered open.
# It is open if the user can view unopened sets or is an instructor editing a problem from the problem editor,
# or it is after the set open date and is not conditionally restricted and is not jitar hidden or closed.
return
unless $authz->hasPermissions($userID, 'view_unopened_sets')
|| $setID eq 'Undefined_Set'
|| (
after($c->{set}->open_date, $c->submitTime)
&& !(
($ce->{options}{enableConditionalRelease} && is_restricted($db, $c->{set}, $effectiveUserID))
|| (
$c->{set}->assignment_type eq 'jitar'
&& (is_jitar_problem_hidden($db, $effectiveUserID, $c->{set}->set_id, $problemID)
|| is_jitar_problem_closed($db, $ce, $effectiveUserID, $c->{set}->set_id, $problemID))
)
)
);
# When a set is created enable_reduced_scoring is null, so we have to set it
if ($c->{set} && $c->{set}->enable_reduced_scoring ne '0' && $c->{set}->enable_reduced_scoring ne '1') {
my $globalSet = $db->getGlobalSet($c->{set}->set_id);
$globalSet->enable_reduced_scoring('0');
$db->putGlobalSet($globalSet);
$c->{set} = $db->getMergedSet($effectiveUserID, $setID);
}
# Obtain the merged problem for the effective user.
my $problem = $db->getMergedProblem($effectiveUserID, $setID, $problemID);
if ($authz->hasPermissions($userID, 'modify_problem_sets')) {
# This is the case of the problem editor for a user that can modify problem sets.
# If a user set does not exist for this user and this set, then check
# the global set. If that does not exist, then create a fake set. If it does, then add fake user data.
unless (defined $c->{set}) {
my $userSetClass = $db->{set_user}->{record};
my $globalSet = $db->getGlobalSet($setID);
if (not defined $globalSet) {
$c->{set} = fake_set($db);
} else {
$c->{set} = global2user($userSetClass, $globalSet);
$c->{set}->psvn(0);
}
}
# If a problem is not defined obtain the global problem, convert it to a user problem, and add fake user data.
unless (defined $problem) {
my $globalProblem = $db->getGlobalProblem($setID, $problemID);
# If the global problem doesn't exist either, bail!
if (!defined $globalProblem) {
my $sourceFilePath = $c->param('sourceFilePath');
# These are problems from setmaker. If declared invalid, they won't come up.
if (defined $sourceFilePath) {
die 'sourceFilePath is unsafe!'
unless path_is_subdir($sourceFilePath, $ce->{courseDirs}{templates}, 1);
} else {
$c->{invalidProblem} = $c->{invalidSet} = 1;
return;
}
$problem = fake_problem($db);
$problem->problem_id(1);
$problem->source_file($sourceFilePath);
$problem->user_id($effectiveUserID);
} else {
$problem = global2user($db->{problem_user}{record}, $globalProblem);
$problem->user_id($effectiveUserID);
$problem->problem_seed(0);
$problem->status(0);
$problem->attempted(0);
$problem->last_answer('');
$problem->num_correct(0);
$problem->num_incorrect(0);
}
}
# Now we're sure we have valid UserSet and UserProblem objects
# Deal with possible editor overrides.
# If the caller is asking to override the source file, and editMode calls for a temporary file, do so.
my $sourceFilePath = $c->param('sourceFilePath');
if (defined $c->{editMode} && $c->{editMode} eq 'temporaryFile' && defined $sourceFilePath) {
die 'sourceFilePath is unsafe!'
unless path_is_subdir($sourceFilePath, $ce->{courseDirs}->{templates}, 1);
$problem->source_file($sourceFilePath);
}
# If the problem does not have a source file or no source file has been passed in
# then this is really an invalid problem (probably from a bad URL).
$c->{invalidProblem} = !(defined $sourceFilePath || $problem->source_file);
# If the caller is asking to override the problem seed, do so.
my $problemSeed = $c->param('problemSeed');
if (defined $problemSeed && $problemSeed =~ /^[+-]?\d+$/) {
$problem->problem_seed($problemSeed);
}
$c->addmessage($c->{set}->visible
? $c->tag('p', class => 'font-visible m-0', $c->maketext('This set is visible to students.'))
: $c->tag('p', class => 'font-hidden m-0', $c->maketext('This set is hidden from students.')));
} else {
# Test for additional problem validity if it's not already invalid.
$c->{invalidProblem} =
!(defined $problem && ($c->{set}->visible || $authz->hasPermissions($userID, 'view_hidden_sets')));
$c->addbadmessage($c->maketext('This problem will not count toward your grade.'))
if $problem && !$problem->value && !$c->{invalidProblem};
}
$c->{userID} = $userID;
$c->{effectiveUserID} = $effectiveUserID;
$c->{user} = $user;
$c->{effectiveUser} = $effectiveUser;
$c->{problem} = $problem;
# Form processing
# Set options from form fields (see comment at top of file for form fields).
my $displayMode = $c->param('displayMode') || $user->displayMode || $ce->{pg}->{options}->{displayMode};
my $redisplay = $c->param('redisplay');
$c->{submitAnswers} = $c->param('submitAnswers');
my $checkAnswers = $c->param('checkAnswers');
my $previewAnswers = $c->param('previewAnswers');
my $requestNewSeed = $c->param('requestNewSeed') // 0;
my $formFields = $c->req->params->to_hash;
# Check for a page refresh which causes a cached form resubmission. In that case this is
# not a valid submission of answers.
if (
$c->{set}->set_id ne 'Undefined_Set'
&& $c->{submitAnswers}
&& (
!defined $formFields->{num_attempts}
|| (defined $formFields->{num_attempts}
&& $formFields->{num_attempts} != $problem->num_correct + $problem->num_incorrect)
)
)
{
$c->{submitAnswers} = 0;
$c->{resubmitDetected} = 1;
delete $formFields->{submitAnswers};
}
$c->{displayMode} = $displayMode;
$c->{redisplay} = $redisplay;
$c->{checkAnswers} = $checkAnswers;
$c->{previewAnswers} = $previewAnswers;
$c->{formFields} = $formFields;
# Get the status message and add it to the messages.
$c->addmessage($c->tag('p', $c->b($c->authen->flash('status_message')))) if $c->authen->flash('status_message');
# Now that the necessary variables are set, return if the set or problem is invalid.
return if $c->{invalidSet} || $c->{invalidProblem};
# Construct a hash containing information for showMeAnother.
# TriesNeeded: The number of times the student needs to attempt this problem before the button is available.
# MaxReps: The maximum number of times that showMeAnother can be used for this problem.
# Count: The number of times the student has used showMeAnother for this problem.
$c->{showMeAnother} = {
TriesNeeded => $problem->{showMeAnother},
MaxReps => $ce->{pg}{options}{showMeAnotherMaxReps},
Count => $problem->{showMeAnotherCount},
};
# Permissions
# What does the user want to do?
my %want = (
showOldAnswers => $user->showOldAnswers ne '' ? $user->showOldAnswers : $ce->{pg}{options}{showOldAnswers},
showCorrectAnswers => 1,
showProblemGrader => $userID ne $effectiveUserID,
showAnsGroupInfo => $c->param('showAnsGroupInfo') || $ce->{pg}{options}{showAnsGroupInfo},
showAnsHashInfo => $c->param('showAnsHashInfo') || $ce->{pg}{options}{showAnsHashInfo},
showPGInfo => $c->param('showPGInfo') || $ce->{pg}{options}{showPGInfo},
showResourceInfo => $c->param('showResourceInfo') || $ce->{pg}{options}{showResourceInfo},
showHints => 1,
showSolutions => 1,
useMathView => $user->useMathView ne '' ? $user->useMathView : $ce->{pg}{options}{useMathView},
useMathQuill => $user->useMathQuill ne '' ? $user->useMathQuill : $ce->{pg}{options}{useMathQuill},
recordAnswers => $c->{submitAnswers} && !$authz->hasPermissions($userID, 'avoid_recording_answers'),
checkAnswers => $checkAnswers,
getSubmitButton => 1,
);
# Does the user have permission to use certain options?
my @args = ($user, $effectiveUser, $c->{set}, $problem);
my %can = (
showOldAnswers => $c->can_showOldAnswers(@args),
showCorrectAnswers => $c->can_showCorrectAnswers(@args),
showProblemGrader => $c->can_showProblemGrader(@args),
showAnsGroupInfo => $c->can_showAnsGroupInfo(@args),
showAnsHashInfo => $c->can_showAnsHashInfo(@args),
showPGInfo => $c->can_showPGInfo(@args),
showResourceInfo => $c->can_showResourceInfo(@args),
showHints => $c->can_showHints(@args),
showSolutions => $c->can_showSolutions(@args),
recordAnswers => $c->can_recordAnswers(@args),
checkAnswers => $c->can_checkAnswers(@args),
showMeAnother => $c->can_showMeAnother(@args, $c->{submitAnswers}),
getSubmitButton => $c->can_recordAnswers(@args, $c->{submitAnswers}),
useMathView => $c->can_useMathView,
useMathQuill => $c->can_useMathQuill,
);
# Re-randomization based on the number of attempts and specified period
my $prEnabled = $ce->{pg}{options}{enablePeriodicRandomization} // 0;
my $rerandomizePeriod = $ce->{pg}{options}{periodicRandomizationPeriod} // 0;
$problem->{prPeriod} = $ce->{problemDefaults}{prPeriod}
if (defined $problem->{prPeriod} && $problem->{prPeriod} =~ /^\s*$/);
$rerandomizePeriod = $problem->{prPeriod}
if (defined $problem->{prPeriod} && $problem->{prPeriod} > -1);
$prEnabled = 0 if ($rerandomizePeriod < 1 || $c->{editMode});
if ($prEnabled) {
$problem->{prCount} = 0
if !defined $problem->{prCount} || $problem->{prCount} =~ /^\s*$/;
$problem->{prCount} += $c->{submitAnswers} ? 1 : 0;
$requestNewSeed = 0
if ($problem->{prCount} < $rerandomizePeriod || after($c->{set}->due_date, $c->submitTime));
if ($requestNewSeed) {
# obtain new random seed to hopefully change the problem
$problem->{problem_seed} =
($problem->{problem_seed} + $problem->num_correct + $problem->num_incorrect) % 10000;
$problem->{prCount} = 0;
}
if ($problem->{prCount} > -1) {
my $pureProblem = $db->getUserProblem($problem->user_id, $problem->set_id, $problem->problem_id);
$pureProblem->problem_seed($problem->{problem_seed});
$pureProblem->prCount($problem->{prCount});
$db->putUserProblem($pureProblem);
}
}
# Final values for options
my %will = map { $_ => $can{$_} && $want{$_} } keys %can;
if ($prEnabled && $problem->{prCount} >= $rerandomizePeriod && !after($c->{set}->due_date, $c->submitTime)) {
$can{requestNewSeed} = 1;
$want{requestNewSeed} = 1;
$will{requestNewSeed} = 1;
$c->{showCorrectOnRandomize} = $ce->{pg}{options}{showCorrectOnRandomize};
# If this happens, it means that the page was refreshed. So prevent the answers from
# being recorded and the number of attempts from being increased.
if ($problem->{prCount} > $rerandomizePeriod) {
$c->{resubmitDetected} = 1;
$can{recordAnswers} = 0;
$want{recordAnswers} = 0;
$will{recordAnswers} = 0;
}
}
# If this is set to 1 below, then feedback is shown when a student returns to a previously worked problem without
# requiring another answer submission.
my $showReturningFeedback = 0;
# Reinsert sticky answers. Do this only if new answers are NOT being submitted,
# and a new problem version is NOT being opened.
if (!($prEnabled && !$problem->{prCount})
&& !($c->{submitAnswers} || $previewAnswers || $checkAnswers)
&& $will{showOldAnswers})
{
my %oldAnswers = decodeAnswers($problem->last_answer);
$formFields->{$_} = $oldAnswers{$_} for (keys %oldAnswers);
$showReturningFeedback = 1
if $ce->{pg}{options}{automaticAnswerFeedback} && $problem->num_correct + $problem->num_incorrect > 0;
}
my $showOnlyCorrectAnswers = $c->param('showCorrectAnswers') && $will{showCorrectAnswers};
# Translation
debug('begin pg processing');
my $pg = await renderPG(
$c,
$effectiveUser,
$c->{set},
$problem,
$c->{set}->psvn,
$prEnabled
&& !$problem->{prCount}
&& !($c->{submitAnswers} || $previewAnswers || $checkAnswers || $showOnlyCorrectAnswers) ? {} : $formFields,
{
displayMode => $displayMode,
showHints => $will{showHints},
showSolutions => $will{showSolutions},
showResourceInfo => $will{showResourceInfo},
refreshMath2img => $will{showHints} || $will{showSolutions},
processAnswers => 1,
permissionLevel => $db->getPermissionLevel($userID)->permission,
effectivePermissionLevel => $db->getPermissionLevel($effectiveUserID)->permission,
useMathQuill => $will{useMathQuill},
useMathView => $will{useMathView},
forceScaffoldsOpen => $showOnlyCorrectAnswers,
isInstructor => $authz->hasPermissions($userID, 'view_answers'),
showFeedback => $c->{submitAnswers} || $c->{previewAnswers} || $showReturningFeedback,
showAttemptAnswers => $showOnlyCorrectAnswers ? 0 : $ce->{pg}{options}{showEvaluatedAnswers},
showAttemptPreviews => !$showOnlyCorrectAnswers,
showAttemptResults => $c->{submitAnswers} || $showReturningFeedback,
forceShowAttemptResults => $showOnlyCorrectAnswers
|| $will{checkAnswers}
|| $will{showProblemGrader}
|| ($ce->{pg}{options}{automaticAnswerFeedback}
&& !$c->{previewAnswers}
&& after($c->{set}->answer_date, $c->submitTime)),
showMessages => !$showOnlyCorrectAnswers,
showCorrectAnswers => (
$c->{submitAnswers} && $c->{showCorrectOnRandomize} ? 2
: !$c->{previewAnswers} && after($c->{set}->answer_date, $c->submitTime)
? ($ce->{pg}{options}{correctRevealBtnAlways} ? 1 : 2)
: $will{showProblemGrader} || (!$c->{previewAnswers} && $will{showCorrectAnswers}) ? 1
: 0
),
debuggingOptions => getTranslatorDebuggingOptions($authz, $userID),
$prEnabled && !$problem->{prCount}
? (problemData => '{}')
: ($can{checkAnswers} && defined $formFields->{problem_data})
? (problemData => $formFields->{problem_data})
: ()
}
);
debug('end pg processing');
$pg->{body_text} .= $c->hidden_field(
num_attempts => $problem->num_correct + $problem->num_incorrect + ($c->{submitAnswers} ? 1 : 0),
id => 'num_attempts'
);
# Update and fix hint/solution options after PG processing
$can{showHints} &&= $pg->{flags}{hintExists};
$can{showSolutions} &&= $pg->{flags}{solutionExists};
# Store fields
$c->{want} = \%want;
$c->{can} = \%can;
$c->{will} = \%will;
$c->{pg} = $pg;
# Process and log answers
$c->{scoreRecordedMessage} = await process_and_log_answer($c) || '';
return;
}
sub head ($c) {
return '' if ($c->{invalidSet});
return $c->{pg}{head_text} if $c->{pg}{head_text};
return '';
}
sub post_header_text ($c) {
return '' if ($c->{invalidSet});
return $c->{pg}->{post_header_text} if $c->{pg}->{post_header_text};
return '';
}
sub siblings ($c) {
my $db = $c->db;
my $ce = $c->ce;
my $authz = $c->authz;
# Can't show sibling problems if the set is invalid.
return '' if $c->{invalidSet};
my $setID = $c->{set}->set_id;
my $eUserID = $c->param('effectiveUser');
my @problemRecords = $db->getMergedProblemsWhere({ user_id => $eUserID, set_id => $setID }, 'problem_id');
my @problemIDs = map { $_->problem_id } @problemRecords;
my $isJitarSet = $setID ne 'Undefined_Set' && $c->{set}->assignment_type eq 'jitar' ? 1 : 0;
# Variables for the progress bar
my $num_of_problems = 0;
my $problemList;
my $total_correct = 0;
my $total_incorrect = 0;
my $total_inprogress = 0;
my $is_reduced = 0;
my $currentProblemID = $c->{invalidProblem} ? 0 : $c->{problem}->problem_id;
my $progressBarEnabled = $c->ce->{pg}{options}{enableProgressBar};
my @items;
for my $problemID (@problemIDs) {
if ($isJitarSet
&& !$authz->hasPermissions($eUserID, 'view_unopened_sets')
&& is_jitar_problem_hidden($db, $eUserID, $setID, $problemID))
{
shift(@problemRecords) if $progressBarEnabled;
next;
}
my $status_symbol = '';
if ($progressBarEnabled) {
my $problemRecord = shift(@problemRecords);
$num_of_problems++;
my $total_attempts = $problemRecord->num_correct + $problemRecord->num_incorrect;
my $status = compute_unreduced_score($ce, $problemRecord, $c->{set});
$is_reduced = 1 if $status > $problemRecord->status;
if ($isJitarSet) {
$status = jitar_problem_adjusted_status($problemRecord, $db);
}
# variables for the widths of the bars in the Progress Bar
if ($status == 1) {
# correct
$total_correct++;
$status_symbol = ' ✓'; # checkmark
} else {
# incorrect
if ($total_attempts >= $problemRecord->max_attempts && $problemRecord->max_attempts != -1) {
$total_incorrect++;
$status_symbol = ' ✗'; # cross
} else {
# in progress
if ($problemRecord->attempted > 0) {
$total_inprogress++;
$status_symbol = ' …'; # horizontal ellipsis
}
}
}
}
my $active = ($progressBarEnabled && $currentProblemID eq $problemID);
my $problemPage = $c->url_for('problem_detail', setID => $setID, problemID => $problemID);
if ($isJitarSet) {
# If it is a jitar set, we need to hide and disable links to hidden or restricted problems.
my @seq = jitar_id_to_seq($problemID);
my $level = $#seq;
my $class = 'nav-link' . ($active ? ' active' : '');
if ($level != 0) {
$class .= ' nested-problem-' . $level;
}
if (!$authz->hasPermissions($eUserID, 'view_unopened_sets')
&& is_jitar_problem_closed($db, $ce, $eUserID, $setID, $problemID))
{
push(
@items,
$c->tag(
'a',
class => $class . ' disabled',
$c->maketext('Problem [_1]', join('.', @seq))
)
);
} else {
push(
@items,
$c->tag(
'a',
$active ? () : (href => $c->systemLink($problemPage)),
class => $class,
$c->b($c->maketext('Problem [_1]', join('.', @seq)) . $status_symbol)
)
);
}
} else {
push(
@items,
$c->tag(
'a',
$active ? () : (href => $c->systemLink($problemPage)),
class => 'nav-link' . ($active ? ' active' : ''),
$c->b($c->maketext('Problem [_1]', $problemID) . $status_symbol)
)
);
}
}
return $c->include(
'ContentGenerator/Problem/siblings',
items => \@items,
num_of_problems => $num_of_problems,
total_correct => $total_correct,
total_incorrect => $total_incorrect,
total_inprogress => $total_inprogress,
is_reduced => $is_reduced
);
}
sub nav ($c, $args) {
return '' if $c->{invalidProblem} || $c->{invalidSet};
my %can = %{ $c->{can} };
my $db = $c->db;
my $ce = $c->ce;
my $authz = $c->authz;
my $setID = $c->{set}->set_id;
my $problemID = $c->{problem}->problem_id;
my $userID = $c->param('user');
my $eUserID = $c->param('effectiveUser');
my $mergedSet = $db->getMergedSet($eUserID, $setID);
return '' if !$mergedSet;
my $isJitarSet = $mergedSet->assignment_type eq 'jitar';
my ($prevID, $nextID);
# Find the next or previous problem, and determine if it is actually open for a jitar set.
if (!$c->{invalidProblem}) {
my @problemIDs =
map { $_->[2] } $db->listUserProblemsWhere({ user_id => $eUserID, set_id => $setID }, 'problem_id');
if ($isJitarSet) {
my @processedProblemIDs;
for my $id (@problemIDs) {
push @processedProblemIDs, $id
unless !$authz->hasPermissions($eUserID, 'view_unopened_sets')
&& is_jitar_problem_hidden($db, $eUserID, $setID, $id);
}
@problemIDs = @processedProblemIDs;
}
my $curr_index = 0;
for (my $i = 0; $i <= $#problemIDs; $i++) {
$curr_index = $i if $problemIDs[$i] == $problemID;
}
$prevID = $problemIDs[ $curr_index - 1 ] if $curr_index - 1 >= 0;
$nextID = $problemIDs[ $curr_index + 1 ] if $curr_index + 1 <= $#problemIDs;
$nextID = ''
if ($isJitarSet
&& $nextID
&& !$authz->hasPermissions($eUserID, 'view_unopened_sets')
&& is_jitar_problem_closed($db, $ce, $eUserID, $setID, $nextID));
}
my @links;
if ($prevID) {
push @links, $c->maketext('Previous Problem'),
$c->url_for('problem_detail', setID => $setID, problemID => $prevID),
$c->maketext('Previous Problem');
} else {
push @links, $c->maketext('Previous Problem'), '', $c->maketext('Previous Problem');
}
if (defined $setID && $setID ne 'Undefined_Set') {
push @links, $c->maketext('Problem List'), $c->url_for('problem_list', setID => $setID),
$c->maketext('Problem List');
} else {
push @links, $c->maketext('Problem List'), '', $c->maketext('Problem List');
}
if ($nextID) {
push @links, $c->maketext('Next Problem'),
$c->url_for('problem_detail', setID => $setID, problemID => $nextID),
$c->maketext('Next Problem');
} else {
push @links, $c->maketext('Next Problem'), '', $c->maketext('Next Problem');
}
my %tail;
$tail{displayMode} = $c->{displayMode} if defined $c->{displayMode};
$tail{showOldAnswers} = 1 if $c->{will}{showOldAnswers};
$tail{studentNavFilter} = $c->param('studentNavFilter') if $c->param('studentNavFilter');
return $c->tag(
'div',
class => 'row sticky-nav',
role => 'navigation',
'aria-label' => 'problem navigation',
$c->c($c->tag('div', class => 'd-flex submit-buttons-container', $c->navMacro($args, \%tail, @links)),
studentNav($c, $setID))->join('')
);
}
sub path ($c, $args) {
my $prettyProblemNumber = $c->stash('problemID');
my $set = $c->db->getGlobalSet($c->stash('setID'));
if ($set && $set->assignment_type eq 'jitar' && $prettyProblemNumber) {
$prettyProblemNumber = join('.', jitar_id_to_seq($prettyProblemNumber));
}
my $navigation_allowed = $c->authz->hasPermissions($c->param('user'), 'navigation_allowed');
my @path = (
WeBWorK => $navigation_allowed ? $c->url_for('root') : '',
$c->stash('courseID') => $navigation_allowed ? $c->url_for('set_list') : '',
$c->stash('setID') => $c->url_for('problem_list'),
);
if ($c->current_route eq 'show_me_another') {
push(
@path,
$prettyProblemNumber => $c->url_for('problem_detail'),
'Show Me Another' => ''
);
} else {
push(@path, $prettyProblemNumber => '');
}
return $c->pathMacro($args, @path);
}
sub page_title ($c) {
my $db = $c->db;
my $setID = $c->stash('setID');
my $problemID = $c->stash('problemID');
my $set = $db->getGlobalSet($setID);
if ($set && $set->assignment_type eq 'jitar') {
$problemID = join('.', jitar_id_to_seq($problemID));
}
my $header =
$c->maketext('[_1]: Problem [_2]', $c->tag('span', dir => 'ltr', format_set_name_display($setID)), $problemID);
# Return here if we don't have the requisite information.
return $header if ($c->{invalidSet} || $c->{invalidProblem});
my $ce = $c->ce;
my $problem = $c->{problem};
my $subheader = '';
my $problemValue = $problem->value;
if (defined $problemValue && $problemValue ne '') {
$subheader .= $c->maketext('([quant,_1,point])', $problemValue);
}
# This uses the permission level and user id of the user assigned to the problem.
my $problemUser = $problem->user_id;
if ($c->authz->hasPermissions($problemUser, 'print_path_to_problem')) {
$subheader .= ' ' . $problem->source_file;
}
# Add the edit link to the sub header if the user has the permisions ot edit problems.
if ($c->authz->hasPermissions($c->param('user'), 'modify_problem_sets')) {
$subheader = $c->c(
$subheader,
$c->tag(
'span',
class => 'ms-2',
$c->link_to(
$c->maketext('Edit') => $c->systemLink(
$c->url_for(
'instructor_problem_editor_withset_withproblem',
setID => $c->{set}->set_id,
problemID => $c->{problem}->problem_id
),
# If we are here without a real homework set, carry that through.
$c->{set}->set_id eq 'Undefined_Set'
? (params => [ 'sourceFilePath' => $c->param('sourceFilePath') ])
: ()
),
target => 'WW_Editor',
class => 'btn btn-sm btn-secondary'
)
)
)->join('');
}
# Add the tag edit button to the sub header if the user has permission to edit tags.
if ($c->authz->hasPermissions($c->param('user'), 'modify_tags')) {
$subheader = $c->c(
$subheader,
$c->tag(
'button',
id => 'tagger',
type => 'button',
class => 'tag-edit-btn btn btn-secondary btn-sm ms-2',
data => { source_file => $c->ce->{courseDirs}{templates} . '/' . $c->{problem}{source_file} },
$c->maketext('Edit Tags')
),
$c->hidden_field(hidden_course_id => $c->stash('courseID'))
)->join('');
}
return $c->c($header, $c->tag('span', class => 'problem-sub-header d-block', $subheader))->join('');
}
# Add a lang and maybe also a dir setting to the DIV tag attributes, if needed by the PROBLEM language.
sub output_problem_lang_and_dir ($c) {
return get_problem_lang_and_dir($c->{pg}{flags}, $c->ce->{perProblemLangAndDirSettingMode}, $c->ce->{language});
}
# Output the body of the current problem
sub output_problem_body ($c) {
# If there are translation errors then render those with the body text of the problem.
if ($c->{pg}{flags}{error_flag}) {
if ($c->authz->hasPermissions($c->param('user'), 'view_problem_debugging_info')) {
# For instructors render the body text of the problem with the errors.
return $c->include(
'ContentGenerator/Base/error_output',
error => $c->{pg}{errors},
details => $c->{pg}{body_text}
);
} else {
# For students render the body text of the problem with a message about error details.
return $c->c(
$c->tag('div', $c->b($c->{pg}{body_text})),
$c->include(
'ContentGenerator/Base/error_output',
error => $c->{pg}{errors},
details => $c->maketext('You do not have permission to view the details of this error.')
)
)->join('');
}
}
return $c->tag(
'div',
id => 'output_problem_body',
class => 'text-dark',
style => 'color-scheme: light',
data => { bs_theme => 'light' },
$c->b($c->{pg}{body_text})
);
}
# Output messages about the problem
sub output_message ($c) {
return $c->include('ContentGenerator/Problem/messages');
}