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

1.38      albertel    1: # The LearningOnline Network with CAPA
1.1       albertel    2: # various response type definitons response definition
1.53      albertel    3: #
1.60    ! albertel    4: # $Id: response.pm,v 1.59 2002/02/14 21:48:31 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.58      albertel   36:   &Apache::lonxml::register('Apache::response',('responseparam','parameter','caparesponse','numericalresponse','formularesponse','stringresponse','radiobuttonresponse','optionresponse','imageresponse','essayresponse','dataresponse','externalresponse'));
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:   }
                     94:   srand($rndseed);
                     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;
                    175:   foreach my $key (reverse(sort(keys(%Apache::lonhomework::history)))) {
                    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';
                    186: 	} else {
                    187: 	  $previous{'award'} = $Apache::lonhomework::history{"resource.$partid.$id.awarddetail"};
                    188: 	  $previous{'last'}='1';
1.32      albertel  189: 	}
1.38      albertel  190: 	if (! $previous{'award'} ) { $previous{'award'} = 'UNKNOWN';	}
                    191: 	&Apache::lonxml::debug("got a match :$previous{'award'}:$previous{'used'}:");
                    192: 	last;
                    193:       }
1.32      albertel  194:     }
1.38      albertel  195:   }
                    196:   return %previous;
1.54      albertel  197: }
                    198: 
                    199: sub handle_previous {
                    200:   my ($previous,$ad)=@_;
                    201:   if ($$previous{'used'} && ($$previous{'award'} eq $ad) ) {
                    202:     if ($$previous{'last'}) {
                    203:       push(@Apache::inputtags::previous,'PREVIOUSLY_LAST');
                    204:     } else {
                    205:       push(@Apache::inputtags::previous,'PREVIOUSLY_USED');
                    206:     }
                    207:   }
1.15      albertel  208: }
                    209: 
1.31      albertel  210: sub start_caparesponse {
1.38      albertel  211:   require Apache::caparesponse; 
                    212:   import Apache::caparesponse; 
                    213:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.31      albertel  214: #  print "\n<br />\nsimple caparesponse\n";
1.38      albertel  215:   return &Apache::caparesponse::start_caparesponse($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
1.31      albertel  216: }
                    217: 
                    218: sub start_stringresponse {
1.38      albertel  219:   require Apache::caparesponse;
                    220:   import Apache::caparesponse;
                    221:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    222:   return &Apache::caparesponse::start_stringresponse($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
1.7       www       223: }
                    224: 
1.49      albertel  225: sub start_formularesponse {
                    226:   require Apache::caparesponse;
                    227:   import Apache::caparesponse;
                    228:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    229:   return &Apache::caparesponse::start_formularesponse($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
                    230: }
                    231: 
1.31      albertel  232: sub start_numericalresponse {
1.1       albertel  233:   require Apache::caparesponse; 
                    234:   import Apache::caparesponse; 
1.38      albertel  235:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.35      albertel  236:   return &Apache::caparesponse::start_numericalresponse
                    237:     ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
1.16      albertel  238: }
                    239: 
                    240: sub start_radiobuttonresponse {
1.38      albertel  241:   require Apache::radiobuttonresponse; 
                    242:   import Apache::radiobuttonresponse; 
                    243:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    244:   return &Apache::radiobuttonresponse::start_radiobuttonresponse($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
1.19      albertel  245: }
                    246: 
                    247: sub start_optionresponse {
1.38      albertel  248:   require Apache::optionresponse; 
                    249:   import Apache::optionresponse; 
                    250:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    251:   return &Apache::optionresponse::start_optionresponse($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
1.21      albertel  252: }
                    253: 
                    254: sub start_imageresponse {
1.38      albertel  255:   require Apache::imageresponse; 
                    256:   import Apache::imageresponse; 
                    257:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    258:   return &Apache::imageresponse::start_imageresponse($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
1.23      albertel  259: }
                    260: 
                    261: sub start_essayresponse {
1.38      albertel  262:   require Apache::essayresponse; 
                    263:   import Apache::essayresponse; 
                    264:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    265:   return &Apache::essayresponse::start_essayresponse($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
1.58      albertel  266: }
                    267: 
                    268: sub start_externalresponse {
                    269:   require Apache::externalresponse; 
                    270:   import Apache::externalresponse; 
                    271:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    272:   return &Apache::externalresponse::start_externalresponse($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
1.44      albertel  273: }
                    274: 
1.45      albertel  275: sub view_or_modify {
1.47      albertel  276:   my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
1.45      albertel  277:   my $myself=0;
1.46      albertel  278:   if ( ($name eq $ENV{'user.name'}) && ($domain eq $ENV{'user.domain'}) ) {
1.45      albertel  279:     $myself=1;
                    280:   }
                    281:   my $vgr=&Apache::lonnet::allowed('vgr',$courseid);
                    282:   my $mgr=&Apache::lonnet::allowed('vgr',$courseid);
                    283:   if ($mgr) { return "M"; }
                    284:   if ($vgr) { return "V"; }
                    285:   if ($myself) { return "V"; }
                    286:   return '';
                    287: }
                    288: 
1.44      albertel  289: sub start_dataresponse {
                    290:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    291:   my $id = &Apache::response::start_response($parstack,$safeeval);
                    292:   my $result;
1.45      albertel  293:   if ($target eq 'web') {
                    294:     $result = $token->[2]->{'display'}.':';
                    295:   } elsif ($target eq 'meta') {
1.44      albertel  296:     $result = &Apache::response::meta_stores_write($token->[2]->{'name'},
                    297: 						   $token->[2]->{'type'},
                    298: 						   $token->[2]->{'display'});
                    299:     $result .= &Apache::response::meta_package_write('dataresponse');
                    300:   }
                    301:   return $result;
                    302: }
                    303: 
                    304: sub end_dataresponse {
                    305:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    306:   my $result;
                    307:   if ( $target eq 'web' ) {
                    308:   } elsif ($target eq 'grade' ) {
                    309:     if ( defined $ENV{'form.submitted'}) {
1.47      albertel  310:       my ($symb,$courseid,$domain,$name)=&Apache::lonxml::whichuser();
1.45      albertel  311:       my $allowed=&Apache::lonnet::allowed('mgr',$courseid);
                    312:       if ($allowed) {
                    313: 	&Apache::response::setup_params('datasubmission');
                    314: 	my $partid = $Apache::inputtags::part;
                    315: 	my $id = $Apache::inputtags::response['-1'];
                    316: 	my $response = $ENV{'form.HWVAL'.$id};
                    317: 	my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    318: 	if ( $response =~ /[^\s]/) {
                    319: 	  $Apache::lonhomework::results{"resource.$partid.$id.$name"}=
                    320: 	    $response;
                    321: 	  $Apache::lonhomework::results{
                    322: 					"resource.$partid.$id.submission"}=
                    323: 					  $response;
                    324: 	  $Apache::lonhomework::results{
                    325: 					"resource.$partid.$id.awarddetail"}=
                    326: 					  'SUBMITTED';
                    327: 	}
                    328:       } else {
                    329: 	$result='Not Permitted to change values.'
1.44      albertel  330:       }
                    331:     }
                    332:   }
                    333:   &Apache::response::end_response;
                    334:   return $result;
1.3       albertel  335: }
                    336: 
                    337: sub start_responseparam {
1.38      albertel  338:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.43      albertel  339:   my $result='';
1.42      albertel  340:   if ($target eq 'meta') {
                    341:     $result = &meta_parameter_write($token->[2]->{'name'},
                    342: 				    $token->[2]->{'type'},
                    343: 				    $token->[2]->{'default'},
                    344: 				    $token->[2]->{'description'});
1.38      albertel  345:   } elsif ($target eq 'edit') {
1.56      matthew   346:     $result.=&Apache::edit::tag_start($target,$token);
1.38      albertel  347:     $result.=&Apache::edit::text_arg('Name:','name',$token).
                    348:       &Apache::edit::text_arg('Type:','type',$token).
                    349: 	&Apache::edit::text_arg('Description:','description',$token).
                    350: 	  &Apache::edit::text_arg('Default:','default',$token).
                    351: 	    "</td></tr>";
                    352:     $result.=&Apache::edit::end_table;
                    353:   } elsif ($target eq 'modified') {
                    354:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
                    355: 						 'name','type','description',
                    356: 						 'default');
                    357:     if ($constructtag) {
                    358:       $result = &Apache::edit::rebuild_tag($token);
                    359:       $result.=&Apache::edit::handle_insert();
                    360:     }
1.60    ! albertel  361:   } elsif ($target eq 'grade' || $target eq 'answer' ||
        !           362: 	   $target eq 'web'   || $target eq 'tex') {
1.52      albertel  363:     if ($ENV{'request.state'} eq 'construct') {
                    364:       my $name    = &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    365:       my $default = &Apache::lonxml::get_param('default',$parstack,$safeeval);
                    366:       $Apache::inputtags::params{$name}=$default;
                    367:     }
1.38      albertel  368:   }
1.42      albertel  369:   return $result;
1.3       albertel  370: }
                    371: 
                    372: sub end_responseparam {
1.38      albertel  373:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    374:   if ($target eq 'edit') { return ('','no'); }
                    375:   return '';
1.55      albertel  376: }
                    377: 
                    378: sub start_parameter {
                    379:   my $result = &start_responseparam(@_);
                    380:   return $result;
                    381: }
                    382: 
                    383: sub end_parameter {
                    384:   my $result = &end_responseparam(@_);
                    385:   return $result;
1.42      albertel  386: }
                    387: 
                    388: sub setup_params {
                    389:   my ($tag) = @_;
                    390: 
1.52      albertel  391:   if ($ENV{'request.state'} eq 'construct') { return; }
1.60    ! albertel  392:   %Apache::inputtags::params=();
1.43      albertel  393:   my %paramlist=();
1.42      albertel  394:   foreach my $key (keys(%Apache::lonnet::packagetab)) {
                    395:     if ($key =~ /^$tag/) {
                    396:       my ($package,$name) = split(/&/,$key);
1.43      albertel  397:       $paramlist{$name}=1;
1.42      albertel  398:     }
                    399:   }
1.43      albertel  400:   foreach my $key (keys(%paramlist)) {
1.42      albertel  401:     my $entry= 'resource.'.$Apache::inputtags::part;
                    402:     if (defined($Apache::inputtags::response[-1])) {
                    403:       $entry.='_'.$Apache::inputtags::response[-1];
                    404:     }
                    405:     $entry.='.'.$key;
                    406:     &Apache::lonxml::debug("looking for $entry");
                    407:     my $value = &Apache::lonnet::EXT("$entry");
                    408:     &Apache::lonxml::debug("$key has value :$value:");
                    409:     if ($value eq 'con_lost' || $value =~ /^error:/) {
                    410:       &Apache::lonxml::debug("using nothing");
                    411:       $Apache::inputtags::params{$key}='';
                    412:     } else {
                    413:       &Apache::lonxml::debug("using value");
                    414:       $Apache::inputtags::params{$key}=$value;
                    415:     }
                    416:   }
1.48      albertel  417: }
                    418: 
                    419: sub answer_header {
                    420:   my ($type) = @_;
                    421:   my $result;
1.50      albertel  422:   if ($type eq 'optionresponse' || $type eq 'radiobuttonresponse' ) {
1.48      albertel  423:     $result = '<table border="1"><tr><th>Answer for Part:'.
                    424:       $Apache::inputtags::part. '</th></tr><tr>'."\n";
                    425:   } else {
1.49      albertel  426:     $result = '<table border="1"><tr><td>Answer for Part:'.
                    427:       $Apache::inputtags::part. '</td>'."\n";
1.48      albertel  428:   }
                    429:   return $result;
                    430: }
                    431: 
                    432: sub answer_part {
                    433:   my ($type,$answer) = @_;
                    434:   my $result;
1.50      albertel  435:   if ($type eq 'optionresponse' || $type eq 'radiobuttonresponse') {
1.48      albertel  436:     $result = '<td>'.$answer.'</td>';
                    437:   } else {
                    438:     $result = '<td>'.$answer.'</td>';
                    439:   }
                    440:   return $result;
                    441: }
                    442: 
                    443: sub answer_footer {
                    444:   my ($type) = @_;
                    445:   my $result;
1.50      albertel  446:   if ($type eq 'optionresponse' || $type eq 'radiobuttonresponse') {
1.48      albertel  447:     $result = '</tr></table>';
                    448:   } else {
                    449:     $result = '</tr></table>';
                    450:   }
                    451:   return $result;
1.1       albertel  452: }
1.2       albertel  453: 
1.1       albertel  454: 1;
                    455: __END__
1.38      albertel  456:  

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