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

1.38      albertel    1: # The LearningOnline Network with CAPA
1.1       albertel    2: # various response type definitons response definition
1.53      albertel    3: #
1.124   ! albertel    4: # $Id: response.pm,v 1.123 2005/08/02 18:05:25 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; }
1.123     albertel   74:     push (@Apache::inputtags::hint,$id);
                     75:     push (@Apache::inputtags::hintlist,$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.123     albertel   81:     pop @Apache::inputtags::hint;
1.73      albertel   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.124   ! albertel  508: 	my $to_use='|';
        !           509: 	foreach my $value (32..126) {
        !           510: 	    my $char=pack('c',$value);
        !           511: 	    if ($answer !~ /\Q$char\E/) {
        !           512: 		$to_use=$char;
        !           513: 		last;
        !           514: 	    }
        !           515: 	}
        !           516: 	$result = '\verb'.$to_use.$answer.$to_use.'\\\\ \hline ';
1.73      albertel  517:     } else {
1.80      albertel  518: 	$result = '<td>'.$answer.'</td>';
1.73      albertel  519:     }
                    520:     return $result;
1.48      albertel  521: }
                    522: 
                    523: sub answer_footer {
1.73      albertel  524:     my ($type) = @_;
                    525:     my $result;
1.120     albertel  526:     if ($env{'form.answer_output_mode'} eq 'tex') {
1.75      sakharuk  527: 	$result = ' \end{tabular} \vskip 0 mm ';
1.73      albertel  528:     } else {
1.80      albertel  529: 	$result = '</tr></table>';
1.73      albertel  530:     }
                    531:     return $result;
1.1       albertel  532: }
1.2       albertel  533: 
1.62      albertel  534: sub showallfoils {
1.120     albertel  535:     if (defined($env{'form.showallfoils'})) {
1.102     albertel  536: 	my ($symb)=&Apache::lonxml::whichuser();
1.120     albertel  537: 	if (($env{'request.state'} eq 'construct') || 
                    538: 	    ($env{'user.adv'} && $symb eq '')      ||
1.118     foxr      539:             ($Apache::lonhomework::viewgrades) ) {
1.102     albertel  540: 	    return 1;
                    541: 	}
1.73      albertel  542:     }
1.108     albertel  543:     if ($Apache::lonhomework::type eq 'survey') { return 1; }
1.102     albertel  544:     return 0;
1.70      albertel  545: }
                    546: 
                    547: sub getresponse {
1.90      albertel  548:     my ($temp,$resulttype)=@_;
1.70      albertel  549:     my $formparm='form.HWVAL_'.$Apache::inputtags::response['-1'];
                    550:     my $response;
                    551:     if (!defined($temp)) {
                    552: 	$temp=1;
                    553:     } else {
                    554: 	$formparm.=":$temp";
                    555:     }
                    556:     my %let_to_num=('A'=>0,'B'=>1,'C'=>2,'D'=>3,'E'=>4,'F'=>5,'G'=>6,'H'=>7,
                    557: 		    'I'=>8,'J'=>9,'K'=>10,'L'=>11,'M'=>12,'N'=>13,'O'=>14,
                    558: 		    'P'=>15,'Q'=>16,'R'=>17,'S'=>18,'T'=>19,'U'=>20,'V'=>21,
                    559: 		    'W'=>22,'X'=>23,'Y'=>24,'Z'=>25);
1.120     albertel  560:     if ($env{'form.submitted'} eq 'scantron') {
1.71      albertel  561: 	my $part  = $Apache::inputtags::part;
                    562: 	my $id    = $Apache::inputtags::response[-1];
1.120     albertel  563: 	$response = $env{'scantron.'.($Apache::lonxml::counter+$temp-1).
1.70      albertel  564: 			 '.answer'};
1.71      albertel  565: 	# save bubbled letter for later
                    566: 	$Apache::lonhomework::results{"resource.$part.$id.scantron"}.=
                    567: 	    $response;
1.90      albertel  568: 	if ($resulttype ne 'letter') {
1.104     albertel  569: 	    if ($resulttype eq 'A is 1') {
1.105     albertel  570: 		$response = $let_to_num{$response}+1;
                    571: 	    } else {
1.104     albertel  572: 		$response = $let_to_num{$response};
                    573: 	    }
1.90      albertel  574: 	}
1.70      albertel  575:     } else {
1.120     albertel  576: 	$response = $env{$formparm};
1.70      albertel  577:     }
                    578:     return $response;
1.62      albertel  579: }
1.71      albertel  580: 
                    581: sub repetition {
                    582:     my $id = $Apache::inputtags::part;
                    583:     my $weight = &Apache::lonnet::EXT("resource.$id.weight");
1.121     albertel  584:     if (!defined($weight) || ($weight eq '')) { $weight=1; }
1.71      albertel  585:     my $repetition = int $weight/9;
                    586:     if ($weight % 9 != 0) {$repetition++;} 
1.72      albertel  587:     return $repetition;
                    588: }
                    589: 
                    590: sub scored_response {
                    591:     my ($part,$id)=@_;
                    592:     my $repetition=&repetition();
                    593:     my $score=0;
                    594:     for (my $i=0;$i<$repetition;$i++) {
                    595: 	my $increase=&Apache::response::getresponse($i+1);
                    596: 	if ($increase ne '') { $score+=$increase+1; }
                    597:     }
                    598:     my $weight = &Apache::lonnet::EXT("resource.$part.weight");
1.91      albertel  599:     if (!defined($weight) || $weight eq '' || $weight eq 0) { $weight = 1; }
1.72      albertel  600:     my $pcr=$score/$weight;
                    601:     $Apache::lonhomework::results{"resource.$part.$id.awarded"}=$pcr;
                    602:     $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
                    603: 	'ASSIGNED_SCORE';
1.71      albertel  604:     return $repetition;
1.78      albertel  605: }
                    606: 
                    607: sub whichorder {
                    608:     my ($max,$randomize,$showall,$hash)=@_;
                    609:     #&Apache::lonxml::debug("man $max randomize $randomize");
                    610:     if (!defined(@{ $$hash{'names'} })) { return; }
                    611:     my @names = @{ $$hash{'names'} };
                    612:     my @whichopt =();
                    613:     my (%top,@toplist,%bottom,@bottomlist);
                    614:     if (!($showall || ($randomize eq 'no'))) {
                    615: 	my $current=0;
                    616: 	foreach my $name (@names) {
                    617: 	    $current++;
                    618: 	    if ($$hash{"$name.location"} eq 'top') {
                    619: 		$top{$name}=$current;
                    620: 	    } elsif ($$hash{"$name.location"} eq 'bottom') {
                    621: 		$bottom{$name}=$current;
                    622: 	    }
                    623: 	}
                    624:     }
                    625:     my $topcount=0;
                    626:     my $bottomcount=0;
                    627:     while (((scalar(@whichopt)+$topcount+$bottomcount) < $max || $showall)
                    628: 	   && ($#names > -1)) {
                    629: 	#&Apache::lonxml::debug("Have $#whichopt max is $max");
                    630: 	my $aopt;
                    631: 	if ($showall || ($randomize eq 'no')) {
                    632: 	    $aopt=0;
                    633: 	} else {
                    634: 	    $aopt=int(&Math::Random::random_uniform() * ($#names+1));
                    635: 	}
                    636: 	#&Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
                    637: 	$aopt=splice(@names,$aopt,1);
                    638: 	#&Apache::lonxml::debug("Picked $aopt");
                    639: 	if ($top{$aopt}) {
                    640: 	    $toplist[$top{$aopt}]=$aopt;
                    641: 	    $topcount++;
                    642: 	} elsif ($bottom{$aopt}) {
                    643: 	    $bottomlist[$bottom{$aopt}]=$aopt;
                    644: 	    $bottomcount++;
                    645: 	} else {
                    646: 	    push (@whichopt,$aopt);
                    647: 	}
                    648:     }
                    649:     for (my $i=0;$i<=$#toplist;$i++) {
                    650: 	if ($toplist[$i]) { unshift(@whichopt,$toplist[$i]) }
                    651:     }
                    652:     for (my $i=0;$i<=$#bottomlist;$i++) {
                    653: 	if ($bottomlist[$i]) { push(@whichopt,$bottomlist[$i]) }
                    654:     }
                    655:     return @whichopt;
1.71      albertel  656: }
                    657: 
1.85      albertel  658: sub show_answer {
                    659:     my $part   = $Apache::inputtags::part;
                    660:     my $award  = $Apache::lonhomework::history{"resource.$part.solved"};
                    661:     my $status = $Apache::inputtags::status[-1];
                    662:     return  ( ($award =~ /^correct/
                    663: 	       && lc($Apache::lonhomework::problemstatus) ne 'no')
                    664: 	      || $status eq "SHOW_ANSWER");
                    665: }
1.87      albertel  666: 
                    667: sub analyze_store_foilgroup {
                    668:     my ($shown,$attrs)=@_;
                    669:     my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                    670:     foreach my $name (@{ $Apache::response::foilgroup{'names'} }) {
                    671: 	if (defined($Apache::lonhomework::analyze{"$part_id.foil.value.$name"})) { next; }
                    672: 	push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },$name);
                    673: 	foreach my $attr (@$attrs) {
                    674: 	    $Apache::lonhomework::analyze{"$part_id.foil.".$attr.".$name"} =
                    675: 		$Apache::response::foilgroup{"$name.".$attr};
                    676: 	}
                    677:     }
                    678:     push (@{ $Apache::lonhomework::analyze{"$part_id.shown"} }, @{ $shown });
1.96      albertel  679: }
                    680: 
                    681: sub check_if_computed {
                    682:     my ($token,$parstack,$safeeval,$name)=@_;
                    683:     my $value = &Apache::lonxml::get_param($name,$parstack,$safeeval);
1.106     matthew   684:     if (ref($token->[2]) eq 'HASH' && $value ne $token->[2]{$name}) {
1.96      albertel  685: 	my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                    686: 	$Apache::lonhomework::analyze{"$part_id.answercomputed"} = 1;
                    687:     }
1.87      albertel  688: }
                    689: 
                    690: sub pick_foil_for_concept {
                    691:     my ($target,$attrs,$hinthash,$parstack,$safeeval)=@_;
                    692:     if (not defined(@{ $Apache::response::conceptgroup{'names'} })) { return; }
                    693:     my @names = @{ $Apache::response::conceptgroup{'names'} };
                    694:     my $pick=int(&Math::Random::random_uniform() * ($#names+1));
                    695:     my $name=$names[$pick];
                    696:     push @{ $Apache::response::foilgroup{'names'} }, $name;
                    697:     foreach my $attr (@$attrs) {
                    698: 	$Apache::response::foilgroup{"$name.".$attr} =
                    699: 	    $Apache::response::conceptgroup{"$name.".$attr};
                    700:     }
                    701:     my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
                    702:     $Apache::response::foilgroup{"$name.concept"} = $concept;
                    703:     &Apache::lonxml::debug("Selecting $name in $concept");
                    704:     my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                    705:     if ($target eq 'analyze') {
                    706: 	push (@{ $Apache::lonhomework::analyze{"$part_id.concepts"} },
                    707: 	      $concept);
                    708: 	$Apache::lonhomework::analyze{"$part_id.concept.$concept"}=
                    709: 	    $Apache::response::conceptgroup{'names'};
                    710: 	foreach my $name (@{ $Apache::response::conceptgroup{'names'} }) {
                    711: 	    push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },
                    712: 		  $name);
                    713: 	    foreach my $attr (@$attrs) {
                    714: 		$Apache::lonhomework::analyze{"$part_id.foil.$attr.$name"}=
                    715: 		    $Apache::response::conceptgroup{"$name.$attr"};
                    716: 	    }
                    717: 	}
                    718:     }
                    719:     push(@{ $hinthash->{"$part_id.concepts"} },$concept);
                    720:     $hinthash->{"$part_id.concept.$concept"}=
                    721: 	$Apache::response::conceptgroup{'names'};
                    722: 
                    723: }
                    724: 
1.95      albertel  725: sub get_response_param {
                    726:     my ($id,$name,$default)=@_;
                    727:     my $parameter;
1.120     albertel  728:     if ($env{'request.state'} eq 'construct' &&
1.95      albertel  729: 	defined($Apache::inputtags::params{$name})) {
                    730: 	$parameter=$Apache::inputtags::params{$name};
                    731:     } else {
                    732: 	$parameter=&Apache::lonnet::EXT("resource.$id.$name");
                    733:     }
                    734:     if (!defined($parameter) ||	$parameter eq '') {
                    735: 	$parameter = $default;
                    736:     }
                    737:     return $parameter;
                    738: }
1.87      albertel  739: 
1.113     albertel  740: sub submitted {
                    741:     my ($who)=@_;
                    742:     
                    743:     # when scatron grading any submission is a submission
1.120     albertel  744:     if ($env{'form.submitted'} eq 'scantron') { return 1; }
1.113     albertel  745:     # if the caller only cared if this was a scantron submission
                    746:     if ($who eq 'scantron') { return 0; }
                    747:     # if the Submit Answer button for this particular part was pressed
                    748:     my $partid=$Apache::inputtags::part;
1.120     albertel  749:     if (defined($env{'form.submit_'.$partid})) { return 1; }
1.122     albertel  750:     # Submit All button on a .page was pressed
                    751:     if (defined($env{'form.all_submit'})) { return 1; }
1.113     albertel  752:     # otherwise no submission occured
                    753:     return 0;
                    754: }
1.117     albertel  755: 
                    756: # basically undef and 0 (both false) mean that they still have work to do
                    757: # and all true values mean that they can't do any more work
                    758: #
                    759: # a return of undef means it is unattempted
                    760: # a return of 0 means it is attmpted and wrong but still has tries
                    761: # a return of 1 means it is marked correct
                    762: # a return of 2 means they have exceed maximum number of tries
                    763: # a return of 3 means it after the answer date
                    764: sub check_status {
                    765:     my ($id)=@_;
                    766:     if (!$id) {	$id=$Apache::linputtags::part; }
                    767:     my $curtime=&Apache::lonnet::EXT('system.time');
                    768:     my $opendate=&Apache::lonnet::EXT("resource.$id.opendate");
                    769:     my $duedate=&Apache::lonnet::EXT("resource.$id.duedate");
                    770:     my $answerdate=&Apache::lonnet::EXT("resource.$id.answerdate");
                    771:     if ( $opendate && $curtime > $opendate &&
                    772:          $duedate && $curtime > $duedate &&
                    773:          $answerdate && $curtime > $answerdate) {
                    774:         return 3;
                    775:     }
                    776:     my $status=&Apache::lonnet::EXT("user.resource.resource.$id.solved");
                    777:     if ($status =~ /^correct/) { return 1; }
                    778:     if (!$status) { return undef; }
                    779:     my $maxtries=&Apache::lonnet::EXT("resource.$id.maxtries");
                    780:     if ($maxtries eq '') { $maxtries=2; }
                    781:     my $curtries=&Apache::lonnet::EXT("user.resource.resource.$id.tries");
                    782:     if ($curtries < $maxtries) { return 0; }
                    783:     return 2;
                    784: }
                    785: 
1.1       albertel  786: 1;
                    787: __END__
1.38      albertel  788:  

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