File:  [LON-CAPA] / loncom / homework / matchresponse.pm
Revision 1.12: download - view: text, annotated - select for diffs
Thu Mar 20 19:38:41 2003 UTC (21 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: HEAD
- eliminating an unneeded global

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

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