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

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

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