1: # The LearningOnline Network with CAPA
2: # Full matching style response
3: #
4: # $Id: matchresponse.pm,v 1.40 2004/04/08 14:22:48 sakharuk Exp $
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: #
28:
29: package Apache::matchresponse;
30: use strict;
31: use HTML::Entities();
32: use Math::Random();
33: use Apache::optionresponse();
34: use Apache::lonlocal;
35:
36: BEGIN {
37: &Apache::lonxml::register('Apache::matchresponse',('matchresponse'));
38: }
39:
40: sub start_matchresponse {
41: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
42: my $result;
43: #when in a matchresponse use these
44: &Apache::lonxml::register('Apache::matchresponse',
45: ('foilgroup','foil','conceptgroup','item',
46: 'itemgroup'));
47: push (@Apache::lonxml::namespace,'matchresponse');
48: my $id = &Apache::response::start_response($parstack,$safeeval);
49: %Apache::hint::match=();
50: undef(%Apache::response::foilnames);
51: if ($target eq 'meta') {
52: $result=&Apache::response::meta_package_write('matchresponse');
53: } elsif ($target eq 'edit' ) {
54: $result.=&Apache::edit::start_table($token).
55: '<tr><td>'.&Apache::lonxml::description($token)."</td><td>Delete:".
56: &Apache::edit::deletelist($target,$token)
57: ."</td><td> ".&Apache::edit::end_row()
58: .&Apache::edit::start_spanning_row();
59:
60: $result.=
61: &Apache::edit::text_arg('Max Number Of Shown Foils:','max',$token,'4').
62: &Apache::edit::select_arg('Randomize Foil Order','randomize',
63: ['yes','no'],$token).
64: &Apache::edit::end_row().&Apache::edit::start_spanning_row()."\n";
65: } elsif ($target eq 'modified') {
66: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
67: $safeeval,'max',
68: 'randomize');
69: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
70: } elsif ($target eq 'analyze') {
71: my $part_id="$Apache::inputtags::part.$id";
72: push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
73: }
74: return $result;
75: }
76:
77: sub end_matchresponse {
78: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
79: my $result;
80: if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
81: &Apache::response::end_response;
82: pop @Apache::lonxml::namespace;
83: &Apache::lonxml::deregister('Apache::matchresponse',
84: ('foilgroup','foil','conceptgroup'));
85: undef(%Apache::response::foilnames);
86: return $result;
87: }
88:
89: %Apache::response::itemgroup=();
90: sub start_itemgroup {
91: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
92: my $result;
93: %Apache::response::itemgroup=();
94: %Apache::matchresponse::itemtable=();
95:
96: if ($target eq 'edit') {
97: $result=&Apache::edit::tag_start($target,$token);
98: $result.=&Apache::edit::select_arg('Randomize Order:','randomize',
99: ['yes','no'],$token);
100: $result.=&Apache::edit::select_arg('Items Display Location:',
101: 'location',
102: ['top','bottom','left','right'],
103: $token);
104: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
105: } elsif ($target eq 'modified') {
106: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
107: $safeeval,'randomize',
108: 'location');
109: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
110: } elsif ($target eq 'web' or $target eq 'tex') {
111: $Apache::matchresponse::itemtable{'location'}=
112: &Apache::lonxml::get_param('location',$parstack,$safeeval);
113: }
114: return $result;
115: }
116:
117: sub end_itemgroup {
118: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
119: my $result;
120:
121: if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
122: if (!defined(@{ $Apache::response::itemgroup{'names'} })) { return; }
123: my @names=@{ $Apache::response::itemgroup{'names'} };
124: my $randomize =&Apache::lonxml::get_param('randomize',$parstack,$safeeval);
125: if ($randomize ne 'no' ) {
126: @names=&Apache::response::whichorder($#names+1,$randomize,0,
127: \%Apache::response::itemgroup);
128: }
129: if ($target eq 'analyze') {
130: my $partid="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
131: push (@{ $Apache::lonhomework::analyze{"$partid.items"} }, @names);
132: }
133: my %letter_name_map;
134: my %name_letter_map;
135: my @alphabet=('A'..'Z');
136: my $i=0;
137: foreach my $name (@names) {
138: $letter_name_map{$alphabet[$i]}=$name;
139: $name_letter_map{$name}=$alphabet[$i];
140: $i++;
141: }
142: $Apache::response::itemgroup{'letter_name_map'}=\%letter_name_map;
143: $Apache::response::itemgroup{'name_letter_map'}=\%name_letter_map;
144: if ($target eq 'web') {
145: my $table='<table>';
146: my $i=0;
147: foreach my $name (@names) {
148: $table.='<tr><td>'.$alphabet[$i].'</td><td>'.
149: $Apache::response::itemgroup{$name.'.text'}.
150: '</td></tr>';
151: $i++;
152: }
153: $table.='</table>';
154: $Apache::matchresponse::itemtable{'display'}=$table;
155: } elsif ($target eq 'tex') {
156: my $table=' \begin{description}\setlength{\leftmargin}{2em}\setlength{\labelwidth}{1em}\setlength{\itemsep}{0.5pt plus1pt minus2pt}\setlength{\listparindent}{0em} ';
157: my $i=0;
158: foreach my $name (@names) {
159: $Apache::response::itemgroup{$name.'.text'}=~s/\$\$/\$/g;
160: $table.='\item['.$alphabet[$i].'] '.
161: $Apache::response::itemgroup{$name.'.text'};
162: $i++;
163: }
164: $table.=' \end{description} \strut ';
165: if ($Apache::lonhomework::type eq 'exam') {$table.='\vskip -13 mm \strut ';}
166: $Apache::matchresponse::itemtable{'display'}=$table;
167: }
168: return $result;
169: }
170:
171: sub start_item {
172: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
173: my $result='';
174: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
175: &Apache::lonxml::startredirection;
176: } elsif ($target eq 'edit') {
177: my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
178: $safeeval,'-2');
179: $result=&Apache::edit::tag_start($target,$token,"Item");
180: $result.=&Apache::edit::text_arg('Name:','name',$token);
181: if ($randomize ne 'no') {
182: $result.=&Apache::edit::select_arg('Location:','location',
183: ['random','top','bottom'],
184: $token);
185: }
186: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
187: } elsif ($target eq 'modified') {
188: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
189: $safeeval,'name',
190: 'location');
191: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
192: }
193: return $result;
194: }
195:
196: sub end_item {
197: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
198: my $text ='';
199: my $result = '';
200: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
201: $text=&Apache::lonxml::endredirection;
202: }
203: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
204: $target eq 'edit' || $target eq 'tex' || $target eq 'analyze') {
205: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
206: my $location=&Apache::lonxml::get_param('location',$parstack,
207: $safeeval);
208: &Apache::lonxml::debug("Got a name of :$name:");
209: if (!$name) { $name=$Apache::lonxml::curdepth; }
210: &Apache::lonxml::debug("Using a name of :$name:");
211: push @{ $Apache::response::itemgroup{'names'} }, $name;
212: $Apache::response::itemgroup{"$name.text"} = $text;
213: $Apache::response::itemgroup{"$name.location"} = $location;
214: }
215: if ($target eq 'edit') {
216: $result.= &Apache::edit::tag_end($target,$token,'');
217: }
218: return $result;
219: }
220:
221: sub insert_item {
222: return '
223: <item name="">
224: <startouttext />
225: <endouttext />
226: </item>';
227: }
228:
229: %Apache::response::foilgroup=();
230: sub start_foilgroup {
231: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
232: my $result;
233: %Apache::response::foilgroup=();
234: $Apache::matchresponse::conceptgroup=0;
235: &Apache::response::setrandomnumber();
236: if ($target eq 'edit') {
237: $result.=&Apache::edit::start_table($token)
238: ."<tr><td>Collection Of Foils</td><td>Delete:"
239: .&Apache::edit::deletelist($target,$token)
240: ."</td><td> ".&Apache::edit::end_row()
241: .&Apache::edit::start_spanning_row()."\n";
242: }
243: return $result;
244: }
245:
246: sub end_foilgroup {
247: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
248: my $result;
249: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || $target eq 'tex' || $target eq 'analyze') {
250: my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
251: my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
252: $safeeval,'-2');
253: if ($target eq 'web' || $target eq 'tex') {
254: $result=&displayfoils($target,$max,$randomize);
255: } elsif ($target eq 'answer' ) {
256: $result=&displayanswers($max,$randomize);
257: } elsif ( $target eq 'grade') {
258: &grade_response($max,$randomize);
259: } elsif ( $target eq 'analyze') {
260: my @shown=&whichfoils($max,$randomize);
261: &Apache::response::analyze_store_foilgroup(\@shown,
262: ['text','value','location']);
263: #FIXME need to store options in some way
264: }
265: &Apache::lonxml::increment_counter(&getfoilcounts($max));
266: } elsif ($target eq 'edit') {
267: $result=&Apache::edit::end_table();
268: }
269: return $result;
270: }
271:
272: sub whichfoils {
273: my ($max,$randomize)=@_;
274: return &Apache::response::whichorder(&getfoilcounts($max),
275: $randomize,
276: &Apache::response::showallfoils(),
277: \%Apache::response::foilgroup);
278: }
279:
280: sub displayanswers {
281: my ($max,$randomize,@opt)=@_;
282: if (!defined(@{ $Apache::response::foilgroup{'names'} })) { return; }
283: my @names = @{ $Apache::response::foilgroup{'names'} };
284: my @whichfoils = &Apache::response::whichorder(&getfoilcounts($max),
285: $randomize,
286: &Apache::response::showallfoils(),
287: \%Apache::response::foilgroup);
288: my $result=&Apache::response::answer_header('matchresponse');
289: my %name_letter_map;
290: if (defined(%{ $Apache::response::itemgroup{'name_letter_map'} })) {
291: %name_letter_map=
292: %{ $Apache::response::itemgroup{'name_letter_map'} };
293: }
294: foreach my $name (@whichfoils) {
295: my $value_name=$Apache::response::foilgroup{$name.'.value'};
296: my $letter=$name_letter_map{$value_name};
297: $result.=&Apache::response::answer_part('matchresponse',$letter);
298: }
299: $result.=&Apache::response::answer_footer('matchresponse');
300: return $result;
301: }
302:
303:
304: sub grade_response {
305: my ($max,$randomize)=@_;
306: my (@whichfoils)=&Apache::response::whichorder(&getfoilcounts($max),
307: $randomize,
308: &Apache::response::showallfoils(),
309: \%Apache::response::foilgroup);
310: if (!defined($ENV{'form.submitted'})) { return; }
311: my %responsehash;
312: my %grade;
313: my ($temp,$right,$wrong,$ignored)=(1,0,0,0);
314: my %letter_name_map;
315: if (defined(%{ $Apache::response::itemgroup{'letter_name_map'} })) {
316: %letter_name_map=
317: %{ $Apache::response::itemgroup{'letter_name_map'} };
318: }
319: my @items;
320: foreach my $name (@whichfoils) {
321: my $response = &Apache::response::getresponse($temp,'letter');
322: push(@items,$response);
323: my $responsename = $letter_name_map{$response};
324: my $value=$Apache::response::foilgroup{$name.'.value'};
325: if ( $response =~ /[^\s]/) {
326: $responsehash{$name}=$responsename;
327: &Apache::lonxml::debug("submitted a $response($responsename) for $value<br />\n");
328: if ($value eq $responsename) {
329: $grade{$name}='1'; $right++;
330: } else {
331: $grade{$name}='0'; $wrong++;
332: }
333: } else {
334: $ignored++;
335: }
336: $temp++;
337: }
338: my $part=$Apache::inputtags::part;
339: my $id = $Apache::inputtags::response['-1'];
340: my $responsestr=&Apache::lonnet::hash2str(%responsehash);
341: my $itemstr =&Apache::lonnet::array2str(@items);
342: my $gradestr =&Apache::lonnet::hash2str(%grade);
343: my %previous=&Apache::response::check_for_previous($responsestr,$part,$id);
344: &Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored ");
345: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
346: $responsestr;
347: $Apache::lonhomework::results{"resource.$part.$id.submissionitems"}=
348: $itemstr;
349: $Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=
350: $gradestr;
351: if (!$Apache::lonhomework::scantronmode) {
352: my $ad;
353: if ($wrong==0 && $ignored==0) {
354: $ad='EXACT_ANS';
355: } elsif ($wrong==0 && $right==0) {
356: #nothing submitted
357: } else {
358: if ($ignored==0) {
359: $ad='INCORRECT';
360: } else {
361: $ad='MISSING_ANSWER';
362: }
363: }
364: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
365: &Apache::response::handle_previous(\%previous,$ad);
366: } else {
367: my $ad;
368: if ($wrong==0 && $right==0) {
369: #nothing submitted
370: } else {
371: $ad='ASSIGNED_SCORE';
372: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
373: $ad;
374: $Apache::lonhomework::results{"resource.$part.$id.awarded"}=
375: $right/(scalar(@whichfoils));
376: $Apache::lonhomework::results{"resource.$part.$id.numfoils"}=
377: scalar(@whichfoils);
378: }
379: }
380: }
381:
382: sub itemdisplay {
383: my ($location)=@_;
384: if ($location eq 'top' &&
385: !defined($Apache::matchresponse::itemtable{'location'})) {
386: return $Apache::matchresponse::itemtable{'display'};
387: }
388: if ($Apache::matchresponse::itemtable{'location'} eq $location) {
389: return $Apache::matchresponse::itemtable{'display'};
390: }
391: return undef;
392: }
393: sub displayfoils {
394: my ($target,$max,$randomize)=@_;
395: my $result;
396: my $question;
397: my (@whichfoils)=&Apache::response::whichorder(&getfoilcounts($max),
398: $randomize,
399: &Apache::response::showallfoils(),
400: \%Apache::response::foilgroup);
401: my $part=$Apache::inputtags::part;
402: my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
403: my %letter_name_map;
404: if (defined(%{ $Apache::response::itemgroup{'letter_name_map'} })) {
405: %letter_name_map=
406: %{ $Apache::response::itemgroup{'letter_name_map'} };
407: }
408: my %name_letter_map;
409: if (defined(%{ $Apache::response::itemgroup{'name_letter_map'} })) {
410: %name_letter_map=
411: %{ $Apache::response::itemgroup{'name_letter_map'} };
412: }
413: if ( &Apache::response::show_answer() && ($target ne 'tex')) {
414: foreach my $name (@whichfoils) {
415: my $text=$Apache::response::foilgroup{$name.'.text'};
416: my $value=$Apache::response::foilgroup{$name.'.value'};
417: my $letter=$name_letter_map{$value};
418: if ($target eq 'tex') {
419: $question.=' \\\\ '.$letter.':'.$text;
420: } else {
421: $question.='<br />'.$letter.':'.$text;
422: }
423: }
424: } else {
425: my $i = 0;
426: my $temp=1;
427: my $id=$Apache::inputtags::response[-1];
428: my $part=$Apache::inputtags::part;
429: my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
430: my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
431: my @alphabet=('A'..'Z');
432: my @used_letters=sort(keys(%letter_name_map));
433: my $internal_counter=$Apache::lonxml::counter;
434: foreach my $name (@whichfoils) {
435: my $lastopt=$lastresponse{$name};
436: my $last_letter=$name_letter_map{$lastopt};
437: my $optionlist = '';
438: if ($target ne 'tex') {
439: $optionlist="<option></option>\n";
440: } else {
441: if ($Apache::lonhomework::type ne 'exam') {
442: $optionlist='\framebox[5 mm][s]{\tiny\strut}';
443: }
444: }
445: my $option;
446: foreach $option (@used_letters) {
447: if ($option eq $last_letter) {
448: if ($target ne 'tex') {$optionlist.="<option selected=\"on\">$option</option>\n";}
449: } else {
450: if ($target ne 'tex') {$optionlist.="<option>$option</option>\n";}
451: }
452: }
453: if ($target ne 'tex' && $Apache::lonhomework::type ne 'exam') {
454: $optionlist='<select name="HWVAL_'.
455: $Apache::inputtags::response[-1].':'.$temp.'">'.
456: $optionlist."</select>\n";
457: }
458: my $text=$Apache::response::foilgroup{$name.'.text'};
459: if ($target ne 'tex') {
460: if ($Apache::lonhomework::type ne 'exam') {
461: $question.='<br />'.$optionlist.$text."\n";
462: } else {
463: $question.='<br />'.$text."\n";
464: }
465: if ($Apache::lonhomework::type eq 'exam') {
466: $question.=&Apache::optionresponse::webbubbles(\@used_letters,\@used_letters,$temp,$last_letter);
467: }
468: } else {
469: if ($Apache::lonhomework::type eq 'exam') {
470: $question.=' '.$optionlist.$text."\n";
471: my @emptyItems = ();
472: for (my $i=0;$i<=$#used_letters;$i++) {push @emptyItems, ' ';}
473: $question.='\vskip -1 mm\noindent\begin{enumerate}\item[\textbf{'.$internal_counter.'}.]'.&Apache::optionresponse::bubbles(\@used_letters,\@emptyItems).'\end{enumerate} \vskip -8 mm \strut ';
474: $internal_counter++;
475: } else {
476: $question.=' '.$optionlist.$text.'\strut\\\\\strut '."\n";
477: }
478: }
479: $temp++;
480: }
481: }
482: if ($result=&itemdisplay('top')) {
483: $result.=$question;
484: } elsif ($result=&itemdisplay('bottom')) {
485: $result=$question.$result;
486: } elsif ($result=&itemdisplay('right')) {
487: if ($target ne 'tex') {
488: $result='<table><tr><td>'.$question.'</td><td>'.$result.
489: '</td></tr></table>';
490: } else {
491: my $tabsize=&Apache::londefdef::recalc($ENV{'form.textwidth'});
492: $tabsize=~/(\d+\.?\d*)/;
493: $tabsize=$1/2.1;
494: $tabsize.=' mm ';
495: $result='\setlength{\tabcolsep}{1 mm}\begin{tabular}{p{'.$tabsize.'}p{'.$tabsize.'}}\begin{minipage}{'.$tabsize.'}'.$question.'\end{minipage}&\begin{minipage}{'.$tabsize.'}'.$result.'\end{minipage}\end{tabular}';
496: }
497: } elsif ($result=&itemdisplay('left')) {
498: if ($target ne 'tex') {
499: $result='<table><tr><td>'.$result.'</td><td>'.$question.
500: '</td></tr></table>';
501: } else {
502: my $tabsize=&Apache::londefdef::recalc($ENV{'form.textwidth'});
503: $tabsize=~/(\d+\.?\d*)/;
504: $tabsize=$1/2.1;
505: $tabsize.=' mm ';
506: $result='\setlength{\tabcolsep}{1 mm}\begin{tabular}{p{'.$tabsize.'}p{'.$tabsize.'}}\begin{minipage}{'.$tabsize.'}'.$result.'\end{minipage}&\begin{minipage}{'.$tabsize.'}'.$question.'\end{minipage}\end{tabular}';
507: }
508: }
509: if ($target ne 'tex') {$result.="<br />";} else {$result.=' \\\\ ';}
510: return $result;
511: }
512:
513: sub getfoilcounts {
514: my ($max)=@_;
515: # +1 since instructors will count from 1
516: my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
517: if (&Apache::response::showallfoils()) { $max=$count; }
518: if ($count>$max) { $count=$max }
519: &Apache::lonxml::debug("Count is $count from $max");
520: return $count;
521: }
522:
523:
524: sub start_conceptgroup {
525: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
526: $Apache::matchresponse::conceptgroup=1;
527: %Apache::response::conceptgroup=();
528: my $result;
529: if ($target eq 'edit') {
530: $result.=&Apache::edit::tag_start($target,$token,
531: "Concept Grouped Foils");
532: $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
533: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
534: }
535: if ($target eq 'modified') {
536: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
537: $safeeval,'concept');
538: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
539: }
540: return $result;
541: }
542:
543: sub end_conceptgroup {
544: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
545: $Apache::matchresponse::conceptgroup=0;
546: my $result='';
547: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
548: $target eq 'tex' || $target eq 'analyze') {
549: &Apache::response::pick_foil_for_concept($target,
550: ['value','text','location'],
551: \%Apache::hint::match,
552: $parstack,$safeeval);
553: } elsif ($target eq 'edit') {
554: $result=&Apache::edit::end_table();
555: }
556: return $result;
557: }
558:
559: sub insert_conceptgroup {
560: my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
561: return $result;
562: }
563:
564: sub start_foil {
565: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
566: my $result='';
567: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
568: &Apache::lonxml::startredirection;
569: } elsif ($target eq 'edit') {
570: $result=&Apache::edit::tag_start($target,$token,"Foil");
571: my $level='-2';
572: if ($$tagstack[-2] eq 'conceptgroup') { $level = '-3'; }
573: $result.=&Apache::edit::text_arg('Name:','name',$token);
574: my @names;
575: if (defined(@{ $Apache::response::itemgroup{'names'} })) {
576: @names=@{ $Apache::response::itemgroup{'names'} };
577: }
578: $result.= &Apache::edit::select_or_text_arg('Correct Option:','value',['unused',@names],$token,'15');
579: my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
580: $safeeval,'-3');
581: if ($randomize ne 'no') {
582: $result.=&Apache::edit::select_arg('Location:','location',
583: ['random','top','bottom'],$token);
584: }
585: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
586: } elsif ($target eq 'modified') {
587: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
588: $safeeval,'value',
589: 'name','location');
590: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
591: }
592: return $result;
593: }
594:
595: sub end_foil {
596: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
597: my $text ='';
598: my $result = '';
599: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
600: $text=&Apache::lonxml::endredirection;
601: }
602: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
603: $target eq 'tex' || $target eq 'analyze') {
604: my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
605: if ($value ne 'unused') {
606: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
607: &Apache::lonxml::debug("Got a name of :$name:");
608: if (!$name) { $name=$Apache::lonxml::curdepth; }
609: &Apache::lonxml::debug("Using a name of :$name:");
610: if (defined($Apache::response::foilnames{$name})) {
611: &Apache::lonxml::error(&mt("Foil name <b><tt>[_1]</tt></b> appears more than once. Foil names need to be unique.",$name));
612: }
613: $Apache::response::foilnames{$name}++;
614: my $location =&Apache::lonxml::get_param('location',$parstack,
615: $safeeval);
616: if ( $Apache::matchresponse::conceptgroup
617: && !&Apache::response::showallfoils() ) {
618: push @{ $Apache::response::conceptgroup{'names'} }, $name;
619: $Apache::response::conceptgroup{"$name.value"} = $value;
620: if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
621: $Apache::response::conceptgroup{"$name.text"} = ' $\triangleright$ '.$text;
622: } else {
623: $Apache::response::conceptgroup{"$name.text"} = $text;
624: }
625: $Apache::response::conceptgroup{"$name.location"} = $location;
626: } else {
627: push @{ $Apache::response::foilgroup{'names'} }, $name;
628: $Apache::response::foilgroup{"$name.value"} = $value;
629: if ($Apache::lonhomework::type eq 'exam') {
630: if ($target eq 'tex') {
631: $Apache::response::foilgroup{"$name.text"} = '\vskip 5 mm $\triangleright$ '.$text;
632: } else {
633: $Apache::response::foilgroup{"$name.text"} = $text;
634: }
635: } else {
636: if ($target eq 'tex') {
637: $Apache::response::foilgroup{"$name.text"} = $text;
638: } else {
639: $Apache::response::foilgroup{"$name.text"} = $text;
640: }
641: }
642: $Apache::response::foilgroup{"$name.location"} = $location;
643: }
644: }
645: }
646: if ($target eq 'edit') {
647: $result.= &Apache::edit::tag_end($target,$token,'');
648: }
649: return $result;
650: }
651:
652: sub insert_foil {
653: return '
654: <foil name="" value="unused">
655: <startouttext />
656: <endouttext />
657: </foil>';
658: }
659: 1;
660: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>