File:  [LON-CAPA] / loncom / homework / rankresponse.pm
Revision 1.25: download - view: text, annotated - select for diffs
Mon Oct 27 19:27:09 2003 UTC (20 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- fixing up analyze mode for radiobutton/option/rank/match/image to all use the smae code

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

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