Annotation of loncom/homework/response.pm, revision 1.84
1.38 albertel 1: # The LearningOnline Network with CAPA
1.1 albertel 2: # various response type definitons response definition
1.53 albertel 3: #
1.84 ! sakharuk 4: # $Id: response.pm,v 1.83 2003/08/11 19:56:45 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 =~/,/) {
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:
1.83 albertel 281: sub decide_package {
282: my ($tagstack)=@_;
283: my $package;
284: if ($$tagstack[-1] eq 'parameter') {
285: $package='part';
286: } else {
287: my $i=-1;
288: while (defined($$tagstack[$i])) {
289: if ($$tagstack[$i] =~ /(response|hint)$/) {
290: $package=$$tagstack[$i];
291: last;
292: }
293: $i--;
294: }
295: }
296: return $package;
297: }
298:
1.3 albertel 299: sub start_responseparam {
1.73 albertel 300: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
301: my $result='';
302: if ($target eq 'meta') {
303: $result = &meta_parameter_write($token->[2]->{'name'},
304: $token->[2]->{'type'},
305: $token->[2]->{'default'},
306: $token->[2]->{'description'});
307: } elsif ($target eq 'edit') {
308: $result.=&Apache::edit::tag_start($target,$token);
1.83 albertel 309: my $optionlist;
310: my $package=&decide_package($tagstack);
311: foreach my $key (sort(keys(%Apache::lonnet::packagetab))) {
312: if ($key =~ /^\Q$package\E&(.*)&display$/) {
313: $optionlist.='<option value="'.$1.'">'.
314: $Apache::lonnet::packagetab{$key}.'</option>';
315: }
316: }
317: if (defined($optionlist)) {
318: $result.='Use template: <select name="'.
319: &Apache::edit::html_element_name('parameter_package').'">'.
320: '<option value=""></option>'.$optionlist.'</select><br />';
321: }
1.73 albertel 322: $result.=&Apache::edit::text_arg('Name:','name',$token).
323: &Apache::edit::text_arg('Type:','type',$token).
324: &Apache::edit::text_arg('Description:','description',$token).
325: &Apache::edit::text_arg('Default:','default',$token).
326: "</td></tr>";
327: $result.=&Apache::edit::end_table;
328: } elsif ($target eq 'modified') {
1.83 albertel 329: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
330: $safeeval,'name','type',
331: 'description','default');
332: my $element=&Apache::edit::html_element_name('parameter_package');
333: if (defined($ENV{"form.$element"}) && $ENV{"form.$element"} ne '') {
334: my $name=$ENV{"form.$element"};
335: my $tag=&decide_package($tagstack);
336: $token->[2]->{'name'}=$name;
337: $token->[2]->{'type'}=
338: $Apache::lonnet::packagetab{"$tag&$name&type"};
339: $token->[2]->{'description'}=
340: $Apache::lonnet::packagetab{"$tag&$name&display"};
341: $token->[2]->{'default'}=
342: $Apache::lonnet::packagetab{"$tag&$name&default"};
343: $constructtag=1;
344: }
1.73 albertel 345: if ($constructtag) {
346: $result = &Apache::edit::rebuild_tag($token);
347: $result.=&Apache::edit::handle_insert();
348: }
349: } elsif ($target eq 'grade' || $target eq 'answer' || $target eq 'web' ||
350: $target eq 'tex' || $target eq 'analyze' ) {
351: if ($ENV{'request.state'} eq 'construct') {
352: my $name =&Apache::lonxml::get_param('name',$parstack,$safeeval);
353: my $default=&Apache::lonxml::get_param('default',$parstack,
354: $safeeval);
355: if ($name) {$Apache::inputtags::params{$name}=$default;}
356: }
1.52 albertel 357: }
1.73 albertel 358: return $result;
1.3 albertel 359: }
360:
361: sub end_responseparam {
1.73 albertel 362: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
363: if ($target eq 'edit') { return ('','no'); }
364: return '';
1.55 albertel 365: }
366:
367: sub start_parameter {
1.73 albertel 368: my $result = &start_responseparam(@_);
369: return $result;
1.55 albertel 370: }
371:
372: sub end_parameter {
1.73 albertel 373: my $result = &end_responseparam(@_);
374: return $result;
1.42 albertel 375: }
376:
1.67 albertel 377: sub reset_params {
378: %Apache::inputtags::params=();
379: }
380:
1.42 albertel 381: sub setup_params {
1.73 albertel 382: my ($tag) = @_;
1.42 albertel 383:
1.73 albertel 384: if ($ENV{'request.state'} eq 'construct') { return; }
385: my %paramlist=();
386: foreach my $key (keys(%Apache::lonnet::packagetab)) {
387: if ($key =~ /^$tag/) {
388: my ($package,$name) = split(/&/,$key);
389: $paramlist{$name}=1;
390: }
1.42 albertel 391: }
1.73 albertel 392: foreach my $key (keys(%paramlist)) {
393: my $entry= 'resource.'.$Apache::inputtags::part;
394: if (defined($Apache::inputtags::response[-1])) {
395: $entry.='_'.$Apache::inputtags::response[-1];
396: }
397: $entry.='.'.$key;
398: &Apache::lonxml::debug("looking for $entry");
399: my $value = &Apache::lonnet::EXT("$entry");
400: &Apache::lonxml::debug("$key has value :$value:");
401: if ($value eq 'con_lost' || $value =~ /^error:/) {
402: &Apache::lonxml::debug("using nothing");
403: $Apache::inputtags::params{$key}='';
404: } else {
405: &Apache::lonxml::debug("using value");
406: $Apache::inputtags::params{$key}=$value;
407: }
1.42 albertel 408: }
1.48 albertel 409: }
410:
411: sub answer_header {
1.73 albertel 412: my ($type) = @_;
413: my $result;
1.77 albertel 414: if ($ENV{'form.answer_output_mode'} eq 'tex') {
1.84 ! sakharuk 415: $result = ' \vskip 0 mm \begin{tabular}{|c|}\hline Answer for Part: \verb|'.
! 416: $Apache::inputtags::part.'| \\\\ \hline ';
1.73 albertel 417: } else {
1.80 albertel 418: $result = '<table border="1"><tr><td>Answer for Part:'.
419: $Apache::inputtags::part. '</td>'."\n";
1.73 albertel 420: }
421: return $result;
1.48 albertel 422: }
423:
424: sub answer_part {
1.73 albertel 425: my ($type,$answer) = @_;
426: my $result;
1.77 albertel 427: if ($ENV{'form.answer_output_mode'} eq 'tex') {
1.81 sakharuk 428: $result = ' \verb|'.$answer.'|\\\\ \hline ';
1.73 albertel 429: } else {
1.80 albertel 430: $result = '<td>'.$answer.'</td>';
1.73 albertel 431: }
432: return $result;
1.48 albertel 433: }
434:
435: sub answer_footer {
1.73 albertel 436: my ($type) = @_;
437: my $result;
1.77 albertel 438: if ($ENV{'form.answer_output_mode'} eq 'tex') {
1.75 sakharuk 439: $result = ' \end{tabular} \vskip 0 mm ';
1.73 albertel 440: } else {
1.80 albertel 441: $result = '</tr></table>';
1.73 albertel 442: }
443: return $result;
1.1 albertel 444: }
1.2 albertel 445:
1.62 albertel 446: sub showallfoils {
1.73 albertel 447: my $return=0;
448: if (defined($ENV{'form.showallfoils'}) &&
449: $ENV{'request.state'} eq 'construct') {
450: $return=1;
451: }
452: return $return;
1.70 albertel 453: }
454:
455: sub getresponse {
456: my ($temp)=@_;
457: my $formparm='form.HWVAL_'.$Apache::inputtags::response['-1'];
458: my $response;
459: if (!defined($temp)) {
460: $temp=1;
461: } else {
462: $formparm.=":$temp";
463: }
464: my %let_to_num=('A'=>0,'B'=>1,'C'=>2,'D'=>3,'E'=>4,'F'=>5,'G'=>6,'H'=>7,
465: 'I'=>8,'J'=>9,'K'=>10,'L'=>11,'M'=>12,'N'=>13,'O'=>14,
466: 'P'=>15,'Q'=>16,'R'=>17,'S'=>18,'T'=>19,'U'=>20,'V'=>21,
467: 'W'=>22,'X'=>23,'Y'=>24,'Z'=>25);
468: if ($ENV{'form.submitted'} eq 'scantron') {
1.71 albertel 469: my $part = $Apache::inputtags::part;
470: my $id = $Apache::inputtags::response[-1];
1.70 albertel 471: $response = $ENV{'scantron.'.($Apache::lonxml::counter+$temp-1).
472: '.answer'};
1.71 albertel 473: # save bubbled letter for later
474: $Apache::lonhomework::results{"resource.$part.$id.scantron"}.=
475: $response;
1.70 albertel 476: $response = $let_to_num{$response};
477: } else {
478: $response = $ENV{$formparm};
479: }
480: return $response;
1.62 albertel 481: }
1.71 albertel 482:
483: sub repetition {
484: my $id = $Apache::inputtags::part;
485: my $weight = &Apache::lonnet::EXT("resource.$id.weight");
486: my $repetition = int $weight/9;
487: if ($weight % 9 != 0) {$repetition++;}
1.72 albertel 488: return $repetition;
489: }
490:
491: sub scored_response {
492: my ($part,$id)=@_;
493: my $repetition=&repetition();
494: my $score=0;
495: for (my $i=0;$i<$repetition;$i++) {
496: my $increase=&Apache::response::getresponse($i+1);
497: if ($increase ne '') { $score+=$increase+1; }
498: }
499: my $weight = &Apache::lonnet::EXT("resource.$part.weight");
500: my $pcr=$score/$weight;
501: $Apache::lonhomework::results{"resource.$part.$id.awarded"}=$pcr;
502: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
503: 'ASSIGNED_SCORE';
1.71 albertel 504: return $repetition;
1.78 albertel 505: }
506:
507: sub whichorder {
508: my ($max,$randomize,$showall,$hash)=@_;
509: #&Apache::lonxml::debug("man $max randomize $randomize");
510: if (!defined(@{ $$hash{'names'} })) { return; }
511: my @names = @{ $$hash{'names'} };
512: my @whichopt =();
513: my (%top,@toplist,%bottom,@bottomlist);
514: if (!($showall || ($randomize eq 'no'))) {
515: my $current=0;
516: foreach my $name (@names) {
517: $current++;
518: if ($$hash{"$name.location"} eq 'top') {
519: $top{$name}=$current;
520: } elsif ($$hash{"$name.location"} eq 'bottom') {
521: $bottom{$name}=$current;
522: }
523: }
524: }
525: my $topcount=0;
526: my $bottomcount=0;
527: while (((scalar(@whichopt)+$topcount+$bottomcount) < $max || $showall)
528: && ($#names > -1)) {
529: #&Apache::lonxml::debug("Have $#whichopt max is $max");
530: my $aopt;
531: if ($showall || ($randomize eq 'no')) {
532: $aopt=0;
533: } else {
534: $aopt=int(&Math::Random::random_uniform() * ($#names+1));
535: }
536: #&Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
537: $aopt=splice(@names,$aopt,1);
538: #&Apache::lonxml::debug("Picked $aopt");
539: if ($top{$aopt}) {
540: $toplist[$top{$aopt}]=$aopt;
541: $topcount++;
542: } elsif ($bottom{$aopt}) {
543: $bottomlist[$bottom{$aopt}]=$aopt;
544: $bottomcount++;
545: } else {
546: push (@whichopt,$aopt);
547: }
548: }
549: for (my $i=0;$i<=$#toplist;$i++) {
550: if ($toplist[$i]) { unshift(@whichopt,$toplist[$i]) }
551: }
552: for (my $i=0;$i<=$#bottomlist;$i++) {
553: if ($bottomlist[$i]) { push(@whichopt,$bottomlist[$i]) }
554: }
555:
556: return @whichopt;
1.71 albertel 557: }
558:
1.1 albertel 559: 1;
560: __END__
1.38 albertel 561:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>