File:  [LON-CAPA] / loncom / homework / rankresponse.pm
Revision 1.66: download - view: text, annotated - select for diffs
Tue Sep 13 21:42:58 2011 UTC (12 years, 7 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Additional (eighteenth field) BubblesPerRow in Bubblesheet Format file.
  - specify bubbles available for each question row (default is 10).
- Support case where question (or subquestion for optionresponse,
    matchresponse, or rankresponse) requires more bubbles than in one line.
  - Students prompted to bubble once in multiple lines.
  - Numbering recognizes multiple lines per question.
  - Also supports: imageresponse, stringresponse, formularesponse where
    students leave link(s) blank if weight assigned is greater than
    bubbles per line -- multiple lines also supported in this case.

For example:
- 15-bubble MSU Art History exams now supported on standard 10-bubble
    bubblesheets used at MSU.

    1: # The LearningOnline Network with CAPA
    2: # rank style response
    3: #
    4: # $Id: rankresponse.pm,v 1.66 2011/09/13 21:42:58 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::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:     if (!defined(@{ $Apache::response::foilgroup{'names'} })) { return; }
  190:     my @names = @{ $Apache::response::foilgroup{'names'} };
  191:     my @whichfoils = &whichfoils($max,$randomize);
  192:     my @correctorder=&get_correct_order($tol,@whichfoils);
  193:     my $result;
  194:     if ($Apache::lonhomework::type eq 'exam') {
  195: 	my @alphabet = ('A'..'Z');
  196: 	my $i=0;
  197: 	foreach my $order (@correctorder) {
  198: 	    $result.=&Apache::response::answer_header('rankresponse',$i++);
  199: 	    $result.=&Apache::response::answer_part('rankresponse',
  200: 						    $alphabet[$order-1]);
  201: 	    $result.=&Apache::response::answer_part('rankresponse',$order);
  202: 	    $result.=&Apache::response::answer_footer('rankresponse');
  203: 	}
  204:     } else {
  205: 	$result=&Apache::response::answer_header('rankresponse');
  206: 	foreach my $order (@correctorder) {
  207: 	    $result.=&Apache::response::answer_part('rankresponse',$order);
  208: 	}
  209: 	$result.=&Apache::response::answer_footer('rankresponse');
  210:     }
  211:     return $result;
  212: }
  213: 
  214: sub check_response_order {
  215:     my (%responsehash)=@_;
  216:     my @order=sort( {$a <=> $b} values(%responsehash));
  217:     my $lastvalue=0;
  218:     my $expected=1;
  219:     my $malformed=0;
  220:     foreach my $current (@order) {
  221: 	&Apache::lonxml::debug("$lastvalue $expected $malformed");
  222: 	if (!($current == $lastvalue || $current == $expected)) {
  223: 	    $malformed=1;
  224: 	}
  225: 	$expected++;
  226: 	$lastvalue=$current;
  227:     }
  228:     return $malformed;
  229: }
  230: 
  231: sub grade_response {
  232:     my ($max,$randomize,$tol)=@_;
  233:     my (@whichfoils)=&whichfoils($max,$randomize);
  234:     if (!&Apache::response::submitted()) { return; }
  235:     my %responsehash;
  236:     my %grade;
  237:     my ($temp,$right,$wrong,$ignored)=(1,0,0,0);
  238:     my @correctorder=&get_correct_order($tol,@whichfoils);
  239:     my ($numrows,$bubbles_per_row);
  240:     if ($Apache::lonhomework::scantronmode) {
  241:         my $numitems = scalar(@whichfoils);
  242:         ($numrows,$bubbles_per_row) =
  243:             &Apache::optionresponse::getnumrows($numitems);
  244:     }
  245:     if ($numrows < 1) {
  246:         $numrows = 1;
  247:     }
  248: 
  249:     foreach my $name (@whichfoils) {
  250:         my $response;
  251:         if ($numrows > 1) {
  252:             my $num = $temp;
  253:             my $totalnum;
  254:             for (my $i=0; $i<$numrows; $i++) {
  255:                 my $item = &Apache::response::getresponse($num,'A is 1');
  256:                 if ($item =~ /^\d+$/) {
  257:                     $totalnum = $i*$bubbles_per_row + $item;
  258:                 }
  259:                 $num ++;
  260:             }
  261:             $response = $totalnum;
  262:             $temp += $numrows;
  263:         } else {
  264:             $response = &Apache::response::getresponse($temp,'A is 1');
  265:             $temp ++;
  266:         }
  267: 	my $value=shift(@correctorder);
  268: 	if ( $response =~ /[^\s]/) {
  269: 	    $responsehash{$name}=$response;
  270: 	    &Apache::lonxml::debug("submitted a $response for $value<br />\n");
  271: 	    if ($value eq $response) {
  272: 		$grade{$name}='1'; $right++;
  273: 	    } else {
  274: 		$grade{$name}='0'; $wrong++;
  275: 	    }
  276: 	} else {
  277: 	    $ignored++;
  278: 	}
  279:     }
  280:     my $malformed=&check_response_order(%responsehash);
  281:     my $part=$Apache::inputtags::part;
  282:     my $id = $Apache::inputtags::response['-1'];
  283:     my $responsestr=&Apache::lonnet::hash2str(%responsehash);
  284:     my $gradestr   =&Apache::lonnet::hash2str(%grade);
  285:     my %previous=&Apache::response::check_for_previous($responsestr,
  286: 						       $part,$id);
  287:     &Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored and was $malformed malformed");
  288:     my $ad;
  289:     if ($malformed) {
  290: 	$ad='MISORDERED_RANK';
  291:     } elsif ($wrong==0 && $ignored==0) {
  292: 	$ad='EXACT_ANS';
  293:     } elsif ($wrong==0 && $right==0) {
  294: 	#nothing submitted
  295:     } else {
  296: 	if ($ignored==0) {
  297: 	    $ad='INCORRECT';
  298: 	} else {
  299: 	    $ad='MISSING_ANSWER';
  300: 	}
  301:     }
  302:     if (($ad eq 'INCORRECT' || $ad eq 'EXACT_ANS')) {
  303:         if ($Apache::lonhomework::type eq 'survey') {
  304:             $ad='SUBMITTED';
  305:         } elsif ($Apache::lonhomework::type eq 'surveycred') {
  306: 	    $ad='SUBMITTED_CREDIT';
  307:         } elsif ($Apache::lonhomework::type eq 'anonsurvey') {
  308:             $ad='ANONYMOUS';
  309:         } elsif ($Apache::lonhomework::type eq 'anonsurveycred') {
  310:             $ad='ANONYMOUS_CREDIT';
  311:         } else {
  312:             $Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=$gradestr;
  313:         }
  314:     } else {
  315: 	$Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=$gradestr;
  316:     }
  317:     if ($Apache::lonhomework::type eq 'randomizetry') {
  318:         if ($Apache::lonhomework::type eq 'randomizetry') {
  319:             $Apache::lonhomework::results{"resource.$part.$id.foilorder"} = &Apache::lonnet::array2str(@whichfoils);
  320:         }
  321:     }
  322:     $Apache::lonhomework::results{"resource.$part.$id.submission"}=
  323: 	$responsestr;
  324:     $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
  325:     &Apache::response::handle_previous(\%previous,$ad);
  326: }
  327: 
  328: sub format_prior_answer {
  329:     my ($mode,$answer,$other_data) = @_;
  330:     my %lastresponse=&Apache::lonnet::str2hash($answer);
  331:     my $foil_order  =$other_data->[0];
  332:     my %grading     =&Apache::lonnet::str2hash($other_data->[1]);
  333:     my $output;
  334:     foreach my $name (@{ $foil_order }) {
  335: 	next if (!defined($lastresponse{$name}));
  336: 	$output .= '<tr><td>'.$lastresponse{$name}.'</td></tr>';
  337:     }
  338:     return if (!defined($output));
  339:     $output =
  340: 	'<table class="LC_prior_rank">'.$output.'</table>';
  341:     return $output;
  342: }
  343: 
  344: sub displayfoils {
  345:     my ($target,$max,$randomize,$tol)=@_;
  346:     my $result;
  347:     my @alphabet=('A'..'Z');
  348:     my (@whichfoils)=&whichfoils($max,$randomize);
  349:     my $part=$Apache::inputtags::part;
  350:     my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
  351:     my @whichopt=(1..($#whichfoils+1));
  352:     my @correctorder=&get_correct_order($tol,@whichfoils);
  353:     if ( &Apache::response::show_answer() && ($target ne 'tex')) {
  354: 	foreach my $name (@whichfoils) {
  355: 	    my $text=$Apache::response::foilgroup{$name.'.text'};
  356: 	    my $value=shift(@correctorder);
  357: 	    if ($target eq 'web') {
  358: 		$result.='<br /><b>'.$value.':</b> '.$text;
  359: 	    } else {
  360: 		$result.=' \strut\\\\\strut '.$value.':'.$text;
  361: 	    }
  362: 	}
  363:     } else {
  364: 	my $i = 0;
  365: 	my $temp=1;
  366: 	my $id=$Apache::inputtags::response[-1];
  367: 	my $part=$Apache::inputtags::part;
  368:         my ($lastresponse,$newvariation);
  369:         if ((($Apache::lonhomework::history{"resource.$part.type"} eq 'randomizetry') ||
  370:              ($Apache::lonhomework::type eq 'randomizetry')) &&
  371:         ($Apache::inputtags::status[-1] eq 'CAN_ANSWER')) {
  372:             if ($env{'form.'.$part.'.rndseed'} ne
  373:                 $Apache::lonhomework::history{"resource.$part.rndseed"}) {
  374:                 $newvariation = 1;
  375:             }
  376:         }
  377:         unless (((($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurvey') || ($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurveycred')) && (defined($env{'form.grade_symb'}))) || $newvariation) {
  378:             $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
  379:         }
  380: 	my %lastresponse=&Apache::lonnet::str2hash($lastresponse); 
  381: 	my @alp = splice @alphabet, 0, $#whichopt + 1;
  382: 	my $internal_counter=$Apache::lonxml::counter;
  383:         my ($numrows,$bubbles_per_row);
  384:         if (($target eq 'tex') && ($Apache::lonhomework::type eq 'exam')) {
  385:             ($numrows,$bubbles_per_row) = 
  386:                 &Apache::optionresponse::getnumrows(scalar(@alp));
  387:         } else {
  388:             $numrows = 1;
  389:         }
  390:         if($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes') {
  391:             $result .= '\strut \\\\ \strut \\\\' ;
  392:         }
  393:         foreach my $name (@whichfoils) {
  394: 	    my $lastopt=$lastresponse{$name};
  395: 	    my $optionlist='';
  396: 	    if ($target ne 'tex') {
  397:                 $optionlist = "<option></option>\n";
  398:             } 
  399:             if ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes') {
  400:                 my $fieldname = $env{'request.symb'}.'&part_'.$Apache::inputtags::part.'&rankresponse'.'&HWVAL_'.$Apache::inputtags::response['-1'].':'.$temp;
  401:                 $optionlist =  &Apache::lonxml::print_pdf_start_combobox($fieldname);
  402:             }
  403: 	    my $option;
  404: 	    foreach $option (@whichopt) {
  405: 		if ($option eq $lastopt) {
  406: 		    if ($target ne 'tex' ) {
  407:                         $optionlist.="<option selected=\"selected\">$option</option>\n";
  408:                     } elsif ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes') {
  409:                         $optionlist .= &Apache::lonxml::print_pdf_add_combobox_option($option); 
  410:                     }
  411: 		} else {
  412: 		    if ($target ne 'tex') {
  413:                         $optionlist.="<option>$option</option>\n";
  414:                     } elsif ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes') {
  415:                         $optionlist .= &Apache::lonxml::print_pdf_add_combobox_option($option); 
  416:                     }
  417: 		} 
  418: 	    }
  419: 	    if ($target ne 'tex' && $Apache::lonhomework::type ne 'exam') {
  420: 		$optionlist='<select onchange="javascript:setSubmittedPart(\''.
  421: 		  $part.'\');" name="HWVAL_'.
  422: 		    $Apache::inputtags::response[-1].':'.$temp.'">'.
  423: 		        $optionlist."</select>\n";
  424: 	    } elsif ($env{'form.pdfFormFields'} eq 'yes') {
  425:                 #do nothing
  426:             } else {
  427: 		$optionlist=' '.$temp.' '.$optionlist.' ';
  428: 	    }
  429: 	    my $text=$Apache::response::foilgroup{$name.'.text'};
  430: 	    if ($target ne 'tex') {
  431: 		if ($Apache::lonhomework::type ne 'exam') {
  432: 		    $result.='<br />'.$optionlist.$text."\n";
  433: 		} else {
  434: 		    $result.='<br />'.$text."\n";
  435: 		}
  436: 		if ($Apache::lonhomework::type eq 'exam') {
  437: 		    my @values=(1..scalar(@whichopt));
  438: 		    $result.=&Apache::optionresponse::webbubbles(\@values,\@whichopt,$temp,$lastopt);
  439: 		}
  440: 	    } else {
  441: 		if ($Apache::lonhomework::type eq 'exam') {
  442:                     my $itemlabel;
  443:                     if ($numrows == 1) {
  444:                         $itemlabel = '\item[\textbf{'.$internal_counter.'}]';
  445:                     } else {
  446:                         my $linetext;
  447:                         for (my $i=0; $i<$numrows; $i++) {
  448:                             $linetext .= $internal_counter+$i.', ';
  449:                         }
  450:                         $linetext =~ s/,\s$//;
  451:                         $itemlabel = '\item[\small {\textbf{'.$linetext.'}}]'.
  452:                                      ' {\footnotesize '.
  453:                                      &mt('(Bubble once in [_1] lines)',$numrows).
  454:                                      '} \hspace*{\fill} \\\\';
  455:                     }
  456: 		    $result .= '\vskip 0 mm '.$text.' \vskip 0 mm '."\n";
  457: 		    $result .= '\vskip -1 mm';
  458:                     $result .= '\begin{enumerate}'.$itemlabel;
  459: 		    $result .= &Apache::optionresponse::bubbles(\@alp,\@whichopt,'rankresponse',undef,$numrows,$bubbles_per_row,$internal_counter);
  460:                     $result .= '\end{enumerate} \vskip -8 mm \strut ';
  461: 		    $internal_counter += $numrows;
  462: 		} else {
  463:                     if($env{'form.pdfFormFields'} ne 'yes') {
  464: 		        $result.=' \vskip 0mm \framebox[5 mm][s]{\tiny\strut} '.$text."\n";
  465:                     } else {
  466:                         $result.= $optionlist.' '. &Apache::lonxml::print_pdf_end_combobox($text).'\strut \\\\';
  467:                     }
  468: 		}
  469: 	    }
  470: 	    $temp++;
  471: 	}
  472:     }
  473:     if ($target eq 'web') {
  474:         my $questiontype;
  475:         if ($Apache::lonhomework::type eq 'randomizetry') {
  476:             $questiontype = $Apache::lonhomework::type;
  477:         }
  478: 	&Apache::response::setup_prior_tries_hash(\&format_prior_answer,
  479: 						  [\@whichfoils,
  480: 						   'submissiongrading'],
  481:                                                   $questiontype);
  482:     }
  483:     if ($target ne 'tex') {$result.="<br />";} else {$result.=' \vskip 0 mm ';}
  484:     return $result;
  485: }
  486: 
  487: sub getfoilcounts {
  488:     my ($max)=@_;
  489:     # +1 since instructors will count from 1
  490:     my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
  491:     if (&Apache::response::showallfoils()) { $max=$count; }
  492:     if ($count>$max) { $count=$max } 
  493:     &Apache::lonxml::debug("Count is $count from $max");
  494:     return $count;
  495: }
  496: 
  497: sub whichfoils {
  498:     my ($max,$randomize)=@_;
  499:     return &Apache::response::whichorder($max,$randomize,
  500: 					 &Apache::response::showallfoils(),
  501: 					 \%Apache::response::foilgroup);
  502: }
  503: 
  504: sub start_conceptgroup {
  505:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  506:     $Apache::rankresponse::conceptgroup=1;
  507:     %Apache::response::conceptgroup=();
  508:     my $result;
  509:     if ($target eq 'edit') {
  510: 	$result.=&Apache::edit::tag_start($target,$token,
  511: 					  "Concept Grouped Foils");
  512: 	$result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
  513: 	    &Apache::edit::end_row().&Apache::edit::start_spanning_row();
  514:     }
  515:     if ($target eq 'modified') {
  516: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  517: 						     $safeeval,'concept');
  518: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  519:     }
  520:     return $result;
  521: }
  522: 
  523: sub end_conceptgroup {
  524:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  525:     $Apache::rankresponse::conceptgroup=0;
  526:     my $result='';
  527:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  528: 	$target eq 'tex' || $target eq 'analyze') {
  529: 	#if not there aren't any foils to display and thus no question
  530: 	&Apache::response::pick_foil_for_concept($target,
  531: 						 ['value','text','location'],
  532: 						 \%Apache::hint::rank,
  533: 						 $parstack,$safeeval);
  534:     } elsif ($target eq 'edit') {
  535: 	$result=&Apache::edit::end_table();
  536:     }
  537:     return $result;
  538: }
  539: 
  540: sub insert_conceptgroup {
  541:     my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
  542:     return $result;
  543: }
  544: 
  545: sub start_foil {
  546:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  547:     my $result='';
  548:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
  549: 	&Apache::lonxml::startredirection;
  550: 	if ($target eq 'analyze') {
  551: 	    &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
  552: 	}
  553:     } elsif ($target eq 'edit') {
  554: 	$result=&Apache::edit::tag_start($target,$token,"Foil");
  555: 	my $level='-2';
  556: 	if ($$tagstack[-2] eq 'conceptgroup') { $level = '-3'; }
  557: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
  558: 	$result.= &Apache::edit::text_arg('Rank Value:','value',$token,'15');
  559: 	my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
  560: 						 $safeeval,'-3');
  561: 	if ($randomize ne 'no') {
  562: 	    $result.=&Apache::edit::select_arg('Location:','location',
  563: 					     ['random','top','bottom'],$token);
  564: 	}
  565: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  566:     } elsif ($target eq 'modified') {
  567: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  568: 						     $safeeval,'value',
  569: 						     'name','location');
  570: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  571:     }
  572:     return $result;
  573: }
  574: 
  575: sub end_foil {
  576:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  577:     my $text ='';
  578:     my $result = '';
  579:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
  580: 	$text=&Apache::lonxml::endredirection;
  581:     }
  582:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  583: 	$target eq 'tex' || $target eq 'analyze') {
  584: 	my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
  585: 	if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
  586: 	    $text='\vskip 5mm $\triangleright$ '.$text;
  587: 	}
  588: 	if ($value ne 'unused') {
  589: 	    my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  590: 	    &Apache::lonxml::debug("Got a name of :$name:");
  591: 	    if ($name eq "") {
  592: 		&Apache::lonxml::warning(&mt('Foils without names exist. This can cause problems to malfunction.'));
  593: 		$name=$Apache::lonxml::curdepth;
  594: 	    }
  595: 	    &Apache::lonxml::debug("Using a name of :$name:");
  596:             if ($name eq "0") {
  597:                 &Apache::lonxml::error(&mt('Foil name [_1] is not supported. Please choose another name.','<b><tt>'.$name.'</tt></b>'));
  598:             }
  599: 	    if (defined($Apache::response::foilnames{$name})) {
  600: 		&Apache::lonxml::error(&mt('Foil name [_1] appears more than once. Foil names need to be unique.','<b><tt>'.$name.'</tt></b>'));
  601: 	    }
  602: 	    $Apache::response::foilnames{$name}++;
  603: 	    my $location =&Apache::lonxml::get_param('location',$parstack,
  604: 						     $safeeval);
  605: 	    if ( $Apache::rankresponse::conceptgroup
  606: 		 && !&Apache::response::showallfoils() ) {
  607: 		push @{ $Apache::response::conceptgroup{'names'} }, $name;
  608: 		$Apache::response::conceptgroup{"$name.value"} = $value;
  609: 		$Apache::response::conceptgroup{"$name.text"} = $text;
  610: 		$Apache::response::conceptgroup{"$name.location"} = $location;
  611: 	    } else {
  612: 		push @{ $Apache::response::foilgroup{'names'} }, $name;
  613: 		$Apache::response::foilgroup{"$name.value"} = $value;
  614: 		$Apache::response::foilgroup{"$name.text"} = $text;
  615: 		$Apache::response::foilgroup{"$name.location"} = $location;
  616: 	    }
  617: 	}
  618:     }
  619:     if ($target eq 'edit') {
  620: 	$result.= &Apache::edit::tag_end($target,$token,'');
  621:     }
  622:     return $result;
  623: }
  624: 
  625: sub insert_foil {
  626:     return '
  627: <foil name="" value="unused">
  628: <startouttext />
  629: <endouttext />
  630: </foil>';
  631: }
  632: 1;
  633: __END__

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