File:  [LON-CAPA] / loncom / homework / rankresponse.pm
Revision 1.40: download - view: text, annotated - select for diffs
Thu Oct 21 06:35:57 2004 UTC (19 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- <rank and match support survey mode BUG#36

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

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