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