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

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

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