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

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

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