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

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

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