File:  [LON-CAPA] / loncom / homework / response.pm
Revision 1.68: download - view: text, annotated - select for diffs
Wed Nov 27 19:25:56 2002 UTC (21 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: version_0_6_2, version_0_6, HEAD
- Fixes BUG#965, multiple responses in the same part (AND style)
  now properly detect whether a situation is a previous submission or
  not, previously it would detect a situation in which the same individual
  response had appeared before (but not together) as a previously seen
  submission, now it properly only detects when a group of submissions
  is the same.

    1: # The LearningOnline Network with CAPA
    2: # various response type definitons response definition
    3: #
    4: # $Id: response.pm,v 1.68 2002/11/27 19:25:56 albertel Exp $
    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: #
   28: # 11/23,11/24,11/28 Gerd Kortemeyer
   29: # Guy Albertelli
   30: # 08/04,08/07 Gerd Kortemeyer
   31: 
   32: package Apache::response;
   33: use strict;
   34: 
   35: BEGIN {
   36:   &Apache::lonxml::register('Apache::response',('responseparam','parameter','dataresponse'));
   37: }
   38: 
   39: sub start_response {
   40:   my ($parstack,$safeeval)=@_;
   41:   my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
   42:   if ($id eq '') { $id = $Apache::lonxml::curdepth; }
   43:   if ($#Apache::inputtags::import > -1) {
   44:     &Apache::lonxml::debug("Turning :$id: into");
   45:     $id = join('_',@Apache::inputtags::import).'_'.$id;
   46:     &Apache::lonxml::debug("New  :$id:");
   47:   }
   48:   push (@Apache::inputtags::response,$id);
   49:   push (@Apache::inputtags::responselist,$id);
   50:   @Apache::inputtags::inputlist=();
   51:   return $id;
   52: }
   53: 
   54: sub end_response {
   55:   pop @Apache::inputtags::response;
   56:   @Apache::inputtags::inputlist=();
   57:   return '';
   58: }
   59: 
   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:   push (@Apache::inputtags::paramstack,[%Apache::inputtags::params]);
   66:   return $id;
   67: }
   68: 
   69: sub end_hintresponse {
   70:   pop @Apache::inputtags::response;
   71:   %Apache::inputtags::params=@{pop(@Apache::inputtags::paramstack)};
   72:   return '';
   73: }
   74: 
   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
   77: sub setrandomnumber {
   78:   my $rndseed;
   79:   if ($ENV{'request.state'} eq "construct") {
   80:     $rndseed=$ENV{'form.rndseed'};
   81:     if (!$rndseed) { $rndseed=time; }
   82:   } else {
   83:     my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
   84:     $rndseed=&Apache::lonnet::rndseed($symb,$courseid,$domain,$name);
   85:   }
   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:   &Math::Random::random_set_seed_from_phrase($rndseed);
   95:   &Apache::lonxml::debug("randseed $rndseed");
   96:   return '';
   97: }
   98: 
   99: sub meta_parameter_write {
  100:     my ($name,$type,$default,$display)=@_;
  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;
  114: }
  115: 
  116: sub meta_package_write {
  117:     my $name=shift;
  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;
  124: }
  125: 
  126: sub meta_stores_write {
  127:     my ($name,$type,$display)=@_;
  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";
  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: #
  147:  return
  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').
  158:     &meta_package_write('part').
  159:     &meta_stores_write('solved','string',
  160:                           'Problem Status').
  161:     &meta_stores_write('tries','int_zeropos',
  162:                           'Number of Attempts').
  163:     &meta_stores_write('awarded','float',
  164: 		          'Partial Credit Factor');
  165: #
  166: # Note: responseid-specific data 'submission' and 'awarddetail'
  167: # not available to spreadsheet -> skip here
  168: #
  169: }
  170: 
  171: sub check_for_previous {
  172:   my ($curresponse,$partid,$id) = @_;
  173:   my %previous;
  174:   $previous{'used'} = 0;
  175:   foreach my $key (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: 	  push(@{ $previous{'version'} },$history);
  187: 	} else {
  188: 	  $previous{'award'} = $Apache::lonhomework::history{"resource.$partid.$id.awarddetail"};
  189: 	  $previous{'last'}='1';
  190: 	}
  191: 	if (! $previous{'award'} ) { $previous{'award'} = 'UNKNOWN';	}
  192: 	&Apache::lonxml::debug("got a match :$previous{'award'}:$previous{'used'}:");
  193:       }
  194:     }
  195:   }
  196:   &Apache::lonhomework::showhash(%previous);
  197:   return %previous;
  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:     }
  208:     push(@Apache::inputtags::previous_version,$$previous{'version'});
  209:   }
  210: }
  211: 
  212: sub view_or_modify {
  213:   my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
  214:   my $myself=0;
  215:   if ( ($name eq $ENV{'user.name'}) && ($domain eq $ENV{'user.domain'}) ) {
  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: 
  226: sub start_dataresponse {
  227:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  228:   my $id = &Apache::response::start_response($parstack,$safeeval);
  229:   my $result;
  230:   if ($target eq 'web') {
  231:     $result = $token->[2]->{'display'}.':';
  232:   } elsif ($target eq 'meta') {
  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'}) {
  247:       my ($symb,$courseid,$domain,$name)=&Apache::lonxml::whichuser();
  248:       my $allowed=&Apache::lonnet::allowed('mgr',$courseid);
  249:       if ($allowed) {
  250: 	&Apache::response::setup_params('dataresponse');
  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.'
  267:       }
  268:     }
  269:   }
  270:   &Apache::response::end_response;
  271:   return $result;
  272: }
  273: 
  274: sub start_responseparam {
  275:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  276:   my $result='';
  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'});
  282:   } elsif ($target eq 'edit') {
  283:     $result.=&Apache::edit::tag_start($target,$token);
  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:     }
  298:   } elsif ($target eq 'grade' || $target eq 'answer' || $target eq 'web' ||
  299: 	   $target eq 'tex' || $target eq 'analyze' ) {
  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);
  303:       if ($name) {$Apache::inputtags::params{$name}=$default;}
  304:     }
  305:   }
  306:   return $result;
  307: }
  308: 
  309: sub end_responseparam {
  310:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  311:   if ($target eq 'edit') { return ('','no'); }
  312:   return '';
  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;
  323: }
  324: 
  325: sub reset_params {
  326:     %Apache::inputtags::params=();
  327: }
  328: 
  329: sub setup_params {
  330:   my ($tag) = @_;
  331: 
  332:   if ($ENV{'request.state'} eq 'construct') { return; }
  333:   my %paramlist=();
  334:   foreach my $key (keys(%Apache::lonnet::packagetab)) {
  335:     if ($key =~ /^$tag/) {
  336:       my ($package,$name) = split(/&/,$key);
  337:       $paramlist{$name}=1;
  338:     }
  339:   }
  340:   foreach my $key (keys(%paramlist)) {
  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:   }
  357: }
  358: 
  359: sub answer_header {
  360:   my ($type) = @_;
  361:   my $result;
  362:   if ($type eq 'optionresponse' || $type eq 'radiobuttonresponse' ) {
  363:     $result = '<table border="1"><tr><th>Answer for Part:'.
  364:       $Apache::inputtags::part. '</th></tr><tr>'."\n";
  365:   } else {
  366:     $result = '<table border="1"><tr><td>Answer for Part:'.
  367:       $Apache::inputtags::part. '</td>'."\n";
  368:   }
  369:   return $result;
  370: }
  371: 
  372: sub answer_part {
  373:   my ($type,$answer) = @_;
  374:   my $result;
  375:   if ($type eq 'optionresponse' || $type eq 'radiobuttonresponse') {
  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;
  386:   if ($type eq 'optionresponse' || $type eq 'radiobuttonresponse') {
  387:     $result = '</tr></table>';
  388:   } else {
  389:     $result = '</tr></table>';
  390:   }
  391:   return $result;
  392: }
  393: 
  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: }
  402: 1;
  403: __END__
  404:  

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