Annotation of loncom/homework/matchresponse.pm, revision 1.34

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # Full matching style response
                      3: #
1.33      albertel    4: # $Id: matchresponse.pm,v 1.32 2004/01/06 20:16:17 sakharuk Exp $
1.1       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
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
                     28: 
                     29: package Apache::matchresponse;
                     30: use strict;
                     31: use HTML::Entities();
                     32: use Math::Random();
1.6       sakharuk   33: use Apache::optionresponse;
                     34: 
1.1       albertel   35: BEGIN {
                     36:     &Apache::lonxml::register('Apache::matchresponse',('matchresponse'));
                     37: }
                     38: 
                     39: sub start_matchresponse {
                     40:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     41:     my $result;
                     42:     #when in a matchresponse use these
                     43:     &Apache::lonxml::register('Apache::matchresponse',
                     44: 			      ('foilgroup','foil','conceptgroup','item',
                     45: 			       'itemgroup'));
                     46:     push (@Apache::lonxml::namespace,'matchresponse');
                     47:     my $id = &Apache::response::start_response($parstack,$safeeval);
                     48:     %Apache::hint::match=();
                     49:     if ($target eq 'meta') {
                     50: 	$result=&Apache::response::meta_package_write('matchresponse');
                     51:     } elsif ($target eq 'edit' ) {
                     52: 	$result.=&Apache::edit::start_table($token).
                     53: 	    '<tr><td>'.&Apache::lonxml::description($token)."</td><td>Delete:".
                     54: 	    &Apache::edit::deletelist($target,$token)
                     55: 	    ."</td><td>&nbsp".&Apache::edit::end_row()
                     56: 	    .&Apache::edit::start_spanning_row();
                     57: 	
                     58: 	$result.=
                     59: 	    &Apache::edit::text_arg('Max Number Of Shown Foils:','max',$token,'4').
                     60: 	    &Apache::edit::select_arg('Randomize Foil Order','randomize',
                     61: 				      ['yes','no'],$token).
                     62: 	    &Apache::edit::end_row().&Apache::edit::start_spanning_row()."\n";
                     63:     } elsif ($target eq 'modified') {
                     64: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                     65: 						     $safeeval,'max',
                     66: 						     'randomize');
                     67: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.28      albertel   68:     } elsif ($target eq 'analyze') {
                     69: 	my $part_id="$Apache::inputtags::part.$id";
                     70: 	push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
1.1       albertel   71:     }
                     72:     return $result;
                     73: }
                     74: 
                     75: sub end_matchresponse {
                     76:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     77:     my $result;
                     78:     if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
                     79:     &Apache::response::end_response;
                     80:     pop @Apache::lonxml::namespace;
                     81:     &Apache::lonxml::deregister('Apache::matchresponse',
                     82: 				('foilgroup','foil','conceptgroup'));
                     83:     return $result;
                     84: }
                     85: 
                     86: %Apache::response::itemgroup=();
                     87: sub start_itemgroup {
                     88:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     89:     my $result;
                     90:     %Apache::response::itemgroup=();
1.4       albertel   91:     %Apache::matchresponse::itemtable=();
1.15      albertel   92: 
1.1       albertel   93:     if ($target eq 'edit') {
                     94: 	$result=&Apache::edit::tag_start($target,$token);
                     95: 	$result.=&Apache::edit::select_arg('Randomize Order:','randomize',
                     96: 					   ['yes','no'],$token);
1.4       albertel   97: 	$result.=&Apache::edit::select_arg('Items Display Location:',
                     98: 					   'location',
                     99: 					   ['top','bottom','left','right'],
                    100: 					   $token);
1.1       albertel  101: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.4       albertel  102:     } elsif ($target eq 'modified') {
                    103: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    104: 						     $safeeval,'randomize',
                    105: 						     'location');
                    106: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.21      sakharuk  107:     } elsif ($target eq 'web' or $target eq 'tex') {
1.4       albertel  108: 	$Apache::matchresponse::itemtable{'location'}=
                    109: 	    &Apache::lonxml::get_param('location',$parstack,$safeeval);
1.1       albertel  110:     }
                    111:     return $result;
                    112: }
                    113: 
                    114: sub end_itemgroup {
                    115:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    116:     my $result;
                    117: 
1.17      albertel  118:     if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
1.1       albertel  119:     if (!defined(@{ $Apache::response::itemgroup{'names'} })) { return; }
                    120:     my @names=@{ $Apache::response::itemgroup{'names'} };
                    121:     my $randomize =&Apache::lonxml::get_param('randomize',$parstack,$safeeval);
1.2       albertel  122:     if ($randomize ne 'no' ) {
1.18      albertel  123: 	@names=&Apache::response::whichorder($#names+1,$randomize,0,
                    124: 					 \%Apache::response::itemgroup);
1.1       albertel  125:     }
1.30      albertel  126:     if ($target eq 'analyze') {
                    127: 	my $partid="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                    128: 	push (@{ $Apache::lonhomework::analyze{"$partid.items"} }, @names);
                    129:     }
1.1       albertel  130:     my %letter_name_map;
                    131:     my %name_letter_map;
                    132:     my @alphabet=('A'..'Z');
                    133:     my $i=0;
                    134:     foreach my $name (@names) {
                    135: 	$letter_name_map{$alphabet[$i]}=$name;
                    136: 	$name_letter_map{$name}=$alphabet[$i];
                    137: 	$i++;
                    138:     }
1.15      albertel  139:     $Apache::response::itemgroup{'letter_name_map'}=\%letter_name_map;
1.1       albertel  140:     $Apache::response::itemgroup{'name_letter_map'}=\%name_letter_map;
                    141:     if ($target eq 'web') {
1.4       albertel  142: 	my $table='<table>';
1.1       albertel  143: 	my $i=0;
                    144: 	foreach my $name (@names) {
1.4       albertel  145: 	    $table.='<tr><td>'.$alphabet[$i].'</td><td>'.
1.1       albertel  146: 		$Apache::response::itemgroup{$name.'.text'}.
                    147: 		    '</td></tr>';
                    148: 	    $i++;
                    149: 	}
1.4       albertel  150: 	$table.='</table>';
                    151: 	$Apache::matchresponse::itemtable{'display'}=$table;
1.5       sakharuk  152:     } elsif ($target eq 'tex') {
1.22      sakharuk  153: 	my $table=' \begin{description} ';
1.5       sakharuk  154: 	my $i=0;
                    155: 	foreach my $name (@names) {
1.11      sakharuk  156: 	    $Apache::response::itemgroup{$name.'.text'}=~s/\$\$/\$/g;
1.19      sakharuk  157: 	    $table.='\item['.$alphabet[$i].'] '.
1.21      sakharuk  158: 		$Apache::response::itemgroup{$name.'.text'};
1.5       sakharuk  159: 	    $i++;
                    160: 	}
1.22      sakharuk  161: 	$table.=' \end{description} \strut ';
1.5       sakharuk  162: 	$Apache::matchresponse::itemtable{'display'}=$table;
1.17      albertel  163:     }
1.1       albertel  164:     return $result;
                    165: }
                    166: 
                    167: sub start_item {
                    168:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    169:     my $result='';
1.30      albertel  170:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.1       albertel  171: 	&Apache::lonxml::startredirection;
                    172:     } elsif ($target eq 'edit') {
1.3       albertel  173: 	my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
                    174: 						 $safeeval,'-2');
1.1       albertel  175: 	$result=&Apache::edit::tag_start($target,$token,"Item");
                    176: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
1.3       albertel  177: 	if ($randomize ne 'no') {
                    178: 	    $result.=&Apache::edit::select_arg('Location:','location',
                    179: 					       ['random','top','bottom'],
                    180: 					       $token);
                    181: 	}
                    182: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.1       albertel  183:     } elsif ($target eq 'modified') {
                    184: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.3       albertel  185: 						     $safeeval,'name',
                    186: 						     'location');
1.1       albertel  187: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    188:     }
                    189:     return $result;
                    190: }
                    191: 
                    192: sub end_item {
                    193:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    194:     my $text ='';
                    195:     my $result = '';
1.30      albertel  196:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.1       albertel  197: 	$text=&Apache::lonxml::endredirection;
                    198:     }
                    199:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
1.30      albertel  200: 	$target eq 'edit' || $target eq 'tex' || $target eq 'analyze') {
1.1       albertel  201: 	my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.3       albertel  202: 	my $location=&Apache::lonxml::get_param('location',$parstack,
                    203: 						$safeeval);
1.1       albertel  204: 	&Apache::lonxml::debug("Got a name of :$name:");
                    205: 	if (!$name) { $name=$Apache::lonxml::curdepth; }
                    206: 	&Apache::lonxml::debug("Using a name of :$name:");
                    207: 	push @{ $Apache::response::itemgroup{'names'} }, $name;
                    208: 	$Apache::response::itemgroup{"$name.text"} = $text;
1.3       albertel  209: 	$Apache::response::itemgroup{"$name.location"} = $location;
1.1       albertel  210:     }
                    211:     if ($target eq 'edit') {
                    212: 	$result.= &Apache::edit::tag_end($target,$token,'');
                    213:     }
                    214:     return $result;
                    215: }
                    216: 
                    217: sub insert_item {
                    218:     return '
                    219: <item name="">
                    220: <startouttext />
                    221: <endouttext />
                    222: </item>';
                    223: }
                    224: 
                    225: %Apache::response::foilgroup=();
                    226: sub start_foilgroup {
                    227:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    228:     my $result;
                    229:     %Apache::response::foilgroup=();
                    230:     $Apache::matchresponse::conceptgroup=0;
                    231:     &Apache::response::setrandomnumber();
                    232:     if ($target eq 'edit') {
                    233: 	$result.=&Apache::edit::start_table($token)
                    234: 	    ."<tr><td>Collection Of Foils</td><td>Delete:"
                    235: 	    .&Apache::edit::deletelist($target,$token)
                    236: 	    ."</td><td>&nbsp;".&Apache::edit::end_row()
                    237:             .&Apache::edit::start_spanning_row()."\n";
                    238:     }
                    239:     return $result;
                    240: }
                    241: 
                    242: sub end_foilgroup {
                    243:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    244:     my $result;
1.28      albertel  245:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || $target eq 'tex' || $target eq 'analyze') {
1.1       albertel  246: 	my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
                    247: 	my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
                    248: 						   $safeeval,'-2');
1.5       sakharuk  249: 	if ($target eq 'web' || $target eq 'tex') {
1.1       albertel  250: 	    $result=&displayfoils($target,$max,$randomize);
                    251: 	} elsif ($target eq 'answer' ) {
                    252: 	    $result=&displayanswers($max,$randomize);
                    253: 	} elsif ( $target eq 'grade') {
                    254: 	    &grade_response($max,$randomize);
1.28      albertel  255: 	} elsif ( $target eq 'analyze') {
                    256: 	    my @shown=&whichfoils($max,$randomize);
                    257: 	    &Apache::response::analyze_store_foilgroup(\@shown,
1.29      albertel  258: 						 ['text','value','location']);
1.28      albertel  259: 	    #FIXME need to store options in some way
1.1       albertel  260: 	}
1.20      sakharuk  261: 	&Apache::lonxml::increment_counter(&getfoilcounts($max));
1.1       albertel  262:     } elsif ($target eq 'edit') {
                    263: 	$result=&Apache::edit::end_table();
                    264:     }
                    265:     return $result;
1.29      albertel  266: }
                    267: 
                    268: sub whichfoils {
                    269:     my ($max,$randomize)=@_;
                    270:     return &Apache::response::whichorder(&getfoilcounts($max),
                    271: 					 $randomize,
                    272: 					 &Apache::response::showallfoils(),
                    273: 					 \%Apache::response::foilgroup);
1.1       albertel  274: }
                    275: 
                    276: sub displayanswers {
                    277:     my ($max,$randomize,@opt)=@_;
                    278:     if (!defined(@{ $Apache::response::foilgroup{'names'} })) { return; }
                    279:     my @names = @{ $Apache::response::foilgroup{'names'} };
1.18      albertel  280:     my @whichfoils = &Apache::response::whichorder(&getfoilcounts($max),
                    281: 						   $randomize,
                    282: 					  &Apache::response::showallfoils(),
                    283: 					 \%Apache::response::foilgroup);
1.1       albertel  284:     my $result=&Apache::response::answer_header('matchresponse');
                    285:     my %name_letter_map;
                    286:     if (defined(%{ $Apache::response::itemgroup{'name_letter_map'} })) {
                    287: 	%name_letter_map=
                    288: 	    %{ $Apache::response::itemgroup{'name_letter_map'} };
                    289:     }
                    290:     foreach my $name (@whichfoils) {
                    291: 	my $value_name=$Apache::response::foilgroup{$name.'.value'};
                    292: 	my $letter=$name_letter_map{$value_name};
                    293: 	$result.=&Apache::response::answer_part('matchresponse',$letter);
                    294:     }
                    295:     $result.=&Apache::response::answer_footer('matchresponse');
                    296:     return $result;
                    297: }
                    298: 
                    299: 
                    300: sub grade_response {
                    301:     my ($max,$randomize)=@_;
1.18      albertel  302:     my (@whichfoils)=&Apache::response::whichorder(&getfoilcounts($max),
                    303: 						   $randomize,
                    304: 				            &Apache::response::showallfoils(),
                    305: 				           \%Apache::response::foilgroup);
1.1       albertel  306:     if (!defined($ENV{'form.submitted'})) { return; }
                    307:     my %responsehash;
                    308:     my %grade;
1.33      albertel  309:     my ($temp,$right,$wrong,$ignored)=(1,0,0,0);
1.1       albertel  310:     my %letter_name_map;
                    311:     if (defined(%{ $Apache::response::itemgroup{'letter_name_map'} })) {
                    312: 	%letter_name_map=
                    313: 	    %{ $Apache::response::itemgroup{'letter_name_map'} };
                    314:     }
1.30      albertel  315:     my @items;
1.1       albertel  316:     foreach my $name (@whichfoils) {
1.34    ! albertel  317: 	my $response = &Apache::response::getresponse($temp,'letter');
1.30      albertel  318: 	push(@items,$response);
1.1       albertel  319: 	my $responsename = $letter_name_map{$response};
                    320: 	my $value=$Apache::response::foilgroup{$name.'.value'};
                    321: 	if ( $response =~ /[^\s]/) {
1.31      albertel  322: 	    $responsehash{$name}=$responsename;
1.33      albertel  323: 	    &Apache::lonxml::debug("submitted a $response($responsename) for $value<br />\n");
1.1       albertel  324: 	    if ($value eq $responsename) {
                    325: 		$grade{$name}='1'; $right++;
                    326: 	    } else {
                    327: 		$grade{$name}='0'; $wrong++;
                    328: 	    }
                    329: 	} else {
                    330: 	    $ignored++;
                    331: 	}
                    332: 	$temp++;
                    333:     }
                    334:     my $part=$Apache::inputtags::part;
                    335:     my $id = $Apache::inputtags::response['-1'];
                    336:     my $responsestr=&Apache::lonnet::hash2str(%responsehash);
1.30      albertel  337:     my $itemstr    =&Apache::lonnet::array2str(@items);
1.1       albertel  338:     my $gradestr   =&Apache::lonnet::hash2str(%grade);
1.30      albertel  339:     my %previous=&Apache::response::check_for_previous($responsestr,$part,$id);
1.1       albertel  340:     &Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored ");
                    341:     $Apache::lonhomework::results{"resource.$part.$id.submission"}=
                    342: 	$responsestr;
1.30      albertel  343:     $Apache::lonhomework::results{"resource.$part.$id.submissionitems"}=
                    344: 	$itemstr;
1.1       albertel  345:     $Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=
                    346: 	$gradestr;
1.34    ! albertel  347:     if (!$Apache::lonhomework::scantronmode) {
        !           348: 	my $ad;
        !           349: 	if ($wrong==0 && $ignored==0) {
        !           350: 	    $ad='EXACT_ANS';
        !           351: 	} elsif ($wrong==0 && $right==0) {
        !           352: 	    #nothing submitted
        !           353: 	} else {
        !           354: 	    if ($ignored==0) {
        !           355: 		$ad='INCORRECT';
        !           356: 	    } else {
        !           357: 		$ad='MISSING_ANSWER';
        !           358: 	    }
        !           359: 	}
        !           360: 	$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
        !           361: 	&Apache::response::handle_previous(\%previous,$ad);
        !           362:     } else {
        !           363: 	my $ad;
        !           364: 	if ($wrong==0 && $right==0) {
        !           365: 	    #nothing submitted
        !           366: 	} else {
        !           367: 	    $ad='ASSIGNED_SCORE';
        !           368: 	    $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
        !           369: 		$ad;
        !           370: 	    $Apache::lonhomework::results{"resource.$part.$id.awarded"}=
        !           371: 		$right/(scalar(@whichopt));
        !           372: 	    $Apache::lonhomework::results{"resource.$part.$id.numfoils"}=
        !           373: 		scalar(@whichopt);
        !           374: 	}
        !           375:     }
1.1       albertel  376: }
                    377: 
1.4       albertel  378: sub itemdisplay {
                    379:     my ($location)=@_;
                    380:     if ($location eq 'top' &&
                    381: 	!defined($Apache::matchresponse::itemtable{'location'})) {
                    382: 	return $Apache::matchresponse::itemtable{'display'};
                    383:     }
                    384:     if ($Apache::matchresponse::itemtable{'location'} eq $location) {
                    385: 	return $Apache::matchresponse::itemtable{'display'};
                    386:     }
                    387:     return undef;
                    388: }
1.1       albertel  389: sub displayfoils {
                    390:     my ($target,$max,$randomize)=@_;
                    391:     my $result;
1.4       albertel  392:     my $question;
1.18      albertel  393:     my (@whichfoils)=&Apache::response::whichorder(&getfoilcounts($max),
                    394: 						   $randomize,
                    395: 				            &Apache::response::showallfoils(),
                    396: 				           \%Apache::response::foilgroup);
1.1       albertel  397:     my $part=$Apache::inputtags::part;
                    398:     my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
                    399:     my %letter_name_map;
                    400:     if (defined(%{ $Apache::response::itemgroup{'letter_name_map'} })) {
                    401: 	%letter_name_map=
                    402: 	    %{ $Apache::response::itemgroup{'letter_name_map'} };
                    403:     }
                    404:     my %name_letter_map;
                    405:     if (defined(%{ $Apache::response::itemgroup{'name_letter_map'} })) {
                    406: 	%name_letter_map=
                    407: 	    %{ $Apache::response::itemgroup{'name_letter_map'} };
                    408:     }
1.25      albertel  409:     if ( &Apache::response::show_answer() && ($target ne 'tex')) {
1.1       albertel  410: 	foreach my $name (@whichfoils) {
                    411: 	    my $text=$Apache::response::foilgroup{$name.'.text'};
                    412: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
                    413: 	    my $letter=$name_letter_map{$value};
1.5       sakharuk  414: 	    if ($target eq 'tex') {
                    415: 		$question.=' \\\\ '.$letter.':'.$text;
                    416: 	    } else {
                    417: 		$question.='<br />'.$letter.':'.$text;
                    418: 	    }
1.1       albertel  419: 	}
                    420:     } else {
                    421: 	my $i = 0;
                    422: 	my $temp=0;
                    423: 	my $id=$Apache::inputtags::response[-1];
                    424: 	my $part=$Apache::inputtags::part;
                    425: 	my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
1.9       sakharuk  426: 	my %lastresponse=&Apache::lonnet::str2hash($lastresponse);    
                    427: 	my @alphabet=('A'..'Z');
1.15      albertel  428: 	my @used_letters=sort(keys(%letter_name_map));
1.20      sakharuk  429: 	my $internal_counter=$Apache::lonxml::counter;
1.1       albertel  430: 	foreach my $name (@whichfoils) {
                    431: 	    my $lastopt=$lastresponse{$name};
                    432: 	    my $last_letter=$name_letter_map{$lastopt};
1.5       sakharuk  433: 	    my $optionlist = '';
1.7       sakharuk  434: 	    if ($target ne 'tex') {
                    435: 		$optionlist="<option></option>\n";
                    436: 	    } else {
                    437: 		if ($Apache::lonhomework::type ne 'exam') {
                    438: 		    $optionlist='\framebox[5 mm][s]{\tiny\strut}';
                    439: 		}
                    440: 	    }
1.1       albertel  441: 	    my $option;
1.15      albertel  442: 	    foreach $option (@used_letters) {
1.1       albertel  443: 		if ($option eq $last_letter) {
1.6       sakharuk  444: 		    if ($target ne 'tex') {$optionlist.="<option selected=\"on\">$option</option>\n";}
1.1       albertel  445: 		} else {
1.6       sakharuk  446: 		    if ($target ne 'tex') {$optionlist.="<option>$option</option>\n";}
1.1       albertel  447: 		}
                    448: 	    }
1.19      sakharuk  449: 	    if ($target ne 'tex' && $Apache::lonhomework::type ne 'exam') {
1.5       sakharuk  450: 	        $optionlist='<select name="HWVAL_'.
                    451: 		    $Apache::inputtags::response[-1].':'.$temp.'">'.
                    452: 		        $optionlist."</select>\n";
                    453: 	    }
1.1       albertel  454: 	    my $text=$Apache::response::foilgroup{$name.'.text'};
1.5       sakharuk  455: 	    if ($target ne 'tex') {
1.19      sakharuk  456: 		if ($Apache::lonhomework::type ne 'exam') {
                    457: 		    $question.='<br />'.$optionlist.$text."\n";
                    458: 		} else {
                    459: 		    $question.='<br />'.$text."\n";
                    460: 		}
1.6       sakharuk  461: 		if ($Apache::lonhomework::type eq 'exam') {
1.27      albertel  462: 		    $question.=&Apache::optionresponse::webbubbles(\@used_letters,\@used_letters,$temp,$last_letter);
1.6       sakharuk  463: 		}
                    464: 	    } else {		
                    465: 		if ($Apache::lonhomework::type eq 'exam') {
                    466: 		    $question.=' '.$optionlist.$text."\n";
1.13      sakharuk  467: 		    my @emptyItems = ();
1.16      albertel  468: 		    for (my $i=0;$i<=$#used_letters;$i++) {push @emptyItems, ' ';}
1.20      sakharuk  469: 		    $question.='\vskip -2 mm\parbox{\textwidth}{\begin{enumerate}\item[\textbf{'.$internal_counter.'}.]\parbox{\textwidth - 5 mm}{'.&Apache::optionresponse::bubbles(\@used_letters,\@emptyItems).'}\end{enumerate}} \vskip -10 mm \strut ';
                    470: 		    $internal_counter++;
1.6       sakharuk  471: 	        } else {
1.22      sakharuk  472: 		    $question.=' '.$optionlist.$text.'\strut\\\\\strut '."\n";
1.6       sakharuk  473: 		}
1.5       sakharuk  474:             } 
1.1       albertel  475: 	    $temp++;
                    476: 	}
1.4       albertel  477:     }
                    478:     if ($result=&itemdisplay('top')) {
                    479: 	$result.=$question;
                    480:     } elsif ($result=&itemdisplay('bottom')) {
                    481: 	$result=$question.$result;
                    482:     } elsif ($result=&itemdisplay('right')) {
1.32      sakharuk  483: 	if ($target ne 'tex') {
                    484: 	    $result='<table><tr><td>'.$question.'</td><td>'.$result.
                    485: 		'</td></tr></table>';
                    486: 	} else {
                    487: 	    $result='\begin{tabular}{p{\textwidth/2}p{\textwidth/2}}\begin{minipage}{\textwidth/2}'.$question.'\end{minipage}&\begin{minipage}{\textwidth/2}'.$result.'\end{minipage}\end{tabular}';
                    488: 	}
1.4       albertel  489:     } elsif ($result=&itemdisplay('left')) {
1.32      sakharuk  490: 	if ($target ne 'tex') {
                    491: 	    $result='<table><tr><td>'.$result.'</td><td>'.$question.
                    492: 		'</td></tr></table>';
                    493: 	} else {
                    494: 	    $result='\begin{tabular}{p{\textwidth/2}p{\textwidth/2}}\begin{minipage}{\textwidth/2}'.$result.'\end{minipage}&\begin{minipage}{\textwidth/2}'.$question.'\end{minipage}\end{tabular}';
                    495: 	}
1.1       albertel  496:     }
1.5       sakharuk  497:     if ($target ne 'tex') {$result.="<br />";} else {$result.=' \\\\ ';}
1.1       albertel  498:     return $result;
                    499: }
                    500: 
                    501: sub getfoilcounts {
                    502:     my ($max)=@_;
                    503:     # +1 since instructors will count from 1
                    504:     my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
                    505:     if (&Apache::response::showallfoils()) { $max=$count; }
                    506:     if ($count>$max) { $count=$max } 
                    507:     &Apache::lonxml::debug("Count is $count from $max");
                    508:     return $count;
                    509: }
                    510: 
                    511: 
                    512: sub start_conceptgroup {
                    513:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    514:     $Apache::matchresponse::conceptgroup=1;
                    515:     %Apache::response::conceptgroup=();
                    516:     my $result;
                    517:     if ($target eq 'edit') {
                    518: 	$result.=&Apache::edit::tag_start($target,$token,
                    519: 					  "Concept Grouped Foils");
                    520: 	$result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
                    521: 	    &Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    522:     }
                    523:     if ($target eq 'modified') {
                    524: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    525: 						     $safeeval,'concept');
                    526: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    527:     }
                    528:     return $result;
                    529: }
                    530: 
                    531: sub end_conceptgroup {
                    532:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    533:     $Apache::matchresponse::conceptgroup=0;
                    534:     my $result='';
1.28      albertel  535:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
                    536: 	$target eq 'tex' || $target eq 'analyze') {
                    537: 	&Apache::response::pick_foil_for_concept($target,
                    538: 						 ['value','text','location'],
                    539: 						 \%Apache::hint::match,
                    540: 						 $parstack,$safeeval);
1.1       albertel  541:     } elsif ($target eq 'edit') {
                    542: 	$result=&Apache::edit::end_table();
                    543:     }
                    544:     return $result;
                    545: }
                    546: 
                    547: sub insert_conceptgroup {
                    548:     my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
                    549:     return $result;
                    550: }
                    551: 
                    552: sub start_foil {
                    553:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    554:     my $result='';
1.28      albertel  555:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.1       albertel  556: 	&Apache::lonxml::startredirection;
                    557:     } elsif ($target eq 'edit') {
                    558: 	$result=&Apache::edit::tag_start($target,$token,"Foil");
                    559: 	my $level='-2';
                    560: 	if ($$tagstack[-2] eq 'conceptgroup') { $level = '-3'; }
                    561: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
                    562: 	my @names;
                    563: 	if (defined(@{ $Apache::response::itemgroup{'names'} })) {
                    564: 	    @names=@{ $Apache::response::itemgroup{'names'} };
                    565: 	}
                    566: 	$result.= &Apache::edit::select_or_text_arg('Correct Option:','value',['unused',@names],$token,'15');
                    567: 	my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
                    568: 						 $safeeval,'-3');
                    569: 	if ($randomize ne 'no') {
                    570: 	    $result.=&Apache::edit::select_arg('Location:','location',
                    571: 					     ['random','top','bottom'],$token);
                    572: 	}
                    573: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    574:     } elsif ($target eq 'modified') {
                    575: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    576: 						     $safeeval,'value',
                    577: 						     'name','location');
                    578: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    579:     }
                    580:     return $result;
                    581: }
                    582: 
                    583: sub end_foil {
                    584:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    585:     my $text ='';
                    586:     my $result = '';
1.28      albertel  587:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.1       albertel  588: 	$text=&Apache::lonxml::endredirection;
                    589:     }
1.28      albertel  590:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
                    591: 	$target eq 'tex' || $target eq 'analyze') {
1.1       albertel  592: 	my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
                    593: 	if ($value ne 'unused') {
                    594: 	    my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    595: 	    my $location =&Apache::lonxml::get_param('location',$parstack,$safeeval);
                    596: 	    &Apache::lonxml::debug("Got a name of :$name:");
                    597: 	    if (!$name) { $name=$Apache::lonxml::curdepth; }
                    598: 	    &Apache::lonxml::debug("Using a name of :$name:");
                    599: 	    if ( $Apache::matchresponse::conceptgroup
                    600: 		 && !&Apache::response::showallfoils() ) {
                    601: 		push @{ $Apache::response::conceptgroup{'names'} }, $name;
                    602: 		$Apache::response::conceptgroup{"$name.value"} = $value;
1.19      sakharuk  603: 		if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
1.14      sakharuk  604: 		    $Apache::response::conceptgroup{"$name.text"} = ' $\triangleright$ '.$text;
                    605: 		} else {
                    606: 		    $Apache::response::conceptgroup{"$name.text"} = $text;
                    607: 		}
1.1       albertel  608: 		$Apache::response::conceptgroup{"$name.location"} = $location;
                    609: 	    } else {
                    610: 		push @{ $Apache::response::foilgroup{'names'} }, $name;
                    611: 		$Apache::response::foilgroup{"$name.value"} = $value;
1.16      albertel  612: 		if ($Apache::lonhomework::type eq 'exam') {
1.19      sakharuk  613: 		    if ($target eq 'tex') {
                    614: 			$Apache::response::foilgroup{"$name.text"} = '\vskip 5 mm $\triangleright$ '.$text;
                    615: 		    } else {
                    616: 			$Apache::response::foilgroup{"$name.text"} = $text;
                    617: 		    }
1.14      sakharuk  618: 		} else {
                    619: 		    if ($target eq 'tex') {
1.24      sakharuk  620: 			$Apache::response::foilgroup{"$name.text"} = $text;
1.14      sakharuk  621: 		    } else {
                    622: 			$Apache::response::foilgroup{"$name.text"} = $text;
                    623: 		    }
                    624: 		}
1.1       albertel  625: 		$Apache::response::foilgroup{"$name.location"} = $location;
                    626: 	    }
                    627: 	}
                    628:     }
                    629:     if ($target eq 'edit') {
                    630: 	$result.= &Apache::edit::tag_end($target,$token,'');
                    631:     }
                    632:     return $result;
                    633: }
                    634: 
                    635: sub insert_foil {
                    636:     return '
                    637: <foil name="" value="unused">
                    638: <startouttext />
                    639: <endouttext />
                    640: </foil>';
                    641: }
                    642: 1;
                    643: __END__

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