Annotation of loncom/homework/response.pm, revision 1.93

1.38      albertel    1: # The LearningOnline Network with CAPA
1.1       albertel    2: # various response type definitons response definition
1.53      albertel    3: #
1.93    ! albertel    4: # $Id: response.pm,v 1.92 2004/02/27 17:18:32 albertel Exp $
1.53      albertel    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: #
1.5       www        28: 
1.1       albertel   29: package Apache::response;
                     30: use strict;
1.93    ! albertel   31: use Apache::lonlocal;
1.1       albertel   32: 
1.57      harris41   33: BEGIN {
1.73      albertel   34:     &Apache::lonxml::register('Apache::response',('responseparam','parameter','dataresponse'));
1.1       albertel   35: }
                     36: 
1.13      albertel   37: sub start_response {
1.73      albertel   38:     my ($parstack,$safeeval)=@_;
                     39:     my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
                     40:     if ($id eq '') { $id = $Apache::lonxml::curdepth; }
                     41:     if ($#Apache::inputtags::import > -1) {
                     42: 	&Apache::lonxml::debug("Turning :$id: into");
                     43: 	$id = join('_',@Apache::inputtags::import).'_'.$id;
                     44: 	&Apache::lonxml::debug("New  :$id:");
                     45:     }
                     46:     push (@Apache::inputtags::response,$id);
                     47:     push (@Apache::inputtags::responselist,$id);
                     48:     @Apache::inputtags::inputlist=();
1.92      albertel   49:     if ($Apache::inputtags::part eq '') {
                     50: 	&Apache::lonxml::error(&HTML::Entities::encode(&mt("Found a <*response> outside of a <part> in a <part>ed problem")));
                     51:     }
                     52:     if ($Apache::inputtags::response_with_no_part &&
                     53: 	$Apache::inputtags::part ne '0') {
                     54: 	&Apache::lonxml::error(&HTML::Entities::encode(&mt("<*response>s are both inside of <part> and outside of <part>, this is not a valid problem, errors in grading may occur.")).'<br />');
                     55:     }
                     56:     if ($Apache::inputtags::part eq '0') {
                     57: 	$Apache::inputtags::response_with_no_part=1;
                     58:     }
1.73      albertel   59:     return $id;
1.13      albertel   60: }
                     61: 
                     62: sub end_response {
1.79      albertel   63:     #pop @Apache::inputtags::response;
1.73      albertel   64:     @Apache::inputtags::inputlist=();
                     65:     return '';
1.13      albertel   66: }
                     67: 
1.41      albertel   68: sub start_hintresponse {
1.73      albertel   69:     my ($parstack,$safeeval)=@_;
                     70:     my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
                     71:     if ($id eq '') { $id = $Apache::lonxml::curdepth; }
                     72:     push (@Apache::inputtags::response,$id);
1.79      albertel   73:     push (@Apache::inputtags::responselist,$id);
1.73      albertel   74:     push (@Apache::inputtags::paramstack,[%Apache::inputtags::params]);
                     75:     return $id;
1.41      albertel   76: }
                     77: 
                     78: sub end_hintresponse {
1.73      albertel   79:     pop @Apache::inputtags::response;
                     80:     if (defined($Apache::inputtags::paramstack[-1])) {
                     81: 	%Apache::inputtags::params=
                     82: 	    @{ pop(@Apache::inputtags::paramstack) };
                     83:     }
                     84:     return '';
1.41      albertel   85: }
                     86: 
1.38      albertel   87: # used by response to set the non-safe space random number generator to something
                     88: # that is stable and unique based on the part number and response number
1.26      albertel   89: sub setrandomnumber {
1.73      albertel   90:     my $rndseed;
1.88      albertel   91:     $rndseed=&Apache::structuretags::setup_rndseed();
                     92:     if (!defined($rndseed)) { $rndseed=&Apache::lonnet::rndseed(); }
1.73      albertel   93:     &Apache::lonxml::debug("randseed $rndseed");
                     94:     #  $rndseed=unpack("%32i",$rndseed);
1.74      albertel   95:     my $rndmod=(&Apache::lonnet::numval($Apache::inputtags::part) << 10);
1.73      albertel   96:     if (defined($Apache::inputtags::response['-1'])) {
1.88      albertel   97: 	$rndmod+=&Apache::lonnet::numval($Apache::inputtags::response[-1]);
1.73      albertel   98:     }
1.74      albertel   99:     if ($rndseed =~/,/) {
1.82      albertel  100: 	{
                    101: 	    use integer;
                    102: 	    my ($num1,$num2)=split(/,/,$rndseed);
                    103: 	    $num1+=$rndmod;
                    104: 	    $num2+=$rndmod;
                    105: 	    $rndseed="$num1,$num2";
                    106: 	}
1.74      albertel  107:     } else {
                    108: 	$rndseed+=$rndmod;
                    109:     }
                    110:     &Apache::lonnet::setup_random_from_rndseed($rndseed);
1.73      albertel  111:     &Apache::lonxml::debug("randseed $rndseed");
                    112:     return '';
1.26      albertel  113: }
                    114: 
1.7       www       115: sub meta_parameter_write {
1.38      albertel  116:     my ($name,$type,$default,$display)=@_;
1.41      albertel  117:     my $partref=$Apache::inputtags::part;
                    118:     my $result='<parameter part="'.$Apache::inputtags::part.'"';
                    119:     if (defined($Apache::inputtags::response[-1])) {
1.73      albertel  120: 	$result.=            ' id="'.$Apache::inputtags::response[-1].'"';
                    121: 	$partref.='_'.$Apache::inputtags::response[-1];
1.41      albertel  122:     }
                    123:     $result.=            ' name="'.$name.'"'.
                    124:                          ' type="'.$type.'"'.
1.89      albertel  125: (defined($default)?' default="'.$default.'"':'').
                    126: (defined($display)?' display="'.$display.' [Part: '.$partref.']"':'')
1.41      albertel  127:              .'></parameter>'
                    128:              ."\n";
                    129:     return $result;
1.33      www       130: }
                    131: 
                    132: sub meta_package_write {
                    133:     my $name=shift;
1.41      albertel  134:     my $result = '<parameter part="'.$Apache::inputtags::part.'"';
                    135:     if(defined($Apache::inputtags::response[-1])) {
1.73      albertel  136: 	$result.= ' id="'.$Apache::inputtags::response[-1].'"';
1.41      albertel  137:     }
                    138:     $result.=' package="'.$name.'"></parameter>'."\n";
                    139:     return $result;
1.7       www       140: }
                    141: 
                    142: sub meta_stores_write {
1.10      www       143:     my ($name,$type,$display)=@_;
1.41      albertel  144:     my $partref=$Apache::inputtags::part;
                    145:     my $result = '<stores part="'.$Apache::inputtags::part.'"';
                    146:     if (defined($Apache::inputtags::response[-1])) {
1.73      albertel  147: 	$result.=           ' id="'.$Apache::inputtags::response[-1].'"';
                    148: 	$partref.='_'.$Apache::inputtags::response[-1];
1.41      albertel  149:     }	
                    150:     $result.=          ' name="'.$name.'"'.
                    151:                        ' type="'.$type.'"'.
                    152: 	            ' display="'.$display.' [Part: '.$partref.']"'.
                    153: 		      "></stores>\n";
1.7       www       154: }
                    155: 
                    156: sub mandatory_part_meta {
                    157: #
                    158: # Autogenerate metadata for mandatory
                    159: # input (from RAT or lonparmset) and 
                    160: # output (to lonspreadsheet)
                    161: # of each part
                    162: #
1.73      albertel  163:     return
1.34      www       164: #    &meta_parameter_write('opendate','date_start','',
                    165: #                          'Opening Date').
                    166: #    &meta_parameter_write('duedate','date_end','',
                    167: #                          'Due Date').
                    168: #    &meta_parameter_write('answerdate','date_start','',
                    169: #                          'Show Answer Date').
                    170: #    &meta_parameter_write('weight','int_zeropos','',
                    171: #                          'Available Points').
                    172: #    &meta_parameter_write('maxtries','int_pos','',
                    173: #                          'Maximum Number of Tries').
1.73      albertel  174: 	&meta_package_write('part').
                    175:         &meta_stores_write('solved','string',
                    176: 			   'Problem Status').
                    177:         &meta_stores_write('tries','int_zeropos',
                    178: 			   'Number of Attempts').
                    179:         &meta_stores_write('awarded','float',
                    180: 			   'Partial Credit Factor');
1.7       www       181: #
                    182: # Note: responseid-specific data 'submission' and 'awarddetail'
                    183: # not available to spreadsheet -> skip here
                    184: #
1.86      albertel  185: }
                    186: 
                    187: sub meta_part_order {
                    188:     if (@Apache::inputtags::partlist) {
                    189: 	my @parts=@Apache::inputtags::partlist;
                    190: 	shift(@parts);
                    191: 	return '<partorder>'.join(',',@parts).'</partorder>';
                    192:     } else {
                    193: 	return '<partorder>0</partorder>';
                    194:     }
1.14      albertel  195: }
                    196: 
1.15      albertel  197: sub check_for_previous {
1.73      albertel  198:     my ($curresponse,$partid,$id) = @_;
                    199:     my %previous;
                    200:     $previous{'used'} = 0;
                    201:     foreach my $key (sort(keys(%Apache::lonhomework::history))) {
                    202: 	if ($key =~ /resource\.$partid\.$id\.submission/) {
                    203: 	    &Apache::lonxml::debug("Trying $key");
                    204: 	    my $pastresponse=$Apache::lonhomework::history{$key};
                    205: 	    if ($pastresponse eq $curresponse) {
                    206: 		$previous{'used'} = 1;
                    207: 		my $history;
                    208: 		if ( $key =~ /^(\d+):/ ) {
                    209: 		    $history=$1;
                    210: 		    $previous{'award'} = $Apache::lonhomework::history{"$history:resource.$partid.$id.awarddetail"};
                    211: 		    $previous{'last'}='0';
                    212: 		    push(@{ $previous{'version'} },$history);
                    213: 		} else {
                    214: 		    $previous{'award'} = $Apache::lonhomework::history{"resource.$partid.$id.awarddetail"};
                    215: 		    $previous{'last'}='1';
                    216: 		}
                    217: 		if (! $previous{'award'} ) { $previous{'award'} = 'UNKNOWN';	}
                    218: 		&Apache::lonxml::debug("got a match :$previous{'award'}:$previous{'used'}:");
                    219: 	    }
1.32      albertel  220: 	}
1.73      albertel  221:     }
                    222:     &Apache::lonhomework::showhash(%previous);
                    223:     return %previous;
1.54      albertel  224: }
                    225: 
                    226: sub handle_previous {
1.73      albertel  227:     my ($previous,$ad)=@_;
                    228:     if ($$previous{'used'} && ($$previous{'award'} eq $ad) ) {
                    229: 	if ($$previous{'last'}) {
                    230: 	    push(@Apache::inputtags::previous,'PREVIOUSLY_LAST');
                    231: 	} else {
                    232: 	    push(@Apache::inputtags::previous,'PREVIOUSLY_USED');
                    233: 	}
                    234: 	push(@Apache::inputtags::previous_version,$$previous{'version'});
1.54      albertel  235:     }
1.44      albertel  236: }
                    237: 
1.45      albertel  238: sub view_or_modify {
1.73      albertel  239:     my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
                    240:     my $myself=0;
                    241:     if ( ($name eq $ENV{'user.name'}) && ($domain eq $ENV{'user.domain'}) ) {
                    242: 	$myself=1;
                    243:     }
                    244:     my $vgr=&Apache::lonnet::allowed('vgr',$courseid);
                    245:     my $mgr=&Apache::lonnet::allowed('vgr',$courseid);
                    246:     if ($mgr) { return "M"; }
                    247:     if ($vgr) { return "V"; }
                    248:     if ($myself) { return "V"; }
                    249:     return '';
1.45      albertel  250: }
                    251: 
1.44      albertel  252: sub start_dataresponse {
1.73      albertel  253:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    254:     my $id = &Apache::response::start_response($parstack,$safeeval);
                    255:     my $result;
                    256:     if ($target eq 'web') {
                    257: 	$result = $token->[2]->{'display'}.':';
                    258:     } elsif ($target eq 'meta') {
                    259: 	$result = &Apache::response::meta_stores_write($token->[2]->{'name'},
                    260: 						       $token->[2]->{'type'},
                    261: 						       $token->[2]->{'display'});
                    262: 	$result .= &Apache::response::meta_package_write('dataresponse');
                    263:     }
                    264:     return $result;
1.44      albertel  265: }
                    266: 
                    267: sub end_dataresponse {
1.73      albertel  268:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    269:     my $result;
                    270:     if ( $target eq 'web' ) {
                    271:     } elsif ($target eq 'grade' ) {
                    272: 	if ( defined $ENV{'form.submitted'}) {
                    273: 	    my ($symb,$courseid,$domain,$name)=&Apache::lonxml::whichuser();
                    274: 	    my $allowed=&Apache::lonnet::allowed('mgr',$courseid);
                    275: 	    if ($allowed) {
                    276: 		&Apache::response::setup_params('dataresponse');
                    277: 		my $partid = $Apache::inputtags::part;
                    278: 		my $id = $Apache::inputtags::response['-1'];
                    279: 		my $response = $ENV{'form.HWVAL_'.$id};
                    280: 		my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    281: 		if ( $response =~ /[^\s]/) {
                    282: 		    $Apache::lonhomework::results{"resource.$partid.$id.$name"}=$response;
                    283: 		    $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
                    284: 		    $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}='SUBMITTED';
                    285: 		}
                    286: 	    } else {
                    287: 		$result='Not Permitted to change values.'
                    288: 	    }
1.45      albertel  289: 	}
1.73      albertel  290:     }
                    291:     &Apache::response::end_response;
                    292:     return $result;
1.3       albertel  293: }
                    294: 
1.83      albertel  295: sub decide_package {
                    296:     my ($tagstack)=@_;
                    297:     my $package;
                    298:     if ($$tagstack[-1] eq 'parameter') {
                    299: 	$package='part';
                    300:     } else {
                    301: 	my $i=-1;
                    302: 	while (defined($$tagstack[$i])) {
                    303: 	    if ($$tagstack[$i] =~ /(response|hint)$/) {
                    304: 		$package=$$tagstack[$i];
                    305: 		last;
                    306: 	    }
                    307: 	    $i--;
                    308: 	}
                    309:     }
                    310:     return $package;
                    311: }
                    312: 
1.3       albertel  313: sub start_responseparam {
1.73      albertel  314:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    315:     my $result='';
                    316:     if ($target eq 'meta') {
                    317: 	$result = &meta_parameter_write($token->[2]->{'name'},
                    318: 					$token->[2]->{'type'},
                    319: 					$token->[2]->{'default'},
                    320: 					$token->[2]->{'description'});
                    321:     } elsif ($target eq 'edit') {
                    322: 	$result.=&Apache::edit::tag_start($target,$token);
1.83      albertel  323: 	my $optionlist;
                    324: 	my $package=&decide_package($tagstack);
                    325: 	foreach my $key (sort(keys(%Apache::lonnet::packagetab))) {
                    326: 	    if ($key =~ /^\Q$package\E&(.*)&display$/) {
                    327: 		$optionlist.='<option value="'.$1.'">'.
                    328: 		    $Apache::lonnet::packagetab{$key}.'</option>';
                    329: 	    }
                    330: 	}
                    331: 	if (defined($optionlist)) {
                    332: 	    $result.='Use template: <select name="'.
                    333: 		&Apache::edit::html_element_name('parameter_package').'">'.
                    334: 		    '<option value=""></option>'.$optionlist.'</select><br />';
                    335: 	}
1.73      albertel  336: 	$result.=&Apache::edit::text_arg('Name:','name',$token).
                    337: 	    &Apache::edit::text_arg('Type:','type',$token).
                    338: 		&Apache::edit::text_arg('Description:','description',$token).
                    339: 		    &Apache::edit::text_arg('Default:','default',$token).
                    340: 			"</td></tr>";
                    341: 	$result.=&Apache::edit::end_table;
                    342:     } elsif ($target eq 'modified') {
1.83      albertel  343: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    344: 						     $safeeval,'name','type',
                    345: 						     'description','default');
                    346: 	my $element=&Apache::edit::html_element_name('parameter_package');
                    347: 	if (defined($ENV{"form.$element"}) && $ENV{"form.$element"} ne '') {
                    348: 	    my $name=$ENV{"form.$element"};
                    349: 	    my $tag=&decide_package($tagstack);
                    350: 	    $token->[2]->{'name'}=$name;
                    351: 	    $token->[2]->{'type'}=
                    352: 		$Apache::lonnet::packagetab{"$tag&$name&type"};
                    353: 	    $token->[2]->{'description'}=
                    354: 		$Apache::lonnet::packagetab{"$tag&$name&display"};
                    355: 	    $token->[2]->{'default'}=
                    356: 		$Apache::lonnet::packagetab{"$tag&$name&default"};
                    357: 	    $constructtag=1;
                    358: 	}
1.73      albertel  359: 	if ($constructtag) {
                    360: 	    $result = &Apache::edit::rebuild_tag($token);
                    361: 	    $result.=&Apache::edit::handle_insert();
                    362: 	}
                    363:     } elsif ($target eq 'grade' || $target eq 'answer' || $target eq 'web' ||
                    364: 	     $target eq 'tex' || $target eq 'analyze' ) {
                    365: 	if ($ENV{'request.state'} eq 'construct') {
                    366: 	    my $name   =&Apache::lonxml::get_param('name',$parstack,$safeeval);
                    367: 	    my $default=&Apache::lonxml::get_param('default',$parstack,
                    368: 						     $safeeval);
                    369: 	    if ($name) {$Apache::inputtags::params{$name}=$default;}
                    370: 	}
1.52      albertel  371:     }
1.73      albertel  372:     return $result;
1.3       albertel  373: }
                    374: 
                    375: sub end_responseparam {
1.73      albertel  376:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    377:     if ($target eq 'edit') { return ('','no'); }
                    378:     return '';
1.55      albertel  379: }
                    380: 
                    381: sub start_parameter {
1.73      albertel  382:     my $result = &start_responseparam(@_);
                    383:     return $result;
1.55      albertel  384: }
                    385: 
                    386: sub end_parameter {
1.73      albertel  387:     my $result = &end_responseparam(@_);
                    388:     return $result;
1.42      albertel  389: }
                    390: 
1.67      albertel  391: sub reset_params {
                    392:     %Apache::inputtags::params=();
                    393: }
                    394: 
1.42      albertel  395: sub setup_params {
1.73      albertel  396:     my ($tag) = @_;
1.42      albertel  397: 
1.73      albertel  398:     if ($ENV{'request.state'} eq 'construct') { return; }
                    399:     my %paramlist=();
                    400:     foreach my $key (keys(%Apache::lonnet::packagetab)) {
                    401: 	if ($key =~ /^$tag/) {
                    402: 	    my ($package,$name) = split(/&/,$key);
                    403: 	    $paramlist{$name}=1;
                    404: 	}
1.42      albertel  405:     }
1.73      albertel  406:     foreach my $key (keys(%paramlist)) {
                    407: 	my $entry= 'resource.'.$Apache::inputtags::part;
                    408: 	if (defined($Apache::inputtags::response[-1])) {
                    409: 	    $entry.='_'.$Apache::inputtags::response[-1];
                    410: 	}
                    411: 	$entry.='.'.$key;
                    412: 	&Apache::lonxml::debug("looking for $entry");
                    413: 	my $value = &Apache::lonnet::EXT("$entry");
                    414: 	&Apache::lonxml::debug("$key has value :$value:");
                    415: 	if ($value eq 'con_lost' || $value =~ /^error:/) {
                    416: 	    &Apache::lonxml::debug("using nothing");
                    417: 	    $Apache::inputtags::params{$key}='';
                    418: 	} else {
                    419: 	    &Apache::lonxml::debug("using value");
                    420: 	    $Apache::inputtags::params{$key}=$value;
                    421: 	}
1.42      albertel  422:     }
1.48      albertel  423: }
                    424: 
                    425: sub answer_header {
1.73      albertel  426:     my ($type) = @_;
                    427:     my $result;
1.77      albertel  428:     if ($ENV{'form.answer_output_mode'} eq 'tex') {
1.84      sakharuk  429: 	$result = ' \vskip 0 mm \begin{tabular}{|c|}\hline Answer for Part: \verb|'.
                    430:                   $Apache::inputtags::part.'| \\\\ \hline ';
1.73      albertel  431:     } else {
1.80      albertel  432: 	$result = '<table border="1"><tr><td>Answer for Part:'.
                    433: 	    $Apache::inputtags::part. '</td>'."\n";
1.73      albertel  434:     }
                    435:     return $result;
1.48      albertel  436: }
                    437: 
                    438: sub answer_part {
1.73      albertel  439:     my ($type,$answer) = @_;
                    440:     my $result;
1.77      albertel  441:     if ($ENV{'form.answer_output_mode'} eq 'tex') {
1.81      sakharuk  442: 	$result = ' \verb|'.$answer.'|\\\\ \hline ';
1.73      albertel  443:     } else {
1.80      albertel  444: 	$result = '<td>'.$answer.'</td>';
1.73      albertel  445:     }
                    446:     return $result;
1.48      albertel  447: }
                    448: 
                    449: sub answer_footer {
1.73      albertel  450:     my ($type) = @_;
                    451:     my $result;
1.77      albertel  452:     if ($ENV{'form.answer_output_mode'} eq 'tex') {
1.75      sakharuk  453: 	$result = ' \end{tabular} \vskip 0 mm ';
1.73      albertel  454:     } else {
1.80      albertel  455: 	$result = '</tr></table>';
1.73      albertel  456:     }
                    457:     return $result;
1.1       albertel  458: }
1.2       albertel  459: 
1.62      albertel  460: sub showallfoils {
1.73      albertel  461:     my $return=0;
                    462:     if (defined($ENV{'form.showallfoils'}) &&
                    463: 	$ENV{'request.state'} eq 'construct') {
                    464: 	$return=1;
                    465:     }
                    466:     return $return;
1.70      albertel  467: }
                    468: 
                    469: sub getresponse {
1.90      albertel  470:     my ($temp,$resulttype)=@_;
1.70      albertel  471:     my $formparm='form.HWVAL_'.$Apache::inputtags::response['-1'];
                    472:     my $response;
                    473:     if (!defined($temp)) {
                    474: 	$temp=1;
                    475:     } else {
                    476: 	$formparm.=":$temp";
                    477:     }
                    478:     my %let_to_num=('A'=>0,'B'=>1,'C'=>2,'D'=>3,'E'=>4,'F'=>5,'G'=>6,'H'=>7,
                    479: 		    'I'=>8,'J'=>9,'K'=>10,'L'=>11,'M'=>12,'N'=>13,'O'=>14,
                    480: 		    'P'=>15,'Q'=>16,'R'=>17,'S'=>18,'T'=>19,'U'=>20,'V'=>21,
                    481: 		    'W'=>22,'X'=>23,'Y'=>24,'Z'=>25);
                    482:     if ($ENV{'form.submitted'} eq 'scantron') {
1.71      albertel  483: 	my $part  = $Apache::inputtags::part;
                    484: 	my $id    = $Apache::inputtags::response[-1];
1.70      albertel  485: 	$response = $ENV{'scantron.'.($Apache::lonxml::counter+$temp-1).
                    486: 			 '.answer'};
1.71      albertel  487: 	# save bubbled letter for later
                    488: 	$Apache::lonhomework::results{"resource.$part.$id.scantron"}.=
                    489: 	    $response;
1.90      albertel  490: 	if ($resulttype ne 'letter') {
                    491: 	    $response = $let_to_num{$response};
                    492: 	}
1.70      albertel  493:     } else {
                    494: 	$response = $ENV{$formparm};
                    495:     }
                    496:     return $response;
1.62      albertel  497: }
1.71      albertel  498: 
                    499: sub repetition {
                    500:     my $id = $Apache::inputtags::part;
                    501:     my $weight = &Apache::lonnet::EXT("resource.$id.weight");
                    502:     my $repetition = int $weight/9;
                    503:     if ($weight % 9 != 0) {$repetition++;} 
1.72      albertel  504:     return $repetition;
                    505: }
                    506: 
                    507: sub scored_response {
                    508:     my ($part,$id)=@_;
                    509:     my $repetition=&repetition();
                    510:     my $score=0;
                    511:     for (my $i=0;$i<$repetition;$i++) {
                    512: 	my $increase=&Apache::response::getresponse($i+1);
                    513: 	if ($increase ne '') { $score+=$increase+1; }
                    514:     }
                    515:     my $weight = &Apache::lonnet::EXT("resource.$part.weight");
1.91      albertel  516:     if (!defined($weight) || $weight eq '' || $weight eq 0) { $weight = 1; }
1.72      albertel  517:     my $pcr=$score/$weight;
                    518:     $Apache::lonhomework::results{"resource.$part.$id.awarded"}=$pcr;
                    519:     $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
                    520: 	'ASSIGNED_SCORE';
1.71      albertel  521:     return $repetition;
1.78      albertel  522: }
                    523: 
                    524: sub whichorder {
                    525:     my ($max,$randomize,$showall,$hash)=@_;
                    526:     #&Apache::lonxml::debug("man $max randomize $randomize");
                    527:     if (!defined(@{ $$hash{'names'} })) { return; }
                    528:     my @names = @{ $$hash{'names'} };
                    529:     my @whichopt =();
                    530:     my (%top,@toplist,%bottom,@bottomlist);
                    531:     if (!($showall || ($randomize eq 'no'))) {
                    532: 	my $current=0;
                    533: 	foreach my $name (@names) {
                    534: 	    $current++;
                    535: 	    if ($$hash{"$name.location"} eq 'top') {
                    536: 		$top{$name}=$current;
                    537: 	    } elsif ($$hash{"$name.location"} eq 'bottom') {
                    538: 		$bottom{$name}=$current;
                    539: 	    }
                    540: 	}
                    541:     }
                    542:     my $topcount=0;
                    543:     my $bottomcount=0;
                    544:     while (((scalar(@whichopt)+$topcount+$bottomcount) < $max || $showall)
                    545: 	   && ($#names > -1)) {
                    546: 	#&Apache::lonxml::debug("Have $#whichopt max is $max");
                    547: 	my $aopt;
                    548: 	if ($showall || ($randomize eq 'no')) {
                    549: 	    $aopt=0;
                    550: 	} else {
                    551: 	    $aopt=int(&Math::Random::random_uniform() * ($#names+1));
                    552: 	}
                    553: 	#&Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
                    554: 	$aopt=splice(@names,$aopt,1);
                    555: 	#&Apache::lonxml::debug("Picked $aopt");
                    556: 	if ($top{$aopt}) {
                    557: 	    $toplist[$top{$aopt}]=$aopt;
                    558: 	    $topcount++;
                    559: 	} elsif ($bottom{$aopt}) {
                    560: 	    $bottomlist[$bottom{$aopt}]=$aopt;
                    561: 	    $bottomcount++;
                    562: 	} else {
                    563: 	    push (@whichopt,$aopt);
                    564: 	}
                    565:     }
                    566:     for (my $i=0;$i<=$#toplist;$i++) {
                    567: 	if ($toplist[$i]) { unshift(@whichopt,$toplist[$i]) }
                    568:     }
                    569:     for (my $i=0;$i<=$#bottomlist;$i++) {
                    570: 	if ($bottomlist[$i]) { push(@whichopt,$bottomlist[$i]) }
                    571:     }
                    572:     return @whichopt;
1.71      albertel  573: }
                    574: 
1.85      albertel  575: sub show_answer {
                    576:     my $part   = $Apache::inputtags::part;
                    577:     my $award  = $Apache::lonhomework::history{"resource.$part.solved"};
                    578:     my $status = $Apache::inputtags::status[-1];
                    579:     return  ( ($award =~ /^correct/
                    580: 	       && lc($Apache::lonhomework::problemstatus) ne 'no')
                    581: 	      || $status eq "SHOW_ANSWER");
                    582: }
1.87      albertel  583: 
                    584: sub analyze_store_foilgroup {
                    585:     my ($shown,$attrs)=@_;
                    586:     my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                    587:     foreach my $name (@{ $Apache::response::foilgroup{'names'} }) {
                    588: 	if (defined($Apache::lonhomework::analyze{"$part_id.foil.value.$name"})) { next; }
                    589: 	push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },$name);
                    590: 	foreach my $attr (@$attrs) {
                    591: 	    $Apache::lonhomework::analyze{"$part_id.foil.".$attr.".$name"} =
                    592: 		$Apache::response::foilgroup{"$name.".$attr};
                    593: 	}
                    594:     }
                    595:     push (@{ $Apache::lonhomework::analyze{"$part_id.shown"} }, @{ $shown });
                    596: }
                    597: 
                    598: sub pick_foil_for_concept {
                    599:     my ($target,$attrs,$hinthash,$parstack,$safeeval)=@_;
                    600:     if (not defined(@{ $Apache::response::conceptgroup{'names'} })) { return; }
                    601:     my @names = @{ $Apache::response::conceptgroup{'names'} };
                    602:     my $pick=int(&Math::Random::random_uniform() * ($#names+1));
                    603:     my $name=$names[$pick];
                    604:     push @{ $Apache::response::foilgroup{'names'} }, $name;
                    605:     foreach my $attr (@$attrs) {
                    606: 	$Apache::response::foilgroup{"$name.".$attr} =
                    607: 	    $Apache::response::conceptgroup{"$name.".$attr};
                    608:     }
                    609:     my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
                    610:     $Apache::response::foilgroup{"$name.concept"} = $concept;
                    611:     &Apache::lonxml::debug("Selecting $name in $concept");
                    612:     my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                    613:     if ($target eq 'analyze') {
                    614: 	push (@{ $Apache::lonhomework::analyze{"$part_id.concepts"} },
                    615: 	      $concept);
                    616: 	$Apache::lonhomework::analyze{"$part_id.concept.$concept"}=
                    617: 	    $Apache::response::conceptgroup{'names'};
                    618: 	foreach my $name (@{ $Apache::response::conceptgroup{'names'} }) {
                    619: 	    push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },
                    620: 		  $name);
                    621: 	    foreach my $attr (@$attrs) {
                    622: 		$Apache::lonhomework::analyze{"$part_id.foil.$attr.$name"}=
                    623: 		    $Apache::response::conceptgroup{"$name.$attr"};
                    624: 	    }
                    625: 	}
                    626:     }
                    627:     push(@{ $hinthash->{"$part_id.concepts"} },$concept);
                    628:     $hinthash->{"$part_id.concept.$concept"}=
                    629: 	$Apache::response::conceptgroup{'names'};
                    630: 
                    631: }
                    632: 
                    633: 
1.1       albertel  634: 1;
                    635: __END__
1.38      albertel  636:  

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