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

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

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