-
-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathRenderProblem.pm
More file actions
339 lines (299 loc) · 13.3 KB
/
Copy pathRenderProblem.pm
File metadata and controls
339 lines (299 loc) · 13.3 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
################################################################################
# WeBWorK Online Homework Delivery System
# Copyright © 2000-2024 The WeBWorK Project, https://github.com/openwebwork
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of either: (a) the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any later
# version, or (b) the "Artistic License" which comes with this package.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See either the GNU General Public License or the
# Artistic License for more details.
################################################################################
package WebworkWebservice::RenderProblem;
use strict;
use warnings;
use Future::AsyncAwait;
use Benchmark;
use Mojo::Util qw(url_unescape);
use WeBWorK::Debug;
use WeBWorK::CourseEnvironment;
use WeBWorK::DB;
use WeBWorK::DB::Utils qw(global2user fake_set fake_problem);
use WeBWorK::Utils qw(decode_utf8_base64);
use WeBWorK::Utils::Files qw(readFile);
use WeBWorK::Utils::Rendering qw(renderPG);
our $UNIT_TESTS_ON = 0;
async sub renderProblem {
my ($invocant, $ws) = @_;
my $rh = $ws->{inputs_ref};
# $WeBWorK::Debug::Enabled needs to be checked, otherwise pretty_print_rh($rh) is called regardless of if debgging
# is enabled. That is an expensive method to always call here.
debug(pretty_print_rh($rh)) if $WeBWorK::Debug::Enabled;
my $problemSeed = $rh->{problemSeed} // '1234';
my $beginTime = Benchmark->new;
my $ce = $ws->ce;
my $db = $ws->db;
# Determine an effective user for this interaction or create one if it is not given.
# Use effectiveUser if given, and $rh->{user} otherwise.
my $effectiveUserName;
if (defined $rh->{effectiveUser} && $rh->{effectiveUser} =~ /\S/) {
$effectiveUserName = $rh->{effectiveUser};
} else {
$effectiveUserName = $rh->{user};
}
if ($UNIT_TESTS_ON) {
print STDERR "RenderProblem.pm: user = $rh->{user}\n";
print STDERR "RenderProblem.pm: courseName = $rh->{courseID}\n";
print STDERR "RenderProblem.pm: effectiveUserName = $effectiveUserName\n";
print STDERR 'environment fileName', $rh->{fileName}, "\n";
}
# The effectiveUser is the student this problem version was written for
# The user might also be the effective user but it could be
# an instructor checking out how well the problem is working.
my $effectiveUser = $db->getUser($effectiveUserName);
my $effectiveUserPermissionLevel;
my $effectiveUserPassword;
unless (defined $effectiveUser) {
$effectiveUser = $db->newUser;
$effectiveUserPermissionLevel = $db->newPermissionLevel;
$effectiveUserPassword = $db->newPassword;
$effectiveUser->user_id($effectiveUserName);
$effectiveUserPermissionLevel->user_id($effectiveUserName);
$effectiveUserPassword->user_id($effectiveUserName);
$effectiveUserPassword->password('');
$effectiveUser->last_name($rh->{studentName} || 'foobar');
$effectiveUser->first_name('');
$effectiveUser->student_id($rh->{studentID} || 'foobar');
$effectiveUser->email_address($rh->{email} || '');
$effectiveUser->section($rh->{section} || '');
$effectiveUser->recitation($rh->{recitation} || '');
$effectiveUser->comment('');
$effectiveUser->status('C');
$effectiveUserPermissionLevel->permission(0);
}
# Insure that set and problem are defined. Define the set and problem information from data in the environment if
# necessary.
my $setName = $rh->{set_id} // $rh->{setNumber} // '';
my $setVersionId = $rh->{version_id} || 0;
my $problemNumber = $rh->{probNum} // 0;
my $psvn = $rh->{psvn} // 1234;
my $problemValue = $rh->{problemValue} // 1;
my $lastAnswer = '';
debug('effectiveUserName: ' . $effectiveUserName);
debug('setName: ' . $setName);
debug('setVersionId: ' . $setVersionId);
debug('problemNumber: ' . $problemNumber);
debug('problemSeed:' . $problemSeed);
debug('psvn: ' . $psvn);
debug('problemValue: ' . $problemValue);
my $setRecord =
$setVersionId
? $db->getMergedSetVersion($effectiveUserName, $setName, $setVersionId)
: $db->getMergedSet($effectiveUserName, $setName);
if (defined $setRecord && ref $setRecord) {
# If an actual set from the database is used, the passed in psvn is ignored.
# So save the actual psvn used and pass that on to the renderer.
$psvn = $setRecord->psvn;
} else {
# if a User Set does not exist for this user and this set
# then we check the Global Set
# if that does not exist we create a fake set
# if it does, we add fake user data
my $userSetClass = $db->{set_user}{record};
my $globalSet = $db->getGlobalSet($setName);
if (!defined $globalSet) {
$setRecord = fake_set($db);
} else {
$setRecord = global2user($userSetClass, $globalSet);
}
# Initializations
$setRecord->set_id($setName);
$setRecord->set_header('');
$setRecord->hardcopy_header('defaultHeader');
$setRecord->open_date(time - 60 * 60 * 24 * 7); # one week ago
$setRecord->due_date(time + 60 * 60 * 24 * 7 * 2); # in two weeks
$setRecord->answer_date(time + 60 * 60 * 24 * 7 * 3); # in three weeks
$setRecord->psvn($rh->{psvn} // 1234);
}
# obtain the merged problem for $effectiveUser
my $problemRecord =
!$problemNumber ? undef
: $setVersionId ? $db->getMergedProblemVersion($effectiveUserName, $setName, $setVersionId, $problemNumber)
: $db->getMergedProblem($effectiveUserName, $setName, $problemNumber);
if (defined $problemRecord) {
# If a problem from the database is used, the passed in problem seed is ignored.
# So save the actual seed used and pass that on to the renderer.
$problemSeed = $problemRecord->problem_seed;
} else {
# If that is not yet defined obtain the global problem,
# convert it to a user problem, and add fake user data
my $userProblemClass = $db->{problem_user}{record};
my $globalProblem = $db->getGlobalProblem($setName, $problemNumber);
# if the global problem doesn't exist either, bail!
if (not defined $globalProblem) {
$problemRecord = fake_problem($db);
} else {
$problemRecord = global2user($userProblemClass, $globalProblem);
}
# initializations
$problemRecord->user_id($effectiveUserName);
$problemRecord->problem_id($problemNumber);
$problemRecord->set_id($setName);
$problemRecord->problem_seed($problemSeed);
$problemRecord->status(0);
$problemRecord->value($problemValue);
# We are faking it
$problemRecord->attempted(2000);
$problemRecord->num_correct(1000);
$problemRecord->num_incorrect(1000);
$problemRecord->last_answer($lastAnswer);
}
if ($UNIT_TESTS_ON) {
print STDERR 'setRecord is ', pretty_print_rh($setRecord);
print STDERR 'template directory path ', $ce->{courseDirs}{templates}, "\n";
print STDERR 'RenderProblem.pm: source file is ', $rh->{sourceFilePath}, "\n";
print STDERR "RenderProblem.pm: problem source is included in the request \n"
if defined($rh->{problemSource}) && $rh->{problemSource};
}
# Initialize problem source
my $r_problem_source;
if ($rh->{problemSource}) {
$r_problem_source = \(decode_utf8_base64($rh->{problemSource}) =~ tr/\r/\n/r);
$problemRecord->source_file($rh->{fileName} ? $rh->{fileName} : $rh->{sourceFilePath});
} elsif ($rh->{rawProblemSource}) {
$r_problem_source = \$rh->{rawProblemSource};
$problemRecord->source_file($rh->{fileName} ? $rh->{fileName} : $rh->{sourceFilePath});
} elsif ($rh->{uriEncodedProblemSource}) {
$r_problem_source = \(url_unescape($rh->{uriEncodedProblemSource}));
$problemRecord->source_file($rh->{fileName} ? $rh->{fileName} : $rh->{sourceFilePath});
} elsif (defined $rh->{sourceFilePath} && $rh->{sourceFilePath} =~ /\S/) {
$problemRecord->source_file($rh->{sourceFilePath});
$r_problem_source = \(readFile($ce->{courseDirs}{templates} . '/' . $rh->{sourceFilePath}));
}
if ($UNIT_TESTS_ON) {
print STDERR 'template directory path ', $ce->{courseDirs}{templates}, "\n";
print STDERR 'RenderProblem.pm: source file is ', $problemRecord->source_file, "\n";
print STDERR "RenderProblem.pm: problem source is included in the request \n" if defined($rh->{problemSource});
}
# now we're sure we have valid UserSet and UserProblem objects
# Other initializations
my $translationOptions = {
displayMode => $rh->{displayMode} // 'MathJax',
showHints => $rh->{showHints},
showSolutions => $rh->{showSolutions},
refreshMath2img => $rh->{showHints} || $rh->{showSolutions},
processAnswers => $rh->{processAnswers} // 1,
catchWarnings => 1,
r_source => $r_problem_source,
problemUUID => $rh->{problemUUID} // 0,
permissionLevel => $rh->{permissionLevel} || 0,
effectivePermissionLevel => $rh->{effectivePermissionLevel} || $rh->{permissionLevel} || 0,
useMathQuill => $ce->{pg}{specialPGEnvironmentVars}{entryAssist} eq 'MathQuill',
useMathView => $ce->{pg}{specialPGEnvironmentVars}{entryAssist} eq 'MathView',
isInstructor => $rh->{isInstructor} // 0,
forceScaffoldsOpen => $rh->{WWcorrectAnsOnly} ? 1 : ($rh->{forceScaffoldsOpen} // 0),
QUIZ_PREFIX => $rh->{answerPrefix},
showFeedback => $rh->{previewAnswers} || $rh->{WWsubmit} || $rh->{WWcorrectAns},
showAttemptAnswers => $rh->{WWcorrectAnsOnly} ? 0
: ($rh->{showAttemptAnswers} // $ce->{pg}{options}{showEvaluatedAnswers}),
showAttemptPreviews => (
$rh->{WWcorrectAnsOnly} ? 0
: ($rh->{showAttemptPreviews} // ($rh->{previewAnswers} || $rh->{WWsubmit} || $rh->{WWcorrectAns}))
),
showAttemptResults => $rh->{showAttemptResults} // ($rh->{WWsubmit} || $rh->{WWcorrectAns}),
forceShowAttemptResults => (
$rh->{WWcorrectAnsOnly} ? 1
: (
$rh->{forceShowAttemptResults}
|| ($rh->{isInstructor}
&& ($rh->{showAttemptResults} // ($rh->{WWsubmit} || $rh->{WWcorrectAns})))
)
),
showMessages => (
$rh->{WWcorrectAnsOnly} ? 0
: ($rh->{showMessages} // ($rh->{previewAsnwers} || $rh->{WWsubmit} || $rh->{WWcorrectAns}))
),
showCorrectAnswers =>
($rh->{WWcorrectAnsOnly} ? 1 : ($rh->{showCorrectAnswers} // ($rh->{WWcorrectAns} ? 2 : 0))),
debuggingOptions => {
show_resource_info => $rh->{show_resource_info} // 0,
view_problem_debugging_info => $rh->{view_problem_debugging_info} // 0,
show_pg_info => $rh->{show_pg_info} // 0,
show_answer_hash_info => $rh->{show_answer_hash_info} // 0,
show_answer_group_info => $rh->{show_answer_group_info} // 0
},
defined $rh->{problem_data} && $rh->{problem_data} ne '' ? (problemData => $rh->{problem_data}) : ()
};
$ce->{pg}{specialPGEnvironmentVars}{problemPreamble} = { TeX => '', HTML => '' } if $rh->{noprepostambles};
$ce->{pg}{specialPGEnvironmentVars}{problemPostamble} = { TeX => '', HTML => '' } if $rh->{noprepostambles};
my $pg =
await renderPG($ws->c, $effectiveUser, $setRecord, $problemRecord, $setRecord->psvn, $rh, $translationOptions);
# New version of output:
return {
text => $pg->{body_text},
header_text => $pg->{head_text},
post_header_text => $pg->{post_header_text},
answers => $pg->{answers},
errors => $pg->{errors},
pg_warnings => $pg->{warnings},
PG_ANSWERS_HASH => $pg->{PG_ANSWERS_HASH},
PERSISTENCE_HASH => $pg->{PERSISTENCE_HASH},
problem_result => $pg->{result},
problem_state => $pg->{state},
flags => $pg->{flags},
psvn => $psvn,
problem_seed => $problemSeed,
resource_list => $pg->{resource_list},
warning_messages => ref $pg->{warning_messages} eq 'ARRAY' ? $pg->{warning_messages} : [],
debug_messages => ref $pg->{debug_messages} eq 'ARRAY' ? $pg->{debug_messages} : [],
deprecated_macros => ref $pg->{deprecated_macros} eq 'ARRAY' ? $pg->{deprecated_macros} : [],
internal_debug_messages => ref $pg->{internal_debug_messages} eq 'ARRAY'
? $pg->{internal_debug_messages}
: [],
compute_time => logTimingInfo($beginTime, Benchmark->new),
};
}
sub logTimingInfo {
my ($beginTime, $endTime) = @_;
return Benchmark::timestr(Benchmark::timediff($endTime, $beginTime));
}
sub pretty_print_rh {
shift if UNIVERSAL::isa($_[0] => __PACKAGE__);
my $rh = shift;
return '' unless defined $rh;
my $indent = shift || 0;
my $out = '';
return $out if $indent > 10;
my $type = ref($rh);
if (defined($type) && $type) {
$out .= " type = $type; ";
} elsif (not defined($rh)) {
$out .= ' type = scalar; ';
}
if (ref $rh eq 'HASH' || eval { %$rh && 1 }) {
$out .= "{\n";
$indent++;
foreach my $key (sort keys %{$rh}) {
$out .= ' ' x $indent . "$key => " . pretty_print_rh($rh->{$key}, $indent) . "\n";
}
$indent--;
$out .= "\n" . ' ' x $indent . "}\n";
} elsif (ref($rh) =~ /ARRAY/ || "$rh" =~ /ARRAY/) {
$out .= ' ( ';
foreach my $elem (@{$rh}) {
$out .= pretty_print_rh($elem, $indent);
}
$out .= " ) \n";
} elsif (ref($rh) =~ /SCALAR/) {
$out .= 'scalar reference ' . ${$rh};
} elsif (ref($rh) =~ /Base64/) {
$out .= 'base64 reference ' . $$rh;
} else {
$out .= $rh;
}
return $out . ' ';
}
1;