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

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.38      albertel   47:   my $rndseed=&Apache::lonnet::rndseed();
                     48:   &Apache::lonxml::debug("randseed $rndseed");
                     49: #  $rndseed=unpack("%32i",$rndseed);
                     50:   $rndseed=$rndseed
                     51:     +(&Apache::lonnet::numval($Apache::inputtags::part) << 10);
                     52:   if (defined($Apache::inputtags::response['-1'])) {
                     53:     $rndseed=$rndseed
                     54:       +&Apache::lonnet::numval($Apache::inputtags::response['-1']);
                     55:   }
                     56:   srand($rndseed);
                     57:   &Apache::lonxml::debug("randseed $rndseed");
                     58:   return '';
1.26      albertel   59: }
                     60: 
1.7       www        61: sub meta_parameter_write {
1.38      albertel   62:     my ($name,$type,$default,$display)=@_;
1.41      albertel   63:     my $partref=$Apache::inputtags::part;
                     64:     my $result='<parameter part="'.$Apache::inputtags::part.'"';
                     65:     if (defined($Apache::inputtags::response[-1])) {
                     66:       $result.=            ' id="'.$Apache::inputtags::response[-1].'"';
                     67:       $partref.='_'.$Apache::inputtags::response[-1];
                     68:     }
                     69:     $result.=            ' name="'.$name.'"'.
                     70:                          ' type="'.$type.'"'.
                     71: ($default?' default="'.$default.'"':'').
                     72: ($display?' display="'.$display.' [Part: '.$partref.']"':'')
                     73:              .'></parameter>'
                     74:              ."\n";
                     75:     return $result;
1.33      www        76: }
                     77: 
                     78: sub meta_package_write {
                     79:     my $name=shift;
1.41      albertel   80:     my $result = '<parameter part="'.$Apache::inputtags::part.'"';
                     81:     if(defined($Apache::inputtags::response[-1])) {
                     82:       $result.=              ' id="'.$Apache::inputtags::response[-1].'"';
                     83:     }
                     84:     $result.=' package="'.$name.'"></parameter>'."\n";
                     85:     return $result;
1.7       www        86: }
                     87: 
                     88: sub meta_stores_write {
1.10      www        89:     my ($name,$type,$display)=@_;
1.41      albertel   90:     my $partref=$Apache::inputtags::part;
                     91:     my $result = '<stores part="'.$Apache::inputtags::part.'"';
                     92:     if (defined($Apache::inputtags::response[-1])) {
                     93:       $result.=           ' id="'.$Apache::inputtags::response[-1].'"';
                     94:       $partref.='_'.$Apache::inputtags::response[-1];
                     95:     }	
                     96:     $result.=          ' name="'.$name.'"'.
                     97:                        ' type="'.$type.'"'.
                     98: 	            ' display="'.$display.' [Part: '.$partref.']"'.
                     99: 		      "></stores>\n";
1.7       www       100: }
                    101: 
                    102: sub mandatory_part_meta {
                    103: #
                    104: # Autogenerate metadata for mandatory
                    105: # input (from RAT or lonparmset) and 
                    106: # output (to lonspreadsheet)
                    107: # of each part
                    108: #
1.38      albertel  109:  return
1.34      www       110: #    &meta_parameter_write('opendate','date_start','',
                    111: #                          'Opening Date').
                    112: #    &meta_parameter_write('duedate','date_end','',
                    113: #                          'Due Date').
                    114: #    &meta_parameter_write('answerdate','date_start','',
                    115: #                          'Show Answer Date').
                    116: #    &meta_parameter_write('weight','int_zeropos','',
                    117: #                          'Available Points').
                    118: #    &meta_parameter_write('maxtries','int_pos','',
                    119: #                          'Maximum Number of Tries').
1.39      www       120:     &meta_package_write('part').
1.10      www       121:     &meta_stores_write('solved','string',
1.7       www       122:                           'Problem Status').
1.38      albertel  123:     &meta_stores_write('tries','int_zeropos',
1.24      albertel  124:                           'Number of Attempts').
1.38      albertel  125:     &meta_stores_write('awarded','float',
1.7       www       126: 		          'Partial Credit Factor');
                    127: #
                    128: # Note: responseid-specific data 'submission' and 'awarddetail'
                    129: # not available to spreadsheet -> skip here
                    130: #
1.14      albertel  131: }
                    132: 
1.15      albertel  133: sub check_for_previous {
1.38      albertel  134:   my ($curresponse,$partid,$id) = @_;
                    135:   my %previous;
                    136:   $previous{'used'} = 0;
                    137:   foreach my $key (reverse(sort(keys(%Apache::lonhomework::history)))) {
                    138:     if ($key =~ /resource\.$partid\.$id\.submission/) {
                    139:       &Apache::lonxml::debug("Trying $key");
                    140:       my $pastresponse=$Apache::lonhomework::history{$key};
                    141:       if ($pastresponse eq $curresponse) {
                    142: 	$previous{'used'} = 1;
                    143: 	my $history;
                    144: 	if ( $key =~ /^(\d+):/ ) {
                    145: 	  $history=$1;
                    146: 	  $previous{'award'} = $Apache::lonhomework::history{"$history:resource.$partid.$id.awarddetail"};
                    147: 	  $previous{'last'}='0';
                    148: 	} else {
                    149: 	  $previous{'award'} = $Apache::lonhomework::history{"resource.$partid.$id.awarddetail"};
                    150: 	  $previous{'last'}='1';
1.32      albertel  151: 	}
1.38      albertel  152: 	if (! $previous{'award'} ) { $previous{'award'} = 'UNKNOWN';	}
                    153: 	&Apache::lonxml::debug("got a match :$previous{'award'}:$previous{'used'}:");
                    154: 	last;
                    155:       }
1.32      albertel  156:     }
1.38      albertel  157:   }
                    158:   return %previous;
1.15      albertel  159: }
                    160: 
1.31      albertel  161: sub start_caparesponse {
1.38      albertel  162:   require Apache::caparesponse; 
                    163:   import Apache::caparesponse; 
                    164:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.31      albertel  165: #  print "\n<br />\nsimple caparesponse\n";
1.38      albertel  166:   return &Apache::caparesponse::start_caparesponse($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
1.31      albertel  167: }
                    168: 
                    169: sub start_stringresponse {
1.38      albertel  170:   require Apache::caparesponse;
                    171:   import Apache::caparesponse;
                    172:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    173:   return &Apache::caparesponse::start_stringresponse($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
1.7       www       174: }
                    175: 
1.31      albertel  176: sub start_numericalresponse {
1.1       albertel  177:   require Apache::caparesponse; 
                    178:   import Apache::caparesponse; 
1.38      albertel  179:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.35      albertel  180:   return &Apache::caparesponse::start_numericalresponse
                    181:     ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
1.16      albertel  182: }
                    183: 
                    184: sub start_radiobuttonresponse {
1.38      albertel  185:   require Apache::radiobuttonresponse; 
                    186:   import Apache::radiobuttonresponse; 
                    187:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    188:   return &Apache::radiobuttonresponse::start_radiobuttonresponse($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
1.19      albertel  189: }
                    190: 
                    191: sub start_optionresponse {
1.38      albertel  192:   require Apache::optionresponse; 
                    193:   import Apache::optionresponse; 
                    194:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    195:   return &Apache::optionresponse::start_optionresponse($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
1.21      albertel  196: }
                    197: 
                    198: sub start_imageresponse {
1.38      albertel  199:   require Apache::imageresponse; 
                    200:   import Apache::imageresponse; 
                    201:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    202:   return &Apache::imageresponse::start_imageresponse($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
1.23      albertel  203: }
                    204: 
                    205: sub start_essayresponse {
1.38      albertel  206:   require Apache::essayresponse; 
                    207:   import Apache::essayresponse; 
                    208:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    209:   return &Apache::essayresponse::start_essayresponse($target,$token,$tagstack,$parstack,$parser,$safeeval,$style);
1.44      albertel  210: }
                    211: 
1.45      albertel  212: sub view_or_modify {
1.47    ! albertel  213:   my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
1.45      albertel  214:   my $myself=0;
1.46      albertel  215:   if ( ($name eq $ENV{'user.name'}) && ($domain eq $ENV{'user.domain'}) ) {
1.45      albertel  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: 
1.44      albertel  226: sub start_dataresponse {
                    227:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    228:   my $id = &Apache::response::start_response($parstack,$safeeval);
                    229:   my $result;
1.45      albertel  230:   if ($target eq 'web') {
                    231:     $result = $token->[2]->{'display'}.':';
                    232:   } elsif ($target eq 'meta') {
1.44      albertel  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'}) {
1.47    ! albertel  247:       my ($symb,$courseid,$domain,$name)=&Apache::lonxml::whichuser();
1.45      albertel  248:       my $allowed=&Apache::lonnet::allowed('mgr',$courseid);
                    249:       if ($allowed) {
                    250: 	&Apache::response::setup_params('datasubmission');
                    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.'
1.44      albertel  267:       }
                    268:     }
                    269:   }
                    270:   &Apache::response::end_response;
                    271:   return $result;
1.3       albertel  272: }
                    273: 
                    274: sub start_responseparam {
1.38      albertel  275:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.43      albertel  276:   my $result='';
1.42      albertel  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'});
1.38      albertel  282:   } elsif ($target eq 'edit') {
1.42      albertel  283:     $result.=&Apache::edit::tag_start($target,$token,
                    284: 				      &Apache::lonxml::description($token));
1.38      albertel  285:     $result.=&Apache::edit::text_arg('Name:','name',$token).
                    286:       &Apache::edit::text_arg('Type:','type',$token).
                    287: 	&Apache::edit::text_arg('Description:','description',$token).
                    288: 	  &Apache::edit::text_arg('Default:','default',$token).
                    289: 	    "</td></tr>";
                    290:     $result.=&Apache::edit::end_table;
                    291:   } elsif ($target eq 'modified') {
                    292:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
                    293: 						 'name','type','description',
                    294: 						 'default');
                    295:     if ($constructtag) {
                    296:       $result = &Apache::edit::rebuild_tag($token);
                    297:       $result.=&Apache::edit::handle_insert();
                    298:     }
                    299:   }
1.42      albertel  300:   return $result;
1.3       albertel  301: }
                    302: 
                    303: sub end_responseparam {
1.38      albertel  304:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    305:   if ($target eq 'edit') { return ('','no'); }
                    306:   return '';
1.42      albertel  307: }
                    308: 
                    309: sub setup_params {
                    310:   my ($tag) = @_;
                    311: 
                    312:   @Apache::inputtags::params=();
1.43      albertel  313:   my %paramlist=();
1.42      albertel  314:   foreach my $key (keys(%Apache::lonnet::packagetab)) {
                    315:     if ($key =~ /^$tag/) {
                    316:       my ($package,$name) = split(/&/,$key);
1.43      albertel  317:       $paramlist{$name}=1;
1.42      albertel  318:     }
                    319:   }
1.43      albertel  320:   foreach my $key (keys(%paramlist)) {
1.42      albertel  321:     my $entry= 'resource.'.$Apache::inputtags::part;
                    322:     if (defined($Apache::inputtags::response[-1])) {
                    323:       $entry.='_'.$Apache::inputtags::response[-1];
                    324:     }
                    325:     $entry.='.'.$key;
                    326:     &Apache::lonxml::debug("looking for $entry");
                    327:     my $value = &Apache::lonnet::EXT("$entry");
                    328:     &Apache::lonxml::debug("$key has value :$value:");
                    329:     if ($value eq 'con_lost' || $value =~ /^error:/) {
                    330:       &Apache::lonxml::debug("using nothing");
                    331:       $Apache::inputtags::params{$key}='';
                    332:     } else {
                    333:       &Apache::lonxml::debug("using value");
                    334:       $Apache::inputtags::params{$key}=$value;
                    335:     }
                    336:   }
1.1       albertel  337: }
1.2       albertel  338: 
1.1       albertel  339: 1;
                    340: __END__
1.38      albertel  341:  

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