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

1.38      albertel    1: # The LearningOnline Network with CAPA
1.1       albertel    2: # various response type definitons response definition
1.53      albertel    3: #
1.88    ! albertel    4: # $Id: response.pm,v 1.87 2003/10/27 19:27:09 albertel Exp $
1.53      albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.5       www        28: 
1.1       albertel   29: package Apache::response;
                     30: use strict;
                     31: 
1.57      harris41   32: BEGIN {
1.73      albertel   33:     &Apache::lonxml::register('Apache::response',('responseparam','parameter','dataresponse'));
1.1       albertel   34: }
                     35: 
1.13      albertel   36: sub start_response {
1.73      albertel   37:     my ($parstack,$safeeval)=@_;
                     38:     my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
                     39:     if ($id eq '') { $id = $Apache::lonxml::curdepth; }
                     40:     if ($#Apache::inputtags::import > -1) {
                     41: 	&Apache::lonxml::debug("Turning :$id: into");
                     42: 	$id = join('_',@Apache::inputtags::import).'_'.$id;
                     43: 	&Apache::lonxml::debug("New  :$id:");
                     44:     }
                     45:     push (@Apache::inputtags::response,$id);
                     46:     push (@Apache::inputtags::responselist,$id);
                     47:     @Apache::inputtags::inputlist=();
                     48:     return $id;
1.13      albertel   49: }
                     50: 
                     51: sub end_response {
1.79      albertel   52:     #pop @Apache::inputtags::response;
1.73      albertel   53:     @Apache::inputtags::inputlist=();
                     54:     return '';
1.13      albertel   55: }
                     56: 
1.41      albertel   57: sub start_hintresponse {
1.73      albertel   58:     my ($parstack,$safeeval)=@_;
                     59:     my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
                     60:     if ($id eq '') { $id = $Apache::lonxml::curdepth; }
                     61:     push (@Apache::inputtags::response,$id);
1.79      albertel   62:     push (@Apache::inputtags::responselist,$id);
1.73      albertel   63:     push (@Apache::inputtags::paramstack,[%Apache::inputtags::params]);
                     64:     return $id;
1.41      albertel   65: }
                     66: 
                     67: sub end_hintresponse {
1.73      albertel   68:     pop @Apache::inputtags::response;
                     69:     if (defined($Apache::inputtags::paramstack[-1])) {
                     70: 	%Apache::inputtags::params=
                     71: 	    @{ pop(@Apache::inputtags::paramstack) };
                     72:     }
                     73:     return '';
1.41      albertel   74: }
                     75: 
1.38      albertel   76: # used by response to set the non-safe space random number generator to something
                     77: # that is stable and unique based on the part number and response number
1.26      albertel   78: sub setrandomnumber {
1.73      albertel   79:     my $rndseed;
1.88    ! albertel   80:     $rndseed=&Apache::structuretags::setup_rndseed();
        !            81:     if (!defined($rndseed)) { $rndseed=&Apache::lonnet::rndseed(); }
1.73      albertel   82:     &Apache::lonxml::debug("randseed $rndseed");
                     83:     #  $rndseed=unpack("%32i",$rndseed);
1.74      albertel   84:     my $rndmod=(&Apache::lonnet::numval($Apache::inputtags::part) << 10);
1.73      albertel   85:     if (defined($Apache::inputtags::response['-1'])) {
1.88    ! albertel   86: 	$rndmod+=&Apache::lonnet::numval($Apache::inputtags::response[-1]);
1.73      albertel   87:     }
1.74      albertel   88:     if ($rndseed =~/,/) {
1.82      albertel   89: 	{
                     90: 	    use integer;
                     91: 	    my ($num1,$num2)=split(/,/,$rndseed);
                     92: 	    $num1+=$rndmod;
                     93: 	    $num2+=$rndmod;
                     94: 	    $rndseed="$num1,$num2";
                     95: 	}
1.74      albertel   96:     } else {
                     97: 	$rndseed+=$rndmod;
                     98:     }
                     99:     &Apache::lonnet::setup_random_from_rndseed($rndseed);
1.73      albertel  100:     &Apache::lonxml::debug("randseed $rndseed");
                    101:     return '';
1.26      albertel  102: }
                    103: 
1.7       www       104: sub meta_parameter_write {
1.38      albertel  105:     my ($name,$type,$default,$display)=@_;
1.41      albertel  106:     my $partref=$Apache::inputtags::part;
                    107:     my $result='<parameter part="'.$Apache::inputtags::part.'"';
                    108:     if (defined($Apache::inputtags::response[-1])) {
1.73      albertel  109: 	$result.=            ' id="'.$Apache::inputtags::response[-1].'"';
                    110: 	$partref.='_'.$Apache::inputtags::response[-1];
1.41      albertel  111:     }
                    112:     $result.=            ' name="'.$name.'"'.
                    113:                          ' type="'.$type.'"'.
                    114: ($default?' default="'.$default.'"':'').
                    115: ($display?' display="'.$display.' [Part: '.$partref.']"':'')
                    116:              .'></parameter>'
                    117:              ."\n";
                    118:     return $result;
1.33      www       119: }
                    120: 
                    121: sub meta_package_write {
                    122:     my $name=shift;
1.41      albertel  123:     my $result = '<parameter part="'.$Apache::inputtags::part.'"';
                    124:     if(defined($Apache::inputtags::response[-1])) {
1.73      albertel  125: 	$result.= ' id="'.$Apache::inputtags::response[-1].'"';
1.41      albertel  126:     }
                    127:     $result.=' package="'.$name.'"></parameter>'."\n";
                    128:     return $result;
1.7       www       129: }
                    130: 
                    131: sub meta_stores_write {
1.10      www       132:     my ($name,$type,$display)=@_;
1.41      albertel  133:     my $partref=$Apache::inputtags::part;
                    134:     my $result = '<stores part="'.$Apache::inputtags::part.'"';
                    135:     if (defined($Apache::inputtags::response[-1])) {
1.73      albertel  136: 	$result.=           ' id="'.$Apache::inputtags::response[-1].'"';
                    137: 	$partref.='_'.$Apache::inputtags::response[-1];
1.41      albertel  138:     }	
                    139:     $result.=          ' name="'.$name.'"'.
                    140:                        ' type="'.$type.'"'.
                    141: 	            ' display="'.$display.' [Part: '.$partref.']"'.
                    142: 		      "></stores>\n";
1.7       www       143: }
                    144: 
                    145: sub mandatory_part_meta {
                    146: #
                    147: # Autogenerate metadata for mandatory
                    148: # input (from RAT or lonparmset) and 
                    149: # output (to lonspreadsheet)
                    150: # of each part
                    151: #
1.73      albertel  152:     return
1.34      www       153: #    &meta_parameter_write('opendate','date_start','',
                    154: #                          'Opening Date').
                    155: #    &meta_parameter_write('duedate','date_end','',
                    156: #                          'Due Date').
                    157: #    &meta_parameter_write('answerdate','date_start','',
                    158: #                          'Show Answer Date').
                    159: #    &meta_parameter_write('weight','int_zeropos','',
                    160: #                          'Available Points').
                    161: #    &meta_parameter_write('maxtries','int_pos','',
                    162: #                          'Maximum Number of Tries').
1.73      albertel  163: 	&meta_package_write('part').
                    164:         &meta_stores_write('solved','string',
                    165: 			   'Problem Status').
                    166:         &meta_stores_write('tries','int_zeropos',
                    167: 			   'Number of Attempts').
                    168:         &meta_stores_write('awarded','float',
                    169: 			   'Partial Credit Factor');
1.7       www       170: #
                    171: # Note: responseid-specific data 'submission' and 'awarddetail'
                    172: # not available to spreadsheet -> skip here
                    173: #
1.86      albertel  174: }
                    175: 
                    176: sub meta_part_order {
                    177:     if (@Apache::inputtags::partlist) {
                    178: 	my @parts=@Apache::inputtags::partlist;
                    179: 	shift(@parts);
                    180: 	return '<partorder>'.join(',',@parts).'</partorder>';
                    181:     } else {
                    182: 	return '<partorder>0</partorder>';
                    183:     }
1.14      albertel  184: }
                    185: 
1.15      albertel  186: sub check_for_previous {
1.73      albertel  187:     my ($curresponse,$partid,$id) = @_;
                    188:     my %previous;
                    189:     $previous{'used'} = 0;
                    190:     foreach my $key (sort(keys(%Apache::lonhomework::history))) {
                    191: 	if ($key =~ /resource\.$partid\.$id\.submission/) {
                    192: 	    &Apache::lonxml::debug("Trying $key");
                    193: 	    my $pastresponse=$Apache::lonhomework::history{$key};
                    194: 	    if ($pastresponse eq $curresponse) {
                    195: 		$previous{'used'} = 1;
                    196: 		my $history;
                    197: 		if ( $key =~ /^(\d+):/ ) {
                    198: 		    $history=$1;
                    199: 		    $previous{'award'} = $Apache::lonhomework::history{"$history:resource.$partid.$id.awarddetail"};
                    200: 		    $previous{'last'}='0';
                    201: 		    push(@{ $previous{'version'} },$history);
                    202: 		} else {
                    203: 		    $previous{'award'} = $Apache::lonhomework::history{"resource.$partid.$id.awarddetail"};
                    204: 		    $previous{'last'}='1';
                    205: 		}
                    206: 		if (! $previous{'award'} ) { $previous{'award'} = 'UNKNOWN';	}
                    207: 		&Apache::lonxml::debug("got a match :$previous{'award'}:$previous{'used'}:");
                    208: 	    }
1.32      albertel  209: 	}
1.73      albertel  210:     }
                    211:     &Apache::lonhomework::showhash(%previous);
                    212:     return %previous;
1.54      albertel  213: }
                    214: 
                    215: sub handle_previous {
1.73      albertel  216:     my ($previous,$ad)=@_;
                    217:     if ($$previous{'used'} && ($$previous{'award'} eq $ad) ) {
                    218: 	if ($$previous{'last'}) {
                    219: 	    push(@Apache::inputtags::previous,'PREVIOUSLY_LAST');
                    220: 	} else {
                    221: 	    push(@Apache::inputtags::previous,'PREVIOUSLY_USED');
                    222: 	}
                    223: 	push(@Apache::inputtags::previous_version,$$previous{'version'});
1.54      albertel  224:     }
1.44      albertel  225: }
                    226: 
1.45      albertel  227: sub view_or_modify {
1.73      albertel  228:     my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
                    229:     my $myself=0;
                    230:     if ( ($name eq $ENV{'user.name'}) && ($domain eq $ENV{'user.domain'}) ) {
                    231: 	$myself=1;
                    232:     }
                    233:     my $vgr=&Apache::lonnet::allowed('vgr',$courseid);
                    234:     my $mgr=&Apache::lonnet::allowed('vgr',$courseid);
                    235:     if ($mgr) { return "M"; }
                    236:     if ($vgr) { return "V"; }
                    237:     if ($myself) { return "V"; }
                    238:     return '';
1.45      albertel  239: }
                    240: 
1.44      albertel  241: sub start_dataresponse {
1.73      albertel  242:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    243:     my $id = &Apache::response::start_response($parstack,$safeeval);
                    244:     my $result;
                    245:     if ($target eq 'web') {
                    246: 	$result = $token->[2]->{'display'}.':';
                    247:     } elsif ($target eq 'meta') {
                    248: 	$result = &Apache::response::meta_stores_write($token->[2]->{'name'},
                    249: 						       $token->[2]->{'type'},
                    250: 						       $token->[2]->{'display'});
                    251: 	$result .= &Apache::response::meta_package_write('dataresponse');
                    252:     }
                    253:     return $result;
1.44      albertel  254: }
                    255: 
                    256: sub end_dataresponse {
1.73      albertel  257:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    258:     my $result;
                    259:     if ( $target eq 'web' ) {
                    260:     } elsif ($target eq 'grade' ) {
                    261: 	if ( defined $ENV{'form.submitted'}) {
                    262: 	    my ($symb,$courseid,$domain,$name)=&Apache::lonxml::whichuser();
                    263: 	    my $allowed=&Apache::lonnet::allowed('mgr',$courseid);
                    264: 	    if ($allowed) {
                    265: 		&Apache::response::setup_params('dataresponse');
                    266: 		my $partid = $Apache::inputtags::part;
                    267: 		my $id = $Apache::inputtags::response['-1'];
                    268: 		my $response = $ENV{'form.HWVAL_'.$id};
                    269: 		my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    270: 		if ( $response =~ /[^\s]/) {
                    271: 		    $Apache::lonhomework::results{"resource.$partid.$id.$name"}=$response;
                    272: 		    $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
                    273: 		    $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}='SUBMITTED';
                    274: 		}
                    275: 	    } else {
                    276: 		$result='Not Permitted to change values.'
                    277: 	    }
1.45      albertel  278: 	}
1.73      albertel  279:     }
                    280:     &Apache::response::end_response;
                    281:     return $result;
1.3       albertel  282: }
                    283: 
1.83      albertel  284: sub decide_package {
                    285:     my ($tagstack)=@_;
                    286:     my $package;
                    287:     if ($$tagstack[-1] eq 'parameter') {
                    288: 	$package='part';
                    289:     } else {
                    290: 	my $i=-1;
                    291: 	while (defined($$tagstack[$i])) {
                    292: 	    if ($$tagstack[$i] =~ /(response|hint)$/) {
                    293: 		$package=$$tagstack[$i];
                    294: 		last;
                    295: 	    }
                    296: 	    $i--;
                    297: 	}
                    298:     }
                    299:     return $package;
                    300: }
                    301: 
1.3       albertel  302: sub start_responseparam {
1.73      albertel  303:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    304:     my $result='';
                    305:     if ($target eq 'meta') {
                    306: 	$result = &meta_parameter_write($token->[2]->{'name'},
                    307: 					$token->[2]->{'type'},
                    308: 					$token->[2]->{'default'},
                    309: 					$token->[2]->{'description'});
                    310:     } elsif ($target eq 'edit') {
                    311: 	$result.=&Apache::edit::tag_start($target,$token);
1.83      albertel  312: 	my $optionlist;
                    313: 	my $package=&decide_package($tagstack);
                    314: 	foreach my $key (sort(keys(%Apache::lonnet::packagetab))) {
                    315: 	    if ($key =~ /^\Q$package\E&(.*)&display$/) {
                    316: 		$optionlist.='<option value="'.$1.'">'.
                    317: 		    $Apache::lonnet::packagetab{$key}.'</option>';
                    318: 	    }
                    319: 	}
                    320: 	if (defined($optionlist)) {
                    321: 	    $result.='Use template: <select name="'.
                    322: 		&Apache::edit::html_element_name('parameter_package').'">'.
                    323: 		    '<option value=""></option>'.$optionlist.'</select><br />';
                    324: 	}
1.73      albertel  325: 	$result.=&Apache::edit::text_arg('Name:','name',$token).
                    326: 	    &Apache::edit::text_arg('Type:','type',$token).
                    327: 		&Apache::edit::text_arg('Description:','description',$token).
                    328: 		    &Apache::edit::text_arg('Default:','default',$token).
                    329: 			"</td></tr>";
                    330: 	$result.=&Apache::edit::end_table;
                    331:     } elsif ($target eq 'modified') {
1.83      albertel  332: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    333: 						     $safeeval,'name','type',
                    334: 						     'description','default');
                    335: 	my $element=&Apache::edit::html_element_name('parameter_package');
                    336: 	if (defined($ENV{"form.$element"}) && $ENV{"form.$element"} ne '') {
                    337: 	    my $name=$ENV{"form.$element"};
                    338: 	    my $tag=&decide_package($tagstack);
                    339: 	    $token->[2]->{'name'}=$name;
                    340: 	    $token->[2]->{'type'}=
                    341: 		$Apache::lonnet::packagetab{"$tag&$name&type"};
                    342: 	    $token->[2]->{'description'}=
                    343: 		$Apache::lonnet::packagetab{"$tag&$name&display"};
                    344: 	    $token->[2]->{'default'}=
                    345: 		$Apache::lonnet::packagetab{"$tag&$name&default"};
                    346: 	    $constructtag=1;
                    347: 	}
1.73      albertel  348: 	if ($constructtag) {
                    349: 	    $result = &Apache::edit::rebuild_tag($token);
                    350: 	    $result.=&Apache::edit::handle_insert();
                    351: 	}
                    352:     } elsif ($target eq 'grade' || $target eq 'answer' || $target eq 'web' ||
                    353: 	     $target eq 'tex' || $target eq 'analyze' ) {
                    354: 	if ($ENV{'request.state'} eq 'construct') {
                    355: 	    my $name   =&Apache::lonxml::get_param('name',$parstack,$safeeval);
                    356: 	    my $default=&Apache::lonxml::get_param('default',$parstack,
                    357: 						     $safeeval);
                    358: 	    if ($name) {$Apache::inputtags::params{$name}=$default;}
                    359: 	}
1.52      albertel  360:     }
1.73      albertel  361:     return $result;
1.3       albertel  362: }
                    363: 
                    364: sub end_responseparam {
1.73      albertel  365:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    366:     if ($target eq 'edit') { return ('','no'); }
                    367:     return '';
1.55      albertel  368: }
                    369: 
                    370: sub start_parameter {
1.73      albertel  371:     my $result = &start_responseparam(@_);
                    372:     return $result;
1.55      albertel  373: }
                    374: 
                    375: sub end_parameter {
1.73      albertel  376:     my $result = &end_responseparam(@_);
                    377:     return $result;
1.42      albertel  378: }
                    379: 
1.67      albertel  380: sub reset_params {
                    381:     %Apache::inputtags::params=();
                    382: }
                    383: 
1.42      albertel  384: sub setup_params {
1.73      albertel  385:     my ($tag) = @_;
1.42      albertel  386: 
1.73      albertel  387:     if ($ENV{'request.state'} eq 'construct') { return; }
                    388:     my %paramlist=();
                    389:     foreach my $key (keys(%Apache::lonnet::packagetab)) {
                    390: 	if ($key =~ /^$tag/) {
                    391: 	    my ($package,$name) = split(/&/,$key);
                    392: 	    $paramlist{$name}=1;
                    393: 	}
1.42      albertel  394:     }
1.73      albertel  395:     foreach my $key (keys(%paramlist)) {
                    396: 	my $entry= 'resource.'.$Apache::inputtags::part;
                    397: 	if (defined($Apache::inputtags::response[-1])) {
                    398: 	    $entry.='_'.$Apache::inputtags::response[-1];
                    399: 	}
                    400: 	$entry.='.'.$key;
                    401: 	&Apache::lonxml::debug("looking for $entry");
                    402: 	my $value = &Apache::lonnet::EXT("$entry");
                    403: 	&Apache::lonxml::debug("$key has value :$value:");
                    404: 	if ($value eq 'con_lost' || $value =~ /^error:/) {
                    405: 	    &Apache::lonxml::debug("using nothing");
                    406: 	    $Apache::inputtags::params{$key}='';
                    407: 	} else {
                    408: 	    &Apache::lonxml::debug("using value");
                    409: 	    $Apache::inputtags::params{$key}=$value;
                    410: 	}
1.42      albertel  411:     }
1.48      albertel  412: }
                    413: 
                    414: sub answer_header {
1.73      albertel  415:     my ($type) = @_;
                    416:     my $result;
1.77      albertel  417:     if ($ENV{'form.answer_output_mode'} eq 'tex') {
1.84      sakharuk  418: 	$result = ' \vskip 0 mm \begin{tabular}{|c|}\hline Answer for Part: \verb|'.
                    419:                   $Apache::inputtags::part.'| \\\\ \hline ';
1.73      albertel  420:     } else {
1.80      albertel  421: 	$result = '<table border="1"><tr><td>Answer for Part:'.
                    422: 	    $Apache::inputtags::part. '</td>'."\n";
1.73      albertel  423:     }
                    424:     return $result;
1.48      albertel  425: }
                    426: 
                    427: sub answer_part {
1.73      albertel  428:     my ($type,$answer) = @_;
                    429:     my $result;
1.77      albertel  430:     if ($ENV{'form.answer_output_mode'} eq 'tex') {
1.81      sakharuk  431: 	$result = ' \verb|'.$answer.'|\\\\ \hline ';
1.73      albertel  432:     } else {
1.80      albertel  433: 	$result = '<td>'.$answer.'</td>';
1.73      albertel  434:     }
                    435:     return $result;
1.48      albertel  436: }
                    437: 
                    438: sub answer_footer {
1.73      albertel  439:     my ($type) = @_;
                    440:     my $result;
1.77      albertel  441:     if ($ENV{'form.answer_output_mode'} eq 'tex') {
1.75      sakharuk  442: 	$result = ' \end{tabular} \vskip 0 mm ';
1.73      albertel  443:     } else {
1.80      albertel  444: 	$result = '</tr></table>';
1.73      albertel  445:     }
                    446:     return $result;
1.1       albertel  447: }
1.2       albertel  448: 
1.62      albertel  449: sub showallfoils {
1.73      albertel  450:     my $return=0;
                    451:     if (defined($ENV{'form.showallfoils'}) &&
                    452: 	$ENV{'request.state'} eq 'construct') {
                    453: 	$return=1;
                    454:     }
                    455:     return $return;
1.70      albertel  456: }
                    457: 
                    458: sub getresponse {
                    459:     my ($temp)=@_;
                    460:     my $formparm='form.HWVAL_'.$Apache::inputtags::response['-1'];
                    461:     my $response;
                    462:     if (!defined($temp)) {
                    463: 	$temp=1;
                    464:     } else {
                    465: 	$formparm.=":$temp";
                    466:     }
                    467:     my %let_to_num=('A'=>0,'B'=>1,'C'=>2,'D'=>3,'E'=>4,'F'=>5,'G'=>6,'H'=>7,
                    468: 		    'I'=>8,'J'=>9,'K'=>10,'L'=>11,'M'=>12,'N'=>13,'O'=>14,
                    469: 		    'P'=>15,'Q'=>16,'R'=>17,'S'=>18,'T'=>19,'U'=>20,'V'=>21,
                    470: 		    'W'=>22,'X'=>23,'Y'=>24,'Z'=>25);
                    471:     if ($ENV{'form.submitted'} eq 'scantron') {
1.71      albertel  472: 	my $part  = $Apache::inputtags::part;
                    473: 	my $id    = $Apache::inputtags::response[-1];
1.70      albertel  474: 	$response = $ENV{'scantron.'.($Apache::lonxml::counter+$temp-1).
                    475: 			 '.answer'};
1.71      albertel  476: 	# save bubbled letter for later
                    477: 	$Apache::lonhomework::results{"resource.$part.$id.scantron"}.=
                    478: 	    $response;
1.70      albertel  479: 	$response = $let_to_num{$response};
                    480:     } else {
                    481: 	$response = $ENV{$formparm};
                    482:     }
                    483:     return $response;
1.62      albertel  484: }
1.71      albertel  485: 
                    486: sub repetition {
                    487:     my $id = $Apache::inputtags::part;
                    488:     my $weight = &Apache::lonnet::EXT("resource.$id.weight");
                    489:     my $repetition = int $weight/9;
                    490:     if ($weight % 9 != 0) {$repetition++;} 
1.72      albertel  491:     return $repetition;
                    492: }
                    493: 
                    494: sub scored_response {
                    495:     my ($part,$id)=@_;
                    496:     my $repetition=&repetition();
                    497:     my $score=0;
                    498:     for (my $i=0;$i<$repetition;$i++) {
                    499: 	my $increase=&Apache::response::getresponse($i+1);
                    500: 	if ($increase ne '') { $score+=$increase+1; }
                    501:     }
                    502:     my $weight = &Apache::lonnet::EXT("resource.$part.weight");
                    503:     my $pcr=$score/$weight;
                    504:     $Apache::lonhomework::results{"resource.$part.$id.awarded"}=$pcr;
                    505:     $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
                    506: 	'ASSIGNED_SCORE';
1.71      albertel  507:     return $repetition;
1.78      albertel  508: }
                    509: 
                    510: sub whichorder {
                    511:     my ($max,$randomize,$showall,$hash)=@_;
                    512:     #&Apache::lonxml::debug("man $max randomize $randomize");
                    513:     if (!defined(@{ $$hash{'names'} })) { return; }
                    514:     my @names = @{ $$hash{'names'} };
                    515:     my @whichopt =();
                    516:     my (%top,@toplist,%bottom,@bottomlist);
                    517:     if (!($showall || ($randomize eq 'no'))) {
                    518: 	my $current=0;
                    519: 	foreach my $name (@names) {
                    520: 	    $current++;
                    521: 	    if ($$hash{"$name.location"} eq 'top') {
                    522: 		$top{$name}=$current;
                    523: 	    } elsif ($$hash{"$name.location"} eq 'bottom') {
                    524: 		$bottom{$name}=$current;
                    525: 	    }
                    526: 	}
                    527:     }
                    528:     my $topcount=0;
                    529:     my $bottomcount=0;
                    530:     while (((scalar(@whichopt)+$topcount+$bottomcount) < $max || $showall)
                    531: 	   && ($#names > -1)) {
                    532: 	#&Apache::lonxml::debug("Have $#whichopt max is $max");
                    533: 	my $aopt;
                    534: 	if ($showall || ($randomize eq 'no')) {
                    535: 	    $aopt=0;
                    536: 	} else {
                    537: 	    $aopt=int(&Math::Random::random_uniform() * ($#names+1));
                    538: 	}
                    539: 	#&Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
                    540: 	$aopt=splice(@names,$aopt,1);
                    541: 	#&Apache::lonxml::debug("Picked $aopt");
                    542: 	if ($top{$aopt}) {
                    543: 	    $toplist[$top{$aopt}]=$aopt;
                    544: 	    $topcount++;
                    545: 	} elsif ($bottom{$aopt}) {
                    546: 	    $bottomlist[$bottom{$aopt}]=$aopt;
                    547: 	    $bottomcount++;
                    548: 	} else {
                    549: 	    push (@whichopt,$aopt);
                    550: 	}
                    551:     }
                    552:     for (my $i=0;$i<=$#toplist;$i++) {
                    553: 	if ($toplist[$i]) { unshift(@whichopt,$toplist[$i]) }
                    554:     }
                    555:     for (my $i=0;$i<=$#bottomlist;$i++) {
                    556: 	if ($bottomlist[$i]) { push(@whichopt,$bottomlist[$i]) }
                    557:     }
                    558: 
                    559:     return @whichopt;
1.71      albertel  560: }
                    561: 
1.85      albertel  562: sub show_answer {
                    563:     my $part   = $Apache::inputtags::part;
                    564:     my $award  = $Apache::lonhomework::history{"resource.$part.solved"};
                    565:     my $status = $Apache::inputtags::status[-1];
                    566:     return  ( ($award =~ /^correct/
                    567: 	       && lc($Apache::lonhomework::problemstatus) ne 'no')
                    568: 	      || $status eq "SHOW_ANSWER");
                    569: }
1.87      albertel  570: 
                    571: sub analyze_store_foilgroup {
                    572:     my ($shown,$attrs)=@_;
                    573:     my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                    574:     foreach my $name (@{ $Apache::response::foilgroup{'names'} }) {
                    575: 	if (defined($Apache::lonhomework::analyze{"$part_id.foil.value.$name"})) { next; }
                    576: 	push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },$name);
                    577: 	foreach my $attr (@$attrs) {
                    578: 	    $Apache::lonhomework::analyze{"$part_id.foil.".$attr.".$name"} =
                    579: 		$Apache::response::foilgroup{"$name.".$attr};
                    580: 	}
                    581:     }
                    582:     push (@{ $Apache::lonhomework::analyze{"$part_id.shown"} }, @{ $shown });
                    583: }
                    584: 
                    585: sub pick_foil_for_concept {
                    586:     my ($target,$attrs,$hinthash,$parstack,$safeeval)=@_;
                    587:     if (not defined(@{ $Apache::response::conceptgroup{'names'} })) { return; }
                    588:     my @names = @{ $Apache::response::conceptgroup{'names'} };
                    589:     my $pick=int(&Math::Random::random_uniform() * ($#names+1));
                    590:     my $name=$names[$pick];
                    591:     push @{ $Apache::response::foilgroup{'names'} }, $name;
                    592:     foreach my $attr (@$attrs) {
                    593: 	$Apache::response::foilgroup{"$name.".$attr} =
                    594: 	    $Apache::response::conceptgroup{"$name.".$attr};
                    595:     }
                    596:     my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
                    597:     $Apache::response::foilgroup{"$name.concept"} = $concept;
                    598:     &Apache::lonxml::debug("Selecting $name in $concept");
                    599:     my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                    600:     if ($target eq 'analyze') {
                    601: 	push (@{ $Apache::lonhomework::analyze{"$part_id.concepts"} },
                    602: 	      $concept);
                    603: 	$Apache::lonhomework::analyze{"$part_id.concept.$concept"}=
                    604: 	    $Apache::response::conceptgroup{'names'};
                    605: 	foreach my $name (@{ $Apache::response::conceptgroup{'names'} }) {
                    606: 	    push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },
                    607: 		  $name);
                    608: 	    foreach my $attr (@$attrs) {
                    609: 		$Apache::lonhomework::analyze{"$part_id.foil.$attr.$name"}=
                    610: 		    $Apache::response::conceptgroup{"$name.$attr"};
                    611: 	    }
                    612: 	}
                    613:     }
                    614:     push(@{ $hinthash->{"$part_id.concepts"} },$concept);
                    615:     $hinthash->{"$part_id.concept.$concept"}=
                    616: 	$Apache::response::conceptgroup{'names'};
                    617: 
                    618: }
                    619: 
                    620: 
1.1       albertel  621: 1;
                    622: __END__
1.38      albertel  623:  

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