Annotation of loncom/homework/response.pm, revision 1.118
1.38 albertel 1: # The LearningOnline Network with CAPA
1.1 albertel 2: # various response type definitons response definition
1.53 albertel 3: #
1.118 ! foxr 4: # $Id: response.pm,v 1.117 2005/04/01 18:08:14 albertel Exp $
1.53 albertel 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
1.5 www 28:
1.1 albertel 29: package Apache::response;
30: use strict;
1.93 albertel 31: use Apache::lonlocal;
1.1 albertel 32:
1.57 harris41 33: BEGIN {
1.73 albertel 34: &Apache::lonxml::register('Apache::response',('responseparam','parameter','dataresponse'));
1.1 albertel 35: }
36:
1.13 albertel 37: sub start_response {
1.73 albertel 38: my ($parstack,$safeeval)=@_;
39: my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
40: if ($id eq '') { $id = $Apache::lonxml::curdepth; }
41: if ($#Apache::inputtags::import > -1) {
42: &Apache::lonxml::debug("Turning :$id: into");
43: $id = join('_',@Apache::inputtags::import).'_'.$id;
44: &Apache::lonxml::debug("New :$id:");
45: }
46: push (@Apache::inputtags::response,$id);
47: push (@Apache::inputtags::responselist,$id);
48: @Apache::inputtags::inputlist=();
1.101 albertel 49: if ($Apache::inputtags::part eq '' &&
50: !$Apache::lonhomework::ignore_response_errors) {
1.97 albertel 51: &Apache::lonxml::error(&HTML::Entities::encode(&mt("Found a <*response> outside of a <part> in a <part>ed problem"),'<>&"'));
1.92 albertel 52: }
53: if ($Apache::inputtags::response_with_no_part &&
54: $Apache::inputtags::part ne '0') {
1.97 albertel 55: &Apache::lonxml::error(&HTML::Entities::encode(&mt("<*response>s are both inside of <part> and outside of <part>, this is not a valid problem, errors in grading may occur."),'<>&"').'<br />');
1.92 albertel 56: }
57: if ($Apache::inputtags::part eq '0') {
58: $Apache::inputtags::response_with_no_part=1;
59: }
1.73 albertel 60: return $id;
1.13 albertel 61: }
62:
63: sub end_response {
1.79 albertel 64: #pop @Apache::inputtags::response;
1.73 albertel 65: @Apache::inputtags::inputlist=();
66: return '';
1.13 albertel 67: }
68:
1.41 albertel 69: sub start_hintresponse {
1.73 albertel 70: my ($parstack,$safeeval)=@_;
71: my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
72: if ($id eq '') { $id = $Apache::lonxml::curdepth; }
73: push (@Apache::inputtags::response,$id);
1.79 albertel 74: push (@Apache::inputtags::responselist,$id);
1.73 albertel 75: push (@Apache::inputtags::paramstack,[%Apache::inputtags::params]);
76: return $id;
1.41 albertel 77: }
78:
79: sub end_hintresponse {
1.73 albertel 80: pop @Apache::inputtags::response;
81: if (defined($Apache::inputtags::paramstack[-1])) {
82: %Apache::inputtags::params=
83: @{ pop(@Apache::inputtags::paramstack) };
84: }
85: return '';
1.41 albertel 86: }
87:
1.99 albertel 88: my @randomseeds;
89: sub pushrandomnumber {
90: my $rand_alg=&Apache::lonnet::get_rand_alg();
91: if (!$rand_alg || $rand_alg eq '32bit' || $rand_alg eq '64bit' ||
92: $rand_alg eq '64bit2') {
93: # do nothing
94: } else {
95: my @seed=&Math::Random::random_get_seed();
96: push (@randomseeds,\@seed);
97: }
98: &Apache::response::setrandomnumber();
99: }
100: sub poprandomnumber {
101: my $rand_alg=&Apache::lonnet::get_rand_alg();
102: if (!$rand_alg || $rand_alg eq '32bit' || $rand_alg eq '64bit' ||
103: $rand_alg eq '64bit2') {
104: return;
105: }
106: my $seed=pop(@randomseeds);
107: if ($seed) {
108: &Math::Random::random_set_seed(@$seed);
109: } else {
110: &Apache::lonxml::error("Unable to restore random algorithm.");
111: }
112: }
1.117 albertel 113:
1.26 albertel 114: sub setrandomnumber {
1.73 albertel 115: my $rndseed;
1.88 albertel 116: $rndseed=&Apache::structuretags::setup_rndseed();
117: if (!defined($rndseed)) { $rndseed=&Apache::lonnet::rndseed(); }
1.73 albertel 118: &Apache::lonxml::debug("randseed $rndseed");
119: # $rndseed=unpack("%32i",$rndseed);
1.99 albertel 120: my $rand_alg=&Apache::lonnet::get_rand_alg();
121: my $rndmod;
122: if (!$rand_alg || $rand_alg eq '32bit' || $rand_alg eq '64bit' ||
123: $rand_alg eq '64bit2') {
124: $rndmod=(&Apache::lonnet::numval($Apache::inputtags::part) << 10);
125: if (defined($Apache::inputtags::response[-1])) {
126: $rndmod+=&Apache::lonnet::numval($Apache::inputtags::response[-1]);
1.82 albertel 127: }
1.110 albertel 128: } elsif ($rand_alg eq '64bit3') {
1.99 albertel 129: $rndmod=(&Apache::lonnet::numval2($Apache::inputtags::part) << 10);
130: if (defined($Apache::inputtags::response[-1])) {
131: $rndmod+=&Apache::lonnet::numval2($Apache::inputtags::response[-1]);
132: }
1.110 albertel 133: } else {
1.112 albertel 134: my $shift=(4*scalar(@Apache::inputtags::responselist))%30;
135: $rndmod=(&Apache::lonnet::numval3($Apache::inputtags::part) << (($shift+15)%30));
1.110 albertel 136: if (defined($Apache::inputtags::response[-1])) {
1.111 albertel 137: $rndmod+=(&Apache::lonnet::numval3($Apache::inputtags::response[-1]) << $shift );
1.110 albertel 138: }
1.99 albertel 139: }
140: if ($rndseed =~/([,:])/) {
141: my $char=$1;
142: use integer;
143: my ($num1,$num2)=split(/\Q$char\E/,$rndseed);
144: $num1+=$rndmod;
145: $num2+=$rndmod;
1.109 albertel 146: if($Apache::lonnet::_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.99 albertel 147: $rndseed=$num1.$char.$num2;
148: } else {
1.74 albertel 149: $rndseed+=$rndmod;
1.109 albertel 150: if($Apache::lonnet::_64bit) {
151: use integer;
152: $rndseed=(($rndseed<<32)>>32);
153: }
1.74 albertel 154: }
1.111 albertel 155: &Apache::lonxml::debug("randseed $rndmod $rndseed");
1.74 albertel 156: &Apache::lonnet::setup_random_from_rndseed($rndseed);
1.73 albertel 157: return '';
1.26 albertel 158: }
159:
1.7 www 160: sub meta_parameter_write {
1.38 albertel 161: my ($name,$type,$default,$display)=@_;
1.41 albertel 162: my $partref=$Apache::inputtags::part;
163: my $result='<parameter part="'.$Apache::inputtags::part.'"';
164: if (defined($Apache::inputtags::response[-1])) {
1.73 albertel 165: $result.= ' id="'.$Apache::inputtags::response[-1].'"';
166: $partref.='_'.$Apache::inputtags::response[-1];
1.41 albertel 167: }
168: $result.= ' name="'.$name.'"'.
169: ' type="'.$type.'"'.
1.89 albertel 170: (defined($default)?' default="'.$default.'"':'').
171: (defined($display)?' display="'.$display.' [Part: '.$partref.']"':'')
1.41 albertel 172: .'></parameter>'
173: ."\n";
174: return $result;
1.33 www 175: }
176:
177: sub meta_package_write {
178: my $name=shift;
1.41 albertel 179: my $result = '<parameter part="'.$Apache::inputtags::part.'"';
180: if(defined($Apache::inputtags::response[-1])) {
1.73 albertel 181: $result.= ' id="'.$Apache::inputtags::response[-1].'"';
1.41 albertel 182: }
183: $result.=' package="'.$name.'"></parameter>'."\n";
184: return $result;
1.7 www 185: }
186:
187: sub meta_stores_write {
1.10 www 188: my ($name,$type,$display)=@_;
1.41 albertel 189: my $partref=$Apache::inputtags::part;
190: my $result = '<stores part="'.$Apache::inputtags::part.'"';
191: if (defined($Apache::inputtags::response[-1])) {
1.73 albertel 192: $result.= ' id="'.$Apache::inputtags::response[-1].'"';
193: $partref.='_'.$Apache::inputtags::response[-1];
1.41 albertel 194: }
195: $result.= ' name="'.$name.'"'.
196: ' type="'.$type.'"'.
197: ' display="'.$display.' [Part: '.$partref.']"'.
198: "></stores>\n";
1.7 www 199: }
200:
201: sub mandatory_part_meta {
202: #
203: # Autogenerate metadata for mandatory
204: # input (from RAT or lonparmset) and
205: # output (to lonspreadsheet)
206: # of each part
207: #
1.73 albertel 208: return
1.34 www 209: # &meta_parameter_write('opendate','date_start','',
210: # 'Opening Date').
211: # &meta_parameter_write('duedate','date_end','',
212: # 'Due Date').
213: # &meta_parameter_write('answerdate','date_start','',
214: # 'Show Answer Date').
215: # &meta_parameter_write('weight','int_zeropos','',
216: # 'Available Points').
217: # &meta_parameter_write('maxtries','int_pos','',
218: # 'Maximum Number of Tries').
1.73 albertel 219: &meta_package_write('part').
220: &meta_stores_write('solved','string',
221: 'Problem Status').
222: &meta_stores_write('tries','int_zeropos',
223: 'Number of Attempts').
224: &meta_stores_write('awarded','float',
225: 'Partial Credit Factor');
1.7 www 226: #
227: # Note: responseid-specific data 'submission' and 'awarddetail'
228: # not available to spreadsheet -> skip here
229: #
1.86 albertel 230: }
231:
232: sub meta_part_order {
233: if (@Apache::inputtags::partlist) {
234: my @parts=@Apache::inputtags::partlist;
235: shift(@parts);
1.100 albertel 236: return '<partorder>'.join(',',@parts).'</partorder>'."\n";
1.86 albertel 237: } else {
1.100 albertel 238: return '<partorder>0</partorder>'."\n";
239: }
240: }
241:
242: sub meta_response_order {
243: if (@Apache::inputtags::responselist) {
244: return '<responseorder>'.join(',',@Apache::inputtags::responselist).
245: '</responseorder>'."\n";
1.86 albertel 246: }
1.14 albertel 247: }
248:
1.15 albertel 249: sub check_for_previous {
1.73 albertel 250: my ($curresponse,$partid,$id) = @_;
251: my %previous;
252: $previous{'used'} = 0;
253: foreach my $key (sort(keys(%Apache::lonhomework::history))) {
1.98 albertel 254: if ($key =~ /resource\.$partid\.$id\.submission$/) {
1.73 albertel 255: &Apache::lonxml::debug("Trying $key");
256: my $pastresponse=$Apache::lonhomework::history{$key};
257: if ($pastresponse eq $curresponse) {
258: $previous{'used'} = 1;
259: my $history;
260: if ( $key =~ /^(\d+):/ ) {
261: $history=$1;
262: $previous{'award'} = $Apache::lonhomework::history{"$history:resource.$partid.$id.awarddetail"};
263: $previous{'last'}='0';
264: push(@{ $previous{'version'} },$history);
265: } else {
266: $previous{'award'} = $Apache::lonhomework::history{"resource.$partid.$id.awarddetail"};
267: $previous{'last'}='1';
268: }
269: if (! $previous{'award'} ) { $previous{'award'} = 'UNKNOWN'; }
270: &Apache::lonxml::debug("got a match :$previous{'award'}:$previous{'used'}:");
271: }
1.32 albertel 272: }
1.73 albertel 273: }
274: &Apache::lonhomework::showhash(%previous);
275: return %previous;
1.54 albertel 276: }
277:
278: sub handle_previous {
1.73 albertel 279: my ($previous,$ad)=@_;
280: if ($$previous{'used'} && ($$previous{'award'} eq $ad) ) {
281: if ($$previous{'last'}) {
282: push(@Apache::inputtags::previous,'PREVIOUSLY_LAST');
1.107 albertel 283: push(@Apache::inputtags::previous_version,$$previous{'version'});
284: } elsif ($Apache::lonhomework::type ne 'survey') {
1.73 albertel 285: push(@Apache::inputtags::previous,'PREVIOUSLY_USED');
1.107 albertel 286: push(@Apache::inputtags::previous_version,$$previous{'version'});
1.73 albertel 287: }
1.54 albertel 288: }
1.44 albertel 289: }
290:
1.45 albertel 291: sub view_or_modify {
1.73 albertel 292: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
293: my $myself=0;
294: if ( ($name eq $ENV{'user.name'}) && ($domain eq $ENV{'user.domain'}) ) {
295: $myself=1;
296: }
297: my $vgr=&Apache::lonnet::allowed('vgr',$courseid);
298: my $mgr=&Apache::lonnet::allowed('vgr',$courseid);
299: if ($mgr) { return "M"; }
300: if ($vgr) { return "V"; }
301: if ($myself) { return "V"; }
302: return '';
1.45 albertel 303: }
304:
1.44 albertel 305: sub start_dataresponse {
1.73 albertel 306: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
307: my $id = &Apache::response::start_response($parstack,$safeeval);
308: my $result;
309: if ($target eq 'web') {
310: $result = $token->[2]->{'display'}.':';
311: } elsif ($target eq 'meta') {
312: $result = &Apache::response::meta_stores_write($token->[2]->{'name'},
313: $token->[2]->{'type'},
314: $token->[2]->{'display'});
315: $result .= &Apache::response::meta_package_write('dataresponse');
316: }
317: return $result;
1.44 albertel 318: }
319:
320: sub end_dataresponse {
1.73 albertel 321: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
322: my $result;
323: if ( $target eq 'web' ) {
324: } elsif ($target eq 'grade' ) {
325: if ( defined $ENV{'form.submitted'}) {
326: my ($symb,$courseid,$domain,$name)=&Apache::lonxml::whichuser();
327: my $allowed=&Apache::lonnet::allowed('mgr',$courseid);
328: if ($allowed) {
1.94 albertel 329: &Apache::response::setup_params('dataresponse',$safeeval);
1.73 albertel 330: my $partid = $Apache::inputtags::part;
331: my $id = $Apache::inputtags::response['-1'];
332: my $response = $ENV{'form.HWVAL_'.$id};
333: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
334: if ( $response =~ /[^\s]/) {
335: $Apache::lonhomework::results{"resource.$partid.$id.$name"}=$response;
336: $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
337: $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}='SUBMITTED';
338: }
339: } else {
340: $result='Not Permitted to change values.'
341: }
1.45 albertel 342: }
1.73 albertel 343: }
344: &Apache::response::end_response;
345: return $result;
1.3 albertel 346: }
347:
1.83 albertel 348: sub decide_package {
349: my ($tagstack)=@_;
350: my $package;
351: if ($$tagstack[-1] eq 'parameter') {
352: $package='part';
353: } else {
354: my $i=-1;
355: while (defined($$tagstack[$i])) {
356: if ($$tagstack[$i] =~ /(response|hint)$/) {
357: $package=$$tagstack[$i];
358: last;
359: }
360: $i--;
361: }
362: }
363: return $package;
364: }
365:
1.3 albertel 366: sub start_responseparam {
1.73 albertel 367: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
368: my $result='';
369: if ($target eq 'meta') {
370: $result = &meta_parameter_write($token->[2]->{'name'},
371: $token->[2]->{'type'},
372: $token->[2]->{'default'},
373: $token->[2]->{'description'});
374: } elsif ($target eq 'edit') {
375: $result.=&Apache::edit::tag_start($target,$token);
1.83 albertel 376: my $optionlist;
377: my $package=&decide_package($tagstack);
378: foreach my $key (sort(keys(%Apache::lonnet::packagetab))) {
379: if ($key =~ /^\Q$package\E&(.*)&display$/) {
380: $optionlist.='<option value="'.$1.'">'.
381: $Apache::lonnet::packagetab{$key}.'</option>';
382: }
383: }
384: if (defined($optionlist)) {
385: $result.='Use template: <select name="'.
386: &Apache::edit::html_element_name('parameter_package').'">'.
387: '<option value=""></option>'.$optionlist.'</select><br />';
388: }
1.73 albertel 389: $result.=&Apache::edit::text_arg('Name:','name',$token).
390: &Apache::edit::text_arg('Type:','type',$token).
391: &Apache::edit::text_arg('Description:','description',$token).
392: &Apache::edit::text_arg('Default:','default',$token).
393: "</td></tr>";
394: $result.=&Apache::edit::end_table;
395: } elsif ($target eq 'modified') {
1.83 albertel 396: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
397: $safeeval,'name','type',
398: 'description','default');
399: my $element=&Apache::edit::html_element_name('parameter_package');
400: if (defined($ENV{"form.$element"}) && $ENV{"form.$element"} ne '') {
401: my $name=$ENV{"form.$element"};
402: my $tag=&decide_package($tagstack);
403: $token->[2]->{'name'}=$name;
404: $token->[2]->{'type'}=
405: $Apache::lonnet::packagetab{"$tag&$name&type"};
406: $token->[2]->{'description'}=
407: $Apache::lonnet::packagetab{"$tag&$name&display"};
408: $token->[2]->{'default'}=
409: $Apache::lonnet::packagetab{"$tag&$name&default"};
410: $constructtag=1;
411: }
1.73 albertel 412: if ($constructtag) {
413: $result = &Apache::edit::rebuild_tag($token);
414: $result.=&Apache::edit::handle_insert();
415: }
416: } elsif ($target eq 'grade' || $target eq 'answer' || $target eq 'web' ||
417: $target eq 'tex' || $target eq 'analyze' ) {
418: if ($ENV{'request.state'} eq 'construct') {
419: my $name =&Apache::lonxml::get_param('name',$parstack,$safeeval);
420: my $default=&Apache::lonxml::get_param('default',$parstack,
421: $safeeval);
422: if ($name) {$Apache::inputtags::params{$name}=$default;}
423: }
1.52 albertel 424: }
1.73 albertel 425: return $result;
1.3 albertel 426: }
427:
428: sub end_responseparam {
1.73 albertel 429: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
430: if ($target eq 'edit') { return ('','no'); }
431: return '';
1.55 albertel 432: }
433:
434: sub start_parameter {
1.114 albertel 435: return &start_responseparam(@_);
1.55 albertel 436: }
437:
438: sub end_parameter {
1.114 albertel 439: return &end_responseparam(@_);
1.42 albertel 440: }
441:
1.67 albertel 442: sub reset_params {
443: %Apache::inputtags::params=();
444: }
445:
1.42 albertel 446: sub setup_params {
1.94 albertel 447: my ($tag,$safeeval) = @_;
1.42 albertel 448:
1.73 albertel 449: if ($ENV{'request.state'} eq 'construct') { return; }
450: my %paramlist=();
451: foreach my $key (keys(%Apache::lonnet::packagetab)) {
452: if ($key =~ /^$tag/) {
453: my ($package,$name) = split(/&/,$key);
454: $paramlist{$name}=1;
455: }
1.42 albertel 456: }
1.73 albertel 457: foreach my $key (keys(%paramlist)) {
458: my $entry= 'resource.'.$Apache::inputtags::part;
459: if (defined($Apache::inputtags::response[-1])) {
460: $entry.='_'.$Apache::inputtags::response[-1];
461: }
462: $entry.='.'.$key;
463: &Apache::lonxml::debug("looking for $entry");
464: my $value = &Apache::lonnet::EXT("$entry");
465: &Apache::lonxml::debug("$key has value :$value:");
466: if ($value eq 'con_lost' || $value =~ /^error:/) {
467: &Apache::lonxml::debug("using nothing");
468: $Apache::inputtags::params{$key}='';
469: } else {
1.94 albertel 470: my $string="{return qq\0".$value."\0}";
471: my $newvalue=&Apache::run::run($string,$safeeval,1);
472: if (defined($newvalue)) { $value=$newvalue; }
1.73 albertel 473: $Apache::inputtags::params{$key}=$value;
474: }
1.42 albertel 475: }
1.48 albertel 476: }
477:
478: sub answer_header {
1.73 albertel 479: my ($type) = @_;
480: my $result;
1.77 albertel 481: if ($ENV{'form.answer_output_mode'} eq 'tex') {
1.84 sakharuk 482: $result = ' \vskip 0 mm \begin{tabular}{|c|}\hline Answer for Part: \verb|'.
483: $Apache::inputtags::part.'| \\\\ \hline ';
1.73 albertel 484: } else {
1.80 albertel 485: $result = '<table border="1"><tr><td>Answer for Part:'.
486: $Apache::inputtags::part. '</td>'."\n";
1.73 albertel 487: }
488: return $result;
1.48 albertel 489: }
490:
491: sub answer_part {
1.73 albertel 492: my ($type,$answer) = @_;
493: my $result;
1.77 albertel 494: if ($ENV{'form.answer_output_mode'} eq 'tex') {
1.81 sakharuk 495: $result = ' \verb|'.$answer.'|\\\\ \hline ';
1.73 albertel 496: } else {
1.80 albertel 497: $result = '<td>'.$answer.'</td>';
1.73 albertel 498: }
499: return $result;
1.48 albertel 500: }
501:
502: sub answer_footer {
1.73 albertel 503: my ($type) = @_;
504: my $result;
1.77 albertel 505: if ($ENV{'form.answer_output_mode'} eq 'tex') {
1.75 sakharuk 506: $result = ' \end{tabular} \vskip 0 mm ';
1.73 albertel 507: } else {
1.80 albertel 508: $result = '</tr></table>';
1.73 albertel 509: }
510: return $result;
1.1 albertel 511: }
1.2 albertel 512:
1.62 albertel 513: sub showallfoils {
1.102 albertel 514: if (defined($ENV{'form.showallfoils'})) {
515: my ($symb)=&Apache::lonxml::whichuser();
1.118 ! foxr 516: if (($ENV{'request.state'} eq 'construct') ||
! 517: ($ENV{'user.adv'} && $symb eq '') ||
! 518: ($Apache::lonhomework::viewgrades) ) {
1.102 albertel 519: return 1;
520: }
1.73 albertel 521: }
1.108 albertel 522: if ($Apache::lonhomework::type eq 'survey') { return 1; }
1.102 albertel 523: return 0;
1.70 albertel 524: }
525:
526: sub getresponse {
1.90 albertel 527: my ($temp,$resulttype)=@_;
1.70 albertel 528: my $formparm='form.HWVAL_'.$Apache::inputtags::response['-1'];
529: my $response;
530: if (!defined($temp)) {
531: $temp=1;
532: } else {
533: $formparm.=":$temp";
534: }
535: my %let_to_num=('A'=>0,'B'=>1,'C'=>2,'D'=>3,'E'=>4,'F'=>5,'G'=>6,'H'=>7,
536: 'I'=>8,'J'=>9,'K'=>10,'L'=>11,'M'=>12,'N'=>13,'O'=>14,
537: 'P'=>15,'Q'=>16,'R'=>17,'S'=>18,'T'=>19,'U'=>20,'V'=>21,
538: 'W'=>22,'X'=>23,'Y'=>24,'Z'=>25);
539: if ($ENV{'form.submitted'} eq 'scantron') {
1.71 albertel 540: my $part = $Apache::inputtags::part;
541: my $id = $Apache::inputtags::response[-1];
1.70 albertel 542: $response = $ENV{'scantron.'.($Apache::lonxml::counter+$temp-1).
543: '.answer'};
1.71 albertel 544: # save bubbled letter for later
545: $Apache::lonhomework::results{"resource.$part.$id.scantron"}.=
546: $response;
1.90 albertel 547: if ($resulttype ne 'letter') {
1.104 albertel 548: if ($resulttype eq 'A is 1') {
1.105 albertel 549: $response = $let_to_num{$response}+1;
550: } else {
1.104 albertel 551: $response = $let_to_num{$response};
552: }
1.90 albertel 553: }
1.70 albertel 554: } else {
555: $response = $ENV{$formparm};
556: }
557: return $response;
1.62 albertel 558: }
1.71 albertel 559:
560: sub repetition {
561: my $id = $Apache::inputtags::part;
562: my $weight = &Apache::lonnet::EXT("resource.$id.weight");
563: my $repetition = int $weight/9;
564: if ($weight % 9 != 0) {$repetition++;}
1.72 albertel 565: return $repetition;
566: }
567:
568: sub scored_response {
569: my ($part,$id)=@_;
570: my $repetition=&repetition();
571: my $score=0;
572: for (my $i=0;$i<$repetition;$i++) {
573: my $increase=&Apache::response::getresponse($i+1);
574: if ($increase ne '') { $score+=$increase+1; }
575: }
576: my $weight = &Apache::lonnet::EXT("resource.$part.weight");
1.91 albertel 577: if (!defined($weight) || $weight eq '' || $weight eq 0) { $weight = 1; }
1.72 albertel 578: my $pcr=$score/$weight;
579: $Apache::lonhomework::results{"resource.$part.$id.awarded"}=$pcr;
580: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
581: 'ASSIGNED_SCORE';
1.71 albertel 582: return $repetition;
1.78 albertel 583: }
584:
585: sub whichorder {
586: my ($max,$randomize,$showall,$hash)=@_;
587: #&Apache::lonxml::debug("man $max randomize $randomize");
588: if (!defined(@{ $$hash{'names'} })) { return; }
589: my @names = @{ $$hash{'names'} };
590: my @whichopt =();
591: my (%top,@toplist,%bottom,@bottomlist);
592: if (!($showall || ($randomize eq 'no'))) {
593: my $current=0;
594: foreach my $name (@names) {
595: $current++;
596: if ($$hash{"$name.location"} eq 'top') {
597: $top{$name}=$current;
598: } elsif ($$hash{"$name.location"} eq 'bottom') {
599: $bottom{$name}=$current;
600: }
601: }
602: }
603: my $topcount=0;
604: my $bottomcount=0;
605: while (((scalar(@whichopt)+$topcount+$bottomcount) < $max || $showall)
606: && ($#names > -1)) {
607: #&Apache::lonxml::debug("Have $#whichopt max is $max");
608: my $aopt;
609: if ($showall || ($randomize eq 'no')) {
610: $aopt=0;
611: } else {
612: $aopt=int(&Math::Random::random_uniform() * ($#names+1));
613: }
614: #&Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
615: $aopt=splice(@names,$aopt,1);
616: #&Apache::lonxml::debug("Picked $aopt");
617: if ($top{$aopt}) {
618: $toplist[$top{$aopt}]=$aopt;
619: $topcount++;
620: } elsif ($bottom{$aopt}) {
621: $bottomlist[$bottom{$aopt}]=$aopt;
622: $bottomcount++;
623: } else {
624: push (@whichopt,$aopt);
625: }
626: }
627: for (my $i=0;$i<=$#toplist;$i++) {
628: if ($toplist[$i]) { unshift(@whichopt,$toplist[$i]) }
629: }
630: for (my $i=0;$i<=$#bottomlist;$i++) {
631: if ($bottomlist[$i]) { push(@whichopt,$bottomlist[$i]) }
632: }
633: return @whichopt;
1.71 albertel 634: }
635:
1.85 albertel 636: sub show_answer {
637: my $part = $Apache::inputtags::part;
638: my $award = $Apache::lonhomework::history{"resource.$part.solved"};
639: my $status = $Apache::inputtags::status[-1];
640: return ( ($award =~ /^correct/
641: && lc($Apache::lonhomework::problemstatus) ne 'no')
642: || $status eq "SHOW_ANSWER");
643: }
1.87 albertel 644:
645: sub analyze_store_foilgroup {
646: my ($shown,$attrs)=@_;
647: my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
648: foreach my $name (@{ $Apache::response::foilgroup{'names'} }) {
649: if (defined($Apache::lonhomework::analyze{"$part_id.foil.value.$name"})) { next; }
650: push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },$name);
651: foreach my $attr (@$attrs) {
652: $Apache::lonhomework::analyze{"$part_id.foil.".$attr.".$name"} =
653: $Apache::response::foilgroup{"$name.".$attr};
654: }
655: }
656: push (@{ $Apache::lonhomework::analyze{"$part_id.shown"} }, @{ $shown });
1.96 albertel 657: }
658:
659: sub check_if_computed {
660: my ($token,$parstack,$safeeval,$name)=@_;
661: my $value = &Apache::lonxml::get_param($name,$parstack,$safeeval);
1.106 matthew 662: if (ref($token->[2]) eq 'HASH' && $value ne $token->[2]{$name}) {
1.96 albertel 663: my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
664: $Apache::lonhomework::analyze{"$part_id.answercomputed"} = 1;
665: }
1.87 albertel 666: }
667:
668: sub pick_foil_for_concept {
669: my ($target,$attrs,$hinthash,$parstack,$safeeval)=@_;
670: if (not defined(@{ $Apache::response::conceptgroup{'names'} })) { return; }
671: my @names = @{ $Apache::response::conceptgroup{'names'} };
672: my $pick=int(&Math::Random::random_uniform() * ($#names+1));
673: my $name=$names[$pick];
674: push @{ $Apache::response::foilgroup{'names'} }, $name;
675: foreach my $attr (@$attrs) {
676: $Apache::response::foilgroup{"$name.".$attr} =
677: $Apache::response::conceptgroup{"$name.".$attr};
678: }
679: my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
680: $Apache::response::foilgroup{"$name.concept"} = $concept;
681: &Apache::lonxml::debug("Selecting $name in $concept");
682: my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
683: if ($target eq 'analyze') {
684: push (@{ $Apache::lonhomework::analyze{"$part_id.concepts"} },
685: $concept);
686: $Apache::lonhomework::analyze{"$part_id.concept.$concept"}=
687: $Apache::response::conceptgroup{'names'};
688: foreach my $name (@{ $Apache::response::conceptgroup{'names'} }) {
689: push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },
690: $name);
691: foreach my $attr (@$attrs) {
692: $Apache::lonhomework::analyze{"$part_id.foil.$attr.$name"}=
693: $Apache::response::conceptgroup{"$name.$attr"};
694: }
695: }
696: }
697: push(@{ $hinthash->{"$part_id.concepts"} },$concept);
698: $hinthash->{"$part_id.concept.$concept"}=
699: $Apache::response::conceptgroup{'names'};
700:
701: }
702:
1.95 albertel 703: sub get_response_param {
704: my ($id,$name,$default)=@_;
705: my $parameter;
706: if ($ENV{'request.state'} eq 'construct' &&
707: defined($Apache::inputtags::params{$name})) {
708: $parameter=$Apache::inputtags::params{$name};
709: } else {
710: $parameter=&Apache::lonnet::EXT("resource.$id.$name");
711: }
712: if (!defined($parameter) || $parameter eq '') {
713: $parameter = $default;
714: }
715: return $parameter;
716: }
1.87 albertel 717:
1.113 albertel 718: sub submitted {
719: my ($who)=@_;
720:
721: # when scatron grading any submission is a submission
722: if ($ENV{'form.submitted'} eq 'scantron') { return 1; }
723: # if the caller only cared if this was a scantron submission
724: if ($who eq 'scantron') { return 0; }
725: # if the Submit Answer button for this particular part was pressed
726: my $partid=$Apache::inputtags::part;
727: if (defined($ENV{'form.submit_'.$partid})) { return 1; }
728: # otherwise no submission occured
729: return 0;
730: }
1.117 albertel 731:
732: # basically undef and 0 (both false) mean that they still have work to do
733: # and all true values mean that they can't do any more work
734: #
735: # a return of undef means it is unattempted
736: # a return of 0 means it is attmpted and wrong but still has tries
737: # a return of 1 means it is marked correct
738: # a return of 2 means they have exceed maximum number of tries
739: # a return of 3 means it after the answer date
740: sub check_status {
741: my ($id)=@_;
742: if (!$id) { $id=$Apache::linputtags::part; }
743: my $curtime=&Apache::lonnet::EXT('system.time');
744: my $opendate=&Apache::lonnet::EXT("resource.$id.opendate");
745: my $duedate=&Apache::lonnet::EXT("resource.$id.duedate");
746: my $answerdate=&Apache::lonnet::EXT("resource.$id.answerdate");
747: if ( $opendate && $curtime > $opendate &&
748: $duedate && $curtime > $duedate &&
749: $answerdate && $curtime > $answerdate) {
750: return 3;
751: }
752: my $status=&Apache::lonnet::EXT("user.resource.resource.$id.solved");
753: if ($status =~ /^correct/) { return 1; }
754: if (!$status) { return undef; }
755: my $maxtries=&Apache::lonnet::EXT("resource.$id.maxtries");
756: if ($maxtries eq '') { $maxtries=2; }
757: my $curtries=&Apache::lonnet::EXT("user.resource.resource.$id.tries");
758: if ($curtries < $maxtries) { return 0; }
759: return 2;
760: }
761:
1.1 albertel 762: 1;
763: __END__
1.38 albertel 764:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>