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