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

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

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