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

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.44      albertel   12:   &Apache::lonxml::register('Apache::response',('responseparam','caparesponse','numericalresponse','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.31      albertel  182: sub start_numericalresponse {
1.1       albertel  183:   require Apache::caparesponse; 
                    184:   import Apache::caparesponse; 
1.38      albertel  185:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.35      albertel  186:   return &Apache::caparesponse::start_numericalresponse
                    187:     ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
1.16      albertel  188: }
                    189: 
                    190: sub start_radiobuttonresponse {
1.38      albertel  191:   require Apache::radiobuttonresponse; 
                    192:   import Apache::radiobuttonresponse; 
                    193:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    194:   return &Apache::radiobuttonresponse::start_radiobuttonresponse($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
1.19      albertel  195: }
                    196: 
                    197: sub start_optionresponse {
1.38      albertel  198:   require Apache::optionresponse; 
                    199:   import Apache::optionresponse; 
                    200:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    201:   return &Apache::optionresponse::start_optionresponse($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
1.21      albertel  202: }
                    203: 
                    204: sub start_imageresponse {
1.38      albertel  205:   require Apache::imageresponse; 
                    206:   import Apache::imageresponse; 
                    207:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    208:   return &Apache::imageresponse::start_imageresponse($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
1.23      albertel  209: }
                    210: 
                    211: sub start_essayresponse {
1.38      albertel  212:   require Apache::essayresponse; 
                    213:   import Apache::essayresponse; 
                    214:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    215:   return &Apache::essayresponse::start_essayresponse($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
1.44      albertel  216: }
                    217: 
1.45      albertel  218: sub view_or_modify {
1.47      albertel  219:   my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
1.45      albertel  220:   my $myself=0;
1.46      albertel  221:   if ( ($name eq $ENV{'user.name'}) && ($domain eq $ENV{'user.domain'}) ) {
1.45      albertel  222:     $myself=1;
                    223:   }
                    224:   my $vgr=&Apache::lonnet::allowed('vgr',$courseid);
                    225:   my $mgr=&Apache::lonnet::allowed('vgr',$courseid);
                    226:   if ($mgr) { return "M"; }
                    227:   if ($vgr) { return "V"; }
                    228:   if ($myself) { return "V"; }
                    229:   return '';
                    230: }
                    231: 
1.44      albertel  232: sub start_dataresponse {
                    233:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    234:   my $id = &Apache::response::start_response($parstack,$safeeval);
                    235:   my $result;
1.45      albertel  236:   if ($target eq 'web') {
                    237:     $result = $token->[2]->{'display'}.':';
                    238:   } elsif ($target eq 'meta') {
1.44      albertel  239:     $result = &Apache::response::meta_stores_write($token->[2]->{'name'},
                    240: 						   $token->[2]->{'type'},
                    241: 						   $token->[2]->{'display'});
                    242:     $result .= &Apache::response::meta_package_write('dataresponse');
                    243:   }
                    244:   return $result;
                    245: }
                    246: 
                    247: sub end_dataresponse {
                    248:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    249:   my $result;
                    250:   if ( $target eq 'web' ) {
                    251:   } elsif ($target eq 'grade' ) {
                    252:     if ( defined $ENV{'form.submitted'}) {
1.47      albertel  253:       my ($symb,$courseid,$domain,$name)=&Apache::lonxml::whichuser();
1.45      albertel  254:       my $allowed=&Apache::lonnet::allowed('mgr',$courseid);
                    255:       if ($allowed) {
                    256: 	&Apache::response::setup_params('datasubmission');
                    257: 	my $partid = $Apache::inputtags::part;
                    258: 	my $id = $Apache::inputtags::response['-1'];
                    259: 	my $response = $ENV{'form.HWVAL'.$id};
                    260: 	my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    261: 	if ( $response =~ /[^\s]/) {
                    262: 	  $Apache::lonhomework::results{"resource.$partid.$id.$name"}=
                    263: 	    $response;
                    264: 	  $Apache::lonhomework::results{
                    265: 					"resource.$partid.$id.submission"}=
                    266: 					  $response;
                    267: 	  $Apache::lonhomework::results{
                    268: 					"resource.$partid.$id.awarddetail"}=
                    269: 					  'SUBMITTED';
                    270: 	}
                    271:       } else {
                    272: 	$result='Not Permitted to change values.'
1.44      albertel  273:       }
                    274:     }
                    275:   }
                    276:   &Apache::response::end_response;
                    277:   return $result;
1.3       albertel  278: }
                    279: 
                    280: sub start_responseparam {
1.38      albertel  281:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.43      albertel  282:   my $result='';
1.42      albertel  283:   if ($target eq 'meta') {
                    284:     $result = &meta_parameter_write($token->[2]->{'name'},
                    285: 				    $token->[2]->{'type'},
                    286: 				    $token->[2]->{'default'},
                    287: 				    $token->[2]->{'description'});
1.38      albertel  288:   } elsif ($target eq 'edit') {
1.42      albertel  289:     $result.=&Apache::edit::tag_start($target,$token,
                    290: 				      &Apache::lonxml::description($token));
1.38      albertel  291:     $result.=&Apache::edit::text_arg('Name:','name',$token).
                    292:       &Apache::edit::text_arg('Type:','type',$token).
                    293: 	&Apache::edit::text_arg('Description:','description',$token).
                    294: 	  &Apache::edit::text_arg('Default:','default',$token).
                    295: 	    "</td></tr>";
                    296:     $result.=&Apache::edit::end_table;
                    297:   } elsif ($target eq 'modified') {
                    298:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
                    299: 						 'name','type','description',
                    300: 						 'default');
                    301:     if ($constructtag) {
                    302:       $result = &Apache::edit::rebuild_tag($token);
                    303:       $result.=&Apache::edit::handle_insert();
                    304:     }
                    305:   }
1.42      albertel  306:   return $result;
1.3       albertel  307: }
                    308: 
                    309: sub end_responseparam {
1.38      albertel  310:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    311:   if ($target eq 'edit') { return ('','no'); }
                    312:   return '';
1.42      albertel  313: }
                    314: 
                    315: sub setup_params {
                    316:   my ($tag) = @_;
                    317: 
                    318:   @Apache::inputtags::params=();
1.43      albertel  319:   my %paramlist=();
1.42      albertel  320:   foreach my $key (keys(%Apache::lonnet::packagetab)) {
                    321:     if ($key =~ /^$tag/) {
                    322:       my ($package,$name) = split(/&/,$key);
1.43      albertel  323:       $paramlist{$name}=1;
1.42      albertel  324:     }
                    325:   }
1.43      albertel  326:   foreach my $key (keys(%paramlist)) {
1.42      albertel  327:     my $entry= 'resource.'.$Apache::inputtags::part;
                    328:     if (defined($Apache::inputtags::response[-1])) {
                    329:       $entry.='_'.$Apache::inputtags::response[-1];
                    330:     }
                    331:     $entry.='.'.$key;
                    332:     &Apache::lonxml::debug("looking for $entry");
                    333:     my $value = &Apache::lonnet::EXT("$entry");
                    334:     &Apache::lonxml::debug("$key has value :$value:");
                    335:     if ($value eq 'con_lost' || $value =~ /^error:/) {
                    336:       &Apache::lonxml::debug("using nothing");
                    337:       $Apache::inputtags::params{$key}='';
                    338:     } else {
                    339:       &Apache::lonxml::debug("using value");
                    340:       $Apache::inputtags::params{$key}=$value;
                    341:     }
                    342:   }
1.48    ! albertel  343: }
        !           344: 
        !           345: sub answer_header {
        !           346:   my ($type) = @_;
        !           347:   my $result;
        !           348:   if ($type eq 'optionresponse') {
        !           349:     $result = '<table border="1"><tr><th>Answer for Part:'.
        !           350:       $Apache::inputtags::part. '</th></tr><tr>'."\n";
        !           351:   } else {
        !           352:     $result = '<table><tr><td>Answer for Part:'.$Apache::inputtags::part.
        !           353:       '</td>'."\n";
        !           354:   }
        !           355:   return $result;
        !           356: }
        !           357: 
        !           358: sub answer_part {
        !           359:   my ($type,$answer) = @_;
        !           360:   my $result;
        !           361:   if ($type eq 'optionresponse') {
        !           362:     $result = '<td>'.$answer.'</td>';
        !           363:   } else {
        !           364:     $result = '<td>'.$answer.'</td>';
        !           365:   }
        !           366:   return $result;
        !           367: }
        !           368: 
        !           369: sub answer_footer {
        !           370:   my ($type) = @_;
        !           371:   my $result;
        !           372:   if ($type eq 'optionresponse') {
        !           373:     $result = '</tr></table>';
        !           374:   } else {
        !           375:     $result = '</tr></table>';
        !           376:   }
        !           377:   return $result;
1.1       albertel  378: }
1.2       albertel  379: 
1.1       albertel  380: 1;
                    381: __END__
1.38      albertel  382:  

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