Annotation of loncom/homework/response.pm, revision 1.144
1.38 albertel 1: # The LearningOnline Network with CAPA
1.1 albertel 2: # various response type definitons response definition
1.53 albertel 3: #
1.144 ! www 4: # $Id: response.pm,v 1.143 2006/07/14 18:40:41 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.5 www 28:
1.1 albertel 29: package Apache::response;
30: use strict;
1.93 albertel 31: use Apache::lonlocal;
1.120 albertel 32: use Apache::lonnet;
1.1 albertel 33:
1.57 harris41 34: BEGIN {
1.139 www 35: &Apache::lonxml::register('Apache::response',('responseparam','parameter','dataresponse','customresponse','mathresponse'));
1.1 albertel 36: }
37:
1.13 albertel 38: sub start_response {
1.73 albertel 39: my ($parstack,$safeeval)=@_;
1.136 albertel 40: my $id = &Apache::lonxml::get_id($parstack,$safeeval);
1.73 albertel 41: if ($#Apache::inputtags::import > -1) {
42: &Apache::lonxml::debug("Turning :$id: into");
43: $id = join('_',@Apache::inputtags::import).'_'.$id;
44: &Apache::lonxml::debug("New :$id:");
45: }
46: push (@Apache::inputtags::response,$id);
47: push (@Apache::inputtags::responselist,$id);
48: @Apache::inputtags::inputlist=();
1.101 albertel 49: if ($Apache::inputtags::part eq '' &&
50: !$Apache::lonhomework::ignore_response_errors) {
1.97 albertel 51: &Apache::lonxml::error(&HTML::Entities::encode(&mt("Found a <*response> outside of a <part> in a <part>ed problem"),'<>&"'));
1.92 albertel 52: }
53: if ($Apache::inputtags::response_with_no_part &&
54: $Apache::inputtags::part ne '0') {
1.97 albertel 55: &Apache::lonxml::error(&HTML::Entities::encode(&mt("<*response>s are both inside of <part> and outside of <part>, this is not a valid problem, errors in grading may occur."),'<>&"').'<br />');
1.92 albertel 56: }
57: if ($Apache::inputtags::part eq '0') {
58: $Apache::inputtags::response_with_no_part=1;
59: }
1.73 albertel 60: return $id;
1.13 albertel 61: }
62:
63: sub end_response {
1.79 albertel 64: #pop @Apache::inputtags::response;
1.73 albertel 65: @Apache::inputtags::inputlist=();
66: return '';
1.13 albertel 67: }
68:
1.41 albertel 69: sub start_hintresponse {
1.73 albertel 70: my ($parstack,$safeeval)=@_;
1.136 albertel 71: my $id = &Apache::lonxml::get_id($parstack,$safeeval);
1.123 albertel 72: push (@Apache::inputtags::hint,$id);
73: push (@Apache::inputtags::hintlist,$id);
1.73 albertel 74: push (@Apache::inputtags::paramstack,[%Apache::inputtags::params]);
75: return $id;
1.41 albertel 76: }
77:
78: sub end_hintresponse {
1.123 albertel 79: pop @Apache::inputtags::hint;
1.73 albertel 80: if (defined($Apache::inputtags::paramstack[-1])) {
81: %Apache::inputtags::params=
82: @{ pop(@Apache::inputtags::paramstack) };
83: }
84: return '';
1.41 albertel 85: }
86:
1.99 albertel 87: my @randomseeds;
88: sub pushrandomnumber {
89: my $rand_alg=&Apache::lonnet::get_rand_alg();
90: if (!$rand_alg || $rand_alg eq '32bit' || $rand_alg eq '64bit' ||
91: $rand_alg eq '64bit2') {
92: # do nothing
93: } else {
94: my @seed=&Math::Random::random_get_seed();
1.127 albertel 95: push(@randomseeds,\@seed);
1.99 albertel 96: }
1.127 albertel 97: &Apache::response::setrandomnumber(@_);
1.99 albertel 98: }
99: sub poprandomnumber {
100: my $rand_alg=&Apache::lonnet::get_rand_alg();
101: if (!$rand_alg || $rand_alg eq '32bit' || $rand_alg eq '64bit' ||
102: $rand_alg eq '64bit2') {
103: return;
104: }
105: my $seed=pop(@randomseeds);
106: if ($seed) {
107: &Math::Random::random_set_seed(@$seed);
108: } else {
109: &Apache::lonxml::error("Unable to restore random algorithm.");
110: }
111: }
1.117 albertel 112:
1.26 albertel 113: sub setrandomnumber {
1.127 albertel 114: my ($ignore_id2) = @_;
1.73 albertel 115: my $rndseed;
1.88 albertel 116: $rndseed=&Apache::structuretags::setup_rndseed();
117: if (!defined($rndseed)) { $rndseed=&Apache::lonnet::rndseed(); }
1.73 albertel 118: &Apache::lonxml::debug("randseed $rndseed");
119: # $rndseed=unpack("%32i",$rndseed);
1.99 albertel 120: my $rand_alg=&Apache::lonnet::get_rand_alg();
1.126 albertel 121: my ($rndmod,$rndmod2);
1.119 albertel 122:
123: my ($id1,$id2,$shift_amt);
124: if ($Apache::lonhomework::parsing_a_problem) {
125: $id1=$Apache::inputtags::part;
126: if (defined($Apache::inputtags::response[-1])) {
127: $id2=$Apache::inputtags::response[-1];
128: }
129: $shift_amt=scalar(@Apache::inputtags::responselist);
130: } elsif ($Apache::lonhomework::parsing_a_task) {
1.141 albertel 131: $id1=&Apache::bridgetask::get_dim_id();
132: if (!$ignore_id2 && ref($Apache::bridgetask::instance{$id1})) {
133: $id2=$Apache::bridgetask::instance{$id1}[-1];
1.142 albertel 134: $shift_amt=scalar(@{$Apache::bridgetask::instance{$id1}});
135: } else {
136: $shift_amt=0;
1.119 albertel 137: }
138: }
139: &Apache::lonxml::debug("id1: $id1, id2: $id2, shift_amt: $shift_amt");
1.99 albertel 140: if (!$rand_alg || $rand_alg eq '32bit' || $rand_alg eq '64bit' ||
141: $rand_alg eq '64bit2') {
1.119 albertel 142: $rndmod=(&Apache::lonnet::numval($id1) << 10);
143: if (defined($id2)) { $rndmod+=&Apache::lonnet::numval($id2); }
1.110 albertel 144: } elsif ($rand_alg eq '64bit3') {
1.119 albertel 145: $rndmod=(&Apache::lonnet::numval2($id1) << 10);
146: if (defined($id2)) { $rndmod+=&Apache::lonnet::numval2($id2); }
1.126 albertel 147: } elsif ($rand_alg eq '64bit4') {
1.119 albertel 148: my $shift=(4*$shift_amt)%30;
149: $rndmod=(&Apache::lonnet::numval3($id1) << (($shift+15)%30));
150: if (defined($id2)) {
151: $rndmod+=(&Apache::lonnet::numval3($id2) << $shift );
1.110 albertel 152: }
1.126 albertel 153: } else {
154: ($rndmod,$rndmod2)=&Apache::lonnet::digest("$id1,$id2");
1.99 albertel 155: }
1.127 albertel 156:
1.99 albertel 157: if ($rndseed =~/([,:])/) {
158: my $char=$1;
159: use integer;
160: my ($num1,$num2)=split(/\Q$char\E/,$rndseed);
161: $num1+=$rndmod;
1.126 albertel 162: $num2+= ((defined($rndmod2)) ? $rndmod2 : $rndmod);
1.109 albertel 163: if($Apache::lonnet::_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.99 albertel 164: $rndseed=$num1.$char.$num2;
165: } else {
1.74 albertel 166: $rndseed+=$rndmod;
1.109 albertel 167: if($Apache::lonnet::_64bit) {
168: use integer;
169: $rndseed=(($rndseed<<32)>>32);
170: }
1.74 albertel 171: }
1.111 albertel 172: &Apache::lonxml::debug("randseed $rndmod $rndseed");
1.74 albertel 173: &Apache::lonnet::setup_random_from_rndseed($rndseed);
1.73 albertel 174: return '';
1.26 albertel 175: }
176:
1.7 www 177: sub meta_parameter_write {
1.38 albertel 178: my ($name,$type,$default,$display)=@_;
1.41 albertel 179: my $partref=$Apache::inputtags::part;
180: my $result='<parameter part="'.$Apache::inputtags::part.'"';
181: if (defined($Apache::inputtags::response[-1])) {
1.73 albertel 182: $result.= ' id="'.$Apache::inputtags::response[-1].'"';
183: $partref.='_'.$Apache::inputtags::response[-1];
1.41 albertel 184: }
185: $result.= ' name="'.$name.'"'.
186: ' type="'.$type.'"'.
1.89 albertel 187: (defined($default)?' default="'.$default.'"':'').
188: (defined($display)?' display="'.$display.' [Part: '.$partref.']"':'')
1.41 albertel 189: .'></parameter>'
190: ."\n";
191: return $result;
1.33 www 192: }
193:
194: sub meta_package_write {
195: my $name=shift;
1.41 albertel 196: my $result = '<parameter part="'.$Apache::inputtags::part.'"';
197: if(defined($Apache::inputtags::response[-1])) {
1.73 albertel 198: $result.= ' id="'.$Apache::inputtags::response[-1].'"';
1.41 albertel 199: }
200: $result.=' package="'.$name.'"></parameter>'."\n";
201: return $result;
1.7 www 202: }
203:
204: sub meta_stores_write {
1.10 www 205: my ($name,$type,$display)=@_;
1.41 albertel 206: my $partref=$Apache::inputtags::part;
207: my $result = '<stores part="'.$Apache::inputtags::part.'"';
208: if (defined($Apache::inputtags::response[-1])) {
1.73 albertel 209: $result.= ' id="'.$Apache::inputtags::response[-1].'"';
210: $partref.='_'.$Apache::inputtags::response[-1];
1.41 albertel 211: }
212: $result.= ' name="'.$name.'"'.
213: ' type="'.$type.'"'.
214: ' display="'.$display.' [Part: '.$partref.']"'.
215: "></stores>\n";
1.7 www 216: }
217:
218: sub mandatory_part_meta {
219: #
220: # Autogenerate metadata for mandatory
221: # input (from RAT or lonparmset) and
222: # output (to lonspreadsheet)
223: # of each part
224: #
1.73 albertel 225: return
1.34 www 226: # &meta_parameter_write('opendate','date_start','',
227: # 'Opening Date').
228: # &meta_parameter_write('duedate','date_end','',
229: # 'Due Date').
230: # &meta_parameter_write('answerdate','date_start','',
231: # 'Show Answer Date').
232: # &meta_parameter_write('weight','int_zeropos','',
233: # 'Available Points').
234: # &meta_parameter_write('maxtries','int_pos','',
235: # 'Maximum Number of Tries').
1.73 albertel 236: &meta_package_write('part').
237: &meta_stores_write('solved','string',
238: 'Problem Status').
239: &meta_stores_write('tries','int_zeropos',
240: 'Number of Attempts').
241: &meta_stores_write('awarded','float',
242: 'Partial Credit Factor');
1.7 www 243: #
244: # Note: responseid-specific data 'submission' and 'awarddetail'
245: # not available to spreadsheet -> skip here
246: #
1.86 albertel 247: }
248:
249: sub meta_part_order {
250: if (@Apache::inputtags::partlist) {
251: my @parts=@Apache::inputtags::partlist;
252: shift(@parts);
1.100 albertel 253: return '<partorder>'.join(',',@parts).'</partorder>'."\n";
1.86 albertel 254: } else {
1.100 albertel 255: return '<partorder>0</partorder>'."\n";
256: }
257: }
258:
259: sub meta_response_order {
260: if (@Apache::inputtags::responselist) {
261: return '<responseorder>'.join(',',@Apache::inputtags::responselist).
262: '</responseorder>'."\n";
1.86 albertel 263: }
1.14 albertel 264: }
265:
1.15 albertel 266: sub check_for_previous {
1.73 albertel 267: my ($curresponse,$partid,$id) = @_;
268: my %previous;
269: $previous{'used'} = 0;
270: foreach my $key (sort(keys(%Apache::lonhomework::history))) {
1.98 albertel 271: if ($key =~ /resource\.$partid\.$id\.submission$/) {
1.73 albertel 272: &Apache::lonxml::debug("Trying $key");
273: my $pastresponse=$Apache::lonhomework::history{$key};
274: if ($pastresponse eq $curresponse) {
275: $previous{'used'} = 1;
276: my $history;
277: if ( $key =~ /^(\d+):/ ) {
278: $history=$1;
279: $previous{'award'} = $Apache::lonhomework::history{"$history:resource.$partid.$id.awarddetail"};
280: $previous{'last'}='0';
281: push(@{ $previous{'version'} },$history);
282: } else {
283: $previous{'award'} = $Apache::lonhomework::history{"resource.$partid.$id.awarddetail"};
284: $previous{'last'}='1';
285: }
286: if (! $previous{'award'} ) { $previous{'award'} = 'UNKNOWN'; }
287: &Apache::lonxml::debug("got a match :$previous{'award'}:$previous{'used'}:");
288: }
1.32 albertel 289: }
1.73 albertel 290: }
291: &Apache::lonhomework::showhash(%previous);
292: return %previous;
1.54 albertel 293: }
294:
295: sub handle_previous {
1.73 albertel 296: my ($previous,$ad)=@_;
297: if ($$previous{'used'} && ($$previous{'award'} eq $ad) ) {
298: if ($$previous{'last'}) {
299: push(@Apache::inputtags::previous,'PREVIOUSLY_LAST');
1.107 albertel 300: push(@Apache::inputtags::previous_version,$$previous{'version'});
301: } elsif ($Apache::lonhomework::type ne 'survey') {
1.73 albertel 302: push(@Apache::inputtags::previous,'PREVIOUSLY_USED');
1.107 albertel 303: push(@Apache::inputtags::previous_version,$$previous{'version'});
1.73 albertel 304: }
1.54 albertel 305: }
1.44 albertel 306: }
307:
1.45 albertel 308: sub view_or_modify {
1.73 albertel 309: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
310: my $myself=0;
1.120 albertel 311: if ( ($name eq $env{'user.name'}) && ($domain eq $env{'user.domain'}) ) {
1.73 albertel 312: $myself=1;
313: }
314: my $vgr=&Apache::lonnet::allowed('vgr',$courseid);
315: my $mgr=&Apache::lonnet::allowed('vgr',$courseid);
316: if ($mgr) { return "M"; }
317: if ($vgr) { return "V"; }
318: if ($myself) { return "V"; }
319: return '';
1.45 albertel 320: }
321:
1.44 albertel 322: sub start_dataresponse {
1.73 albertel 323: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
324: my $id = &Apache::response::start_response($parstack,$safeeval);
325: my $result;
326: if ($target eq 'web') {
327: $result = $token->[2]->{'display'}.':';
328: } elsif ($target eq 'meta') {
329: $result = &Apache::response::meta_stores_write($token->[2]->{'name'},
330: $token->[2]->{'type'},
331: $token->[2]->{'display'});
332: $result .= &Apache::response::meta_package_write('dataresponse');
333: }
334: return $result;
1.44 albertel 335: }
336:
337: sub end_dataresponse {
1.73 albertel 338: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
339: my $result;
340: if ( $target eq 'web' ) {
341: } elsif ($target eq 'grade' ) {
1.120 albertel 342: if ( defined $env{'form.submitted'}) {
1.73 albertel 343: my ($symb,$courseid,$domain,$name)=&Apache::lonxml::whichuser();
344: my $allowed=&Apache::lonnet::allowed('mgr',$courseid);
345: if ($allowed) {
1.94 albertel 346: &Apache::response::setup_params('dataresponse',$safeeval);
1.73 albertel 347: my $partid = $Apache::inputtags::part;
348: my $id = $Apache::inputtags::response['-1'];
1.120 albertel 349: my $response = $env{'form.HWVAL_'.$id};
1.73 albertel 350: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
351: if ( $response =~ /[^\s]/) {
352: $Apache::lonhomework::results{"resource.$partid.$id.$name"}=$response;
353: $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
354: $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}='SUBMITTED';
355: }
356: } else {
357: $result='Not Permitted to change values.'
358: }
1.45 albertel 359: }
1.73 albertel 360: }
361: &Apache::response::end_response;
362: return $result;
1.3 albertel 363: }
364:
1.129 albertel 365: sub start_customresponse {
1.128 albertel 366: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
367: my $id = &Apache::response::start_response($parstack,$safeeval);
1.129 albertel 368: push(@Apache::lonxml::namespace,'customresponse');
1.128 albertel 369: my $result;
1.129 albertel 370: undef($Apache::response::custom_answer);
1.128 albertel 371: &Apache::lonxml::register('Apache::response',('answer'));
372: if ($target eq 'web') {
373: if ( &Apache::response::show_answer() ) {
374: my $answer = &Apache::lonxml::get_param('answerdisplay',$parstack,
375: $safeeval);
376: $Apache::inputtags::answertxt{$id}=$answer;
377: }
378: } elsif ($target eq 'edit') {
379: $result.=&Apache::edit::tag_start($target,$token);
380: $result.=&Apache::edit::text_arg('String to display for answer:',
1.144 ! www 381: 'answerdisplay',$token).
! 382: &Apache::loncommon::help_open_topic('Custom_Response_Problems','How to write custom responses');
1.128 albertel 383: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
384: } elsif ($target eq 'modified') {
385: my $constructtag;
386: $constructtag=&Apache::edit::get_new_args($token,$parstack,
387: $safeeval,'answerdisplay');
388: if ($constructtag) {
389: $result = &Apache::edit::rebuild_tag($token);
390: $result.=&Apache::edit::handle_insert();
391: }
392: } elsif ($target eq 'answer' || $target eq 'grade') {
393: &Apache::response::reset_params();
394: } elsif ($target eq 'meta') {
1.129 albertel 395: $result .= &Apache::response::meta_package_write('customresponse');
1.128 albertel 396: }
397: return $result;
398: }
399:
1.129 albertel 400: sub end_customresponse {
1.128 albertel 401: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
402: my $result;
403: my $part=$Apache::inputtags::part;
404: my $id=$Apache::inputtags::response[-1];
405: if ( $target eq 'grade' && &Apache::response::submitted() ) {
406: my $response = &Apache::response::getresponse();
407: if ( $response =~ /[^\s]/ &&
1.129 albertel 408: $Apache::response::custom_answer_type eq 'loncapa/perl') {
1.128 albertel 409: if (!$Apache::lonxml::default_homework_loaded) {
410: &Apache::lonxml::default_homework_load($safeeval);
411: }
412: my %previous = &Apache::response::check_for_previous($response,
413: $part,$id);
414: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
415: $response;
416: my $error;
1.129 albertel 417: ${$safeeval->varglob('LONCAPA::customresponse_submission')}=
1.128 albertel 418: $response;
419:
1.129 albertel 420: my $award = &Apache::run::run('{ my $submission=$LONCAPA::customresponse_submission;'.$Apache::response::custom_answer.'}',$safeeval);
1.128 albertel 421: if (!&Apache::inputtags::valid_award($award)) {
422: $error = $award;
423: $award = 'ERROR';
424: }
425: &Apache::response::handle_previous(\%previous,$award);
426: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
427: $award;
428: if ($error) {
429: $Apache::lonhomework::results{"resource.$part.$id.awardmsg"}=
430: $error;
431: }
432: }
433: }
434: pop(@Apache::lonxml::namespace);
435: &Apache::lonxml::deregister('Apache::response',('answer'));
436: &Apache::response::end_response();
437: return $result;
438: }
439:
1.140 www 440:
1.139 www 441: sub start_mathresponse {
1.140 www 442: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
443: my $id = &Apache::response::start_response($parstack,$safeeval);
444: push(@Apache::lonxml::namespace,'mathresponse');
1.139 www 445: my $result;
1.140 www 446: undef($Apache::response::custom_answer);
447: &Apache::lonxml::register('Apache::response',('answer'));
448: if ($target eq 'web') {
449: if ( &Apache::response::show_answer() ) {
450: my $answer = &Apache::lonxml::get_param('answerdisplay',$parstack,
451: $safeeval);
452: $Apache::inputtags::answertxt{$id}=$answer;
453: }
454: } elsif ($target eq 'edit') {
455: $result.=&Apache::edit::tag_start($target,$token);
456: $result.=&Apache::edit::text_arg('String to display for answer:',
457: 'answerdisplay',$token);
458: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
459: } elsif ($target eq 'modified') {
460: my $constructtag;
461: $constructtag=&Apache::edit::get_new_args($token,$parstack,
462: $safeeval,'answerdisplay');
463: if ($constructtag) {
464: $result = &Apache::edit::rebuild_tag($token);
465: $result.=&Apache::edit::handle_insert();
466: }
467: } elsif ($target eq 'answer' || $target eq 'grade') {
468: &Apache::response::reset_params();
469: } elsif ($target eq 'meta') {
470: $result .= &Apache::response::meta_package_write('mathresponse');
1.139 www 471: }
472: return $result;
473: }
474:
475: sub end_mathresponse {
1.140 www 476: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
477: my $result;
478: my $part=$Apache::inputtags::part;
479: my $id=$Apache::inputtags::response[-1];
480: if ( $target eq 'grade' && &Apache::response::submitted() ) {
481: my $response = &Apache::response::getresponse();
482: if ( $response =~ /[^\s]/ &&
483: $Apache::response::custom_answer_type eq 'loncapa/perl') {
484: if (!$Apache::lonxml::default_homework_loaded) {
485: &Apache::lonxml::default_homework_load($safeeval);
486: }
487: my %previous = &Apache::response::check_for_previous($response,
488: $part,$id);
489: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
490: $response;
491: my $error;
492: ${$safeeval->varglob('LONCAPA::mathresponse_submission')}=
493: $response;
494:
495: my $award = &Apache::run::run('{ my $submission=$LONCAPA::mathresponse_submission;'.$Apache::response::custom_answer.'}',$safeeval);
496: if (!&Apache::inputtags::valid_award($award)) {
497: $error = $award;
498: $award = 'ERROR';
499: }
500: &Apache::response::handle_previous(\%previous,$award);
501: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
502: $award;
503: if ($error) {
504: $Apache::lonhomework::results{"resource.$part.$id.awardmsg"}=
505: $error;
506: }
507: }
508: }
509: pop(@Apache::lonxml::namespace);
510: &Apache::lonxml::deregister('Apache::response',('answer'));
511: &Apache::response::end_response();
512: return $result;
1.139 www 513: }
514:
1.140 www 515:
1.128 albertel 516: sub start_answer {
517: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
518: my $result;
1.129 albertel 519: $Apache::response::custom_answer=
1.128 albertel 520: &Apache::lonxml::get_all_text_unbalanced("/answer",$parser);
1.129 albertel 521: $Apache::response::custom_answer_type=
1.128 albertel 522: lc(&Apache::lonxml::get_param('type',$parstack,$safeeval));
1.129 albertel 523: $Apache::response::custom_answer_type =~ s/\s+//g;
1.128 albertel 524: if ($target eq "edit" ) {
525: $result=&Apache::edit::tag_start($target,$token,'Answer algorithm');
526: $result.=&Apache::edit::editfield($token->[1],
1.129 albertel 527: $Apache::response::custom_answer,
1.128 albertel 528: '',80,4);
529: } elsif ( $target eq "modified" ) {
530: $result=$token->[4].&Apache::edit::modifiedfield('/answer',$parser);
531: }
532: return $result;
533: }
534:
535: sub end_answer {
536: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
537: if ($target eq 'edit' ) {
538: return &Apache::edit::end_table();
539: }
540: }
541:
1.83 albertel 542: sub decide_package {
543: my ($tagstack)=@_;
544: my $package;
545: if ($$tagstack[-1] eq 'parameter') {
546: $package='part';
547: } else {
548: my $i=-1;
549: while (defined($$tagstack[$i])) {
550: if ($$tagstack[$i] =~ /(response|hint)$/) {
551: $package=$$tagstack[$i];
552: last;
553: }
554: $i--;
555: }
556: }
557: return $package;
558: }
559:
1.3 albertel 560: sub start_responseparam {
1.73 albertel 561: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
562: my $result='';
563: if ($target eq 'meta') {
564: $result = &meta_parameter_write($token->[2]->{'name'},
565: $token->[2]->{'type'},
566: $token->[2]->{'default'},
567: $token->[2]->{'description'});
568: } elsif ($target eq 'edit') {
569: $result.=&Apache::edit::tag_start($target,$token);
1.83 albertel 570: my $optionlist;
571: my $package=&decide_package($tagstack);
572: foreach my $key (sort(keys(%Apache::lonnet::packagetab))) {
573: if ($key =~ /^\Q$package\E&(.*)&display$/) {
574: $optionlist.='<option value="'.$1.'">'.
575: $Apache::lonnet::packagetab{$key}.'</option>';
576: }
577: }
578: if (defined($optionlist)) {
579: $result.='Use template: <select name="'.
580: &Apache::edit::html_element_name('parameter_package').'">'.
581: '<option value=""></option>'.$optionlist.'</select><br />';
582: }
1.73 albertel 583: $result.=&Apache::edit::text_arg('Name:','name',$token).
584: &Apache::edit::text_arg('Type:','type',$token).
585: &Apache::edit::text_arg('Description:','description',$token).
586: &Apache::edit::text_arg('Default:','default',$token).
587: "</td></tr>";
588: $result.=&Apache::edit::end_table;
589: } elsif ($target eq 'modified') {
1.83 albertel 590: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
591: $safeeval,'name','type',
592: 'description','default');
593: my $element=&Apache::edit::html_element_name('parameter_package');
1.120 albertel 594: if (defined($env{"form.$element"}) && $env{"form.$element"} ne '') {
595: my $name=$env{"form.$element"};
1.83 albertel 596: my $tag=&decide_package($tagstack);
597: $token->[2]->{'name'}=$name;
598: $token->[2]->{'type'}=
599: $Apache::lonnet::packagetab{"$tag&$name&type"};
600: $token->[2]->{'description'}=
601: $Apache::lonnet::packagetab{"$tag&$name&display"};
602: $token->[2]->{'default'}=
603: $Apache::lonnet::packagetab{"$tag&$name&default"};
604: $constructtag=1;
605: }
1.73 albertel 606: if ($constructtag) {
607: $result = &Apache::edit::rebuild_tag($token);
608: $result.=&Apache::edit::handle_insert();
609: }
610: } elsif ($target eq 'grade' || $target eq 'answer' || $target eq 'web' ||
611: $target eq 'tex' || $target eq 'analyze' ) {
1.120 albertel 612: if ($env{'request.state'} eq 'construct') {
1.73 albertel 613: my $name =&Apache::lonxml::get_param('name',$parstack,$safeeval);
614: my $default=&Apache::lonxml::get_param('default',$parstack,
615: $safeeval);
616: if ($name) {$Apache::inputtags::params{$name}=$default;}
617: }
1.52 albertel 618: }
1.73 albertel 619: return $result;
1.3 albertel 620: }
621:
622: sub end_responseparam {
1.73 albertel 623: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
624: if ($target eq 'edit') { return ('','no'); }
625: return '';
1.55 albertel 626: }
627:
628: sub start_parameter {
1.114 albertel 629: return &start_responseparam(@_);
1.55 albertel 630: }
631:
632: sub end_parameter {
1.114 albertel 633: return &end_responseparam(@_);
1.42 albertel 634: }
635:
1.67 albertel 636: sub reset_params {
637: %Apache::inputtags::params=();
638: }
639:
1.42 albertel 640: sub setup_params {
1.94 albertel 641: my ($tag,$safeeval) = @_;
1.42 albertel 642:
1.120 albertel 643: if ($env{'request.state'} eq 'construct') { return; }
1.73 albertel 644: my %paramlist=();
645: foreach my $key (keys(%Apache::lonnet::packagetab)) {
646: if ($key =~ /^$tag/) {
647: my ($package,$name) = split(/&/,$key);
648: $paramlist{$name}=1;
649: }
1.42 albertel 650: }
1.73 albertel 651: foreach my $key (keys(%paramlist)) {
652: my $entry= 'resource.'.$Apache::inputtags::part;
653: if (defined($Apache::inputtags::response[-1])) {
654: $entry.='_'.$Apache::inputtags::response[-1];
655: }
656: $entry.='.'.$key;
657: &Apache::lonxml::debug("looking for $entry");
658: my $value = &Apache::lonnet::EXT("$entry");
659: &Apache::lonxml::debug("$key has value :$value:");
660: if ($value eq 'con_lost' || $value =~ /^error:/) {
661: &Apache::lonxml::debug("using nothing");
662: $Apache::inputtags::params{$key}='';
663: } else {
1.94 albertel 664: my $string="{return qq\0".$value."\0}";
665: my $newvalue=&Apache::run::run($string,$safeeval,1);
666: if (defined($newvalue)) { $value=$newvalue; }
1.73 albertel 667: $Apache::inputtags::params{$key}=$value;
668: }
1.42 albertel 669: }
1.48 albertel 670: }
671:
1.132 albertel 672: {
673: my @answer_bits;
674:
1.48 albertel 675: sub answer_header {
1.133 albertel 676: my ($type,$increment) = @_;
1.73 albertel 677: my $result;
1.120 albertel 678: if ($env{'form.answer_output_mode'} eq 'tex') {
1.132 albertel 679: undef(@answer_bits);
1.133 albertel 680: my $bit;
681: if ($Apache::lonhomework::type eq 'exam') {
682: $bit = ($Apache::lonxml::counter+$increment).') ';
683: } else {
684: $bit .= ' Answer for Part: \verb|'.
685: $Apache::inputtags::part.'| ';
686: }
687: push(@answer_bits,$bit);
1.73 albertel 688: } else {
1.132 albertel 689: $result = '<table border="1"><tr>';
690: if ($Apache::lonhomework::type eq 'exam') {
1.138 albertel 691: $result .= '<td>'.($Apache::lonxml::counter+$increment). ')</td>';
1.132 albertel 692: } else {
693: $result .= '<td>Answer for Part:'.$Apache::inputtags::part.'</td>';
694: }
695: $result .= "\n";
1.73 albertel 696: }
697: return $result;
1.48 albertel 698: }
699:
700: sub answer_part {
1.73 albertel 701: my ($type,$answer) = @_;
702: my $result;
1.120 albertel 703: if ($env{'form.answer_output_mode'} eq 'tex') {
1.124 albertel 704: my $to_use='|';
705: foreach my $value (32..126) {
706: my $char=pack('c',$value);
707: if ($answer !~ /\Q$char\E/) {
708: $to_use=$char;
709: last;
710: }
711: }
1.132 albertel 712: if ($answer ne '') {
713: push(@answer_bits,'\verb'.$to_use.$answer.$to_use);
714: }
1.73 albertel 715: } else {
1.80 albertel 716: $result = '<td>'.$answer.'</td>';
1.73 albertel 717: }
718: return $result;
1.48 albertel 719: }
720:
721: sub answer_footer {
1.73 albertel 722: my ($type) = @_;
723: my $result;
1.120 albertel 724: if ($env{'form.answer_output_mode'} eq 'tex') {
1.133 albertel 725: my $columns = scalar(@answer_bits);
1.134 albertel 726: $result = ' \vskip 0 mm \noindent \begin{tabular}{|'.'c|'x$columns.'}\hline ';
1.133 albertel 727: $result .= join(' & ',@answer_bits);
1.132 albertel 728: $result .= ' \\\\ \\hline \end{tabular} \vskip 0 mm ';
1.73 albertel 729: } else {
1.80 albertel 730: $result = '</tr></table>';
1.73 albertel 731: }
732: return $result;
1.1 albertel 733: }
1.2 albertel 734:
1.132 albertel 735: }
736:
1.62 albertel 737: sub showallfoils {
1.120 albertel 738: if (defined($env{'form.showallfoils'})) {
1.102 albertel 739: my ($symb)=&Apache::lonxml::whichuser();
1.120 albertel 740: if (($env{'request.state'} eq 'construct') ||
741: ($env{'user.adv'} && $symb eq '') ||
1.118 foxr 742: ($Apache::lonhomework::viewgrades) ) {
1.102 albertel 743: return 1;
744: }
1.73 albertel 745: }
1.108 albertel 746: if ($Apache::lonhomework::type eq 'survey') { return 1; }
1.102 albertel 747: return 0;
1.70 albertel 748: }
749:
750: sub getresponse {
1.90 albertel 751: my ($temp,$resulttype)=@_;
1.70 albertel 752: my $formparm='form.HWVAL_'.$Apache::inputtags::response['-1'];
753: my $response;
754: if (!defined($temp)) {
755: $temp=1;
756: } else {
757: $formparm.=":$temp";
758: }
759: my %let_to_num=('A'=>0,'B'=>1,'C'=>2,'D'=>3,'E'=>4,'F'=>5,'G'=>6,'H'=>7,
760: 'I'=>8,'J'=>9,'K'=>10,'L'=>11,'M'=>12,'N'=>13,'O'=>14,
761: 'P'=>15,'Q'=>16,'R'=>17,'S'=>18,'T'=>19,'U'=>20,'V'=>21,
762: 'W'=>22,'X'=>23,'Y'=>24,'Z'=>25);
1.120 albertel 763: if ($env{'form.submitted'} eq 'scantron') {
1.71 albertel 764: my $part = $Apache::inputtags::part;
765: my $id = $Apache::inputtags::response[-1];
1.120 albertel 766: $response = $env{'scantron.'.($Apache::lonxml::counter+$temp-1).
1.70 albertel 767: '.answer'};
1.71 albertel 768: # save bubbled letter for later
769: $Apache::lonhomework::results{"resource.$part.$id.scantron"}.=
770: $response;
1.90 albertel 771: if ($resulttype ne 'letter') {
1.104 albertel 772: if ($resulttype eq 'A is 1') {
1.105 albertel 773: $response = $let_to_num{$response}+1;
774: } else {
1.104 albertel 775: $response = $let_to_num{$response};
776: }
1.90 albertel 777: }
1.70 albertel 778: } else {
1.120 albertel 779: $response = $env{$formparm};
1.70 albertel 780: }
781: return $response;
1.62 albertel 782: }
1.71 albertel 783:
784: sub repetition {
785: my $id = $Apache::inputtags::part;
786: my $weight = &Apache::lonnet::EXT("resource.$id.weight");
1.121 albertel 787: if (!defined($weight) || ($weight eq '')) { $weight=1; }
1.125 albertel 788: my $repetition = int($weight/10);
789: if ($weight % 10 != 0) { $repetition++; }
1.72 albertel 790: return $repetition;
791: }
792:
793: sub scored_response {
794: my ($part,$id)=@_;
795: my $repetition=&repetition();
796: my $score=0;
797: for (my $i=0;$i<$repetition;$i++) {
1.125 albertel 798: # A is 1, B is 2, etc. (get response return 0-9 and then we add 1)
1.72 albertel 799: my $increase=&Apache::response::getresponse($i+1);
800: if ($increase ne '') { $score+=$increase+1; }
801: }
802: my $weight = &Apache::lonnet::EXT("resource.$part.weight");
1.91 albertel 803: if (!defined($weight) || $weight eq '' || $weight eq 0) { $weight = 1; }
1.72 albertel 804: my $pcr=$score/$weight;
805: $Apache::lonhomework::results{"resource.$part.$id.awarded"}=$pcr;
806: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
807: 'ASSIGNED_SCORE';
1.71 albertel 808: return $repetition;
1.78 albertel 809: }
810:
811: sub whichorder {
812: my ($max,$randomize,$showall,$hash)=@_;
813: #&Apache::lonxml::debug("man $max randomize $randomize");
814: if (!defined(@{ $$hash{'names'} })) { return; }
815: my @names = @{ $$hash{'names'} };
816: my @whichopt =();
817: my (%top,@toplist,%bottom,@bottomlist);
818: if (!($showall || ($randomize eq 'no'))) {
819: my $current=0;
820: foreach my $name (@names) {
821: $current++;
822: if ($$hash{"$name.location"} eq 'top') {
823: $top{$name}=$current;
824: } elsif ($$hash{"$name.location"} eq 'bottom') {
825: $bottom{$name}=$current;
826: }
827: }
828: }
829: my $topcount=0;
830: my $bottomcount=0;
831: while (((scalar(@whichopt)+$topcount+$bottomcount) < $max || $showall)
832: && ($#names > -1)) {
833: #&Apache::lonxml::debug("Have $#whichopt max is $max");
834: my $aopt;
835: if ($showall || ($randomize eq 'no')) {
836: $aopt=0;
837: } else {
838: $aopt=int(&Math::Random::random_uniform() * ($#names+1));
839: }
840: #&Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
841: $aopt=splice(@names,$aopt,1);
842: #&Apache::lonxml::debug("Picked $aopt");
843: if ($top{$aopt}) {
844: $toplist[$top{$aopt}]=$aopt;
845: $topcount++;
846: } elsif ($bottom{$aopt}) {
847: $bottomlist[$bottom{$aopt}]=$aopt;
848: $bottomcount++;
849: } else {
850: push (@whichopt,$aopt);
851: }
852: }
853: for (my $i=0;$i<=$#toplist;$i++) {
854: if ($toplist[$i]) { unshift(@whichopt,$toplist[$i]) }
855: }
856: for (my $i=0;$i<=$#bottomlist;$i++) {
857: if ($bottomlist[$i]) { push(@whichopt,$bottomlist[$i]) }
858: }
859: return @whichopt;
1.71 albertel 860: }
861:
1.85 albertel 862: sub show_answer {
863: my $part = $Apache::inputtags::part;
864: my $award = $Apache::lonhomework::history{"resource.$part.solved"};
865: my $status = $Apache::inputtags::status[-1];
866: return ( ($award =~ /^correct/
867: && lc($Apache::lonhomework::problemstatus) ne 'no')
868: || $status eq "SHOW_ANSWER");
869: }
1.87 albertel 870:
871: sub analyze_store_foilgroup {
872: my ($shown,$attrs)=@_;
873: my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
874: foreach my $name (@{ $Apache::response::foilgroup{'names'} }) {
875: if (defined($Apache::lonhomework::analyze{"$part_id.foil.value.$name"})) { next; }
876: push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },$name);
877: foreach my $attr (@$attrs) {
878: $Apache::lonhomework::analyze{"$part_id.foil.".$attr.".$name"} =
879: $Apache::response::foilgroup{"$name.".$attr};
880: }
881: }
882: push (@{ $Apache::lonhomework::analyze{"$part_id.shown"} }, @{ $shown });
1.96 albertel 883: }
884:
885: sub check_if_computed {
886: my ($token,$parstack,$safeeval,$name)=@_;
887: my $value = &Apache::lonxml::get_param($name,$parstack,$safeeval);
1.106 matthew 888: if (ref($token->[2]) eq 'HASH' && $value ne $token->[2]{$name}) {
1.96 albertel 889: my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
890: $Apache::lonhomework::analyze{"$part_id.answercomputed"} = 1;
891: }
1.87 albertel 892: }
893:
894: sub pick_foil_for_concept {
895: my ($target,$attrs,$hinthash,$parstack,$safeeval)=@_;
896: if (not defined(@{ $Apache::response::conceptgroup{'names'} })) { return; }
897: my @names = @{ $Apache::response::conceptgroup{'names'} };
898: my $pick=int(&Math::Random::random_uniform() * ($#names+1));
899: my $name=$names[$pick];
900: push @{ $Apache::response::foilgroup{'names'} }, $name;
901: foreach my $attr (@$attrs) {
902: $Apache::response::foilgroup{"$name.".$attr} =
903: $Apache::response::conceptgroup{"$name.".$attr};
904: }
905: my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
906: $Apache::response::foilgroup{"$name.concept"} = $concept;
907: &Apache::lonxml::debug("Selecting $name in $concept");
908: my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
909: if ($target eq 'analyze') {
910: push (@{ $Apache::lonhomework::analyze{"$part_id.concepts"} },
911: $concept);
912: $Apache::lonhomework::analyze{"$part_id.concept.$concept"}=
913: $Apache::response::conceptgroup{'names'};
914: foreach my $name (@{ $Apache::response::conceptgroup{'names'} }) {
915: push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },
916: $name);
917: foreach my $attr (@$attrs) {
918: $Apache::lonhomework::analyze{"$part_id.foil.$attr.$name"}=
919: $Apache::response::conceptgroup{"$name.$attr"};
920: }
921: }
922: }
923: push(@{ $hinthash->{"$part_id.concepts"} },$concept);
924: $hinthash->{"$part_id.concept.$concept"}=
925: $Apache::response::conceptgroup{'names'};
926:
927: }
928:
1.95 albertel 929: sub get_response_param {
930: my ($id,$name,$default)=@_;
931: my $parameter;
1.120 albertel 932: if ($env{'request.state'} eq 'construct' &&
1.95 albertel 933: defined($Apache::inputtags::params{$name})) {
934: $parameter=$Apache::inputtags::params{$name};
935: } else {
936: $parameter=&Apache::lonnet::EXT("resource.$id.$name");
937: }
938: if (!defined($parameter) || $parameter eq '') {
939: $parameter = $default;
940: }
941: return $parameter;
942: }
1.87 albertel 943:
1.113 albertel 944: sub submitted {
945: my ($who)=@_;
946:
947: # when scatron grading any submission is a submission
1.120 albertel 948: if ($env{'form.submitted'} eq 'scantron') { return 1; }
1.113 albertel 949: # if the caller only cared if this was a scantron submission
950: if ($who eq 'scantron') { return 0; }
951: # if the Submit Answer button for this particular part was pressed
952: my $partid=$Apache::inputtags::part;
1.120 albertel 953: if (defined($env{'form.submit_'.$partid})) { return 1; }
1.122 albertel 954: # Submit All button on a .page was pressed
955: if (defined($env{'form.all_submit'})) { return 1; }
1.113 albertel 956: # otherwise no submission occured
957: return 0;
958: }
1.117 albertel 959:
1.130 albertel 960: sub add_to_gradingqueue {
1.131 albertel 961: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
962: if ( $courseid eq ''
963: || $symb eq ''
1.135 albertel 964: || $env{'request.state'} eq 'construct'
965: || $Apache::lonhomework::type ne 'problem') {
1.131 albertel 966: return;
967: }
1.130 albertel 968:
969: my %queue_info = ( 'type' => 'problem',
970: 'time' => time);
971:
972: if (exists($Apache::lonhomework::history{"resource.0.checkedin.slot"})) {
973: $queue_info{'slot'}=
974: $Apache::lonhomework::history{"resource.0.checkedin.slot"};
975: }
976:
977: my $result=&Apache::bridgetask::add_to_queue('gradingqueue',\%queue_info);
978: if ($result ne 'ok') {
979: &Apache::lonxml::error("add_to_queue said $result");
980: }
981: }
982:
1.117 albertel 983: # basically undef and 0 (both false) mean that they still have work to do
984: # and all true values mean that they can't do any more work
985: #
986: # a return of undef means it is unattempted
987: # a return of 0 means it is attmpted and wrong but still has tries
988: # a return of 1 means it is marked correct
989: # a return of 2 means they have exceed maximum number of tries
990: # a return of 3 means it after the answer date
991: sub check_status {
992: my ($id)=@_;
1.143 albertel 993: if (!defined($id)) { $id=$Apache::inputtags::part; }
1.117 albertel 994: my $curtime=&Apache::lonnet::EXT('system.time');
995: my $opendate=&Apache::lonnet::EXT("resource.$id.opendate");
996: my $duedate=&Apache::lonnet::EXT("resource.$id.duedate");
997: my $answerdate=&Apache::lonnet::EXT("resource.$id.answerdate");
998: if ( $opendate && $curtime > $opendate &&
999: $duedate && $curtime > $duedate &&
1000: $answerdate && $curtime > $answerdate) {
1001: return 3;
1002: }
1003: my $status=&Apache::lonnet::EXT("user.resource.resource.$id.solved");
1004: if ($status =~ /^correct/) { return 1; }
1005: if (!$status) { return undef; }
1006: my $maxtries=&Apache::lonnet::EXT("resource.$id.maxtries");
1007: if ($maxtries eq '') { $maxtries=2; }
1008: my $curtries=&Apache::lonnet::EXT("user.resource.resource.$id.tries");
1009: if ($curtries < $maxtries) { return 0; }
1010: return 2;
1011: }
1012:
1.1 albertel 1013: 1;
1014: __END__
1.38 albertel 1015:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>