File:  [LON-CAPA] / loncom / homework / rankresponse.pm
Revision 1.33: download - view: text, annotated - select for diffs
Wed Apr 7 19:31:06 2004 UTC (20 years ago) by sakharuk
Branches: MAIN
CVS tags: HEAD
Changes to avoide overlapping of bubbles in the exam mode.

    1: # The LearningOnline Network with CAPA
    2: # rank style response
    3: #
    4: # $Id: rankresponse.pm,v 1.33 2004/04/07 19:31:06 sakharuk 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: 
   34: BEGIN {
   35:     &Apache::lonxml::register('Apache::rankresponse',('rankresponse'));
   36: }
   37: 
   38: sub start_rankresponse {
   39:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   40:     my $result;
   41:     #when in a rank response use these
   42:     &Apache::lonxml::register('Apache::rankresponse',
   43: 			      ('foilgroup','foil','conceptgroup'));
   44:     push (@Apache::lonxml::namespace,'rankresponse');
   45:     my $id = &Apache::response::start_response($parstack,$safeeval);
   46:     %Apache::hint::rank=();
   47:     undef(%Apache::response::foilnames);
   48:     if ($target eq 'meta') {
   49: 	$result=&Apache::response::meta_package_write('rankresponse');
   50:     } elsif ($target eq 'edit' ) {
   51: 	$result.=&Apache::edit::start_table($token).
   52: 	    '<tr><td>'.&Apache::lonxml::description($token)."</td><td>Delete:".
   53: 	    &Apache::edit::deletelist($target,$token)
   54: 	    ."</td><td>&nbsp".&Apache::edit::end_row()
   55: 	    .&Apache::edit::start_spanning_row();
   56: 	
   57: 	$result.=
   58: 	    &Apache::edit::text_arg('Max Number Of Shown Foils:','max',$token,'4').
   59: 	    &Apache::edit::select_arg('Randomize Foil Order','randomize',
   60: 				      ['yes','no'],$token).
   61: 	    &Apache::edit::end_row().&Apache::edit::start_spanning_row()."\n";
   62:     } elsif ($target eq 'modified') {
   63: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
   64: 						     $safeeval,'max',
   65: 						     'randomize');
   66: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
   67:     } elsif ($target eq 'analyze') {
   68: 	my $part_id="$Apache::inputtags::part.$id";
   69: 	push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
   70:     }
   71:     return $result;
   72: }
   73: 
   74: sub end_rankresponse {
   75:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   76:     my $result;
   77:     if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
   78:     &Apache::response::end_response;
   79:     pop @Apache::lonxml::namespace;
   80:     &Apache::lonxml::deregister('Apache::rankresponse',
   81: 				('foilgroup','foil','conceptgroup'));
   82:     undef(%Apache::response::foilnames);
   83:     return $result;
   84: }
   85: 
   86: %Apache::response::foilgroup=();
   87: sub start_foilgroup {
   88:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   89:     my $result;
   90:     %Apache::response::foilgroup=();
   91:     $Apache::rankresponse::conceptgroup=0;
   92:     &Apache::response::setrandomnumber();
   93:     return $result;
   94: }
   95: 
   96: sub end_foilgroup {
   97:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   98:     my $result;
   99:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
  100: 	$target eq 'tex' || $target eq 'analyze') {
  101: 	my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
  102: 	my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
  103: 						   $safeeval,'-2');
  104: 	my $tol = &Apache::lonxml::get_param('tol',$parstack,$safeeval,'-2');
  105: 	if (!defined($tol)) { $tol=0; }
  106: 	if ($target eq 'web' || $target eq 'tex') {
  107: 	    $result=&displayfoils($target,$max,$randomize,$tol);
  108: 	} elsif ($target eq 'answer' ) {
  109: 	    $result=&displayanswers($max,$randomize,$tol);
  110: 	} elsif ( $target eq 'grade') {
  111: 	    &grade_response($max,$randomize,$tol);
  112: 	} elsif ( $target eq 'analyze') {
  113: 	    my @shown = &whichfoils($max,$randomize);
  114: 	    &Apache::response::analyze_store_foilgroup(\@shown,
  115: 						  ['text','value','location']);
  116: 	}
  117: 	&Apache::lonxml::increment_counter(&getfoilcounts($max));
  118:     } elsif ($target eq 'edit') {
  119: 	$result=&Apache::edit::end_table();
  120:     }
  121:     return $result;
  122: }
  123: 
  124: sub get_correct_order {
  125:     my ($tol,@foils) =@_;
  126:     my @correctorder;
  127:     my @value_names;
  128:     foreach my $name (@foils) {
  129: 	my @pair=($Apache::response::foilgroup{$name.'.value'},$name);
  130: 	push(@value_names,\@pair);
  131:     }
  132:     @value_names =
  133: 	sort {
  134: 	    if (abs($a->[0] - $b->[0]) > $tol) {return ($a->[0] <=> $b->[0]);}
  135: 	    return 0;
  136: 	} @value_names;
  137:     my @value_names_tmp=@value_names;
  138:     my $firstpair=shift(@value_names_tmp);
  139:     my $order=1;
  140:     my %order;
  141:     my $count=1;
  142:     my $lastvalue=$firstpair->[0];
  143:     $order{$firstpair->[1]}=$order;
  144:     foreach my $pair (@value_names_tmp) {
  145: 	$count++;
  146: 	if (abs($pair->[0]-$lastvalue) > $tol ) {
  147: 	    $order=$count;
  148: 	}
  149: 	$order{$pair->[1]}=$order;
  150: 	$lastvalue=$pair->[0];
  151:     }
  152:     foreach my $name (@foils) {
  153: 	push(@correctorder,$order{$name});
  154:     }
  155:     &Apache::lonhomework::showhash('b' => \@value_names);
  156:     &Apache::lonhomework::showhash('b' => \@correctorder);
  157:     return @correctorder;
  158: }
  159: 
  160: sub displayanswers {
  161:     my ($max,$randomize,$tol,@opt)=@_;
  162:     if (!defined(@{ $Apache::response::foilgroup{'names'} })) { return; }
  163:     my @names = @{ $Apache::response::foilgroup{'names'} };
  164:     my @whichfoils = &whichfoils($max,$randomize);
  165:     my $result=&Apache::response::answer_header('rankresponse');
  166:     my @correctorder=&get_correct_order($tol,@whichfoils);
  167:     foreach my $order (@correctorder) {
  168: 	$result.=&Apache::response::answer_part('rankresponse',$order);
  169:     }
  170:     $result.=&Apache::response::answer_footer('rankresponse');
  171:     return $result;
  172: }
  173: 
  174: sub check_response_order {
  175:     my (%responsehash)=@_;
  176:     my @order=sort(values(%responsehash));
  177:     my $lastvalue=0;
  178:     my $expected=1;
  179:     my $malformed=0;
  180:     foreach my $current (@order) {
  181: 	&Apache::lonxml::debug("$lastvalue $expected $malformed");
  182: 	if (!($current == $lastvalue || $current == $expected)) {
  183: 	    $malformed=1;
  184: 	}
  185: 	$expected++;
  186: 	$lastvalue=$current;
  187:     }
  188:     return $malformed;
  189: }
  190: 
  191: sub grade_response {
  192:     my ($max,$randomize,$tol)=@_;
  193:     my (@whichfoils)=&whichfoils($max,$randomize);
  194:     if (!defined($ENV{'form.submitted'})) { return; }
  195:     my %responsehash;
  196:     my %grade;
  197:     my ($temp,$right,$wrong,$ignored)=(1,0,0,0);
  198:     my @correctorder=&get_correct_order($tol,@whichfoils);
  199:     foreach my $name (@whichfoils) {
  200: 	my $response = &Apache::response::getresponse($temp);
  201: 	my $value=shift(@correctorder);
  202: 	if ( $response =~ /[^\s]/) {
  203: 	    $responsehash{$name}=$response;
  204: 	    &Apache::lonxml::debug("submitted a $response for $value<br />\n");
  205: 	    if ($value eq $response) {
  206: 		$grade{$name}='1'; $right++;
  207: 	    } else {
  208: 		$grade{$name}='0'; $wrong++;
  209: 	    }
  210: 	} else {
  211: 	    $ignored++;
  212: 	}
  213: 	$temp++;
  214:     }
  215:     my $malformed=&check_response_order(%responsehash);
  216:     my $part=$Apache::inputtags::part;
  217:     my $id = $Apache::inputtags::response['-1'];
  218:     my $responsestr=&Apache::lonnet::hash2str(%responsehash);
  219:     my $gradestr   =&Apache::lonnet::hash2str(%grade);
  220:     my %previous=&Apache::response::check_for_previous($responsestr,
  221: 						       $part,$id);
  222:     &Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored and was $malformed malformed");
  223:     my $ad;
  224:     if ($malformed) {
  225: 	$ad='MISORDERED_RANK';
  226:     } elsif ($wrong==0 && $ignored==0) {
  227: 	$ad='EXACT_ANS';
  228:     } elsif ($wrong==0 && $right==0) {
  229: 	#nothing submitted
  230:     } else {
  231: 	if ($ignored==0) {
  232: 	    $ad='INCORRECT';
  233: 	} else {
  234: 	    $ad='MISSING_ANSWER';
  235: 	}
  236:     }
  237:     $Apache::lonhomework::results{"resource.$part.$id.submission"}=
  238: 	$responsestr;
  239:     $Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=$gradestr;
  240:     $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
  241:     &Apache::response::handle_previous(\%previous,$ad);
  242: }
  243: 
  244: sub displayfoils {
  245:     my ($target,$max,$randomize,$tol)=@_;
  246:     my $result;
  247:     my @alphabet=('A'..'Z');
  248:     my (@whichfoils)=&whichfoils($max,$randomize);
  249:     my $part=$Apache::inputtags::part;
  250:     my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
  251:     my @whichopt=(1..($#whichfoils+1));
  252:     my @correctorder=&get_correct_order($tol,@whichfoils);
  253:     if ( &Apache::response::show_answer() && ($target ne 'tex')) {
  254: 	foreach my $name (@whichfoils) {
  255: 	    my $text=$Apache::response::foilgroup{$name.'.text'};
  256: 	    my $value=shift(@correctorder);
  257: 	    if ($target eq 'web') {$result.='<br />';} else {$result.=' \strut\\\\\strut ';}
  258: 	    $result.=$value.':'.$text;
  259: 	}
  260:     } else {
  261: 	my $i = 0;
  262: 	my $temp=1;
  263: 	my $id=$Apache::inputtags::response[-1];
  264: 	my $part=$Apache::inputtags::part;
  265: 	my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
  266: 	my %lastresponse=&Apache::lonnet::str2hash($lastresponse); 
  267: 	my @alp = splice @alphabet, 0, $#whichopt + 1;
  268: 	my $internal_counter=$Apache::lonxml::counter;
  269: 	foreach my $name (@whichfoils) {
  270: 	    my $lastopt=$lastresponse{$name};
  271: 	    my $optionlist='';
  272: 	    if ($target ne 'tex') {$optionlist="<option></option>\n";}
  273: 	    my $option;
  274: 	    foreach $option (@whichopt) {
  275: 		if ($option eq $lastopt) {
  276: 		    if ($target ne 'tex') {$optionlist.="<option selected=\"on\">$option</option>\n";}
  277: 		} else {
  278: 		    if ($target ne 'tex') {$optionlist.="<option>$option</option>\n";}
  279: 		}
  280: 	    }
  281: 	    if ($target ne 'tex' && $Apache::lonhomework::type ne 'exam') {
  282: 		$optionlist='<select name="HWVAL_'.
  283: 		    $Apache::inputtags::response[-1].':'.$temp.'">'.
  284: 		        $optionlist."</select>\n";
  285: 	    } else {
  286: 		$optionlist=' '.$temp.' '.$optionlist.' ';
  287: 	    }
  288: 	    my $text=$Apache::response::foilgroup{$name.'.text'};
  289: 	    if ($target ne 'tex') {
  290: 		if ($Apache::lonhomework::type ne 'exam') {
  291: 		    $result.='<br />'.$optionlist.$text."\n";
  292: 		} else {
  293: 		    $result.='<br />'.$text."\n";
  294: 		}
  295: 		if ($Apache::lonhomework::type eq 'exam') {
  296: 		    my @values=(1..scalar(@whichopt));
  297: 		    $result.=&Apache::optionresponse::webbubbles(\@values,\@whichopt,$temp,$lastopt);
  298: 		}
  299: 	    } else {
  300: 		if ($Apache::lonhomework::type eq 'exam') {
  301: 		    $result.='\vskip 0 mm   '.$text.' \vskip 0 mm '."\n";
  302: 		    $result.='\vskip -1 mm\noindent\begin{enumerate}\item[\textbf{'.$internal_counter.'}.]'.&Apache::optionresponse::bubbles(\@alp,\@whichopt).'\end{enumerate} \vskip -8 mm \strut ';
  303: 		    $internal_counter++;
  304: 		} else {
  305: 		    $result.=' \vskip 0mm \framebox[5 mm][s]{\tiny\strut} '.$text."\n";
  306: 		}
  307: 	    }
  308: 	    $temp++;
  309: 	}
  310:     }
  311:     if ($target ne 'tex') {$result.="<br />";} else {$result.=' \vskip 0 mm ';}
  312:     return $result;
  313: }
  314: 
  315: sub getfoilcounts {
  316:     my ($max)=@_;
  317:     # +1 since instructors will count from 1
  318:     my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
  319:     if (&Apache::response::showallfoils()) { $max=$count; }
  320:     if ($count>$max) { $count=$max } 
  321:     &Apache::lonxml::debug("Count is $count from $max");
  322:     return $count;
  323: }
  324: 
  325: sub whichfoils {
  326:     my ($max,$randomize)=@_;
  327:     return &Apache::response::whichorder($max,$randomize,
  328: 					 &Apache::response::showallfoils(),
  329: 					 \%Apache::response::foilgroup);
  330: }
  331: 
  332: sub start_conceptgroup {
  333:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  334:     $Apache::rankresponse::conceptgroup=1;
  335:     %Apache::response::conceptgroup=();
  336:     my $result;
  337:     if ($target eq 'edit') {
  338: 	$result.=&Apache::edit::tag_start($target,$token,
  339: 					  "Concept Grouped Foils");
  340: 	$result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
  341: 	    &Apache::edit::end_row().&Apache::edit::start_spanning_row();
  342:     }
  343:     if ($target eq 'modified') {
  344: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  345: 						     $safeeval,'concept');
  346: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  347:     }
  348:     return $result;
  349: }
  350: 
  351: sub end_conceptgroup {
  352:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  353:     $Apache::rankresponse::conceptgroup=0;
  354:     my $result='';
  355:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  356: 	$target eq 'tex' || $target eq 'analyze') {
  357: 	#if not there aren't any foils to display and thus no question
  358: 	&Apache::response::pick_foil_for_concept($target,
  359: 						 ['value','text','location'],
  360: 						 \%Apache::hint::rank,
  361: 						 $parstack,$safeeval);
  362:     } elsif ($target eq 'edit') {
  363: 	$result=&Apache::edit::end_table();
  364:     }
  365:     return $result;
  366: }
  367: 
  368: sub insert_conceptgroup {
  369:     my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
  370:     return $result;
  371: }
  372: 
  373: sub start_foil {
  374:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  375:     my $result='';
  376:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
  377: 	&Apache::lonxml::startredirection;
  378:     } elsif ($target eq 'edit') {
  379: 	$result=&Apache::edit::tag_start($target,$token,"Foil");
  380: 	my $level='-2';
  381: 	if ($$tagstack[-2] eq 'conceptgroup') { $level = '-3'; }
  382: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
  383: 	$result.= &Apache::edit::text_arg('Rank Value:','value',$token,'15');
  384: 	my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
  385: 						 $safeeval,'-3');
  386: 	if ($randomize ne 'no') {
  387: 	    $result.=&Apache::edit::select_arg('Location:','location',
  388: 					     ['random','top','bottom'],$token);
  389: 	}
  390: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  391:     } elsif ($target eq 'modified') {
  392: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  393: 						     $safeeval,'value',
  394: 						     'name','location');
  395: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  396:     }
  397:     return $result;
  398: }
  399: 
  400: sub end_foil {
  401:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  402:     my $text ='';
  403:     my $result = '';
  404:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
  405: 	$text=&Apache::lonxml::endredirection;
  406:     }
  407:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  408: 	$target eq 'tex' || $target eq 'analyze') {
  409: 	my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
  410: 	if ($value ne 'unused') {
  411: 	    my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  412: 	    &Apache::lonxml::debug("Got a name of :$name:");
  413: 	    if (!$name) { $name=$Apache::lonxml::curdepth; }
  414: 	    &Apache::lonxml::debug("Using a name of :$name:");
  415: 	    if (defined($Apache::response::foilnames{$name})) {
  416: 		&Apache::lonxml::error(&mt("Foil name <b><tt>[_1]</tt></b> appears more than once. Foil names need to be unique.",$name));
  417: 	    }
  418: 	    $Apache::response::foilnames{$name}++;
  419: 	    my $location =&Apache::lonxml::get_param('location',$parstack,
  420: 						     $safeeval);
  421: 	    if ( $Apache::rankresponse::conceptgroup
  422: 		 && !&Apache::response::showallfoils() ) {
  423: 		push @{ $Apache::response::conceptgroup{'names'} }, $name;
  424: 		$Apache::response::conceptgroup{"$name.value"} = $value;
  425: 		if ($target eq 'tex' and $Apache::lonhomework::type eq 'exam') {
  426: 		    $Apache::response::conceptgroup{"$name.text"} = ' $\triangleright$ '.$text;
  427: 		} else {
  428: 		    $Apache::response::conceptgroup{"$name.text"} = $text;
  429: 		}
  430: 		$Apache::response::conceptgroup{"$name.location"} = $location;
  431: 	    } else {
  432: 		push @{ $Apache::response::foilgroup{'names'} }, $name;
  433: 		$Apache::response::foilgroup{"$name.value"} = $value;
  434: 		if ($target eq 'tex' and $Apache::lonhomework::type eq 'exam') {
  435: 		    $Apache::response::foilgroup{"$name.text"} = '\vskip 5 mm $\triangleright$ '.$text;
  436: 		} else {
  437: 		    if ($target eq 'tex' and $Apache::lonhomework::type eq 'exam') {
  438: 			$Apache::response::foilgroup{"$name.text"} = ' $\triangleright$ '.$text;
  439: 		    } else {
  440: 			$Apache::response::foilgroup{"$name.text"} = $text;
  441: 		    } 
  442: 		}
  443: 		$Apache::response::foilgroup{"$name.location"} = $location;
  444: 	    }
  445: 	}
  446:     }
  447:     if ($target eq 'edit') {
  448: 	$result.= &Apache::edit::tag_end($target,$token,'');
  449:     }
  450:     return $result;
  451: }
  452: 
  453: sub insert_foil {
  454:     return '
  455: <foil name="" value="unused">
  456: <startouttext />
  457: <endouttext />
  458: </foil>';
  459: }
  460: 1;
  461: __END__

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