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