Annotation of loncom/homework/response.pm, revision 1.105.2.1
1.38 albertel 1: # The LearningOnline Network with CAPA
1.1 albertel 2: # various response type definitons response definition
1.53 albertel 3: #
1.105.2.1! albertel 4: # $Id: response.pm,v 1.105 2004/09/27 22:47:49 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.26 albertel 113: sub setrandomnumber {
1.73 albertel 114: my $rndseed;
1.88 albertel 115: $rndseed=&Apache::structuretags::setup_rndseed();
116: if (!defined($rndseed)) { $rndseed=&Apache::lonnet::rndseed(); }
1.73 albertel 117: &Apache::lonxml::debug("randseed $rndseed");
118: # $rndseed=unpack("%32i",$rndseed);
1.99 albertel 119: my $rand_alg=&Apache::lonnet::get_rand_alg();
120: my $rndmod;
121: if (!$rand_alg || $rand_alg eq '32bit' || $rand_alg eq '64bit' ||
122: $rand_alg eq '64bit2') {
123: $rndmod=(&Apache::lonnet::numval($Apache::inputtags::part) << 10);
124: if (defined($Apache::inputtags::response[-1])) {
125: $rndmod+=&Apache::lonnet::numval($Apache::inputtags::response[-1]);
1.82 albertel 126: }
1.74 albertel 127: } else {
1.99 albertel 128: $rndmod=(&Apache::lonnet::numval2($Apache::inputtags::part) << 10);
129: if (defined($Apache::inputtags::response[-1])) {
130: $rndmod+=&Apache::lonnet::numval2($Apache::inputtags::response[-1]);
131: }
132: }
133: if ($rndseed =~/([,:])/) {
134: my $char=$1;
135: use integer;
136: my ($num1,$num2)=split(/\Q$char\E/,$rndseed);
137: $num1+=$rndmod;
138: $num2+=$rndmod;
1.105.2.1! albertel 139: if($Apache::lonnet::_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.99 albertel 140: $rndseed=$num1.$char.$num2;
141: } else {
1.74 albertel 142: $rndseed+=$rndmod;
1.105.2.1! albertel 143: if($Apache::lonnet::_64bit) {
! 144: use integer;
! 145: $rndseed=(($rndseed<<32)>>32);
! 146: }
1.74 albertel 147: }
1.73 albertel 148: &Apache::lonxml::debug("randseed $rndseed");
1.105.2.1! albertel 149: &Apache::lonnet::setup_random_from_rndseed($rndseed);
1.73 albertel 150: return '';
1.26 albertel 151: }
152:
1.7 www 153: sub meta_parameter_write {
1.38 albertel 154: my ($name,$type,$default,$display)=@_;
1.41 albertel 155: my $partref=$Apache::inputtags::part;
156: my $result='<parameter part="'.$Apache::inputtags::part.'"';
157: if (defined($Apache::inputtags::response[-1])) {
1.73 albertel 158: $result.= ' id="'.$Apache::inputtags::response[-1].'"';
159: $partref.='_'.$Apache::inputtags::response[-1];
1.41 albertel 160: }
161: $result.= ' name="'.$name.'"'.
162: ' type="'.$type.'"'.
1.89 albertel 163: (defined($default)?' default="'.$default.'"':'').
164: (defined($display)?' display="'.$display.' [Part: '.$partref.']"':'')
1.41 albertel 165: .'></parameter>'
166: ."\n";
167: return $result;
1.33 www 168: }
169:
170: sub meta_package_write {
171: my $name=shift;
1.41 albertel 172: my $result = '<parameter part="'.$Apache::inputtags::part.'"';
173: if(defined($Apache::inputtags::response[-1])) {
1.73 albertel 174: $result.= ' id="'.$Apache::inputtags::response[-1].'"';
1.41 albertel 175: }
176: $result.=' package="'.$name.'"></parameter>'."\n";
177: return $result;
1.7 www 178: }
179:
180: sub meta_stores_write {
1.10 www 181: my ($name,$type,$display)=@_;
1.41 albertel 182: my $partref=$Apache::inputtags::part;
183: my $result = '<stores part="'.$Apache::inputtags::part.'"';
184: if (defined($Apache::inputtags::response[-1])) {
1.73 albertel 185: $result.= ' id="'.$Apache::inputtags::response[-1].'"';
186: $partref.='_'.$Apache::inputtags::response[-1];
1.41 albertel 187: }
188: $result.= ' name="'.$name.'"'.
189: ' type="'.$type.'"'.
190: ' display="'.$display.' [Part: '.$partref.']"'.
191: "></stores>\n";
1.7 www 192: }
193:
194: sub mandatory_part_meta {
195: #
196: # Autogenerate metadata for mandatory
197: # input (from RAT or lonparmset) and
198: # output (to lonspreadsheet)
199: # of each part
200: #
1.73 albertel 201: return
1.34 www 202: # &meta_parameter_write('opendate','date_start','',
203: # 'Opening Date').
204: # &meta_parameter_write('duedate','date_end','',
205: # 'Due Date').
206: # &meta_parameter_write('answerdate','date_start','',
207: # 'Show Answer Date').
208: # &meta_parameter_write('weight','int_zeropos','',
209: # 'Available Points').
210: # &meta_parameter_write('maxtries','int_pos','',
211: # 'Maximum Number of Tries').
1.73 albertel 212: &meta_package_write('part').
213: &meta_stores_write('solved','string',
214: 'Problem Status').
215: &meta_stores_write('tries','int_zeropos',
216: 'Number of Attempts').
217: &meta_stores_write('awarded','float',
218: 'Partial Credit Factor');
1.7 www 219: #
220: # Note: responseid-specific data 'submission' and 'awarddetail'
221: # not available to spreadsheet -> skip here
222: #
1.86 albertel 223: }
224:
225: sub meta_part_order {
226: if (@Apache::inputtags::partlist) {
227: my @parts=@Apache::inputtags::partlist;
228: shift(@parts);
1.100 albertel 229: return '<partorder>'.join(',',@parts).'</partorder>'."\n";
1.86 albertel 230: } else {
1.100 albertel 231: return '<partorder>0</partorder>'."\n";
232: }
233: }
234:
235: sub meta_response_order {
236: if (@Apache::inputtags::responselist) {
237: return '<responseorder>'.join(',',@Apache::inputtags::responselist).
238: '</responseorder>'."\n";
1.86 albertel 239: }
1.14 albertel 240: }
241:
1.15 albertel 242: sub check_for_previous {
1.73 albertel 243: my ($curresponse,$partid,$id) = @_;
244: my %previous;
245: $previous{'used'} = 0;
246: foreach my $key (sort(keys(%Apache::lonhomework::history))) {
1.98 albertel 247: if ($key =~ /resource\.$partid\.$id\.submission$/) {
1.73 albertel 248: &Apache::lonxml::debug("Trying $key");
249: my $pastresponse=$Apache::lonhomework::history{$key};
250: if ($pastresponse eq $curresponse) {
251: $previous{'used'} = 1;
252: my $history;
253: if ( $key =~ /^(\d+):/ ) {
254: $history=$1;
255: $previous{'award'} = $Apache::lonhomework::history{"$history:resource.$partid.$id.awarddetail"};
256: $previous{'last'}='0';
257: push(@{ $previous{'version'} },$history);
258: } else {
259: $previous{'award'} = $Apache::lonhomework::history{"resource.$partid.$id.awarddetail"};
260: $previous{'last'}='1';
261: }
262: if (! $previous{'award'} ) { $previous{'award'} = 'UNKNOWN'; }
263: &Apache::lonxml::debug("got a match :$previous{'award'}:$previous{'used'}:");
264: }
1.32 albertel 265: }
1.73 albertel 266: }
267: &Apache::lonhomework::showhash(%previous);
268: return %previous;
1.54 albertel 269: }
270:
271: sub handle_previous {
1.73 albertel 272: my ($previous,$ad)=@_;
273: if ($$previous{'used'} && ($$previous{'award'} eq $ad) ) {
274: if ($$previous{'last'}) {
275: push(@Apache::inputtags::previous,'PREVIOUSLY_LAST');
276: } else {
277: push(@Apache::inputtags::previous,'PREVIOUSLY_USED');
278: }
279: push(@Apache::inputtags::previous_version,$$previous{'version'});
1.54 albertel 280: }
1.44 albertel 281: }
282:
1.45 albertel 283: sub view_or_modify {
1.73 albertel 284: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
285: my $myself=0;
286: if ( ($name eq $ENV{'user.name'}) && ($domain eq $ENV{'user.domain'}) ) {
287: $myself=1;
288: }
289: my $vgr=&Apache::lonnet::allowed('vgr',$courseid);
290: my $mgr=&Apache::lonnet::allowed('vgr',$courseid);
291: if ($mgr) { return "M"; }
292: if ($vgr) { return "V"; }
293: if ($myself) { return "V"; }
294: return '';
1.45 albertel 295: }
296:
1.44 albertel 297: sub start_dataresponse {
1.73 albertel 298: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
299: my $id = &Apache::response::start_response($parstack,$safeeval);
300: my $result;
301: if ($target eq 'web') {
302: $result = $token->[2]->{'display'}.':';
303: } elsif ($target eq 'meta') {
304: $result = &Apache::response::meta_stores_write($token->[2]->{'name'},
305: $token->[2]->{'type'},
306: $token->[2]->{'display'});
307: $result .= &Apache::response::meta_package_write('dataresponse');
308: }
309: return $result;
1.44 albertel 310: }
311:
312: sub end_dataresponse {
1.73 albertel 313: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
314: my $result;
315: if ( $target eq 'web' ) {
316: } elsif ($target eq 'grade' ) {
317: if ( defined $ENV{'form.submitted'}) {
318: my ($symb,$courseid,$domain,$name)=&Apache::lonxml::whichuser();
319: my $allowed=&Apache::lonnet::allowed('mgr',$courseid);
320: if ($allowed) {
1.94 albertel 321: &Apache::response::setup_params('dataresponse',$safeeval);
1.73 albertel 322: my $partid = $Apache::inputtags::part;
323: my $id = $Apache::inputtags::response['-1'];
324: my $response = $ENV{'form.HWVAL_'.$id};
325: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
326: if ( $response =~ /[^\s]/) {
327: $Apache::lonhomework::results{"resource.$partid.$id.$name"}=$response;
328: $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
329: $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}='SUBMITTED';
330: }
331: } else {
332: $result='Not Permitted to change values.'
333: }
1.45 albertel 334: }
1.73 albertel 335: }
336: &Apache::response::end_response;
337: return $result;
1.3 albertel 338: }
339:
1.83 albertel 340: sub decide_package {
341: my ($tagstack)=@_;
342: my $package;
343: if ($$tagstack[-1] eq 'parameter') {
344: $package='part';
345: } else {
346: my $i=-1;
347: while (defined($$tagstack[$i])) {
348: if ($$tagstack[$i] =~ /(response|hint)$/) {
349: $package=$$tagstack[$i];
350: last;
351: }
352: $i--;
353: }
354: }
355: return $package;
356: }
357:
1.3 albertel 358: sub start_responseparam {
1.73 albertel 359: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
360: my $result='';
361: if ($target eq 'meta') {
362: $result = &meta_parameter_write($token->[2]->{'name'},
363: $token->[2]->{'type'},
364: $token->[2]->{'default'},
365: $token->[2]->{'description'});
366: } elsif ($target eq 'edit') {
367: $result.=&Apache::edit::tag_start($target,$token);
1.83 albertel 368: my $optionlist;
369: my $package=&decide_package($tagstack);
370: foreach my $key (sort(keys(%Apache::lonnet::packagetab))) {
371: if ($key =~ /^\Q$package\E&(.*)&display$/) {
372: $optionlist.='<option value="'.$1.'">'.
373: $Apache::lonnet::packagetab{$key}.'</option>';
374: }
375: }
376: if (defined($optionlist)) {
377: $result.='Use template: <select name="'.
378: &Apache::edit::html_element_name('parameter_package').'">'.
379: '<option value=""></option>'.$optionlist.'</select><br />';
380: }
1.73 albertel 381: $result.=&Apache::edit::text_arg('Name:','name',$token).
382: &Apache::edit::text_arg('Type:','type',$token).
383: &Apache::edit::text_arg('Description:','description',$token).
384: &Apache::edit::text_arg('Default:','default',$token).
385: "</td></tr>";
386: $result.=&Apache::edit::end_table;
387: } elsif ($target eq 'modified') {
1.83 albertel 388: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
389: $safeeval,'name','type',
390: 'description','default');
391: my $element=&Apache::edit::html_element_name('parameter_package');
392: if (defined($ENV{"form.$element"}) && $ENV{"form.$element"} ne '') {
393: my $name=$ENV{"form.$element"};
394: my $tag=&decide_package($tagstack);
395: $token->[2]->{'name'}=$name;
396: $token->[2]->{'type'}=
397: $Apache::lonnet::packagetab{"$tag&$name&type"};
398: $token->[2]->{'description'}=
399: $Apache::lonnet::packagetab{"$tag&$name&display"};
400: $token->[2]->{'default'}=
401: $Apache::lonnet::packagetab{"$tag&$name&default"};
402: $constructtag=1;
403: }
1.73 albertel 404: if ($constructtag) {
405: $result = &Apache::edit::rebuild_tag($token);
406: $result.=&Apache::edit::handle_insert();
407: }
408: } elsif ($target eq 'grade' || $target eq 'answer' || $target eq 'web' ||
409: $target eq 'tex' || $target eq 'analyze' ) {
410: if ($ENV{'request.state'} eq 'construct') {
411: my $name =&Apache::lonxml::get_param('name',$parstack,$safeeval);
412: my $default=&Apache::lonxml::get_param('default',$parstack,
413: $safeeval);
414: if ($name) {$Apache::inputtags::params{$name}=$default;}
415: }
1.52 albertel 416: }
1.73 albertel 417: return $result;
1.3 albertel 418: }
419:
420: sub end_responseparam {
1.73 albertel 421: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
422: if ($target eq 'edit') { return ('','no'); }
423: return '';
1.55 albertel 424: }
425:
426: sub start_parameter {
1.73 albertel 427: my $result = &start_responseparam(@_);
428: return $result;
1.55 albertel 429: }
430:
431: sub end_parameter {
1.73 albertel 432: my $result = &end_responseparam(@_);
433: return $result;
1.42 albertel 434: }
435:
1.67 albertel 436: sub reset_params {
437: %Apache::inputtags::params=();
438: }
439:
1.42 albertel 440: sub setup_params {
1.94 albertel 441: my ($tag,$safeeval) = @_;
1.42 albertel 442:
1.73 albertel 443: if ($ENV{'request.state'} eq 'construct') { return; }
444: my %paramlist=();
445: foreach my $key (keys(%Apache::lonnet::packagetab)) {
446: if ($key =~ /^$tag/) {
447: my ($package,$name) = split(/&/,$key);
448: $paramlist{$name}=1;
449: }
1.42 albertel 450: }
1.73 albertel 451: foreach my $key (keys(%paramlist)) {
452: my $entry= 'resource.'.$Apache::inputtags::part;
453: if (defined($Apache::inputtags::response[-1])) {
454: $entry.='_'.$Apache::inputtags::response[-1];
455: }
456: $entry.='.'.$key;
457: &Apache::lonxml::debug("looking for $entry");
458: my $value = &Apache::lonnet::EXT("$entry");
459: &Apache::lonxml::debug("$key has value :$value:");
460: if ($value eq 'con_lost' || $value =~ /^error:/) {
461: &Apache::lonxml::debug("using nothing");
462: $Apache::inputtags::params{$key}='';
463: } else {
1.94 albertel 464: my $string="{return qq\0".$value."\0}";
465: my $newvalue=&Apache::run::run($string,$safeeval,1);
466: if (defined($newvalue)) { $value=$newvalue; }
1.73 albertel 467: $Apache::inputtags::params{$key}=$value;
468: }
1.42 albertel 469: }
1.48 albertel 470: }
471:
472: sub answer_header {
1.73 albertel 473: my ($type) = @_;
474: my $result;
1.77 albertel 475: if ($ENV{'form.answer_output_mode'} eq 'tex') {
1.84 sakharuk 476: $result = ' \vskip 0 mm \begin{tabular}{|c|}\hline Answer for Part: \verb|'.
477: $Apache::inputtags::part.'| \\\\ \hline ';
1.73 albertel 478: } else {
1.80 albertel 479: $result = '<table border="1"><tr><td>Answer for Part:'.
480: $Apache::inputtags::part. '</td>'."\n";
1.73 albertel 481: }
482: return $result;
1.48 albertel 483: }
484:
485: sub answer_part {
1.73 albertel 486: my ($type,$answer) = @_;
487: my $result;
1.77 albertel 488: if ($ENV{'form.answer_output_mode'} eq 'tex') {
1.81 sakharuk 489: $result = ' \verb|'.$answer.'|\\\\ \hline ';
1.73 albertel 490: } else {
1.80 albertel 491: $result = '<td>'.$answer.'</td>';
1.73 albertel 492: }
493: return $result;
1.48 albertel 494: }
495:
496: sub answer_footer {
1.73 albertel 497: my ($type) = @_;
498: my $result;
1.77 albertel 499: if ($ENV{'form.answer_output_mode'} eq 'tex') {
1.75 sakharuk 500: $result = ' \end{tabular} \vskip 0 mm ';
1.73 albertel 501: } else {
1.80 albertel 502: $result = '</tr></table>';
1.73 albertel 503: }
504: return $result;
1.1 albertel 505: }
1.2 albertel 506:
1.62 albertel 507: sub showallfoils {
1.102 albertel 508: if (defined($ENV{'form.showallfoils'})) {
509: my ($symb)=&Apache::lonxml::whichuser();
510: if ($ENV{'request.state'} eq 'construct' ||
511: ($ENV{'user.adv'} && $symb eq '')) {
512: return 1;
513: }
1.73 albertel 514: }
1.102 albertel 515: return 0;
1.70 albertel 516: }
517:
518: sub getresponse {
1.90 albertel 519: my ($temp,$resulttype)=@_;
1.70 albertel 520: my $formparm='form.HWVAL_'.$Apache::inputtags::response['-1'];
521: my $response;
522: if (!defined($temp)) {
523: $temp=1;
524: } else {
525: $formparm.=":$temp";
526: }
527: my %let_to_num=('A'=>0,'B'=>1,'C'=>2,'D'=>3,'E'=>4,'F'=>5,'G'=>6,'H'=>7,
528: 'I'=>8,'J'=>9,'K'=>10,'L'=>11,'M'=>12,'N'=>13,'O'=>14,
529: 'P'=>15,'Q'=>16,'R'=>17,'S'=>18,'T'=>19,'U'=>20,'V'=>21,
530: 'W'=>22,'X'=>23,'Y'=>24,'Z'=>25);
531: if ($ENV{'form.submitted'} eq 'scantron') {
1.71 albertel 532: my $part = $Apache::inputtags::part;
533: my $id = $Apache::inputtags::response[-1];
1.70 albertel 534: $response = $ENV{'scantron.'.($Apache::lonxml::counter+$temp-1).
535: '.answer'};
1.71 albertel 536: # save bubbled letter for later
537: $Apache::lonhomework::results{"resource.$part.$id.scantron"}.=
538: $response;
1.90 albertel 539: if ($resulttype ne 'letter') {
1.104 albertel 540: if ($resulttype eq 'A is 1') {
1.105 albertel 541: $response = $let_to_num{$response}+1;
542: } else {
1.104 albertel 543: $response = $let_to_num{$response};
544: }
1.90 albertel 545: }
1.70 albertel 546: } else {
547: $response = $ENV{$formparm};
548: }
549: return $response;
1.62 albertel 550: }
1.71 albertel 551:
552: sub repetition {
553: my $id = $Apache::inputtags::part;
554: my $weight = &Apache::lonnet::EXT("resource.$id.weight");
555: my $repetition = int $weight/9;
556: if ($weight % 9 != 0) {$repetition++;}
1.72 albertel 557: return $repetition;
558: }
559:
560: sub scored_response {
561: my ($part,$id)=@_;
562: my $repetition=&repetition();
563: my $score=0;
564: for (my $i=0;$i<$repetition;$i++) {
565: my $increase=&Apache::response::getresponse($i+1);
566: if ($increase ne '') { $score+=$increase+1; }
567: }
568: my $weight = &Apache::lonnet::EXT("resource.$part.weight");
1.91 albertel 569: if (!defined($weight) || $weight eq '' || $weight eq 0) { $weight = 1; }
1.72 albertel 570: my $pcr=$score/$weight;
571: $Apache::lonhomework::results{"resource.$part.$id.awarded"}=$pcr;
572: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
573: 'ASSIGNED_SCORE';
1.71 albertel 574: return $repetition;
1.78 albertel 575: }
576:
577: sub whichorder {
578: my ($max,$randomize,$showall,$hash)=@_;
579: #&Apache::lonxml::debug("man $max randomize $randomize");
580: if (!defined(@{ $$hash{'names'} })) { return; }
581: my @names = @{ $$hash{'names'} };
582: my @whichopt =();
583: my (%top,@toplist,%bottom,@bottomlist);
584: if (!($showall || ($randomize eq 'no'))) {
585: my $current=0;
586: foreach my $name (@names) {
587: $current++;
588: if ($$hash{"$name.location"} eq 'top') {
589: $top{$name}=$current;
590: } elsif ($$hash{"$name.location"} eq 'bottom') {
591: $bottom{$name}=$current;
592: }
593: }
594: }
595: my $topcount=0;
596: my $bottomcount=0;
597: while (((scalar(@whichopt)+$topcount+$bottomcount) < $max || $showall)
598: && ($#names > -1)) {
599: #&Apache::lonxml::debug("Have $#whichopt max is $max");
600: my $aopt;
601: if ($showall || ($randomize eq 'no')) {
602: $aopt=0;
603: } else {
604: $aopt=int(&Math::Random::random_uniform() * ($#names+1));
605: }
606: #&Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
607: $aopt=splice(@names,$aopt,1);
608: #&Apache::lonxml::debug("Picked $aopt");
609: if ($top{$aopt}) {
610: $toplist[$top{$aopt}]=$aopt;
611: $topcount++;
612: } elsif ($bottom{$aopt}) {
613: $bottomlist[$bottom{$aopt}]=$aopt;
614: $bottomcount++;
615: } else {
616: push (@whichopt,$aopt);
617: }
618: }
619: for (my $i=0;$i<=$#toplist;$i++) {
620: if ($toplist[$i]) { unshift(@whichopt,$toplist[$i]) }
621: }
622: for (my $i=0;$i<=$#bottomlist;$i++) {
623: if ($bottomlist[$i]) { push(@whichopt,$bottomlist[$i]) }
624: }
625: return @whichopt;
1.71 albertel 626: }
627:
1.85 albertel 628: sub show_answer {
629: my $part = $Apache::inputtags::part;
630: my $award = $Apache::lonhomework::history{"resource.$part.solved"};
631: my $status = $Apache::inputtags::status[-1];
632: return ( ($award =~ /^correct/
633: && lc($Apache::lonhomework::problemstatus) ne 'no')
634: || $status eq "SHOW_ANSWER");
635: }
1.87 albertel 636:
637: sub analyze_store_foilgroup {
638: my ($shown,$attrs)=@_;
639: my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
640: foreach my $name (@{ $Apache::response::foilgroup{'names'} }) {
641: if (defined($Apache::lonhomework::analyze{"$part_id.foil.value.$name"})) { next; }
642: push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },$name);
643: foreach my $attr (@$attrs) {
644: $Apache::lonhomework::analyze{"$part_id.foil.".$attr.".$name"} =
645: $Apache::response::foilgroup{"$name.".$attr};
646: }
647: }
648: push (@{ $Apache::lonhomework::analyze{"$part_id.shown"} }, @{ $shown });
1.96 albertel 649: }
650:
651: sub check_if_computed {
652: my ($token,$parstack,$safeeval,$name)=@_;
653: my $value = &Apache::lonxml::get_param($name,$parstack,$safeeval);
654: if ($value ne $token->[2]{$name}) {
655: my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
656: $Apache::lonhomework::analyze{"$part_id.answercomputed"} = 1;
657: }
1.87 albertel 658: }
659:
660: sub pick_foil_for_concept {
661: my ($target,$attrs,$hinthash,$parstack,$safeeval)=@_;
662: if (not defined(@{ $Apache::response::conceptgroup{'names'} })) { return; }
663: my @names = @{ $Apache::response::conceptgroup{'names'} };
664: my $pick=int(&Math::Random::random_uniform() * ($#names+1));
665: my $name=$names[$pick];
666: push @{ $Apache::response::foilgroup{'names'} }, $name;
667: foreach my $attr (@$attrs) {
668: $Apache::response::foilgroup{"$name.".$attr} =
669: $Apache::response::conceptgroup{"$name.".$attr};
670: }
671: my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
672: $Apache::response::foilgroup{"$name.concept"} = $concept;
673: &Apache::lonxml::debug("Selecting $name in $concept");
674: my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
675: if ($target eq 'analyze') {
676: push (@{ $Apache::lonhomework::analyze{"$part_id.concepts"} },
677: $concept);
678: $Apache::lonhomework::analyze{"$part_id.concept.$concept"}=
679: $Apache::response::conceptgroup{'names'};
680: foreach my $name (@{ $Apache::response::conceptgroup{'names'} }) {
681: push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },
682: $name);
683: foreach my $attr (@$attrs) {
684: $Apache::lonhomework::analyze{"$part_id.foil.$attr.$name"}=
685: $Apache::response::conceptgroup{"$name.$attr"};
686: }
687: }
688: }
689: push(@{ $hinthash->{"$part_id.concepts"} },$concept);
690: $hinthash->{"$part_id.concept.$concept"}=
691: $Apache::response::conceptgroup{'names'};
692:
693: }
694:
1.95 albertel 695: sub get_response_param {
696: my ($id,$name,$default)=@_;
697: my $parameter;
698: if ($ENV{'request.state'} eq 'construct' &&
699: defined($Apache::inputtags::params{$name})) {
700: $parameter=$Apache::inputtags::params{$name};
701: } else {
702: $parameter=&Apache::lonnet::EXT("resource.$id.$name");
703: }
704: if (!defined($parameter) || $parameter eq '') {
705: $parameter = $default;
706: }
707: return $parameter;
708: }
1.87 albertel 709:
1.1 albertel 710: 1;
711: __END__
1.38 albertel 712:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>