Annotation of loncom/homework/radiobuttonresponse.pm, revision 1.161

1.22      albertel    1: # The LearningOnline Network with CAPA
                      2: # mutliple choice style responses
1.31      albertel    3: #
1.161   ! raeburn     4: # $Id: radiobuttonresponse.pm,v 1.160 2016/01/22 22:42:51 damieng Exp $
1.31      albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
1.130     foxr       21: # along with LON-CAPA; if not, write to the Free Software# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1.31      albertel   22: #
                     23: # /home/httpd/html/adm/gpl.txt
                     24: #
                     25: # http://www.lon-capa.org/
                     26: #
1.1       albertel   27: 
1.154     raeburn    28: 
                     29: 
1.1       albertel   30: package Apache::radiobuttonresponse;
                     31: use strict;
1.42      albertel   32: use HTML::Entities();
1.85      albertel   33: use Apache::lonlocal;
1.100     albertel   34: use Apache::lonnet;
1.115     foxr       35: use Apache::response;
1.155     foxr       36: use Apache::caparesponse;
1.1       albertel   37: 
1.120     foxr       38: my $default_bubbles_per_line = 10;
1.155     foxr       39: my @alphabet      = ( 'A' .. 'Z' ); # Foil labels.
                     40: 
                     41: 
1.121     foxr       42: 
1.154     raeburn    43: 
1.36      harris41   44: BEGIN {
1.154     raeburn    45:     &Apache::lonxml::register('Apache::radiobuttonresponse',('radiobuttonresponse'));
1.1       albertel   46: }
                     47: 
1.155     foxr       48: #---------------------------------------------------------------------------
                     49: #
                     50: #  Generic utility subs.
                     51: 
1.121     foxr       52: sub bubble_line_count {
1.154     raeburn    53:     my ($numfoils, $bubbles_per_line) = @_;
1.121     foxr       54:     my $bubble_lines;
1.154     raeburn    55:     $bubble_lines = int($numfoils / $bubbles_per_line);
                     56:     if (($numfoils % $bubbles_per_line) != 0) {
                     57: 	$bubble_lines++;
1.121     foxr       58:     }
                     59:     return $bubble_lines;
1.154     raeburn    60:     
                     61: }
1.153     foxr       62: 
1.121     foxr       63: 
1.155     foxr       64: 
                     65: #------------------------------------------------------------------------------
                     66: #
                     67: #  XML handlers.
1.1       albertel   68: sub start_radiobuttonresponse {
1.154     raeburn    69:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.83      albertel   70:     my $result;
1.115     foxr       71: 
1.83      albertel   72:     #when in a radiobutton response use these
1.154     raeburn    73:     &Apache::lonxml::register('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));
                     74:     push (@Apache::lonxml::namespace,'radiobuttonresponse');
                     75:     my $id = &Apache::response::start_response($parstack,$safeeval);
1.120     foxr       76: 
1.154     raeburn    77:     %Apache::hint::radiobutton=();
1.85      albertel   78:     undef(%Apache::response::foilnames);
1.154     raeburn    79:     if ($target eq 'meta') {
                     80: 	$result=&Apache::response::meta_package_write('radiobuttonresponse');
                     81:     } elsif ($target eq 'edit' ) {
                     82: 	$result.=&Apache::edit::start_table($token)
1.158     golterma   83:            .'<tr><td>'.&Apache::loncommon::insert_folding_button()
                     84:            .&Apache::lonxml::description($token)
1.154     raeburn    85:            .&Apache::loncommon::help_open_topic('Radio_Response_Problems')
                     86:            .'</td>'
                     87:            .'<td><span class="LC_nobreak">'.&mt('Delete?').' '
                     88:            .&Apache::edit::deletelist($target,$token)
                     89:            .'</span></td>'
                     90:            .'<td>&nbsp;'.&Apache::edit::end_row()
                     91:            .&Apache::edit::start_spanning_row();
                     92: 	$result.=
                     93: 	    &Apache::edit::text_arg('Max Number Of Shown Foils:','max',
1.156     bisitz     94: 				    $token,'4').
1.154     raeburn    95: 	    &Apache::edit::select_arg('Randomize Foil Order:','randomize',
1.156     bisitz     96: 				      ['yes','no'],$token).
1.154     raeburn    97: 	    &Apache::edit::select_arg('Display Direction:','direction',
                     98: 				      ['vertical','horizontal'],$token).
                     99: 				      &Apache::edit::end_row().
                    100: 				      &Apache::edit::start_spanning_row()."\n";
                    101:     } elsif ($target eq 'modified') {
                    102: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    103: 						     $safeeval,'max',
                    104: 						     'randomize','direction');
                    105: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.155     foxr      106: 
                    107:    } elsif ( $target eq 'tex' ) {
                    108:         my $type =
                    109:           &Apache::lonxml::get_param( 'TeXtype', $parstack, $safeeval, undef,
                    110:             0 );
                    111:         if ( $type eq '1' ) {
                    112:             $result .= ' \renewcommand{\labelenumi}{\arabic{enumi}.}';
                    113:         }
                    114:         elsif ( $type eq 'A' ) {
                    115:             $result .= ' \renewcommand{\labelenumi}{\Alph{enumi}.}';
                    116:         }
                    117:         elsif ( $type eq 'a' ) {
                    118:             $result .= ' \renewcommand{\labelenumi}{\alph{enumi}.}';
                    119:         }
                    120:         elsif ( $type eq 'i' ) {
                    121:             $result .= ' \renewcommand{\labelenumi}{\roman{enumi}.}';
                    122:         }
                    123:         else {
                    124:             $result .= ' \renewcommand{\labelenumi}{\Alph{enumi}.}';
1.135     onken     125:         }
1.155     foxr      126: 
                    127:     }
                    128:     elsif ( $target eq 'analyze' ) {
                    129:         my $part_id = "$Apache::inputtags::part.$id";
1.131     raeburn   130:         $Apache::lonhomework::analyze{"$part_id.type"} = 'radiobuttonresponse';
1.154     raeburn   131: 	push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
1.83      albertel  132:     }
                    133:     return $result;
1.1       albertel  134: }
                    135: 
                    136: sub end_radiobuttonresponse {
1.154     raeburn   137:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.83      albertel  138:     my $result;
1.155     foxr      139:     if ( $target eq 'edit' ) { 
                    140: 	$result = &Apache::edit::end_table(); 
1.135     onken     141:     }
1.83      albertel  142:     &Apache::response::end_response;
                    143:     pop @Apache::lonxml::namespace;
1.154     raeburn   144:     &Apache::lonxml::deregister('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));
1.85      albertel  145:     undef(%Apache::response::foilnames);
1.83      albertel  146:     return $result;
1.1       albertel  147: }
                    148: 
1.154     raeburn   149: %Apache::response::foilgroup=();
1.1       albertel  150: sub start_foilgroup {
1.154     raeburn   151:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    152:     %Apache::response::foilgroup=();
                    153:     $Apache::radiobuttonresponse::conceptgroup=0;
                    154:     &Apache::response::pushrandomnumber(undef,$target);
1.151     raeburn   155:     return;
1.5       albertel  156: }
                    157: 
1.15      albertel  158: sub storesurvey {
1.144     raeburn   159:     my ($style) = @_;
1.99      albertel  160:     if ( !&Apache::response::submitted() ) { return ''; }
1.154     raeburn   161:     my $response = $env{'form.HWVAL_'.$Apache::inputtags::response['-1']};
1.83      albertel  162:     &Apache::lonxml::debug("Here I am!:$response:");
1.154     raeburn   163:     if ( $response !~ /[0-9]+/) { return ''; }
                    164:     my $part = $Apache::inputtags::part;
                    165:     my $id = $Apache::inputtags::response['-1'];
                    166:     my @whichfoils=@{ $Apache::response::foilgroup{'names'} };
1.83      albertel  167:     my %responsehash;
1.154     raeburn   168:     $responsehash{$whichfoils[$response]}=$response;
                    169:     my $responsestr=&Apache::lonnet::hash2str(%responsehash);
                    170:     $Apache::lonhomework::results{"resource.$part.$id.submission"}=
                    171: 	$responsestr;
                    172:     my %previous=&Apache::response::check_for_previous($responsestr,$part,$id);
1.144     raeburn   173:     my $ad;
1.154     raeburn   174:     if ($style eq 'anonsurvey') {
                    175:         $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='ANONYMOUS';
                    176:     } elsif ($style eq 'anonsurveycred') {
                    177:         $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='ANONYMOUS_CREDIT';
                    178:     } elsif ($style eq 'surveycred') {
                    179:         $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED_CREDIT';
                    180:     } else {
                    181:         $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED';
1.144     raeburn   182:     }
1.154     raeburn   183:     &Apache::response::handle_previous(\%previous,$ad);
1.83      albertel  184:     &Apache::lonxml::debug("submitted a $response<br />\n");
                    185:     return '';
1.15      albertel  186: }
                    187: 
1.154     raeburn   188: 
1.32      albertel  189: sub grade_response {
1.154     raeburn   190:     my ($answer, $whichfoils, $bubbles_per_line)=@_;
1.123     albertel  191: 
1.99      albertel  192:     if ( !&Apache::response::submitted() ) { return; }
1.83      albertel  193:     my $response;
1.154     raeburn   194:     
                    195:     if ($env{'form.submitted'} eq 'scantron') {
                    196: 	$response = &Apache::response::getresponse(1,undef,
                    197: 						   &bubble_line_count(scalar(@{ $whichfoils}),
                    198: 								      $bubbles_per_line),
                    199: 						   $bubbles_per_line);
1.116     foxr      200: 
1.154     raeburn   201:     } else {
                    202: 	$response = $env{'form.HWVAL_'.$Apache::inputtags::response['-1']};
                    203:     }
1.153     foxr      204: 
1.120     foxr      205: 
1.154     raeburn   206:     if ( $response !~ /[0-9]+/) { return; }
                    207:     my $part=$Apache::inputtags::part;
                    208:     my $id = $Apache::inputtags::response['-1'];
1.83      albertel  209:     my %responsehash;
1.154     raeburn   210:     $responsehash{$whichfoils->[$response]}=$response;
                    211:     my $responsestr=&Apache::lonnet::hash2str(%responsehash);
                    212:     my %previous=&Apache::response::check_for_previous($responsestr,
                    213: 						       $part,$id);
                    214:     $Apache::lonhomework::results{"resource.$part.$id.submission"}=
                    215: 	$responsestr;
1.83      albertel  216:     &Apache::lonxml::debug("submitted a $response<br />\n");
                    217:     my $ad;
1.154     raeburn   218:     if ($response == $answer) {
                    219: 	$ad='EXACT_ANS';
                    220:     } else {
                    221: 	$ad='INCORRECT';
1.83      albertel  222:     }
1.154     raeburn   223:     $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
                    224:     &Apache::response::handle_previous(\%previous,$ad);
1.32      albertel  225: }
                    226: 
1.1       albertel  227: sub end_foilgroup {
1.154     raeburn   228:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.29      albertel  229: 
1.83      albertel  230:     my $result;
1.121     foxr      231:     my $bubble_lines;
                    232:     my $answer_count;
1.154     raeburn   233:     my $id   = $Apache::inputtags::response['-1'];
                    234:     my $part = $Apache::inputtags::part;
                    235:     my $bubbles_per_line = &getbubblesnum($part,$id);
1.153     foxr      236: 
1.155     foxr      237: 
1.154     raeburn   238:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
                    239: 	$target eq 'tex' || $target eq 'analyze') {
                    240: 	my $style = $Apache::lonhomework::type;
                    241: 	my $direction = &Apache::lonxml::get_param('direction',$parstack,
                    242: 						   $safeeval,'-2');
                    243: 	if ( (($style eq 'survey') || ($style eq 'surveycred') || 
                    244:               ($style eq 'anonsurvey') || ($style eq 'anonsurveycred')) 
                    245:              && ($target ne 'analyze')) {
                    246: 	    if ($target eq 'web' || $target eq 'tex') {
                    247: 		$result=&displayallfoils($direction, $target);
                    248: 	    } elsif ( $target eq 'answer' ) {
                    249: 		$result=&displayallanswers();
                    250: 	    } elsif ( $target eq 'grade' ) {
                    251: 		$result=&storesurvey($style);
                    252: 	    }
                    253: 	    $answer_count = scalar(@{$Apache::response::foilgroup{'names'}});
                    254: 
                    255: 	} else {
                    256: 
                    257: 	    my $name;
                    258: 	    my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,
                    259: 						 '-2');
                    260: 	    my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
                    261: 						       $safeeval,'-2');
                    262: 	    my ($answer, @shown) = &whichfoils($max, $randomize);
                    263: 	    $answer_count = scalar(@shown);
                    264: 
                    265: 	    if ($target eq 'web' || $target eq 'tex') {
                    266:                 $result=&displayfoils($target,
                    267: 				      $answer, \@shown,
                    268: 				      $direction,
                    269: 				      $bubbles_per_line);
                    270: 	    } elsif ($target eq 'answer' ) {
                    271: 		$result=&displayanswers($answer, \@shown, $bubbles_per_line);
                    272: 	    } elsif ( $target eq 'grade') {
                    273: 		&grade_response($answer, \@shown, $bubbles_per_line);
                    274: 	    }  elsif ( $target eq 'analyze') {
                    275: 		my $bubble_lines = &bubble_line_count($answer_count, 
                    276: 						      $bubbles_per_line);
                    277: 		&Apache::response::analyze_store_foilgroup(\@shown,
                    278: 							   ['text','value','location']);
                    279: 		my $part_id="$part.$id";
                    280: 		push (@{ $Apache::lonhomework::analyze{"$part_id.options"} },
                    281: 		      ('true','false'));
                    282: 
                    283: 	    }
                    284: 	}
                    285: 	$Apache::lonxml::post_evaluate=0;
                    286:     }
                    287:     if ($target eq 'web') {
                    288: 	&Apache::response::setup_prior_tries_hash(\&format_prior_answer,
                    289: 						  [\%Apache::response::foilgroup]);
1.114     albertel  290:     }
1.128     foxr      291:     &Apache::response::poprandomnumber();
1.154     raeburn   292:     $bubble_lines = &bubble_line_count($answer_count, $bubbles_per_line);
                    293:     &Apache::lonxml::increment_counter($bubble_lines,
                    294: 				       "$part.$id");
                    295:     if ($target eq 'analyze') {
                    296: 	&Apache::lonhomework::set_bubble_lines();
1.128     foxr      297:     }
1.83      albertel  298:     return $result;
1.6       albertel  299: }
1.151     raeburn   300: sub getbubblesnum {
1.154     raeburn   301:     my ($part,$id) = @_;
1.151     raeburn   302:     my $bubbles_per_line;
                    303:     my $default_numbubbles = $default_bubbles_per_line;
1.154     raeburn   304:     if (($env{'form.bubbles_per_row'} =~ /^\d+$/) &&
                    305:         ($env{'form.bubbles_per_row'} > 0)) {
1.151     raeburn   306:         $default_numbubbles = $env{'form.bubbles_per_row'};
                    307:     }
1.154     raeburn   308:     $bubbles_per_line =
                    309:         &Apache::response::get_response_param($part."_$id",'numbubbles',
                    310:                                               $default_numbubbles);
1.151     raeburn   311:     return $bubbles_per_line;
                    312: }
                    313: 
1.6       albertel  314: sub getfoilcounts {
1.83      albertel  315:     my @names;
1.154     raeburn   316:     my $truecnt=0;
                    317:     my $falsecnt=0;
1.83      albertel  318:     my $name;
                    319:     if ( $Apache::response::foilgroup{'names'} ) {
1.154     raeburn   320: 	@names= @{ $Apache::response::foilgroup{'names'} };
1.6       albertel  321:     }
1.83      albertel  322:     foreach $name (@names) {
1.154     raeburn   323: 	if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
                    324: 	    $truecnt++;
                    325: 	} elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
                    326: 	    $falsecnt++;
                    327: 	}
1.83      albertel  328:     }
1.154     raeburn   329:     return ($truecnt,$falsecnt);
1.5       albertel  330: }
                    331: 
1.114     albertel  332: sub format_prior_answer {
1.154     raeburn   333:     my ($mode,$answer,$other_data) = @_;
1.114     albertel  334:     my $foil_data = $other_data->[0];
1.154     raeburn   335:     my %response = &Apache::lonnet::str2hash($answer);
                    336:     my ($name)   = keys(%response);
                    337:     return '<span class="LC_prior_radiobutton">'.
                    338: 	$foil_data->{$name.'.text'}.'</span>';
1.114     albertel  339: 
1.112     albertel  340: }
                    341: 
1.120     foxr      342: 
1.155     foxr      343: ## 
                    344: # Return the last survey response.  The logic is slightly different than that of 
                    345: # get_last_responses.  TODO: See if there are chunks of code betweenthis and
                    346: # get_last_reponses that are common and can be factored.
                    347: #
                    348: # @param $part - Problem part under consideration.
                    349: # @param $showanswer - True if answers should be shown.
                    350: # @param $id         - Problem id.
                    351: #
                    352: # @return hash reference.
                    353: # @retval reference to the has indexed by answer selection that 
                    354: #         indicates the most recent answer.
                    355: #
                    356: sub get_last_survey_response {
                    357:     my ($part, $showanswer, $id) = @_;
                    358: 
                    359:     my $newvariation;
                    360:     my $lastresponse;		# stringified last response.
                    361: 
                    362:     if (
                    363:         (
                    364:             (
                    365:                 $Apache::lonhomework::history{"resource.$part.type"} eq
                    366:                 'randomizetry'
                    367:             )
                    368:             || ( $Apache::lonhomework::type eq 'randomizetry' )
                    369:         )
                    370:         && ( $Apache::inputtags::status[-1] eq 'CAN_ANSWER' )
                    371:       )
                    372:     {
                    373:         if ( $env{ 'form.' . $part . '.rndseed' } ne
                    374:             $Apache::lonhomework::history{"resource.$part.rndseed"} )
                    375:         {
1.147     raeburn   376:             $newvariation = 1;
                    377:         }
                    378:     }
                    379:     $showanswer = &Apache::response::show_answer();
1.157     raeburn   380:     unless ($newvariation && !$showanswer) {
                    381:         if ((($env{'form.grade_username'} eq '') && ($env{'form.grade_domain'} eq '')) ||
                    382:             (($env{'form.grade_username'} eq $env{'user.name'}) &&
                    383:              ($env{'form.grade_domain'} eq $env{'user.domain'}))) {
                    384:             $lastresponse = $Apache::lonhomework::history{"resource.$part.$id.submission"};
                    385:         } else {
                    386:             unless (($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurvey') ||
                    387:                     ($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurveycred')) {
                    388:                 $lastresponse = $Apache::lonhomework::history{"resource.$part.$id.submission"};
                    389:             }
                    390:         }
1.155     foxr      391:     }
                    392:     my %lastresponse = &Apache::lonnet::str2hash($lastresponse);
                    393: 
                    394:     return \%lastresponse;
                    395: 
                    396: }
                    397: ##
                    398: # Removes the names from a foil group that are marked as unused.
                    399: #
                    400: # @param $names - reference to the array of names to filter.
                    401: #
                    402: # @return arrayref
                    403: # @retval reference to the filtered array.
                    404: #
                    405: sub remove_unused {
                    406:     my ($names) = @_;
                    407:     my @result;
                    408: 
                    409:     foreach my $name (@{$names}) {
                    410: 	if ($Apache::response::foilgroup{$name . '.value'} ne 'unused') {
                    411: 	    push(@result, $name);
                    412: 	}
1.144     raeburn   413:     }
1.155     foxr      414:     return \@result;
                    415: }
                    416: ## 
                    417: # Displays all foils in a survey type problem for HTML rendition.
                    418: # TODO: See if there is any logic in this sub that can be shared
                    419: #      with display_foils_html
                    420: #
                    421: # @param $names        - ref to array of names of the foils to display.
                    422: # @param $part         - Problem part number.
                    423: # @param $showanswer   - If true, show the answers.
                    424: # @param $lastresponse - Ref to the last response hash.
                    425: # @param $direction    - Display direction of the radiobuttons.
                    426: #
                    427: # @return string
                    428: # @retval HTML required to display the resource in a browser.
                    429: #
                    430: sub display_survey_html {
                    431:     my ($names, $part, $showanswer, $lastresponse, $direction) = @_;
                    432:     my $result;
                    433: 
                    434:     # Figure out a few fragments of html that depend onthe 
                    435:     # orientation of the radiobuttons:
                    436:     # closing_html - HTML to emit at the end of the resource.
                    437:     # pre_foil     - HTML to emit prior to each foil.
                    438:     # post_foil    - HTML to emit following each foil.
                    439:     #
                    440:     #  The opening HTML is just added to the $result now
                    441:     #
                    442:     #  Figuring these outin advance compresses the loop over foils into something
                    443:     #  pretty simple:
                    444:     #
                    445:     # NOTE: There's probably a really cool way to do this with style sheets
                    446:     #       and picking the selector based on the orientation, if someone wants to puzzle
                    447:     #       that out.  In that case, probably the whole thing lives in a <div> and each
                    448:     #       foil lives in a <p>
                    449:     #
                    450: 
                    451: 
                    452:     my ($opening_html, $closing_html, $pre_foil, $post_foil) = 
                    453: 	&html_direction_fragments($direction);
                    454: 
                    455:     $result = $opening_html;
                    456: 
                    457:     # Different rendering depending on whether answers are shown:
                    458:     # I played with different factorings but this seems the most concise/clear...
                    459:     # although I don't like the $showanswer conditino inside the loop.  Other things I tried
                    460:     #  - two loops..much longer code..no gain in clarity.
                    461:     #  - Using a visitor patttern passing it the rendering code chunklets and
                    462:     #    an anonymous hash reference for state data etc. Very cool but
                    463:     #    quite a bit more code and quite a bit less clear.
                    464:     
                    465:     my $temp = 0;
                    466:     foreach my $name (@{$names}) {
                    467: 	$result .= $pre_foil;
                    468: 
                    469: 	if ($showanswer) {
                    470: 	    my $foiltext =  $Apache::response::foilgroup{$name . '.text'};
                    471: 
                    472: 	    # Bold the prior  response:
                    473: 
                    474: 	    if (defined($lastresponse->{$name})) {
                    475: 		$result .= '<b>' . $foiltext . '</b>';
                    476: 	    } else {
                    477: 		$result .= $foiltext;
1.154     raeburn   478: 	    }
1.155     foxr      479: 	} else {
                    480: 	    $result .= &html_radiobutton(
                    481: 		$part, $Apache::inputtags::response['-1'], $name, $lastresponse, $temp
                    482: 	     );
1.154     raeburn   483: 	}
1.155     foxr      484: 
                    485: 	$result .= $post_foil;
                    486: 	$temp++;
                    487:     }
                    488: 
                    489: 
                    490:     $result .= $closing_html;
                    491:     return $result;
                    492: 
                    493: }
                    494: 
                    495: ##
                    496: #  Generate LaTeX for surveys.
                    497: #  
                    498: #   @param $names - names of the foils to display.
                    499: #   @param $showanswer - flag that is true to display answers.
                    500: #   @param $lastresponse - Reference to a hash the indicates the last response.
                    501: #   @param $direction    - Orientation of foils ('horiztonal' or otherwise).
                    502: #   @param $venv         - LaTeX name for vertical env.
                    503: #
                    504: #   @return string
                    505: #   @retval LaTeX rendering of the survey question.
                    506: 
                    507: sub latex_survey {
                    508:     my ($names, $showanswer, $lastresponse, $direction, $venv) = @_;
                    509: 
                    510:     my $result;
                    511:     if ($showanswer) {
                    512: 	$result .= "\\begin{$venv}";
                    513: 	foreach my $name (@{$names}) {
                    514: 	    
                    515: 	    
                    516: 	    $result .= '\item \vskip -2mm ';
                    517: 	    
                    518: 	    if ( defined( $lastresponse->{$name} ) ) {
                    519: 		$result .= '}';
                    520: 	    }
                    521: 	    $result .= $Apache::response::foilgroup{ $name . '.text' } . ' ';
                    522: 	}
                    523: 	$result .= "\\end{$venv}";
                    524: 
                    525:     } elsif ( $env{'form.pdfFormFields'} eq 'yes'
                    526: 	      && $Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
                    527: 	$result .= &display_pdf_form($names, $direction, $venv);
1.154     raeburn   528:     } else {
1.155     foxr      529: 	if ($direction eq 'horizontal') {
                    530: 	    my @foil_texts = &get_foil_texts($names);
                    531: 	    $result .=  &Apache::caparesponse::make_horizontal_latex_bubbles(
                    532: 	    $names, \@foil_texts, '$\bigcirc$');
                    533: 	} else {
                    534: 	    $result .= "\\begin{$venv}";
                    535: 
                    536: 	    my $temp = 0;
                    537: 	    my $i    = 0;
                    538: 	    foreach my $name (@{$names}) {
                    539: 
                    540: 		$result .= '\item \vskip -2mm ';
                    541: 		
                    542: 		if ($env{'form.pdfFormFields'} ne 'yes'
                    543: 		    or $Apache::inputtags::status[-1] ne 'CAN_ANSWER' )
                    544: 		{
                    545: 		    $result .=
                    546: 			'$\bigcirc$'
                    547: 			. $Apache::response::foilgroup{ $name . '.text' }
                    548: 		    . '\\\\';    #' stupid emacs
1.154     raeburn   549: 		}
1.155     foxr      550: 		
                    551: 		$i++;	    
1.154     raeburn   552: 		$temp++;
1.155     foxr      553: 		
                    554: 		$result .= '\vskip 0 mm ';
1.154     raeburn   555: 	    }
1.155     foxr      556: 	    $result .= "\\end{$venv}";
                    557: 	}	
                    558:     }
                    559:     return $result;
                    560: }
                    561: ##
                    562: #  Figure out the LaTeX environment in which to wrap the LaTeX vertical output.
                    563: #
                    564: # @return string
                    565: # @retval the environment name.  The LaTeX should be wrapped a 
                    566: #    \begin{retval} \end{retval} pair.
                    567: #
                    568: sub latex_vertical_environment {
                    569:     if ($env{'form.pdfFormFields'} eq 'yes'
                    570: 	&& $Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
                    571: 	return 'itemize';
                    572:     } else {
                    573: 	return 'enumerate';
                    574:     }
                    575: }
                    576: 
                    577: ##
                    578: # Figure out the key html fragments that depend on the rendering direction:
                    579: #
                    580: # @param $direction - 'horizontal' for horizontal direction.
                    581: #
                    582: # @return list
                    583: # @retval (part_start, part_end, foil_start, foil_end)
                    584: # Where:
                    585: #   - part_start is the HTML to emit at the start of the part.
                    586: #   - part_end   is the HTML to emit at the end of the part.
                    587: #   - foil_start is the HTML to emit prior to each foil.
                    588: #   - foil_end is the HTML to emit after each foil
                    589: #
                    590: sub html_direction_fragments {
                    591:     my $direction = shift;
                    592:     if ($direction eq 'horizontal') {
                    593: 	return ('<table><tr>', '</tr></table>', '<td>', '</td>');
                    594:     } else {
1.160     damieng   595: 	return ('', '', '<div class="LC_radiofoil">', '</div>');
1.155     foxr      596:     }
                    597: }
                    598: 
                    599: ##
                    600: #
                    601: #  Displays all the foils of a problem in a format suitable for
                    602: #   surveys, surveys for credit, anonymous surveys and anonymous surveys for credit.
                    603: #
                    604: #  @param $direction - Display direction of the choices ('horiztonal' or not).
                    605: #  @param $target    - Rendering target.
                    606: #
                    607: #  @return string
                    608: #  @retval Text that renders for the selected target.
                    609: # 
                    610: sub displayallfoils{
                    611:     my ( $direction, $target ) = @_;
                    612:     my $result;
                    613:     &Apache::lonxml::debug("survey style display");
                    614: 
                    615:     my @names;
                    616: 
                    617:     if ( $Apache::response::foilgroup{'names'} ) {
                    618:         @names = @{ $Apache::response::foilgroup{'names'} };
1.45      albertel  619:     }
1.155     foxr      620: 
                    621: 
                    622:     my $id   = $Apache::inputtags::response['-1'];
                    623:     my $part = $Apache::inputtags::part;
1.154     raeburn   624:     
1.155     foxr      625:     my $showanswer = &Apache::response::show_answer();
                    626:     my $lastresponse = &get_last_survey_response($part, $showanswer, $id);
                    627:     my $used_names = &remove_unused(\@names);
                    628: 
                    629: 
                    630:     if ($target ne 'tex') {
                    631: 	$result .= &display_survey_html(
                    632: 	    $used_names, $part, $showanswer, $lastresponse, $direction
                    633: 	);
                    634:     } else {	
                    635: 
                    636: 	my $vertical_env = &latex_vertical_environment();
                    637: 	$result .= &latex_survey(
                    638: 	    $used_names, $showanswer, $lastresponse, $direction, $vertical_env
                    639: 	);
                    640: 
                    641:     }
1.83      albertel  642:     return $result;
1.15      albertel  643: }
                    644: 
1.28      albertel  645: sub whichfoils {
1.154     raeburn   646:     my ($max,$randomize)=@_;
1.28      albertel  647: 
1.83      albertel  648:     my @truelist;
                    649:     my @falselist;
1.154     raeburn   650:     my @whichfalse =();
                    651:     my ($truecnt,$falsecnt) = &getfoilcounts();
                    652:     my $count=0;
1.83      albertel  653:     # we will add in 1 of the true statements
1.154     raeburn   654:     if ( $max>0 && ($falsecnt+1)>$max) { $count=$max } else { $count=$falsecnt+1; $max=$count; }
                    655:     my $answer=int(&Math::Random::random_uniform() * ($count));
1.83      albertel  656:     &Apache::lonxml::debug("Count is $count, $answer is $answer");
                    657:     my @names;
                    658:     if ( $Apache::response::foilgroup{'names'} ) {
1.154     raeburn   659: 	@names= @{ $Apache::response::foilgroup{'names'} };
1.83      albertel  660:     }
1.154     raeburn   661:     if (&Apache::response::showallfoils()) {
                    662: 	@whichfalse=@names;
                    663:     } elsif ($randomize eq 'no') {
                    664: 	&Apache::lonxml::debug("No randomization");
                    665: 	my $havetrue=0;
                    666: 	foreach my $name (@names) {
                    667: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
                    668: 		if (!$havetrue ) {
                    669: 		    push(@whichfalse,$name); $havetrue++; $answer=$#whichfalse;
                    670: 		}
                    671: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
                    672: 		push (@whichfalse,$name);
                    673: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
                    674: 	    } else {
                    675: 		&Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>",'<>&"'));
                    676: 	    }
                    677: 	}
                    678: 	if ((!$havetrue) && 
                    679:             ($Apache::lonhomework::type ne 'survey') && 
                    680:             ($Apache::lonhomework::type ne 'surveycred') &&
                    681:             ($Apache::lonhomework::type ne 'anonsurvey') &&
                    682:             ($Apache::lonhomework::type ne 'anonsurveycred')) {
                    683: 	    &Apache::lonxml::error(&mt('There are no true statements available.').'<br />');
                    684: 	}
                    685:     } else {
                    686: 	my $current=0;
                    687: 	&Apache::lonhomework::showhash(%Apache::response::foilgroup);
                    688: 	my (%top,%bottom);
                    689: 	#first find out where everyone wants to be
                    690: 	foreach my $name (@names) {
                    691: 	    $current++;
                    692: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
                    693: 		push (@truelist,$name);
                    694: 		if ($Apache::response::foilgroup{$name.'.location'} eq 'top') {
                    695: 		    $top{$name}=$current;
                    696: 		} elsif ($Apache::response::foilgroup{$name.'.location'} eq 'bottom') {
                    697: 		    $bottom{$name}=$current;
                    698: 		}
                    699: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
                    700: 		push (@falselist,$name);
                    701: 		if ($Apache::response::foilgroup{$name.'.location'} eq 'top') {
                    702: 		    $top{$name}=$current;
                    703: 		} elsif ($Apache::response::foilgroup{$name.'.location'} eq 'bottom') {
                    704: 		    $bottom{$name}=$current;
                    705: 		}
                    706: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
                    707: 	    } else {
                    708: 		&Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>",'<>&"'));
                    709: 	    }
                    710: 	}
                    711: 	#pick a true statement
                    712: 	my $notrue=0;
                    713: 	if (scalar(@truelist) == 0) { $notrue=1; }
                    714: 	my $whichtrue = int(&Math::Random::random_uniform() * ($#truelist+1));
                    715: 	&Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue");
                    716: 	my (@toplist, @bottomlist);
                    717: 	my $topcount=0;
                    718: 	my $bottomcount=0;
                    719: 	# assign everyone to either toplist/bottomlist or whichfalse
                    720: 	# which false is randomized, toplist bottomlist are in order
                    721: 	while ((($#whichfalse+$topcount+$bottomcount) < $max-2) && ($#falselist > -1)) {
                    722: 	    &Apache::lonxml::debug("Have $#whichfalse max is $max");
                    723: 	    my $afalse=int(&Math::Random::random_uniform() * ($#falselist+1));
                    724: 	    &Apache::lonxml::debug("From $#falselist elms, picking $afalse");
                    725: 	    $afalse=splice(@falselist,$afalse,1);
                    726: 	    &Apache::lonxml::debug("Picked $afalse");
                    727: 	    &Apache::lonhomework::showhash(('names'=>\@names));
                    728: 	    &Apache::lonhomework::showhash(%top);
                    729: 	    if ($top{$afalse}) {
                    730: 		$toplist[$top{$afalse}]=$afalse;
                    731: 		$topcount++;
                    732: 	    } elsif ($bottom{$afalse}) {
                    733: 		$bottomlist[$bottom{$afalse}]=$afalse;
                    734: 		$bottomcount++;
                    735: 	    } else {
                    736: 		push (@whichfalse,$afalse);
                    737: 	    }
                    738: 	}
                    739: 	&Apache::lonxml::debug("Answer wants $answer");
                    740: 	my $truename=$truelist[$whichtrue];
                    741: 	my $dosplice=1;
                    742: 	if (($notrue) && 
                    743:             ($Apache::lonhomework::type ne 'survey') &&
                    744:             ($Apache::lonhomework::type ne 'surveycred') &&
                    745:             ($Apache::lonhomework::type ne 'anonsurvey') &&
                    746:             ($Apache::lonhomework::type ne 'anonsurveycred')) {
                    747: 	    $dosplice=0;
                    748: 	    &Apache::lonxml::error(&mt('There are no true statements available.').'<br />');
                    749: 	}
                    750: 	#insert the true statement, keeping track of where it wants to be
                    751: 	if ($Apache::response::foilgroup{$truename.'.location'} eq 'top' && $dosplice) {
                    752: 	    $toplist[$top{$truename}]=$truename;
                    753: 	    $answer=-1;
                    754: 	    foreach my $top (reverse(@toplist)) {
                    755: 		if ($top) { $answer++;}
                    756: 		if ($top eq $truename) { last; }
                    757: 	    }
                    758: 	    $dosplice=0;
                    759: 	} elsif ($Apache::response::foilgroup{$truename.'.location'} eq 'bottom' && $dosplice) {
                    760: 	    $bottomlist[$bottom{$truename}]=$truename;
                    761: 	    $answer=-1;
                    762: 	    foreach my $bot (@bottomlist) {
                    763: 		if ($bot) { $answer++;}
                    764: 		if ($bot eq $truename) { last; }
                    765: 	    }
                    766: 	    $answer+=$topcount+$#whichfalse+1;
                    767: 	    $dosplice=0;
                    768: 	} else {
                    769: 	    if ($topcount>0 || $bottomcount>0) {
1.150     raeburn   770:                 my $inc = 1;
1.154     raeburn   771:                 if (($bottomcount > 0) && ($Apache::lonhomework::type ne 'exam')) {
1.150     raeburn   772:                     $inc = 2;
                    773:                 }
1.154     raeburn   774:                 $answer=int(&Math::Random::random_uniform() * ($#whichfalse+$inc))
                    775:                         + $topcount;
                    776: 	    }
                    777: 	}
                    778: 	&Apache::lonxml::debug("Answer now wants $answer");
                    779: 	#add the top items to the top, bottom items to the bottom
                    780: 	for (my $i=0;$i<=$#toplist;$i++) {
                    781: 	    if ($toplist[$i]) { unshift(@whichfalse,$toplist[$i]) }
                    782: 	}
                    783: 	for (my $i=0;$i<=$#bottomlist;$i++) {
                    784: 	    if ($bottomlist[$i]) { push(@whichfalse,$bottomlist[$i]) }
                    785: 	}
                    786: 	#if the true statement is randomized insert it into the list
                    787: 	if ($dosplice) { splice(@whichfalse,$answer,0,$truelist[$whichtrue]); }
1.49      albertel  788:     }
1.83      albertel  789:     &Apache::lonxml::debug("Answer is $answer");
1.154     raeburn   790:     return ($answer,@whichfalse);
1.28      albertel  791: }
                    792: 
1.155     foxr      793: ## 
                    794: #  Return a list  of foil texts given foil names.
                    795: #  
                    796: # @param $whichfoils - Reference to a list of foil names.
                    797: #
                    798: # @return array
                    799: # @retval foil texts
                    800: #
                    801: sub get_foil_texts {
                    802:     my ($whichfoils) = @_;
                    803:     my @foil_texts;
                    804: 
                    805:     foreach my $name (@{$whichfoils}) {
                    806: 	push(@foil_texts, $Apache::response::foilgroup{$name . '.text'});
                    807:     }
                    808:     return @foil_texts;
                    809: }
                    810: 
                    811: ##
                    812: # Generate the HTML for a single html foil.
                    813: # @param $part           - The part for which the response is being generated.
                    814: # @param $fieldname      - The basename of the radiobutton field
                    815: # @param $name           - The foilname.
                    816: # @param $last_responses - Reference to a hash that holds the most recent
                    817: #                          responses.
                    818: # @param $value          - radiobutton value.
                    819: # 
                    820: # @return text
                    821: # @retval The generated html.
                    822: #
                    823: sub html_radiobutton {
                    824:     my ($part, $fieldname, $name, $last_responses, $value) = @_;
                    825: 
                    826:     my $result='<label>';
                    827:     
1.159     raeburn   828:     $result .= '<input type="radio" '
                    829: 		. 'onchange="javascript:setSubmittedPart(' . "'$part');\" "
                    830: 		. 'name="HWVAL_' . $fieldname . '" '
                    831: 		. 'value="'.$value.'"';
1.155     foxr      832: 
                    833:     if (defined($last_responses->{$name})) {
                    834: 	$result .= '  checked="checked" ';
                    835:     }
                    836:     $result .= ' />';
                    837:     $result .= $Apache::response::foilgroup{$name . '.text'};
                    838:     $result .= '</label>';
                    839: 
                    840:     return $result;
                    841: 
                    842: }
                    843: ##
                    844: # Return a reference to the last response hash. This hash has exactly
                    845: # one or zero entries.  The one entry is keyed by the foil 'name' of
                    846: # the prior response
                    847: #
                    848: # @param $part - Number of the problem part.
                    849: # 
                    850: # @return reference to a hash.
                    851: # @retval see above.
                    852: #
                    853: sub get_last_response {
                    854:     my ($part) = @_;
                    855: 
                    856:     my $id = $Apache::inputtags::response['-1'];
                    857:     my ( $lastresponse, $newvariation );
                    858:     
                    859:     if ((( $Apache::lonhomework::history{"resource.$part.type"} eq  'randomizetry')
                    860: 	 || ( $Apache::lonhomework::type eq 'randomizetry' )
                    861: 	)
                    862: 	&& ( $Apache::inputtags::status[-1] eq 'CAN_ANSWER' )
                    863: 	)
                    864:     {
                    865: 	
                    866: 	if ( $env{ 'form.' . $part . '.rndseed' } ne
                    867: 	     $Apache::lonhomework::history{"resource.$part.rndseed"} )
                    868: 	{
                    869: 	    $newvariation = 1;
                    870: 	}
                    871:     }
                    872:     unless ($newvariation) {
1.157     raeburn   873:         if ((($env{'form.grade_username'} eq '') && ($env{'form.grade_domain'} eq '')) ||
                    874:             (($env{'form.grade_username'} eq $env{'user.name'}) &&
                    875:              ($env{'form.grade_domain'} eq $env{'user.domain'}))) {
                    876:             $lastresponse =
                    877:                 $Apache::lonhomework::history{"resource.$part.$id.submission"};
                    878:         } else {
                    879:             unless (($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurvey') ||
                    880:                     ($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurveycred')) {
                    881:                 $lastresponse =
                    882:                     $Apache::lonhomework::history{"resource.$part.$id.submission"};
                    883: 
                    884:             }
                    885:         }
1.155     foxr      886:     }
1.157     raeburn   887: 
1.155     foxr      888:     my %lastresponse = &Apache::lonnet::str2hash($lastresponse);
                    889: 
                    890:     return \%lastresponse;
                    891: }
                    892: 
                    893: ##
                    894: # Display foils in html rendition.:
                    895: #
                    896: # @param $whichfoils - Set of foils to display.
                    897: # @param $target     - Rendition target...there are several html targets.
                    898: # @param $direction  - 'horizontal' if layout is horizontal.
                    899: # @param $part       - Part of the problem that's being displayed.
                    900: # @param $show_answer- True if answers should be shown.
                    901: #
                    902: # @return string
                    903: # @retval generated html.
                    904: #
                    905: sub display_foils_html {
                    906:     my ($whichfoils, $target, $direction, $part, $show_answer) = @_;
1.83      albertel  907:     my $result;
1.28      albertel  908: 
1.155     foxr      909: 
                    910:     # if the answers get shown, we need to label each item as correct or
                    911:     # incorrect.
                    912: 
                    913:     my ($opening_html, $finalclose, $item_pretext, $item_posttext) = 
                    914: 	&html_direction_fragments($direction);
                    915: 
                    916:     $result .= $opening_html;
                    917: 
                    918: 
                    919:     if ($show_answer) {
                    920: 
                    921: 	foreach my $name (@{$whichfoils}) {
                    922: 
                    923: 	    # If the item gets further surrounded by tags, this 
                    924: 	    # holds the closures for those tages.
                    925: 
                    926: 	    my $item_closetag = '';
                    927: 
                    928: 	    $result .= $item_pretext;
                    929: 
                    930: 	    # Label each foil as correct or incorrect:
                    931: 
                    932: 	    if ($Apache::response::foilgroup{$name . '.value'} eq 'true') {
                    933: 		$result .= &mt('Correct:') . '<b>';
                    934: 		$item_closetag .= '</b>';
                    935: 		
1.154     raeburn   936: 	    } else {
1.155     foxr      937: 		$result .= &mt('Incorrect');
1.154     raeburn   938: 	    }
1.155     foxr      939: 
                    940: 	    # Web rendition encloses the 
                    941: 	    # item text in a label tag as well:
                    942: 
                    943: 	    if ($target eq 'web') {
                    944: 		$result .= '<label>';
                    945: 		$item_closetag = '</label>' . $item_closetag;
1.154     raeburn   946: 	    }
1.155     foxr      947: 	    $result .= $Apache::response::foilgroup{$name . '.text'};
                    948: 	    $result .= $item_closetag;
                    949: 	    $result .= $item_posttext;
                    950: 	    $result .= "\n";	# make the html a bit more readable.
                    951: 	}
                    952: 
                    953: 
                    954:     } else {
                    955: 	my $lastresponse = &get_last_response($part);
                    956: 	
                    957: 	my $item_no = 0;
                    958: 	foreach my $name (@{$whichfoils}) {
                    959: 	    $result .= $item_pretext;
                    960: 	    $result .= &html_radiobutton(
                    961: 		$part, $Apache::inputtags::response[-1],
                    962: 		$name, $lastresponse, $item_no
                    963: 		);
                    964: 	    $result .= $item_posttext;
                    965: 	    $item_no++;
                    966: 	}
                    967: 	
                    968:     }
                    969:     $result .= $finalclose;
                    970: 
                    971:     return $result;
                    972: }
                    973: ##
                    974: #  Display foils in exam mode for latex
                    975: #
                    976: # @param $whichfoils       - Reference to an array that contains the foil names to display
                    977: # @param $bubbles_per_line - Number of bubbles on a line.
                    978: # @param $direction        - Rendering direction 'horizontal' is what we're looking for.
                    979: # @param $venv             - Name of LaTeX environment to use for vertical rendering.
                    980: #
                    981: # @return string
                    982: # @return the latex rendering of the exam problem.
                    983: #
                    984: #
                    985: sub display_latex_exam {
                    986:     my ($whichfoils, $bubbles_per_line, $direction, $venv) = @_;
                    987:     my $result;
                    988:     my $numlines;
                    989:     my $bubble_number = 0;
                    990:     my $line          = 0;
                    991:     my $i             = 0;
                    992: 
                    993:     
                    994:     if ($direction eq  'horizontal') {
                    995: 
                    996: 	# Marshall the display text for each foil and turn things over to
                    997: 	# Apache::response::make_horizontal_bubbles:
                    998: 
                    999: 	my @foil_texts = &get_foil_texts($whichfoils);
                   1000: 	$result .= &Apache::caparesponse::make_horizontal_latex_bubbles(
                   1001: 	    $whichfoils, \@foil_texts, '$\bigcirc$');
                   1002: 
                   1003:     } else {
                   1004: 	
                   1005: 	$result .= '\vskip 2mm \noindent';
                   1006: 
                   1007: 	# This section puts out the prefix that tells the user
                   1008: 	# (if necessary) to only choose one bubble in the next n lines
                   1009: 	# for problems with more than one line worth of bubbles in the grid sheet:
                   1010: 	
                   1011: 	my $numitems = scalar( @{$whichfoils} );
                   1012: 	$numlines = int( $numitems / $bubbles_per_line );
                   1013: 	if ( ( $numitems % $bubbles_per_line ) != 0 ) {
                   1014: 	    $numlines++;
                   1015: 	}
                   1016: 	if ( $numlines < 1 ) {
                   1017: 	    $numlines = 1;
                   1018: 	}
                   1019: 	if ( $numlines > 1 ) {
                   1020: 	    my $linetext;
                   1021: 	    for ( my $i = 0 ; $i < $numlines ; $i++ ) {
                   1022: 		$linetext .= $Apache::lonxml::counter + $i . ', ';
                   1023: 	    }
                   1024: 	    $linetext =~ s/,\s$//;
                   1025: 	    $result .=
                   1026: 		'\small {\textbf{'
                   1027: 		. $linetext . '}} '
                   1028: 		. ' {\footnotesize '
                   1029: 		. &mt( '(Bubble once in [_1] lines)', $numlines )
                   1030: 		. '} \hspace*{\fill} \\\\';
                   1031: 	}
                   1032: 	else {
                   1033: 	    $result .= '\textbf{' . $Apache::lonxml::counter . '}.';
                   1034: 	}
                   1035: 	
                   1036: 	# Now output the bubbles themselves:
                   1037: 	
                   1038: 	foreach my $name (@{$whichfoils}) {
                   1039: 	    if ( $bubble_number >= $bubbles_per_line ) {
                   1040: 		$line++;
                   1041: 		$i             = 0;
                   1042: 		$bubble_number = 0;
                   1043: 	    }
                   1044: 	    my $identifier;
                   1045: 	    if ( $numlines > 1 ) {
                   1046: 		$identifier = $Apache::lonxml::counter + $line;
                   1047: 	    }
                   1048: 	    my $preindent;
                   1049: 	    if ($bubble_number > 0) {
1.161   ! raeburn  1050:                 my $offset = 3;
        !          1051:                 if ($Apache::lonxml::counter) {
        !          1052:                     if ($identifier) {
        !          1053:                         $offset += 2 * (length($identifier)-1);
        !          1054:                     } else {
        !          1055:                         $offset += 2 * (length($Apache::lonxml::counter)-1);
        !          1056:                     }
        !          1057:                 }
        !          1058:                 $preindent = '\hspace*{'.$offset.' mm}';
1.155     foxr     1059: 	    }
                   1060: 	    my $foiltext = $Apache::response::foilgroup{$name . '.text'};
                   1061: 	    $foiltext =~ s/\\noindent//; # forgive me for I have sinned..
                   1062: 	    $result .= '{\small \textbf{'
                   1063: 		. $identifier  .$preindent
                   1064: 		. $alphabet[$i]
                   1065: 		. '}}$\bigcirc$'
                   1066: 		. $foiltext
                   1067: 	    . '\\\\';    #' stupid emacs -- it thinks it needs that apostrophe to close the quote
                   1068: 	    
                   1069: 	    $i++;
                   1070: 	    $bubble_number++;
1.154     raeburn  1071: 	}
1.155     foxr     1072: 
                   1073:     }	
                   1074: 
                   1075:     return $result;
                   1076:     
                   1077: }
                   1078: 
                   1079: ##
                   1080: #  Display latex when exam mode is not on.
                   1081: #
                   1082: #  @param $whichfoils - The foils to display
                   1083: #  @param $direction  - Display direction ('horizontal' is what matters to us).
                   1084: #  @param $venv       - Vertical env. to use for vertical rendering.
                   1085: #  @param  $vend      - End the vertical environment being used.
                   1086: #
                   1087: #  @return string
                   1088: #  @retval - The LaTeX rendering of the resource.'
                   1089: #
                   1090: sub display_latex {
                   1091:     my ($whichfoils, $direction, $venv) = @_;
                   1092:     my $result;
                   1093: 
                   1094:     # how we render depends on the direction.
                   1095:     # Vertical is some kind of list environment determined by vbegin/vend.
                   1096:     # Horizontal is a table that is generated by 
                   1097:     # Apache::caparesponse::make_horizontal_latex_bubbles with an empty string
                   1098:     # for the actual bubble text.
                   1099: 
                   1100:     if ($direction eq 'horizontal') {
                   1101: 	my @foil_texts = &get_foil_texts($whichfoils);
                   1102: 	$result .= &Apache::caparesponse::make_horizontal_latex_bubbles(
                   1103: 	    $whichfoils, \@foil_texts, '');
                   1104:     } else {
                   1105: 	$result .= "\\begin{$venv}";
                   1106: 	foreach my $name (@{$whichfoils}) {
                   1107: 	    $result .=  '\vspace*{-2 mm}\item '
                   1108: 		. $Apache::response::foilgroup{ $name . '.text' };
1.154     raeburn  1109: 	}
1.155     foxr     1110: 	
                   1111: 	$result .= "\\end{$venv}";
                   1112:     }
                   1113:     return $result;
                   1114: }
                   1115: 
                   1116: 
                   1117: ##
                   1118: #  Render foils for a PDF form. This is a variant of tex rednering that provides
                   1119: #  sufficient markup that the final PDF is a form that can be filled in online,
                   1120: #  or offline.
                   1121: #
                   1122: # @param $whichfoils - References an array of foils to display in the order in which
                   1123: #                     they should be displayed.
                   1124: # @param $direction  - Rendering direction.  'horiztonal' means inputs are laid out
                   1125: #                      horizontally otherwise they are stacked vertically.
                   1126: # @param $venv       - Vertical environment in which to wrap the foils.
                   1127: #
                   1128: # @return string
                   1129: # @retval String containing the rendering of the resource.
                   1130: #
                   1131: # TODO: Take into account direction!!!
                   1132: #
                   1133: sub display_pdf_form {
                   1134:     my ($whichfoils, $direction, $venv) = @_;
                   1135:     my $temp = 0;
                   1136:     my $result;
                   1137: 
                   1138:     $result .= "\\begin{$venv}";
                   1139:     foreach my $name ( @{$whichfoils} ) {
                   1140: 	
                   1141: 	my $fieldname =
                   1142: 	    $env{'request.symb'} 
                   1143: 	. '&part_'
                   1144: 	    . $Apache::inputtags::part
                   1145: 	    . '&radiobuttonresponse'
                   1146: 	    . '&HWVAL_'
                   1147: 	    . $Apache::inputtags::response['-1'];
                   1148: 	$result .= '\item[{'
                   1149: 	    . &Apache::lonxml::print_pdf_radiobutton( $fieldname,
                   1150: 						      $temp )
                   1151: 	    . '}]'
                   1152: 	    . $Apache::response::foilgroup{ $name . '.text' }
                   1153: 	. "\n";
                   1154: 	
                   1155: 	$temp++;
                   1156:     }
                   1157:     $result .= "\\end{$venv}";
                   1158: 
                   1159:     return $result;
                   1160: }
                   1161: 
                   1162: 
                   1163: ##
                   1164: # Display selected foils:  This is really just a dispatchter to appropriate renderers
                   1165: #  
                   1166: # @param $target   - Target (e.g. 'tex'...).
                   1167: # @param $answer   - True if answers should be shown.
                   1168: # @param $whichfoils - Array of foil selectors that indicate which foils shouild be
                   1169: #                      rendered, in rendering order.
                   1170: # @param $direction- Rendering direction ('horizontal' is the one we look for,
                   1171: #                    otherwise foils are rendered one per line vertically.
                   1172: # @param $bubbles_per_line - number of exam bubbles per line.
                   1173: #
                   1174: # @return string
                   1175: # @retval The rendered problem.
                   1176: 
                   1177: sub displayfoils {
                   1178:     my ($target,$answer,$whichfoils,$direction, $bubbles_per_line)=@_;
                   1179:     my $result;
                   1180: 
                   1181: 
                   1182:     my $part   = $Apache::inputtags::part;
                   1183:     my $solved = $Apache::lonhomework::history{"resource.$part.solved"};
                   1184: 
                   1185:     # Show answers html.
                   1186: 
                   1187:     if ( ( $target ne 'tex' )
                   1188:         && &Apache::response::show_answer() )
                   1189:     {
                   1190: 
                   1191: 	$result = &display_foils_html(
                   1192: 	    $whichfoils, $target, $direction, $part,  1);
                   1193: 	
                   1194: 	# other html
                   1195:     }  elsif ($target ne 'tex') {
                   1196: 	    $result = &display_foils_html($whichfoils, $target, $direction, $part,
                   1197: 					  0, 0);
                   1198: 
                   1199:        # LaTeX rendering:
1.154     raeburn  1200:     } else {
1.155     foxr     1201: 
                   1202: 
                   1203:         my $id            = $Apache::inputtags::response['-1'];
                   1204:         my $part          = $Apache::inputtags::part;
                   1205: 	my $numlines;
                   1206: 	
                   1207: 	# Decide how to bracket the list of foils:
                   1208: 
                   1209: 	my $vertical_env = &latex_vertical_environment();
                   1210: 
                   1211: 	# Rendering for latex exams.
                   1212: 	
                   1213: 	if ( ( $Apache::lonhomework::type eq 'exam' ) )
                   1214: 	{
                   1215: 	    $result .= &display_latex_exam(
                   1216: 		$whichfoils, $bubbles_per_line, $direction, $vertical_env);
                   1217: 
                   1218: 	    $result .= '\vskip 0mm ';
                   1219: 	    
                   1220: 	} else {	    
                   1221: 
                   1222: 	    # Different rendering for PDF form than for a
                   1223: 	    # 'regular' answer direction is honored in both of those
                   1224: 	    #
                   1225: 
                   1226: 	    if ( ($env{'form.pdfFormFields'} eq 'yes')
                   1227: 		    && ($Apache::inputtags::status[-1] eq 'CAN_ANSWER'))
                   1228: 	    {
                   1229: 		$result .= &display_pdf_form($whichfoils, $direction, $vertical_env);
1.154     raeburn  1230: 	    } else {
1.155     foxr     1231: 		$result .= &display_latex($whichfoils,  $direction, $vertical_env );
1.154     raeburn  1232: 	    }
1.155     foxr     1233: 	    $result .= '\vskip 0 mm '; 
                   1234:        
1.154     raeburn  1235: 	}
1.83      albertel 1236:     }
                   1237:     return $result;
1.81      albertel 1238: }
                   1239: 
                   1240: sub displayallanswers {
1.106     albertel 1241:     my @names;
                   1242:     if ( $Apache::response::foilgroup{'names'} ) {
1.154     raeburn  1243: 	@names= @{ $Apache::response::foilgroup{'names'} };
1.106     albertel 1244:     }
1.154     raeburn  1245:     my $result=&Apache::response::answer_header('radiobuttonresponse');
1.81      albertel 1246:     foreach my $name (@names) {
1.154     raeburn  1247: 	$result.=&Apache::response::answer_part('radiobuttonresponse',
                   1248: 				$Apache::response::foilgroup{$name.'.value'});
1.81      albertel 1249:     }
1.154     raeburn  1250:     $result.=&Apache::response::answer_footer('radiobuttonresponse');
1.81      albertel 1251:     return $result;
1.14      albertel 1252: }
                   1253: 
1.28      albertel 1254: sub displayanswers {
1.154     raeburn  1255:     my ($answer, $whichopt, $bubbles_per_line)=@_;
1.124     albertel 1256:     my $result;
                   1257: 
1.154     raeburn  1258:     if ($Apache::lonhomework::type eq 'exam') {
                   1259: 	my $line = int($answer/$bubbles_per_line);
                   1260: 	my $correct = ('A'..'Z')[$answer%$bubbles_per_line];
                   1261: 	$result .= &Apache::response::answer_header('radiobuttonresponse',
                   1262: 						    $line);
                   1263: 	$result .= &Apache::response::answer_part('radiobuttonresponse',
                   1264: 						  $correct);
                   1265:     } else {
                   1266: 	$result .= &Apache::response::answer_header('radiobuttonresponse');
                   1267:     }
                   1268:     foreach my $name (@{ $whichopt }) {
                   1269: 	$result.=&Apache::response::answer_part('radiobuttonresponse',
                   1270: 						$Apache::response::foilgroup{$name.'.value'});
1.105     albertel 1271:     }
1.154     raeburn  1272:     $result.=&Apache::response::answer_footer('radiobuttonresponse');
1.83      albertel 1273:     return $result;
1.28      albertel 1274: }
                   1275: 
1.14      albertel 1276: sub start_conceptgroup {
1.154     raeburn  1277:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1278:     $Apache::radiobuttonresponse::conceptgroup=1;
                   1279:     %Apache::response::conceptgroup=();
1.83      albertel 1280:     my $result;
1.154     raeburn  1281:     if ($target eq 'edit') {
                   1282: 	$result.=&Apache::edit::tag_start($target,$token);
                   1283: 	$result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
                   1284: 	    &Apache::edit::end_row().&Apache::edit::start_spanning_row();
                   1285:     } elsif ($target eq 'modified') {
                   1286: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                   1287: 						     $safeeval,'concept');
                   1288: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.83      albertel 1289:     }
                   1290:     return $result;
1.14      albertel 1291: }
                   1292: 
                   1293: sub end_conceptgroup {
1.154     raeburn  1294:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1295:     $Apache::radiobuttonresponse::conceptgroup=0;
1.83      albertel 1296:     my $result;
1.154     raeburn  1297:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer'  ||
                   1298: 	$target eq 'tex' || $target eq 'analyze') {
                   1299: 	&Apache::response::pick_foil_for_concept($target,
                   1300: 						 ['value','text','location'],
                   1301: 						 \%Apache::hint::radiobutton,
                   1302: 						 $parstack,$safeeval);
                   1303:     } elsif ($target eq 'edit') {
                   1304: 	$result=&Apache::edit::end_table();
1.83      albertel 1305:     }
                   1306:     return $result;
1.26      albertel 1307: }
                   1308: 
                   1309: sub insert_conceptgroup {
1.154     raeburn  1310:     my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
1.83      albertel 1311:     return $result;
1.1       albertel 1312: }
                   1313: 
                   1314: sub start_foil {
1.154     raeburn  1315:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1316:     my $result='';
                   1317:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
                   1318: 	&Apache::lonxml::startredirection;
                   1319: 	if ($target eq 'analyze') {
                   1320: 	    &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
                   1321: 	}
                   1322:     } elsif ($target eq 'edit') {
                   1323: 	$result=&Apache::edit::tag_start($target,$token);
                   1324: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
                   1325: 	$result.=&Apache::edit::select_or_text_arg('Correct Option:','value',
                   1326: 						   ['unused','true','false'],
                   1327: 						   $token);
                   1328: 	my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
                   1329: 						 $safeeval,'-3');
                   1330: 	if ($randomize ne 'no') {
                   1331: 	    $result.=&Apache::edit::select_arg('Location:','location',
                   1332: 					       ['random','top','bottom'],$token);
                   1333: 	}
                   1334: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                   1335:     } elsif ($target eq 'modified') {
                   1336: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                   1337: 						     $safeeval,'value','name',
                   1338: 						     'location');
                   1339: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                   1340:     } 
1.83      albertel 1341:     return $result;
1.1       albertel 1342: }
                   1343: 
                   1344: sub end_foil {
1.154     raeburn  1345:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1346:     my $text='';
                   1347:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
                   1348: 	$text=&Apache::lonxml::endredirection;
                   1349:     }
                   1350:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer'
                   1351: 	|| $target eq 'tex' || $target eq 'analyze') {
                   1352: 	my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
                   1353: 	if ($value ne 'unused') {
                   1354: 	    my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
                   1355: 	    if ($name eq "") {
                   1356: 		&Apache::lonxml::warning(&mt('Foils without names exist. This can cause problems to malfunction.'));
                   1357: 		$name=$Apache::lonxml::curdepth;
                   1358: 	    }
                   1359: 	    if (defined($Apache::response::foilnames{$name})) {
                   1360: 		&Apache::lonxml::error(&mt('Foil name [_1] appears more than once. Foil names need to be unique.','<b><tt>'.$name.'</tt></b>'));
                   1361: 	    }
                   1362: 	    $Apache::response::foilnames{$name}++;
                   1363: 	    my $location =&Apache::lonxml::get_param('location',$parstack,
                   1364: 						     $safeeval);
                   1365: 	    if ( $Apache::radiobuttonresponse::conceptgroup
                   1366: 		 && !&Apache::response::showallfoils() ) {
                   1367: 		push @{ $Apache::response::conceptgroup{'names'} }, $name;
                   1368: 		$Apache::response::conceptgroup{"$name.value"} = $value;
                   1369: 		$Apache::response::conceptgroup{"$name.text"} = $text;	
                   1370: 		$Apache::response::conceptgroup{"$name.location"} = $location;
                   1371: 	    } else {
                   1372: 		push @{ $Apache::response::foilgroup{'names'} }, $name;
                   1373: 		$Apache::response::foilgroup{"$name.value"} = $value;
                   1374: 		$Apache::response::foilgroup{"$name.text"} = $text;
                   1375: 		$Apache::response::foilgroup{"$name.location"} = $location;
                   1376: 	    }
                   1377: 	}
1.18      albertel 1378:     }
1.83      albertel 1379:     return '';
1.1       albertel 1380: }
                   1381: 
1.27      albertel 1382: sub insert_foil {
1.83      albertel 1383:     return '
1.27      albertel 1384: <foil name="" value="unused">
                   1385: <startouttext />
                   1386: <endouttext />
                   1387: </foil>';
                   1388: }
1.151     raeburn  1389: 
1.1       albertel 1390: 1;
                   1391: __END__
1.139     jms      1392: 
                   1393: 
                   1394: 
                   1395: =head1 NAME
                   1396: 
                   1397: Apache::radiobuttonresponse
                   1398: 
                   1399: =head1 SYNOPSIS
                   1400: 
                   1401: Handles multiple-choice style responses.
                   1402: 
                   1403: This is part of the LearningOnline Network with CAPA project
                   1404: described at http://www.lon-capa.org.
                   1405: 
                   1406: =head1 SUBROUTINES
                   1407: 
                   1408: =over
                   1409: 
                   1410: =item start_radiobuttonresponse()
                   1411: 
                   1412: =item bubble_line_count()
                   1413: 
                   1414: =item end_radiobuttonresponse()
                   1415: 
                   1416: =item start_foilgroup()
                   1417: 
                   1418: =item storesurvey()
                   1419: 
                   1420: =item grade_response()
                   1421: 
                   1422: =item end_foilgroup()
                   1423: 
                   1424: =item getfoilcounts()
                   1425: 
                   1426: =item format_prior_answer()
                   1427: 
                   1428: =item displayallfoils()
                   1429: 
                   1430: =item &whichfoils($max,$randomize)
                   1431: 
                   1432: Randomizes the list of foils.
                   1433: Respects
                   1434:   - each foils desire to be randomized
                   1435:   - the existance of Concept groups of foils (select 1 foil from each)
                   1436:   - and selects a single correct statement from all possilble true statments
                   1437:   - and limits it to a toal of $max foils
                   1438: 
                   1439: WARNING: this routine uses the random number generator, it should only
                   1440: be called once per target, otherwise it can cause randomness changes in
                   1441: homework problems.
                   1442: 
                   1443: Arguments
                   1444:   $max - maximum number of foils to select (including the true one)
                   1445:          (so a max of 5 is: 1 true, 4 false)
                   1446: 
                   1447:   $randomize - whether to randomize the listing of foils, by default
                   1448:                will randomize, only if randomize is 'no' will it not
                   1449: 
                   1450: Returns
                   1451:   $answer - location in the array of the correct answer
                   1452:   @foils  - array of foil names in to display order
                   1453: 
                   1454: =item displayfoils()
                   1455: 
                   1456: =item displayallanswers()
                   1457: 
                   1458: =item displayanswers()
                   1459: 
                   1460: =item start_conceptgroup()
                   1461: 
                   1462: =item end_conceptgroup()
                   1463: 
                   1464: =item insert_conceptgroup()
                   1465: 
                   1466: =item start_foil()
                   1467: 
                   1468: =item end_foil()
                   1469: 
                   1470: =item insert_foil()
                   1471: 
                   1472: =back
                   1473: 
                   1474: =cut
1.1       albertel 1475:  

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>