Annotation of loncom/homework/response.pm, revision 1.79
1.38 albertel 1: # The LearningOnline Network with CAPA
1.1 albertel 2: # various response type definitons response definition
1.53 albertel 3: #
1.79 ! albertel 4: # $Id: response.pm,v 1.78 2003/05/16 20:16:18 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.75 sakharuk 371: if ($type eq 'optionresponse' || $type eq 'radiobuttonresponse' ) {
372: $result = '<table border="1"><tr><th>Answer for Part:'.
373: $Apache::inputtags::part. '</th></tr><tr>'."\n";
374: } else {
375: $result = '<table border="1"><tr><td>Answer for Part:'.
376: $Apache::inputtags::part. '</td>'."\n";
377: }
1.73 albertel 378: }
379: return $result;
1.48 albertel 380: }
381:
382: sub answer_part {
1.73 albertel 383: my ($type,$answer) = @_;
384: my $result;
1.77 albertel 385: if ($ENV{'form.answer_output_mode'} eq 'tex') {
1.75 sakharuk 386: $result = ' '.$answer.'\\\\ \hline ';
1.73 albertel 387: } else {
1.75 sakharuk 388: if ($type eq 'optionresponse' || $type eq 'radiobuttonresponse') {
389: $result = '<td>'.$answer.'</td>';
390: } else {
391: $result = '<td>'.$answer.'</td>';
392: }
1.73 albertel 393: }
394: return $result;
1.48 albertel 395: }
396:
397: sub answer_footer {
1.73 albertel 398: my ($type) = @_;
399: my $result;
1.77 albertel 400: if ($ENV{'form.answer_output_mode'} eq 'tex') {
1.75 sakharuk 401: $result = ' \end{tabular} \vskip 0 mm ';
1.73 albertel 402: } else {
1.75 sakharuk 403: if ($type eq 'optionresponse' || $type eq 'radiobuttonresponse') {
404: $result = '</tr></table>';
405: } else {
406: $result = '</tr></table>';
407: }
1.73 albertel 408: }
409: return $result;
1.1 albertel 410: }
1.2 albertel 411:
1.62 albertel 412: sub showallfoils {
1.73 albertel 413: my $return=0;
414: if (defined($ENV{'form.showallfoils'}) &&
415: $ENV{'request.state'} eq 'construct') {
416: $return=1;
417: }
418: return $return;
1.70 albertel 419: }
420:
421: sub getresponse {
422: my ($temp)=@_;
423: my $formparm='form.HWVAL_'.$Apache::inputtags::response['-1'];
424: my $response;
425: if (!defined($temp)) {
426: $temp=1;
427: } else {
428: $formparm.=":$temp";
429: }
430: my %let_to_num=('A'=>0,'B'=>1,'C'=>2,'D'=>3,'E'=>4,'F'=>5,'G'=>6,'H'=>7,
431: 'I'=>8,'J'=>9,'K'=>10,'L'=>11,'M'=>12,'N'=>13,'O'=>14,
432: 'P'=>15,'Q'=>16,'R'=>17,'S'=>18,'T'=>19,'U'=>20,'V'=>21,
433: 'W'=>22,'X'=>23,'Y'=>24,'Z'=>25);
434: if ($ENV{'form.submitted'} eq 'scantron') {
1.71 albertel 435: my $part = $Apache::inputtags::part;
436: my $id = $Apache::inputtags::response[-1];
1.70 albertel 437: $response = $ENV{'scantron.'.($Apache::lonxml::counter+$temp-1).
438: '.answer'};
1.71 albertel 439: # save bubbled letter for later
440: $Apache::lonhomework::results{"resource.$part.$id.scantron"}.=
441: $response;
1.70 albertel 442: $response = $let_to_num{$response};
443: } else {
444: $response = $ENV{$formparm};
445: }
446: return $response;
1.62 albertel 447: }
1.71 albertel 448:
449: sub repetition {
450: my $id = $Apache::inputtags::part;
451: my $weight = &Apache::lonnet::EXT("resource.$id.weight");
452: my $repetition = int $weight/9;
453: if ($weight % 9 != 0) {$repetition++;}
1.72 albertel 454: return $repetition;
455: }
456:
457: sub scored_response {
458: my ($part,$id)=@_;
459: my $repetition=&repetition();
460: my $score=0;
461: for (my $i=0;$i<$repetition;$i++) {
462: my $increase=&Apache::response::getresponse($i+1);
463: if ($increase ne '') { $score+=$increase+1; }
464: }
465: my $weight = &Apache::lonnet::EXT("resource.$part.weight");
466: my $pcr=$score/$weight;
467: $Apache::lonhomework::results{"resource.$part.$id.awarded"}=$pcr;
468: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
469: 'ASSIGNED_SCORE';
1.71 albertel 470: return $repetition;
1.78 albertel 471: }
472:
473: sub whichorder {
474: my ($max,$randomize,$showall,$hash)=@_;
475: #&Apache::lonxml::debug("man $max randomize $randomize");
476: if (!defined(@{ $$hash{'names'} })) { return; }
477: my @names = @{ $$hash{'names'} };
478: my @whichopt =();
479: my (%top,@toplist,%bottom,@bottomlist);
480: if (!($showall || ($randomize eq 'no'))) {
481: my $current=0;
482: foreach my $name (@names) {
483: $current++;
484: if ($$hash{"$name.location"} eq 'top') {
485: $top{$name}=$current;
486: } elsif ($$hash{"$name.location"} eq 'bottom') {
487: $bottom{$name}=$current;
488: }
489: }
490: }
491: my $topcount=0;
492: my $bottomcount=0;
493: while (((scalar(@whichopt)+$topcount+$bottomcount) < $max || $showall)
494: && ($#names > -1)) {
495: #&Apache::lonxml::debug("Have $#whichopt max is $max");
496: my $aopt;
497: if ($showall || ($randomize eq 'no')) {
498: $aopt=0;
499: } else {
500: $aopt=int(&Math::Random::random_uniform() * ($#names+1));
501: }
502: #&Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
503: $aopt=splice(@names,$aopt,1);
504: #&Apache::lonxml::debug("Picked $aopt");
505: if ($top{$aopt}) {
506: $toplist[$top{$aopt}]=$aopt;
507: $topcount++;
508: } elsif ($bottom{$aopt}) {
509: $bottomlist[$bottom{$aopt}]=$aopt;
510: $bottomcount++;
511: } else {
512: push (@whichopt,$aopt);
513: }
514: }
515: for (my $i=0;$i<=$#toplist;$i++) {
516: if ($toplist[$i]) { unshift(@whichopt,$toplist[$i]) }
517: }
518: for (my $i=0;$i<=$#bottomlist;$i++) {
519: if ($bottomlist[$i]) { push(@whichopt,$bottomlist[$i]) }
520: }
521:
522: return @whichopt;
1.71 albertel 523: }
524:
1.1 albertel 525: 1;
526: __END__
1.38 albertel 527:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>