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