1: # The LearningOnline Network with CAPA
2: # Full matching style response
3: #
4: # $Id: matchresponse.pm,v 1.42 2004/07/02 08:20:55 albertel 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::pushrandomnumber();
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: &Apache::response::poprandomnumber();
270: return $result;
271: }
272:
273: sub whichfoils {
274: my ($max,$randomize)=@_;
275: return &Apache::response::whichorder(&getfoilcounts($max),
276: $randomize,
277: &Apache::response::showallfoils(),
278: \%Apache::response::foilgroup);
279: }
280:
281: sub displayanswers {
282: my ($max,$randomize,@opt)=@_;
283: if (!defined(@{ $Apache::response::foilgroup{'names'} })) { return; }
284: my @names = @{ $Apache::response::foilgroup{'names'} };
285: my @whichfoils = &Apache::response::whichorder(&getfoilcounts($max),
286: $randomize,
287: &Apache::response::showallfoils(),
288: \%Apache::response::foilgroup);
289: my $result=&Apache::response::answer_header('matchresponse');
290: my %name_letter_map;
291: if (defined(%{ $Apache::response::itemgroup{'name_letter_map'} })) {
292: %name_letter_map=
293: %{ $Apache::response::itemgroup{'name_letter_map'} };
294: }
295: foreach my $name (@whichfoils) {
296: my $value_name=$Apache::response::foilgroup{$name.'.value'};
297: my $letter=$name_letter_map{$value_name};
298: $result.=&Apache::response::answer_part('matchresponse',$letter);
299: }
300: $result.=&Apache::response::answer_footer('matchresponse');
301: return $result;
302: }
303:
304:
305: sub grade_response {
306: my ($max,$randomize)=@_;
307: my (@whichfoils)=&Apache::response::whichorder(&getfoilcounts($max),
308: $randomize,
309: &Apache::response::showallfoils(),
310: \%Apache::response::foilgroup);
311: if (!defined($ENV{'form.submitted'})) { return; }
312: my %responsehash;
313: my %grade;
314: my ($temp,$right,$wrong,$ignored)=(1,0,0,0);
315: my %letter_name_map;
316: if (defined(%{ $Apache::response::itemgroup{'letter_name_map'} })) {
317: %letter_name_map=
318: %{ $Apache::response::itemgroup{'letter_name_map'} };
319: }
320: my @items;
321: foreach my $name (@whichfoils) {
322: my $response = &Apache::response::getresponse($temp,'letter');
323: push(@items,$response);
324: my $responsename = $letter_name_map{$response};
325: my $value=$Apache::response::foilgroup{$name.'.value'};
326: if ( $response =~ /[^\s]/) {
327: $responsehash{$name}=$responsename;
328: &Apache::lonxml::debug("submitted a $response($responsename) for $value<br />\n");
329: if ($value eq $responsename) {
330: $grade{$name}='1'; $right++;
331: } else {
332: $grade{$name}='0'; $wrong++;
333: }
334: } else {
335: $ignored++;
336: }
337: $temp++;
338: }
339: my $part=$Apache::inputtags::part;
340: my $id = $Apache::inputtags::response['-1'];
341: my $responsestr=&Apache::lonnet::hash2str(%responsehash);
342: my $itemstr =&Apache::lonnet::array2str(@items);
343: my $gradestr =&Apache::lonnet::hash2str(%grade);
344: my %previous=&Apache::response::check_for_previous($responsestr,$part,$id);
345: &Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored ");
346: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
347: $responsestr;
348: $Apache::lonhomework::results{"resource.$part.$id.submissionitems"}=
349: $itemstr;
350: $Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=
351: $gradestr;
352: if (!$Apache::lonhomework::scantronmode) {
353: my $ad;
354: if ($wrong==0 && $ignored==0) {
355: $ad='EXACT_ANS';
356: } elsif ($wrong==0 && $right==0) {
357: #nothing submitted
358: } else {
359: if ($ignored==0) {
360: $ad='INCORRECT';
361: } else {
362: $ad='MISSING_ANSWER';
363: }
364: }
365: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
366: &Apache::response::handle_previous(\%previous,$ad);
367: } else {
368: my $ad;
369: if ($wrong==0 && $right==0) {
370: #nothing submitted
371: } else {
372: $ad='ASSIGNED_SCORE';
373: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
374: $ad;
375: $Apache::lonhomework::results{"resource.$part.$id.awarded"}=
376: $right/(scalar(@whichfoils));
377: $Apache::lonhomework::results{"resource.$part.$id.numfoils"}=
378: scalar(@whichfoils);
379: }
380: }
381: }
382:
383: sub itemdisplay {
384: my ($location)=@_;
385: if ($location eq 'top' &&
386: !defined($Apache::matchresponse::itemtable{'location'})) {
387: return $Apache::matchresponse::itemtable{'display'};
388: }
389: if ($Apache::matchresponse::itemtable{'location'} eq $location) {
390: return $Apache::matchresponse::itemtable{'display'};
391: }
392: return undef;
393: }
394: sub displayfoils {
395: my ($target,$max,$randomize)=@_;
396: my $result;
397: my $question;
398: my (@whichfoils)=&Apache::response::whichorder(&getfoilcounts($max),
399: $randomize,
400: &Apache::response::showallfoils(),
401: \%Apache::response::foilgroup);
402: my $part=$Apache::inputtags::part;
403: my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
404: my %letter_name_map;
405: if (defined(%{ $Apache::response::itemgroup{'letter_name_map'} })) {
406: %letter_name_map=
407: %{ $Apache::response::itemgroup{'letter_name_map'} };
408: }
409: my %name_letter_map;
410: if (defined(%{ $Apache::response::itemgroup{'name_letter_map'} })) {
411: %name_letter_map=
412: %{ $Apache::response::itemgroup{'name_letter_map'} };
413: }
414: if ( &Apache::response::show_answer() && ($target ne 'tex')) {
415: foreach my $name (@whichfoils) {
416: my $text=$Apache::response::foilgroup{$name.'.text'};
417: my $value=$Apache::response::foilgroup{$name.'.value'};
418: my $letter=$name_letter_map{$value};
419: if ($target eq 'tex') {
420: $question.=' \\\\ '.$letter.':'.$text;
421: } else {
422: $question.='<br />'.$letter.':'.$text;
423: }
424: }
425: } else {
426: my $i = 0;
427: my $temp=1;
428: my $id=$Apache::inputtags::response[-1];
429: my $part=$Apache::inputtags::part;
430: my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
431: my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
432: my @alphabet=('A'..'Z');
433: my @used_letters=sort(keys(%letter_name_map));
434: my $internal_counter=$Apache::lonxml::counter;
435: foreach my $name (@whichfoils) {
436: my $lastopt=$lastresponse{$name};
437: my $last_letter=$name_letter_map{$lastopt};
438: my $optionlist = '';
439: if ($target ne 'tex') {
440: $optionlist="<option></option>\n";
441: } else {
442: if ($Apache::lonhomework::type ne 'exam') {
443: $optionlist='\framebox[5 mm][s]{\tiny\strut}';
444: }
445: }
446: my $option;
447: foreach $option (@used_letters) {
448: if ($option eq $last_letter) {
449: if ($target ne 'tex') {$optionlist.="<option selected=\"on\">$option</option>\n";}
450: } else {
451: if ($target ne 'tex') {$optionlist.="<option>$option</option>\n";}
452: }
453: }
454: if ($target ne 'tex' && $Apache::lonhomework::type ne 'exam') {
455: $optionlist='<select name="HWVAL_'.
456: $Apache::inputtags::response[-1].':'.$temp.'">'.
457: $optionlist."</select>\n";
458: }
459: my $text=$Apache::response::foilgroup{$name.'.text'};
460: if ($target ne 'tex') {
461: if ($Apache::lonhomework::type ne 'exam') {
462: $question.='<br />'.$optionlist.$text."\n";
463: } else {
464: $question.='<br />'.$text."\n";
465: }
466: if ($Apache::lonhomework::type eq 'exam') {
467: my @blank;
468: $question.=&Apache::optionresponse::webbubbles(\@used_letters,\@blank,$temp,$last_letter);
469: }
470: } else {
471: if ($Apache::lonhomework::type eq 'exam') {
472: $question.=' '.$optionlist.$text."\n";
473: my @emptyItems = ();
474: for (my $i=0;$i<=$#used_letters;$i++) {push @emptyItems, ' ';}
475: $question.='\vskip -1 mm\noindent\begin{enumerate}\item[\textbf{'.$internal_counter.'}.]'.&Apache::optionresponse::bubbles(\@used_letters,\@emptyItems).'\end{enumerate} \vskip -8 mm \strut ';
476: $internal_counter++;
477: } else {
478: $question.=' '.$optionlist.$text.'\strut\\\\\strut '."\n";
479: }
480: }
481: $temp++;
482: }
483: }
484: if ($result=&itemdisplay('top')) {
485: $result.=$question;
486: } elsif ($result=&itemdisplay('bottom')) {
487: $result=$question.$result;
488: } elsif ($result=&itemdisplay('right')) {
489: if ($target ne 'tex') {
490: $result='<table><tr><td>'.$question.'</td><td>'.$result.
491: '</td></tr></table>';
492: } else {
493: my $tabsize=&Apache::londefdef::recalc($ENV{'form.textwidth'});
494: $tabsize=~/(\d+\.?\d*)/;
495: $tabsize=$1/2.1;
496: $tabsize.=' mm ';
497: $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}';
498: }
499: } elsif ($result=&itemdisplay('left')) {
500: if ($target ne 'tex') {
501: $result='<table><tr><td>'.$result.'</td><td>'.$question.
502: '</td></tr></table>';
503: } else {
504: my $tabsize=&Apache::londefdef::recalc($ENV{'form.textwidth'});
505: $tabsize=~/(\d+\.?\d*)/;
506: $tabsize=$1/2.1;
507: $tabsize.=' mm ';
508: $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}';
509: }
510: }
511: if ($target ne 'tex') {$result.="<br />";} else {$result.=' \\\\ ';}
512: return $result;
513: }
514:
515: sub getfoilcounts {
516: my ($max)=@_;
517: # +1 since instructors will count from 1
518: my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
519: if (&Apache::response::showallfoils()) { $max=$count; }
520: if ($count>$max) { $count=$max }
521: &Apache::lonxml::debug("Count is $count from $max");
522: return $count;
523: }
524:
525:
526: sub start_conceptgroup {
527: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
528: $Apache::matchresponse::conceptgroup=1;
529: %Apache::response::conceptgroup=();
530: my $result;
531: if ($target eq 'edit') {
532: $result.=&Apache::edit::tag_start($target,$token,
533: "Concept Grouped Foils");
534: $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
535: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
536: }
537: if ($target eq 'modified') {
538: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
539: $safeeval,'concept');
540: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
541: }
542: return $result;
543: }
544:
545: sub end_conceptgroup {
546: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
547: $Apache::matchresponse::conceptgroup=0;
548: my $result='';
549: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
550: $target eq 'tex' || $target eq 'analyze') {
551: &Apache::response::pick_foil_for_concept($target,
552: ['value','text','location'],
553: \%Apache::hint::match,
554: $parstack,$safeeval);
555: } elsif ($target eq 'edit') {
556: $result=&Apache::edit::end_table();
557: }
558: return $result;
559: }
560:
561: sub insert_conceptgroup {
562: my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
563: return $result;
564: }
565:
566: sub start_foil {
567: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
568: my $result='';
569: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
570: &Apache::lonxml::startredirection;
571: } elsif ($target eq 'edit') {
572: $result=&Apache::edit::tag_start($target,$token,"Foil");
573: my $level='-2';
574: if ($$tagstack[-2] eq 'conceptgroup') { $level = '-3'; }
575: $result.=&Apache::edit::text_arg('Name:','name',$token);
576: my @names;
577: if (defined(@{ $Apache::response::itemgroup{'names'} })) {
578: @names=@{ $Apache::response::itemgroup{'names'} };
579: }
580: $result.= &Apache::edit::select_or_text_arg('Correct Option:','value',['unused',@names],$token,'15');
581: my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
582: $safeeval,'-3');
583: if ($randomize ne 'no') {
584: $result.=&Apache::edit::select_arg('Location:','location',
585: ['random','top','bottom'],$token);
586: }
587: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
588: } elsif ($target eq 'modified') {
589: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
590: $safeeval,'value',
591: 'name','location');
592: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
593: }
594: return $result;
595: }
596:
597: sub end_foil {
598: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
599: my $text ='';
600: my $result = '';
601: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
602: $text=&Apache::lonxml::endredirection;
603: }
604: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
605: $target eq 'tex' || $target eq 'analyze') {
606: my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
607: if ($value ne 'unused') {
608: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
609: &Apache::lonxml::debug("Got a name of :$name:");
610: if (!$name) { $name=$Apache::lonxml::curdepth; }
611: &Apache::lonxml::debug("Using a name of :$name:");
612: if (defined($Apache::response::foilnames{$name})) {
613: &Apache::lonxml::error(&mt("Foil name <b><tt>[_1]</tt></b> appears more than once. Foil names need to be unique.",$name));
614: }
615: $Apache::response::foilnames{$name}++;
616: my $location =&Apache::lonxml::get_param('location',$parstack,
617: $safeeval);
618: if ( $Apache::matchresponse::conceptgroup
619: && !&Apache::response::showallfoils() ) {
620: push @{ $Apache::response::conceptgroup{'names'} }, $name;
621: $Apache::response::conceptgroup{"$name.value"} = $value;
622: if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
623: $Apache::response::conceptgroup{"$name.text"} = ' $\triangleright$ '.$text;
624: } else {
625: $Apache::response::conceptgroup{"$name.text"} = $text;
626: }
627: $Apache::response::conceptgroup{"$name.location"} = $location;
628: } else {
629: push @{ $Apache::response::foilgroup{'names'} }, $name;
630: $Apache::response::foilgroup{"$name.value"} = $value;
631: if ($Apache::lonhomework::type eq 'exam') {
632: if ($target eq 'tex') {
633: $Apache::response::foilgroup{"$name.text"} = '\vskip 5 mm $\triangleright$ '.$text;
634: } else {
635: $Apache::response::foilgroup{"$name.text"} = $text;
636: }
637: } else {
638: if ($target eq 'tex') {
639: $Apache::response::foilgroup{"$name.text"} = $text;
640: } else {
641: $Apache::response::foilgroup{"$name.text"} = $text;
642: }
643: }
644: $Apache::response::foilgroup{"$name.location"} = $location;
645: }
646: }
647: }
648: if ($target eq 'edit') {
649: $result.= &Apache::edit::tag_end($target,$token,'');
650: }
651: return $result;
652: }
653:
654: sub insert_foil {
655: return '
656: <foil name="" value="unused">
657: <startouttext />
658: <endouttext />
659: </foil>';
660: }
661: 1;
662: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>