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

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

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