Annotation of loncom/homework/rankresponse.pm, revision 1.29

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

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