-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathFormatRenderedProblem.pm
More file actions
350 lines (312 loc) · 13.4 KB
/
FormatRenderedProblem.pm
File metadata and controls
350 lines (312 loc) · 13.4 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
=head1 NAME
FormatRenderedProblem.pm
=cut
package WeBWorK::FormatRenderedProblem;
use strict;
use warnings;
use Mojo::JSON qw(encode_json);
use Mojo::Util qw(xml_escape);
use Mojo::DOM;
use Mojo::URL;
use WeBWorK::Localize;
use WeBWorK::Utils qw(getAssetURL);
use WeBWorK::Utils::LanguageAndDirection;
sub formatRenderedProblem {
my $c = shift;
my $rh_result = shift;
my $inputs_ref = $rh_result->{inputs_ref};
my $renderErrorOccurred = 0;
my $problemText = $rh_result->{text} // '';
$problemText .= $rh_result->{flags}{comment} if ($rh_result->{flags}{comment} && $inputs_ref->{showComments});
if ($rh_result->{flags}{error_flag}) {
$rh_result->{problem_result}{score} = 0; # force score to 0 for such errors.
$renderErrorOccurred = 1;
}
# TODO: add configuration to disable these overrides
my $SITE_URL = $inputs_ref->{baseURL} ? Mojo::URL->new($inputs_ref->{baseURL}) : $main::basehref;
my $FORM_ACTION_URL = $inputs_ref->{formURL} ? Mojo::URL->new($inputs_ref->{formURL}) : $main::formURL;
my $displayMode = $inputs_ref->{displayMode} // 'MathJax';
# HTML document language setting
my $formLanguage = $inputs_ref->{language} // 'en';
# Third party CSS
my @third_party_css = map { getAssetURL($formLanguage, $_->[0]) } (
[ 'css/bootstrap.css', ],
[ 'node_modules/jquery-ui-dist/jquery-ui.min.css', ],
['node_modules/@fortawesome/fontawesome-free/css/all.min.css'],
);
# Add CSS files requested by problems via ADD_CSS_FILE() in the PG file
# or via a setting of $ce->{pg}{specialPGEnvironmentVars}{extra_css_files}
# which can be set in course.conf (the value should be an anonomous array).
my @cssFiles;
# if (ref($ce->{pg}{specialPGEnvironmentVars}{extra_css_files}) eq 'ARRAY') {
# push(@cssFiles, { file => $_, external => 0 }) for @{ $ce->{pg}{specialPGEnvironmentVars}{extra_css_files} };
# }
if (ref($rh_result->{flags}{extra_css_files}) eq 'ARRAY') {
push @cssFiles, @{ $rh_result->{flags}{extra_css_files} };
}
my %cssFilesAdded; # Used to avoid duplicates
my @extra_css_files;
for (@cssFiles) {
next if $cssFilesAdded{ $_->{file} };
$cssFilesAdded{ $_->{file} } = 1;
if ($_->{external}) {
push(@extra_css_files, $_);
} else {
push(@extra_css_files, { file => getAssetURL($formLanguage, $_->{file}), external => 0 });
}
}
# Third party JavaScript
# The second element is a hash containing the necessary attributes for the script tag.
my @third_party_js = map { [ getAssetURL($formLanguage, $_->[0]), $_->[1] ] } (
[ 'node_modules/jquery/dist/jquery.min.js', {} ],
[ 'node_modules/jquery-ui-dist/jquery-ui.min.js', {} ],
[ 'node_modules/iframe-resizer/js/iframeResizer.contentWindow.min.js', {} ],
[ "js/apps/MathJaxConfig/mathjax-config.js", { defer => undef } ],
[ 'node_modules/mathjax/es5/tex-svg.js', { defer => undef, id => 'MathJax-script' } ],
[ 'node_modules/bootstrap/dist/js/bootstrap.bundle.min.js', { defer => undef } ],
[ "js/apps/Problem/problem.js", { defer => undef } ],
[ "js/apps/Problem/submithelper.js", { defer => undef } ],
[ "js/apps/CSSMessage/css-message.js", { defer => undef } ],
);
# Get the requested format. (outputFormat or outputformat)
my $formatName = $inputs_ref->{outputFormat} || $inputs_ref->{outputformat} || 'default';
# Add JS files requested by problems via ADD_JS_FILE() in the PG file.
my @extra_js_files;
if (ref($rh_result->{flags}{extra_js_files}) eq 'ARRAY') {
my %jsFiles;
for (@{ $rh_result->{flags}{extra_js_files} }) {
next if $jsFiles{ $_->{file} };
$jsFiles{ $_->{file} } = 1;
my %attributes = ref($_->{attributes}) eq 'HASH' ? %{ $_->{attributes} } : ();
if ($_->{external}) {
push(@extra_js_files, $_);
} else {
push(
@extra_js_files,
{
file => getAssetURL($formLanguage, $_->{file}),
external => 0,
attributes => $_->{attributes}
}
);
}
}
}
# Set up the problem language and direction
# PG files can request their language and text direction be set. If we do not have access to a default course
# language, fall back to the $formLanguage instead.
# TODO: support for right-to-left languages
my %PROBLEM_LANG_AND_DIR = get_problem_lang_and_dir($rh_result->{flags}, 'auto:en:ltr', $formLanguage);
my $PROBLEM_LANG_AND_DIR = join(' ', map {qq{$_="$PROBLEM_LANG_AND_DIR{$_}"}} keys %PROBLEM_LANG_AND_DIR);
# is there a reason this doesn't use the same button IDs?
my $previewMode = defined($inputs_ref->{previewAnswers}) || 0;
my $submitMode = defined($inputs_ref->{submitAnswers}) || $inputs_ref->{answersSubmitted} || 0;
my $showCorrectMode = defined($inputs_ref->{showCorrectAnswers}) || 0;
# A problemUUID should be added to the request as a parameter. It is used by PG to create a proper UUID for use in
# aliases for resources. It should be unique for a course, user, set, problem, and version.
my $problemUUID = $inputs_ref->{problemUUID} // '';
my $problemResult = $rh_result->{problem_result} // {};
my $showSummary = $inputs_ref->{showSummary} // 1;
my $showScoreSummary = $inputs_ref->{showScoreSummary} // 0;
# my $showAnswerNumbers = $inputs_ref->{showAnswerNumbers} // 0; # default no
# allow the request to hide the results table or messages
my $showTable = $inputs_ref->{hideAttemptsTable} ? 0 : 1;
my $showMessages = $inputs_ref->{hideMessages} ? 0 : 1;
# allow the request to override the display of partial correct answers
my $showPartialCorrectAnswers = $inputs_ref->{showPartialCorrectAnswers}
// $rh_result->{flags}{showPartialCorrectAnswers};
# Do not produce a result summary when we had a rendering error.
my $resultSummary = '';
if (!$renderErrorOccurred
&& $showSummary
&& !$previewMode
&& ($submitMode || $showCorrectMode)
&& $problemResult->{summary})
{
$resultSummary = $c->c(
$c->tag(
'h2',
class => 'fs-3 mb-2',
'Results for this submission'
)
. $c->tag('div', role => 'alert', $c->b($problemResult->{summary}))
)->join('');
}
# Answer hash in XML format used by the PTX format.
my $answerhashXML = '';
if ($formatName eq 'ptx') {
my $dom = Mojo::DOM->new->xml(1);
for my $answer (sort keys %{ $rh_result->{answers} }) {
$dom->append_content($dom->new_tag(
$answer,
map { $_ => ($rh_result->{answers}{$answer}{$_} // '') } keys %{ $rh_result->{answers}{$answer} }
));
}
$dom->wrap_content('<answerhashes></answerhashes>');
$answerhashXML = $dom->to_string;
}
# Make sure this is defined and is an array reference as saveGradeToLTI might add to it.
$rh_result->{debug_messages} = [] unless defined $rh_result && ref $rh_result->{debug_messages} eq 'ARRAY';
# Execute and return the interpolated problem template
# Raw format
# This format returns javascript object notation corresponding to the perl hash
# with everything that a client-side application could use to work with the problem.
# There is no wrapping HTML "_format" template.
if ($formatName eq 'raw') {
my $output = {};
# Everything that ships out with other formats can be constructed from these
$output->{rh_result} = $rh_result;
$output->{inputs_ref} = $inputs_ref;
# The following could be constructed from the above, but this is a convenience
# $output->{answerTemplate} = $answerTemplate->to_string if ($answerTemplate);
$output->{resultSummary} = $resultSummary->to_string if $resultSummary;
$output->{lang} = $PROBLEM_LANG_AND_DIR{lang};
$output->{dir} = $PROBLEM_LANG_AND_DIR{dir};
$output->{extra_css_files} = \@extra_css_files;
$output->{extra_js_files} = \@extra_js_files;
# Include third party css and javascript files. Only jquery, jquery-ui, mathjax, and bootstrap are needed for
# PG. See the comments before the subroutine definitions for load_css and load_js in pg/macros/PG.pl.
# The other files included are only needed to make themes work in the webwork2 formats.
$output->{third_party_css} = \@third_party_css;
$output->{third_party_js} = \@third_party_js;
# Convert to JSON and render.
return $c->render(data => encode_json($output));
}
# Setup and render the appropriate template in the templates/RPCRenderFormats folder depending on the outputformat.
# "ptx" has a special template. "json" uses the default json template. All others use the default html template.
my %template_params = (
template => $formatName eq 'ptx' ? 'RPCRenderFormats/ptx' : 'RPCRenderFormats/default',
$formatName eq 'json' ? (format => 'json') : (),
formatName => $formatName,
lh => WeBWorK::Localize::getLangHandle($inputs_ref->{language} // 'en'),
rh_result => $rh_result,
SITE_URL => $SITE_URL,
FORM_ACTION_URL => $FORM_ACTION_URL,
COURSE_LANG_AND_DIR => get_lang_and_dir($formLanguage),
PROBLEM_LANG_AND_DIR => $PROBLEM_LANG_AND_DIR,
third_party_css => \@third_party_css,
extra_css_files => \@extra_css_files,
third_party_js => \@third_party_js,
extra_js_files => \@extra_js_files,
problemText => $problemText,
extra_header_text => $inputs_ref->{extra_header_text} // '',
resultSummary => $resultSummary,
showSummary => $showSummary,
showScoreSummary => $submitMode && !$renderErrorOccurred && !$previewMode && $problemResult,
answerhashXML => $answerhashXML,
showPreviewButton => $inputs_ref->{hidePreviewButton} ? '0' : '',
showCheckAnswersButton => $inputs_ref->{hideCheckAnswersButton} ? '0' : '',
showCorrectAnswersButton => $inputs_ref->{showCorrectAnswersButton}
// ($inputs_ref->{isInstructor} ? '' : '0'),
showFooter => $inputs_ref->{showFooter} // '0',
pretty_print => \&pretty_print,
);
return $c->render(%template_params) if $formatName eq 'json';
$rh_result->{renderedHTML} = $c->render_to_string(%template_params)->to_string;
return $c->respond_to(
html => { text => $rh_result->{renderedHTML} },
json => {
json => jsonResponse(
$rh_result, $inputs_ref, @extra_css_files, @third_party_css, @extra_js_files, @third_party_js
)
},
);
}
sub jsonResponse {
my ($rh_result, $inputs_ref, @extra_files) = @_;
return {
(
$inputs_ref->{isInstructor}
? (
answers => $rh_result->{answers},
inputs => $inputs_ref,
pgcore => {
persist => $rh_result->{PERSISTENCE_HASH},
persist_up => $rh_result->{PERSISTENCE_HASH_UPDATED},
pgah => $rh_result->{PG_ANSWERS_HASH}
}
)
: ()
),
(
$inputs_ref->{includeTags}
? (tags => $rh_result->{tags}, raw_metadata_text => $rh_result->{raw_metadata_text})
: ()
),
renderedHTML => $rh_result->{renderedHTML},
debug => {
perl_warn => $rh_result->{WARNINGS},
pg_warn => $rh_result->{warning_messages},
debug => $rh_result->{debug_messages},
internal => $rh_result->{internal_debug_messages}
},
problem_result => $rh_result->{problem_result},
problem_state => $rh_result->{problem_state},
flags => $rh_result->{flags},
resources => {
regex => $rh_result->{pgResources},
alias => $rh_result->{resources},
assets =>
[ map { ref $_ eq 'HASH' ? "$_->{file}" : ref $_ eq 'ARRAY' ? "$_->[0]" : "$_" } @extra_files ],
},
JWT => {
problem => $inputs_ref->{problemJWT},
session => $rh_result->{sessionJWT},
answer => $rh_result->{answerJWT}
},
};
}
# Nice output for debugging
sub pretty_print {
my ($r_input, $level) = @_;
return 'undef' unless defined $r_input;
$level //= 4;
$level--;
return 'too deep' unless $level > 0; # Only print three levels of hashes (safety feature)
my $ref = ref($r_input);
if (!$ref) {
return xml_escape($r_input);
} elsif (eval { %$r_input && 1 }) {
# `eval { %$r_input && 1 }` will pick up all objects that can be accessed like a hash and so works better than
# `ref $r_input`. Do not use `"$r_input" =~ /hash/i` because that will pick up strings containing the word
# hash, and that will cause an error below.
my $out =
'<div style="display:table;border:1px solid black;background-color:#fff;">'
. ($ref eq 'HASH'
? ''
: '<div style="'
. 'display:table-caption;padding:3px;border:1px solid black;background-color:#fff;text-align:center;">'
. "$ref</div>")
. '<div style="display:table-row-group">';
for my $key (sort keys %$r_input) {
# Safety feature - we do not want to display the contents of %seed_ce which
# contains the database password and lots of other things, and explicitly hide
# certain internals of the CourseEnvironment in case one slips in.
next
if (($key =~ /database/)
|| ($key =~ /dbLayout/)
|| ($key eq "ConfigValues")
|| ($key eq "ENV")
|| ($key eq "externalPrograms")
|| ($key eq "permissionLevels")
|| ($key eq "seed_ce"));
$out .=
'<div style="display:table-row"><div style="display:table-cell;vertical-align:middle;padding:3px">'
. xml_escape($key)
. '</div>'
. qq{<div style="display:table-cell;vertical-align:middle;padding:3px">=></div>}
. qq{<div style="display:table-cell;vertical-align:middle;padding:3px">}
. pretty_print($r_input->{$key}, $level)
. '</div></div>';
}
$out .= '</div></div>';
return $out;
} elsif ($ref eq 'ARRAY') {
return '[ ' . join(', ', map { pretty_print($_, $level) } @$r_input) . ' ]';
} elsif ($ref eq 'CODE') {
return 'CODE';
} else {
return xml_escape($r_input);
}
}
1;