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

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # Full matching style response
                      3: #
1.16    ! albertel    4: # $Id: matchresponse.pm,v 1.15 2003/03/26 22:41:34 albertel 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); }
                    105:     } elsif ($target eq 'web') {
                    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: 
                    116:     if (!defined(@{ $Apache::response::itemgroup{'names'} })) { return; }
                    117:     my @names=@{ $Apache::response::itemgroup{'names'} };
                    118:     my $randomize =&Apache::lonxml::get_param('randomize',$parstack,$safeeval);
1.2       albertel  119:     if ($randomize ne 'no' ) {
1.3       albertel  120: 	@names=&whichorder($#names+1,$randomize,0,
                    121: 			   \%Apache::response::itemgroup)
1.1       albertel  122:     }
                    123:     my %letter_name_map;
                    124:     my %name_letter_map;
                    125:     my @alphabet=('A'..'Z');
                    126:     my $i=0;
                    127:     foreach my $name (@names) {
                    128: 	$letter_name_map{$alphabet[$i]}=$name;
                    129: 	$name_letter_map{$name}=$alphabet[$i];
                    130: 	$i++;
                    131:     }
1.15      albertel  132:     $Apache::response::itemgroup{'letter_name_map'}=\%letter_name_map;
1.1       albertel  133:     $Apache::response::itemgroup{'name_letter_map'}=\%name_letter_map;
                    134:     if ($target eq 'web') {
1.4       albertel  135: 	my $table='<table>';
1.1       albertel  136: 	my $i=0;
                    137: 	foreach my $name (@names) {
1.4       albertel  138: 	    $table.='<tr><td>'.$alphabet[$i].'</td><td>'.
1.1       albertel  139: 		$Apache::response::itemgroup{$name.'.text'}.
                    140: 		    '</td></tr>';
                    141: 	    $i++;
                    142: 	}
1.4       albertel  143: 	$table.='</table>';
                    144: 	$Apache::matchresponse::itemtable{'display'}=$table;
1.5       sakharuk  145:     } elsif ($target eq 'tex') {
                    146: 	my $table=' \\\\\\\\ \begin{tabular}{ll} ';
                    147: 	my $i=0;
                    148: 	foreach my $name (@names) {
1.11      sakharuk  149: 	    $Apache::response::itemgroup{$name.'.text'}=~s/\$\$/\$/g;
1.5       sakharuk  150: 	    $table.=' '.$alphabet[$i].' & '.
                    151: 		$Apache::response::itemgroup{$name.'.text'}.
                    152: 		    ' \\\\ ';
                    153: 	    $i++;
                    154: 	}
                    155: 	$table.=' \end{tabular} \\\\ ';
                    156: 	$Apache::matchresponse::itemtable{'display'}=$table;
1.1       albertel  157:     } elsif ($target eq 'edit') { $result=&Apache::edit::end_table(); }
                    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: 	}
                    250:     } elsif ($target eq 'edit') {
                    251: 	$result=&Apache::edit::end_table();
                    252:     }
                    253:     return $result;
                    254: }
                    255: 
                    256: sub displayanswers {
                    257:     my ($max,$randomize,@opt)=@_;
                    258:     if (!defined(@{ $Apache::response::foilgroup{'names'} })) { return; }
                    259:     my @names = @{ $Apache::response::foilgroup{'names'} };
1.3       albertel  260:     my @whichfoils = &whichorder(&getfoilcounts($max),$randomize,
                    261: 				 &Apache::response::showallfoils(),
                    262: 				 \%Apache::response::foilgroup);
1.1       albertel  263:     my $result=&Apache::response::answer_header('matchresponse');
                    264:     my %name_letter_map;
                    265:     if (defined(%{ $Apache::response::itemgroup{'name_letter_map'} })) {
                    266: 	%name_letter_map=
                    267: 	    %{ $Apache::response::itemgroup{'name_letter_map'} };
                    268:     }
                    269:     foreach my $name (@whichfoils) {
                    270: 	my $value_name=$Apache::response::foilgroup{$name.'.value'};
                    271: 	my $letter=$name_letter_map{$value_name};
                    272: 	$result.=&Apache::response::answer_part('matchresponse',$letter);
                    273:     }
                    274:     $result.=&Apache::response::answer_footer('matchresponse');
                    275:     return $result;
                    276: }
                    277: 
                    278: 
                    279: sub grade_response {
                    280:     my ($max,$randomize)=@_;
1.3       albertel  281:     my (@whichfoils)=&whichorder(&getfoilcounts($max),$randomize,
                    282: 				 &Apache::response::showallfoils(),
                    283: 				 \%Apache::response::foilgroup);
1.1       albertel  284:     if (!defined($ENV{'form.submitted'})) { return; }
                    285:     my %responsehash;
                    286:     my %grade;
                    287:     my ($temp,$right,$wrong,$ignored)=(0,0,0,0);
                    288:     my %letter_name_map;
                    289:     if (defined(%{ $Apache::response::itemgroup{'letter_name_map'} })) {
                    290: 	%letter_name_map=
                    291: 	    %{ $Apache::response::itemgroup{'letter_name_map'} };
                    292:     }
                    293:     foreach my $name (@whichfoils) {
                    294: 	my $response = $ENV{'form.HWVAL_'.$Apache::inputtags::response['-1'].":$temp"};
                    295: 	my $responsename = $letter_name_map{$response};
                    296: 	$responsehash{$name}=$responsename;
                    297: 	my $value=$Apache::response::foilgroup{$name.'.value'};
                    298: 	if ( $response =~ /[^\s]/) {
                    299: 	    &Apache::lonxml::debug("submitted a $response for $value<br />\n");
                    300: 	    if ($value eq $responsename) {
                    301: 		$grade{$name}='1'; $right++;
                    302: 	    } else {
                    303: 		$grade{$name}='0'; $wrong++;
                    304: 	    }
                    305: 	} else {
                    306: 	    $ignored++;
                    307: 	}
                    308: 	$temp++;
                    309:     }
                    310:     my $part=$Apache::inputtags::part;
                    311:     my $id = $Apache::inputtags::response['-1'];
                    312:     my $responsestr=&Apache::lonnet::hash2str(%responsehash);
                    313:     my $gradestr   =&Apache::lonnet::hash2str(%grade);
                    314:     my %previous =&Apache::response::check_for_previous($responsestr,
                    315: 							$part,$id);
                    316:     &Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored ");
                    317:     my $ad;
                    318:     if ($wrong==0 && $ignored==0) {
                    319: 	$ad='EXACT_ANS';
                    320:     } elsif ($wrong==0 && $right==0) {
                    321: 	#nothing submitted
                    322:     } else {
                    323: 	if ($ignored==0) {
                    324: 	    $ad='INCORRECT';
                    325: 	} else {
                    326: 	    $ad='MISSING_ANSWER';
                    327: 	}
                    328:     }
                    329:     $Apache::lonhomework::results{"resource.$part.$id.submission"}=
                    330: 	$responsestr;
                    331:     $Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=
                    332: 	$gradestr;
                    333:     $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
                    334:     &Apache::response::handle_previous(\%previous,$ad);
                    335: }
                    336: 
1.4       albertel  337: sub itemdisplay {
                    338:     my ($location)=@_;
                    339:     if ($location eq 'top' &&
                    340: 	!defined($Apache::matchresponse::itemtable{'location'})) {
                    341: 	return $Apache::matchresponse::itemtable{'display'};
                    342:     }
                    343:     if ($Apache::matchresponse::itemtable{'location'} eq $location) {
                    344: 	return $Apache::matchresponse::itemtable{'display'};
                    345:     }
                    346:     return undef;
                    347: }
1.1       albertel  348: sub displayfoils {
                    349:     my ($target,$max,$randomize)=@_;
                    350:     my $result;
1.4       albertel  351:     my $question;
1.3       albertel  352:     my (@whichfoils)=&whichorder(&getfoilcounts($max),$randomize,
                    353: 				 &Apache::response::showallfoils(),
                    354: 				 \%Apache::response::foilgroup);
1.1       albertel  355:     my $part=$Apache::inputtags::part;
                    356:     my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
                    357:     my $status=$Apache::inputtags::status[-1];
                    358:     my %letter_name_map;
                    359:     if (defined(%{ $Apache::response::itemgroup{'letter_name_map'} })) {
                    360: 	%letter_name_map=
                    361: 	    %{ $Apache::response::itemgroup{'letter_name_map'} };
                    362:     }
                    363:     my %name_letter_map;
                    364:     if (defined(%{ $Apache::response::itemgroup{'name_letter_map'} })) {
                    365: 	%name_letter_map=
                    366: 	    %{ $Apache::response::itemgroup{'name_letter_map'} };
                    367:     }
                    368:     if (($solved =~ /^correct/) || ($status eq  'SHOW_ANSWER')) {
                    369: 	foreach my $name (@whichfoils) {
                    370: 	    my $text=$Apache::response::foilgroup{$name.'.text'};
                    371: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
                    372: 	    my $letter=$name_letter_map{$value};
1.5       sakharuk  373: 	    if ($target eq 'tex') {
                    374: 		$question.=' \\\\ '.$letter.':'.$text;
                    375: 	    } else {
                    376: 		$question.='<br />'.$letter.':'.$text;
                    377: 	    }
1.1       albertel  378: 	}
                    379:     } else {
                    380: 	my $i = 0;
                    381: 	my $temp=0;
                    382: 	my $id=$Apache::inputtags::response[-1];
                    383: 	my $part=$Apache::inputtags::part;
                    384: 	my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
1.9       sakharuk  385: 	my %lastresponse=&Apache::lonnet::str2hash($lastresponse);    
                    386: 	my @alphabet=('A'..'Z');
1.15      albertel  387: 	my @used_letters=sort(keys(%letter_name_map));
1.1       albertel  388: 	foreach my $name (@whichfoils) {
                    389: 	    my $lastopt=$lastresponse{$name};
                    390: 	    my $last_letter=$name_letter_map{$lastopt};
1.5       sakharuk  391: 	    my $optionlist = '';
1.7       sakharuk  392: 	    if ($target ne 'tex') {
                    393: 		$optionlist="<option></option>\n";
                    394: 	    } else {
                    395: 		if ($Apache::lonhomework::type ne 'exam') {
                    396: 		    $optionlist='\framebox[5 mm][s]{\tiny\strut}';
                    397: 		}
                    398: 	    }
1.1       albertel  399: 	    my $option;
1.15      albertel  400: 	    foreach $option (@used_letters) {
1.1       albertel  401: 		if ($option eq $last_letter) {
1.6       sakharuk  402: 		    if ($target ne 'tex') {$optionlist.="<option selected=\"on\">$option</option>\n";}
1.1       albertel  403: 		} else {
1.6       sakharuk  404: 		    if ($target ne 'tex') {$optionlist.="<option>$option</option>\n";}
1.1       albertel  405: 		}
                    406: 	    }
1.5       sakharuk  407: 	    if ($target ne 'tex') {
                    408: 	        $optionlist='<select name="HWVAL_'.
                    409: 		    $Apache::inputtags::response[-1].':'.$temp.'">'.
                    410: 		        $optionlist."</select>\n";
                    411: 	    }
1.1       albertel  412: 	    my $text=$Apache::response::foilgroup{$name.'.text'};
1.5       sakharuk  413: 	    if ($target ne 'tex') {
                    414: 		$question.='<br />'.$optionlist.$text."\n";
1.6       sakharuk  415: 		if ($Apache::lonhomework::type eq 'exam') {
1.15      albertel  416: 		    $question.=&Apache::optionresponse::webbubbles(\@used_letters,\@used_letters);
1.6       sakharuk  417: 		}
                    418: 	    } else {		
                    419: 		if ($Apache::lonhomework::type eq 'exam') {
                    420: 		    $question.=' '.$optionlist.$text."\n";
1.13      sakharuk  421: 		    my @emptyItems = ();
1.16    ! albertel  422: 		    for (my $i=0;$i<=$#used_letters;$i++) {push @emptyItems, ' ';}
        !           423: 		    $question.='\vskip -2 mm\parbox{\textwidth}{\begin{enumerate}\item[\textbf{'.$Apache::lonxml::counter.'}.]\parbox{\textwidth - 5 mm}{'.&Apache::optionresponse::bubbles(\@used_letters,\@emptyItems).'}\end{enumerate}} \vskip -10 mm \strut ';
1.6       sakharuk  424: 	        } else {
                    425: 		    $question.=' \\\\ '.$optionlist.$text."\n";
                    426: 		}
1.5       sakharuk  427:             } 
1.1       albertel  428: 	    $temp++;
                    429: 	}
1.4       albertel  430:     }
                    431:     if ($result=&itemdisplay('top')) {
                    432: 	$result.=$question;
                    433:     } elsif ($result=&itemdisplay('bottom')) {
                    434: 	$result=$question.$result;
                    435:     } elsif ($result=&itemdisplay('right')) {
                    436: 	$result='<table><tr><td>'.$question.'</td><td>'.$result.
                    437: 	    '</td></tr></table>';
                    438:     } elsif ($result=&itemdisplay('left')) {
                    439: 	$result='<table><tr><td>'.$result.'</td><td>'.$question.
                    440: 	    '</td></tr></table>';
1.1       albertel  441:     }
1.5       sakharuk  442:     if ($target ne 'tex') {$result.="<br />";} else {$result.=' \\\\ ';}
1.1       albertel  443:     return $result;
                    444: }
                    445: 
                    446: sub getfoilcounts {
                    447:     my ($max)=@_;
                    448:     # +1 since instructors will count from 1
                    449:     my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
                    450:     if (&Apache::response::showallfoils()) { $max=$count; }
                    451:     if ($count>$max) { $count=$max } 
                    452:     &Apache::lonxml::debug("Count is $count from $max");
                    453:     return $count;
                    454: }
                    455: 
1.3       albertel  456: sub whichorder {
                    457:     my ($max,$randomize,$showall,$hash)=@_;
                    458:     #&Apache::lonxml::debug("man $max randomize $randomize");
                    459:     if (!defined(@{ $$hash{'names'} })) { return; }
                    460:     my @names = @{ $$hash{'names'} };
1.1       albertel  461:     my @whichopt =();
                    462:     my (%top,@toplist,%bottom,@bottomlist);
1.3       albertel  463:     if (!($showall || ($randomize eq 'no'))) {
1.1       albertel  464: 	my $current=0;
                    465: 	foreach my $name (@names) {
                    466: 	    $current++;
1.3       albertel  467: 	    if ($$hash{"$name.location"} eq 'top') {
1.1       albertel  468: 		$top{$name}=$current;
1.3       albertel  469: 	    } elsif ($$hash{"$name.location"} eq 'bottom') {
1.1       albertel  470: 		$bottom{$name}=$current;
                    471: 	    }
                    472: 	}
                    473:     }
                    474:     while ((($#whichopt+1) < $max) && ($#names > -1)) {
1.3       albertel  475: 	#&Apache::lonxml::debug("Have $#whichopt max is $max");
1.1       albertel  476: 	my $aopt;
1.3       albertel  477: 	if ($showall || ($randomize eq 'no')) {
1.1       albertel  478: 	    $aopt=0;
                    479: 	} else {
                    480: 	    $aopt=int(&Math::Random::random_uniform() * ($#names+1));
                    481: 	}
1.3       albertel  482: 	#&Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
1.1       albertel  483: 	$aopt=splice(@names,$aopt,1);
1.3       albertel  484: 	#&Apache::lonxml::debug("Picked $aopt");
1.1       albertel  485: 	if ($top{$aopt}) {
                    486: 	    $toplist[$top{$aopt}]=$aopt;
                    487: 	} elsif ($bottom{$aopt}) {
                    488: 	    $bottomlist[$bottom{$aopt}]=$aopt;
                    489: 	} else {
                    490: 	    push (@whichopt,$aopt);
                    491: 	}
                    492:     }
                    493:     for (my $i=0;$i<=$#toplist;$i++) {
                    494: 	if ($toplist[$i]) { unshift(@whichopt,$toplist[$i]) }
                    495:     }
                    496:     for (my $i=0;$i<=$#bottomlist;$i++) {
                    497: 	if ($bottomlist[$i]) { push(@whichopt,$bottomlist[$i]) }
                    498:     }
                    499: 
                    500:     return @whichopt;
                    501: }
                    502: 
                    503: sub start_conceptgroup {
                    504:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    505:     $Apache::matchresponse::conceptgroup=1;
                    506:     %Apache::response::conceptgroup=();
                    507:     my $result;
                    508:     if ($target eq 'edit') {
                    509: 	$result.=&Apache::edit::tag_start($target,$token,
                    510: 					  "Concept Grouped Foils");
                    511: 	$result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
                    512: 	    &Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    513:     }
                    514:     if ($target eq 'modified') {
                    515: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    516: 						     $safeeval,'concept');
                    517: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    518:     }
                    519:     return $result;
                    520: }
                    521: 
                    522: sub end_conceptgroup {
                    523:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    524:     $Apache::matchresponse::conceptgroup=0;
                    525:     my $result='';
                    526:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ) {
                    527: 	#if not there aren't any foils to display and thus no question
                    528: 	if (defined(@{ $Apache::response::conceptgroup{'names'} })) {
                    529: 	    my @names = @{ $Apache::response::conceptgroup{'names'} };
                    530: 	    my $pick=int(&Math::Random::random_uniform() * ($#names+1));
                    531: 	    my $name=$names[$pick];
                    532: 	    push @{ $Apache::response::foilgroup{'names'} }, $name;
                    533: 	    $Apache::response::foilgroup{"$name.value"} =
                    534: 		$Apache::response::conceptgroup{"$name.value"};
                    535: 	    $Apache::response::foilgroup{"$name.text"} =
                    536: 		$Apache::response::conceptgroup{"$name.text"};
                    537: 	    $Apache::response::foilgroup{"$name.location"} =
                    538: 		$Apache::response::conceptgroup{"$name.location"};
                    539: 	    my $concept = &Apache::lonxml::get_param('concept',$parstack,
                    540: 						     $safeeval);
                    541: 	    $Apache::response::foilgroup{"$name.concept"} = $concept;
                    542: 	    &Apache::lonxml::debug("Selecting $name in $concept");
                    543: 	    if ($target eq 'web') {
                    544: 		my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                    545: 		push(@{ $Apache::hint::match{"$part_id.concepts"} },
                    546: 		     $concept);
                    547: 		$Apache::hint::match{"$part_id.concept.$concept"}=
                    548: 		    $Apache::response::conceptgroup{'names'};
                    549: 	    }
                    550: 	}
                    551:     } elsif ($target eq 'edit') {
                    552: 	$result=&Apache::edit::end_table();
                    553:     }
                    554:     return $result;
                    555: }
                    556: 
                    557: sub insert_conceptgroup {
                    558:     my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
                    559:     return $result;
                    560: }
                    561: 
                    562: sub start_foil {
                    563:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    564:     my $result='';
1.5       sakharuk  565:     if ($target eq 'web' || $target eq 'tex') {
1.1       albertel  566: 	&Apache::lonxml::startredirection;
                    567:     } elsif ($target eq 'edit') {
                    568: 	$result=&Apache::edit::tag_start($target,$token,"Foil");
                    569: 	my $level='-2';
                    570: 	if ($$tagstack[-2] eq 'conceptgroup') { $level = '-3'; }
                    571: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
                    572: 	my @names;
                    573: 	if (defined(@{ $Apache::response::itemgroup{'names'} })) {
                    574: 	    @names=@{ $Apache::response::itemgroup{'names'} };
                    575: 	}
                    576: 	$result.= &Apache::edit::select_or_text_arg('Correct Option:','value',['unused',@names],$token,'15');
                    577: 	my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
                    578: 						 $safeeval,'-3');
                    579: 	if ($randomize ne 'no') {
                    580: 	    $result.=&Apache::edit::select_arg('Location:','location',
                    581: 					     ['random','top','bottom'],$token);
                    582: 	}
                    583: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    584:     } elsif ($target eq 'modified') {
                    585: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    586: 						     $safeeval,'value',
                    587: 						     'name','location');
                    588: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    589:     }
                    590:     return $result;
                    591: }
                    592: 
                    593: sub end_foil {
                    594:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    595:     my $text ='';
                    596:     my $result = '';
1.5       sakharuk  597:     if ($target eq 'web' || $target eq 'tex') {
1.1       albertel  598: 	$text=&Apache::lonxml::endredirection;
                    599:     }
1.5       sakharuk  600:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
1.1       albertel  601: 	my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
                    602: 	if ($value ne 'unused') {
                    603: 	    my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    604: 	    my $location =&Apache::lonxml::get_param('location',$parstack,$safeeval);
                    605: 	    &Apache::lonxml::debug("Got a name of :$name:");
                    606: 	    if (!$name) { $name=$Apache::lonxml::curdepth; }
                    607: 	    &Apache::lonxml::debug("Using a name of :$name:");
                    608: 	    if ( $Apache::matchresponse::conceptgroup
                    609: 		 && !&Apache::response::showallfoils() ) {
                    610: 		push @{ $Apache::response::conceptgroup{'names'} }, $name;
                    611: 		$Apache::response::conceptgroup{"$name.value"} = $value;
1.14      sakharuk  612: 		if ($target eq 'tex') {
                    613: 		    $Apache::response::conceptgroup{"$name.text"} = ' $\triangleright$ '.$text;
                    614: 		} else {
                    615: 		    $Apache::response::conceptgroup{"$name.text"} = $text;
                    616: 		}
1.1       albertel  617: 		$Apache::response::conceptgroup{"$name.location"} = $location;
                    618: 	    } else {
                    619: 		push @{ $Apache::response::foilgroup{'names'} }, $name;
                    620: 		$Apache::response::foilgroup{"$name.value"} = $value;
1.16    ! albertel  621: 		if ($Apache::lonhomework::type eq 'exam') {
1.14      sakharuk  622: 		    $Apache::response::foilgroup{"$name.text"} = '\vskip 5 mm $\triangleright$ '.$text;
                    623: 		} else {
                    624: 		    if ($target eq 'tex') {
                    625: 			$Apache::response::foilgroup{"$name.text"} = ' $\triangleright$ '.$text;
                    626: 		    } else {
                    627: 			$Apache::response::foilgroup{"$name.text"} = $text;
                    628: 		    }
                    629: 		}
1.1       albertel  630: 		$Apache::response::foilgroup{"$name.location"} = $location;
                    631: 	    }
                    632: 	}
                    633:     }
                    634:     if ($target eq 'edit') {
                    635: 	$result.= &Apache::edit::tag_end($target,$token,'');
                    636:     }
                    637:     return $result;
                    638: }
                    639: 
                    640: sub insert_foil {
                    641:     return '
                    642: <foil name="" value="unused">
                    643: <startouttext />
                    644: <endouttext />
                    645: </foil>';
                    646: }
                    647: 1;
                    648: __END__

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