File:  [LON-CAPA] / loncom / homework / rankresponse.pm
Revision 1.74: download - view: text, annotated - select for diffs
Thu Jun 7 15:58:11 2018 UTC (5 years, 10 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, HEAD
- Bug 6883 -- specific to MS Edge on Windows 10 (rev. 1803).

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

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