--- loncom/homework/radiobuttonresponse.pm 2008/12/11 03:29:30 1.134.2.1 +++ loncom/homework/radiobuttonresponse.pm 2013/04/30 03:03:34 1.157 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # mutliple choice style responses # -# $Id: radiobuttonresponse.pm,v 1.134.2.1 2008/12/11 03:29:30 raeburn Exp $ +# $Id: radiobuttonresponse.pm,v 1.157 2013/04/30 03:03:34 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -25,20 +25,30 @@ # http://www.lon-capa.org/ # + + package Apache::radiobuttonresponse; use strict; use HTML::Entities(); use Apache::lonlocal; use Apache::lonnet; use Apache::response; +use Apache::caparesponse; my $default_bubbles_per_line = 10; +my @alphabet = ( 'A' .. 'Z' ); # Foil labels. + + BEGIN { &Apache::lonxml::register('Apache::radiobuttonresponse',('radiobuttonresponse')); } +#--------------------------------------------------------------------------- +# +# Generic utility subs. + sub bubble_line_count { my ($numfoils, $bubbles_per_line) = @_; my $bubble_lines; @@ -51,6 +61,10 @@ sub bubble_line_count { } + +#------------------------------------------------------------------------------ +# +# XML handlers. sub start_radiobuttonresponse { my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_; my $result; @@ -65,7 +79,7 @@ sub start_radiobuttonresponse { if ($target eq 'meta') { $result=&Apache::response::meta_package_write('radiobuttonresponse'); } elsif ($target eq 'edit' ) { - $result.=&Apache::edit::start_table($token) + $result.=&Apache::edit::start_table($token) .'
";
- } else {
- if ($target eq 'tex') {
- $result .= '\item \vskip -2mm ';
- } else {
- $result.=" "; - } - } - if (defined($lastresponse{$name})) { - if ($target eq 'tex') { - $result .= '}'; - } else { - $result.=''; - } - } - $result .= $Apache::response::foilgroup{$name.'.text'}; - if (defined($lastresponse{$name}) && ($target ne 'tex')) { - $result.=''; - } - if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.=" | "; }
+ return \%lastresponse;
+
+}
+##
+# Removes the names from a foil group that are marked as unused.
+#
+# @param $names - reference to the array of names to filter.
+#
+# @return arrayref
+# @retval reference to the filtered array.
+#
+sub remove_unused {
+ my ($names) = @_;
+ my @result;
+
+ foreach my $name (@{$names}) {
+ if ($Apache::response::foilgroup{$name . '.value'} ne 'unused') {
+ push(@result, $name);
+ }
+ }
+ return \@result;
+}
+##
+# Displays all foils in a survey type problem for HTML rendition.
+# TODO: See if there is any logic in this sub that can be shared
+# with display_foils_html
+#
+# @param $names - ref to array of names of the foils to display.
+# @param $part - Problem part number.
+# @param $showanswer - If true, show the answers.
+# @param $lastresponse - Ref to the last response hash.
+# @param $direction - Display direction of the radiobuttons.
+#
+# @return string
+# @retval HTML required to display the resource in a browser.
+#
+sub display_survey_html {
+ my ($names, $part, $showanswer, $lastresponse, $direction) = @_;
+ my $result;
+
+ # Figure out a few fragments of html that depend onthe
+ # orientation of the radiobuttons:
+ # closing_html - HTML to emit at the end of the resource.
+ # pre_foil - HTML to emit prior to each foil.
+ # post_foil - HTML to emit following each foil.
+ #
+ # The opening HTML is just added to the $result now
+ #
+ # Figuring these outin advance compresses the loop over foils into something
+ # pretty simple:
+ #
+ # NOTE: There's probably a really cool way to do this with style sheets
+ # and picking the selector based on the orientation, if someone wants to puzzle
+ # that out. In that case, probably the whole thing lives in a ";
- } else {
- if ($target eq 'tex') {
- $result .= '\item \vskip -2mm ';
- } else {
- $result.=" "; - } - } - if ($target eq 'tex') { - $result .= '$\bigcirc$'.$Apache::response::foilgroup{$name.'.text'}.'\\\\'; #' stupid emacs - $i++; - } else { - $result .= ' | "; }
- } else {
- $result.='\vskip 0 mm ';
- }
+
+ $result .= '\vskip 0 mm ';
}
- }
+ $result .= "\\end{$venv}";
+ }
}
-
- if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.='
'; }
- }
- if ($target ne 'tex') {
- $result.=" "; - } else { - $result.='\item \vskip -2 mm '; - } - if ($Apache::response::foilgroup{$name.'.value'} eq 'true') { - if ($target ne 'tex') { - $result.=&mt('Correct:').''; - } else { - $result.=&mt('Correct:').' \textbf{'; - } + + # if the answers get shown, we need to label each item as correct or + # incorrect. + + my ($opening_html, $finalclose, $item_pretext, $item_posttext) = + &html_direction_fragments($direction); + + $result .= $opening_html; + + + if ($show_answer) { + + foreach my $name (@{$whichfoils}) { + + # If the item gets further surrounded by tags, this + # holds the closures for those tages. + + my $item_closetag = ''; + + $result .= $item_pretext; + + # Label each foil as correct or incorrect: + + if ($Apache::response::foilgroup{$name . '.value'} eq 'true') { + $result .= &mt('Correct:') . ''; + $item_closetag .= ''; + } else { - $result.=&mt('Incorrect:'); - } - if ($target eq 'web') { $result.=""; } - if ($Apache::response::foilgroup{$name.'.value'} eq 'true') { - if ($target ne 'tex') { $result.='';} else {$result.='}';} + $result .= &mt('Incorrect'); } - if ($direction eq 'horizontal') { - if ($target ne 'tex') { $result.=' | '; }
+
+ # Web rendition encloses the
+ # item text in a label tag as well:
+
+ if ($target eq 'web') {
+ $result .= '' . $item_closetag;
}
+ $result .= $Apache::response::foilgroup{$name . '.text'};
+ $result .= $item_closetag;
+ $result .= $item_posttext;
+ $result .= "\n"; # make the html a bit more readable.
}
- if ($direction eq 'horizontal') {
- if ($target ne 'tex') {
- $result.='
";
- } else {
- $result.=" "; - } - } - if ($target ne 'tex') { - $result.= ' | ";
- }
- $temp++;
- }
- if ($target ne 'tex' && $direction eq 'horizontal') {
- $result.="