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

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

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