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

1.38      albertel    1: # The LearningOnline Network with CAPA
1.1       albertel    2: # various response type definitons response definition
1.53      albertel    3: #
1.219   ! raeburn     4: # $Id: response.pm,v 1.218 2010/06/20 12:42:44 raeburn 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.208     jms        29: =pod
                     30: 
                     31: =head1 NAME
                     32: 
1.218     raeburn    33: Apache::response.pm
1.208     jms        34: 
                     35: =head1 SYNOPSIS
                     36: 
                     37: This is part of the LearningOnline Network with CAPA project
                     38: described at http://www.lon-capa.org.
                     39: 
                     40: 
                     41: =head1 NOTABLE SUBROUTINES
                     42: 
                     43: =over
                     44: 
                     45: =item 
                     46: 
                     47: =back
                     48: 
                     49: =cut
                     50: 
                     51: 
1.1       albertel   52: package Apache::response;
                     53: use strict;
1.93      albertel   54: use Apache::lonlocal;
1.120     albertel   55: use Apache::lonnet;
1.153     www        56: use Apache::lonmaxima();
1.214     www        57: use Apache::lonr();
1.1       albertel   58: 
1.57      harris41   59: BEGIN {
1.139     www        60:     &Apache::lonxml::register('Apache::response',('responseparam','parameter','dataresponse','customresponse','mathresponse'));
1.1       albertel   61: }
                     62: 
1.13      albertel   63: sub start_response {
1.73      albertel   64:     my ($parstack,$safeeval)=@_;
1.136     albertel   65:     my $id = &Apache::lonxml::get_id($parstack,$safeeval);
1.73      albertel   66:     if ($#Apache::inputtags::import > -1) {
                     67: 	&Apache::lonxml::debug("Turning :$id: into");
                     68: 	$id = join('_',@Apache::inputtags::import).'_'.$id;
                     69: 	&Apache::lonxml::debug("New  :$id:");
                     70:     }
                     71:     push (@Apache::inputtags::response,$id);
                     72:     push (@Apache::inputtags::responselist,$id);
                     73:     @Apache::inputtags::inputlist=();
1.101     albertel   74:     if ($Apache::inputtags::part eq '' && 
                     75: 	!$Apache::lonhomework::ignore_response_errors) {
1.97      albertel   76: 	&Apache::lonxml::error(&HTML::Entities::encode(&mt("Found a <*response> outside of a <part> in a <part>ed problem"),'<>&"'));
1.92      albertel   77:     }
                     78:     if ($Apache::inputtags::response_with_no_part &&
                     79: 	$Apache::inputtags::part ne '0') {
1.97      albertel   80: 	&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   81:     }
                     82:     if ($Apache::inputtags::part eq '0') {
                     83: 	$Apache::inputtags::response_with_no_part=1;
                     84:     }
1.73      albertel   85:     return $id;
1.13      albertel   86: }
                     87: 
                     88: sub end_response {
1.79      albertel   89:     #pop @Apache::inputtags::response;
1.73      albertel   90:     @Apache::inputtags::inputlist=();
                     91:     return '';
1.13      albertel   92: }
                     93: 
1.41      albertel   94: sub start_hintresponse {
1.73      albertel   95:     my ($parstack,$safeeval)=@_;
1.136     albertel   96:     my $id = &Apache::lonxml::get_id($parstack,$safeeval);
1.123     albertel   97:     push (@Apache::inputtags::hint,$id);
                     98:     push (@Apache::inputtags::hintlist,$id);
1.73      albertel   99:     push (@Apache::inputtags::paramstack,[%Apache::inputtags::params]);
                    100:     return $id;
1.41      albertel  101: }
                    102: 
                    103: sub end_hintresponse {
1.123     albertel  104:     pop @Apache::inputtags::hint;
1.73      albertel  105:     if (defined($Apache::inputtags::paramstack[-1])) {
                    106: 	%Apache::inputtags::params=
                    107: 	    @{ pop(@Apache::inputtags::paramstack) };
                    108:     }
                    109:     return '';
1.41      albertel  110: }
                    111: 
1.99      albertel  112: my @randomseeds;
                    113: sub pushrandomnumber {
                    114:     my $rand_alg=&Apache::lonnet::get_rand_alg();
                    115:     if (!$rand_alg || $rand_alg eq '32bit' || $rand_alg eq '64bit' ||
                    116: 	$rand_alg eq '64bit2') {
                    117: 	# do nothing
                    118:     } else {
                    119: 	my @seed=&Math::Random::random_get_seed();
1.127     albertel  120: 	push(@randomseeds,\@seed);
1.99      albertel  121:     }
1.127     albertel  122:     &Apache::response::setrandomnumber(@_);
1.99      albertel  123: }
                    124: sub poprandomnumber {
                    125:     my $rand_alg=&Apache::lonnet::get_rand_alg();
                    126:     if (!$rand_alg || $rand_alg eq '32bit' || $rand_alg eq '64bit' ||
                    127: 	$rand_alg eq '64bit2') {
                    128: 	return;
                    129:     }
                    130:     my $seed=pop(@randomseeds);
                    131:     if ($seed) {
                    132: 	&Math::Random::random_set_seed(@$seed);
                    133:     } else {
                    134: 	&Apache::lonxml::error("Unable to restore random algorithm.");
                    135:     }
                    136: }
1.117     albertel  137: 
1.26      albertel  138: sub setrandomnumber {
1.127     albertel  139:     my ($ignore_id2) = @_;
1.73      albertel  140:     my $rndseed;
1.88      albertel  141:     $rndseed=&Apache::structuretags::setup_rndseed();
                    142:     if (!defined($rndseed)) { $rndseed=&Apache::lonnet::rndseed(); }
1.73      albertel  143:     &Apache::lonxml::debug("randseed $rndseed");
                    144:     #  $rndseed=unpack("%32i",$rndseed);
1.99      albertel  145:     my $rand_alg=&Apache::lonnet::get_rand_alg();
1.126     albertel  146:     my ($rndmod,$rndmod2);
1.119     albertel  147: 
                    148:     my ($id1,$id2,$shift_amt);
                    149:     if ($Apache::lonhomework::parsing_a_problem) {
                    150: 	$id1=$Apache::inputtags::part;
                    151: 	if (defined($Apache::inputtags::response[-1])) {
                    152: 	    $id2=$Apache::inputtags::response[-1];
                    153: 	}
                    154: 	$shift_amt=scalar(@Apache::inputtags::responselist);
                    155:     } elsif ($Apache::lonhomework::parsing_a_task) {
1.141     albertel  156: 	$id1=&Apache::bridgetask::get_dim_id();
                    157: 	if (!$ignore_id2 && ref($Apache::bridgetask::instance{$id1})) {
                    158: 	    $id2=$Apache::bridgetask::instance{$id1}[-1];
1.142     albertel  159: 	    $shift_amt=scalar(@{$Apache::bridgetask::instance{$id1}});
                    160: 	} else {
                    161: 	    $shift_amt=0;
1.119     albertel  162: 	}
                    163:     } 
                    164:     &Apache::lonxml::debug("id1: $id1, id2: $id2, shift_amt: $shift_amt");
1.99      albertel  165:     if (!$rand_alg || $rand_alg eq '32bit' || $rand_alg eq '64bit' ||
                    166: 	$rand_alg eq '64bit2') {
1.119     albertel  167: 	$rndmod=(&Apache::lonnet::numval($id1) << 10);
                    168: 	if (defined($id2)) { $rndmod+=&Apache::lonnet::numval($id2); }
1.110     albertel  169:     } elsif ($rand_alg eq '64bit3') {
1.119     albertel  170: 	$rndmod=(&Apache::lonnet::numval2($id1) << 10);
                    171: 	if (defined($id2)) { $rndmod+=&Apache::lonnet::numval2($id2); }
1.126     albertel  172:     } elsif ($rand_alg eq '64bit4') {
1.119     albertel  173: 	my $shift=(4*$shift_amt)%30;
                    174: 	$rndmod=(&Apache::lonnet::numval3($id1) << (($shift+15)%30));
                    175: 	if (defined($id2)) {
                    176: 	    $rndmod+=(&Apache::lonnet::numval3($id2) << $shift );
1.110     albertel  177: 	}
1.126     albertel  178:     } else {
                    179: 	($rndmod,$rndmod2)=&Apache::lonnet::digest("$id1,$id2");
1.99      albertel  180:     }
1.127     albertel  181: 
1.99      albertel  182:     if ($rndseed =~/([,:])/) {
                    183: 	my $char=$1;
                    184: 	use integer;
                    185: 	my ($num1,$num2)=split(/\Q$char\E/,$rndseed);
                    186: 	$num1+=$rndmod;
1.126     albertel  187: 	$num2+= ((defined($rndmod2)) ? $rndmod2 : $rndmod);
1.109     albertel  188: 	if($Apache::lonnet::_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.99      albertel  189: 	$rndseed=$num1.$char.$num2;
                    190:     } else {
1.74      albertel  191: 	$rndseed+=$rndmod;
1.109     albertel  192: 	if($Apache::lonnet::_64bit) {
                    193: 	    use integer;
                    194: 	    $rndseed=(($rndseed<<32)>>32);
                    195: 	}
1.74      albertel  196:     }
1.111     albertel  197:     &Apache::lonxml::debug("randseed $rndmod $rndseed");
1.74      albertel  198:     &Apache::lonnet::setup_random_from_rndseed($rndseed);
1.73      albertel  199:     return '';
1.26      albertel  200: }
                    201: 
1.7       www       202: sub meta_parameter_write {
1.38      albertel  203:     my ($name,$type,$default,$display)=@_;
1.41      albertel  204:     my $partref=$Apache::inputtags::part;
                    205:     my $result='<parameter 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.'"'.
1.89      albertel  212: (defined($default)?' default="'.$default.'"':'').
                    213: (defined($display)?' display="'.$display.' [Part: '.$partref.']"':'')
1.41      albertel  214:              .'></parameter>'
                    215:              ."\n";
                    216:     return $result;
1.33      www       217: }
                    218: 
                    219: sub meta_package_write {
                    220:     my $name=shift;
1.41      albertel  221:     my $result = '<parameter part="'.$Apache::inputtags::part.'"';
                    222:     if(defined($Apache::inputtags::response[-1])) {
1.73      albertel  223: 	$result.= ' id="'.$Apache::inputtags::response[-1].'"';
1.41      albertel  224:     }
                    225:     $result.=' package="'.$name.'"></parameter>'."\n";
                    226:     return $result;
1.7       www       227: }
                    228: 
                    229: sub meta_stores_write {
1.10      www       230:     my ($name,$type,$display)=@_;
1.41      albertel  231:     my $partref=$Apache::inputtags::part;
                    232:     my $result = '<stores part="'.$Apache::inputtags::part.'"';
                    233:     if (defined($Apache::inputtags::response[-1])) {
1.73      albertel  234: 	$result.=           ' id="'.$Apache::inputtags::response[-1].'"';
                    235: 	$partref.='_'.$Apache::inputtags::response[-1];
1.41      albertel  236:     }	
                    237:     $result.=          ' name="'.$name.'"'.
                    238:                        ' type="'.$type.'"'.
                    239: 	            ' display="'.$display.' [Part: '.$partref.']"'.
                    240: 		      "></stores>\n";
1.7       www       241: }
                    242: 
1.207     jms       243: =pod
                    244: 
1.210     raeburn   245: =item mandatory_part_meta()
1.207     jms       246: 
                    247: Autogenerate metadata for mandatory
1.210     raeburn   248: input (from RAT or lonparmset) and
1.207     jms       249: output (to lonspreadsheet)
                    250: of each part
                    251: 
                    252: Note: responseid-specific data 'submission' and 'awarddetail'
                    253: not available to spreadsheet -> skip here
                    254: 
                    255: =cut
                    256: 
1.210     raeburn   257: 
                    258: sub mandatory_part_meta {
                    259:     return &meta_package_write('part').
                    260:            &meta_stores_write('solved','string','Problem Status').
                    261:            &meta_stores_write('tries','int_zeropos','Number of Attempts').
                    262:            &meta_stores_write('awarded','float','Partial Credit Factor');
1.86      albertel  263: }
                    264: 
                    265: sub meta_part_order {
                    266:     if (@Apache::inputtags::partlist) {
                    267: 	my @parts=@Apache::inputtags::partlist;
                    268: 	shift(@parts);
1.100     albertel  269: 	return '<partorder>'.join(',',@parts).'</partorder>'."\n";
1.86      albertel  270:     } else {
1.100     albertel  271: 	return '<partorder>0</partorder>'."\n";
                    272:     }
                    273: }
                    274: 
                    275: sub meta_response_order {
                    276:     if (@Apache::inputtags::responselist) {
                    277: 	return '<responseorder>'.join(',',@Apache::inputtags::responselist).
                    278: 	    '</responseorder>'."\n";
1.86      albertel  279:     }
1.14      albertel  280: }
                    281: 
1.15      albertel  282: sub check_for_previous {
1.219   ! raeburn   283:     my ($curresponse,$partid,$id,$last,$type) = @_;
1.73      albertel  284:     my %previous;
                    285:     $previous{'used'} = 0;
                    286:     foreach my $key (sort(keys(%Apache::lonhomework::history))) {
1.212     raeburn   287: 	if ($key =~ /resource\.\Q$partid\E\.\Q$id\E\.submission$/) {
1.160     albertel  288: 	    if ( $last && $key =~ /^(\d+):/ ) {
                    289: 		next if ($1 >= $last);
                    290: 	    }
1.73      albertel  291: 	    &Apache::lonxml::debug("Trying $key");
                    292: 	    my $pastresponse=$Apache::lonhomework::history{$key};
                    293: 	    if ($pastresponse eq $curresponse) {
                    294: 		$previous{'used'} = 1;
                    295: 		my $history;
                    296: 		if ( $key =~ /^(\d+):/ ) {
                    297: 		    $history=$1;
                    298: 		    $previous{'award'} = $Apache::lonhomework::history{"$history:resource.$partid.$id.awarddetail"};
                    299: 		    $previous{'last'}='0';
                    300: 		    push(@{ $previous{'version'} },$history);
                    301: 		} else {
                    302: 		    $previous{'award'} = $Apache::lonhomework::history{"resource.$partid.$id.awarddetail"};
                    303: 		    $previous{'last'}='1';
                    304: 		}
                    305: 		if (! $previous{'award'} ) { $previous{'award'} = 'UNKNOWN';	}
1.209     www       306:                 if ($previous{'award'} eq 'INTERNAL_ERROR') { $previous{'used'}=0; }
1.73      albertel  307: 		&Apache::lonxml::debug("got a match :$previous{'award'}:$previous{'used'}:");
1.219   ! raeburn   308:             } elsif ($type eq 'ci') {
        !           309:                 if (lc($pastresponse) eq lc($curresponse)) {
        !           310:                     if ($key =~ /^(\d+):/) {
        !           311:                         push (@{$previous{'versionci'}},$1);
        !           312:                         $previous{'awardci'} = $Apache::lonhomework::history{"resource.$partid.$id.awarddetail"};
        !           313:                         $previous{'usedci'} = 1;
        !           314:                     }
        !           315:                 }
        !           316:             }
1.32      albertel  317: 	}
1.73      albertel  318:     }
                    319:     &Apache::lonhomework::showhash(%previous);
                    320:     return %previous;
1.54      albertel  321: }
                    322: 
                    323: sub handle_previous {
1.73      albertel  324:     my ($previous,$ad)=@_;
                    325:     if ($$previous{'used'} && ($$previous{'award'} eq $ad) ) {
                    326: 	if ($$previous{'last'}) {
                    327: 	    push(@Apache::inputtags::previous,'PREVIOUSLY_LAST');
1.107     albertel  328: 	    push(@Apache::inputtags::previous_version,$$previous{'version'});
1.217     raeburn   329: 	} elsif (($Apache::lonhomework::type ne 'survey') &&
                    330:                  ($Apache::lonhomework::type ne 'surveycred') &&
                    331:                  ($Apache::lonhomework::type ne 'anonsurvey') &&
                    332:                  ($Apache::lonhomework::type ne 'anonsurveycred')) { 
1.73      albertel  333: 	    push(@Apache::inputtags::previous,'PREVIOUSLY_USED');
1.107     albertel  334: 	    push(@Apache::inputtags::previous_version,$$previous{'version'});
1.73      albertel  335: 	}
1.54      albertel  336:     }
1.44      albertel  337: }
                    338: 
1.45      albertel  339: sub view_or_modify {
1.149     albertel  340:     my ($symb,$courseid,$domain,$name) = &Apache::lonnet::whichuser();
1.73      albertel  341:     my $myself=0;
1.120     albertel  342:     if ( ($name eq $env{'user.name'}) && ($domain eq $env{'user.domain'}) ) {
1.73      albertel  343: 	$myself=1;
                    344:     }
                    345:     my $vgr=&Apache::lonnet::allowed('vgr',$courseid);
                    346:     my $mgr=&Apache::lonnet::allowed('vgr',$courseid);
                    347:     if ($mgr) { return "M"; }
                    348:     if ($vgr) { return "V"; }
                    349:     if ($myself) { return "V"; }
                    350:     return '';
1.45      albertel  351: }
                    352: 
1.44      albertel  353: sub start_dataresponse {
1.73      albertel  354:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    355:     my $id = &Apache::response::start_response($parstack,$safeeval);
                    356:     my $result;
                    357:     if ($target eq 'web') {
                    358: 	$result = $token->[2]->{'display'}.':';
                    359:     } elsif ($target eq 'meta') {
                    360: 	$result = &Apache::response::meta_stores_write($token->[2]->{'name'},
                    361: 						       $token->[2]->{'type'},
                    362: 						       $token->[2]->{'display'});
                    363: 	$result .= &Apache::response::meta_package_write('dataresponse');
                    364:     }
                    365:     return $result;
1.44      albertel  366: }
                    367: 
                    368: sub end_dataresponse {
1.73      albertel  369:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    370:     my $result;
                    371:     if ( $target eq 'web' ) {
                    372:     } elsif ($target eq 'grade' ) {
1.120     albertel  373: 	if ( defined $env{'form.submitted'}) {
1.149     albertel  374: 	    my ($symb,$courseid,$domain,$name)=&Apache::lonnet::whichuser();
1.73      albertel  375: 	    my $allowed=&Apache::lonnet::allowed('mgr',$courseid);
                    376: 	    if ($allowed) {
1.94      albertel  377: 		&Apache::response::setup_params('dataresponse',$safeeval);
1.73      albertel  378: 		my $partid = $Apache::inputtags::part;
                    379: 		my $id = $Apache::inputtags::response['-1'];
1.120     albertel  380: 		my $response = $env{'form.HWVAL_'.$id};
1.73      albertel  381: 		my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    382: 		if ( $response =~ /[^\s]/) {
                    383: 		    $Apache::lonhomework::results{"resource.$partid.$id.$name"}=$response;
                    384: 		    $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
                    385: 		    $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}='SUBMITTED';
                    386: 		}
                    387: 	    } else {
                    388: 		$result='Not Permitted to change values.'
                    389: 	    }
1.45      albertel  390: 	}
1.73      albertel  391:     }
                    392:     &Apache::response::end_response;
                    393:     return $result;
1.3       albertel  394: }
                    395: 
1.129     albertel  396: sub start_customresponse {
1.128     albertel  397:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    398:     my $id = &Apache::response::start_response($parstack,$safeeval);
1.129     albertel  399:     push(@Apache::lonxml::namespace,'customresponse');
1.128     albertel  400:     my $result;
1.157     www       401:     @Apache::response::custom_answer=();
                    402:     @Apache::response::custom_answer_type=();
1.128     albertel  403:     &Apache::lonxml::register('Apache::response',('answer'));
                    404:     if ($target eq 'web') {
                    405:   	if (  &Apache::response::show_answer() ) {
                    406: 	    my $answer = &Apache::lonxml::get_param('answerdisplay',$parstack,
                    407: 						   $safeeval);
1.152     albertel  408: 	    $Apache::inputtags::answertxt{$id}=[$answer];
1.128     albertel  409: 	}
                    410:     } elsif ($target eq 'edit') {
                    411: 	$result.=&Apache::edit::tag_start($target,$token);
                    412: 	$result.=&Apache::edit::text_arg('String to display for answer:',
1.145     albertel  413: 					 'answerdisplay',$token);
1.128     albertel  414: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    415:     } elsif ($target eq 'modified') {
                    416: 	my $constructtag;
                    417: 	$constructtag=&Apache::edit::get_new_args($token,$parstack,
                    418: 						  $safeeval,'answerdisplay');
                    419: 	if ($constructtag) {
                    420: 	    $result = &Apache::edit::rebuild_tag($token);
                    421: 	}
                    422:     } elsif ($target eq 'answer' || $target eq 'grade') {
                    423: 	&Apache::response::reset_params();
                    424:     } elsif ($target eq 'meta') {
1.129     albertel  425: 	$result .= &Apache::response::meta_package_write('customresponse');
1.128     albertel  426:     }
                    427:     return $result;
                    428: }
                    429: 
1.129     albertel  430: sub end_customresponse {
1.128     albertel  431:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    432:     my $result;
                    433:     my $part=$Apache::inputtags::part;
                    434:     my $id=$Apache::inputtags::response[-1];
                    435:     if ( $target eq 'grade' && &Apache::response::submitted() ) {
                    436: 	my $response = &Apache::response::getresponse();
1.146     albertel  437: 	if ($Apache::lonhomework::type eq 'exam' ||
                    438: 	    &Apache::response::submitted('scantron')) {
                    439: 	    &Apache::response::scored_response($part,$id);
                    440: 	} elsif ( $response =~ /[^\s]/ && 
1.158     www       441: 		  $Apache::response::custom_answer_type[-1] eq 'loncapa/perl') {
1.128     albertel  442: 	    if (!$Apache::lonxml::default_homework_loaded) {
                    443: 		&Apache::lonxml::default_homework_load($safeeval);
                    444: 	    }
                    445: 	    my %previous = &Apache::response::check_for_previous($response,
                    446: 								 $part,$id);
                    447: 	    $Apache::lonhomework::results{"resource.$part.$id.submission"}=
                    448: 		$response;
                    449: 	    my $error;
1.129     albertel  450: 	    ${$safeeval->varglob('LONCAPA::customresponse_submission')}=
1.128     albertel  451: 		$response;
                    452: 	    
1.216     www       453: 	    my ($award,$score) = &Apache::run::run('{ my $submission=$LONCAPA::customresponse_submission;'.$Apache::response::custom_answer[-1].'}',$safeeval);
1.128     albertel  454: 	    if (!&Apache::inputtags::valid_award($award)) {
                    455: 		$error = $award;
                    456: 		$award = 'ERROR';
                    457: 	    }
                    458: 	    &Apache::response::handle_previous(\%previous,$award);
                    459: 	    $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
                    460: 		$award;
1.216     www       461:             if ($award eq 'ASSIGNED_SCORE') {
                    462:                 $Apache::lonhomework::results{"resource.$part.$id.awarded"}=1.0*$score;
                    463:             }
1.128     albertel  464: 	    if ($error) {
                    465: 		$Apache::lonhomework::results{"resource.$part.$id.awardmsg"}=
                    466: 		    $error;
                    467: 	    }
                    468: 	}
1.146     albertel  469:     } elsif ( $target eq 'answer') {
                    470: 	$result  = &Apache::response::answer_header('customresponse');
                    471: 	my $answer = &Apache::lonxml::get_param('answerdisplay',$parstack,
                    472: 						$safeeval);
                    473: 	if ($env{'form.answer_output_mode'} ne 'tex') {
                    474: 	    $answer = '<b>'.$answer.'</b>';
                    475: 	}
                    476: 	$result .= &Apache::response::answer_part('customresponse',$answer);
                    477: 	$result .= &Apache::response::answer_footer('customresponse');
                    478:     }
1.163     albertel  479:     if ($target eq 'web') {
                    480: 	&setup_prior_tries_hash(\&format_prior_response_math);
                    481:     }
1.146     albertel  482:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
                    483: 	$target eq 'tex' || $target eq 'analyze') {
1.179     foxr      484: 	&Apache::lonxml::increment_counter(&Apache::response::repetition(),
1.180     foxr      485: 					   "$part.$id");
1.179     foxr      486: 	if ($target eq 'analyze') {
1.187     raeburn   487:             $Apache::lonhomework::analyze{"$part.$id.type"} = 'customresponse';
1.179     foxr      488: 	    &Apache::lonhomework::set_bubble_lines();
                    489: 	}
1.128     albertel  490:     }
                    491:     pop(@Apache::lonxml::namespace);
1.157     www       492:     pop(@Apache::response::custom_answer);
                    493:     pop(@Apache::response::custom_answer_type);
1.128     albertel  494:     &Apache::lonxml::deregister('Apache::response',('answer'));
                    495:     &Apache::response::end_response();
                    496:     return $result;
                    497: }
                    498: 
1.163     albertel  499: sub format_prior_response_custom {
                    500:     my ($mode,$answer) =@_;
                    501:     return '<span class="LC_prior_custom">'.
                    502: 	    &HTML::Entities::encode($answer,'"<>&').'</span>';
                    503: }
1.140     www       504: 
1.139     www       505: sub start_mathresponse {
1.140     www       506:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    507:     my $id = &Apache::response::start_response($parstack,$safeeval);
                    508:     push(@Apache::lonxml::namespace,'mathresponse');
1.139     www       509:     my $result;
1.157     www       510:     @Apache::response::custom_answer=();
                    511:     @Apache::response::custom_answer_type=();
1.140     www       512:     &Apache::lonxml::register('Apache::response',('answer'));
                    513:     if ($target eq 'web') {
                    514:   	if (  &Apache::response::show_answer() ) {
                    515: 	    my $answer = &Apache::lonxml::get_param('answerdisplay',$parstack,
                    516: 						   $safeeval);
1.152     albertel  517: 	    $Apache::inputtags::answertxt{$id}=[$answer];
1.140     www       518: 	}
1.191     www       519: 
1.140     www       520:     } elsif ($target eq 'edit') {
                    521: 	$result.=&Apache::edit::tag_start($target,$token);
                    522: 	$result.=&Apache::edit::text_arg('String to display for answer:',
                    523: 					 'answerdisplay',$token);
1.150     www       524: 	$result.=&Apache::edit::select_arg('Algebra System:',
                    525: 					   'cas',
1.214     www       526: 					   ['maxima','R'],
1.150     www       527: 					   $token);
                    528: 	$result.=&Apache::edit::text_arg('Argument Array:',
1.193     www       529: 					 'args',$token).
                    530:                  &Apache::loncommon::help_open_topic('Maxima_Argument_Array');
1.192     www       531:         $result.=&Apache::edit::text_arg('Libraries:',
1.193     www       532:                                          'libraries',$token).
                    533:                  &Apache::loncommon::help_open_topic('Maxima_Libraries');
1.140     www       534: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    535:     } elsif ($target eq 'modified') {
                    536: 	my $constructtag;
                    537: 	$constructtag=&Apache::edit::get_new_args($token,$parstack,
1.192     www       538: 						  $safeeval,'answerdisplay','cas','args','libraries');
1.140     www       539: 	if ($constructtag) {
                    540: 	    $result = &Apache::edit::rebuild_tag($token);
                    541: 	}
                    542:     } elsif ($target eq 'answer' || $target eq 'grade') {
                    543: 	&Apache::response::reset_params();
                    544:     } elsif ($target eq 'meta') {
                    545: 	$result .= &Apache::response::meta_package_write('mathresponse');
1.139     www       546:     }
                    547:     return $result;
                    548: }
                    549: 
1.191     www       550: sub edit_mathresponse_button {
                    551:     my ($id,$field)=@_;
                    552:     my $button=&mt('Edit Answer');
1.201     riegler   553: #    my $helplink=&Apache::loncommon::help_open_topic('Formula_Editor');
1.197     riegler   554:     my $iconpath=$Apache::lonnet::perlvar{'lonIconsURL'};
1.191     www       555:     return(<<ENDFORMULABUTTON);
1.213     bisitz    556: <script type="text/javascript" language="JavaScript">
1.191     www       557: function edit_${id}_${field} (textarea) {
                    558:     thenumber = textarea;
                    559:     thedata = document.forms['lonhomework'].elements[textarea].value;
                    560:     newwin = window.open("/adm/dragmath/applet/MaximaPopup.html","","width=565,height=400,resizable");
                    561: }
                    562: </script>
1.215     bisitz    563: <a href="javascript:edit_${id}_${field}('${field}');void(0);"><img class="stift" src="$iconpath/stift.gif" alt="$button" title="$button" /></a>
1.191     www       564: ENDFORMULABUTTON
                    565: }
                    566: 
1.139     www       567: sub end_mathresponse {
1.140     www       568:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    569:     my $result;
                    570:     my $part=$Apache::inputtags::part;
                    571:     my $id=$Apache::inputtags::response[-1];
                    572:     if ( $target eq 'grade' && &Apache::response::submitted() ) {
                    573: 	my $response = &Apache::response::getresponse();
1.150     www       574: 	if ( $response =~ /[^\s]/ ) {
1.140     www       575: 	    if (!$Apache::lonxml::default_homework_loaded) {
                    576: 		&Apache::lonxml::default_homework_load($safeeval);
                    577: 	    }
                    578: 	    my %previous = &Apache::response::check_for_previous($response,
                    579: 								 $part,$id);
                    580: 	    $Apache::lonhomework::results{"resource.$part.$id.submission"}=
                    581: 		$response;
                    582: 	    my $error;
1.155     www       583: 	    my $award;
                    584: 	    my $cas = &Apache::lonxml::get_param('cas',$parstack,$safeeval);
                    585:             if ($cas eq 'maxima') {
                    586:                 my $args = [&Apache::lonxml::get_param_var('args',$parstack,$safeeval)];
1.192     www       587:                 $award=&Apache::lonmaxima::maxima_run($Apache::response::custom_answer[-1],$response,$args,
                    588:                                                       &Apache::lonxml::get_param('libraries',$parstack,$safeeval));
1.155     www       589:             }
1.214     www       590:             if ($cas eq 'R') {
                    591:                 my $args = [&Apache::lonxml::get_param_var('args',$parstack,$safeeval)];
                    592:                 $award=&Apache::lonr::r_run($Apache::response::custom_answer[-1],$response,$args,
                    593:                                             &Apache::lonxml::get_param('libraries',$parstack,$safeeval));
                    594:             }
                    595: 
1.140     www       596: 	    if (!&Apache::inputtags::valid_award($award)) {
1.151     albertel  597: 		$error = $award;
                    598: 		$award = 'ERROR';
1.140     www       599: 	    }
                    600: 	    &Apache::response::handle_previous(\%previous,$award);
                    601: 	    $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
                    602: 		$award;
                    603: 	    if ($error) {
                    604: 		$Apache::lonhomework::results{"resource.$part.$id.awardmsg"}=
                    605: 		    $error;
                    606: 	    }
                    607: 	}
                    608:     }
1.163     albertel  609:     if ($target eq 'web') {
                    610: 	&setup_prior_tries_hash(\&format_prior_response_math);
1.203     riegler   611:         my $partid = $Apache::inputtags::part;
                    612:         my $id = $Apache::inputtags::response[-1];
                    613:         if (($Apache::inputtags::status['-1'] eq 'CAN_ANSWER')
1.205     raeburn   614:            && (&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffeditor') ne 'yes')) {
1.202     riegler   615:             $result.=&edit_mathresponse_button($id,"HWVAL_$id");
                    616:         }
1.163     albertel  617:     }
                    618: 
1.140     www       619:     pop(@Apache::lonxml::namespace);
1.157     www       620:     pop(@Apache::response::custom_answer);
                    621:     pop(@Apache::response::custom_answer_type);
1.140     www       622:     &Apache::lonxml::deregister('Apache::response',('answer'));
                    623:     &Apache::response::end_response();
                    624:     return $result;
1.139     www       625: }
                    626: 
1.163     albertel  627: sub format_prior_response_math {
                    628:     my ($mode,$answer) =@_;
                    629:     return '<span class="LC_prior_math">'.
                    630: 	    &HTML::Entities::encode($answer,'"<>&').'</span>';
                    631: }
                    632: 
1.156     www       633: sub implicit_multiplication {
                    634:     my ($expression)=@_;
                    635: # Escape scientific notation, so 3e8 does not become 3*e*8
                    636: # 3e8 -> 3&8; 3e-8 -> 3&-8; 3E+8 -> e&+8
                    637:     $expression=~s/(\d+)e([\+\-]*\d+)/$1\&\($2\)/gsi;
                    638: # 3x10^8 -> 3&8; 3*10^-8 -> 3&-8
                    639:     $expression=~s/(\d+)(?:x|\*)10(?:\^|\*\*)([\+\-]*\d+)/$1\&\($2\)/gsi;
                    640: # Fill in multiplication signs
                    641: # a b -> a*b;3 b -> 3*b;3 4 -> 3*4
1.176     albertel  642:     $expression=~s/([A-Za-z0-9])\s+(?=[A-Za-z0-9])/$1\*/gs;
1.156     www       643: # )( -> )*(; ) ( -> )*(
                    644:     $expression=~s/\)\s*\(/\)\*\(/gs;
                    645: # 3a -> 3*a; 3( -> 3*(; 3 ( -> 3*(; 3A -> 3*A
                    646:     $expression=~s/(\d)\s*([a-zA-Z\(])/$1\*$2/gs;
                    647: # a ( -> a*(
1.174     riegler   648:     $expression=~s/([A-Za-z0-9])\s+\(/$1\*\(/gs;
1.156     www       649: # )a -> )*a; )3 -> )*3; ) 3 -> )*3
1.174     riegler   650:     $expression=~s/\)\s*([A-Za-z0-9])/\)\*$1/gs;
1.156     www       651: # 3&8 -> 3e8; 3&-4 -> 3e-4
                    652:     $expression=~s/(\d+)\&\(([\+\-]*\d+)\)/$1e$2/gs;
                    653:     return $expression;
                    654: }
1.140     www       655: 
1.128     albertel  656: sub start_answer {
                    657:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    658:     my $result;
1.157     www       659:     push(@Apache::response::custom_answer,
                    660: 	&Apache::lonxml::get_all_text_unbalanced("/answer",$parser));
                    661:     push(@Apache::response::custom_answer_type,
                    662: 	lc(&Apache::lonxml::get_param('type',$parstack,$safeeval)));
                    663:     $Apache::response::custom_answer_type[-1] =~ s/\s+//g;
1.128     albertel  664:     if ($target eq "edit" ) {
                    665: 	$result=&Apache::edit::tag_start($target,$token,'Answer algorithm');
                    666: 	$result.=&Apache::edit::editfield($token->[1],
1.158     www       667: 					  $Apache::response::custom_answer[-1],
1.128     albertel  668: 					  '',80,4);
                    669:     } elsif ( $target eq "modified" ) {
                    670: 	$result=$token->[4].&Apache::edit::modifiedfield('/answer',$parser);
                    671:     }
                    672:     return $result;
                    673: }
                    674: 
                    675: sub end_answer {
                    676:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    677:     if ($target eq 'edit' ) {
                    678: 	return &Apache::edit::end_table();
                    679:     }
                    680: }
                    681: 
1.83      albertel  682: sub decide_package {
                    683:     my ($tagstack)=@_;
                    684:     my $package;
                    685:     if ($$tagstack[-1] eq 'parameter') {
                    686: 	$package='part';
                    687:     } else {
                    688: 	my $i=-1;
                    689: 	while (defined($$tagstack[$i])) {
                    690: 	    if ($$tagstack[$i] =~ /(response|hint)$/) {
                    691: 		$package=$$tagstack[$i];
                    692: 		last;
                    693: 	    }
                    694: 	    $i--;
                    695: 	}
                    696:     }
                    697:     return $package;
                    698: }
                    699: 
1.3       albertel  700: sub start_responseparam {
1.73      albertel  701:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    702:     my $result='';
                    703:     if ($target eq 'meta') {
                    704: 	$result = &meta_parameter_write($token->[2]->{'name'},
                    705: 					$token->[2]->{'type'},
                    706: 					$token->[2]->{'default'},
                    707: 					$token->[2]->{'description'});
                    708:     } elsif ($target eq 'edit') {
                    709: 	$result.=&Apache::edit::tag_start($target,$token);
1.83      albertel  710: 	my $optionlist;
                    711: 	my $package=&decide_package($tagstack);
                    712: 	foreach my $key (sort(keys(%Apache::lonnet::packagetab))) {
                    713: 	    if ($key =~ /^\Q$package\E&(.*)&display$/) {
                    714: 		$optionlist.='<option value="'.$1.'">'.
                    715: 		    $Apache::lonnet::packagetab{$key}.'</option>';
                    716: 	    }
                    717: 	}
                    718: 	if (defined($optionlist)) {
1.206     bisitz    719: 	    $result.=&mt('Use template:').' <select name="'.
1.83      albertel  720: 		&Apache::edit::html_element_name('parameter_package').'">'.
                    721: 		    '<option value=""></option>'.$optionlist.'</select><br />';
                    722: 	}
1.73      albertel  723: 	$result.=&Apache::edit::text_arg('Name:','name',$token).
                    724: 	    &Apache::edit::text_arg('Type:','type',$token).
                    725: 		&Apache::edit::text_arg('Description:','description',$token).
                    726: 		    &Apache::edit::text_arg('Default:','default',$token).
                    727: 			"</td></tr>";
                    728: 	$result.=&Apache::edit::end_table;
                    729:     } elsif ($target eq 'modified') {
1.83      albertel  730: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    731: 						     $safeeval,'name','type',
                    732: 						     'description','default');
                    733: 	my $element=&Apache::edit::html_element_name('parameter_package');
1.120     albertel  734: 	if (defined($env{"form.$element"}) && $env{"form.$element"} ne '') {
                    735: 	    my $name=$env{"form.$element"};
1.83      albertel  736: 	    my $tag=&decide_package($tagstack);
                    737: 	    $token->[2]->{'name'}=$name;
                    738: 	    $token->[2]->{'type'}=
                    739: 		$Apache::lonnet::packagetab{"$tag&$name&type"};
                    740: 	    $token->[2]->{'description'}=
                    741: 		$Apache::lonnet::packagetab{"$tag&$name&display"};
                    742: 	    $token->[2]->{'default'}=
                    743: 		$Apache::lonnet::packagetab{"$tag&$name&default"};
1.186     raeburn   744:             $token->[3] = ['name','type','description','default'];
1.83      albertel  745: 	    $constructtag=1;
                    746: 	}
1.73      albertel  747: 	if ($constructtag) {
                    748: 	    $result = &Apache::edit::rebuild_tag($token);
                    749: 	}
                    750:     } elsif ($target eq 'grade' || $target eq 'answer' || $target eq 'web' ||
                    751: 	     $target eq 'tex' || $target eq 'analyze' ) {
1.120     albertel  752: 	if ($env{'request.state'} eq 'construct') {
1.73      albertel  753: 	    my $name   =&Apache::lonxml::get_param('name',$parstack,$safeeval);
                    754: 	    my $default=&Apache::lonxml::get_param('default',$parstack,
                    755: 						     $safeeval);
                    756: 	    if ($name) {$Apache::inputtags::params{$name}=$default;}
                    757: 	}
1.52      albertel  758:     }
1.73      albertel  759:     return $result;
1.3       albertel  760: }
                    761: 
                    762: sub end_responseparam {
1.73      albertel  763:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    764:     if ($target eq 'edit') { return ('','no'); }
                    765:     return '';
1.55      albertel  766: }
                    767: 
                    768: sub start_parameter {
1.114     albertel  769:     return &start_responseparam(@_);
1.55      albertel  770: }
                    771: 
                    772: sub end_parameter {
1.114     albertel  773:     return &end_responseparam(@_);
1.42      albertel  774: }
                    775: 
1.67      albertel  776: sub reset_params {
                    777:     %Apache::inputtags::params=();
                    778: }
                    779: 
1.42      albertel  780: sub setup_params {
1.94      albertel  781:     my ($tag,$safeeval) = @_;
1.42      albertel  782: 
1.120     albertel  783:     if ($env{'request.state'} eq 'construct') { return; }
1.73      albertel  784:     my %paramlist=();
                    785:     foreach my $key (keys(%Apache::lonnet::packagetab)) {
1.161     albertel  786: 	if ($key =~ /^\Q$tag\E/) {
1.73      albertel  787: 	    my ($package,$name) = split(/&/,$key);
                    788: 	    $paramlist{$name}=1;
                    789: 	}
1.42      albertel  790:     }
1.73      albertel  791:     foreach my $key (keys(%paramlist)) {
                    792: 	my $entry= 'resource.'.$Apache::inputtags::part;
                    793: 	if (defined($Apache::inputtags::response[-1])) {
                    794: 	    $entry.='_'.$Apache::inputtags::response[-1];
                    795: 	}
                    796: 	$entry.='.'.$key;
                    797: 	&Apache::lonxml::debug("looking for $entry");
                    798: 	my $value = &Apache::lonnet::EXT("$entry");
                    799: 	&Apache::lonxml::debug("$key has value :$value:");
                    800: 	if ($value eq 'con_lost' || $value =~ /^error:/) {
                    801: 	    &Apache::lonxml::debug("using nothing");
                    802: 	    $Apache::inputtags::params{$key}='';
                    803: 	} else {
1.94      albertel  804: 	    my $string="{return qq\0".$value."\0}";
                    805: 	    my $newvalue=&Apache::run::run($string,$safeeval,1);
                    806: 	    if (defined($newvalue)) { $value=$newvalue; }
1.73      albertel  807: 	    $Apache::inputtags::params{$key}=$value;
                    808: 	}
1.42      albertel  809:     }
1.48      albertel  810: }
                    811: 
1.132     albertel  812: {
                    813:     my @answer_bits;
1.147     albertel  814:     my $need_row_start;
1.132     albertel  815: 
1.48      albertel  816: sub answer_header {
1.147     albertel  817:     my ($type,$increment,$rows) = @_;
1.73      albertel  818:     my $result;
1.120     albertel  819:     if ($env{'form.answer_output_mode'} eq 'tex') {
1.132     albertel  820: 	undef(@answer_bits);
1.133     albertel  821: 	my $bit;
                    822: 	if ($Apache::lonhomework::type eq 'exam') {
                    823: 	    $bit = ($Apache::lonxml::counter+$increment).') ';
                    824: 	} else {
                    825: 	    $bit .= ' Answer for Part: \verb|'.
                    826: 		$Apache::inputtags::part.'| ';
                    827: 	}
                    828: 	push(@answer_bits,$bit);
1.73      albertel  829:     } else {
1.147     albertel  830: 	my $td = '<td '.(defined($rows)?'rowspan="'.$rows.'"':'').'>';
1.132     albertel  831: 	$result  = '<table border="1"><tr>';
                    832: 	if ($Apache::lonhomework::type eq 'exam') {
1.147     albertel  833: 	    $result .= $td.($Apache::lonxml::counter+$increment). ')</td>';
1.132     albertel  834: 	} else {
1.147     albertel  835: 	    $result .= $td.&mt('Answer for Part: [_1]',
                    836: 			       $Apache::inputtags::part).'</td>';
1.132     albertel  837: 	}
                    838: 	$result .= "\n";
1.147     albertel  839: 	$need_row_start = 0;
                    840:     }
                    841:     return $result;
                    842: }
                    843: 
                    844: sub next_answer {
                    845:     my ($type) = @_;
                    846:     my $result;
                    847:     if ($env{'form.answer_output_mode'} eq 'tex') {
                    848: 	# FIXME ... need to do something with tex mode
                    849:     } else {
                    850: 	$result .= "</tr>";
                    851: 	$need_row_start = 1;
1.73      albertel  852:     }
                    853:     return $result;
1.48      albertel  854: }
                    855: 
                    856: sub answer_part {
1.148     albertel  857:     my ($type,$answer,$args) = @_;
1.73      albertel  858:     my $result;
1.120     albertel  859:     if ($env{'form.answer_output_mode'} eq 'tex') {
1.148     albertel  860: 	if (!$args->{'no_verbatim'}) {
                    861: 	    my $to_use='|';
                    862: 	    foreach my $value (32..126) {
                    863: 		my $char=pack('c',$value);
                    864: 		if ($answer !~ /\Q$char\E/) {
                    865: 		    $to_use=$char;
                    866: 		    last;
                    867: 		}
                    868: 	    }
1.189     www       869:             my $fullanswer=$answer;
1.188     www       870:             $answer='';
1.189     www       871:             foreach my $element (split(/[\;]/,$fullanswer)) {
                    872: 	       if ($element ne '') {
                    873: 	 	  $answer.= '\verb'.$to_use.$element.$to_use.' \newline';
                    874: 	       }
1.188     www       875:             }
1.124     albertel  876: 	}
1.132     albertel  877: 	if ($answer ne '') {
1.148     albertel  878: 	    push(@answer_bits,$answer);
1.132     albertel  879: 	}
1.73      albertel  880:     } else {
1.147     albertel  881: 	if ($need_row_start) {
                    882: 	    $result .= '<tr>';
                    883: 	    $need_row_start = 0;
                    884: 	}
1.189     www       885: 	$result .= '<td>'.$answer.'</td>';
1.73      albertel  886:     }
                    887:     return $result;
1.48      albertel  888: }
                    889: 
                    890: sub answer_footer {
1.73      albertel  891:     my ($type) = @_;
                    892:     my $result;
1.120     albertel  893:     if ($env{'form.answer_output_mode'} eq 'tex') {
1.189     www       894: 	$result  = ' \vskip 0 mm \noindent \begin{tabular}{|p{1.5cm}|p{6.8cm}|}\hline ';
                    895: 	$result .= $answer_bits[0].'&\vspace*{-4mm}\begin{itemize}';
                    896:         for (my $i=1;$i<=$#answer_bits;$i++) {
                    897:             $result.='\item '.$answer_bits[$i].'\vspace*{-7mm}';
                    898:         }
                    899: 	$result .= ' \end{itemize} \\\\ \hline \end{tabular} \vskip 0 mm ';
1.73      albertel  900:     } else {
1.185     albertel  901: 	if (!$need_row_start) {
                    902: 	    $result .= '</tr>';
                    903: 	}
                    904: 	$result .= '</table>';
1.73      albertel  905:     }
                    906:     return $result;
1.1       albertel  907: }
1.2       albertel  908: 
1.132     albertel  909: }
                    910: 
1.62      albertel  911: sub showallfoils {
1.120     albertel  912:     if (defined($env{'form.showallfoils'})) {
1.149     albertel  913: 	my ($symb)=&Apache::lonnet::whichuser();
1.120     albertel  914: 	if (($env{'request.state'} eq 'construct') || 
                    915: 	    ($env{'user.adv'} && $symb eq '')      ||
1.118     foxr      916:             ($Apache::lonhomework::viewgrades) ) {
1.102     albertel  917: 	    return 1;
                    918: 	}
1.73      albertel  919:     }
1.108     albertel  920:     if ($Apache::lonhomework::type eq 'survey') { return 1; }
1.217     raeburn   921:     if ($Apache::lonhomework::type eq 'surveycred') { return 1; }
                    922:     if ($Apache::lonhomework::type eq 'anonsurvey') { return 1; }
                    923:     if ($Apache::lonhomework::type eq 'anonsurveycred') { return 1; }
                    924: 
1.102     albertel  925:     return 0;
1.70      albertel  926: }
                    927: 
1.168     albertel  928: =pod
                    929: 
1.210     raeburn   930: =item &getresponse();
1.168     albertel  931: 
                    932: Retreives the current submitted response, helps out in the case of
                    933: scantron mode.
                    934: 
                    935: Returns either the exact text of the submission, or a bubbled response
                    936: converted to something usable.
                    937: 
                    938: Optional Arguments:
1.171     albertel  939:   $offset - (defaults to 1) if a problem has more than one bubble
                    940:             response, pass in the number of the bubble wanted, (the
                    941:             first bubble associated with a problem has an offset of 1,
                    942:             the second bubble is 2
                    943: 
1.168     albertel  944:   $resulttype - undef    -> a number between 0 and 25
                    945:                 'A is 1' -> a number between 1 and 26
                    946:                 'letter' -> a letter between 'A' and 'Z'
1.172     foxr      947:   $lines  - undef problem only needs a single line of bubbles.
                    948:             nonzero  Problem wants the first nonempty response in 
                    949:                       $lines lines of bubbles.
                    950:   $bubbles_per_line - Must be provided if lines is defined.. number of
                    951:                       bubbles on a line.
1.173     albertel  952: 
1.168     albertel  953: =cut
                    954: 
1.70      albertel  955: sub getresponse {
1.172     foxr      956:     my ($offset,$resulttype, $lines, $bubbles_per_line)=@_;
1.70      albertel  957:     my $formparm='form.HWVAL_'.$Apache::inputtags::response['-1'];
                    958:     my $response;
1.168     albertel  959:     if (!defined($offset)) {
1.170     albertel  960: 	$offset=1;
1.70      albertel  961:     } else {
1.168     albertel  962: 	$formparm.=":$offset";
1.70      albertel  963:     }
1.172     foxr      964:     if (!defined($lines)) {
                    965: 	$lines = 1;
                    966:     }
1.70      albertel  967:     my %let_to_num=('A'=>0,'B'=>1,'C'=>2,'D'=>3,'E'=>4,'F'=>5,'G'=>6,'H'=>7,
                    968: 		    'I'=>8,'J'=>9,'K'=>10,'L'=>11,'M'=>12,'N'=>13,'O'=>14,
                    969: 		    'P'=>15,'Q'=>16,'R'=>17,'S'=>18,'T'=>19,'U'=>20,'V'=>21,
                    970: 		    'W'=>22,'X'=>23,'Y'=>24,'Z'=>25);
1.120     albertel  971:     if ($env{'form.submitted'} eq 'scantron') {
1.71      albertel  972: 	my $part  = $Apache::inputtags::part;
                    973: 	my $id    = $Apache::inputtags::response[-1];
1.172     foxr      974: 	
                    975: 	my $line;
1.211     raeburn   976: 	my $startline = $env{'form.scantron_questnum_start.'.$part.'.'.$id};
                    977:         if (!$startline) {
                    978:             $startline = $Apache::lonxml::counter;
                    979:         }
1.172     foxr      980: 	for ($line = 0; $line < $lines; $line++) {
1.211     raeburn   981:             my $theline = $startline+$offset-1+$line;
1.184     foxr      982: 	    $response = $env{"scantron.$theline.answer"};
                    983: 	    if ((defined($response)) && ($response ne "") && ($response ne " ")) {
1.172     foxr      984: 		last;
                    985: 	    }
1.211     raeburn   986:  
1.172     foxr      987: 	}
1.178     albertel  988: 
                    989: 	# save bubbled letter for later
                    990: 	$Apache::lonhomework::results{"resource.$part.$id.scantron"}.=
                    991: 	    $response;
1.90      albertel  992: 	if ($resulttype ne 'letter') {
1.104     albertel  993: 	    if ($resulttype eq 'A is 1') {
1.105     albertel  994: 		$response = $let_to_num{$response}+1;
                    995: 	    } else {
1.104     albertel  996: 		$response = $let_to_num{$response};
                    997: 	    }
1.172     foxr      998: 	    if ($response ne "") {
                    999: 		$response += $line * $bubbles_per_line;
                   1000: 	    }
                   1001: 	} else {
                   1002: 	    if ($response ne "") {
                   1003: 		$response = chr(ord($response) + $line * $bubbles_per_line);
                   1004: 	    }
1.90      albertel 1005: 	}
1.172     foxr     1006: 
1.70      albertel 1007:     } else {
1.120     albertel 1008: 	$response = $env{$formparm};
1.70      albertel 1009:     }
1.172     foxr     1010:     # 
                   1011:     #  If we have a nonempty answer, correct the numeric value
                   1012:     #  of the answer for the line on which it was found.
                   1013:     #
                   1014: 
1.70      albertel 1015:     return $response;
1.62      albertel 1016: }
1.71      albertel 1017: 
1.168     albertel 1018: =pod
                   1019: 
                   1020: =item &repetition();
                   1021: 
                   1022: Returns the number of lines that are required to encode the weight.
                   1023: (Currently expects that there are 10 bubbles per line)
                   1024: 
                   1025: =cut
                   1026: 
1.71      albertel 1027: sub repetition {
                   1028:     my $id = $Apache::inputtags::part;
                   1029:     my $weight = &Apache::lonnet::EXT("resource.$id.weight");
1.121     albertel 1030:     if (!defined($weight) || ($weight eq '')) { $weight=1; }
1.125     albertel 1031:     my $repetition = int($weight/10);
                   1032:     if ($weight % 10 != 0) { $repetition++; } 
1.72      albertel 1033:     return $repetition;
                   1034: }
                   1035: 
1.168     albertel 1036: =pod
                   1037: 
1.210     raeburn  1038: =item &scored_response();
1.168     albertel 1039: 
                   1040: Sets the results hash elements
                   1041: 
                   1042:    resource.$part_id.$response_id.awarded - to the floating point
                   1043:      number between 0 and 1 that was awarded on the bubbled input
                   1044: 
                   1045:    resource.$part_id.$response_id.awarddetail - to 'ASSIGNED_SCORE'
                   1046: 
                   1047: Returns
                   1048: 
                   1049:    the number of bubble sheet lines that were used (and likely need to
                   1050:      be passed to &Apache::lonxml::increment_counter()
                   1051: 
                   1052: Arguments
                   1053: 
                   1054:    $part_id - id of the part to grade
                   1055:    $response_id - id of the response to grade
                   1056:   
                   1057: 
                   1058: =cut
                   1059: 
1.72      albertel 1060: sub scored_response {
                   1061:     my ($part,$id)=@_;
                   1062:     my $repetition=&repetition();
                   1063:     my $score=0;
                   1064:     for (my $i=0;$i<$repetition;$i++) {
1.125     albertel 1065: 	# A is 1, B is 2, etc. (get response return 0-9 and then we add 1)
1.72      albertel 1066: 	my $increase=&Apache::response::getresponse($i+1);
                   1067: 	if ($increase ne '') { $score+=$increase+1; }
                   1068:     }
                   1069:     my $weight = &Apache::lonnet::EXT("resource.$part.weight");
1.91      albertel 1070:     if (!defined($weight) || $weight eq '' || $weight eq 0) { $weight = 1; }
1.72      albertel 1071:     my $pcr=$score/$weight;
                   1072:     $Apache::lonhomework::results{"resource.$part.$id.awarded"}=$pcr;
                   1073:     $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
                   1074: 	'ASSIGNED_SCORE';
1.71      albertel 1075:     return $repetition;
1.78      albertel 1076: }
                   1077: 
                   1078: sub whichorder {
                   1079:     my ($max,$randomize,$showall,$hash)=@_;
                   1080:     #&Apache::lonxml::debug("man $max randomize $randomize");
                   1081:     if (!defined(@{ $$hash{'names'} })) { return; }
                   1082:     my @names = @{ $$hash{'names'} };
                   1083:     my @whichopt =();
                   1084:     my (%top,@toplist,%bottom,@bottomlist);
                   1085:     if (!($showall || ($randomize eq 'no'))) {
                   1086: 	my $current=0;
                   1087: 	foreach my $name (@names) {
                   1088: 	    $current++;
                   1089: 	    if ($$hash{"$name.location"} eq 'top') {
                   1090: 		$top{$name}=$current;
                   1091: 	    } elsif ($$hash{"$name.location"} eq 'bottom') {
                   1092: 		$bottom{$name}=$current;
                   1093: 	    }
                   1094: 	}
                   1095:     }
                   1096:     my $topcount=0;
                   1097:     my $bottomcount=0;
                   1098:     while (((scalar(@whichopt)+$topcount+$bottomcount) < $max || $showall)
                   1099: 	   && ($#names > -1)) {
                   1100: 	#&Apache::lonxml::debug("Have $#whichopt max is $max");
                   1101: 	my $aopt;
                   1102: 	if ($showall || ($randomize eq 'no')) {
                   1103: 	    $aopt=0;
                   1104: 	} else {
                   1105: 	    $aopt=int(&Math::Random::random_uniform() * ($#names+1));
                   1106: 	}
                   1107: 	#&Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
                   1108: 	$aopt=splice(@names,$aopt,1);
                   1109: 	#&Apache::lonxml::debug("Picked $aopt");
                   1110: 	if ($top{$aopt}) {
                   1111: 	    $toplist[$top{$aopt}]=$aopt;
                   1112: 	    $topcount++;
                   1113: 	} elsif ($bottom{$aopt}) {
                   1114: 	    $bottomlist[$bottom{$aopt}]=$aopt;
                   1115: 	    $bottomcount++;
                   1116: 	} else {
                   1117: 	    push (@whichopt,$aopt);
                   1118: 	}
                   1119:     }
                   1120:     for (my $i=0;$i<=$#toplist;$i++) {
                   1121: 	if ($toplist[$i]) { unshift(@whichopt,$toplist[$i]) }
                   1122:     }
                   1123:     for (my $i=0;$i<=$#bottomlist;$i++) {
                   1124: 	if ($bottomlist[$i]) { push(@whichopt,$bottomlist[$i]) }
                   1125:     }
                   1126:     return @whichopt;
1.71      albertel 1127: }
                   1128: 
1.85      albertel 1129: sub show_answer {
                   1130:     my $part   = $Apache::inputtags::part;
                   1131:     my $award  = $Apache::lonhomework::history{"resource.$part.solved"};
                   1132:     my $status = $Apache::inputtags::status[-1];
                   1133:     return  ( ($award =~ /^correct/
1.181     albertel 1134: 	       && &Apache::lonhomework::show_problem_status())
1.85      albertel 1135: 	      || $status eq "SHOW_ANSWER");
                   1136: }
1.87      albertel 1137: 
                   1138: sub analyze_store_foilgroup {
                   1139:     my ($shown,$attrs)=@_;
                   1140:     my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                   1141:     foreach my $name (@{ $Apache::response::foilgroup{'names'} }) {
                   1142: 	if (defined($Apache::lonhomework::analyze{"$part_id.foil.value.$name"})) { next; }
                   1143: 	push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },$name);
                   1144: 	foreach my $attr (@$attrs) {
                   1145: 	    $Apache::lonhomework::analyze{"$part_id.foil.".$attr.".$name"} =
                   1146: 		$Apache::response::foilgroup{"$name.".$attr};
                   1147: 	}
                   1148:     }
                   1149:     push (@{ $Apache::lonhomework::analyze{"$part_id.shown"} }, @{ $shown });
1.96      albertel 1150: }
                   1151: 
                   1152: sub check_if_computed {
                   1153:     my ($token,$parstack,$safeeval,$name)=@_;
                   1154:     my $value = &Apache::lonxml::get_param($name,$parstack,$safeeval);
1.106     matthew  1155:     if (ref($token->[2]) eq 'HASH' && $value ne $token->[2]{$name}) {
1.96      albertel 1156: 	my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                   1157: 	$Apache::lonhomework::analyze{"$part_id.answercomputed"} = 1;
                   1158:     }
1.87      albertel 1159: }
                   1160: 
                   1161: sub pick_foil_for_concept {
                   1162:     my ($target,$attrs,$hinthash,$parstack,$safeeval)=@_;
                   1163:     if (not defined(@{ $Apache::response::conceptgroup{'names'} })) { return; }
                   1164:     my @names = @{ $Apache::response::conceptgroup{'names'} };
                   1165:     my $pick=int(&Math::Random::random_uniform() * ($#names+1));
                   1166:     my $name=$names[$pick];
                   1167:     push @{ $Apache::response::foilgroup{'names'} }, $name;
                   1168:     foreach my $attr (@$attrs) {
                   1169: 	$Apache::response::foilgroup{"$name.".$attr} =
                   1170: 	    $Apache::response::conceptgroup{"$name.".$attr};
                   1171:     }
                   1172:     my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
                   1173:     $Apache::response::foilgroup{"$name.concept"} = $concept;
                   1174:     &Apache::lonxml::debug("Selecting $name in $concept");
                   1175:     my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                   1176:     if ($target eq 'analyze') {
                   1177: 	push (@{ $Apache::lonhomework::analyze{"$part_id.concepts"} },
                   1178: 	      $concept);
                   1179: 	$Apache::lonhomework::analyze{"$part_id.concept.$concept"}=
                   1180: 	    $Apache::response::conceptgroup{'names'};
                   1181: 	foreach my $name (@{ $Apache::response::conceptgroup{'names'} }) {
                   1182: 	    push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },
                   1183: 		  $name);
                   1184: 	    foreach my $attr (@$attrs) {
                   1185: 		$Apache::lonhomework::analyze{"$part_id.foil.$attr.$name"}=
                   1186: 		    $Apache::response::conceptgroup{"$name.$attr"};
                   1187: 	    }
                   1188: 	}
                   1189:     }
                   1190:     push(@{ $hinthash->{"$part_id.concepts"} },$concept);
                   1191:     $hinthash->{"$part_id.concept.$concept"}=
                   1192: 	$Apache::response::conceptgroup{'names'};
                   1193: 
                   1194: }
1.208     jms      1195: 
                   1196: =pod
                   1197: 
                   1198: =item get_response_param()
                   1199: 
                   1200: Get a parameter associated with a problem.
                   1201: Parameters:
                   1202: 	$id        - the id of the paramater, either a part id, 
                   1203:               or a partid and responspe id joined by _
                   1204: 	$name      - Name of the parameter to fetch
                   1205: 	$default   - Default value for the paramter.
                   1206: 
                   1207: =cut
                   1208: 
1.95      albertel 1209: sub get_response_param {
                   1210:     my ($id,$name,$default)=@_;
                   1211:     my $parameter;
1.120     albertel 1212:     if ($env{'request.state'} eq 'construct' &&
1.95      albertel 1213: 	defined($Apache::inputtags::params{$name})) {
                   1214: 	$parameter=$Apache::inputtags::params{$name};
                   1215:     } else {
                   1216: 	$parameter=&Apache::lonnet::EXT("resource.$id.$name");
                   1217:     }
                   1218:     if (!defined($parameter) ||	$parameter eq '') {
                   1219: 	$parameter = $default;
                   1220:     }
                   1221:     return $parameter;
                   1222: }
1.87      albertel 1223: 
1.113     albertel 1224: sub submitted {
                   1225:     my ($who)=@_;
                   1226:     
                   1227:     # when scatron grading any submission is a submission
1.120     albertel 1228:     if ($env{'form.submitted'} eq 'scantron') { return 1; }
1.113     albertel 1229:     # if the caller only cared if this was a scantron submission
                   1230:     if ($who eq 'scantron') { return 0; }
                   1231:     # if the Submit Answer button for this particular part was pressed
                   1232:     my $partid=$Apache::inputtags::part;
1.159     albertel 1233:     if ($env{'form.submitted'} eq "part_$partid") {
                   1234: 	return 1;
                   1235:     }
                   1236:     if ($env{'form.submitted'} eq "yes"
                   1237: 	&& defined($env{'form.submit_'.$partid})) {
                   1238: 	return 1;
                   1239:     }
1.122     albertel 1240:     # Submit All button on a .page was pressed
                   1241:     if (defined($env{'form.all_submit'})) { return 1; }
1.204     bisitz   1242:     # otherwise no submission occurred
1.113     albertel 1243:     return 0;
                   1244: }
1.117     albertel 1245: 
1.130     albertel 1246: sub add_to_gradingqueue {
1.149     albertel 1247:     my ($symb,$courseid,$domain,$name) = &Apache::lonnet::whichuser();
1.131     albertel 1248:     if (   $courseid eq ''
                   1249: 	|| $symb eq ''
1.135     albertel 1250: 	|| $env{'request.state'} eq 'construct'
                   1251: 	|| $Apache::lonhomework::type ne 'problem') {
1.131     albertel 1252: 	return;
                   1253:     }
1.130     albertel 1254: 
                   1255:     my %queue_info = ( 'type' => 'problem',
                   1256: 		       'time' => time);
                   1257: 
                   1258:     if (exists($Apache::lonhomework::history{"resource.0.checkedin.slot"})) {
                   1259: 	$queue_info{'slot'}=
                   1260: 	     $Apache::lonhomework::history{"resource.0.checkedin.slot"};
                   1261:     }
                   1262: 
                   1263:     my $result=&Apache::bridgetask::add_to_queue('gradingqueue',\%queue_info);
                   1264:     if ($result ne 'ok') {
                   1265: 	&Apache::lonxml::error("add_to_queue said $result");
                   1266:     }
                   1267: }
                   1268: 
1.208     jms      1269: =pod 
                   1270: 
                   1271: =item check_status()
                   1272: 
                   1273: basically undef and 0 (both false) mean that they still have work to do
                   1274: and all true values mean that they can't do any more work
                   1275: 
                   1276: 	a return of undef means it is unattempted
                   1277: 	a return of 0 means it is attmpted and wrong but still has tries
                   1278: 	a return of 1 means it is marked correct
                   1279: 	a return of 2 means they have exceed maximum number of tries
                   1280: 	a return of 3 means it after the answer date
                   1281: 
                   1282: =cut
                   1283: 
1.117     albertel 1284: sub check_status {
                   1285:     my ($id)=@_;
1.143     albertel 1286:     if (!defined($id)) { $id=$Apache::inputtags::part; }
1.117     albertel 1287:     my $curtime=&Apache::lonnet::EXT('system.time');
                   1288:     my $opendate=&Apache::lonnet::EXT("resource.$id.opendate");
                   1289:     my $duedate=&Apache::lonnet::EXT("resource.$id.duedate");
                   1290:     my $answerdate=&Apache::lonnet::EXT("resource.$id.answerdate");
                   1291:     if ( $opendate && $curtime > $opendate &&
                   1292:          $duedate && $curtime > $duedate &&
                   1293:          $answerdate && $curtime > $answerdate) {
                   1294:         return 3;
                   1295:     }
                   1296:     my $status=&Apache::lonnet::EXT("user.resource.resource.$id.solved");
                   1297:     if ($status =~ /^correct/) { return 1; }
                   1298:     if (!$status) { return undef; }
                   1299:     my $maxtries=&Apache::lonnet::EXT("resource.$id.maxtries");
                   1300:     if ($maxtries eq '') { $maxtries=2; }
                   1301:     my $curtries=&Apache::lonnet::EXT("user.resource.resource.$id.tries");
                   1302:     if ($curtries < $maxtries) { return 0; }
                   1303:     return 2;
                   1304: }
                   1305: 
1.177     albertel 1306: =pod
                   1307: 
1.210     raeburn  1308: =item setup_prior_tries_hash()
1.177     albertel 1309: 
                   1310:   Foreach each past .submission $func is called with 3 arguments
                   1311:      - the mode to set things up for (currently always 'grade')
                   1312:      - the stored .submission string
                   1313:      - The expansion of $data
                   1314: 
                   1315:   $data is an array ref containing elements that are either
                   1316:     - scalars that are other elements of the history hash to pass to $func
                   1317:     - ref to data to be passed untouched to $func
                   1318: 
                   1319: =cut
                   1320: 
1.162     albertel 1321: sub setup_prior_tries_hash {
                   1322:     my ($func,$data) = @_;
                   1323:     my $part = $Apache::inputtags::part;
                   1324:     my $id   = $Apache::inputtags::response[-1];	
                   1325:     foreach my $i (1..$Apache::lonhomework::history{'version'}) {
                   1326: 	my $sub_key   = "$i:resource.$part.$id.submission";
                   1327: 	next if (!exists($Apache::lonhomework::history{$sub_key}));
                   1328: 	my @other_data;
                   1329: 	foreach my $datum (@{ $data }) {
                   1330: 	    if (ref($datum)) {
                   1331: 		push(@other_data,$datum);
                   1332: 	    } else {
                   1333: 		my $info_key = "$i:resource.$part.$id.$datum";
                   1334: 		push(@other_data,$Apache::lonhomework::history{$info_key});
                   1335: 	    }
                   1336: 	}
                   1337: 
                   1338: 	my $output =
                   1339: 	    &$func('grade',
                   1340: 		   $Apache::lonhomework::history{$sub_key},
                   1341: 		   \@other_data);
                   1342: 	if (defined($output)) {
                   1343: 	    $Apache::inputtags::submission_display{$sub_key} = $output;
                   1344: 	}
                   1345:     }
                   1346: }
                   1347: 
1.1       albertel 1348: 1;
                   1349: __END__
1.38      albertel 1350:  
1.208     jms      1351: =pod
                   1352: 
1.209     www      1353: =cut

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