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

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

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