Annotation of loncom/homework/response.pm, revision 1.82
1.38 albertel 1: # The LearningOnline Network with CAPA
1.1 albertel 2: # various response type definitons response definition
1.53 albertel 3: #
1.82 ! albertel 4: # $Id: response.pm,v 1.81 2003/07/14 18:41:40 sakharuk 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:
1.57 harris41 35: BEGIN {
1.73 albertel 36: &Apache::lonxml::register('Apache::response',('responseparam','parameter','dataresponse'));
1.1 albertel 37: }
38:
1.13 albertel 39: sub start_response {
1.73 albertel 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;
1.13 albertel 52: }
53:
54: sub end_response {
1.79 albertel 55: #pop @Apache::inputtags::response;
1.73 albertel 56: @Apache::inputtags::inputlist=();
57: return '';
1.13 albertel 58: }
59:
1.41 albertel 60: sub start_hintresponse {
1.73 albertel 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);
1.79 albertel 65: push (@Apache::inputtags::responselist,$id);
1.73 albertel 66: push (@Apache::inputtags::paramstack,[%Apache::inputtags::params]);
67: return $id;
1.41 albertel 68: }
69:
70: sub end_hintresponse {
1.73 albertel 71: pop @Apache::inputtags::response;
72: if (defined($Apache::inputtags::paramstack[-1])) {
73: %Apache::inputtags::params=
74: @{ pop(@Apache::inputtags::paramstack) };
75: }
76: return '';
1.41 albertel 77: }
78:
1.38 albertel 79: # used by response to set the non-safe space random number generator to something
80: # that is stable and unique based on the part number and response number
1.26 albertel 81: sub setrandomnumber {
1.73 albertel 82: my $rndseed;
83: if ($ENV{'request.state'} eq "construct") {
84: $rndseed=$ENV{'form.rndseed'};
85: if (!$rndseed) { $rndseed=time; }
86: } else {
1.74 albertel 87: $rndseed=&Apache::lonnet::rndseed();
1.73 albertel 88: }
89: &Apache::lonxml::debug("randseed $rndseed");
90: # $rndseed=unpack("%32i",$rndseed);
1.74 albertel 91: my $rndmod=(&Apache::lonnet::numval($Apache::inputtags::part) << 10);
1.73 albertel 92: if (defined($Apache::inputtags::response['-1'])) {
1.74 albertel 93: $rndmod+=&Apache::lonnet::numval($Apache::inputtags::response[-1]);
1.73 albertel 94: }
1.74 albertel 95: if ($rndseed =~/,/) {
1.82 ! albertel 96: {
! 97: use integer;
! 98: my ($num1,$num2)=split(/,/,$rndseed);
! 99: $num1+=$rndmod;
! 100: $num2+=$rndmod;
! 101: $rndseed="$num1,$num2";
! 102: }
1.74 albertel 103: } else {
104: $rndseed+=$rndmod;
105: }
106: &Apache::lonnet::setup_random_from_rndseed($rndseed);
1.73 albertel 107: &Apache::lonxml::debug("randseed $rndseed");
108: return '';
1.26 albertel 109: }
110:
1.7 www 111: sub meta_parameter_write {
1.38 albertel 112: my ($name,$type,$default,$display)=@_;
1.41 albertel 113: my $partref=$Apache::inputtags::part;
114: my $result='<parameter part="'.$Apache::inputtags::part.'"';
115: if (defined($Apache::inputtags::response[-1])) {
1.73 albertel 116: $result.= ' id="'.$Apache::inputtags::response[-1].'"';
117: $partref.='_'.$Apache::inputtags::response[-1];
1.41 albertel 118: }
119: $result.= ' name="'.$name.'"'.
120: ' type="'.$type.'"'.
121: ($default?' default="'.$default.'"':'').
122: ($display?' display="'.$display.' [Part: '.$partref.']"':'')
123: .'></parameter>'
124: ."\n";
125: return $result;
1.33 www 126: }
127:
128: sub meta_package_write {
129: my $name=shift;
1.41 albertel 130: my $result = '<parameter part="'.$Apache::inputtags::part.'"';
131: if(defined($Apache::inputtags::response[-1])) {
1.73 albertel 132: $result.= ' id="'.$Apache::inputtags::response[-1].'"';
1.41 albertel 133: }
134: $result.=' package="'.$name.'"></parameter>'."\n";
135: return $result;
1.7 www 136: }
137:
138: sub meta_stores_write {
1.10 www 139: my ($name,$type,$display)=@_;
1.41 albertel 140: my $partref=$Apache::inputtags::part;
141: my $result = '<stores part="'.$Apache::inputtags::part.'"';
142: if (defined($Apache::inputtags::response[-1])) {
1.73 albertel 143: $result.= ' id="'.$Apache::inputtags::response[-1].'"';
144: $partref.='_'.$Apache::inputtags::response[-1];
1.41 albertel 145: }
146: $result.= ' name="'.$name.'"'.
147: ' type="'.$type.'"'.
148: ' display="'.$display.' [Part: '.$partref.']"'.
149: "></stores>\n";
1.7 www 150: }
151:
152: sub mandatory_part_meta {
153: #
154: # Autogenerate metadata for mandatory
155: # input (from RAT or lonparmset) and
156: # output (to lonspreadsheet)
157: # of each part
158: #
1.73 albertel 159: return
1.34 www 160: # &meta_parameter_write('opendate','date_start','',
161: # 'Opening Date').
162: # &meta_parameter_write('duedate','date_end','',
163: # 'Due Date').
164: # &meta_parameter_write('answerdate','date_start','',
165: # 'Show Answer Date').
166: # &meta_parameter_write('weight','int_zeropos','',
167: # 'Available Points').
168: # &meta_parameter_write('maxtries','int_pos','',
169: # 'Maximum Number of Tries').
1.73 albertel 170: &meta_package_write('part').
171: &meta_stores_write('solved','string',
172: 'Problem Status').
173: &meta_stores_write('tries','int_zeropos',
174: 'Number of Attempts').
175: &meta_stores_write('awarded','float',
176: 'Partial Credit Factor');
1.7 www 177: #
178: # Note: responseid-specific data 'submission' and 'awarddetail'
179: # not available to spreadsheet -> skip here
180: #
1.14 albertel 181: }
182:
1.15 albertel 183: sub check_for_previous {
1.73 albertel 184: my ($curresponse,$partid,$id) = @_;
185: my %previous;
186: $previous{'used'} = 0;
187: foreach my $key (sort(keys(%Apache::lonhomework::history))) {
188: if ($key =~ /resource\.$partid\.$id\.submission/) {
189: &Apache::lonxml::debug("Trying $key");
190: my $pastresponse=$Apache::lonhomework::history{$key};
191: if ($pastresponse eq $curresponse) {
192: $previous{'used'} = 1;
193: my $history;
194: if ( $key =~ /^(\d+):/ ) {
195: $history=$1;
196: $previous{'award'} = $Apache::lonhomework::history{"$history:resource.$partid.$id.awarddetail"};
197: $previous{'last'}='0';
198: push(@{ $previous{'version'} },$history);
199: } else {
200: $previous{'award'} = $Apache::lonhomework::history{"resource.$partid.$id.awarddetail"};
201: $previous{'last'}='1';
202: }
203: if (! $previous{'award'} ) { $previous{'award'} = 'UNKNOWN'; }
204: &Apache::lonxml::debug("got a match :$previous{'award'}:$previous{'used'}:");
205: }
1.32 albertel 206: }
1.73 albertel 207: }
208: &Apache::lonhomework::showhash(%previous);
209: return %previous;
1.54 albertel 210: }
211:
212: sub handle_previous {
1.73 albertel 213: my ($previous,$ad)=@_;
214: if ($$previous{'used'} && ($$previous{'award'} eq $ad) ) {
215: if ($$previous{'last'}) {
216: push(@Apache::inputtags::previous,'PREVIOUSLY_LAST');
217: } else {
218: push(@Apache::inputtags::previous,'PREVIOUSLY_USED');
219: }
220: push(@Apache::inputtags::previous_version,$$previous{'version'});
1.54 albertel 221: }
1.44 albertel 222: }
223:
1.45 albertel 224: sub view_or_modify {
1.73 albertel 225: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
226: my $myself=0;
227: if ( ($name eq $ENV{'user.name'}) && ($domain eq $ENV{'user.domain'}) ) {
228: $myself=1;
229: }
230: my $vgr=&Apache::lonnet::allowed('vgr',$courseid);
231: my $mgr=&Apache::lonnet::allowed('vgr',$courseid);
232: if ($mgr) { return "M"; }
233: if ($vgr) { return "V"; }
234: if ($myself) { return "V"; }
235: return '';
1.45 albertel 236: }
237:
1.44 albertel 238: sub start_dataresponse {
1.73 albertel 239: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
240: my $id = &Apache::response::start_response($parstack,$safeeval);
241: my $result;
242: if ($target eq 'web') {
243: $result = $token->[2]->{'display'}.':';
244: } elsif ($target eq 'meta') {
245: $result = &Apache::response::meta_stores_write($token->[2]->{'name'},
246: $token->[2]->{'type'},
247: $token->[2]->{'display'});
248: $result .= &Apache::response::meta_package_write('dataresponse');
249: }
250: return $result;
1.44 albertel 251: }
252:
253: sub end_dataresponse {
1.73 albertel 254: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
255: my $result;
256: if ( $target eq 'web' ) {
257: } elsif ($target eq 'grade' ) {
258: if ( defined $ENV{'form.submitted'}) {
259: my ($symb,$courseid,$domain,$name)=&Apache::lonxml::whichuser();
260: my $allowed=&Apache::lonnet::allowed('mgr',$courseid);
261: if ($allowed) {
262: &Apache::response::setup_params('dataresponse');
263: my $partid = $Apache::inputtags::part;
264: my $id = $Apache::inputtags::response['-1'];
265: my $response = $ENV{'form.HWVAL_'.$id};
266: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
267: if ( $response =~ /[^\s]/) {
268: $Apache::lonhomework::results{"resource.$partid.$id.$name"}=$response;
269: $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
270: $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}='SUBMITTED';
271: }
272: } else {
273: $result='Not Permitted to change values.'
274: }
1.45 albertel 275: }
1.73 albertel 276: }
277: &Apache::response::end_response;
278: return $result;
1.3 albertel 279: }
280:
281: sub start_responseparam {
1.73 albertel 282: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
283: my $result='';
284: if ($target eq 'meta') {
285: $result = &meta_parameter_write($token->[2]->{'name'},
286: $token->[2]->{'type'},
287: $token->[2]->{'default'},
288: $token->[2]->{'description'});
289: } elsif ($target eq 'edit') {
290: $result.=&Apache::edit::tag_start($target,$token);
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: } elsif ($target eq 'grade' || $target eq 'answer' || $target eq 'web' ||
306: $target eq 'tex' || $target eq 'analyze' ) {
307: if ($ENV{'request.state'} eq 'construct') {
308: my $name =&Apache::lonxml::get_param('name',$parstack,$safeeval);
309: my $default=&Apache::lonxml::get_param('default',$parstack,
310: $safeeval);
311: if ($name) {$Apache::inputtags::params{$name}=$default;}
312: }
1.52 albertel 313: }
1.73 albertel 314: return $result;
1.3 albertel 315: }
316:
317: sub end_responseparam {
1.73 albertel 318: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
319: if ($target eq 'edit') { return ('','no'); }
320: return '';
1.55 albertel 321: }
322:
323: sub start_parameter {
1.73 albertel 324: my $result = &start_responseparam(@_);
325: return $result;
1.55 albertel 326: }
327:
328: sub end_parameter {
1.73 albertel 329: my $result = &end_responseparam(@_);
330: return $result;
1.42 albertel 331: }
332:
1.67 albertel 333: sub reset_params {
334: %Apache::inputtags::params=();
335: }
336:
1.42 albertel 337: sub setup_params {
1.73 albertel 338: my ($tag) = @_;
1.42 albertel 339:
1.73 albertel 340: if ($ENV{'request.state'} eq 'construct') { return; }
341: my %paramlist=();
342: foreach my $key (keys(%Apache::lonnet::packagetab)) {
343: if ($key =~ /^$tag/) {
344: my ($package,$name) = split(/&/,$key);
345: $paramlist{$name}=1;
346: }
1.42 albertel 347: }
1.73 albertel 348: foreach my $key (keys(%paramlist)) {
349: my $entry= 'resource.'.$Apache::inputtags::part;
350: if (defined($Apache::inputtags::response[-1])) {
351: $entry.='_'.$Apache::inputtags::response[-1];
352: }
353: $entry.='.'.$key;
354: &Apache::lonxml::debug("looking for $entry");
355: my $value = &Apache::lonnet::EXT("$entry");
356: &Apache::lonxml::debug("$key has value :$value:");
357: if ($value eq 'con_lost' || $value =~ /^error:/) {
358: &Apache::lonxml::debug("using nothing");
359: $Apache::inputtags::params{$key}='';
360: } else {
361: &Apache::lonxml::debug("using value");
362: $Apache::inputtags::params{$key}=$value;
363: }
1.42 albertel 364: }
1.48 albertel 365: }
366:
367: sub answer_header {
1.73 albertel 368: my ($type) = @_;
369: my $result;
1.77 albertel 370: if ($ENV{'form.answer_output_mode'} eq 'tex') {
1.75 sakharuk 371: $result = ' \vskip 0 mm \begin{tabular}{|c|}\hline Answer for Part: '.
372: $Apache::inputtags::part.' \\\\ \hline ';
1.73 albertel 373: } else {
1.80 albertel 374: $result = '<table border="1"><tr><td>Answer for Part:'.
375: $Apache::inputtags::part. '</td>'."\n";
1.73 albertel 376: }
377: return $result;
1.48 albertel 378: }
379:
380: sub answer_part {
1.73 albertel 381: my ($type,$answer) = @_;
382: my $result;
1.77 albertel 383: if ($ENV{'form.answer_output_mode'} eq 'tex') {
1.81 sakharuk 384: $result = ' \verb|'.$answer.'|\\\\ \hline ';
1.73 albertel 385: } else {
1.80 albertel 386: $result = '<td>'.$answer.'</td>';
1.73 albertel 387: }
388: return $result;
1.48 albertel 389: }
390:
391: sub answer_footer {
1.73 albertel 392: my ($type) = @_;
393: my $result;
1.77 albertel 394: if ($ENV{'form.answer_output_mode'} eq 'tex') {
1.75 sakharuk 395: $result = ' \end{tabular} \vskip 0 mm ';
1.73 albertel 396: } else {
1.80 albertel 397: $result = '</tr></table>';
1.73 albertel 398: }
399: return $result;
1.1 albertel 400: }
1.2 albertel 401:
1.62 albertel 402: sub showallfoils {
1.73 albertel 403: my $return=0;
404: if (defined($ENV{'form.showallfoils'}) &&
405: $ENV{'request.state'} eq 'construct') {
406: $return=1;
407: }
408: return $return;
1.70 albertel 409: }
410:
411: sub getresponse {
412: my ($temp)=@_;
413: my $formparm='form.HWVAL_'.$Apache::inputtags::response['-1'];
414: my $response;
415: if (!defined($temp)) {
416: $temp=1;
417: } else {
418: $formparm.=":$temp";
419: }
420: my %let_to_num=('A'=>0,'B'=>1,'C'=>2,'D'=>3,'E'=>4,'F'=>5,'G'=>6,'H'=>7,
421: 'I'=>8,'J'=>9,'K'=>10,'L'=>11,'M'=>12,'N'=>13,'O'=>14,
422: 'P'=>15,'Q'=>16,'R'=>17,'S'=>18,'T'=>19,'U'=>20,'V'=>21,
423: 'W'=>22,'X'=>23,'Y'=>24,'Z'=>25);
424: if ($ENV{'form.submitted'} eq 'scantron') {
1.71 albertel 425: my $part = $Apache::inputtags::part;
426: my $id = $Apache::inputtags::response[-1];
1.70 albertel 427: $response = $ENV{'scantron.'.($Apache::lonxml::counter+$temp-1).
428: '.answer'};
1.71 albertel 429: # save bubbled letter for later
430: $Apache::lonhomework::results{"resource.$part.$id.scantron"}.=
431: $response;
1.70 albertel 432: $response = $let_to_num{$response};
433: } else {
434: $response = $ENV{$formparm};
435: }
436: return $response;
1.62 albertel 437: }
1.71 albertel 438:
439: sub repetition {
440: my $id = $Apache::inputtags::part;
441: my $weight = &Apache::lonnet::EXT("resource.$id.weight");
442: my $repetition = int $weight/9;
443: if ($weight % 9 != 0) {$repetition++;}
1.72 albertel 444: return $repetition;
445: }
446:
447: sub scored_response {
448: my ($part,$id)=@_;
449: my $repetition=&repetition();
450: my $score=0;
451: for (my $i=0;$i<$repetition;$i++) {
452: my $increase=&Apache::response::getresponse($i+1);
453: if ($increase ne '') { $score+=$increase+1; }
454: }
455: my $weight = &Apache::lonnet::EXT("resource.$part.weight");
456: my $pcr=$score/$weight;
457: $Apache::lonhomework::results{"resource.$part.$id.awarded"}=$pcr;
458: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
459: 'ASSIGNED_SCORE';
1.71 albertel 460: return $repetition;
1.78 albertel 461: }
462:
463: sub whichorder {
464: my ($max,$randomize,$showall,$hash)=@_;
465: #&Apache::lonxml::debug("man $max randomize $randomize");
466: if (!defined(@{ $$hash{'names'} })) { return; }
467: my @names = @{ $$hash{'names'} };
468: my @whichopt =();
469: my (%top,@toplist,%bottom,@bottomlist);
470: if (!($showall || ($randomize eq 'no'))) {
471: my $current=0;
472: foreach my $name (@names) {
473: $current++;
474: if ($$hash{"$name.location"} eq 'top') {
475: $top{$name}=$current;
476: } elsif ($$hash{"$name.location"} eq 'bottom') {
477: $bottom{$name}=$current;
478: }
479: }
480: }
481: my $topcount=0;
482: my $bottomcount=0;
483: while (((scalar(@whichopt)+$topcount+$bottomcount) < $max || $showall)
484: && ($#names > -1)) {
485: #&Apache::lonxml::debug("Have $#whichopt max is $max");
486: my $aopt;
487: if ($showall || ($randomize eq 'no')) {
488: $aopt=0;
489: } else {
490: $aopt=int(&Math::Random::random_uniform() * ($#names+1));
491: }
492: #&Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
493: $aopt=splice(@names,$aopt,1);
494: #&Apache::lonxml::debug("Picked $aopt");
495: if ($top{$aopt}) {
496: $toplist[$top{$aopt}]=$aopt;
497: $topcount++;
498: } elsif ($bottom{$aopt}) {
499: $bottomlist[$bottom{$aopt}]=$aopt;
500: $bottomcount++;
501: } else {
502: push (@whichopt,$aopt);
503: }
504: }
505: for (my $i=0;$i<=$#toplist;$i++) {
506: if ($toplist[$i]) { unshift(@whichopt,$toplist[$i]) }
507: }
508: for (my $i=0;$i<=$#bottomlist;$i++) {
509: if ($bottomlist[$i]) { push(@whichopt,$bottomlist[$i]) }
510: }
511:
512: return @whichopt;
1.71 albertel 513: }
514:
1.1 albertel 515: 1;
516: __END__
1.38 albertel 517:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>