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

1.38      albertel    1: # The LearningOnline Network with CAPA
1.1       albertel    2: # various response type definitons response definition
1.53      albertel    3: #
1.73    ! albertel    4: # $Id: response.pm,v 1.72 2003/04/21 20:59:02 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.7       www        28: # 11/23,11/24,11/28 Gerd Kortemeyer
1.38      albertel   29: # Guy Albertelli
1.39      www        30: # 08/04,08/07 Gerd Kortemeyer
1.5       www        31: 
1.1       albertel   32: package Apache::response;
                     33: use strict;
                     34: 
1.57      harris41   35: BEGIN {
1.73    ! albertel   36:     &Apache::lonxml::register('Apache::response',('responseparam','parameter','dataresponse'));
1.1       albertel   37: }
                     38: 
1.13      albertel   39: sub start_response {
1.73    ! albertel   40:     my ($parstack,$safeeval)=@_;
        !            41:     my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
        !            42:     if ($id eq '') { $id = $Apache::lonxml::curdepth; }
        !            43:     if ($#Apache::inputtags::import > -1) {
        !            44: 	&Apache::lonxml::debug("Turning :$id: into");
        !            45: 	$id = join('_',@Apache::inputtags::import).'_'.$id;
        !            46: 	&Apache::lonxml::debug("New  :$id:");
        !            47:     }
        !            48:     push (@Apache::inputtags::response,$id);
        !            49:     push (@Apache::inputtags::responselist,$id);
        !            50:     @Apache::inputtags::inputlist=();
        !            51:     return $id;
1.13      albertel   52: }
                     53: 
                     54: sub end_response {
1.73    ! albertel   55:     pop @Apache::inputtags::response;
        !            56:     @Apache::inputtags::inputlist=();
        !            57:     return '';
1.13      albertel   58: }
                     59: 
1.41      albertel   60: sub start_hintresponse {
1.73    ! albertel   61:     my ($parstack,$safeeval)=@_;
        !            62:     my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
        !            63:     if ($id eq '') { $id = $Apache::lonxml::curdepth; }
        !            64:     push (@Apache::inputtags::response,$id);
        !            65:     push (@Apache::inputtags::paramstack,[%Apache::inputtags::params]);
        !            66:     return $id;
1.41      albertel   67: }
                     68: 
                     69: sub end_hintresponse {
1.73    ! albertel   70:     pop @Apache::inputtags::response;
        !            71:     if (defined($Apache::inputtags::paramstack[-1])) {
        !            72: 	%Apache::inputtags::params=
        !            73: 	    @{ pop(@Apache::inputtags::paramstack) };
        !            74:     }
        !            75:     return '';
1.41      albertel   76: }
                     77: 
1.38      albertel   78: # used by response to set the non-safe space random number generator to something
                     79: # that is stable and unique based on the part number and response number
1.26      albertel   80: sub setrandomnumber {
1.73    ! albertel   81:     my $rndseed;
        !            82:     if ($ENV{'request.state'} eq "construct") {
        !            83: 	$rndseed=$ENV{'form.rndseed'};
        !            84: 	if (!$rndseed) { $rndseed=time; }
        !            85:     } else {
        !            86: 	my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
        !            87: 	$rndseed=&Apache::lonnet::rndseed($symb,$courseid,$domain,$name);
        !            88:     }
        !            89:     &Apache::lonxml::debug("randseed $rndseed");
        !            90:     #  $rndseed=unpack("%32i",$rndseed);
1.38      albertel   91:     $rndseed=$rndseed
1.73    ! albertel   92: 	+(&Apache::lonnet::numval($Apache::inputtags::part) << 10);
        !            93:     if (defined($Apache::inputtags::response['-1'])) {
        !            94: 	$rndseed=$rndseed
        !            95: 	    +&Apache::lonnet::numval($Apache::inputtags::response['-1']);
        !            96:     }
        !            97:     &Math::Random::random_set_seed_from_phrase($rndseed);
        !            98:     &Apache::lonxml::debug("randseed $rndseed");
        !            99:     return '';
1.26      albertel  100: }
                    101: 
1.7       www       102: sub meta_parameter_write {
1.38      albertel  103:     my ($name,$type,$default,$display)=@_;
1.41      albertel  104:     my $partref=$Apache::inputtags::part;
                    105:     my $result='<parameter part="'.$Apache::inputtags::part.'"';
                    106:     if (defined($Apache::inputtags::response[-1])) {
1.73    ! albertel  107: 	$result.=            ' id="'.$Apache::inputtags::response[-1].'"';
        !           108: 	$partref.='_'.$Apache::inputtags::response[-1];
1.41      albertel  109:     }
                    110:     $result.=            ' name="'.$name.'"'.
                    111:                          ' type="'.$type.'"'.
                    112: ($default?' default="'.$default.'"':'').
                    113: ($display?' display="'.$display.' [Part: '.$partref.']"':'')
                    114:              .'></parameter>'
                    115:              ."\n";
                    116:     return $result;
1.33      www       117: }
                    118: 
                    119: sub meta_package_write {
                    120:     my $name=shift;
1.41      albertel  121:     my $result = '<parameter part="'.$Apache::inputtags::part.'"';
                    122:     if(defined($Apache::inputtags::response[-1])) {
1.73    ! albertel  123: 	$result.= ' id="'.$Apache::inputtags::response[-1].'"';
1.41      albertel  124:     }
                    125:     $result.=' package="'.$name.'"></parameter>'."\n";
                    126:     return $result;
1.7       www       127: }
                    128: 
                    129: sub meta_stores_write {
1.10      www       130:     my ($name,$type,$display)=@_;
1.41      albertel  131:     my $partref=$Apache::inputtags::part;
                    132:     my $result = '<stores part="'.$Apache::inputtags::part.'"';
                    133:     if (defined($Apache::inputtags::response[-1])) {
1.73    ! albertel  134: 	$result.=           ' id="'.$Apache::inputtags::response[-1].'"';
        !           135: 	$partref.='_'.$Apache::inputtags::response[-1];
1.41      albertel  136:     }	
                    137:     $result.=          ' name="'.$name.'"'.
                    138:                        ' type="'.$type.'"'.
                    139: 	            ' display="'.$display.' [Part: '.$partref.']"'.
                    140: 		      "></stores>\n";
1.7       www       141: }
                    142: 
                    143: sub mandatory_part_meta {
                    144: #
                    145: # Autogenerate metadata for mandatory
                    146: # input (from RAT or lonparmset) and 
                    147: # output (to lonspreadsheet)
                    148: # of each part
                    149: #
1.73    ! albertel  150:     return
1.34      www       151: #    &meta_parameter_write('opendate','date_start','',
                    152: #                          'Opening Date').
                    153: #    &meta_parameter_write('duedate','date_end','',
                    154: #                          'Due Date').
                    155: #    &meta_parameter_write('answerdate','date_start','',
                    156: #                          'Show Answer Date').
                    157: #    &meta_parameter_write('weight','int_zeropos','',
                    158: #                          'Available Points').
                    159: #    &meta_parameter_write('maxtries','int_pos','',
                    160: #                          'Maximum Number of Tries').
1.73    ! albertel  161: 	&meta_package_write('part').
        !           162:         &meta_stores_write('solved','string',
        !           163: 			   'Problem Status').
        !           164:         &meta_stores_write('tries','int_zeropos',
        !           165: 			   'Number of Attempts').
        !           166:         &meta_stores_write('awarded','float',
        !           167: 			   'Partial Credit Factor');
1.7       www       168: #
                    169: # Note: responseid-specific data 'submission' and 'awarddetail'
                    170: # not available to spreadsheet -> skip here
                    171: #
1.14      albertel  172: }
                    173: 
1.15      albertel  174: sub check_for_previous {
1.73    ! albertel  175:     my ($curresponse,$partid,$id) = @_;
        !           176:     my %previous;
        !           177:     $previous{'used'} = 0;
        !           178:     foreach my $key (sort(keys(%Apache::lonhomework::history))) {
        !           179: 	if ($key =~ /resource\.$partid\.$id\.submission/) {
        !           180: 	    &Apache::lonxml::debug("Trying $key");
        !           181: 	    my $pastresponse=$Apache::lonhomework::history{$key};
        !           182: 	    if ($pastresponse eq $curresponse) {
        !           183: 		$previous{'used'} = 1;
        !           184: 		my $history;
        !           185: 		if ( $key =~ /^(\d+):/ ) {
        !           186: 		    $history=$1;
        !           187: 		    $previous{'award'} = $Apache::lonhomework::history{"$history:resource.$partid.$id.awarddetail"};
        !           188: 		    $previous{'last'}='0';
        !           189: 		    push(@{ $previous{'version'} },$history);
        !           190: 		} else {
        !           191: 		    $previous{'award'} = $Apache::lonhomework::history{"resource.$partid.$id.awarddetail"};
        !           192: 		    $previous{'last'}='1';
        !           193: 		}
        !           194: 		if (! $previous{'award'} ) { $previous{'award'} = 'UNKNOWN';	}
        !           195: 		&Apache::lonxml::debug("got a match :$previous{'award'}:$previous{'used'}:");
        !           196: 	    }
1.32      albertel  197: 	}
1.73    ! albertel  198:     }
        !           199:     &Apache::lonhomework::showhash(%previous);
        !           200:     return %previous;
1.54      albertel  201: }
                    202: 
                    203: sub handle_previous {
1.73    ! albertel  204:     my ($previous,$ad)=@_;
        !           205:     if ($$previous{'used'} && ($$previous{'award'} eq $ad) ) {
        !           206: 	if ($$previous{'last'}) {
        !           207: 	    push(@Apache::inputtags::previous,'PREVIOUSLY_LAST');
        !           208: 	} else {
        !           209: 	    push(@Apache::inputtags::previous,'PREVIOUSLY_USED');
        !           210: 	}
        !           211: 	push(@Apache::inputtags::previous_version,$$previous{'version'});
1.54      albertel  212:     }
1.44      albertel  213: }
                    214: 
1.45      albertel  215: sub view_or_modify {
1.73    ! albertel  216:     my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
        !           217:     my $myself=0;
        !           218:     if ( ($name eq $ENV{'user.name'}) && ($domain eq $ENV{'user.domain'}) ) {
        !           219: 	$myself=1;
        !           220:     }
        !           221:     my $vgr=&Apache::lonnet::allowed('vgr',$courseid);
        !           222:     my $mgr=&Apache::lonnet::allowed('vgr',$courseid);
        !           223:     if ($mgr) { return "M"; }
        !           224:     if ($vgr) { return "V"; }
        !           225:     if ($myself) { return "V"; }
        !           226:     return '';
1.45      albertel  227: }
                    228: 
1.44      albertel  229: sub start_dataresponse {
1.73    ! albertel  230:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
        !           231:     my $id = &Apache::response::start_response($parstack,$safeeval);
        !           232:     my $result;
        !           233:     if ($target eq 'web') {
        !           234: 	$result = $token->[2]->{'display'}.':';
        !           235:     } elsif ($target eq 'meta') {
        !           236: 	$result = &Apache::response::meta_stores_write($token->[2]->{'name'},
        !           237: 						       $token->[2]->{'type'},
        !           238: 						       $token->[2]->{'display'});
        !           239: 	$result .= &Apache::response::meta_package_write('dataresponse');
        !           240:     }
        !           241:     return $result;
1.44      albertel  242: }
                    243: 
                    244: sub end_dataresponse {
1.73    ! albertel  245:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
        !           246:     my $result;
        !           247:     if ( $target eq 'web' ) {
        !           248:     } elsif ($target eq 'grade' ) {
        !           249: 	if ( defined $ENV{'form.submitted'}) {
        !           250: 	    my ($symb,$courseid,$domain,$name)=&Apache::lonxml::whichuser();
        !           251: 	    my $allowed=&Apache::lonnet::allowed('mgr',$courseid);
        !           252: 	    if ($allowed) {
        !           253: 		&Apache::response::setup_params('dataresponse');
        !           254: 		my $partid = $Apache::inputtags::part;
        !           255: 		my $id = $Apache::inputtags::response['-1'];
        !           256: 		my $response = $ENV{'form.HWVAL_'.$id};
        !           257: 		my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
        !           258: 		if ( $response =~ /[^\s]/) {
        !           259: 		    $Apache::lonhomework::results{"resource.$partid.$id.$name"}=$response;
        !           260: 		    $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
        !           261: 		    $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}='SUBMITTED';
        !           262: 		}
        !           263: 	    } else {
        !           264: 		$result='Not Permitted to change values.'
        !           265: 	    }
1.45      albertel  266: 	}
1.73    ! albertel  267:     }
        !           268:     &Apache::response::end_response;
        !           269:     return $result;
1.3       albertel  270: }
                    271: 
                    272: sub start_responseparam {
1.73    ! albertel  273:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
        !           274:     my $result='';
        !           275:     if ($target eq 'meta') {
        !           276: 	$result = &meta_parameter_write($token->[2]->{'name'},
        !           277: 					$token->[2]->{'type'},
        !           278: 					$token->[2]->{'default'},
        !           279: 					$token->[2]->{'description'});
        !           280:     } elsif ($target eq 'edit') {
        !           281: 	$result.=&Apache::edit::tag_start($target,$token);
        !           282: 	$result.=&Apache::edit::text_arg('Name:','name',$token).
        !           283: 	    &Apache::edit::text_arg('Type:','type',$token).
        !           284: 		&Apache::edit::text_arg('Description:','description',$token).
        !           285: 		    &Apache::edit::text_arg('Default:','default',$token).
        !           286: 			"</td></tr>";
        !           287: 	$result.=&Apache::edit::end_table;
        !           288:     } elsif ($target eq 'modified') {
        !           289: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
        !           290: 						     'name','type','description',
        !           291: 						     'default');
        !           292: 	if ($constructtag) {
        !           293: 	    $result = &Apache::edit::rebuild_tag($token);
        !           294: 	    $result.=&Apache::edit::handle_insert();
        !           295: 	}
        !           296:     } elsif ($target eq 'grade' || $target eq 'answer' || $target eq 'web' ||
        !           297: 	     $target eq 'tex' || $target eq 'analyze' ) {
        !           298: 	if ($ENV{'request.state'} eq 'construct') {
        !           299: 	    my $name   =&Apache::lonxml::get_param('name',$parstack,$safeeval);
        !           300: 	    my $default=&Apache::lonxml::get_param('default',$parstack,
        !           301: 						     $safeeval);
        !           302: 	    if ($name) {$Apache::inputtags::params{$name}=$default;}
        !           303: 	}
1.52      albertel  304:     }
1.73    ! albertel  305:     return $result;
1.3       albertel  306: }
                    307: 
                    308: sub end_responseparam {
1.73    ! albertel  309:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
        !           310:     if ($target eq 'edit') { return ('','no'); }
        !           311:     return '';
1.55      albertel  312: }
                    313: 
                    314: sub start_parameter {
1.73    ! albertel  315:     my $result = &start_responseparam(@_);
        !           316:     return $result;
1.55      albertel  317: }
                    318: 
                    319: sub end_parameter {
1.73    ! albertel  320:     my $result = &end_responseparam(@_);
        !           321:     return $result;
1.42      albertel  322: }
                    323: 
1.67      albertel  324: sub reset_params {
                    325:     %Apache::inputtags::params=();
                    326: }
                    327: 
1.42      albertel  328: sub setup_params {
1.73    ! albertel  329:     my ($tag) = @_;
1.42      albertel  330: 
1.73    ! albertel  331:     if ($ENV{'request.state'} eq 'construct') { return; }
        !           332:     my %paramlist=();
        !           333:     foreach my $key (keys(%Apache::lonnet::packagetab)) {
        !           334: 	if ($key =~ /^$tag/) {
        !           335: 	    my ($package,$name) = split(/&/,$key);
        !           336: 	    $paramlist{$name}=1;
        !           337: 	}
1.42      albertel  338:     }
1.73    ! albertel  339:     foreach my $key (keys(%paramlist)) {
        !           340: 	my $entry= 'resource.'.$Apache::inputtags::part;
        !           341: 	if (defined($Apache::inputtags::response[-1])) {
        !           342: 	    $entry.='_'.$Apache::inputtags::response[-1];
        !           343: 	}
        !           344: 	$entry.='.'.$key;
        !           345: 	&Apache::lonxml::debug("looking for $entry");
        !           346: 	my $value = &Apache::lonnet::EXT("$entry");
        !           347: 	&Apache::lonxml::debug("$key has value :$value:");
        !           348: 	if ($value eq 'con_lost' || $value =~ /^error:/) {
        !           349: 	    &Apache::lonxml::debug("using nothing");
        !           350: 	    $Apache::inputtags::params{$key}='';
        !           351: 	} else {
        !           352: 	    &Apache::lonxml::debug("using value");
        !           353: 	    $Apache::inputtags::params{$key}=$value;
        !           354: 	}
1.42      albertel  355:     }
1.48      albertel  356: }
                    357: 
                    358: sub answer_header {
1.73    ! albertel  359:     my ($type) = @_;
        !           360:     my $result;
        !           361:     if ($type eq 'optionresponse' || $type eq 'radiobuttonresponse' ) {
        !           362: 	$result = '<table border="1"><tr><th>Answer for Part:'.
        !           363: 	    $Apache::inputtags::part. '</th></tr><tr>'."\n";
        !           364:     } else {
        !           365: 	$result = '<table border="1"><tr><td>Answer for Part:'.
        !           366: 	    $Apache::inputtags::part. '</td>'."\n";
        !           367:     }
        !           368:     return $result;
1.48      albertel  369: }
                    370: 
                    371: sub answer_part {
1.73    ! albertel  372:     my ($type,$answer) = @_;
        !           373:     my $result;
        !           374:     if ($type eq 'optionresponse' || $type eq 'radiobuttonresponse') {
        !           375: 	$result = '<td>'.$answer.'</td>';
        !           376:     } else {
        !           377: 	$result = '<td>'.$answer.'</td>';
        !           378:     }
        !           379:     return $result;
1.48      albertel  380: }
                    381: 
                    382: sub answer_footer {
1.73    ! albertel  383:     my ($type) = @_;
        !           384:     my $result;
        !           385:     if ($type eq 'optionresponse' || $type eq 'radiobuttonresponse') {
        !           386: 	$result = '</tr></table>';
        !           387:     } else {
        !           388: 	$result = '</tr></table>';
        !           389:     }
        !           390:     return $result;
1.1       albertel  391: }
1.2       albertel  392: 
1.62      albertel  393: sub showallfoils {
1.73    ! albertel  394:     my $return=0;
        !           395:     if (defined($ENV{'form.showallfoils'}) &&
        !           396: 	$ENV{'request.state'} eq 'construct') {
        !           397: 	$return=1;
        !           398:     }
        !           399:     return $return;
1.70      albertel  400: }
                    401: 
                    402: sub getresponse {
                    403:     my ($temp)=@_;
                    404:     my $formparm='form.HWVAL_'.$Apache::inputtags::response['-1'];
                    405:     my $response;
                    406:     if (!defined($temp)) {
                    407: 	$temp=1;
                    408:     } else {
                    409: 	$formparm.=":$temp";
                    410:     }
                    411:     my %let_to_num=('A'=>0,'B'=>1,'C'=>2,'D'=>3,'E'=>4,'F'=>5,'G'=>6,'H'=>7,
                    412: 		    'I'=>8,'J'=>9,'K'=>10,'L'=>11,'M'=>12,'N'=>13,'O'=>14,
                    413: 		    'P'=>15,'Q'=>16,'R'=>17,'S'=>18,'T'=>19,'U'=>20,'V'=>21,
                    414: 		    'W'=>22,'X'=>23,'Y'=>24,'Z'=>25);
                    415:     if ($ENV{'form.submitted'} eq 'scantron') {
1.71      albertel  416: 	my $part  = $Apache::inputtags::part;
                    417: 	my $id    = $Apache::inputtags::response[-1];
1.70      albertel  418: 	$response = $ENV{'scantron.'.($Apache::lonxml::counter+$temp-1).
                    419: 			 '.answer'};
1.71      albertel  420: 	# save bubbled letter for later
                    421: 	$Apache::lonhomework::results{"resource.$part.$id.scantron"}.=
                    422: 	    $response;
1.70      albertel  423: 	$response = $let_to_num{$response};
                    424:     } else {
                    425: 	$response = $ENV{$formparm};
                    426:     }
                    427:     return $response;
1.62      albertel  428: }
1.71      albertel  429: 
                    430: sub repetition {
                    431:     my $id = $Apache::inputtags::part;
                    432:     my $weight = &Apache::lonnet::EXT("resource.$id.weight");
                    433:     my $repetition = int $weight/9;
                    434:     if ($weight % 9 != 0) {$repetition++;} 
1.72      albertel  435:     return $repetition;
                    436: }
                    437: 
                    438: sub scored_response {
                    439:     my ($part,$id)=@_;
                    440:     my $repetition=&repetition();
                    441:     my $score=0;
                    442:     for (my $i=0;$i<$repetition;$i++) {
                    443: 	my $increase=&Apache::response::getresponse($i+1);
                    444: 	if ($increase ne '') { $score+=$increase+1; }
                    445:     }
                    446:     my $weight = &Apache::lonnet::EXT("resource.$part.weight");
                    447:     my $pcr=$score/$weight;
                    448:     $Apache::lonhomework::results{"resource.$part.$id.awarded"}=$pcr;
                    449:     $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
                    450: 	'ASSIGNED_SCORE';
1.71      albertel  451:     return $repetition;
                    452: }
                    453: 
1.1       albertel  454: 1;
                    455: __END__
1.38      albertel  456:  

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