File:  [LON-CAPA] / loncom / homework / rankresponse.pm
Revision 1.3: download - view: text, annotated - select for diffs
Mon Mar 17 20:08:21 2003 UTC (21 years, 1 month ago) by sakharuk
Branches: MAIN
CVS tags: HEAD
Can be printed now. Tested on my example and Ed's more sofisticated one (/msu/kashy/Testing/C2LC/A7rankingColors.problem). Works fine. Nevertheless needs more testing and especially for the situations where answers are there.

    1: # The LearningOnline Network with CAPA
    2: # rank style response
    3: #
    4: # $Id: rankresponse.pm,v 1.3 2003/03/17 20:08:21 sakharuk Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: # 2/21 Guy
   29: 
   30: package Apache::rankresponse;
   31: use strict;
   32: use HTML::Entities();
   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:     }
  108:     return $result;
  109: }
  110: 
  111: sub get_correct_order {
  112:     my ($tol,@foils) =@_;
  113:     my @correctorder;
  114:     my @value_names;
  115:     foreach my $name (@foils) {
  116: 	my @pair=($Apache::response::foilgroup{$name.'.value'},$name);
  117: 	push(@value_names,\@pair);
  118:     }
  119:     @value_names =
  120: 	sort {
  121: 	    if (abs($a->[0] - $b->[0]) > $tol) {return ($a->[0] cmp $b->[0]);}
  122: 	    return 0;
  123: 	} @value_names;
  124:     my @value_names_tmp=@value_names;
  125:     my $firstpair=shift(@value_names_tmp);
  126:     my $order=1;
  127:     my %order;
  128:     my $count=1;
  129:     my $lastvalue=$firstpair->[0];
  130:     $order{$firstpair->[1]}=$order;
  131:     foreach my $pair (@value_names_tmp) {
  132: 	$count++;
  133: 	if (abs($pair->[0]-$lastvalue) > $tol ) {
  134: 	    $order=$count;
  135: 	}
  136: 	$order{$pair->[1]}=$order;
  137: 	$lastvalue=$pair->[0];
  138:     }
  139:     foreach my $name (@foils) {
  140: 	push(@correctorder,$order{$name});
  141:     }
  142:     &Apache::lonhomework::showhash('b' => \@value_names);
  143:     &Apache::lonhomework::showhash('b' => \@correctorder);
  144:     return @correctorder;
  145: }
  146: 
  147: sub displayanswers {
  148:     my ($max,$randomize,$tol,@opt)=@_;
  149:     if (!defined(@{ $Apache::response::foilgroup{'names'} })) { return; }
  150:     my @names = @{ $Apache::response::foilgroup{'names'} };
  151:     my @whichfoils = &whichfoils($max,$randomize);
  152:     my $result=&Apache::response::answer_header('rankresponse');
  153:     my @correctorder=&get_correct_order($tol,@whichfoils);
  154:     foreach my $order (@correctorder) {
  155: 	$result.=&Apache::response::answer_part('rankresponse',$order);
  156:     }
  157:     $result.=&Apache::response::answer_footer('rankresponse');
  158:     return $result;
  159: }
  160: 
  161: sub check_response_order {
  162:     my (%responsehash)=@_;
  163:     my @order=sort(values(%responsehash));
  164:     my $lastvalue=0;
  165:     my $expected=1;
  166:     my $malformed=0;
  167:     foreach my $current (@order) {
  168: 	&Apache::lonxml::debug("$lastvalue $expected $malformed");
  169: 	if (!($current == $lastvalue || $current == $expected)) {
  170: 	    $malformed=1;
  171: 	}
  172: 	$expected++;
  173: 	$lastvalue=$current;
  174:     }
  175:     return $malformed;
  176: }
  177: 
  178: sub grade_response {
  179:     my ($max,$randomize,$tol)=@_;
  180:     my (@whichfoils)=&whichfoils($max,$randomize);
  181:     if (!defined($ENV{'form.submitted'})) { return; }
  182:     my %responsehash;
  183:     my %grade;
  184:     my ($temp,$right,$wrong,$ignored)=(0,0,0,0);
  185:     my @correctorder=&get_correct_order($tol,@whichfoils);
  186:     foreach my $name (@whichfoils) {
  187: 	my $response = $ENV{'form.HWVAL_'.$Apache::inputtags::response['-1'].":$temp"};
  188: 	$responsehash{$name}=$response;
  189: 	my $value=shift(@correctorder);
  190: 	if ( $response =~ /[^\s]/) {
  191: 	    &Apache::lonxml::debug("submitted a $response for $value<br />\n");
  192: 	    if ($value eq $response) {
  193: 		$grade{$name}='1'; $right++;
  194: 	    } else {
  195: 		$grade{$name}='0'; $wrong++;
  196: 	    }
  197: 	} else {
  198: 	    $ignored++;
  199: 	}
  200: 	$temp++;
  201:     }
  202:     my $malformed=&check_response_order(%responsehash);
  203:     my $part=$Apache::inputtags::part;
  204:     my $id = $Apache::inputtags::response['-1'];
  205:     my $responsestr=&Apache::lonnet::hash2str(%responsehash);
  206:     my $gradestr   =&Apache::lonnet::hash2str(%grade);
  207:     my %previous=&Apache::response::check_for_previous($responsestr,
  208: 						       $part,$id);
  209:     &Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored and was $malformed malformed");
  210:     my $ad;
  211:     if ($malformed) {
  212: 	$ad='MISORDERED_RANK';
  213:     } elsif ($wrong==0 && $ignored==0) {
  214: 	$ad='EXACT_ANS';
  215:     } elsif ($wrong==0 && $right==0) {
  216: 	#nothing submitted
  217:     } else {
  218: 	if ($ignored==0) {
  219: 	    $ad='INCORRECT';
  220: 	} else {
  221: 	    $ad='MISSING_ANSWER';
  222: 	}
  223:     }
  224:     $Apache::lonhomework::results{"resource.$part.$id.submission"}=
  225: 	$responsestr;
  226:     $Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=$gradestr;
  227:     $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
  228:     &Apache::response::handle_previous(\%previous,$ad);
  229: }
  230: 
  231: sub displayfoils {
  232:     my ($target,$max,$randomize,$tol)=@_;
  233:     my $result;
  234:     my (@whichfoils)=&whichfoils($max,$randomize);
  235:     my $part=$Apache::inputtags::part;
  236:     my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
  237:     my $status=$Apache::inputtags::status[-1];
  238:     my @whichopt=(1..($#whichfoils+1));
  239:     my @correctorder=&get_correct_order($tol,@whichfoils);
  240:     if (($solved =~ /^correct/) || ($status eq  'SHOW_ANSWER')) {
  241: 	foreach my $name (@whichfoils) {
  242: 	    my $text=$Apache::response::foilgroup{$name.'.text'};
  243: 	    my $value=shift(@correctorder);
  244: 	    $result.='<br />'.$value.':'.$text;
  245: 	}
  246:     } else {
  247: 	my $i = 0;
  248: 	my $temp=0;
  249: 	my $id=$Apache::inputtags::response[-1];
  250: 	my $part=$Apache::inputtags::part;
  251: 	my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
  252: 	my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
  253: 	my $localcount = 1;
  254: 	foreach my $name (@whichfoils) {
  255: 	    my $lastopt=$lastresponse{$name};
  256: 	    my $optionlist='';
  257: 	    if ($target ne 'tex') {$optionlist="<option></option>\n";}
  258: 	    my $option;
  259: 	    foreach $option (@whichopt) {
  260: 		if ($option eq $lastopt) {
  261: 		    if ($target ne 'tex') {$optionlist.="<option selected=\"on\">$option</option>\n";}
  262: 		} else {
  263: 		    if ($target ne 'tex') {$optionlist.="<option>$option</option>\n";}
  264: 		}
  265: 	    }
  266: 	    if ($target ne 'tex') {
  267: 		$optionlist='<select name="HWVAL_'.
  268: 		    $Apache::inputtags::response[-1].':'.$temp.'">'.
  269: 		        $optionlist."</select>\n";
  270: 	    } else {
  271: 		$optionlist=' '.$temp.' '.$optionlist.' ';
  272: 	    }
  273: 	    my $text=$Apache::response::foilgroup{$name.'.text'};
  274: 	    if ($target ne 'tex') {
  275: 		$result.='<br />'.$optionlist.$text."\n";
  276: 	    } else {$result.=' \\\\ '.$localcount.'. '.$text."\n"; $localcount++;}
  277: 	    $temp++;
  278: 	}
  279:     }
  280:     if ($target ne 'tex') {$result.="<br />";} else {$result.=' \\\\ ';}
  281:     return $result;
  282: }
  283: 
  284: sub getfoilcounts {
  285:     my ($max)=@_;
  286:     # +1 since instructors will count from 1
  287:     my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
  288:     if (&Apache::response::showallfoils()) { $max=$count; }
  289:     if ($count>$max) { $count=$max } 
  290:     &Apache::lonxml::debug("Count is $count from $max");
  291:     return $count;
  292: }
  293: 
  294: sub whichfoils {
  295:     my ($max,$randomize)=@_;
  296:     $max = &getfoilcounts($max);
  297:  #   &Apache::lonxml::debug("man $max randomize $randomize");
  298:     if (!defined(@{ $Apache::response::foilgroup{'names'} })) { return; }
  299:     my @names = @{ $Apache::response::foilgroup{'names'} };
  300:     my @whichopt =();
  301:     my (%top,@toplist,%bottom,@bottomlist);
  302:     if (!(&Apache::response::showallfoils() || ($randomize eq 'no'))) {
  303: 	my $current=0;
  304: 	foreach my $name (@names) {
  305: 	    $current++;
  306: 	    if ($Apache::response::foilgroup{"$name.location"} eq 'top') {
  307: 		$top{$name}=$current;
  308: 	    } elsif ($Apache::response::foilgroup{"$name.location"} eq
  309: 		     'bottom') {
  310: 		$bottom{$name}=$current;
  311: 	    }
  312: 	}
  313:     }
  314:     while ((($#whichopt+1) < $max) && ($#names > -1)) {
  315: #	&Apache::lonxml::debug("Have $#whichopt max is $max");
  316: 	my $aopt;
  317: 	if (&Apache::response::showallfoils() || ($randomize eq 'no')) {
  318: 	    $aopt=0;
  319: 	} else {
  320: 	    $aopt=int(&Math::Random::random_uniform() * ($#names+1));
  321: 	}
  322: #	&Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
  323: 	$aopt=splice(@names,$aopt,1);
  324: #	&Apache::lonxml::debug("Picked $aopt");
  325: 	if ($top{$aopt}) {
  326: 	    $toplist[$top{$aopt}]=$aopt;
  327: 	} elsif ($bottom{$aopt}) {
  328: 	    $bottomlist[$bottom{$aopt}]=$aopt;
  329: 	} else {
  330: 	    push (@whichopt,$aopt);
  331: 	}
  332:     }
  333: #    &Apache::lonxml::debug("Grr, ".$#whichopt.":".$#toplist.':'.$#bottomlist);
  334:     for (my $i=0;$i<=$#toplist;$i++) {
  335: 	if ($toplist[$i]) { unshift(@whichopt,$toplist[$i]) }
  336:     }
  337:     for (my $i=0;$i<=$#bottomlist;$i++) {
  338: 	if ($bottomlist[$i]) { push(@whichopt,$bottomlist[$i]) }
  339:     }
  340:     &Apache::lonxml::debug("Grr, ".$#whichopt.":".$#toplist.':'.$#bottomlist);
  341:     return @whichopt;
  342: }
  343: 
  344: sub start_conceptgroup {
  345:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  346:     $Apache::rankresponse::conceptgroup=1;
  347:     %Apache::response::conceptgroup=();
  348:     my $result;
  349:     if ($target eq 'edit') {
  350: 	$result.=&Apache::edit::tag_start($target,$token,
  351: 					  "Concept Grouped Foils");
  352: 	$result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
  353: 	    &Apache::edit::end_row().&Apache::edit::start_spanning_row();
  354:     }
  355:     if ($target eq 'modified') {
  356: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  357: 						     $safeeval,'concept');
  358: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  359:     }
  360:     return $result;
  361: }
  362: 
  363: sub end_conceptgroup {
  364:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  365:     $Apache::rankresponse::conceptgroup=0;
  366:     my $result='';
  367:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ) {
  368: 	#if not there aren't any foils to display and thus no question
  369: 	if (defined(@{ $Apache::response::conceptgroup{'names'} })) {
  370: 	    my @names = @{ $Apache::response::conceptgroup{'names'} };
  371: 	    my $pick=int(&Math::Random::random_uniform() * ($#names+1));
  372: 	    my $name=$names[$pick];
  373: 	    push @{ $Apache::response::foilgroup{'names'} }, $name;
  374: 	    $Apache::response::foilgroup{"$name.value"} =
  375: 		$Apache::response::conceptgroup{"$name.value"};
  376: 	    $Apache::response::foilgroup{"$name.text"} =
  377: 		$Apache::response::conceptgroup{"$name.text"};
  378: 	    $Apache::response::foilgroup{"$name.location"} =
  379: 		$Apache::response::conceptgroup{"$name.location"};
  380: 	    my $concept = &Apache::lonxml::get_param('concept',$parstack,
  381: 						     $safeeval);
  382: 	    $Apache::response::foilgroup{"$name.concept"} = $concept;
  383: 	    &Apache::lonxml::debug("Selecting $name in $concept");
  384: 	    if ($target eq 'web') {
  385: 		my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
  386: 		push(@{ $Apache::hint::rank{"$part_id.concepts"} },
  387: 		     $concept);
  388: 		$Apache::hint::rank{"$part_id.concept.$concept"}=
  389: 		    $Apache::response::conceptgroup{'names'};
  390: 	    }
  391: 	}
  392:     } elsif ($target eq 'edit') {
  393: 	$result=&Apache::edit::end_table();
  394:     }
  395:     return $result;
  396: }
  397: 
  398: sub insert_conceptgroup {
  399:     my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
  400:     return $result;
  401: }
  402: 
  403: sub start_foil {
  404:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  405:     my $result='';
  406:     if ($target eq 'web' || $target eq 'tex') {
  407: 	&Apache::lonxml::startredirection;
  408:     } elsif ($target eq 'edit') {
  409: 	$result=&Apache::edit::tag_start($target,$token,"Foil");
  410: 	my $level='-2';
  411: 	if ($$tagstack[-2] eq 'conceptgroup') { $level = '-3'; }
  412: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
  413: 	$result.= &Apache::edit::text_arg('Rank Value:','value',$token,'15');
  414: 	my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
  415: 						 $safeeval,'-3');
  416: 	if ($randomize ne 'no') {
  417: 	    $result.=&Apache::edit::select_arg('Location:','location',
  418: 					     ['random','top','bottom'],$token);
  419: 	}
  420: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  421:     } elsif ($target eq 'modified') {
  422: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  423: 						     $safeeval,'value',
  424: 						     'name','location');
  425: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  426:     }
  427:     return $result;
  428: }
  429: 
  430: sub end_foil {
  431:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  432:     my $text ='';
  433:     my $result = '';
  434:     if ($target eq 'web' || $target eq 'tex') {
  435: 	$text=&Apache::lonxml::endredirection;
  436:     }
  437:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
  438: 	my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
  439: 	if ($value ne 'unused') {
  440: 	    my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  441: 	    my $location =&Apache::lonxml::get_param('location',$parstack,$safeeval);
  442: 	    &Apache::lonxml::debug("Got a name of :$name:");
  443: 	    if (!$name) { $name=$Apache::lonxml::curdepth; }
  444: 	    &Apache::lonxml::debug("Using a name of :$name:");
  445: 	    if ( $Apache::rankresponse::conceptgroup
  446: 		 && !&Apache::response::showallfoils() ) {
  447: 		push @{ $Apache::response::conceptgroup{'names'} }, $name;
  448: 		$Apache::response::conceptgroup{"$name.value"} = $value;
  449: 		$Apache::response::conceptgroup{"$name.text"} = $text;
  450: 		$Apache::response::conceptgroup{"$name.location"} = $location;
  451: 	    } else {
  452: 		push @{ $Apache::response::foilgroup{'names'} }, $name;
  453: 		$Apache::response::foilgroup{"$name.value"} = $value;
  454: 		$Apache::response::foilgroup{"$name.text"} = $text;
  455: 		$Apache::response::foilgroup{"$name.location"} = $location;
  456: 	    }
  457: 	}
  458:     }
  459:     if ($target eq 'edit') {
  460: 	$result.= &Apache::edit::tag_end($target,$token,'');
  461:     }
  462:     return $result;
  463: }
  464: 
  465: sub insert_foil {
  466:     return '
  467: <foil name="" value="unused">
  468: <startouttext />
  469: <endouttext />
  470: </foil>';
  471: }
  472: 1;
  473: __END__

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