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

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

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