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

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

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