Skip to content

Commit

Permalink
Merge pull request openwebwork#2397 from drgrice1/pgml-lab-no-link
Browse files Browse the repository at this point in the history
Make the PGLM lab problem open via the `render_rpc` endpoint.
  • Loading branch information
Alex-Jordan authored Apr 13, 2024
2 parents 50e2d2f + 7dd127d commit eaf3bd6
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 35 deletions.
67 changes: 60 additions & 7 deletions assets/pg/PGMLLab/PGML-lab.pg
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ DOCUMENT();

loadMacros('PGstandard.pl', 'PGML.pl', 'PGcourse.pl');

# Hide things that are appropriate for regular problem use
HEADER_TEXT(tag(
'style',
'#page-title, #message, #message_bottom, #custom_edit_message, #breadcrumb-navigation, #score_summary, #problemFooter {display: none}'
));

sub EscapeHTML {
my $s = shift;
$s =~ s/&/~~&/g;
Expand Down Expand Up @@ -467,6 +461,64 @@ TEXT(tag(
ENDPGML
]
],
[
'Tags' => [
<<~ 'END_PG',
loadMacros('parserMultiAnswer.pl', 'parserPopUp.pl');
$ma = MultiAnswer(DropDown([ [ 'minimum', 'maximum' ] ], 1), 4)->with(
singleResult => 1,
checker => sub {
my ($cor, $stu) = @_;
return $cor->[0] == $stu->[0] && $cor->[1] == $stu->[1]
? 1
: 0;
}
);
END_PG
<<~'ENDPGML'
A tag is a "div" by default. HTML attributes can be set via an array in the
first option. The only other allowed tag type is a span. To switch to a
span add 'span' to the beginning of the attribute array. The second tex
option and the third ptx option are used to set the output for the TeX and
PTX display modes. The format of the tex option is an array containing two
strings. The first string is the TeX code to insert before the content,
and the second the TeX code to insert after the content. The format of the
ptx option is similar to the format for the first html. It is an array with
the tag name (required), optionally followed by an array of attributes, and
optionally a separator.

[<Dangerous content.>]{
[ 'span', class => 'p-1 alert alert-danger', role => 'alert' ]
}{
[ '{\color{red}', '}' ]
}{
['alert']
}

Tags may contain other PGML content and answers. Note that a span tag may
not contain new lines, tables, or anything else that would be invalid in a
span. Basically only text elements are valid. However, div tags may
contain pretty much anything.

[<
This is an equation [`x + 3 = 5`].

The solution to the above equation is [_]{2}.
>]{ [ style => 'border: 1px solid black; padding: 1rem;' ] }

One useful application is when using the parserMultiAnswer.pl macro with
singleResult answers. Wrap the answers in in a div tag with the
"ww-feedback-container" class to tell PG where to place the feedback
button. The feedback button will be placed at the end of the containing div
tag.

[<
The [_]{$ma} value of [`f(x)`] is [_]{$ma} for the function
[`f(x) = 4 - x^2`].
>]{ [ class => 'ww-feedback-container' ] }
ENDPGML
]
],
),
Examples(
'Inline formatting',
Expand Down Expand Up @@ -981,7 +1033,8 @@ TEXT(tag(
'[@ perl-command @]* (no escaping)',
'[@ perl-command @]** (parse results)',
'[% comment %]',
'[&lt;url&gt;] (not implemented)',
'[&lt;tag&gt;]{html}{tex}{ptx}',
'[[url]] (not implemented)',
'[!image!]{source}{width}{height}',
),
Menu(
Expand Down
1 change: 0 additions & 1 deletion courses.dist/modelCourse/templates/PGMLLab

This file was deleted.

File renamed without changes
34 changes: 33 additions & 1 deletion htdocs/js/PGProblemEditor/pgproblemeditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,6 @@
/text\/html/.test(response.headers.get('content-type'))
) {
throw await response.text();
return;
}

const data = await response.blob();
Expand Down Expand Up @@ -564,4 +563,37 @@
rendering = false;
}
};

const pgmlLabButton = document.getElementById('pgml-lab');
pgmlLabButton?.addEventListener('click', () => {
const form = document.createElement('form');
form.style.display = 'none';
form.target = 'PGML';
form.action = renderURL;
form.method = 'post';

const inputs = [
['courseID', document.getElementsByName('courseID')[0]?.value],
['displayMode', document.getElementById('action_view_displayMode_id')?.value ?? 'MathJax'],
['fileName', 'PGMLLab/PGML-lab.pg'],
['uriEncodedProblemSource', pgmlLabButton.dataset.source]
];

const user = document.getElementsByName('user')[0];
if (user) inputs.push(['user', user.value]);
const sessionKey = document.getElementsByName('key')[0];
if (sessionKey) inputs.push(['key', sessionKey.value]);

for (const [name, value] of inputs) {
const input = document.createElement('input');
input.name = name;
input.value = value;
input.type = 'hidden';
form.append(input);
}

document.body.append(form);
form.submit();
form.remove();
});
})();
1 change: 1 addition & 0 deletions lib/FormatRenderedProblem.pm
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ sub formatRenderedProblem {
LTIGradeMessage => $LTIGradeMessage,
sourceFilePath => $ws->{inputs_ref}{sourceFilePath} // '',
problemSource => $ws->{inputs_ref}{problemSource} // '',
rawProblemSource => $ws->{inputs_ref}{rawProblemSource} // '',
uriEncodedProblemSource => $ws->{inputs_ref}{uriEncodedProblemSource} // '',
fileName => $ws->{inputs_ref}{fileName} // '',
formLanguage => $formLanguage,
Expand Down
3 changes: 2 additions & 1 deletion lib/WeBWorK/ContentGenerator/RenderViaRPC.pm
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ async sub pre_header_initialize ($c) {
return;
}

$c->param('displayMode', 'tex') if ($c->param('outputformat') eq 'pdf' || $c->param('outputformat') eq 'tex');
$c->param('displayMode', 'tex')
if $c->param('outputformat') && ($c->param('outputformat') eq 'pdf' || $c->param('outputformat') eq 'tex');

# Call the WebworkWebservice to render the problem and store the result in $c->return_object.
my $rpc_service = WebworkWebservice->new($c);
Expand Down
3 changes: 2 additions & 1 deletion lib/WebworkWebservice.pm
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ otherwise.
sub formatRenderedProblem {
my $self = shift;
return HardcopyRenderedProblem::hardcopyRenderedProblem($self)
if $self->{inputs_ref}{outputformat} eq 'tex' || $self->{inputs_ref}{outputformat} eq 'pdf';
if $self->{inputs_ref}{outputformat}
&& ($self->{inputs_ref}{outputformat} eq 'tex' || $self->{inputs_ref}{outputformat} eq 'pdf');
return FormatRenderedProblem::formatRenderedProblem($self);
}

Expand Down
8 changes: 3 additions & 5 deletions lib/WebworkWebservice/RenderProblem.pm
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ use Mojo::Util qw(url_unescape);

use WeBWorK::Debug;
use WeBWorK::CourseEnvironment;
use WeBWorK::PG::IO;
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;
Expand Down Expand Up @@ -190,17 +190,15 @@ async sub renderProblem {
if ($rh->{problemSource}) {
$r_problem_source = \(decode_utf8_base64($rh->{problemSource}) =~ tr/\r/\n/r);
$problemRecord->source_file(defined $rh->{fileName} ? $rh->{fileName} : $rh->{sourceFilePath});
} elsif (defined $rh->{rawProblemSource}) {
} elsif ($rh->{rawProblemSource}) {
$r_problem_source = \$rh->{rawProblemSource};
$problemRecord->source_file(defined $rh->{fileName} ? $rh->{fileName} : $rh->{sourceFilePath});
} elsif ($rh->{uriEncodedProblemSource}) {
$r_problem_source = \(url_unescape($rh->{uriEncodedProblemSource}));
$problemRecord->source_file(defined $rh->{fileName} ? $rh->{fileName} : $rh->{sourceFilePath});
} elsif (defined $rh->{sourceFilePath} && $rh->{sourceFilePath} =~ /\S/) {
$problemRecord->source_file($rh->{sourceFilePath});
$r_problem_source =
\(WeBWorK::PG::IO::read_whole_file($ce->{courseDirs}{templates} . '/' . $rh->{sourceFilePath}));
$problemRecord->source_file('RenderProblemFooBar') unless defined($problemRecord->source_file);
$r_problem_source = \(readFile($ce->{courseDirs}{templates} . '/' . $rh->{sourceFilePath}));
}

if ($UNIT_TESTS_ON) {
Expand Down
30 changes: 12 additions & 18 deletions templates/ContentGenerator/Instructor/PGProblemEditor.html.ep
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
% use Mojo::URL;
% use Mojo::Util qw(url_escape);
%
% use WeBWorK::Utils qw(not_blank x getAssetURL);
% use WeBWorK::Utils::Files qw(readFile);
% use WeBWorK::Utils::Sets qw(format_set_name_display);
% use WeBWorK::HTML::CodeMirrorEditor
% qw(generate_codemirror_html generate_codemirror_controls_html output_codemirror_static_files);
Expand Down Expand Up @@ -114,24 +116,16 @@
class => 'reference-link btn btn-sm btn-info',
data => { bs_toggle => 'tooltip', bs_placement => 'top' } =%>
% # PGML lab problem rendered as an unattached problem in a new window.
% if (-e "$ce->{courseDirs}{templates}/PGMLLab/PGML-lab.pg") {
<%= link_to maketext('PGML') => $c->systemLink(
url_for('problem_detail', setID => 'Undefined_Set', problemID => 1),
params => {
displayMode => $ce->{pg}{options}{displayMode},
problemSeed => 1234,
editMode => 'temporaryFile',
sourceFilePath => 'PGMLLab/PGML-lab.pg'
}
),
target => 'PGML',
title => maketext(
'PG markdown syntax used to format WeBWorK questions. '
. 'This interactive lab can help you to learn the techniques.'
),
class => 'reference-link btn btn-sm btn-info',
data => { bs_toggle => 'tooltip', bs_placement => 'top' } =%>
% }
% my $pgml_lab_source = readFile("$ce->{webworkDirs}{assets}/pg/PGMLLab/PGML-lab.pg");
<%= tag 'button', type => 'button',
title => maketext(
'PG markdown syntax used to format WeBWorK questions. '
. 'This interactive lab can help you to learn the techniques.'
),
id => 'pgml-lab',
class => 'reference-link btn btn-sm btn-info',
data => { source => url_escape($pgml_lab_source), bs_toggle => 'tooltip', bs_placement => 'top' },
'PGML' =%>
% # http://webwork.maa.org/wiki/Category:Authors
<%= link_to maketext('Author Info') => $ce->{webworkURLs}{AuthorHelpURL},
target => 'author_info',
Expand Down
3 changes: 2 additions & 1 deletion templates/RPCRenderFormats/default.html.ep
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,12 @@
%
%= hidden_field sourceFilePath => $sourceFilePath
%= hidden_field problemSource => $problemSource
%= hidden_field rawProblemSource => $rawProblemSource
%= hidden_field uriEncodedProblemSource => $uriEncodedProblemSource
%= hidden_field problemSeed => $problemSeed
%= hidden_field problemUUID => $problemUUID
%= hidden_field psvn => $psvn
%= hidden_field pathToProblemFile => $fileName
%= hidden_field fileName => $fileName
%= hidden_field courseID => $courseID
%= hidden_field user => $user
%= hidden_field passwd => $passwd
Expand Down

0 comments on commit eaf3bd6

Please sign in to comment.