File:  [LON-CAPA] / loncom / homework / rankresponse.pm
Revision 1.17: download - view: text, annotated - select for diffs
Mon Jul 21 19:42:17 2003 UTC (20 years, 9 months ago) by albertel
Branches: MAIN
CVS tags: version_1_0_2, version_1_0_1, version_1_0_0, version_0_99_5, version_0_99_4, HEAD
- BUG#1928 string ordered not numerically ordered

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

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