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

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

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