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

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

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