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

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

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