File:  [LON-CAPA] / loncom / homework / rankresponse.pm
Revision 1.71: download - view: text, annotated - select for diffs
Mon May 5 17:40:54 2014 UTC (10 years ago) by bisitz
Branches: MAIN
CVS tags: version_2_11_1, version_2_11_0_RC3, version_2_11_0, HEAD
More practicable and consistent output of previous tries with incomplete answers:
Explicit display of empty answer items, i.e. not submitted

    1: # The LearningOnline Network with CAPA
    2: # rank style response
    3: #
    4: # $Id: rankresponse.pm,v 1.71 2014/05/05 17:40:54 bisitz Exp $
    5: # Copyright Michigan State University Board of Trustees
    6: #
    7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    8: #
    9: # LON-CAPA is free software; you can redistribute it and/or modify
   10: # it under the terms of the GNU General Public License as published by
   11: # the Free Software Foundation; either version 2 of the License, or
   12: # (at your option) any later version.
   13: #
   14: # LON-CAPA is distributed in the hope that it will be useful,
   15: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   16: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   17: # GNU General Public License for more details.
   18: #
   19: # You should have received a copy of the GNU General Public License
   20: # along with LON-CAPA; if not, write to the Free Software
   21: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   22: #
   23: # /home/httpd/html/adm/gpl.txt
   24: #
   25: # http://www.lon-capa.org/
   26: #
   27: 
   28: package Apache::rankresponse;
   29: use strict;
   30: use HTML::Entities();
   31: use Apache::optionresponse();
   32: use Apache::lonlocal;
   33: use Apache::lonxml;
   34: use Apache::lonnet;
   35: 
   36: BEGIN {
   37:     &Apache::lonxml::register('Apache::rankresponse',('rankresponse'));
   38: }
   39: 
   40: sub start_rankresponse {
   41:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   42:     my $result;
   43:     #when in a rank response use these
   44:     &Apache::lonxml::register('Apache::rankresponse',
   45: 			      ('foilgroup','foil','conceptgroup'));
   46:     push (@Apache::lonxml::namespace,'rankresponse');
   47:     my $id = &Apache::response::start_response($parstack,$safeeval);
   48:     %Apache::hint::rank=();
   49:     undef(%Apache::response::foilnames);
   50:     if ($target eq 'meta') {
   51: 	$result=&Apache::response::meta_package_write('rankresponse');
   52:     } elsif ($target eq 'edit' ) {
   53: 	$result.=&Apache::edit::start_table($token)
   54:            .'<tr><td>'.&Apache::lonxml::description($token).'</td>'
   55:            .'<td><span class="LC_nobreak">'.&mt('Delete?').' '
   56:            .&Apache::edit::deletelist($target,$token)
   57:            .'</span></td>'
   58:            .'<td>&nbsp;'.&Apache::edit::end_row()
   59:            .&Apache::edit::start_spanning_row();
   60: 	
   61: 	$result.=
   62: 	    &Apache::edit::text_arg('Max Number Of Shown Foils:','max',$token,'4').
   63: 	    &Apache::edit::select_arg('Randomize Foil Order:','randomize',
   64: 				      ['yes','no'],$token).
   65: 	    &Apache::edit::end_row().&Apache::edit::start_spanning_row()."\n";
   66:     } elsif ($target eq 'modified') {
   67: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
   68: 						     $safeeval,'max',
   69: 						     'randomize');
   70: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
   71:     } elsif ($target eq 'analyze') {
   72: 	my $part_id="$Apache::inputtags::part.$id";
   73:         $Apache::lonhomework::analyze{"$part_id.type"} = 'rankresponse';
   74: 	push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
   75: 	push (@{ $Apache::lonhomework::analyze{"$part_id.bubble_lines"} }, 1);
   76: 
   77:     }
   78:     return $result;
   79: }
   80: 
   81: sub end_rankresponse {
   82:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   83:     my $result;
   84:     if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
   85:     &Apache::response::end_response;
   86:     pop @Apache::lonxml::namespace;
   87:     &Apache::lonxml::deregister('Apache::rankresponse',
   88: 				('foilgroup','foil','conceptgroup'));
   89:     undef(%Apache::response::foilnames);
   90:     return $result;
   91: }
   92: 
   93: %Apache::response::foilgroup=();
   94: sub start_foilgroup {
   95:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   96:     my $result;
   97:     %Apache::response::foilgroup=();
   98:     $Apache::rankresponse::conceptgroup=0;
   99:     &Apache::response::pushrandomnumber(undef,$target);
  100:     return $result;
  101: }
  102: 
  103: sub end_foilgroup {
  104:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  105:     my $result;
  106:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
  107: 	$target eq 'tex' || $target eq 'analyze') {
  108: 	my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
  109: 	my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
  110: 						   $safeeval,'-2');
  111: 	my $tol = &Apache::lonxml::get_param('tol',$parstack,$safeeval,'-2');
  112: 	if (!defined($tol)) { $tol=0; }
  113: 	if ($target eq 'web' || $target eq 'tex') {
  114: 	    $result=&displayfoils($target,$max,$randomize,$tol);
  115: 	    $Apache::lonxml::post_evaluate=0;
  116: 	} elsif ($target eq 'answer' ) {
  117: 	    $result=&displayanswers($max,$randomize,$tol);
  118: 	} elsif ( $target eq 'grade') {
  119: 	    &grade_response($max,$randomize,$tol);
  120: 	} elsif ( $target eq 'analyze') {
  121: 	    my @shown = &whichfoils($max,$randomize);
  122: 	    &Apache::response::analyze_store_foilgroup(\@shown,
  123: 						  ['text','value','location']);
  124: 	    my $part_id=
  125: 		"$Apache::inputtags::part.$Apache::inputtags::response[-1]";
  126: 	    $Apache::lonhomework::analyze{"$part_id.tol"}=$tol;
  127: 	}
  128: 	my $part = $Apache::inputtags::part;
  129: 	my $id   = $Apache::inputtags::response[-1];
  130:         my ($numrows,$bubbles_per_row);
  131:         if (($target eq 'tex') && ($Apache::lonhomework::type eq 'exam')) {
  132:             my (@whichfoils)=&whichfoils($max,$randomize);
  133:             ($numrows,$bubbles_per_row) =
  134:                 &Apache::optionresponse::getnumrows(scalar(@whichfoils));
  135:         }
  136:         if ($numrows < 1) {
  137:             $numrows = 1;
  138:         }
  139:         my $increment = &getfoilcounts($max) * $numrows;
  140: 	&Apache::lonxml::increment_counter($increment,"$part.$id");
  141: 	if ($target eq 'analyze') {
  142: 	    &Apache::lonhomework::set_bubble_lines();
  143: 	}
  144:     } elsif ($target eq 'edit') {
  145: 	$result=&Apache::edit::end_table();
  146:     }
  147:     &Apache::response::poprandomnumber();
  148:     return $result;
  149: }
  150: 
  151: sub get_correct_order {
  152:     my ($tol,@foils) =@_;
  153:     my @correctorder;
  154:     my @value_names;
  155:     foreach my $name (@foils) {
  156: 	my @pair=($Apache::response::foilgroup{$name.'.value'},$name);
  157: 	push(@value_names,\@pair);
  158:     }
  159:     @value_names =
  160: 	sort {
  161: 	    if (abs($a->[0] - $b->[0]) > $tol) {return ($a->[0] <=> $b->[0]);}
  162: 	    return 0;
  163: 	} @value_names;
  164:     my @value_names_tmp=@value_names;
  165:     my $firstpair=shift(@value_names_tmp);
  166:     my $order=1;
  167:     my %order;
  168:     my $count=1;
  169:     my $lastvalue=$firstpair->[0];
  170:     $order{$firstpair->[1]}=$order;
  171:     foreach my $pair (@value_names_tmp) {
  172: 	$count++;
  173: 	if (abs($pair->[0]-$lastvalue) > $tol ) {
  174: 	    $order=$count;
  175: 	}
  176: 	$order{$pair->[1]}=$order;
  177: 	$lastvalue=$pair->[0];
  178:     }
  179:     foreach my $name (@foils) {
  180: 	push(@correctorder,$order{$name});
  181:     }
  182:     &Apache::lonhomework::showhash('b' => \@value_names);
  183:     &Apache::lonhomework::showhash('b' => \@correctorder);
  184:     return @correctorder;
  185: }
  186: 
  187: sub displayanswers {
  188:     my ($max,$randomize,$tol,@opt)=@_;
  189:     my @names;
  190:     if (ref($Apache::response::foilgroup{'names'}) eq 'ARRAY') {
  191:         @names = @{ $Apache::response::foilgroup{'names'} };
  192:     }
  193:     return if (!@names);
  194:     my @whichfoils = &whichfoils($max,$randomize);
  195:     my @correctorder=&get_correct_order($tol,@whichfoils);
  196:     my $result;
  197:     if ($Apache::lonhomework::type eq 'exam') {
  198: 	my @alphabet = ('A'..'Z');
  199: 	my $i=0;
  200: 	foreach my $order (@correctorder) {
  201: 	    $result.=&Apache::response::answer_header('rankresponse',$i++);
  202: 	    $result.=&Apache::response::answer_part('rankresponse',
  203: 						    $alphabet[$order-1]);
  204: 	    $result.=&Apache::response::answer_part('rankresponse',$order);
  205: 	    $result.=&Apache::response::answer_footer('rankresponse');
  206: 	}
  207:     } else {
  208: 	$result=&Apache::response::answer_header('rankresponse');
  209: 	foreach my $order (@correctorder) {
  210: 	    $result.=&Apache::response::answer_part('rankresponse',$order);
  211: 	}
  212: 	$result.=&Apache::response::answer_footer('rankresponse');
  213:     }
  214:     return $result;
  215: }
  216: 
  217: sub check_response_order {
  218:     my (%responsehash)=@_;
  219:     my @order=sort( {$a <=> $b} values(%responsehash));
  220:     my $lastvalue=0;
  221:     my $expected=1;
  222:     my $malformed=0;
  223:     foreach my $current (@order) {
  224: 	&Apache::lonxml::debug("$lastvalue $expected $malformed");
  225: 	if (!($current == $lastvalue || $current == $expected)) {
  226: 	    $malformed=1;
  227: 	}
  228: 	$expected++;
  229: 	$lastvalue=$current;
  230:     }
  231:     return $malformed;
  232: }
  233: 
  234: sub grade_response {
  235:     my ($max,$randomize,$tol)=@_;
  236:     my (@whichfoils)=&whichfoils($max,$randomize);
  237:     if (!&Apache::response::submitted()) { return; }
  238:     my %responsehash;
  239:     my %grade;
  240:     my ($temp,$right,$wrong,$ignored)=(1,0,0,0);
  241:     my @correctorder=&get_correct_order($tol,@whichfoils);
  242:     my ($numrows,$bubbles_per_row);
  243:     if ($Apache::lonhomework::scantronmode) {
  244:         my $numitems = scalar(@whichfoils);
  245:         ($numrows,$bubbles_per_row) =
  246:             &Apache::optionresponse::getnumrows($numitems);
  247:     }
  248:     if ($numrows < 1) {
  249:         $numrows = 1;
  250:     }
  251: 
  252:     foreach my $name (@whichfoils) {
  253:         my $response;
  254:         if ($numrows > 1) {
  255:             my $num = $temp;
  256:             my $totalnum;
  257:             for (my $i=0; $i<$numrows; $i++) {
  258:                 my $item = &Apache::response::getresponse($num,'A is 1');
  259:                 if ($item =~ /^\d+$/) {
  260:                     $totalnum = $i*$bubbles_per_row + $item;
  261:                 }
  262:                 $num ++;
  263:             }
  264:             $response = $totalnum;
  265:             $temp += $numrows;
  266:         } else {
  267:             $response = &Apache::response::getresponse($temp,'A is 1');
  268:             $temp ++;
  269:         }
  270: 	my $value=shift(@correctorder);
  271: 	if ( $response =~ /[^\s]/) {
  272: 	    $responsehash{$name}=$response;
  273: 	    &Apache::lonxml::debug("submitted a $response for $value<br />\n");
  274: 	    if ($value eq $response) {
  275: 		$grade{$name}='1'; $right++;
  276: 	    } else {
  277: 		$grade{$name}='0'; $wrong++;
  278: 	    }
  279: 	} else {
  280: 	    $ignored++;
  281: 	}
  282:     }
  283:     my $malformed=&check_response_order(%responsehash);
  284:     my $part=$Apache::inputtags::part;
  285:     my $id = $Apache::inputtags::response['-1'];
  286:     my $responsestr=&Apache::lonnet::hash2str(%responsehash);
  287:     my $gradestr   =&Apache::lonnet::hash2str(%grade);
  288:     my %previous=&Apache::response::check_for_previous($responsestr,
  289: 						       $part,$id);
  290:     &Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored and was $malformed malformed");
  291:     my $ad;
  292:     if ($malformed) {
  293: 	$ad='MISORDERED_RANK';
  294:     } elsif ($wrong==0 && $ignored==0) {
  295: 	$ad='EXACT_ANS';
  296:     } elsif ($wrong==0 && $right==0) {
  297: 	#nothing submitted
  298:     } else {
  299: 	if ($ignored==0) {
  300: 	    $ad='INCORRECT';
  301: 	} else {
  302: 	    $ad='MISSING_ANSWER';
  303: 	}
  304:     }
  305:     if (($ad eq 'INCORRECT' || $ad eq 'EXACT_ANS')) {
  306:         if ($Apache::lonhomework::type eq 'survey') {
  307:             $ad='SUBMITTED';
  308:         } elsif ($Apache::lonhomework::type eq 'surveycred') {
  309: 	    $ad='SUBMITTED_CREDIT';
  310:         } elsif ($Apache::lonhomework::type eq 'anonsurvey') {
  311:             $ad='ANONYMOUS';
  312:         } elsif ($Apache::lonhomework::type eq 'anonsurveycred') {
  313:             $ad='ANONYMOUS_CREDIT';
  314:         } else {
  315:             $Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=$gradestr;
  316:         }
  317:     } else {
  318: 	$Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=$gradestr;
  319:     }
  320:     if ($Apache::lonhomework::type eq 'randomizetry') {
  321:         if ($Apache::lonhomework::type eq 'randomizetry') {
  322:             $Apache::lonhomework::results{"resource.$part.$id.foilorder"} = &Apache::lonnet::array2str(@whichfoils);
  323:         }
  324:     }
  325:     $Apache::lonhomework::results{"resource.$part.$id.submission"}=
  326: 	$responsestr;
  327:     $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
  328:     &Apache::response::handle_previous(\%previous,$ad);
  329: }
  330: 
  331: sub format_prior_answer {
  332:     my ($mode,$answer,$other_data) = @_;
  333:     my %lastresponse=&Apache::lonnet::str2hash($answer);
  334:     my $foil_order  =$other_data->[0];
  335:     my %grading     =&Apache::lonnet::str2hash($other_data->[1]);
  336:     my $output;
  337:     foreach my $name (@{ $foil_order }) {
  338:         if (defined($lastresponse{$name})) {
  339:             $output .= '<tr><td>'.&HTML::Entities::encode($lastresponse{$name},'<>&"').'</td></tr>';
  340:         } else {
  341:             $output .= '<tr><td>&nbsp;</td></tr>';
  342:         }
  343:     }
  344:     return if (!defined($output));
  345:     $output =
  346: 	'<table class="LC_prior_rank">'.$output.'</table>';
  347:     return $output;
  348: }
  349: 
  350: sub displayfoils {
  351:     my ($target,$max,$randomize,$tol)=@_;
  352:     my $result;
  353:     my @alphabet=('A'..'Z');
  354:     my (@whichfoils)=&whichfoils($max,$randomize);
  355:     my $part=$Apache::inputtags::part;
  356:     my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
  357:     my @whichopt=(1..($#whichfoils+1));
  358:     my @correctorder=&get_correct_order($tol,@whichfoils);
  359:     if ( &Apache::response::show_answer() && ($target ne 'tex')) {
  360: 	foreach my $name (@whichfoils) {
  361: 	    my $text=$Apache::response::foilgroup{$name.'.text'};
  362: 	    my $value=shift(@correctorder);
  363: 	    if ($target eq 'web') {
  364: 		$result.='<br /><b>'.$value.':</b> '.$text;
  365: 	    } else {
  366: 		$result.=' \strut\\\\\strut '.$value.':'.$text;
  367: 	    }
  368: 	}
  369:     } else {
  370: 	my $i = 0;
  371: 	my $temp=1;
  372: 	my $id=$Apache::inputtags::response[-1];
  373: 	my $part=$Apache::inputtags::part;
  374:         my ($lastresponse,$newvariation);
  375:         if ((($Apache::lonhomework::history{"resource.$part.type"} eq 'randomizetry') ||
  376:              ($Apache::lonhomework::type eq 'randomizetry')) &&
  377:         ($Apache::inputtags::status[-1] eq 'CAN_ANSWER')) {
  378:             if ($env{'form.'.$part.'.rndseed'} ne
  379:                 $Apache::lonhomework::history{"resource.$part.rndseed"}) {
  380:                 $newvariation = 1;
  381:             }
  382:         }
  383:         unless ($newvariation) {
  384:             if ((($env{'form.grade_username'} eq '') && ($env{'form.grade_domain'} eq '')) ||
  385:                 (($env{'form.grade_username'} eq $env{'user.name'}) &&
  386:                  ($env{'form.grade_domain'} eq $env{'user.domain'}))) {
  387:                 $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
  388:             } else {
  389:                 unless (($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurvey') ||
  390:                         ($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurveycred')) {
  391:                     $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
  392:                 }
  393:             }
  394:         }
  395: 	my %lastresponse=&Apache::lonnet::str2hash($lastresponse); 
  396: 	my @alp = splice @alphabet, 0, $#whichopt + 1;
  397: 	my $internal_counter=$Apache::lonxml::counter;
  398:         my ($numrows,$bubbles_per_row);
  399:         if (($target eq 'tex') && ($Apache::lonhomework::type eq 'exam')) {
  400:             ($numrows,$bubbles_per_row) = 
  401:                 &Apache::optionresponse::getnumrows(scalar(@alp));
  402:         } else {
  403:             $numrows = 1;
  404:         }
  405:         if($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes') {
  406:             $result .= '\strut \\\\ \strut \\\\' ;
  407:         }
  408:         foreach my $name (@whichfoils) {
  409: 	    my $lastopt=$lastresponse{$name};
  410: 	    my $optionlist='';
  411: 	    if ($target ne 'tex') {
  412:                 $optionlist = "<option></option>\n";
  413:             } 
  414:             if ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes') {
  415:                 my $fieldname = $env{'request.symb'}.'&part_'.$Apache::inputtags::part.'&rankresponse'.'&HWVAL_'.$Apache::inputtags::response['-1'].':'.$temp;
  416:                 $optionlist =  &Apache::lonxml::print_pdf_start_combobox($fieldname);
  417:             }
  418: 	    my $option;
  419: 	    foreach $option (@whichopt) {
  420: 		if ($option eq $lastopt) {
  421: 		    if ($target ne 'tex' ) {
  422:                         $optionlist.="<option selected=\"selected\">$option</option>\n";
  423:                     } elsif ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes') {
  424:                         $optionlist .= &Apache::lonxml::print_pdf_add_combobox_option($option); 
  425:                     }
  426: 		} else {
  427: 		    if ($target ne 'tex') {
  428:                         $optionlist.="<option>$option</option>\n";
  429:                     } elsif ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes') {
  430:                         $optionlist .= &Apache::lonxml::print_pdf_add_combobox_option($option); 
  431:                     }
  432: 		} 
  433: 	    }
  434: 	    if ($target ne 'tex' && $Apache::lonhomework::type ne 'exam') {
  435: 		$optionlist='<select onchange="javascript:setSubmittedPart(\''.
  436: 		  $part.'\');" name="HWVAL_'.
  437: 		    $Apache::inputtags::response[-1].':'.$temp.'">'.
  438: 		        $optionlist."</select>\n";
  439: 	    } elsif ($env{'form.pdfFormFields'} eq 'yes') {
  440:                 #do nothing
  441:             } else {
  442: 		$optionlist=' '.$temp.' '.$optionlist.' ';
  443: 	    }
  444: 	    my $text=$Apache::response::foilgroup{$name.'.text'};
  445: 	    if ($target ne 'tex') {
  446: 		if ($Apache::lonhomework::type ne 'exam') {
  447: 		    $result.='<br />'.$optionlist.$text."\n";
  448: 		} else {
  449: 		    $result.='<br />'.$text."\n";
  450: 		}
  451: 		if ($Apache::lonhomework::type eq 'exam') {
  452: 		    my @values=(1..scalar(@whichopt));
  453: 		    $result.=&Apache::optionresponse::webbubbles(\@values,\@whichopt,$temp,$lastopt);
  454: 		}
  455: 	    } else {
  456: 		if ($Apache::lonhomework::type eq 'exam') {
  457:                     my $itemlabel;
  458:                     if ($numrows == 1) {
  459:                         $itemlabel = '\item[\textbf{'.$internal_counter.'}]';
  460:                     } else {
  461:                         my $linetext;
  462:                         for (my $i=0; $i<$numrows; $i++) {
  463:                             $linetext .= $internal_counter+$i.', ';
  464:                         }
  465:                         $linetext =~ s/,\s$//;
  466:                         $itemlabel = '\item[\small {\textbf{'.$linetext.'}}]'.
  467:                                      ' {\footnotesize '.
  468:                                      &mt('(Bubble once in [_1] lines)',$numrows).
  469:                                      '} \hspace*{\fill} \\\\';
  470:                     }
  471: 		    $result .= '\vskip 0 mm '.$text.' \vskip 0 mm '."\n";
  472: 		    $result .= '\vskip -1 mm';
  473:                     $result .= '\begin{enumerate}'.$itemlabel;
  474: 		    $result .= &Apache::optionresponse::bubbles(\@alp,\@whichopt,'rankresponse',undef,$numrows,$bubbles_per_row,$internal_counter);
  475:                     $result .= '\end{enumerate} \vskip -8 mm \strut ';
  476: 		    $internal_counter += $numrows;
  477: 		} else {
  478:                     if($env{'form.pdfFormFields'} ne 'yes') {
  479: 		        $result.=' \vskip 0mm \framebox[5 mm][s]{\tiny\strut} '.$text."\n";
  480:                     } else {
  481:                         $result.= $optionlist.' '. &Apache::lonxml::print_pdf_end_combobox($text).'\strut \\\\';
  482:                     }
  483: 		}
  484: 	    }
  485: 	    $temp++;
  486: 	}
  487:     }
  488:     if ($target eq 'web') {
  489:         my $questiontype;
  490:         if ($Apache::lonhomework::type eq 'randomizetry') {
  491:             $questiontype = $Apache::lonhomework::type;
  492:         }
  493: 	&Apache::response::setup_prior_tries_hash(\&format_prior_answer,
  494: 						  [\@whichfoils,
  495: 						   'submissiongrading'],
  496:                                                   $questiontype);
  497:     }
  498:     if ($target ne 'tex') {$result.="<br />";} else {$result.=' \vskip 0 mm ';}
  499:     return $result;
  500: }
  501: 
  502: sub getfoilcounts {
  503:     my ($max)=@_;
  504:     # +1 since instructors will count from 1
  505:     my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
  506:     if (&Apache::response::showallfoils()) { $max=$count; }
  507:     if ($count>$max) { $count=$max } 
  508:     &Apache::lonxml::debug("Count is $count from $max");
  509:     return $count;
  510: }
  511: 
  512: sub whichfoils {
  513:     my ($max,$randomize)=@_;
  514:     return &Apache::response::whichorder($max,$randomize,
  515: 					 &Apache::response::showallfoils(),
  516: 					 \%Apache::response::foilgroup);
  517: }
  518: 
  519: sub start_conceptgroup {
  520:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  521:     $Apache::rankresponse::conceptgroup=1;
  522:     %Apache::response::conceptgroup=();
  523:     my $result;
  524:     if ($target eq 'edit') {
  525: 	$result.=&Apache::edit::tag_start($target,$token,
  526: 					  "Concept Grouped Foils");
  527: 	$result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
  528: 	    &Apache::edit::end_row().&Apache::edit::start_spanning_row();
  529:     }
  530:     if ($target eq 'modified') {
  531: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  532: 						     $safeeval,'concept');
  533: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  534:     }
  535:     return $result;
  536: }
  537: 
  538: sub end_conceptgroup {
  539:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  540:     $Apache::rankresponse::conceptgroup=0;
  541:     my $result='';
  542:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  543: 	$target eq 'tex' || $target eq 'analyze') {
  544: 	#if not there aren't any foils to display and thus no question
  545: 	&Apache::response::pick_foil_for_concept($target,
  546: 						 ['value','text','location'],
  547: 						 \%Apache::hint::rank,
  548: 						 $parstack,$safeeval);
  549:     } elsif ($target eq 'edit') {
  550: 	$result=&Apache::edit::end_table();
  551:     }
  552:     return $result;
  553: }
  554: 
  555: sub insert_conceptgroup {
  556:     my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
  557:     return $result;
  558: }
  559: 
  560: sub start_foil {
  561:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  562:     my $result='';
  563:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
  564: 	&Apache::lonxml::startredirection;
  565: 	if ($target eq 'analyze') {
  566: 	    &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
  567: 	}
  568:     } elsif ($target eq 'edit') {
  569: 	$result=&Apache::edit::tag_start($target,$token,"Foil");
  570: 	my $level='-2';
  571: 	if ($$tagstack[-2] eq 'conceptgroup') { $level = '-3'; }
  572: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
  573: 	$result.= &Apache::edit::text_arg('Rank Value:','value',$token,'15');
  574: 	my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
  575: 						 $safeeval,'-3');
  576: 	if ($randomize ne 'no') {
  577: 	    $result.=&Apache::edit::select_arg('Location:','location',
  578: 					     ['random','top','bottom'],$token);
  579: 	}
  580: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  581:     } elsif ($target eq 'modified') {
  582: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  583: 						     $safeeval,'value',
  584: 						     'name','location');
  585: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  586:     }
  587:     return $result;
  588: }
  589: 
  590: sub end_foil {
  591:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  592:     my $text ='';
  593:     my $result = '';
  594:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
  595: 	$text=&Apache::lonxml::endredirection;
  596:     }
  597:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  598: 	$target eq 'tex' || $target eq 'analyze') {
  599: 	my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
  600: 	if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
  601: 	    $text='\vskip 5mm $\triangleright$ '.$text;
  602: 	}
  603: 	if ($value ne 'unused') {
  604: 	    my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  605: 	    &Apache::lonxml::debug("Got a name of :$name:");
  606: 	    if ($name eq "") {
  607: 		&Apache::lonxml::warning(&mt('Foils without names exist. This can cause problems to malfunction.'));
  608: 		$name=$Apache::lonxml::curdepth;
  609: 	    }
  610: 	    &Apache::lonxml::debug("Using a name of :$name:");
  611: 	    if (defined($Apache::response::foilnames{$name})) {
  612: 		&Apache::lonxml::error(&mt('Foil name [_1] appears more than once. Foil names need to be unique.','<b><tt>'.$name.'</tt></b>'));
  613: 	    }
  614: 	    $Apache::response::foilnames{$name}++;
  615: 	    my $location =&Apache::lonxml::get_param('location',$parstack,
  616: 						     $safeeval);
  617: 	    if ( $Apache::rankresponse::conceptgroup
  618: 		 && !&Apache::response::showallfoils() ) {
  619: 		push @{ $Apache::response::conceptgroup{'names'} }, $name;
  620: 		$Apache::response::conceptgroup{"$name.value"} = $value;
  621: 		$Apache::response::conceptgroup{"$name.text"} = $text;
  622: 		$Apache::response::conceptgroup{"$name.location"} = $location;
  623: 	    } else {
  624: 		push @{ $Apache::response::foilgroup{'names'} }, $name;
  625: 		$Apache::response::foilgroup{"$name.value"} = $value;
  626: 		$Apache::response::foilgroup{"$name.text"} = $text;
  627: 		$Apache::response::foilgroup{"$name.location"} = $location;
  628: 	    }
  629: 	}
  630:     }
  631:     if ($target eq 'edit') {
  632: 	$result.= &Apache::edit::tag_end($target,$token,'');
  633:     }
  634:     return $result;
  635: }
  636: 
  637: sub insert_foil {
  638:     return '
  639: <foil name="" value="unused">
  640: <startouttext />
  641: <endouttext />
  642: </foil>';
  643: }
  644: 1;
  645: __END__

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