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

1.38      albertel    1: # The LearningOnline Network with CAPA
1.1       albertel    2: # various response type definitons response definition
1.53      albertel    3: #
1.68    ! albertel    4: # $Id: response.pm,v 1.67 2002/11/25 21:36:42 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.65      bowersj2   36:   &Apache::lonxml::register('Apache::response',('responseparam','parameter','dataresponse'));
1.1       albertel   37: }
                     38: 
1.13      albertel   39: sub start_response {
1.41      albertel   40:   my ($parstack,$safeeval)=@_;
1.38      albertel   41:   my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
                     42:   if ($id eq '') { $id = $Apache::lonxml::curdepth; }
1.51      albertel   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:   }
1.38      albertel   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 {
                     55:   pop @Apache::inputtags::response;
1.38      albertel   56:   @Apache::inputtags::inputlist=();
1.13      albertel   57:   return '';
                     58: }
                     59: 
1.41      albertel   60: sub start_hintresponse {
                     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);
1.60      albertel   65:   push (@Apache::inputtags::paramstack,[%Apache::inputtags::params]);
1.41      albertel   66:   return $id;
                     67: }
                     68: 
                     69: sub end_hintresponse {
                     70:   pop @Apache::inputtags::response;
1.60      albertel   71:   %Apache::inputtags::params=@{pop(@Apache::inputtags::paramstack)};
1.41      albertel   72:   return '';
                     73: }
                     74: 
1.38      albertel   75: # used by response to set the non-safe space random number generator to something
                     76: # that is stable and unique based on the part number and response number
1.26      albertel   77: sub setrandomnumber {
1.48      albertel   78:   my $rndseed;
                     79:   if ($ENV{'request.state'} eq "construct") {
                     80:     $rndseed=$ENV{'form.rndseed'};
                     81:     if (!$rndseed) { $rndseed=time; }
                     82:   } else {
1.59      albertel   83:     my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
                     84:     $rndseed=&Apache::lonnet::rndseed($symb,$courseid,$domain,$name);
1.48      albertel   85:   }
1.38      albertel   86:   &Apache::lonxml::debug("randseed $rndseed");
                     87: #  $rndseed=unpack("%32i",$rndseed);
                     88:   $rndseed=$rndseed
                     89:     +(&Apache::lonnet::numval($Apache::inputtags::part) << 10);
                     90:   if (defined($Apache::inputtags::response['-1'])) {
                     91:     $rndseed=$rndseed
                     92:       +&Apache::lonnet::numval($Apache::inputtags::response['-1']);
                     93:   }
1.64      albertel   94:   &Math::Random::random_set_seed_from_phrase($rndseed);
1.38      albertel   95:   &Apache::lonxml::debug("randseed $rndseed");
                     96:   return '';
1.26      albertel   97: }
                     98: 
1.7       www        99: sub meta_parameter_write {
1.38      albertel  100:     my ($name,$type,$default,$display)=@_;
1.41      albertel  101:     my $partref=$Apache::inputtags::part;
                    102:     my $result='<parameter part="'.$Apache::inputtags::part.'"';
                    103:     if (defined($Apache::inputtags::response[-1])) {
                    104:       $result.=            ' id="'.$Apache::inputtags::response[-1].'"';
                    105:       $partref.='_'.$Apache::inputtags::response[-1];
                    106:     }
                    107:     $result.=            ' name="'.$name.'"'.
                    108:                          ' type="'.$type.'"'.
                    109: ($default?' default="'.$default.'"':'').
                    110: ($display?' display="'.$display.' [Part: '.$partref.']"':'')
                    111:              .'></parameter>'
                    112:              ."\n";
                    113:     return $result;
1.33      www       114: }
                    115: 
                    116: sub meta_package_write {
                    117:     my $name=shift;
1.41      albertel  118:     my $result = '<parameter part="'.$Apache::inputtags::part.'"';
                    119:     if(defined($Apache::inputtags::response[-1])) {
                    120:       $result.=              ' id="'.$Apache::inputtags::response[-1].'"';
                    121:     }
                    122:     $result.=' package="'.$name.'"></parameter>'."\n";
                    123:     return $result;
1.7       www       124: }
                    125: 
                    126: sub meta_stores_write {
1.10      www       127:     my ($name,$type,$display)=@_;
1.41      albertel  128:     my $partref=$Apache::inputtags::part;
                    129:     my $result = '<stores part="'.$Apache::inputtags::part.'"';
                    130:     if (defined($Apache::inputtags::response[-1])) {
                    131:       $result.=           ' id="'.$Apache::inputtags::response[-1].'"';
                    132:       $partref.='_'.$Apache::inputtags::response[-1];
                    133:     }	
                    134:     $result.=          ' name="'.$name.'"'.
                    135:                        ' type="'.$type.'"'.
                    136: 	            ' display="'.$display.' [Part: '.$partref.']"'.
                    137: 		      "></stores>\n";
1.7       www       138: }
                    139: 
                    140: sub mandatory_part_meta {
                    141: #
                    142: # Autogenerate metadata for mandatory
                    143: # input (from RAT or lonparmset) and 
                    144: # output (to lonspreadsheet)
                    145: # of each part
                    146: #
1.38      albertel  147:  return
1.34      www       148: #    &meta_parameter_write('opendate','date_start','',
                    149: #                          'Opening Date').
                    150: #    &meta_parameter_write('duedate','date_end','',
                    151: #                          'Due Date').
                    152: #    &meta_parameter_write('answerdate','date_start','',
                    153: #                          'Show Answer Date').
                    154: #    &meta_parameter_write('weight','int_zeropos','',
                    155: #                          'Available Points').
                    156: #    &meta_parameter_write('maxtries','int_pos','',
                    157: #                          'Maximum Number of Tries').
1.39      www       158:     &meta_package_write('part').
1.10      www       159:     &meta_stores_write('solved','string',
1.7       www       160:                           'Problem Status').
1.38      albertel  161:     &meta_stores_write('tries','int_zeropos',
1.24      albertel  162:                           'Number of Attempts').
1.38      albertel  163:     &meta_stores_write('awarded','float',
1.7       www       164: 		          'Partial Credit Factor');
                    165: #
                    166: # Note: responseid-specific data 'submission' and 'awarddetail'
                    167: # not available to spreadsheet -> skip here
                    168: #
1.14      albertel  169: }
                    170: 
1.15      albertel  171: sub check_for_previous {
1.38      albertel  172:   my ($curresponse,$partid,$id) = @_;
                    173:   my %previous;
                    174:   $previous{'used'} = 0;
1.68    ! albertel  175:   foreach my $key (sort(keys(%Apache::lonhomework::history))) {
1.38      albertel  176:     if ($key =~ /resource\.$partid\.$id\.submission/) {
                    177:       &Apache::lonxml::debug("Trying $key");
                    178:       my $pastresponse=$Apache::lonhomework::history{$key};
                    179:       if ($pastresponse eq $curresponse) {
                    180: 	$previous{'used'} = 1;
                    181: 	my $history;
                    182: 	if ( $key =~ /^(\d+):/ ) {
                    183: 	  $history=$1;
                    184: 	  $previous{'award'} = $Apache::lonhomework::history{"$history:resource.$partid.$id.awarddetail"};
                    185: 	  $previous{'last'}='0';
1.68    ! albertel  186: 	  push(@{ $previous{'version'} },$history);
1.38      albertel  187: 	} else {
                    188: 	  $previous{'award'} = $Apache::lonhomework::history{"resource.$partid.$id.awarddetail"};
                    189: 	  $previous{'last'}='1';
1.32      albertel  190: 	}
1.38      albertel  191: 	if (! $previous{'award'} ) { $previous{'award'} = 'UNKNOWN';	}
                    192: 	&Apache::lonxml::debug("got a match :$previous{'award'}:$previous{'used'}:");
                    193:       }
1.32      albertel  194:     }
1.38      albertel  195:   }
1.68    ! albertel  196:   &Apache::lonhomework::showhash(%previous);
1.38      albertel  197:   return %previous;
1.54      albertel  198: }
                    199: 
                    200: sub handle_previous {
                    201:   my ($previous,$ad)=@_;
                    202:   if ($$previous{'used'} && ($$previous{'award'} eq $ad) ) {
                    203:     if ($$previous{'last'}) {
                    204:       push(@Apache::inputtags::previous,'PREVIOUSLY_LAST');
                    205:     } else {
                    206:       push(@Apache::inputtags::previous,'PREVIOUSLY_USED');
                    207:     }
1.68    ! albertel  208:     push(@Apache::inputtags::previous_version,$$previous{'version'});
1.54      albertel  209:   }
1.44      albertel  210: }
                    211: 
1.45      albertel  212: sub view_or_modify {
1.47      albertel  213:   my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
1.45      albertel  214:   my $myself=0;
1.46      albertel  215:   if ( ($name eq $ENV{'user.name'}) && ($domain eq $ENV{'user.domain'}) ) {
1.45      albertel  216:     $myself=1;
                    217:   }
                    218:   my $vgr=&Apache::lonnet::allowed('vgr',$courseid);
                    219:   my $mgr=&Apache::lonnet::allowed('vgr',$courseid);
                    220:   if ($mgr) { return "M"; }
                    221:   if ($vgr) { return "V"; }
                    222:   if ($myself) { return "V"; }
                    223:   return '';
                    224: }
                    225: 
1.44      albertel  226: sub start_dataresponse {
                    227:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    228:   my $id = &Apache::response::start_response($parstack,$safeeval);
                    229:   my $result;
1.45      albertel  230:   if ($target eq 'web') {
                    231:     $result = $token->[2]->{'display'}.':';
                    232:   } elsif ($target eq 'meta') {
1.44      albertel  233:     $result = &Apache::response::meta_stores_write($token->[2]->{'name'},
                    234: 						   $token->[2]->{'type'},
                    235: 						   $token->[2]->{'display'});
                    236:     $result .= &Apache::response::meta_package_write('dataresponse');
                    237:   }
                    238:   return $result;
                    239: }
                    240: 
                    241: sub end_dataresponse {
                    242:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    243:   my $result;
                    244:   if ( $target eq 'web' ) {
                    245:   } elsif ($target eq 'grade' ) {
                    246:     if ( defined $ENV{'form.submitted'}) {
1.47      albertel  247:       my ($symb,$courseid,$domain,$name)=&Apache::lonxml::whichuser();
1.45      albertel  248:       my $allowed=&Apache::lonnet::allowed('mgr',$courseid);
                    249:       if ($allowed) {
1.66      albertel  250: 	&Apache::response::setup_params('dataresponse');
1.45      albertel  251: 	my $partid = $Apache::inputtags::part;
                    252: 	my $id = $Apache::inputtags::response['-1'];
                    253: 	my $response = $ENV{'form.HWVAL'.$id};
                    254: 	my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    255: 	if ( $response =~ /[^\s]/) {
                    256: 	  $Apache::lonhomework::results{"resource.$partid.$id.$name"}=
                    257: 	    $response;
                    258: 	  $Apache::lonhomework::results{
                    259: 					"resource.$partid.$id.submission"}=
                    260: 					  $response;
                    261: 	  $Apache::lonhomework::results{
                    262: 					"resource.$partid.$id.awarddetail"}=
                    263: 					  'SUBMITTED';
                    264: 	}
                    265:       } else {
                    266: 	$result='Not Permitted to change values.'
1.44      albertel  267:       }
                    268:     }
                    269:   }
                    270:   &Apache::response::end_response;
                    271:   return $result;
1.3       albertel  272: }
                    273: 
                    274: sub start_responseparam {
1.38      albertel  275:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.43      albertel  276:   my $result='';
1.42      albertel  277:   if ($target eq 'meta') {
                    278:     $result = &meta_parameter_write($token->[2]->{'name'},
                    279: 				    $token->[2]->{'type'},
                    280: 				    $token->[2]->{'default'},
                    281: 				    $token->[2]->{'description'});
1.38      albertel  282:   } elsif ($target eq 'edit') {
1.56      matthew   283:     $result.=&Apache::edit::tag_start($target,$token);
1.38      albertel  284:     $result.=&Apache::edit::text_arg('Name:','name',$token).
                    285:       &Apache::edit::text_arg('Type:','type',$token).
                    286: 	&Apache::edit::text_arg('Description:','description',$token).
                    287: 	  &Apache::edit::text_arg('Default:','default',$token).
                    288: 	    "</td></tr>";
                    289:     $result.=&Apache::edit::end_table;
                    290:   } elsif ($target eq 'modified') {
                    291:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
                    292: 						 'name','type','description',
                    293: 						 'default');
                    294:     if ($constructtag) {
                    295:       $result = &Apache::edit::rebuild_tag($token);
                    296:       $result.=&Apache::edit::handle_insert();
                    297:     }
1.66      albertel  298:   } elsif ($target eq 'grade' || $target eq 'answer' || $target eq 'web' ||
                    299: 	   $target eq 'tex' || $target eq 'analyze' ) {
1.52      albertel  300:     if ($ENV{'request.state'} eq 'construct') {
                    301:       my $name    = &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    302:       my $default = &Apache::lonxml::get_param('default',$parstack,$safeeval);
1.61      albertel  303:       if ($name) {$Apache::inputtags::params{$name}=$default;}
1.52      albertel  304:     }
1.38      albertel  305:   }
1.42      albertel  306:   return $result;
1.3       albertel  307: }
                    308: 
                    309: sub end_responseparam {
1.38      albertel  310:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    311:   if ($target eq 'edit') { return ('','no'); }
                    312:   return '';
1.55      albertel  313: }
                    314: 
                    315: sub start_parameter {
                    316:   my $result = &start_responseparam(@_);
                    317:   return $result;
                    318: }
                    319: 
                    320: sub end_parameter {
                    321:   my $result = &end_responseparam(@_);
                    322:   return $result;
1.42      albertel  323: }
                    324: 
1.67      albertel  325: sub reset_params {
                    326:     %Apache::inputtags::params=();
                    327: }
                    328: 
1.42      albertel  329: sub setup_params {
                    330:   my ($tag) = @_;
                    331: 
1.52      albertel  332:   if ($ENV{'request.state'} eq 'construct') { return; }
1.43      albertel  333:   my %paramlist=();
1.42      albertel  334:   foreach my $key (keys(%Apache::lonnet::packagetab)) {
                    335:     if ($key =~ /^$tag/) {
                    336:       my ($package,$name) = split(/&/,$key);
1.43      albertel  337:       $paramlist{$name}=1;
1.42      albertel  338:     }
                    339:   }
1.43      albertel  340:   foreach my $key (keys(%paramlist)) {
1.42      albertel  341:     my $entry= 'resource.'.$Apache::inputtags::part;
                    342:     if (defined($Apache::inputtags::response[-1])) {
                    343:       $entry.='_'.$Apache::inputtags::response[-1];
                    344:     }
                    345:     $entry.='.'.$key;
                    346:     &Apache::lonxml::debug("looking for $entry");
                    347:     my $value = &Apache::lonnet::EXT("$entry");
                    348:     &Apache::lonxml::debug("$key has value :$value:");
                    349:     if ($value eq 'con_lost' || $value =~ /^error:/) {
                    350:       &Apache::lonxml::debug("using nothing");
                    351:       $Apache::inputtags::params{$key}='';
                    352:     } else {
                    353:       &Apache::lonxml::debug("using value");
                    354:       $Apache::inputtags::params{$key}=$value;
                    355:     }
                    356:   }
1.48      albertel  357: }
                    358: 
                    359: sub answer_header {
                    360:   my ($type) = @_;
                    361:   my $result;
1.50      albertel  362:   if ($type eq 'optionresponse' || $type eq 'radiobuttonresponse' ) {
1.48      albertel  363:     $result = '<table border="1"><tr><th>Answer for Part:'.
                    364:       $Apache::inputtags::part. '</th></tr><tr>'."\n";
                    365:   } else {
1.49      albertel  366:     $result = '<table border="1"><tr><td>Answer for Part:'.
                    367:       $Apache::inputtags::part. '</td>'."\n";
1.48      albertel  368:   }
                    369:   return $result;
                    370: }
                    371: 
                    372: sub answer_part {
                    373:   my ($type,$answer) = @_;
                    374:   my $result;
1.50      albertel  375:   if ($type eq 'optionresponse' || $type eq 'radiobuttonresponse') {
1.48      albertel  376:     $result = '<td>'.$answer.'</td>';
                    377:   } else {
                    378:     $result = '<td>'.$answer.'</td>';
                    379:   }
                    380:   return $result;
                    381: }
                    382: 
                    383: sub answer_footer {
                    384:   my ($type) = @_;
                    385:   my $result;
1.50      albertel  386:   if ($type eq 'optionresponse' || $type eq 'radiobuttonresponse') {
1.48      albertel  387:     $result = '</tr></table>';
                    388:   } else {
                    389:     $result = '</tr></table>';
                    390:   }
                    391:   return $result;
1.1       albertel  392: }
1.2       albertel  393: 
1.62      albertel  394: sub showallfoils {
                    395:   my $return=0;
                    396:   if (defined($ENV{'form.showallfoils'}) &&
                    397:       $ENV{'request.state'} eq 'construct') {
                    398:     $return=1;
                    399:   }
                    400:   return $return;
                    401: }
1.1       albertel  402: 1;
                    403: __END__
1.38      albertel  404:  

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