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

1.38      albertel    1: # The LearningOnline Network with CAPA
1.1       albertel    2: # various response type definitons response definition
1.53      albertel    3: #
1.104   ! albertel    4: # $Id: response.pm,v 1.103 2004/09/09 18:34:29 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');
                    271: 	} else {
                    272: 	    push(@Apache::inputtags::previous,'PREVIOUSLY_USED');
                    273: 	}
                    274: 	push(@Apache::inputtags::previous_version,$$previous{'version'});
1.54      albertel  275:     }
1.44      albertel  276: }
                    277: 
1.45      albertel  278: sub view_or_modify {
1.73      albertel  279:     my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
                    280:     my $myself=0;
                    281:     if ( ($name eq $ENV{'user.name'}) && ($domain eq $ENV{'user.domain'}) ) {
                    282: 	$myself=1;
                    283:     }
                    284:     my $vgr=&Apache::lonnet::allowed('vgr',$courseid);
                    285:     my $mgr=&Apache::lonnet::allowed('vgr',$courseid);
                    286:     if ($mgr) { return "M"; }
                    287:     if ($vgr) { return "V"; }
                    288:     if ($myself) { return "V"; }
                    289:     return '';
1.45      albertel  290: }
                    291: 
1.44      albertel  292: sub start_dataresponse {
1.73      albertel  293:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    294:     my $id = &Apache::response::start_response($parstack,$safeeval);
                    295:     my $result;
                    296:     if ($target eq 'web') {
                    297: 	$result = $token->[2]->{'display'}.':';
                    298:     } elsif ($target eq 'meta') {
                    299: 	$result = &Apache::response::meta_stores_write($token->[2]->{'name'},
                    300: 						       $token->[2]->{'type'},
                    301: 						       $token->[2]->{'display'});
                    302: 	$result .= &Apache::response::meta_package_write('dataresponse');
                    303:     }
                    304:     return $result;
1.44      albertel  305: }
                    306: 
                    307: sub end_dataresponse {
1.73      albertel  308:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    309:     my $result;
                    310:     if ( $target eq 'web' ) {
                    311:     } elsif ($target eq 'grade' ) {
                    312: 	if ( defined $ENV{'form.submitted'}) {
                    313: 	    my ($symb,$courseid,$domain,$name)=&Apache::lonxml::whichuser();
                    314: 	    my $allowed=&Apache::lonnet::allowed('mgr',$courseid);
                    315: 	    if ($allowed) {
1.94      albertel  316: 		&Apache::response::setup_params('dataresponse',$safeeval);
1.73      albertel  317: 		my $partid = $Apache::inputtags::part;
                    318: 		my $id = $Apache::inputtags::response['-1'];
                    319: 		my $response = $ENV{'form.HWVAL_'.$id};
                    320: 		my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    321: 		if ( $response =~ /[^\s]/) {
                    322: 		    $Apache::lonhomework::results{"resource.$partid.$id.$name"}=$response;
                    323: 		    $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
                    324: 		    $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}='SUBMITTED';
                    325: 		}
                    326: 	    } else {
                    327: 		$result='Not Permitted to change values.'
                    328: 	    }
1.45      albertel  329: 	}
1.73      albertel  330:     }
                    331:     &Apache::response::end_response;
                    332:     return $result;
1.3       albertel  333: }
                    334: 
1.83      albertel  335: sub decide_package {
                    336:     my ($tagstack)=@_;
                    337:     my $package;
                    338:     if ($$tagstack[-1] eq 'parameter') {
                    339: 	$package='part';
                    340:     } else {
                    341: 	my $i=-1;
                    342: 	while (defined($$tagstack[$i])) {
                    343: 	    if ($$tagstack[$i] =~ /(response|hint)$/) {
                    344: 		$package=$$tagstack[$i];
                    345: 		last;
                    346: 	    }
                    347: 	    $i--;
                    348: 	}
                    349:     }
                    350:     return $package;
                    351: }
                    352: 
1.3       albertel  353: sub start_responseparam {
1.73      albertel  354:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    355:     my $result='';
                    356:     if ($target eq 'meta') {
                    357: 	$result = &meta_parameter_write($token->[2]->{'name'},
                    358: 					$token->[2]->{'type'},
                    359: 					$token->[2]->{'default'},
                    360: 					$token->[2]->{'description'});
                    361:     } elsif ($target eq 'edit') {
                    362: 	$result.=&Apache::edit::tag_start($target,$token);
1.83      albertel  363: 	my $optionlist;
                    364: 	my $package=&decide_package($tagstack);
                    365: 	foreach my $key (sort(keys(%Apache::lonnet::packagetab))) {
                    366: 	    if ($key =~ /^\Q$package\E&(.*)&display$/) {
                    367: 		$optionlist.='<option value="'.$1.'">'.
                    368: 		    $Apache::lonnet::packagetab{$key}.'</option>';
                    369: 	    }
                    370: 	}
                    371: 	if (defined($optionlist)) {
                    372: 	    $result.='Use template: <select name="'.
                    373: 		&Apache::edit::html_element_name('parameter_package').'">'.
                    374: 		    '<option value=""></option>'.$optionlist.'</select><br />';
                    375: 	}
1.73      albertel  376: 	$result.=&Apache::edit::text_arg('Name:','name',$token).
                    377: 	    &Apache::edit::text_arg('Type:','type',$token).
                    378: 		&Apache::edit::text_arg('Description:','description',$token).
                    379: 		    &Apache::edit::text_arg('Default:','default',$token).
                    380: 			"</td></tr>";
                    381: 	$result.=&Apache::edit::end_table;
                    382:     } elsif ($target eq 'modified') {
1.83      albertel  383: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    384: 						     $safeeval,'name','type',
                    385: 						     'description','default');
                    386: 	my $element=&Apache::edit::html_element_name('parameter_package');
                    387: 	if (defined($ENV{"form.$element"}) && $ENV{"form.$element"} ne '') {
                    388: 	    my $name=$ENV{"form.$element"};
                    389: 	    my $tag=&decide_package($tagstack);
                    390: 	    $token->[2]->{'name'}=$name;
                    391: 	    $token->[2]->{'type'}=
                    392: 		$Apache::lonnet::packagetab{"$tag&$name&type"};
                    393: 	    $token->[2]->{'description'}=
                    394: 		$Apache::lonnet::packagetab{"$tag&$name&display"};
                    395: 	    $token->[2]->{'default'}=
                    396: 		$Apache::lonnet::packagetab{"$tag&$name&default"};
                    397: 	    $constructtag=1;
                    398: 	}
1.73      albertel  399: 	if ($constructtag) {
                    400: 	    $result = &Apache::edit::rebuild_tag($token);
                    401: 	    $result.=&Apache::edit::handle_insert();
                    402: 	}
                    403:     } elsif ($target eq 'grade' || $target eq 'answer' || $target eq 'web' ||
                    404: 	     $target eq 'tex' || $target eq 'analyze' ) {
                    405: 	if ($ENV{'request.state'} eq 'construct') {
                    406: 	    my $name   =&Apache::lonxml::get_param('name',$parstack,$safeeval);
                    407: 	    my $default=&Apache::lonxml::get_param('default',$parstack,
                    408: 						     $safeeval);
                    409: 	    if ($name) {$Apache::inputtags::params{$name}=$default;}
                    410: 	}
1.52      albertel  411:     }
1.73      albertel  412:     return $result;
1.3       albertel  413: }
                    414: 
                    415: sub end_responseparam {
1.73      albertel  416:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    417:     if ($target eq 'edit') { return ('','no'); }
                    418:     return '';
1.55      albertel  419: }
                    420: 
                    421: sub start_parameter {
1.73      albertel  422:     my $result = &start_responseparam(@_);
                    423:     return $result;
1.55      albertel  424: }
                    425: 
                    426: sub end_parameter {
1.73      albertel  427:     my $result = &end_responseparam(@_);
                    428:     return $result;
1.42      albertel  429: }
                    430: 
1.67      albertel  431: sub reset_params {
                    432:     %Apache::inputtags::params=();
                    433: }
                    434: 
1.42      albertel  435: sub setup_params {
1.94      albertel  436:     my ($tag,$safeeval) = @_;
1.42      albertel  437: 
1.73      albertel  438:     if ($ENV{'request.state'} eq 'construct') { return; }
                    439:     my %paramlist=();
                    440:     foreach my $key (keys(%Apache::lonnet::packagetab)) {
                    441: 	if ($key =~ /^$tag/) {
                    442: 	    my ($package,$name) = split(/&/,$key);
                    443: 	    $paramlist{$name}=1;
                    444: 	}
1.42      albertel  445:     }
1.73      albertel  446:     foreach my $key (keys(%paramlist)) {
                    447: 	my $entry= 'resource.'.$Apache::inputtags::part;
                    448: 	if (defined($Apache::inputtags::response[-1])) {
                    449: 	    $entry.='_'.$Apache::inputtags::response[-1];
                    450: 	}
                    451: 	$entry.='.'.$key;
                    452: 	&Apache::lonxml::debug("looking for $entry");
                    453: 	my $value = &Apache::lonnet::EXT("$entry");
                    454: 	&Apache::lonxml::debug("$key has value :$value:");
                    455: 	if ($value eq 'con_lost' || $value =~ /^error:/) {
                    456: 	    &Apache::lonxml::debug("using nothing");
                    457: 	    $Apache::inputtags::params{$key}='';
                    458: 	} else {
1.94      albertel  459: 	    my $string="{return qq\0".$value."\0}";
                    460: 	    my $newvalue=&Apache::run::run($string,$safeeval,1);
                    461: 	    if (defined($newvalue)) { $value=$newvalue; }
1.73      albertel  462: 	    $Apache::inputtags::params{$key}=$value;
                    463: 	}
1.42      albertel  464:     }
1.48      albertel  465: }
                    466: 
                    467: sub answer_header {
1.73      albertel  468:     my ($type) = @_;
                    469:     my $result;
1.77      albertel  470:     if ($ENV{'form.answer_output_mode'} eq 'tex') {
1.84      sakharuk  471: 	$result = ' \vskip 0 mm \begin{tabular}{|c|}\hline Answer for Part: \verb|'.
                    472:                   $Apache::inputtags::part.'| \\\\ \hline ';
1.73      albertel  473:     } else {
1.80      albertel  474: 	$result = '<table border="1"><tr><td>Answer for Part:'.
                    475: 	    $Apache::inputtags::part. '</td>'."\n";
1.73      albertel  476:     }
                    477:     return $result;
1.48      albertel  478: }
                    479: 
                    480: sub answer_part {
1.73      albertel  481:     my ($type,$answer) = @_;
                    482:     my $result;
1.77      albertel  483:     if ($ENV{'form.answer_output_mode'} eq 'tex') {
1.81      sakharuk  484: 	$result = ' \verb|'.$answer.'|\\\\ \hline ';
1.73      albertel  485:     } else {
1.80      albertel  486: 	$result = '<td>'.$answer.'</td>';
1.73      albertel  487:     }
                    488:     return $result;
1.48      albertel  489: }
                    490: 
                    491: sub answer_footer {
1.73      albertel  492:     my ($type) = @_;
                    493:     my $result;
1.77      albertel  494:     if ($ENV{'form.answer_output_mode'} eq 'tex') {
1.75      sakharuk  495: 	$result = ' \end{tabular} \vskip 0 mm ';
1.73      albertel  496:     } else {
1.80      albertel  497: 	$result = '</tr></table>';
1.73      albertel  498:     }
                    499:     return $result;
1.1       albertel  500: }
1.2       albertel  501: 
1.62      albertel  502: sub showallfoils {
1.102     albertel  503:     if (defined($ENV{'form.showallfoils'})) {
                    504: 	my ($symb)=&Apache::lonxml::whichuser();
                    505: 	if ($ENV{'request.state'} eq 'construct' || 
                    506: 	    ($ENV{'user.adv'} && $symb eq '')) {
                    507: 	    return 1;
                    508: 	}
1.73      albertel  509:     }
1.102     albertel  510:     return 0;
1.70      albertel  511: }
                    512: 
                    513: sub getresponse {
1.90      albertel  514:     my ($temp,$resulttype)=@_;
1.70      albertel  515:     my $formparm='form.HWVAL_'.$Apache::inputtags::response['-1'];
                    516:     my $response;
                    517:     if (!defined($temp)) {
                    518: 	$temp=1;
                    519:     } else {
                    520: 	$formparm.=":$temp";
                    521:     }
                    522:     my %let_to_num=('A'=>0,'B'=>1,'C'=>2,'D'=>3,'E'=>4,'F'=>5,'G'=>6,'H'=>7,
                    523: 		    'I'=>8,'J'=>9,'K'=>10,'L'=>11,'M'=>12,'N'=>13,'O'=>14,
                    524: 		    'P'=>15,'Q'=>16,'R'=>17,'S'=>18,'T'=>19,'U'=>20,'V'=>21,
                    525: 		    'W'=>22,'X'=>23,'Y'=>24,'Z'=>25);
                    526:     if ($ENV{'form.submitted'} eq 'scantron') {
1.71      albertel  527: 	my $part  = $Apache::inputtags::part;
                    528: 	my $id    = $Apache::inputtags::response[-1];
1.70      albertel  529: 	$response = $ENV{'scantron.'.($Apache::lonxml::counter+$temp-1).
                    530: 			 '.answer'};
1.71      albertel  531: 	# save bubbled letter for later
                    532: 	$Apache::lonhomework::results{"resource.$part.$id.scantron"}.=
                    533: 	    $response;
1.90      albertel  534: 	if ($resulttype ne 'letter') {
1.104   ! albertel  535: 	    if ($resulttype eq 'A is 1') {
        !           536: 		$response = $let_to_num{$response};
        !           537: 	    } else {
        !           538: 		$response = $let_to_num{$response}+1;
        !           539: 	    }
1.90      albertel  540: 	}
1.70      albertel  541:     } else {
                    542: 	$response = $ENV{$formparm};
                    543:     }
                    544:     return $response;
1.62      albertel  545: }
1.71      albertel  546: 
                    547: sub repetition {
                    548:     my $id = $Apache::inputtags::part;
                    549:     my $weight = &Apache::lonnet::EXT("resource.$id.weight");
                    550:     my $repetition = int $weight/9;
                    551:     if ($weight % 9 != 0) {$repetition++;} 
1.72      albertel  552:     return $repetition;
                    553: }
                    554: 
                    555: sub scored_response {
                    556:     my ($part,$id)=@_;
                    557:     my $repetition=&repetition();
                    558:     my $score=0;
                    559:     for (my $i=0;$i<$repetition;$i++) {
                    560: 	my $increase=&Apache::response::getresponse($i+1);
                    561: 	if ($increase ne '') { $score+=$increase+1; }
                    562:     }
                    563:     my $weight = &Apache::lonnet::EXT("resource.$part.weight");
1.91      albertel  564:     if (!defined($weight) || $weight eq '' || $weight eq 0) { $weight = 1; }
1.72      albertel  565:     my $pcr=$score/$weight;
                    566:     $Apache::lonhomework::results{"resource.$part.$id.awarded"}=$pcr;
                    567:     $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
                    568: 	'ASSIGNED_SCORE';
1.71      albertel  569:     return $repetition;
1.78      albertel  570: }
                    571: 
                    572: sub whichorder {
                    573:     my ($max,$randomize,$showall,$hash)=@_;
                    574:     #&Apache::lonxml::debug("man $max randomize $randomize");
                    575:     if (!defined(@{ $$hash{'names'} })) { return; }
                    576:     my @names = @{ $$hash{'names'} };
                    577:     my @whichopt =();
                    578:     my (%top,@toplist,%bottom,@bottomlist);
                    579:     if (!($showall || ($randomize eq 'no'))) {
                    580: 	my $current=0;
                    581: 	foreach my $name (@names) {
                    582: 	    $current++;
                    583: 	    if ($$hash{"$name.location"} eq 'top') {
                    584: 		$top{$name}=$current;
                    585: 	    } elsif ($$hash{"$name.location"} eq 'bottom') {
                    586: 		$bottom{$name}=$current;
                    587: 	    }
                    588: 	}
                    589:     }
                    590:     my $topcount=0;
                    591:     my $bottomcount=0;
                    592:     while (((scalar(@whichopt)+$topcount+$bottomcount) < $max || $showall)
                    593: 	   && ($#names > -1)) {
                    594: 	#&Apache::lonxml::debug("Have $#whichopt max is $max");
                    595: 	my $aopt;
                    596: 	if ($showall || ($randomize eq 'no')) {
                    597: 	    $aopt=0;
                    598: 	} else {
                    599: 	    $aopt=int(&Math::Random::random_uniform() * ($#names+1));
                    600: 	}
                    601: 	#&Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
                    602: 	$aopt=splice(@names,$aopt,1);
                    603: 	#&Apache::lonxml::debug("Picked $aopt");
                    604: 	if ($top{$aopt}) {
                    605: 	    $toplist[$top{$aopt}]=$aopt;
                    606: 	    $topcount++;
                    607: 	} elsif ($bottom{$aopt}) {
                    608: 	    $bottomlist[$bottom{$aopt}]=$aopt;
                    609: 	    $bottomcount++;
                    610: 	} else {
                    611: 	    push (@whichopt,$aopt);
                    612: 	}
                    613:     }
                    614:     for (my $i=0;$i<=$#toplist;$i++) {
                    615: 	if ($toplist[$i]) { unshift(@whichopt,$toplist[$i]) }
                    616:     }
                    617:     for (my $i=0;$i<=$#bottomlist;$i++) {
                    618: 	if ($bottomlist[$i]) { push(@whichopt,$bottomlist[$i]) }
                    619:     }
                    620:     return @whichopt;
1.71      albertel  621: }
                    622: 
1.85      albertel  623: sub show_answer {
                    624:     my $part   = $Apache::inputtags::part;
                    625:     my $award  = $Apache::lonhomework::history{"resource.$part.solved"};
                    626:     my $status = $Apache::inputtags::status[-1];
                    627:     return  ( ($award =~ /^correct/
                    628: 	       && lc($Apache::lonhomework::problemstatus) ne 'no')
                    629: 	      || $status eq "SHOW_ANSWER");
                    630: }
1.87      albertel  631: 
                    632: sub analyze_store_foilgroup {
                    633:     my ($shown,$attrs)=@_;
                    634:     my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                    635:     foreach my $name (@{ $Apache::response::foilgroup{'names'} }) {
                    636: 	if (defined($Apache::lonhomework::analyze{"$part_id.foil.value.$name"})) { next; }
                    637: 	push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },$name);
                    638: 	foreach my $attr (@$attrs) {
                    639: 	    $Apache::lonhomework::analyze{"$part_id.foil.".$attr.".$name"} =
                    640: 		$Apache::response::foilgroup{"$name.".$attr};
                    641: 	}
                    642:     }
                    643:     push (@{ $Apache::lonhomework::analyze{"$part_id.shown"} }, @{ $shown });
1.96      albertel  644: }
                    645: 
                    646: sub check_if_computed {
                    647:     my ($token,$parstack,$safeeval,$name)=@_;
                    648:     my $value = &Apache::lonxml::get_param($name,$parstack,$safeeval);
                    649:     if ($value ne $token->[2]{$name}) {
                    650: 	my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                    651: 	$Apache::lonhomework::analyze{"$part_id.answercomputed"} = 1;
                    652:     }
1.87      albertel  653: }
                    654: 
                    655: sub pick_foil_for_concept {
                    656:     my ($target,$attrs,$hinthash,$parstack,$safeeval)=@_;
                    657:     if (not defined(@{ $Apache::response::conceptgroup{'names'} })) { return; }
                    658:     my @names = @{ $Apache::response::conceptgroup{'names'} };
                    659:     my $pick=int(&Math::Random::random_uniform() * ($#names+1));
                    660:     my $name=$names[$pick];
                    661:     push @{ $Apache::response::foilgroup{'names'} }, $name;
                    662:     foreach my $attr (@$attrs) {
                    663: 	$Apache::response::foilgroup{"$name.".$attr} =
                    664: 	    $Apache::response::conceptgroup{"$name.".$attr};
                    665:     }
                    666:     my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
                    667:     $Apache::response::foilgroup{"$name.concept"} = $concept;
                    668:     &Apache::lonxml::debug("Selecting $name in $concept");
                    669:     my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                    670:     if ($target eq 'analyze') {
                    671: 	push (@{ $Apache::lonhomework::analyze{"$part_id.concepts"} },
                    672: 	      $concept);
                    673: 	$Apache::lonhomework::analyze{"$part_id.concept.$concept"}=
                    674: 	    $Apache::response::conceptgroup{'names'};
                    675: 	foreach my $name (@{ $Apache::response::conceptgroup{'names'} }) {
                    676: 	    push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },
                    677: 		  $name);
                    678: 	    foreach my $attr (@$attrs) {
                    679: 		$Apache::lonhomework::analyze{"$part_id.foil.$attr.$name"}=
                    680: 		    $Apache::response::conceptgroup{"$name.$attr"};
                    681: 	    }
                    682: 	}
                    683:     }
                    684:     push(@{ $hinthash->{"$part_id.concepts"} },$concept);
                    685:     $hinthash->{"$part_id.concept.$concept"}=
                    686: 	$Apache::response::conceptgroup{'names'};
                    687: 
                    688: }
                    689: 
1.95      albertel  690: sub get_response_param {
                    691:     my ($id,$name,$default)=@_;
                    692:     my $parameter;
                    693:     if ($ENV{'request.state'} eq 'construct' &&
                    694: 	defined($Apache::inputtags::params{$name})) {
                    695: 	$parameter=$Apache::inputtags::params{$name};
                    696:     } else {
                    697: 	$parameter=&Apache::lonnet::EXT("resource.$id.$name");
                    698:     }
                    699:     if (!defined($parameter) ||	$parameter eq '') {
                    700: 	$parameter = $default;
                    701:     }
                    702:     return $parameter;
                    703: }
1.87      albertel  704: 
1.1       albertel  705: 1;
                    706: __END__
1.38      albertel  707:  

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