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

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # Full matching style response
                      3: #
1.14    ! sakharuk    4: # $Id: matchresponse.pm,v 1.13 2003/03/25 22:21:20 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.14    ! sakharuk   36: my $flag_for_exam_printing = 1;
1.13      sakharuk   37: my @Items = ();
                     38: 
1.1       albertel   39: BEGIN {
                     40:     &Apache::lonxml::register('Apache::matchresponse',('matchresponse'));
                     41: }
                     42: 
                     43: sub start_matchresponse {
                     44:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     45:     my $result;
                     46:     #when in a matchresponse use these
                     47:     &Apache::lonxml::register('Apache::matchresponse',
                     48: 			      ('foilgroup','foil','conceptgroup','item',
                     49: 			       'itemgroup'));
                     50:     push (@Apache::lonxml::namespace,'matchresponse');
                     51:     my $id = &Apache::response::start_response($parstack,$safeeval);
                     52:     %Apache::hint::match=();
                     53:     if ($target eq 'meta') {
                     54: 	$result=&Apache::response::meta_package_write('matchresponse');
                     55:     } elsif ($target eq 'edit' ) {
                     56: 	$result.=&Apache::edit::start_table($token).
                     57: 	    '<tr><td>'.&Apache::lonxml::description($token)."</td><td>Delete:".
                     58: 	    &Apache::edit::deletelist($target,$token)
                     59: 	    ."</td><td>&nbsp".&Apache::edit::end_row()
                     60: 	    .&Apache::edit::start_spanning_row();
                     61: 	
                     62: 	$result.=
                     63: 	    &Apache::edit::text_arg('Max Number Of Shown Foils:','max',$token,'4').
                     64: 	    &Apache::edit::select_arg('Randomize Foil Order','randomize',
                     65: 				      ['yes','no'],$token).
                     66: 	    &Apache::edit::end_row().&Apache::edit::start_spanning_row()."\n";
                     67:     } elsif ($target eq 'modified') {
                     68: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                     69: 						     $safeeval,'max',
                     70: 						     'randomize');
                     71: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                     72:     }
                     73:     return $result;
                     74: }
                     75: 
                     76: sub end_matchresponse {
                     77:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     78:     my $result;
                     79:     if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
                     80:     &Apache::response::end_response;
                     81:     pop @Apache::lonxml::namespace;
                     82:     &Apache::lonxml::deregister('Apache::matchresponse',
                     83: 				('foilgroup','foil','conceptgroup'));
                     84:     return $result;
                     85: }
                     86: 
                     87: %Apache::response::itemgroup=();
                     88: sub start_itemgroup {
                     89:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     90:     my $result;
                     91:     %Apache::response::itemgroup=();
1.4       albertel   92:     %Apache::matchresponse::itemtable=();
1.1       albertel   93:     if ($target eq 'edit') {
                     94: 	$result=&Apache::edit::tag_start($target,$token);
                     95: 	$result.=&Apache::edit::select_arg('Randomize Order:','randomize',
                     96: 					   ['yes','no'],$token);
1.4       albertel   97: 	$result.=&Apache::edit::select_arg('Items Display Location:',
                     98: 					   'location',
                     99: 					   ['top','bottom','left','right'],
                    100: 					   $token);
1.1       albertel  101: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.4       albertel  102:     } elsif ($target eq 'modified') {
                    103: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    104: 						     $safeeval,'randomize',
                    105: 						     'location');
                    106: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    107:     } elsif ($target eq 'web') {
                    108: 	$Apache::matchresponse::itemtable{'location'}=
                    109: 	    &Apache::lonxml::get_param('location',$parstack,$safeeval);
1.1       albertel  110:     }
                    111:     return $result;
                    112: }
                    113: 
                    114: sub end_itemgroup {
                    115:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    116:     my $result;
                    117: 
                    118:     if (!defined(@{ $Apache::response::itemgroup{'names'} })) { return; }
                    119:     my @names=@{ $Apache::response::itemgroup{'names'} };
                    120:     my $randomize =&Apache::lonxml::get_param('randomize',$parstack,$safeeval);
1.2       albertel  121:     if ($randomize ne 'no' ) {
1.3       albertel  122: 	@names=&whichorder($#names+1,$randomize,0,
                    123: 			   \%Apache::response::itemgroup)
1.1       albertel  124:     }
                    125:     my %letter_name_map;
                    126:     my %name_letter_map;
                    127:     my @alphabet=('A'..'Z');
                    128:     my $i=0;
                    129:     foreach my $name (@names) {
                    130: 	$letter_name_map{$alphabet[$i]}=$name;
                    131: 	$name_letter_map{$name}=$alphabet[$i];
1.13      sakharuk  132: 	$Items[$i] = $alphabet[$i];
1.1       albertel  133: 	$i++;
                    134:     }
1.13      sakharuk  135:      $Apache::response::itemgroup{'letter_name_map'}=\%letter_name_map;
1.1       albertel  136:     $Apache::response::itemgroup{'name_letter_map'}=\%name_letter_map;
                    137:     if ($target eq 'web') {
1.4       albertel  138: 	my $table='<table>';
1.1       albertel  139: 	my $i=0;
                    140: 	foreach my $name (@names) {
1.4       albertel  141: 	    $table.='<tr><td>'.$alphabet[$i].'</td><td>'.
1.1       albertel  142: 		$Apache::response::itemgroup{$name.'.text'}.
                    143: 		    '</td></tr>';
                    144: 	    $i++;
                    145: 	}
1.4       albertel  146: 	$table.='</table>';
                    147: 	$Apache::matchresponse::itemtable{'display'}=$table;
1.5       sakharuk  148:     } elsif ($target eq 'tex') {
                    149: 	my $table=' \\\\\\\\ \begin{tabular}{ll} ';
                    150: 	my $i=0;
                    151: 	foreach my $name (@names) {
1.11      sakharuk  152: 	    $Apache::response::itemgroup{$name.'.text'}=~s/\$\$/\$/g;
1.5       sakharuk  153: 	    $table.=' '.$alphabet[$i].' & '.
                    154: 		$Apache::response::itemgroup{$name.'.text'}.
                    155: 		    ' \\\\ ';
                    156: 	    $i++;
                    157: 	}
                    158: 	$table.=' \end{tabular} \\\\ ';
                    159: 	$Apache::matchresponse::itemtable{'display'}=$table;
1.1       albertel  160:     } elsif ($target eq 'edit') { $result=&Apache::edit::end_table(); }
                    161:     return $result;
                    162: }
                    163: 
                    164: sub start_item {
                    165:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    166:     my $result='';
1.5       sakharuk  167:     if ($target eq 'web' || $target eq 'tex') {
1.1       albertel  168: 	&Apache::lonxml::startredirection;
                    169:     } elsif ($target eq 'edit') {
1.3       albertel  170: 	my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
                    171: 						 $safeeval,'-2');
1.1       albertel  172: 	$result=&Apache::edit::tag_start($target,$token,"Item");
                    173: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
1.3       albertel  174: 	if ($randomize ne 'no') {
                    175: 	    $result.=&Apache::edit::select_arg('Location:','location',
                    176: 					       ['random','top','bottom'],
                    177: 					       $token);
                    178: 	}
                    179: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.1       albertel  180:     } elsif ($target eq 'modified') {
                    181: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.3       albertel  182: 						     $safeeval,'name',
                    183: 						     'location');
1.1       albertel  184: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    185:     }
                    186:     return $result;
                    187: }
                    188: 
                    189: sub end_item {
                    190:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    191:     my $text ='';
                    192:     my $result = '';
1.5       sakharuk  193:     if ($target eq 'web' || $target eq 'tex') {
1.1       albertel  194: 	$text=&Apache::lonxml::endredirection;
                    195:     }
                    196:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
1.5       sakharuk  197: 	$target eq 'edit' || $target eq 'tex') {
1.1       albertel  198: 	my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.3       albertel  199: 	my $location=&Apache::lonxml::get_param('location',$parstack,
                    200: 						$safeeval);
1.1       albertel  201: 	&Apache::lonxml::debug("Got a name of :$name:");
                    202: 	if (!$name) { $name=$Apache::lonxml::curdepth; }
                    203: 	&Apache::lonxml::debug("Using a name of :$name:");
                    204: 	push @{ $Apache::response::itemgroup{'names'} }, $name;
                    205: 	$Apache::response::itemgroup{"$name.text"} = $text;
1.3       albertel  206: 	$Apache::response::itemgroup{"$name.location"} = $location;
1.1       albertel  207:     }
                    208:     if ($target eq 'edit') {
                    209: 	$result.= &Apache::edit::tag_end($target,$token,'');
                    210:     }
                    211:     return $result;
                    212: }
                    213: 
                    214: sub insert_item {
                    215:     return '
                    216: <item name="">
                    217: <startouttext />
                    218: <endouttext />
                    219: </item>';
                    220: }
                    221: 
                    222: %Apache::response::foilgroup=();
                    223: sub start_foilgroup {
                    224:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    225:     my $result;
                    226:     %Apache::response::foilgroup=();
                    227:     $Apache::matchresponse::conceptgroup=0;
                    228:     &Apache::response::setrandomnumber();
                    229:     if ($target eq 'edit') {
                    230: 	$result.=&Apache::edit::start_table($token)
                    231: 	    ."<tr><td>Collection Of Foils</td><td>Delete:"
                    232: 	    .&Apache::edit::deletelist($target,$token)
                    233: 	    ."</td><td>&nbsp;".&Apache::edit::end_row()
                    234:             .&Apache::edit::start_spanning_row()."\n";
                    235:     }
                    236:     return $result;
                    237: }
                    238: 
                    239: sub end_foilgroup {
                    240:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    241:     my $result;
1.5       sakharuk  242:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || $target eq 'tex') {
1.1       albertel  243: 	my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
                    244: 	my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
                    245: 						   $safeeval,'-2');
1.5       sakharuk  246: 	if ($target eq 'web' || $target eq 'tex') {
1.1       albertel  247: 	    $result=&displayfoils($target,$max,$randomize);
                    248: 	} elsif ($target eq 'answer' ) {
                    249: 	    $result=&displayanswers($max,$randomize);
                    250: 	} elsif ( $target eq 'grade') {
                    251: 	    &grade_response($max,$randomize);
                    252: 	}
                    253:     } elsif ($target eq 'edit') {
                    254: 	$result=&Apache::edit::end_table();
                    255:     }
                    256:     return $result;
                    257: }
                    258: 
                    259: sub displayanswers {
                    260:     my ($max,$randomize,@opt)=@_;
                    261:     if (!defined(@{ $Apache::response::foilgroup{'names'} })) { return; }
                    262:     my @names = @{ $Apache::response::foilgroup{'names'} };
1.3       albertel  263:     my @whichfoils = &whichorder(&getfoilcounts($max),$randomize,
                    264: 				 &Apache::response::showallfoils(),
                    265: 				 \%Apache::response::foilgroup);
1.1       albertel  266:     my $result=&Apache::response::answer_header('matchresponse');
                    267:     my %name_letter_map;
                    268:     if (defined(%{ $Apache::response::itemgroup{'name_letter_map'} })) {
                    269: 	%name_letter_map=
                    270: 	    %{ $Apache::response::itemgroup{'name_letter_map'} };
                    271:     }
                    272:     foreach my $name (@whichfoils) {
                    273: 	my $value_name=$Apache::response::foilgroup{$name.'.value'};
                    274: 	my $letter=$name_letter_map{$value_name};
                    275: 	$result.=&Apache::response::answer_part('matchresponse',$letter);
                    276:     }
                    277:     $result.=&Apache::response::answer_footer('matchresponse');
                    278:     return $result;
                    279: }
                    280: 
                    281: 
                    282: sub grade_response {
                    283:     my ($max,$randomize)=@_;
1.3       albertel  284:     my (@whichfoils)=&whichorder(&getfoilcounts($max),$randomize,
                    285: 				 &Apache::response::showallfoils(),
                    286: 				 \%Apache::response::foilgroup);
1.1       albertel  287:     if (!defined($ENV{'form.submitted'})) { return; }
                    288:     my %responsehash;
                    289:     my %grade;
                    290:     my ($temp,$right,$wrong,$ignored)=(0,0,0,0);
                    291:     my %letter_name_map;
                    292:     if (defined(%{ $Apache::response::itemgroup{'letter_name_map'} })) {
                    293: 	%letter_name_map=
                    294: 	    %{ $Apache::response::itemgroup{'letter_name_map'} };
                    295:     }
                    296:     foreach my $name (@whichfoils) {
                    297: 	my $response = $ENV{'form.HWVAL_'.$Apache::inputtags::response['-1'].":$temp"};
                    298: 	my $responsename = $letter_name_map{$response};
                    299: 	$responsehash{$name}=$responsename;
                    300: 	my $value=$Apache::response::foilgroup{$name.'.value'};
                    301: 	if ( $response =~ /[^\s]/) {
                    302: 	    &Apache::lonxml::debug("submitted a $response for $value<br />\n");
                    303: 	    if ($value eq $responsename) {
                    304: 		$grade{$name}='1'; $right++;
                    305: 	    } else {
                    306: 		$grade{$name}='0'; $wrong++;
                    307: 	    }
                    308: 	} else {
                    309: 	    $ignored++;
                    310: 	}
                    311: 	$temp++;
                    312:     }
                    313:     my $part=$Apache::inputtags::part;
                    314:     my $id = $Apache::inputtags::response['-1'];
                    315:     my $responsestr=&Apache::lonnet::hash2str(%responsehash);
                    316:     my $gradestr   =&Apache::lonnet::hash2str(%grade);
                    317:     my %previous =&Apache::response::check_for_previous($responsestr,
                    318: 							$part,$id);
                    319:     &Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored ");
                    320:     my $ad;
                    321:     if ($wrong==0 && $ignored==0) {
                    322: 	$ad='EXACT_ANS';
                    323:     } elsif ($wrong==0 && $right==0) {
                    324: 	#nothing submitted
                    325:     } else {
                    326: 	if ($ignored==0) {
                    327: 	    $ad='INCORRECT';
                    328: 	} else {
                    329: 	    $ad='MISSING_ANSWER';
                    330: 	}
                    331:     }
                    332:     $Apache::lonhomework::results{"resource.$part.$id.submission"}=
                    333: 	$responsestr;
                    334:     $Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=
                    335: 	$gradestr;
                    336:     $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
                    337:     &Apache::response::handle_previous(\%previous,$ad);
                    338: }
                    339: 
1.4       albertel  340: sub itemdisplay {
                    341:     my ($location)=@_;
                    342:     if ($location eq 'top' &&
                    343: 	!defined($Apache::matchresponse::itemtable{'location'})) {
                    344: 	return $Apache::matchresponse::itemtable{'display'};
                    345:     }
                    346:     if ($Apache::matchresponse::itemtable{'location'} eq $location) {
                    347: 	return $Apache::matchresponse::itemtable{'display'};
                    348:     }
                    349:     return undef;
                    350: }
1.1       albertel  351: sub displayfoils {
                    352:     my ($target,$max,$randomize)=@_;
                    353:     my $result;
1.4       albertel  354:     my $question;
1.3       albertel  355:     my (@whichfoils)=&whichorder(&getfoilcounts($max),$randomize,
                    356: 				 &Apache::response::showallfoils(),
                    357: 				 \%Apache::response::foilgroup);
1.1       albertel  358:     my $part=$Apache::inputtags::part;
                    359:     my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
                    360:     my $status=$Apache::inputtags::status[-1];
                    361:     my %letter_name_map;
                    362:     if (defined(%{ $Apache::response::itemgroup{'letter_name_map'} })) {
                    363: 	%letter_name_map=
                    364: 	    %{ $Apache::response::itemgroup{'letter_name_map'} };
                    365:     }
                    366:     my %name_letter_map;
                    367:     if (defined(%{ $Apache::response::itemgroup{'name_letter_map'} })) {
                    368: 	%name_letter_map=
                    369: 	    %{ $Apache::response::itemgroup{'name_letter_map'} };
                    370:     }
                    371:     if (($solved =~ /^correct/) || ($status eq  'SHOW_ANSWER')) {
                    372: 	foreach my $name (@whichfoils) {
                    373: 	    my $text=$Apache::response::foilgroup{$name.'.text'};
                    374: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
                    375: 	    my $letter=$name_letter_map{$value};
1.5       sakharuk  376: 	    if ($target eq 'tex') {
                    377: 		$question.=' \\\\ '.$letter.':'.$text;
                    378: 	    } else {
                    379: 		$question.='<br />'.$letter.':'.$text;
                    380: 	    }
1.1       albertel  381: 	}
                    382:     } else {
                    383: 	my $i = 0;
                    384: 	my $temp=0;
                    385: 	my $id=$Apache::inputtags::response[-1];
                    386: 	my $part=$Apache::inputtags::part;
                    387: 	my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
1.9       sakharuk  388: 	my %lastresponse=&Apache::lonnet::str2hash($lastresponse);    
                    389: 	my @alphabet=('A'..'Z');
1.13      sakharuk  390:         my @Items_letter = splice @alphabet, 0, $#Items + 1;
1.1       albertel  391: 	foreach my $name (@whichfoils) {
                    392: 	    my $lastopt=$lastresponse{$name};
                    393: 	    my $last_letter=$name_letter_map{$lastopt};
1.5       sakharuk  394: 	    my $optionlist = '';
1.7       sakharuk  395: 	    if ($target ne 'tex') {
                    396: 		$optionlist="<option></option>\n";
                    397: 	    } else {
                    398: 		if ($Apache::lonhomework::type ne 'exam') {
                    399: 		    $optionlist='\framebox[5 mm][s]{\tiny\strut}';
                    400: 		}
                    401: 	    }
1.1       albertel  402: 	    my $option;
1.13      sakharuk  403: 	    foreach $option (sort(keys(%letter_name_map))) {
1.1       albertel  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: 	    }
1.1       albertel  415: 	    my $text=$Apache::response::foilgroup{$name.'.text'};
1.5       sakharuk  416: 	    if ($target ne 'tex') {
                    417: 		$question.='<br />'.$optionlist.$text."\n";
1.6       sakharuk  418: 		if ($Apache::lonhomework::type eq 'exam') {
1.13      sakharuk  419: 		    $question.=&Apache::optionresponse::webbubbles(\@Items_letter,\@Items);
1.6       sakharuk  420: 		}
                    421: 	    } else {		
                    422: 		if ($Apache::lonhomework::type eq 'exam') {
                    423: 		    $question.=' '.$optionlist.$text."\n";
1.13      sakharuk  424: 		    my @emptyItems = ();
                    425: 		    for (my $i=0;$i<=$#Items;$i++) {push @emptyItems, ' ';}
1.14    ! sakharuk  426: 		    $question.='\vskip -2 mm\parbox{\textwidth}{\begin{enumerate}\item[\textbf{'.$Apache::lonxml::counter.'}.]\parbox{\textwidth - 5 mm}{'.&Apache::optionresponse::bubbles(\@Items_letter,\@emptyItems).'}\end{enumerate}} \vskip -10 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;
1.14    ! sakharuk  615: 		if ($target eq 'tex') {
        !           616: 		    $Apache::response::conceptgroup{"$name.text"} = ' $\triangleright$ '.$text;
        !           617: 		} else {
        !           618: 		    $Apache::response::conceptgroup{"$name.text"} = $text;
        !           619: 		}
1.1       albertel  620: 		$Apache::response::conceptgroup{"$name.location"} = $location;
                    621: 	    } else {
                    622: 		push @{ $Apache::response::foilgroup{'names'} }, $name;
                    623: 		$Apache::response::foilgroup{"$name.value"} = $value;
1.14    ! sakharuk  624: 		if ($Apache::lonhomework::type eq 'exam' and $flag_for_exam_printing) {
        !           625: 		    $Apache::response::foilgroup{"$name.text"} = '\vskip 5 mm $\triangleright$ '.$text;
        !           626: 		} else {
        !           627: 		    if ($target eq 'tex') {
        !           628: 			$Apache::response::foilgroup{"$name.text"} = ' $\triangleright$ '.$text;
        !           629: 		    } else {
        !           630: 			$Apache::response::foilgroup{"$name.text"} = $text;
        !           631: 		    }
        !           632: 		}
1.1       albertel  633: 		$Apache::response::foilgroup{"$name.location"} = $location;
                    634: 	    }
                    635: 	}
                    636:     }
                    637:     if ($target eq 'edit') {
                    638: 	$result.= &Apache::edit::tag_end($target,$token,'');
                    639:     }
                    640:     return $result;
                    641: }
                    642: 
                    643: sub insert_foil {
                    644:     return '
                    645: <foil name="" value="unused">
                    646: <startouttext />
                    647: <endouttext />
                    648: </foil>';
                    649: }
                    650: 1;
                    651: __END__

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