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

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

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