File:  [LON-CAPA] / loncom / homework / matchresponse.pm
Revision 1.20.2.2: download - view: text, annotated - select for diffs
Wed Oct 15 19:52:53 2003 UTC (20 years, 6 months ago) by albertel
Branches: version_1_0_2
CVS tags: version_1_0_3
Diff to branchpoint 1.20: preferred, unified
- backport 1.27

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

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