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

1.38      albertel    1: # The LearningOnline Network with CAPA
1.1       albertel    2: # various response type definitons response definition
1.53      albertel    3: #
1.102   ! albertel    4: # $Id: response.pm,v 1.101 2004/08/29 07:49:35 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') {
                    535: 	    $response = $let_to_num{$response};
                    536: 	}
1.70      albertel  537:     } else {
                    538: 	$response = $ENV{$formparm};
                    539:     }
                    540:     return $response;
1.62      albertel  541: }
1.71      albertel  542: 
                    543: sub repetition {
                    544:     my $id = $Apache::inputtags::part;
                    545:     my $weight = &Apache::lonnet::EXT("resource.$id.weight");
                    546:     my $repetition = int $weight/9;
                    547:     if ($weight % 9 != 0) {$repetition++;} 
1.72      albertel  548:     return $repetition;
                    549: }
                    550: 
                    551: sub scored_response {
                    552:     my ($part,$id)=@_;
                    553:     my $repetition=&repetition();
                    554:     my $score=0;
                    555:     for (my $i=0;$i<$repetition;$i++) {
                    556: 	my $increase=&Apache::response::getresponse($i+1);
                    557: 	if ($increase ne '') { $score+=$increase+1; }
                    558:     }
                    559:     my $weight = &Apache::lonnet::EXT("resource.$part.weight");
1.91      albertel  560:     if (!defined($weight) || $weight eq '' || $weight eq 0) { $weight = 1; }
1.72      albertel  561:     my $pcr=$score/$weight;
                    562:     $Apache::lonhomework::results{"resource.$part.$id.awarded"}=$pcr;
                    563:     $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
                    564: 	'ASSIGNED_SCORE';
1.71      albertel  565:     return $repetition;
1.78      albertel  566: }
                    567: 
                    568: sub whichorder {
                    569:     my ($max,$randomize,$showall,$hash)=@_;
                    570:     #&Apache::lonxml::debug("man $max randomize $randomize");
                    571:     if (!defined(@{ $$hash{'names'} })) { return; }
                    572:     my @names = @{ $$hash{'names'} };
                    573:     my @whichopt =();
                    574:     my (%top,@toplist,%bottom,@bottomlist);
                    575:     if (!($showall || ($randomize eq 'no'))) {
                    576: 	my $current=0;
                    577: 	foreach my $name (@names) {
                    578: 	    $current++;
                    579: 	    if ($$hash{"$name.location"} eq 'top') {
                    580: 		$top{$name}=$current;
                    581: 	    } elsif ($$hash{"$name.location"} eq 'bottom') {
                    582: 		$bottom{$name}=$current;
                    583: 	    }
                    584: 	}
                    585:     }
                    586:     my $topcount=0;
                    587:     my $bottomcount=0;
                    588:     while (((scalar(@whichopt)+$topcount+$bottomcount) < $max || $showall)
                    589: 	   && ($#names > -1)) {
                    590: 	#&Apache::lonxml::debug("Have $#whichopt max is $max");
                    591: 	my $aopt;
                    592: 	if ($showall || ($randomize eq 'no')) {
                    593: 	    $aopt=0;
                    594: 	} else {
                    595: 	    $aopt=int(&Math::Random::random_uniform() * ($#names+1));
                    596: 	}
                    597: 	#&Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
                    598: 	$aopt=splice(@names,$aopt,1);
                    599: 	#&Apache::lonxml::debug("Picked $aopt");
                    600: 	if ($top{$aopt}) {
                    601: 	    $toplist[$top{$aopt}]=$aopt;
                    602: 	    $topcount++;
                    603: 	} elsif ($bottom{$aopt}) {
                    604: 	    $bottomlist[$bottom{$aopt}]=$aopt;
                    605: 	    $bottomcount++;
                    606: 	} else {
                    607: 	    push (@whichopt,$aopt);
                    608: 	}
                    609:     }
                    610:     for (my $i=0;$i<=$#toplist;$i++) {
                    611: 	if ($toplist[$i]) { unshift(@whichopt,$toplist[$i]) }
                    612:     }
                    613:     for (my $i=0;$i<=$#bottomlist;$i++) {
                    614: 	if ($bottomlist[$i]) { push(@whichopt,$bottomlist[$i]) }
                    615:     }
                    616:     return @whichopt;
1.71      albertel  617: }
                    618: 
1.85      albertel  619: sub show_answer {
                    620:     my $part   = $Apache::inputtags::part;
                    621:     my $award  = $Apache::lonhomework::history{"resource.$part.solved"};
                    622:     my $status = $Apache::inputtags::status[-1];
                    623:     return  ( ($award =~ /^correct/
                    624: 	       && lc($Apache::lonhomework::problemstatus) ne 'no')
                    625: 	      || $status eq "SHOW_ANSWER");
                    626: }
1.87      albertel  627: 
                    628: sub analyze_store_foilgroup {
                    629:     my ($shown,$attrs)=@_;
                    630:     my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                    631:     foreach my $name (@{ $Apache::response::foilgroup{'names'} }) {
                    632: 	if (defined($Apache::lonhomework::analyze{"$part_id.foil.value.$name"})) { next; }
                    633: 	push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },$name);
                    634: 	foreach my $attr (@$attrs) {
                    635: 	    $Apache::lonhomework::analyze{"$part_id.foil.".$attr.".$name"} =
                    636: 		$Apache::response::foilgroup{"$name.".$attr};
                    637: 	}
                    638:     }
                    639:     push (@{ $Apache::lonhomework::analyze{"$part_id.shown"} }, @{ $shown });
1.96      albertel  640: }
                    641: 
                    642: sub check_if_computed {
                    643:     my ($token,$parstack,$safeeval,$name)=@_;
                    644:     my $value = &Apache::lonxml::get_param($name,$parstack,$safeeval);
                    645:     if ($value ne $token->[2]{$name}) {
                    646: 	my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                    647: 	$Apache::lonhomework::analyze{"$part_id.answercomputed"} = 1;
                    648:     }
1.87      albertel  649: }
                    650: 
                    651: sub pick_foil_for_concept {
                    652:     my ($target,$attrs,$hinthash,$parstack,$safeeval)=@_;
                    653:     if (not defined(@{ $Apache::response::conceptgroup{'names'} })) { return; }
                    654:     my @names = @{ $Apache::response::conceptgroup{'names'} };
                    655:     my $pick=int(&Math::Random::random_uniform() * ($#names+1));
                    656:     my $name=$names[$pick];
                    657:     push @{ $Apache::response::foilgroup{'names'} }, $name;
                    658:     foreach my $attr (@$attrs) {
                    659: 	$Apache::response::foilgroup{"$name.".$attr} =
                    660: 	    $Apache::response::conceptgroup{"$name.".$attr};
                    661:     }
                    662:     my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
                    663:     $Apache::response::foilgroup{"$name.concept"} = $concept;
                    664:     &Apache::lonxml::debug("Selecting $name in $concept");
                    665:     my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                    666:     if ($target eq 'analyze') {
                    667: 	push (@{ $Apache::lonhomework::analyze{"$part_id.concepts"} },
                    668: 	      $concept);
                    669: 	$Apache::lonhomework::analyze{"$part_id.concept.$concept"}=
                    670: 	    $Apache::response::conceptgroup{'names'};
                    671: 	foreach my $name (@{ $Apache::response::conceptgroup{'names'} }) {
                    672: 	    push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },
                    673: 		  $name);
                    674: 	    foreach my $attr (@$attrs) {
                    675: 		$Apache::lonhomework::analyze{"$part_id.foil.$attr.$name"}=
                    676: 		    $Apache::response::conceptgroup{"$name.$attr"};
                    677: 	    }
                    678: 	}
                    679:     }
                    680:     push(@{ $hinthash->{"$part_id.concepts"} },$concept);
                    681:     $hinthash->{"$part_id.concept.$concept"}=
                    682: 	$Apache::response::conceptgroup{'names'};
                    683: 
                    684: }
                    685: 
1.95      albertel  686: sub get_response_param {
                    687:     my ($id,$name,$default)=@_;
                    688:     my $parameter;
                    689:     if ($ENV{'request.state'} eq 'construct' &&
                    690: 	defined($Apache::inputtags::params{$name})) {
                    691: 	$parameter=$Apache::inputtags::params{$name};
                    692:     } else {
                    693: 	$parameter=&Apache::lonnet::EXT("resource.$id.$name");
                    694:     }
                    695:     if (!defined($parameter) ||	$parameter eq '') {
                    696: 	$parameter = $default;
                    697:     }
                    698:     return $parameter;
                    699: }
1.87      albertel  700: 
1.1       albertel  701: 1;
                    702: __END__
1.38      albertel  703:  

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