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