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

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

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