Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions lib/Value/AnswerChecker.pm
Original file line number Diff line number Diff line change
Expand Up @@ -441,21 +441,17 @@ sub ans_matrix {
my $named_ans_rule = pgRef('NAMED_ANS_RULE');
my $HTML = "";
pgCall('RECORD_IMPLICIT_ANS_NAME', $name = pgCall('NEW_ANS_NAME')) unless $name;
my $ename = "${answerPrefix}_${name}";
my $ename = "${answerPrefix}_${name}";
my $base_label = pgCall('generate_aria_label', $ename, $options{aria_label});
$self->{ans_name} = $ename;
$self->{ans_rows} = $rows;
$self->{ans_cols} = $cols;
# warn "ans_matrix: ename=$ename answer_group_name=$options{answer_group_name}";
my @array = ();
foreach my $i (0 .. $rows - 1) {
for my $i (0 .. $rows - 1) {
my @row = ();
foreach my $j (0 .. $cols - 1) {
my $label;
if ($options{aria_label}) {
$label = $options{aria_label} . pgCall('maketext', 'row [_1] col [_2] ', $i + 1, $j + 1);
} else {
$label = pgCall('generate_aria_label', ANS_NAME($ename, $i, $j));
}
for my $j (0 .. $cols - 1) {
my $label = $base_label . pgCall('maketext', 'row [_1] col [_2] ', $i + 1, $j + 1);
my $answer_group_name = $options{answer_group_name} // $name;
if ($i == 0 && $j == 0) {
if ($extend) {
Expand Down
17 changes: 11 additions & 6 deletions macros/core/PGML.pl
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ sub Image {
sub Answer {
my $self = shift;
my $token = shift;
my $def = { options => [ "answer", "width", "name", "cmp_options" ] };
my $def = { options => [ 'answer', 'width', 'name', 'cmp_options', 'aria_label' ] };
$def->{hasStar} = 1 if $token =~ m/\*$/;
$self->Item("answer", $token, $def);
}
Expand Down Expand Up @@ -1285,6 +1285,11 @@ sub string {
return join('', @strings);
}

sub aria_label {
my ($self, $label) = @_;
return $label ? (aria_label => "$label ") : ();
}

sub nl {
my $self = shift;
my $nl = $self->{nl};
Expand Down Expand Up @@ -1373,10 +1378,10 @@ sub Answer {
if (defined($ans)) {
if (ref($ans) eq 'CODE' || (ref($ans) eq 'AnswerEvaluator' && !Value::isValue($ans->{rh_ans}{correct_value}))) {
if (defined($item->{name})) {
$rule = main::NAMED_ANS_RULE($item->{name}, $item->{width});
$rule = main::NAMED_ANS_RULE($item->{name}, $item->{width}, $self->aria_label($item->{aria_label}));
main::NAMED_ANS($item->{name} => $ans);
} else {
$rule = main::ans_rule($item->{width});
$rule = main::ans_rule($item->{width}, $self->aria_label($item->{aria_label}));
main::ANS($ans);
}
} else {
Expand All @@ -1398,7 +1403,7 @@ sub Answer {
$ans = main::String(""); ### use something else?
}
}
my @options = ($item->{width});
my @options = ($item->{width}, $self->aria_label($item->{aria_label}));
my $method = ($item->{hasStar} ? "ans_array" : "ans_rule");
if ($item->{name}) {
unshift(@options, $item->{name});
Expand Down Expand Up @@ -1426,9 +1431,9 @@ sub Answer {
}
} else {
if (defined($item->{name})) {
$rule = main::NAMED_ANS_RULE($item->{name}, $item->{width});
$rule = main::NAMED_ANS_RULE($item->{name}, $item->{width}, $self->aria_label($item->{aria_label}));
} else {
$rule = main::ans_rule($item->{width});
$rule = main::ans_rule($item->{width}, $self->aria_label($item->{aria_label}));
}
}
return $rule;
Expand Down
88 changes: 46 additions & 42 deletions macros/core/PGbasicmacros.pl
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ sub NAMED_ANS_RULE {
size => $col,
name => $name,
id => $name,
aria_label => $options{aria_label} // generate_aria_label($name),
aria_label => generate_aria_label($name, $options{aria_label}),
dir => 'auto',
autocomplete => 'off',
autocapitalize => 'off',
Expand Down Expand Up @@ -385,7 +385,7 @@ sub NAMED_ANS_RULE_EXTENSION {
size => $col,
name => $name,
id => $name,
aria_label => $options{aria_label} // generate_aria_label($name),
aria_label => generate_aria_label($name, $options{aria_label}),
dir => 'auto',
autocomplete => 'off',
autocapitalize => 'off',
Expand All @@ -402,7 +402,7 @@ sub ANS_RULE {
my ($number, $col) = @_;
my $name = NEW_ANS_NAME();
RECORD_IMPLICIT_ANS_NAME($name);
return NAMED_ANS_RULE($name, $col);
return NAMED_ANS_RULE($name, $col, @_);
}

sub NAMED_ANS_BOX {
Expand All @@ -413,7 +413,6 @@ sub NAMED_ANS_BOX {
my $height = .07 * $row;
my $answer_value = $inputs_ref->{$name} // '';
$name = RECORD_ANS_NAME($name, $answer_value);
my $label = $options{aria_label} // generate_aria_label($name);

return MODES(
TeX => qq!\\vskip $height in \\hrulefill\\quad !,
Expand All @@ -426,7 +425,7 @@ sub NAMED_ANS_BOX {
id => $name,
rows => $row,
cols => $col,
aria_label => $label,
aria_label => generate_aria_label($name, $options{aria_label}),
encode_pg_and_html($answer_value)
)
)
Expand Down Expand Up @@ -454,10 +453,11 @@ sub NAMED_ANS_RADIO {
'label',
tag(
'input',
type => 'radio',
name => $name,
id => $name,
value => $value,
type => 'radio',
name => $name,
id => $name,
value => $value,
aria_label => generate_aria_label($name, $options{aria_label}),
$checked ? (checked => undef) : (),
%{ $options{attributes} }
)
Expand Down Expand Up @@ -485,10 +485,11 @@ sub NAMED_ANS_RADIO_EXTENSION {
'label',
tag(
'input',
type => 'radio',
name => $name,
id => $options{id} // "${name}_$value",
value => $value,
type => 'radio',
name => $name,
id => $options{id} // "${name}_$value",
value => $value,
aria_label => generate_aria_label($name, $options{aria_label}),
$checked ? (checked => undef) : (),
%{ $options{attributes} }
)
Expand All @@ -508,51 +509,56 @@ sub NAMED_ANS_RADIO_BUTTONS {
while (@buttons) {
$value = shift @buttons;
$tag = shift @buttons;
push(@out, NAMED_ANS_RADIO_EXTENSION($name, $value, $tag, aria_label => $label . "option $count "));
push(@out,
NAMED_ANS_RADIO_EXTENSION($name, $value, $tag, aria_label => $label . maketext('option [_1] ', $count))
);
$count++;
}
return wantarray ? @out : join(" ", @out);
}

##############################################
# generate_aria_label( $name )
# takes the name of an ANS_RULE and generates an appropriate
# aria label for screen readers
# generate_aria_label($name, $inLabel)
# Takes the name of an ANS_RULE and generates an appropriate aria label for screen readers.
##############################################

sub generate_aria_label {
my $name = shift;
my $label = '';
my ($name, $inLabel) = @_;
my $outLabel = '';

# if we dont have an AnSwEr type name then we do the best we can
# Return the input label unless it contains '%s'.
if ($inLabel) {
if ($inLabel =~ /^\S$/) {
$inLabel = '';
} else {
$inLabel = "$inLabel " unless $inLabel =~ / $/;
return $inLabel unless $inLabel =~ /%s/;
}
}

# If we don't have an AnSwEr type name then we do the best we can.
if ($name !~ /AnSwEr\d+/) {
return maketext('answer [_1] ', $name);
$outLabel = maketext('answer [_1] ', $name);
return $inLabel ? $inLabel =~ s/%s/$outLabel/r : $outLabel;
}

# check for quiz prefix
# Check for quiz prefix.
if ($name =~ /^Q\d+/ || $name =~ /^MaTrIx_Q\d+/) {
$name =~ s/Q0*(\d+)_//;
$label .= maketext('problem [_1] ', $1);
$outLabel .= maketext('problem [_1] ', $1);
}

# get answer number
# Get answer number.
$name =~ /AnSwEr0*(\d+)/;
$label .= maketext('answer [_1] ', $1);
$outLabel .= maketext('answer [_1] ', $1);

# check for Multianswer
# Check for Multianswer.
if ($name =~ /MuLtIaNsWeR_/) {
$name =~ s/MuLtIaNsWeR_//;
$name =~ /AnSwEr(\d+)_(\d+)/;
$label .= maketext('part [_1] ', $2 + 1);
$outLabel .= maketext('part [_1] ', $2 + 1);
}

# check for Matrix
if ($name =~ /^MaTrIx_/) {
$name =~ /_(\d+)_(\d+)$/;
$label .= maketext('row [_1] column [_2] ', $1 + 1, $2 + 1);
}

return $label;
return $inLabel ? $inLabel =~ s/%s/$outLabel/r : $outLabel;
}

##############################################
Expand Down Expand Up @@ -609,7 +615,7 @@ sub NAMED_ANS_CHECKBOX {
type => 'checkbox',
name => $name,
id => $name,
aria_label => $options{aria_label} // (generate_aria_label($name) . maketext('option [_1] ', 1)),
aria_label => generate_aria_label($name, $options{aria_label}),
value => $value,
$checked ? (checked => undef) : (),
%{ $options{attributes} }
Expand Down Expand Up @@ -640,8 +646,8 @@ sub NAMED_ANS_CHECKBOX_OPTION {
'input',
type => 'checkbox',
name => $name,
id => $options{id} // "${name}_$value",
aria_label => $options{aria_label} // generate_aria_label($name),
id => $options{id} // "${name}_$value",
aria_label => generate_aria_label($name, $options{aria_label}),
value => $value,
$checked ? (checked => undef) : (),
%{ $options{attributes} }
Expand Down Expand Up @@ -675,7 +681,7 @@ sub ans_rule {
my $len = shift;
my $name = NEW_ANS_NAME();
RECORD_IMPLICIT_ANS_NAME($name);
return NAMED_ANS_RULE($name, $len || 20);
return NAMED_ANS_RULE($name, $len || 20, @_);
}

sub ans_radio_buttons {
Expand Down Expand Up @@ -832,8 +838,6 @@ sub NAMED_ANS_ARRAY_EXTENSION {
$answer_value = '' unless defined($answer_value);
}

my $label = $options{aria_label} // generate_aria_label($name);

# the name of the answer evaluator controlling this collection of responses.
my $answer_group_name;

Expand Down Expand Up @@ -869,7 +873,7 @@ sub NAMED_ANS_ARRAY_EXTENSION {
name => $name,
id => $name,
class => 'codeshard',
aria_label => $label,
aria_label => generate_aria_label($name, $options{aria_label}),
autocomplete => 'off',
autocapitalize => 'off',
spellcheck => 'false',
Expand Down
2 changes: 1 addition & 1 deletion macros/core/PGessaymacros.pl
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ sub NAMED_ESSAY_BOX {
%html_options,
name => $name,
id => $name,
aria_label => generate_aria_label($name),
aria_label => generate_aria_label($name, $html_options{aria_label}),
rows => $row,
cols => $col,
class => 'latexentryfield',
Expand Down
2 changes: 1 addition & 1 deletion macros/parsers/parserCheckboxList.pl
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ sub CHECKS {

my @checks;
main::RECORD_IMPLICIT_ANS_NAME($name = main::NEW_ANS_NAME()) unless $name;
my $label = (delete $options{aria_label}) // main::generate_aria_label($name);
my $label = main::generate_aria_label($name, delete $options{aria_label});

for my $i (0 .. $#{ $self->{orderedChoices} }) {
my $value = $self->{values}[$i];
Expand Down
23 changes: 11 additions & 12 deletions macros/parsers/parserMultiAnswer.pl
Original file line number Diff line number Diff line change
Expand Up @@ -422,26 +422,26 @@ sub NEW_NAME {
# by the settings of the MultiAnswer.
#
sub ans_rule {
my $self = shift;
my $size = shift || 20;
my ($self, $size, %options) = @_;
$size ||= 20;
my $data = $self->{data}[ $self->{part} ];
my $name = $self->ANS_NAME($self->{part}++);
if ($self->{singleResult} && $self->{part} == 1) {
my $label = main::generate_aria_label($answerPrefix . $name . "_0");
main::RECORD_IMPLICIT_ANS_NAME($name) unless $self->{namedRules};
return $data->named_ans_rule($name, $size, @_, aria_label => $label);
return $data->named_ans_rule($name, $size, %options,
aria_label => main::generate_aria_label($answerPrefix . $name . '_0', $options{aria_label}));
}
if ($self->{singleResult} && $self->{part} > 1) {
my $extension_ans_rule = $data->named_ans_rule_extension(
$name, $size,
answer_group_name => $self->{answerNames}{0},
@_
%options
);
# warn "extension rule created: $extension_ans_rule for ", ref($data);
return $extension_ans_rule;
} else {
main::RECORD_IMPLICIT_ANS_NAME($name) unless $self->{namedRules};
return $data->named_ans_rule($name, $size, @_);
return $data->named_ans_rule($name, $size, %options);
}
}

Expand All @@ -451,30 +451,29 @@ sub ans_rule {
# Reset the correct_ans once the array is made
#
sub ans_array {
my $self = shift;
my $size = shift || 5;
my ($self, $size, %options) = @_;
$size ||= 5;
my $HTML;
my $data = $self->{data}[ $self->{part} ];
my $name = $self->ANS_NAME($self->{part}++);
if ($self->{singleResult} && $self->{part} == 1) {
my $label = main::generate_aria_label($answerPrefix . $name . "_0");
main::RECORD_IMPLICIT_ANS_NAME($name) unless $self->{namedRules};
return $data->named_ans_array(
$name, $size,
answer_group_name => $self->{answerNames}{0},
@_, aria_label => $label
%options, aria_label => main::generate_aria_label($answerPrefix . $name . '_0', $options{aria_label})
);
}
if ($self->{singleResult} && $self->{part} > 1) {
$HTML = $data->named_ans_array_extension(
$self->NEW_NAME($name), $size,
answer_group_name => $self->{answerNames}{0},
@_
%options
);
# warn "array extension rule created: $HTML for ", ref($data);
} else {
main::RECORD_IMPLICIT_ANS_NAME($name) unless $self->{namedRules};
$HTML = $data->named_ans_array($name, $size, @_);
$HTML = $data->named_ans_array($name, $size, %options);
}
$self->{cmp}[ $self->{part} - 1 ] = $data->cmp(@ans_defaults);
return $HTML;
Expand Down
6 changes: 3 additions & 3 deletions macros/parsers/parserPopUp.pl
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ sub MENU {
my $menu = "";
main::RECORD_IMPLICIT_ANS_NAME($name = main::NEW_ANS_NAME()) unless $name;
my $answer_value = (defined($main::inputs_ref->{$name}) ? $main::inputs_ref->{$name} : '');
my $aria_label = $options{aria_label} // main::generate_aria_label($name);
my $aria_label = main::generate_aria_label($name, $options{aria_label});

if ($main::displayMode =~ m/^HTML/) {
if ($self->{useHTMLSelect}) {
Expand Down Expand Up @@ -457,8 +457,8 @@ sub MENU {
data_feedback_insert_method => 'append_content',
join(
'',
main::tag('input', type => 'hidden', name => $name, value => $answer_value),
main::tag('span', class => 'visually-hidden', $aria_label),
main::tag('input', type => 'hidden', name => $name, value => $answer_value),
main::tag('span', class => 'visually-hidden', aria_label => $aria_label),
main::tag(
'button',
class => 'btn dropdown-toggle text-nowrap ',
Expand Down
Loading