Annotation of loncom/homework/response.pm, revision 1.251
1.38 albertel 1: # The LearningOnline Network with CAPA
1.1 albertel 2: # various response type definitons response definition
1.53 albertel 3: #
1.251 ! raeburn 4: # $Id: response.pm,v 1.250 2024/10/29 03:22:10 raeburn 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.208 jms 29: =pod
30:
31: =head1 NAME
32:
1.218 raeburn 33: Apache::response.pm
1.208 jms 34:
35: =head1 SYNOPSIS
36:
37: This is part of the LearningOnline Network with CAPA project
38: described at http://www.lon-capa.org.
39:
40:
41: =head1 NOTABLE SUBROUTINES
42:
43: =over
44:
1.251 ! raeburn 45: =item
1.208 jms 46:
47: =back
48:
49: =cut
50:
51:
1.1 albertel 52: package Apache::response;
53: use strict;
1.93 albertel 54: use Apache::lonlocal;
1.120 albertel 55: use Apache::lonnet;
1.228 raeburn 56: use Apache::inputtags();
1.153 www 57: use Apache::lonmaxima();
1.214 www 58: use Apache::lonr();
1.238 raeburn 59: use Apache::lontexconvert();
1.1 albertel 60:
1.57 harris41 61: BEGIN {
1.139 www 62: &Apache::lonxml::register('Apache::response',('responseparam','parameter','dataresponse','customresponse','mathresponse'));
1.1 albertel 63: }
64:
1.13 albertel 65: sub start_response {
1.73 albertel 66: my ($parstack,$safeeval)=@_;
1.136 albertel 67: my $id = &Apache::lonxml::get_id($parstack,$safeeval);
1.73 albertel 68: if ($#Apache::inputtags::import > -1) {
69: &Apache::lonxml::debug("Turning :$id: into");
70: $id = join('_',@Apache::inputtags::import).'_'.$id;
71: &Apache::lonxml::debug("New :$id:");
72: }
73: push (@Apache::inputtags::response,$id);
74: push (@Apache::inputtags::responselist,$id);
75: @Apache::inputtags::inputlist=();
1.251 ! raeburn 76: if ($Apache::inputtags::part eq '' &&
1.101 albertel 77: !$Apache::lonhomework::ignore_response_errors) {
1.97 albertel 78: &Apache::lonxml::error(&HTML::Entities::encode(&mt("Found a <*response> outside of a <part> in a <part>ed problem"),'<>&"'));
1.92 albertel 79: }
80: if ($Apache::inputtags::response_with_no_part &&
81: $Apache::inputtags::part ne '0') {
1.97 albertel 82: &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 83: }
84: if ($Apache::inputtags::part eq '0') {
85: $Apache::inputtags::response_with_no_part=1;
86: }
1.73 albertel 87: return $id;
1.13 albertel 88: }
89:
90: sub end_response {
1.79 albertel 91: #pop @Apache::inputtags::response;
1.73 albertel 92: @Apache::inputtags::inputlist=();
93: return '';
1.13 albertel 94: }
95:
1.41 albertel 96: sub start_hintresponse {
1.73 albertel 97: my ($parstack,$safeeval)=@_;
1.136 albertel 98: my $id = &Apache::lonxml::get_id($parstack,$safeeval);
1.123 albertel 99: push (@Apache::inputtags::hint,$id);
100: push (@Apache::inputtags::hintlist,$id);
1.73 albertel 101: push (@Apache::inputtags::paramstack,[%Apache::inputtags::params]);
102: return $id;
1.41 albertel 103: }
104:
105: sub end_hintresponse {
1.123 albertel 106: pop @Apache::inputtags::hint;
1.73 albertel 107: if (defined($Apache::inputtags::paramstack[-1])) {
108: %Apache::inputtags::params=
109: @{ pop(@Apache::inputtags::paramstack) };
110: }
111: return '';
1.41 albertel 112: }
113:
1.99 albertel 114: my @randomseeds;
115: sub pushrandomnumber {
116: my $rand_alg=&Apache::lonnet::get_rand_alg();
117: if (!$rand_alg || $rand_alg eq '32bit' || $rand_alg eq '64bit' ||
118: $rand_alg eq '64bit2') {
119: # do nothing
120: } else {
121: my @seed=&Math::Random::random_get_seed();
1.127 albertel 122: push(@randomseeds,\@seed);
1.99 albertel 123: }
1.127 albertel 124: &Apache::response::setrandomnumber(@_);
1.99 albertel 125: }
126: sub poprandomnumber {
127: my $rand_alg=&Apache::lonnet::get_rand_alg();
128: if (!$rand_alg || $rand_alg eq '32bit' || $rand_alg eq '64bit' ||
129: $rand_alg eq '64bit2') {
130: return;
131: }
132: my $seed=pop(@randomseeds);
133: if ($seed) {
134: &Math::Random::random_set_seed(@$seed);
135: } else {
136: &Apache::lonxml::error("Unable to restore random algorithm.");
137: }
138: }
1.117 albertel 139:
1.26 albertel 140: sub setrandomnumber {
1.221 raeburn 141: my ($ignore_id2,$target,$rndseed) = @_;
142: if (!defined($rndseed)) {
143: $rndseed=&Apache::structuretags::setup_rndseed(undef,$target);
1.251 ! raeburn 144: }
1.88 albertel 145: if (!defined($rndseed)) { $rndseed=&Apache::lonnet::rndseed(); }
1.73 albertel 146: &Apache::lonxml::debug("randseed $rndseed");
147: # $rndseed=unpack("%32i",$rndseed);
1.99 albertel 148: my $rand_alg=&Apache::lonnet::get_rand_alg();
1.126 albertel 149: my ($rndmod,$rndmod2);
1.119 albertel 150:
151: my ($id1,$id2,$shift_amt);
152: if ($Apache::lonhomework::parsing_a_problem) {
153: $id1=$Apache::inputtags::part;
154: if (defined($Apache::inputtags::response[-1])) {
155: $id2=$Apache::inputtags::response[-1];
156: }
157: $shift_amt=scalar(@Apache::inputtags::responselist);
158: } elsif ($Apache::lonhomework::parsing_a_task) {
1.141 albertel 159: $id1=&Apache::bridgetask::get_dim_id();
160: if (!$ignore_id2 && ref($Apache::bridgetask::instance{$id1})) {
161: $id2=$Apache::bridgetask::instance{$id1}[-1];
1.142 albertel 162: $shift_amt=scalar(@{$Apache::bridgetask::instance{$id1}});
163: } else {
164: $shift_amt=0;
1.119 albertel 165: }
1.251 ! raeburn 166: }
1.119 albertel 167: &Apache::lonxml::debug("id1: $id1, id2: $id2, shift_amt: $shift_amt");
1.99 albertel 168: if (!$rand_alg || $rand_alg eq '32bit' || $rand_alg eq '64bit' ||
169: $rand_alg eq '64bit2') {
1.119 albertel 170: $rndmod=(&Apache::lonnet::numval($id1) << 10);
171: if (defined($id2)) { $rndmod+=&Apache::lonnet::numval($id2); }
1.110 albertel 172: } elsif ($rand_alg eq '64bit3') {
1.119 albertel 173: $rndmod=(&Apache::lonnet::numval2($id1) << 10);
174: if (defined($id2)) { $rndmod+=&Apache::lonnet::numval2($id2); }
1.126 albertel 175: } elsif ($rand_alg eq '64bit4') {
1.119 albertel 176: my $shift=(4*$shift_amt)%30;
177: $rndmod=(&Apache::lonnet::numval3($id1) << (($shift+15)%30));
178: if (defined($id2)) {
179: $rndmod+=(&Apache::lonnet::numval3($id2) << $shift );
1.110 albertel 180: }
1.126 albertel 181: } else {
182: ($rndmod,$rndmod2)=&Apache::lonnet::digest("$id1,$id2");
1.99 albertel 183: }
184: if ($rndseed =~/([,:])/) {
185: my $char=$1;
186: use integer;
187: my ($num1,$num2)=split(/\Q$char\E/,$rndseed);
188: $num1+=$rndmod;
1.126 albertel 189: $num2+= ((defined($rndmod2)) ? $rndmod2 : $rndmod);
1.109 albertel 190: if($Apache::lonnet::_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.99 albertel 191: $rndseed=$num1.$char.$num2;
192: } else {
1.74 albertel 193: $rndseed+=$rndmod;
1.109 albertel 194: if($Apache::lonnet::_64bit) {
195: use integer;
196: $rndseed=(($rndseed<<32)>>32);
197: }
1.74 albertel 198: }
1.111 albertel 199: &Apache::lonxml::debug("randseed $rndmod $rndseed");
1.74 albertel 200: &Apache::lonnet::setup_random_from_rndseed($rndseed);
1.73 albertel 201: return '';
1.26 albertel 202: }
203:
1.7 www 204: sub meta_parameter_write {
1.38 albertel 205: my ($name,$type,$default,$display)=@_;
1.41 albertel 206: my $partref=$Apache::inputtags::part;
207: my $result='<parameter 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.'"'.
1.89 albertel 214: (defined($default)?' default="'.$default.'"':'').
215: (defined($display)?' display="'.$display.' [Part: '.$partref.']"':'')
1.41 albertel 216: .'></parameter>'
217: ."\n";
218: return $result;
1.33 www 219: }
220:
221: sub meta_package_write {
222: my $name=shift;
1.41 albertel 223: my $result = '<parameter part="'.$Apache::inputtags::part.'"';
224: if(defined($Apache::inputtags::response[-1])) {
1.73 albertel 225: $result.= ' id="'.$Apache::inputtags::response[-1].'"';
1.41 albertel 226: }
227: $result.=' package="'.$name.'"></parameter>'."\n";
228: return $result;
1.7 www 229: }
230:
231: sub meta_stores_write {
1.10 www 232: my ($name,$type,$display)=@_;
1.41 albertel 233: my $partref=$Apache::inputtags::part;
234: my $result = '<stores part="'.$Apache::inputtags::part.'"';
235: if (defined($Apache::inputtags::response[-1])) {
1.73 albertel 236: $result.= ' id="'.$Apache::inputtags::response[-1].'"';
237: $partref.='_'.$Apache::inputtags::response[-1];
1.41 albertel 238: }
239: $result.= ' name="'.$name.'"'.
240: ' type="'.$type.'"'.
241: ' display="'.$display.' [Part: '.$partref.']"'.
242: "></stores>\n";
1.7 www 243: }
244:
1.207 jms 245: =pod
246:
1.210 raeburn 247: =item mandatory_part_meta()
1.207 jms 248:
249: Autogenerate metadata for mandatory
1.210 raeburn 250: input (from RAT or lonparmset) and
1.207 jms 251: output (to lonspreadsheet)
252: of each part
253:
254: Note: responseid-specific data 'submission' and 'awarddetail'
255: not available to spreadsheet -> skip here
256:
257: =cut
258:
1.210 raeburn 259:
260: sub mandatory_part_meta {
261: return &meta_package_write('part').
262: &meta_stores_write('solved','string','Problem Status').
263: &meta_stores_write('tries','int_zeropos','Number of Attempts').
264: &meta_stores_write('awarded','float','Partial Credit Factor');
1.86 albertel 265: }
266:
267: sub meta_part_order {
1.251 ! raeburn 268: my ($type) = @_;
1.86 albertel 269: if (@Apache::inputtags::partlist) {
270: my @parts=@Apache::inputtags::partlist;
1.245 raeburn 271: unless ($type eq 'library') {
272: shift(@parts);
273: }
1.100 albertel 274: return '<partorder>'.join(',',@parts).'</partorder>'."\n";
1.245 raeburn 275: } elsif ($type eq 'library') {
276: return '<partorder></partorder>'."\n";
1.86 albertel 277: } else {
1.100 albertel 278: return '<partorder>0</partorder>'."\n";
279: }
280: }
281:
282: sub meta_response_order {
283: if (@Apache::inputtags::responselist) {
284: return '<responseorder>'.join(',',@Apache::inputtags::responselist).
285: '</responseorder>'."\n";
1.86 albertel 286: }
1.14 albertel 287: }
288:
1.15 albertel 289: sub check_for_previous {
1.219 raeburn 290: my ($curresponse,$partid,$id,$last,$type) = @_;
1.73 albertel 291: my %previous;
292: $previous{'used'} = 0;
1.221 raeburn 293: my $questiontype = $Apache::lonhomework::type;
294: my $curr_rndseed = $env{'form.'.$partid.'.rndseed'};
1.73 albertel 295: foreach my $key (sort(keys(%Apache::lonhomework::history))) {
1.212 raeburn 296: if ($key =~ /resource\.\Q$partid\E\.\Q$id\E\.submission$/) {
1.160 albertel 297: if ( $last && $key =~ /^(\d+):/ ) {
298: next if ($1 >= $last);
299: }
1.73 albertel 300: &Apache::lonxml::debug("Trying $key");
301: my $pastresponse=$Apache::lonhomework::history{$key};
302: if ($pastresponse eq $curresponse) {
303: my $history;
304: if ( $key =~ /^(\d+):/ ) {
1.221 raeburn 305: $history=$1;
306: next if ((($questiontype eq 'randomizetry') ||
307: ($Apache::lonhomework::history{"$history:resource.$partid.type"} eq 'randomizetry')) &&
308: ($curr_rndseed ne $Apache::lonhomework::history{"$history:resource.$partid.rndseed"}));
1.73 albertel 309: $previous{'award'} = $Apache::lonhomework::history{"$history:resource.$partid.$id.awarddetail"};
310: $previous{'last'}='0';
311: push(@{ $previous{'version'} },$history);
312: } else {
1.221 raeburn 313: next if ((($questiontype eq 'randomizetry') ||
314: ($Apache::lonhomework::history{"resource.$partid.type"} eq 'randomizetry')) &&
315: ($curr_rndseed ne $Apache::lonhomework::history{"resource.$partid.rndseed"}));
1.73 albertel 316: $previous{'award'} = $Apache::lonhomework::history{"resource.$partid.$id.awarddetail"};
317: $previous{'last'}='1';
318: }
1.221 raeburn 319: $previous{'used'} = 1;
1.73 albertel 320: if (! $previous{'award'} ) { $previous{'award'} = 'UNKNOWN'; }
1.209 www 321: if ($previous{'award'} eq 'INTERNAL_ERROR') { $previous{'used'}=0; }
1.73 albertel 322: &Apache::lonxml::debug("got a match :$previous{'award'}:$previous{'used'}:");
1.219 raeburn 323: } elsif ($type eq 'ci') {
324: if (lc($pastresponse) eq lc($curresponse)) {
325: if ($key =~ /^(\d+):/) {
1.221 raeburn 326: my $history = $1;
327: next if (($questiontype eq 'randomizetry') &&
328: ($curr_rndseed ne $Apache::lonhomework::history{"$history:resource.$partid.rndseed"}));
329: push (@{$previous{'versionci'}},$history);
1.225 raeburn 330: $previous{'awardci'} = $Apache::lonhomework::history{"$history:resource.$partid.$id.awarddetail"};
1.219 raeburn 331: $previous{'usedci'} = 1;
332: }
333: }
334: }
1.32 albertel 335: }
1.73 albertel 336: }
337: &Apache::lonhomework::showhash(%previous);
338: return %previous;
1.54 albertel 339: }
340:
341: sub handle_previous {
1.73 albertel 342: my ($previous,$ad)=@_;
343: if ($$previous{'used'} && ($$previous{'award'} eq $ad) ) {
344: if ($$previous{'last'}) {
345: push(@Apache::inputtags::previous,'PREVIOUSLY_LAST');
1.107 albertel 346: push(@Apache::inputtags::previous_version,$$previous{'version'});
1.217 raeburn 347: } elsif (($Apache::lonhomework::type ne 'survey') &&
348: ($Apache::lonhomework::type ne 'surveycred') &&
349: ($Apache::lonhomework::type ne 'anonsurvey') &&
1.221 raeburn 350: ($Apache::lonhomework::type ne 'anonsurveycred')) {
1.73 albertel 351: push(@Apache::inputtags::previous,'PREVIOUSLY_USED');
1.107 albertel 352: push(@Apache::inputtags::previous_version,$$previous{'version'});
1.73 albertel 353: }
1.54 albertel 354: }
1.44 albertel 355: }
356:
1.45 albertel 357: sub view_or_modify {
1.149 albertel 358: my ($symb,$courseid,$domain,$name) = &Apache::lonnet::whichuser();
1.73 albertel 359: my $myself=0;
1.120 albertel 360: if ( ($name eq $env{'user.name'}) && ($domain eq $env{'user.domain'}) ) {
1.73 albertel 361: $myself=1;
362: }
363: my $vgr=&Apache::lonnet::allowed('vgr',$courseid);
364: my $mgr=&Apache::lonnet::allowed('vgr',$courseid);
365: if ($mgr) { return "M"; }
366: if ($vgr) { return "V"; }
367: if ($myself) { return "V"; }
368: return '';
1.45 albertel 369: }
370:
1.44 albertel 371: sub start_dataresponse {
1.73 albertel 372: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
373: my $id = &Apache::response::start_response($parstack,$safeeval);
374: my $result;
375: if ($target eq 'web') {
376: $result = $token->[2]->{'display'}.':';
377: } elsif ($target eq 'meta') {
378: $result = &Apache::response::meta_stores_write($token->[2]->{'name'},
379: $token->[2]->{'type'},
380: $token->[2]->{'display'});
381: $result .= &Apache::response::meta_package_write('dataresponse');
382: }
383: return $result;
1.44 albertel 384: }
385:
386: sub end_dataresponse {
1.73 albertel 387: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
388: my $result;
389: if ( $target eq 'web' ) {
390: } elsif ($target eq 'grade' ) {
1.120 albertel 391: if ( defined $env{'form.submitted'}) {
1.149 albertel 392: my ($symb,$courseid,$domain,$name)=&Apache::lonnet::whichuser();
1.73 albertel 393: my $allowed=&Apache::lonnet::allowed('mgr',$courseid);
394: if ($allowed) {
1.94 albertel 395: &Apache::response::setup_params('dataresponse',$safeeval);
1.73 albertel 396: my $partid = $Apache::inputtags::part;
397: my $id = $Apache::inputtags::response['-1'];
1.120 albertel 398: my $response = $env{'form.HWVAL_'.$id};
1.73 albertel 399: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
400: if ( $response =~ /[^\s]/) {
401: $Apache::lonhomework::results{"resource.$partid.$id.$name"}=$response;
402: $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
403: $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}='SUBMITTED';
404: }
405: } else {
1.235 bisitz 406: $result=&mt('Not Permitted to change values');
1.73 albertel 407: }
1.45 albertel 408: }
1.73 albertel 409: }
410: &Apache::response::end_response;
411: return $result;
1.3 albertel 412: }
413:
1.129 albertel 414: sub start_customresponse {
1.128 albertel 415: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
416: my $id = &Apache::response::start_response($parstack,$safeeval);
1.129 albertel 417: push(@Apache::lonxml::namespace,'customresponse');
1.128 albertel 418: my $result;
1.157 www 419: @Apache::response::custom_answer=();
420: @Apache::response::custom_answer_type=();
1.128 albertel 421: &Apache::lonxml::register('Apache::response',('answer'));
422: if ($target eq 'web') {
423: if ( &Apache::response::show_answer() ) {
424: my $answer = &Apache::lonxml::get_param('answerdisplay',$parstack,
425: $safeeval);
1.152 albertel 426: $Apache::inputtags::answertxt{$id}=[$answer];
1.128 albertel 427: }
428: } elsif ($target eq 'edit') {
429: $result.=&Apache::edit::tag_start($target,$token);
430: $result.=&Apache::edit::text_arg('String to display for answer:',
1.220 www 431: 'answerdisplay',$token,'50');
1.128 albertel 432: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
433: } elsif ($target eq 'modified') {
434: my $constructtag;
435: $constructtag=&Apache::edit::get_new_args($token,$parstack,
436: $safeeval,'answerdisplay');
437: if ($constructtag) {
438: $result = &Apache::edit::rebuild_tag($token);
439: }
440: } elsif ($target eq 'answer' || $target eq 'grade') {
441: &Apache::response::reset_params();
442: } elsif ($target eq 'meta') {
1.129 albertel 443: $result .= &Apache::response::meta_package_write('customresponse');
1.128 albertel 444: }
445: return $result;
446: }
447:
1.129 albertel 448: sub end_customresponse {
1.128 albertel 449: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
450: my $result;
451: my $part=$Apache::inputtags::part;
452: my $id=$Apache::inputtags::response[-1];
453: if ( $target eq 'grade' && &Apache::response::submitted() ) {
454: my $response = &Apache::response::getresponse();
1.146 albertel 455: if ($Apache::lonhomework::type eq 'exam' ||
456: &Apache::response::submitted('scantron')) {
457: &Apache::response::scored_response($part,$id);
1.251 ! raeburn 458: } elsif ( $response =~ /[^\s]/ &&
1.158 www 459: $Apache::response::custom_answer_type[-1] eq 'loncapa/perl') {
1.128 albertel 460: if (!$Apache::lonxml::default_homework_loaded) {
461: &Apache::lonxml::default_homework_load($safeeval);
462: }
463: my %previous = &Apache::response::check_for_previous($response,
464: $part,$id);
465: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
466: $response;
467: my $error;
1.129 albertel 468: ${$safeeval->varglob('LONCAPA::customresponse_submission')}=
1.128 albertel 469: $response;
1.251 ! raeburn 470:
1.216 www 471: my ($award,$score) = &Apache::run::run('{ my $submission=$LONCAPA::customresponse_submission;'.$Apache::response::custom_answer[-1].'}',$safeeval);
1.128 albertel 472: if (!&Apache::inputtags::valid_award($award)) {
473: $error = $award;
474: $award = 'ERROR';
475: }
1.248 raeburn 476: if (($award eq 'INCORRECT') || ($award eq 'APPROX_ANS') ||
477: ($award eq 'EXACT_ANS') || ($award eq 'ASSIGNED_SCORE')) {
1.233 raeburn 478: if ($Apache::lonhomework::type eq 'survey') {
479: $award='SUBMITTED';
480: } elsif ($Apache::lonhomework::type eq 'surveycred') {
481: $award='SUBMITTED_CREDIT';
482: } elsif ($Apache::lonhomework::type eq 'anonsurvey') {
483: $award='ANONYMOUS';
484: } elsif ($Apache::lonhomework::type eq 'anonsurveycred') {
485: $award='ANONYMOUS_CREDIT';
486: }
487: }
1.128 albertel 488: &Apache::response::handle_previous(\%previous,$award);
489: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
490: $award;
1.216 www 491: if ($award eq 'ASSIGNED_SCORE') {
492: $Apache::lonhomework::results{"resource.$part.$id.awarded"}=1.0*$score;
493: }
1.128 albertel 494: if ($error) {
495: $Apache::lonhomework::results{"resource.$part.$id.awardmsg"}=
496: $error;
497: }
498: }
1.146 albertel 499: } elsif ( $target eq 'answer') {
500: $result = &Apache::response::answer_header('customresponse');
501: my $answer = &Apache::lonxml::get_param('answerdisplay',$parstack,
502: $safeeval);
503: if ($env{'form.answer_output_mode'} ne 'tex') {
504: $answer = '<b>'.$answer.'</b>';
505: }
506: $result .= &Apache::response::answer_part('customresponse',$answer);
507: $result .= &Apache::response::answer_footer('customresponse');
508: }
1.163 albertel 509: if ($target eq 'web') {
1.229 raeburn 510: &setup_prior_tries_hash(\&format_prior_response_custom);
1.163 albertel 511: }
1.251 ! raeburn 512: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
1.146 albertel 513: $target eq 'tex' || $target eq 'analyze') {
1.227 raeburn 514: my $repetition = &repetition();
515: &Apache::lonxml::increment_counter($repetition,"$part.$id");
1.179 foxr 516: if ($target eq 'analyze') {
1.187 raeburn 517: $Apache::lonhomework::analyze{"$part.$id.type"} = 'customresponse';
1.179 foxr 518: &Apache::lonhomework::set_bubble_lines();
519: }
1.128 albertel 520: }
1.244 raeburn 521: if ($target eq 'web') {
522: &setup_prior_tries_hash(\&format_prior_response_math);
523: }
1.128 albertel 524: pop(@Apache::lonxml::namespace);
1.157 www 525: pop(@Apache::response::custom_answer);
526: pop(@Apache::response::custom_answer_type);
1.128 albertel 527: &Apache::lonxml::deregister('Apache::response',('answer'));
528: &Apache::response::end_response();
529: return $result;
530: }
531:
1.163 albertel 532: sub format_prior_response_custom {
533: my ($mode,$answer) =@_;
1.229 raeburn 534: if (ref($answer) eq 'ARRAY') {
535: $answer = '('.join(', ', @{ $answer }).')';
536: }
1.163 albertel 537: return '<span class="LC_prior_custom">'.
538: &HTML::Entities::encode($answer,'"<>&').'</span>';
539: }
1.140 www 540:
1.139 www 541: sub start_mathresponse {
1.140 www 542: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
543: my $id = &Apache::response::start_response($parstack,$safeeval);
544: push(@Apache::lonxml::namespace,'mathresponse');
1.139 www 545: my $result;
1.157 www 546: @Apache::response::custom_answer=();
547: @Apache::response::custom_answer_type=();
1.140 www 548: &Apache::lonxml::register('Apache::response',('answer'));
549: if ($target eq 'web') {
550: if ( &Apache::response::show_answer() ) {
551: my $answer = &Apache::lonxml::get_param('answerdisplay',$parstack,
552: $safeeval);
1.152 albertel 553: $Apache::inputtags::answertxt{$id}=[$answer];
1.140 www 554: }
1.191 www 555:
1.140 www 556: } elsif ($target eq 'edit') {
557: $result.=&Apache::edit::tag_start($target,$token);
558: $result.=&Apache::edit::text_arg('String to display for answer:',
1.220 www 559: 'answerdisplay',$token,'50');
1.150 www 560: $result.=&Apache::edit::select_arg('Algebra System:',
561: 'cas',
1.214 www 562: ['maxima','R'],
1.150 www 563: $token);
564: $result.=&Apache::edit::text_arg('Argument Array:',
1.193 www 565: 'args',$token).
566: &Apache::loncommon::help_open_topic('Maxima_Argument_Array');
1.192 www 567: $result.=&Apache::edit::text_arg('Libraries:',
1.193 www 568: 'libraries',$token).
569: &Apache::loncommon::help_open_topic('Maxima_Libraries');
1.140 www 570: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
571: } elsif ($target eq 'modified') {
572: my $constructtag;
573: $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.192 www 574: $safeeval,'answerdisplay','cas','args','libraries');
1.140 www 575: if ($constructtag) {
576: $result = &Apache::edit::rebuild_tag($token);
577: }
578: } elsif ($target eq 'answer' || $target eq 'grade') {
579: &Apache::response::reset_params();
580: } elsif ($target eq 'meta') {
581: $result .= &Apache::response::meta_package_write('mathresponse');
1.139 www 582: }
583: return $result;
584: }
585:
586: sub end_mathresponse {
1.140 www 587: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
588: my $result;
589: my $part=$Apache::inputtags::part;
590: my $id=$Apache::inputtags::response[-1];
591: if ( $target eq 'grade' && &Apache::response::submitted() ) {
592: my $response = &Apache::response::getresponse();
1.150 www 593: if ( $response =~ /[^\s]/ ) {
1.140 www 594: if (!$Apache::lonxml::default_homework_loaded) {
595: &Apache::lonxml::default_homework_load($safeeval);
596: }
597: my %previous = &Apache::response::check_for_previous($response,
598: $part,$id);
599: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
600: $response;
601: my $error;
1.155 www 602: my $award;
603: my $cas = &Apache::lonxml::get_param('cas',$parstack,$safeeval);
604: if ($cas eq 'maxima') {
605: my $args = [&Apache::lonxml::get_param_var('args',$parstack,$safeeval)];
1.192 www 606: $award=&Apache::lonmaxima::maxima_run($Apache::response::custom_answer[-1],$response,$args,
607: &Apache::lonxml::get_param('libraries',$parstack,$safeeval));
1.155 www 608: }
1.214 www 609: if ($cas eq 'R') {
610: my $args = [&Apache::lonxml::get_param_var('args',$parstack,$safeeval)];
611: $award=&Apache::lonr::r_run($Apache::response::custom_answer[-1],$response,$args,
612: &Apache::lonxml::get_param('libraries',$parstack,$safeeval));
613: }
614:
1.140 www 615: if (!&Apache::inputtags::valid_award($award)) {
1.151 albertel 616: $error = $award;
617: $award = 'ERROR';
1.140 www 618: }
1.233 raeburn 619: if (($award eq 'INCORRECT' || $award eq 'APPROX_ANS' ||
620: $award eq 'EXACT_ANS')) {
621: if ($Apache::lonhomework::type eq 'survey') {
622: $award='SUBMITTED';
623: } elsif ($Apache::lonhomework::type eq 'surveycred') {
624: $award='SUBMITTED_CREDIT';
625: } elsif ($Apache::lonhomework::type eq 'anonsurvey') {
626: $award='ANONYMOUS';
627: } elsif ($Apache::lonhomework::type eq 'anonsurveycred') {
628: $award='ANONYMOUS_CREDIT';
629: }
630: }
1.140 www 631: &Apache::response::handle_previous(\%previous,$award);
632: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
633: $award;
634: if ($error) {
635: $Apache::lonhomework::results{"resource.$part.$id.awardmsg"}=
636: $error;
637: }
638: }
639: }
1.163 albertel 640:
1.140 www 641: pop(@Apache::lonxml::namespace);
1.157 www 642: pop(@Apache::response::custom_answer);
643: pop(@Apache::response::custom_answer_type);
1.140 www 644: &Apache::lonxml::deregister('Apache::response',('answer'));
645: &Apache::response::end_response();
646: return $result;
1.139 www 647: }
648:
1.163 albertel 649: sub format_prior_response_math {
650: my ($mode,$answer) =@_;
651: return '<span class="LC_prior_math">'.
652: &HTML::Entities::encode($answer,'"<>&').'</span>';
653: }
654:
1.156 www 655: sub implicit_multiplication {
656: my ($expression)=@_;
657: # Escape scientific notation, so 3e8 does not become 3*e*8
658: # 3e8 -> 3&8; 3e-8 -> 3&-8; 3E+8 -> e&+8
659: $expression=~s/(\d+)e([\+\-]*\d+)/$1\&\($2\)/gsi;
660: # 3x10^8 -> 3&8; 3*10^-8 -> 3&-8
661: $expression=~s/(\d+)(?:x|\*)10(?:\^|\*\*)([\+\-]*\d+)/$1\&\($2\)/gsi;
662: # Fill in multiplication signs
663: # a b -> a*b;3 b -> 3*b;3 4 -> 3*4
1.176 albertel 664: $expression=~s/([A-Za-z0-9])\s+(?=[A-Za-z0-9])/$1\*/gs;
1.156 www 665: # )( -> )*(; ) ( -> )*(
666: $expression=~s/\)\s*\(/\)\*\(/gs;
667: # 3a -> 3*a; 3( -> 3*(; 3 ( -> 3*(; 3A -> 3*A
668: $expression=~s/(\d)\s*([a-zA-Z\(])/$1\*$2/gs;
669: # a ( -> a*(
1.174 riegler 670: $expression=~s/([A-Za-z0-9])\s+\(/$1\*\(/gs;
1.156 www 671: # )a -> )*a; )3 -> )*3; ) 3 -> )*3
1.174 riegler 672: $expression=~s/\)\s*([A-Za-z0-9])/\)\*$1/gs;
1.156 www 673: # 3&8 -> 3e8; 3&-4 -> 3e-4
674: $expression=~s/(\d+)\&\(([\+\-]*\d+)\)/$1e$2/gs;
675: return $expression;
676: }
1.140 www 677:
1.128 albertel 678: sub start_answer {
679: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
680: my $result;
1.157 www 681: push(@Apache::response::custom_answer,
682: &Apache::lonxml::get_all_text_unbalanced("/answer",$parser));
683: push(@Apache::response::custom_answer_type,
684: lc(&Apache::lonxml::get_param('type',$parstack,$safeeval)));
685: $Apache::response::custom_answer_type[-1] =~ s/\s+//g;
1.128 albertel 686: if ($target eq "edit" ) {
687: $result=&Apache::edit::tag_start($target,$token,'Answer algorithm');
688: $result.=&Apache::edit::editfield($token->[1],
1.158 www 689: $Apache::response::custom_answer[-1],
1.128 albertel 690: '',80,4);
691: } elsif ( $target eq "modified" ) {
692: $result=$token->[4].&Apache::edit::modifiedfield('/answer',$parser);
693: }
694: return $result;
695: }
696:
697: sub end_answer {
698: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
699: if ($target eq 'edit' ) {
700: return &Apache::edit::end_table();
701: }
702: }
703:
1.83 albertel 704: sub decide_package {
705: my ($tagstack)=@_;
706: my $package;
707: if ($$tagstack[-1] eq 'parameter') {
708: $package='part';
709: } else {
710: my $i=-1;
711: while (defined($$tagstack[$i])) {
712: if ($$tagstack[$i] =~ /(response|hint)$/) {
713: $package=$$tagstack[$i];
714: last;
715: }
716: $i--;
717: }
718: }
719: return $package;
720: }
721:
1.3 albertel 722: sub start_responseparam {
1.73 albertel 723: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
724: my $result='';
725: if ($target eq 'meta') {
726: $result = &meta_parameter_write($token->[2]->{'name'},
727: $token->[2]->{'type'},
728: $token->[2]->{'default'},
729: $token->[2]->{'description'});
730: } elsif ($target eq 'edit') {
731: $result.=&Apache::edit::tag_start($target,$token);
1.83 albertel 732: my $optionlist;
733: my $package=&decide_package($tagstack);
734: foreach my $key (sort(keys(%Apache::lonnet::packagetab))) {
735: if ($key =~ /^\Q$package\E&(.*)&display$/) {
736: $optionlist.='<option value="'.$1.'">'.
737: $Apache::lonnet::packagetab{$key}.'</option>';
738: }
739: }
740: if (defined($optionlist)) {
1.206 bisitz 741: $result.=&mt('Use template:').' <select name="'.
1.83 albertel 742: &Apache::edit::html_element_name('parameter_package').'">'.
743: '<option value=""></option>'.$optionlist.'</select><br />';
744: }
1.73 albertel 745: $result.=&Apache::edit::text_arg('Name:','name',$token).
746: &Apache::edit::text_arg('Type:','type',$token).
747: &Apache::edit::text_arg('Description:','description',$token).
748: &Apache::edit::text_arg('Default:','default',$token).
749: "</td></tr>";
750: $result.=&Apache::edit::end_table;
751: } elsif ($target eq 'modified') {
1.83 albertel 752: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
753: $safeeval,'name','type',
754: 'description','default');
755: my $element=&Apache::edit::html_element_name('parameter_package');
1.120 albertel 756: if (defined($env{"form.$element"}) && $env{"form.$element"} ne '') {
757: my $name=$env{"form.$element"};
1.83 albertel 758: my $tag=&decide_package($tagstack);
759: $token->[2]->{'name'}=$name;
760: $token->[2]->{'type'}=
761: $Apache::lonnet::packagetab{"$tag&$name&type"};
762: $token->[2]->{'description'}=
763: $Apache::lonnet::packagetab{"$tag&$name&display"};
764: $token->[2]->{'default'}=
765: $Apache::lonnet::packagetab{"$tag&$name&default"};
1.186 raeburn 766: $token->[3] = ['name','type','description','default'];
1.83 albertel 767: $constructtag=1;
768: }
1.73 albertel 769: if ($constructtag) {
770: $result = &Apache::edit::rebuild_tag($token);
771: }
772: } elsif ($target eq 'grade' || $target eq 'answer' || $target eq 'web' ||
773: $target eq 'tex' || $target eq 'analyze' ) {
1.249 raeburn 774: if (($env{'request.state'} eq 'construct') ||
775: ($env{'request.noversionuri'} =~ m{^\Q/res/adm/includes/templates/\E[^/]+\.problem$})) {
1.73 albertel 776: my $name =&Apache::lonxml::get_param('name',$parstack,$safeeval);
777: my $default=&Apache::lonxml::get_param('default',$parstack,
778: $safeeval);
779: if ($name) {$Apache::inputtags::params{$name}=$default;}
780: }
1.52 albertel 781: }
1.73 albertel 782: return $result;
1.3 albertel 783: }
784:
785: sub end_responseparam {
1.73 albertel 786: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
787: if ($target eq 'edit') { return ('','no'); }
788: return '';
1.55 albertel 789: }
790:
791: sub start_parameter {
1.114 albertel 792: return &start_responseparam(@_);
1.55 albertel 793: }
794:
795: sub end_parameter {
1.114 albertel 796: return &end_responseparam(@_);
1.42 albertel 797: }
798:
1.67 albertel 799: sub reset_params {
800: %Apache::inputtags::params=();
801: }
802:
1.42 albertel 803: sub setup_params {
1.94 albertel 804: my ($tag,$safeeval) = @_;
1.42 albertel 805:
1.249 raeburn 806: if (($env{'request.state'} eq 'construct') ||
807: ($env{'request.noversionuri'} =~ m{^\Q/res/adm/includes/templates/\E[^/]+\.problem$})) {
808: return;
809: }
1.73 albertel 810: my %paramlist=();
811: foreach my $key (keys(%Apache::lonnet::packagetab)) {
1.161 albertel 812: if ($key =~ /^\Q$tag\E/) {
1.73 albertel 813: my ($package,$name) = split(/&/,$key);
814: $paramlist{$name}=1;
815: }
1.42 albertel 816: }
1.73 albertel 817: foreach my $key (keys(%paramlist)) {
818: my $entry= 'resource.'.$Apache::inputtags::part;
819: if (defined($Apache::inputtags::response[-1])) {
820: $entry.='_'.$Apache::inputtags::response[-1];
821: }
822: $entry.='.'.$key;
823: &Apache::lonxml::debug("looking for $entry");
824: my $value = &Apache::lonnet::EXT("$entry");
825: &Apache::lonxml::debug("$key has value :$value:");
826: if ($value eq 'con_lost' || $value =~ /^error:/) {
827: &Apache::lonxml::debug("using nothing");
828: $Apache::inputtags::params{$key}='';
829: } else {
1.94 albertel 830: my $string="{return qq\0".$value."\0}";
831: my $newvalue=&Apache::run::run($string,$safeeval,1);
832: if (defined($newvalue)) { $value=$newvalue; }
1.73 albertel 833: $Apache::inputtags::params{$key}=$value;
834: }
1.42 albertel 835: }
1.48 albertel 836: }
837:
1.132 albertel 838: {
839: my @answer_bits;
1.147 albertel 840: my $need_row_start;
1.132 albertel 841:
1.48 albertel 842: sub answer_header {
1.147 albertel 843: my ($type,$increment,$rows) = @_;
1.73 albertel 844: my $result;
1.120 albertel 845: if ($env{'form.answer_output_mode'} eq 'tex') {
1.132 albertel 846: undef(@answer_bits);
1.133 albertel 847: my $bit;
848: if ($Apache::lonhomework::type eq 'exam') {
849: $bit = ($Apache::lonxml::counter+$increment).') ';
850: } else {
1.235 bisitz 851: $bit .= ' '.&mt('Answer for Part: [_1]',
852: '\verb|'.$Apache::inputtags::part.'|').' ';
1.133 albertel 853: }
854: push(@answer_bits,$bit);
1.73 albertel 855: } else {
1.147 albertel 856: my $td = '<td '.(defined($rows)?'rowspan="'.$rows.'"':'').'>';
1.132 albertel 857: $result = '<table border="1"><tr>';
858: if ($Apache::lonhomework::type eq 'exam') {
1.147 albertel 859: $result .= $td.($Apache::lonxml::counter+$increment). ')</td>';
1.132 albertel 860: } else {
1.147 albertel 861: $result .= $td.&mt('Answer for Part: [_1]',
862: $Apache::inputtags::part).'</td>';
1.132 albertel 863: }
864: $result .= "\n";
1.147 albertel 865: $need_row_start = 0;
866: }
867: return $result;
868: }
869:
870: sub next_answer {
871: my ($type) = @_;
872: my $result;
873: if ($env{'form.answer_output_mode'} eq 'tex') {
874: # FIXME ... need to do something with tex mode
875: } else {
876: $result .= "</tr>";
877: $need_row_start = 1;
1.73 albertel 878: }
879: return $result;
1.48 albertel 880: }
881:
882: sub answer_part {
1.148 albertel 883: my ($type,$answer,$args) = @_;
1.73 albertel 884: my $result;
1.120 albertel 885: if ($env{'form.answer_output_mode'} eq 'tex') {
1.148 albertel 886: if (!$args->{'no_verbatim'}) {
887: my $to_use='|';
1.250 raeburn 888: foreach my $value (33..41,43..126) {
1.148 albertel 889: my $char=pack('c',$value);
890: if ($answer !~ /\Q$char\E/) {
891: $to_use=$char;
892: last;
893: }
894: }
1.189 www 895: my $fullanswer=$answer;
1.188 www 896: $answer='';
1.189 www 897: foreach my $element (split(/[\;]/,$fullanswer)) {
898: if ($element ne '') {
899: $answer.= '\verb'.$to_use.$element.$to_use.' \newline';
900: }
1.188 www 901: }
1.124 albertel 902: }
1.132 albertel 903: if ($answer ne '') {
1.148 albertel 904: push(@answer_bits,$answer);
1.132 albertel 905: }
1.73 albertel 906: } else {
1.147 albertel 907: if ($need_row_start) {
908: $result .= '<tr>';
909: $need_row_start = 0;
910: }
1.189 www 911: $result .= '<td>'.$answer.'</td>';
1.73 albertel 912: }
913: return $result;
1.48 albertel 914: }
915:
916: sub answer_footer {
1.73 albertel 917: my ($type) = @_;
918: my $result;
1.120 albertel 919: if ($env{'form.answer_output_mode'} eq 'tex') {
1.189 www 920: $result = ' \vskip 0 mm \noindent \begin{tabular}{|p{1.5cm}|p{6.8cm}|}\hline ';
921: $result .= $answer_bits[0].'&\vspace*{-4mm}\begin{itemize}';
922: for (my $i=1;$i<=$#answer_bits;$i++) {
923: $result.='\item '.$answer_bits[$i].'\vspace*{-7mm}';
924: }
925: $result .= ' \end{itemize} \\\\ \hline \end{tabular} \vskip 0 mm ';
1.73 albertel 926: } else {
1.185 albertel 927: if (!$need_row_start) {
928: $result .= '</tr>';
929: }
930: $result .= '</table>';
1.73 albertel 931: }
932: return $result;
1.1 albertel 933: }
1.2 albertel 934:
1.132 albertel 935: }
936:
1.62 albertel 937: sub showallfoils {
1.120 albertel 938: if (defined($env{'form.showallfoils'})) {
1.149 albertel 939: my ($symb)=&Apache::lonnet::whichuser();
1.251 ! raeburn 940: if (($env{'request.state'} eq 'construct') ||
1.120 albertel 941: ($env{'user.adv'} && $symb eq '') ||
1.118 foxr 942: ($Apache::lonhomework::viewgrades) ) {
1.102 albertel 943: return 1;
944: }
1.73 albertel 945: }
1.108 albertel 946: if ($Apache::lonhomework::type eq 'survey') { return 1; }
1.217 raeburn 947: if ($Apache::lonhomework::type eq 'surveycred') { return 1; }
948: if ($Apache::lonhomework::type eq 'anonsurvey') { return 1; }
949: if ($Apache::lonhomework::type eq 'anonsurveycred') { return 1; }
950:
1.102 albertel 951: return 0;
1.70 albertel 952: }
953:
1.168 albertel 954: =pod
955:
1.210 raeburn 956: =item &getresponse();
1.168 albertel 957:
958: Retreives the current submitted response, helps out in the case of
959: scantron mode.
960:
961: Returns either the exact text of the submission, or a bubbled response
962: converted to something usable.
963:
964: Optional Arguments:
1.171 albertel 965: $offset - (defaults to 1) if a problem has more than one bubble
966: response, pass in the number of the bubble wanted, (the
967: first bubble associated with a problem has an offset of 1,
968: the second bubble is 2
969:
1.168 albertel 970: $resulttype - undef -> a number between 0 and 25
971: 'A is 1' -> a number between 1 and 26
972: 'letter' -> a letter between 'A' and 'Z'
1.172 foxr 973: $lines - undef problem only needs a single line of bubbles.
1.251 ! raeburn 974: nonzero Problem wants the first nonempty response in
1.172 foxr 975: $lines lines of bubbles.
976: $bubbles_per_line - Must be provided if lines is defined.. number of
977: bubbles on a line.
1.173 albertel 978:
1.168 albertel 979: =cut
980:
1.70 albertel 981: sub getresponse {
1.172 foxr 982: my ($offset,$resulttype, $lines, $bubbles_per_line)=@_;
1.70 albertel 983: my $formparm='form.HWVAL_'.$Apache::inputtags::response['-1'];
984: my $response;
1.168 albertel 985: if (!defined($offset)) {
1.170 albertel 986: $offset=1;
1.70 albertel 987: } else {
1.168 albertel 988: $formparm.=":$offset";
1.70 albertel 989: }
1.172 foxr 990: if (!defined($lines)) {
991: $lines = 1;
992: }
1.70 albertel 993: my %let_to_num=('A'=>0,'B'=>1,'C'=>2,'D'=>3,'E'=>4,'F'=>5,'G'=>6,'H'=>7,
994: 'I'=>8,'J'=>9,'K'=>10,'L'=>11,'M'=>12,'N'=>13,'O'=>14,
995: 'P'=>15,'Q'=>16,'R'=>17,'S'=>18,'T'=>19,'U'=>20,'V'=>21,
996: 'W'=>22,'X'=>23,'Y'=>24,'Z'=>25);
1.120 albertel 997: if ($env{'form.submitted'} eq 'scantron') {
1.71 albertel 998: my $part = $Apache::inputtags::part;
999: my $id = $Apache::inputtags::response[-1];
1.172 foxr 1000:
1001: my $line;
1.211 raeburn 1002: my $startline = $env{'form.scantron_questnum_start.'.$part.'.'.$id};
1003: if (!$startline) {
1004: $startline = $Apache::lonxml::counter;
1005: }
1.172 foxr 1006: for ($line = 0; $line < $lines; $line++) {
1.211 raeburn 1007: my $theline = $startline+$offset-1+$line;
1.184 foxr 1008: $response = $env{"scantron.$theline.answer"};
1009: if ((defined($response)) && ($response ne "") && ($response ne " ")) {
1.172 foxr 1010: last;
1011: }
1012: }
1.178 albertel 1013:
1014: # save bubbled letter for later
1015: $Apache::lonhomework::results{"resource.$part.$id.scantron"}.=
1016: $response;
1.90 albertel 1017: if ($resulttype ne 'letter') {
1.226 raeburn 1018: $response = $let_to_num{$response};
1019: if ($resulttype eq 'A is 1') {
1020: if ($response ne "") {
1021: $response = $response+1;
1022: }
1.104 albertel 1023: }
1.172 foxr 1024: if ($response ne "") {
1025: $response += $line * $bubbles_per_line;
1026: }
1027: } else {
1028: if ($response ne "") {
1.226 raeburn 1029: my $raw = $response;
1.172 foxr 1030: $response = chr(ord($response) + $line * $bubbles_per_line);
1031: }
1.90 albertel 1032: }
1.172 foxr 1033:
1.70 albertel 1034: } else {
1.120 albertel 1035: $response = $env{$formparm};
1.70 albertel 1036: }
1.251 ! raeburn 1037: #
1.172 foxr 1038: # If we have a nonempty answer, correct the numeric value
1039: # of the answer for the line on which it was found.
1040: #
1041:
1.70 albertel 1042: return $response;
1.62 albertel 1043: }
1.71 albertel 1044:
1.168 albertel 1045: =pod
1046:
1047: =item &repetition();
1048:
1.227 raeburn 1049: In scalar context:
1050:
1051: returns: the number of lines that are required to encode the weight.
1.251 ! raeburn 1052: (Default is for 10 bubbles per bubblesheet item; other (integer)
! 1053: values can be specified by using a custom Bubblesheet format file
! 1054: with an eighteenth entry (BubblesPerRow) set to the integer
1.226 raeburn 1055: appropriate for the bubblesheets which will be used to assign weights.
1.168 albertel 1056:
1.227 raeburn 1057: In array context:
1.251 ! raeburn 1058:
1.227 raeburn 1059: returns: number of lines required to encode weight, and bubbles/line.
1060:
1.168 albertel 1061: =cut
1062:
1.71 albertel 1063: sub repetition {
1064: my $id = $Apache::inputtags::part;
1065: my $weight = &Apache::lonnet::EXT("resource.$id.weight");
1.121 albertel 1066: if (!defined($weight) || ($weight eq '')) { $weight=1; }
1.226 raeburn 1067: my $bubbles_per_row;
1.251 ! raeburn 1068: if (($env{'form.bubbles_per_row'} =~ /^\d+$/) &&
1.226 raeburn 1069: ($env{'form.bubbles_per_row'} > 0)) {
1070: $bubbles_per_row = $env{'form.bubbles_per_row'};
1071: } else {
1072: $bubbles_per_row = 10;
1073: }
1.227 raeburn 1074: my $denominator = $bubbles_per_row;
1.251 ! raeburn 1075: if (($env{'form.scantron_lastbubblepoints'} == 0) &&
1.227 raeburn 1076: ($bubbles_per_row > 1)) {
1077: $denominator = $bubbles_per_row - 1;
1.251 ! raeburn 1078: }
1.227 raeburn 1079: my $repetition = int($weight/$denominator);
1.251 ! raeburn 1080: if ($weight % $denominator != 0) { $repetition++; }
1.227 raeburn 1081: if (wantarray) {
1082: return ($repetition,$bubbles_per_row);
1083: }
1.72 albertel 1084: return $repetition;
1.227 raeburn 1085:
1.72 albertel 1086: }
1087:
1.168 albertel 1088: =pod
1089:
1.210 raeburn 1090: =item &scored_response();
1.168 albertel 1091:
1092: Sets the results hash elements
1093:
1094: resource.$part_id.$response_id.awarded - to the floating point
1095: number between 0 and 1 that was awarded on the bubbled input
1096:
1097: resource.$part_id.$response_id.awarddetail - to 'ASSIGNED_SCORE'
1098:
1099: Returns
1100:
1101: the number of bubble sheet lines that were used (and likely need to
1102: be passed to &Apache::lonxml::increment_counter()
1103:
1104: Arguments
1105:
1106: $part_id - id of the part to grade
1107: $response_id - id of the response to grade
1.251 ! raeburn 1108:
1.168 albertel 1109:
1110: =cut
1111:
1.72 albertel 1112: sub scored_response {
1113: my ($part,$id)=@_;
1114: my $repetition=&repetition();
1.227 raeburn 1115: my $bubbles_per_row;
1116: if (($env{'form.bubbles_per_row'} =~ /^\d+$/) &&
1117: ($env{'form.bubbles_per_row'} > 0)) {
1118: $bubbles_per_row = $env{'form.bubbles_per_row'};
1119: } else {
1120: $bubbles_per_row = 10;
1121: }
1.72 albertel 1122: my $score=0;
1123: for (my $i=0;$i<$repetition;$i++) {
1.227 raeburn 1124: # A is 1, B is 2, etc.
1.72 albertel 1125: my $increase=&Apache::response::getresponse($i+1);
1.227 raeburn 1126: unless (($increase == $bubbles_per_row-1) &&
1127: ($env{'form.scantron_lastbubblepoints'} == 0)) {
1128: # (get response return 0-9 and then we add 1)
1129: if ($increase ne '') {
1130: $score+=$increase+1;
1131: }
1132: }
1.72 albertel 1133: }
1134: my $weight = &Apache::lonnet::EXT("resource.$part.weight");
1.91 albertel 1135: if (!defined($weight) || $weight eq '' || $weight eq 0) { $weight = 1; }
1.72 albertel 1136: my $pcr=$score/$weight;
1137: $Apache::lonhomework::results{"resource.$part.$id.awarded"}=$pcr;
1138: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
1139: 'ASSIGNED_SCORE';
1.71 albertel 1140: return $repetition;
1.78 albertel 1141: }
1142:
1143: sub whichorder {
1.221 raeburn 1144: my ($max,$randomize,$showall,$hash,$rndseed)=@_;
1.78 albertel 1145: #&Apache::lonxml::debug("man $max randomize $randomize");
1.231 raeburn 1146: my @names;
1147: if (ref($hash->{'names'}) eq 'ARRAY') {
1148: @names = @{$hash->{'names'}};
1149: }
1150: return if (!@names);
1.78 albertel 1151: my @whichopt =();
1152: my (%top,@toplist,%bottom,@bottomlist);
1153: if (!($showall || ($randomize eq 'no'))) {
1154: my $current=0;
1155: foreach my $name (@names) {
1156: $current++;
1157: if ($$hash{"$name.location"} eq 'top') {
1158: $top{$name}=$current;
1159: } elsif ($$hash{"$name.location"} eq 'bottom') {
1160: $bottom{$name}=$current;
1161: }
1162: }
1163: }
1164: my $topcount=0;
1165: my $bottomcount=0;
1166: while (((scalar(@whichopt)+$topcount+$bottomcount) < $max || $showall)
1167: && ($#names > -1)) {
1168: #&Apache::lonxml::debug("Have $#whichopt max is $max");
1169: my $aopt;
1170: if ($showall || ($randomize eq 'no')) {
1171: $aopt=0;
1172: } else {
1173: $aopt=int(&Math::Random::random_uniform() * ($#names+1));
1174: }
1175: #&Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
1176: $aopt=splice(@names,$aopt,1);
1177: #&Apache::lonxml::debug("Picked $aopt");
1178: if ($top{$aopt}) {
1179: $toplist[$top{$aopt}]=$aopt;
1180: $topcount++;
1181: } elsif ($bottom{$aopt}) {
1182: $bottomlist[$bottom{$aopt}]=$aopt;
1183: $bottomcount++;
1184: } else {
1185: push (@whichopt,$aopt);
1186: }
1187: }
1188: for (my $i=0;$i<=$#toplist;$i++) {
1189: if ($toplist[$i]) { unshift(@whichopt,$toplist[$i]) }
1190: }
1191: for (my $i=0;$i<=$#bottomlist;$i++) {
1192: if ($bottomlist[$i]) { push(@whichopt,$bottomlist[$i]) }
1193: }
1194: return @whichopt;
1.71 albertel 1195: }
1196:
1.85 albertel 1197: sub show_answer {
1198: my $part = $Apache::inputtags::part;
1.228 raeburn 1199: my $award = $Apache::lonhomework::history{"resource.$part.solved"};
1.85 albertel 1200: my $status = $Apache::inputtags::status[-1];
1.228 raeburn 1201: my $canshow = 0;
1202: if ($award =~ /^correct/) {
1.230 raeburn 1203: if (($Apache::lonhomework::history{"resource.$part.awarded"} >= 1) ||
1204: (&Apache::lonnet::EXT("resource.$part.retrypartial") !~/^1|on|yes$/)) {
1.228 raeburn 1205: $canshow = 1;
1.251 ! raeburn 1206: }
1.228 raeburn 1207: }
1.251 ! raeburn 1208: return (($canshow && &Apache::lonhomework::show_problem_status())
1.228 raeburn 1209: || $status eq "SHOW_ANSWER");
1.85 albertel 1210: }
1.87 albertel 1211:
1212: sub analyze_store_foilgroup {
1213: my ($shown,$attrs)=@_;
1214: my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
1215: foreach my $name (@{ $Apache::response::foilgroup{'names'} }) {
1216: if (defined($Apache::lonhomework::analyze{"$part_id.foil.value.$name"})) { next; }
1217: push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },$name);
1218: foreach my $attr (@$attrs) {
1219: $Apache::lonhomework::analyze{"$part_id.foil.".$attr.".$name"} =
1220: $Apache::response::foilgroup{"$name.".$attr};
1221: }
1222: }
1223: push (@{ $Apache::lonhomework::analyze{"$part_id.shown"} }, @{ $shown });
1.96 albertel 1224: }
1225:
1226: sub check_if_computed {
1227: my ($token,$parstack,$safeeval,$name)=@_;
1228: my $value = &Apache::lonxml::get_param($name,$parstack,$safeeval);
1.106 matthew 1229: if (ref($token->[2]) eq 'HASH' && $value ne $token->[2]{$name}) {
1.96 albertel 1230: my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
1231: $Apache::lonhomework::analyze{"$part_id.answercomputed"} = 1;
1232: }
1.87 albertel 1233: }
1234:
1235: sub pick_foil_for_concept {
1236: my ($target,$attrs,$hinthash,$parstack,$safeeval)=@_;
1.231 raeburn 1237: my @names;
1238: if (ref($Apache::response::conceptgroup{'names'}) eq 'ARRAY') {
1239: @names = @{ $Apache::response::conceptgroup{'names'} };
1240: }
1241: return if (!@names);
1.87 albertel 1242: my $pick=int(&Math::Random::random_uniform() * ($#names+1));
1243: my $name=$names[$pick];
1244: push @{ $Apache::response::foilgroup{'names'} }, $name;
1245: foreach my $attr (@$attrs) {
1246: $Apache::response::foilgroup{"$name.".$attr} =
1247: $Apache::response::conceptgroup{"$name.".$attr};
1248: }
1249: my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
1250: $Apache::response::foilgroup{"$name.concept"} = $concept;
1251: &Apache::lonxml::debug("Selecting $name in $concept");
1252: my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
1253: if ($target eq 'analyze') {
1254: push (@{ $Apache::lonhomework::analyze{"$part_id.concepts"} },
1255: $concept);
1256: $Apache::lonhomework::analyze{"$part_id.concept.$concept"}=
1257: $Apache::response::conceptgroup{'names'};
1258: foreach my $name (@{ $Apache::response::conceptgroup{'names'} }) {
1259: push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },
1260: $name);
1261: foreach my $attr (@$attrs) {
1262: $Apache::lonhomework::analyze{"$part_id.foil.$attr.$name"}=
1263: $Apache::response::conceptgroup{"$name.$attr"};
1264: }
1265: }
1266: }
1267: push(@{ $hinthash->{"$part_id.concepts"} },$concept);
1268: $hinthash->{"$part_id.concept.$concept"}=
1269: $Apache::response::conceptgroup{'names'};
1270:
1271: }
1.208 jms 1272:
1273: =pod
1274:
1275: =item get_response_param()
1276:
1277: Get a parameter associated with a problem.
1278: Parameters:
1.251 ! raeburn 1279: $id - the id of the paramater, either a part id,
1.208 jms 1280: or a partid and responspe id joined by _
1281: $name - Name of the parameter to fetch
1282: $default - Default value for the paramter.
1283:
1284: =cut
1285:
1.95 albertel 1286: sub get_response_param {
1287: my ($id,$name,$default)=@_;
1288: my $parameter;
1.120 albertel 1289: if ($env{'request.state'} eq 'construct' &&
1.95 albertel 1290: defined($Apache::inputtags::params{$name})) {
1291: $parameter=$Apache::inputtags::params{$name};
1292: } else {
1293: $parameter=&Apache::lonnet::EXT("resource.$id.$name");
1294: }
1295: if (!defined($parameter) || $parameter eq '') {
1296: $parameter = $default;
1297: }
1298: return $parameter;
1299: }
1.87 albertel 1300:
1.113 albertel 1301: sub submitted {
1302: my ($who)=@_;
1.251 ! raeburn 1303:
1.113 albertel 1304: # when scatron grading any submission is a submission
1.120 albertel 1305: if ($env{'form.submitted'} eq 'scantron') { return 1; }
1.113 albertel 1306: # if the caller only cared if this was a scantron submission
1307: if ($who eq 'scantron') { return 0; }
1308: # if the Submit Answer button for this particular part was pressed
1309: my $partid=$Apache::inputtags::part;
1.159 albertel 1310: if ($env{'form.submitted'} eq "part_$partid") {
1311: return 1;
1312: }
1313: if ($env{'form.submitted'} eq "yes"
1314: && defined($env{'form.submit_'.$partid})) {
1315: return 1;
1316: }
1.122 albertel 1317: # Submit All button on a .page was pressed
1.247 raeburn 1318: if ($env{'form.all_submit'}) { return 1; }
1.204 bisitz 1319: # otherwise no submission occurred
1.113 albertel 1320: return 0;
1321: }
1.117 albertel 1322:
1.130 albertel 1323: sub add_to_gradingqueue {
1.149 albertel 1324: my ($symb,$courseid,$domain,$name) = &Apache::lonnet::whichuser();
1.131 albertel 1325: if ( $courseid eq ''
1326: || $symb eq ''
1.135 albertel 1327: || $env{'request.state'} eq 'construct'
1328: || $Apache::lonhomework::type ne 'problem') {
1.131 albertel 1329: return;
1330: }
1.130 albertel 1331:
1332: my %queue_info = ( 'type' => 'problem',
1333: 'time' => time);
1334:
1335: if (exists($Apache::lonhomework::history{"resource.0.checkedin.slot"})) {
1336: $queue_info{'slot'}=
1337: $Apache::lonhomework::history{"resource.0.checkedin.slot"};
1338: }
1339:
1340: my $result=&Apache::bridgetask::add_to_queue('gradingqueue',\%queue_info);
1341: if ($result ne 'ok') {
1342: &Apache::lonxml::error("add_to_queue said $result");
1343: }
1344: }
1345:
1.251 ! raeburn 1346: =pod
1.208 jms 1347:
1348: =item check_status()
1349:
1350: basically undef and 0 (both false) mean that they still have work to do
1351: and all true values mean that they can't do any more work
1352:
1353: a return of undef means it is unattempted
1.246 raeburn 1354: a return of 0 means it is both attempted and still has tries and
1.251 ! raeburn 1355: is wrong or is only partially correct, and retries
1.246 raeburn 1356: are allowed.
1.208 jms 1357: a return of 1 means it is marked correct
1.246 raeburn 1358: a return of 2 means they have exceeded maximum number of tries
1359: a return of 3 means it is after the answer date
1.208 jms 1360:
1361: =cut
1362:
1.117 albertel 1363: sub check_status {
1364: my ($id)=@_;
1.143 albertel 1365: if (!defined($id)) { $id=$Apache::inputtags::part; }
1.117 albertel 1366: my $curtime=&Apache::lonnet::EXT('system.time');
1367: my $opendate=&Apache::lonnet::EXT("resource.$id.opendate");
1.236 raeburn 1368: my $duedate=&Apache::lonhomework::due_date($id);
1.117 albertel 1369: my $answerdate=&Apache::lonnet::EXT("resource.$id.answerdate");
1370: if ( $opendate && $curtime > $opendate &&
1371: $duedate && $curtime > $duedate &&
1372: $answerdate && $curtime > $answerdate) {
1373: return 3;
1374: }
1375: my $status=&Apache::lonnet::EXT("user.resource.resource.$id.solved");
1.251 ! raeburn 1376: if ($status =~ /^correct/) {
1.246 raeburn 1377: my $awarded=&Apache::lonnet::EXT("user.resource.resource.$id.awarded");
1378: my $retrypartial=&Apache::lonnet::EXT("resource.$id.retrypartial");
1379: unless (($retrypartial =~ /^1|on|yes$/) && ($awarded <1)) {
1380: return 1;
1381: }
1382: }
1.117 albertel 1383: if (!$status) { return undef; }
1384: my $maxtries=&Apache::lonnet::EXT("resource.$id.maxtries");
1385: if ($maxtries eq '') { $maxtries=2; }
1386: my $curtries=&Apache::lonnet::EXT("user.resource.resource.$id.tries");
1387: if ($curtries < $maxtries) { return 0; }
1388: return 2;
1389: }
1390:
1.177 albertel 1391: =pod
1392:
1.210 raeburn 1393: =item setup_prior_tries_hash()
1.177 albertel 1394:
1395: Foreach each past .submission $func is called with 3 arguments
1396: - the mode to set things up for (currently always 'grade')
1397: - the stored .submission string
1398: - The expansion of $data
1399:
1400: $data is an array ref containing elements that are either
1401: - scalars that are other elements of the history hash to pass to $func
1402: - ref to data to be passed untouched to $func
1403:
1.221 raeburn 1404: $questiontype is the questiontype (currently only passed in if
1405: randomizebytry.
1406:
1.177 albertel 1407: =cut
1408:
1.162 albertel 1409: sub setup_prior_tries_hash {
1.221 raeburn 1410: my ($func,$data,$questiontype) = @_;
1.162 albertel 1411: my $part = $Apache::inputtags::part;
1.221 raeburn 1412: my $id = $Apache::inputtags::response[-1];
1.162 albertel 1413: foreach my $i (1..$Apache::lonhomework::history{'version'}) {
1.221 raeburn 1414: my $partprefix = "$i:resource.$part";
1415: my $sub_key = "$partprefix.$id.submission";
1.162 albertel 1416: next if (!exists($Apache::lonhomework::history{$sub_key}));
1.221 raeburn 1417: my $type_key = "$partprefix.type";
1418: my $type = $Apache::lonhomework::history{$type_key};
1.162 albertel 1419: my @other_data;
1.221 raeburn 1420: if (ref($data) eq 'ARRAY') {
1421: foreach my $datum (@{ $data }) {
1422: if (ref($datum)) {
1423: push(@other_data,$datum);
1424: } else {
1425: my $info_key = "$i:resource.$part.$id.$datum";
1426: push(@other_data,$Apache::lonhomework::history{$info_key});
1427: }
1.162 albertel 1428: }
1.221 raeburn 1429: }
1.251 ! raeburn 1430: if ($questiontype eq 'randomizetry') {
1.221 raeburn 1431: my $order_key = "$partprefix.$id.foilorder";
1432: my @whichopts = &Apache::lonnet::str2array($Apache::lonhomework::history{$order_key});
1433: if (@whichopts > 0) {
1434: shift(@other_data);
1435: unshift(@other_data,\@whichopts);
1436: }
1437: }
1.162 albertel 1438: my $output =
1439: &$func('grade',
1440: $Apache::lonhomework::history{$sub_key},
1441: \@other_data);
1442: if (defined($output)) {
1443: $Apache::inputtags::submission_display{$sub_key} = $output;
1444: }
1445: }
1446: }
1447:
1.1 albertel 1448: 1;
1449: __END__
1.251 ! raeburn 1450:
1.208 jms 1451: =pod
1452:
1.209 www 1453: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>