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

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

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