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

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

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