Annotation of loncom/homework/matchresponse.pm, revision 1.41
1.1 albertel 1: # The LearningOnline Network with CAPA
2: # Full matching style response
3: #
1.41 ! albertel 4: # $Id: matchresponse.pm,v 1.40 2004/04/08 14:22:48 sakharuk Exp $
1.1 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: #
28:
29: package Apache::matchresponse;
30: use strict;
31: use HTML::Entities();
32: use Math::Random();
1.37 albertel 33: use Apache::optionresponse();
34: use Apache::lonlocal;
1.6 sakharuk 35:
1.1 albertel 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=();
1.37 albertel 50: undef(%Apache::response::foilnames);
1.1 albertel 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); }
1.28 albertel 70: } elsif ($target eq 'analyze') {
71: my $part_id="$Apache::inputtags::part.$id";
72: push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
1.1 albertel 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'));
1.37 albertel 85: undef(%Apache::response::foilnames);
1.1 albertel 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=();
1.4 albertel 94: %Apache::matchresponse::itemtable=();
1.15 albertel 95:
1.1 albertel 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);
1.4 albertel 100: $result.=&Apache::edit::select_arg('Items Display Location:',
101: 'location',
102: ['top','bottom','left','right'],
103: $token);
1.1 albertel 104: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.4 albertel 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); }
1.21 sakharuk 110: } elsif ($target eq 'web' or $target eq 'tex') {
1.4 albertel 111: $Apache::matchresponse::itemtable{'location'}=
112: &Apache::lonxml::get_param('location',$parstack,$safeeval);
1.1 albertel 113: }
114: return $result;
115: }
116:
117: sub end_itemgroup {
118: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
119: my $result;
120:
1.17 albertel 121: if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
1.1 albertel 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);
1.2 albertel 125: if ($randomize ne 'no' ) {
1.18 albertel 126: @names=&Apache::response::whichorder($#names+1,$randomize,0,
127: \%Apache::response::itemgroup);
1.1 albertel 128: }
1.30 albertel 129: if ($target eq 'analyze') {
130: my $partid="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
131: push (@{ $Apache::lonhomework::analyze{"$partid.items"} }, @names);
132: }
1.1 albertel 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: }
1.15 albertel 142: $Apache::response::itemgroup{'letter_name_map'}=\%letter_name_map;
1.1 albertel 143: $Apache::response::itemgroup{'name_letter_map'}=\%name_letter_map;
144: if ($target eq 'web') {
1.4 albertel 145: my $table='<table>';
1.1 albertel 146: my $i=0;
147: foreach my $name (@names) {
1.4 albertel 148: $table.='<tr><td>'.$alphabet[$i].'</td><td>'.
1.1 albertel 149: $Apache::response::itemgroup{$name.'.text'}.
150: '</td></tr>';
151: $i++;
152: }
1.4 albertel 153: $table.='</table>';
154: $Apache::matchresponse::itemtable{'display'}=$table;
1.5 sakharuk 155: } elsif ($target eq 'tex') {
1.39 sakharuk 156: my $table=' \begin{description}\setlength{\leftmargin}{2em}\setlength{\labelwidth}{1em}\setlength{\itemsep}{0.5pt plus1pt minus2pt}\setlength{\listparindent}{0em} ';
1.5 sakharuk 157: my $i=0;
158: foreach my $name (@names) {
1.11 sakharuk 159: $Apache::response::itemgroup{$name.'.text'}=~s/\$\$/\$/g;
1.19 sakharuk 160: $table.='\item['.$alphabet[$i].'] '.
1.21 sakharuk 161: $Apache::response::itemgroup{$name.'.text'};
1.5 sakharuk 162: $i++;
163: }
1.40 sakharuk 164: $table.=' \end{description} \strut ';
165: if ($Apache::lonhomework::type eq 'exam') {$table.='\vskip -13 mm \strut ';}
1.5 sakharuk 166: $Apache::matchresponse::itemtable{'display'}=$table;
1.17 albertel 167: }
1.1 albertel 168: return $result;
169: }
170:
171: sub start_item {
172: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
173: my $result='';
1.30 albertel 174: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.1 albertel 175: &Apache::lonxml::startredirection;
176: } elsif ($target eq 'edit') {
1.3 albertel 177: my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
178: $safeeval,'-2');
1.1 albertel 179: $result=&Apache::edit::tag_start($target,$token,"Item");
180: $result.=&Apache::edit::text_arg('Name:','name',$token);
1.3 albertel 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();
1.1 albertel 187: } elsif ($target eq 'modified') {
188: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.3 albertel 189: $safeeval,'name',
190: 'location');
1.1 albertel 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 = '';
1.30 albertel 200: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.1 albertel 201: $text=&Apache::lonxml::endredirection;
202: }
203: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
1.30 albertel 204: $target eq 'edit' || $target eq 'tex' || $target eq 'analyze') {
1.1 albertel 205: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.3 albertel 206: my $location=&Apache::lonxml::get_param('location',$parstack,
207: $safeeval);
1.1 albertel 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;
1.3 albertel 213: $Apache::response::itemgroup{"$name.location"} = $location;
1.1 albertel 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;
1.41 ! albertel 235: &Apache::response::pushrandomnumber();
1.1 albertel 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;
1.28 albertel 249: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || $target eq 'tex' || $target eq 'analyze') {
1.1 albertel 250: my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
251: my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
252: $safeeval,'-2');
1.5 sakharuk 253: if ($target eq 'web' || $target eq 'tex') {
1.1 albertel 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);
1.28 albertel 259: } elsif ( $target eq 'analyze') {
260: my @shown=&whichfoils($max,$randomize);
261: &Apache::response::analyze_store_foilgroup(\@shown,
1.29 albertel 262: ['text','value','location']);
1.28 albertel 263: #FIXME need to store options in some way
1.1 albertel 264: }
1.20 sakharuk 265: &Apache::lonxml::increment_counter(&getfoilcounts($max));
1.1 albertel 266: } elsif ($target eq 'edit') {
267: $result=&Apache::edit::end_table();
268: }
1.41 ! albertel 269: &Apache::response::poprandomnumber();
1.1 albertel 270: return $result;
1.29 albertel 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);
1.1 albertel 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'} };
1.18 albertel 285: my @whichfoils = &Apache::response::whichorder(&getfoilcounts($max),
286: $randomize,
287: &Apache::response::showallfoils(),
288: \%Apache::response::foilgroup);
1.1 albertel 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)=@_;
1.18 albertel 307: my (@whichfoils)=&Apache::response::whichorder(&getfoilcounts($max),
308: $randomize,
309: &Apache::response::showallfoils(),
310: \%Apache::response::foilgroup);
1.1 albertel 311: if (!defined($ENV{'form.submitted'})) { return; }
312: my %responsehash;
313: my %grade;
1.33 albertel 314: my ($temp,$right,$wrong,$ignored)=(1,0,0,0);
1.1 albertel 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: }
1.30 albertel 320: my @items;
1.1 albertel 321: foreach my $name (@whichfoils) {
1.34 albertel 322: my $response = &Apache::response::getresponse($temp,'letter');
1.30 albertel 323: push(@items,$response);
1.1 albertel 324: my $responsename = $letter_name_map{$response};
325: my $value=$Apache::response::foilgroup{$name.'.value'};
326: if ( $response =~ /[^\s]/) {
1.31 albertel 327: $responsehash{$name}=$responsename;
1.33 albertel 328: &Apache::lonxml::debug("submitted a $response($responsename) for $value<br />\n");
1.1 albertel 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);
1.30 albertel 342: my $itemstr =&Apache::lonnet::array2str(@items);
1.1 albertel 343: my $gradestr =&Apache::lonnet::hash2str(%grade);
1.30 albertel 344: my %previous=&Apache::response::check_for_previous($responsestr,$part,$id);
1.1 albertel 345: &Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored ");
346: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
347: $responsestr;
1.30 albertel 348: $Apache::lonhomework::results{"resource.$part.$id.submissionitems"}=
349: $itemstr;
1.1 albertel 350: $Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=
351: $gradestr;
1.34 albertel 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"}=
1.35 albertel 376: $right/(scalar(@whichfoils));
1.34 albertel 377: $Apache::lonhomework::results{"resource.$part.$id.numfoils"}=
1.35 albertel 378: scalar(@whichfoils);
1.34 albertel 379: }
380: }
1.1 albertel 381: }
382:
1.4 albertel 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: }
1.1 albertel 394: sub displayfoils {
395: my ($target,$max,$randomize)=@_;
396: my $result;
1.4 albertel 397: my $question;
1.18 albertel 398: my (@whichfoils)=&Apache::response::whichorder(&getfoilcounts($max),
399: $randomize,
400: &Apache::response::showallfoils(),
401: \%Apache::response::foilgroup);
1.1 albertel 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: }
1.25 albertel 414: if ( &Apache::response::show_answer() && ($target ne 'tex')) {
1.1 albertel 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};
1.5 sakharuk 419: if ($target eq 'tex') {
420: $question.=' \\\\ '.$letter.':'.$text;
421: } else {
422: $question.='<br />'.$letter.':'.$text;
423: }
1.1 albertel 424: }
425: } else {
426: my $i = 0;
1.36 albertel 427: my $temp=1;
1.1 albertel 428: my $id=$Apache::inputtags::response[-1];
429: my $part=$Apache::inputtags::part;
430: my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
1.9 sakharuk 431: my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
432: my @alphabet=('A'..'Z');
1.15 albertel 433: my @used_letters=sort(keys(%letter_name_map));
1.20 sakharuk 434: my $internal_counter=$Apache::lonxml::counter;
1.1 albertel 435: foreach my $name (@whichfoils) {
436: my $lastopt=$lastresponse{$name};
437: my $last_letter=$name_letter_map{$lastopt};
1.5 sakharuk 438: my $optionlist = '';
1.7 sakharuk 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: }
1.1 albertel 446: my $option;
1.15 albertel 447: foreach $option (@used_letters) {
1.1 albertel 448: if ($option eq $last_letter) {
1.6 sakharuk 449: if ($target ne 'tex') {$optionlist.="<option selected=\"on\">$option</option>\n";}
1.1 albertel 450: } else {
1.6 sakharuk 451: if ($target ne 'tex') {$optionlist.="<option>$option</option>\n";}
1.1 albertel 452: }
453: }
1.19 sakharuk 454: if ($target ne 'tex' && $Apache::lonhomework::type ne 'exam') {
1.5 sakharuk 455: $optionlist='<select name="HWVAL_'.
456: $Apache::inputtags::response[-1].':'.$temp.'">'.
457: $optionlist."</select>\n";
458: }
1.1 albertel 459: my $text=$Apache::response::foilgroup{$name.'.text'};
1.5 sakharuk 460: if ($target ne 'tex') {
1.19 sakharuk 461: if ($Apache::lonhomework::type ne 'exam') {
462: $question.='<br />'.$optionlist.$text."\n";
463: } else {
464: $question.='<br />'.$text."\n";
465: }
1.6 sakharuk 466: if ($Apache::lonhomework::type eq 'exam') {
1.27 albertel 467: $question.=&Apache::optionresponse::webbubbles(\@used_letters,\@used_letters,$temp,$last_letter);
1.6 sakharuk 468: }
469: } else {
470: if ($Apache::lonhomework::type eq 'exam') {
471: $question.=' '.$optionlist.$text."\n";
1.13 sakharuk 472: my @emptyItems = ();
1.16 albertel 473: for (my $i=0;$i<=$#used_letters;$i++) {push @emptyItems, ' ';}
1.40 sakharuk 474: $question.='\vskip -1 mm\noindent\begin{enumerate}\item[\textbf{'.$internal_counter.'}.]'.&Apache::optionresponse::bubbles(\@used_letters,\@emptyItems).'\end{enumerate} \vskip -8 mm \strut ';
1.20 sakharuk 475: $internal_counter++;
1.6 sakharuk 476: } else {
1.22 sakharuk 477: $question.=' '.$optionlist.$text.'\strut\\\\\strut '."\n";
1.6 sakharuk 478: }
1.5 sakharuk 479: }
1.1 albertel 480: $temp++;
481: }
1.4 albertel 482: }
483: if ($result=&itemdisplay('top')) {
484: $result.=$question;
485: } elsif ($result=&itemdisplay('bottom')) {
486: $result=$question.$result;
487: } elsif ($result=&itemdisplay('right')) {
1.32 sakharuk 488: if ($target ne 'tex') {
489: $result='<table><tr><td>'.$question.'</td><td>'.$result.
490: '</td></tr></table>';
491: } else {
1.39 sakharuk 492: my $tabsize=&Apache::londefdef::recalc($ENV{'form.textwidth'});
493: $tabsize=~/(\d+\.?\d*)/;
494: $tabsize=$1/2.1;
495: $tabsize.=' mm ';
496: $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}';
1.32 sakharuk 497: }
1.4 albertel 498: } elsif ($result=&itemdisplay('left')) {
1.32 sakharuk 499: if ($target ne 'tex') {
500: $result='<table><tr><td>'.$result.'</td><td>'.$question.
501: '</td></tr></table>';
502: } else {
1.39 sakharuk 503: my $tabsize=&Apache::londefdef::recalc($ENV{'form.textwidth'});
504: $tabsize=~/(\d+\.?\d*)/;
505: $tabsize=$1/2.1;
506: $tabsize.=' mm ';
507: $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}';
1.32 sakharuk 508: }
1.1 albertel 509: }
1.5 sakharuk 510: if ($target ne 'tex') {$result.="<br />";} else {$result.=' \\\\ ';}
1.1 albertel 511: return $result;
512: }
513:
514: sub getfoilcounts {
515: my ($max)=@_;
516: # +1 since instructors will count from 1
517: my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
518: if (&Apache::response::showallfoils()) { $max=$count; }
519: if ($count>$max) { $count=$max }
520: &Apache::lonxml::debug("Count is $count from $max");
521: return $count;
522: }
523:
524:
525: sub start_conceptgroup {
526: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
527: $Apache::matchresponse::conceptgroup=1;
528: %Apache::response::conceptgroup=();
529: my $result;
530: if ($target eq 'edit') {
531: $result.=&Apache::edit::tag_start($target,$token,
532: "Concept Grouped Foils");
533: $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
534: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
535: }
536: if ($target eq 'modified') {
537: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
538: $safeeval,'concept');
539: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
540: }
541: return $result;
542: }
543:
544: sub end_conceptgroup {
545: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
546: $Apache::matchresponse::conceptgroup=0;
547: my $result='';
1.28 albertel 548: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
549: $target eq 'tex' || $target eq 'analyze') {
550: &Apache::response::pick_foil_for_concept($target,
551: ['value','text','location'],
552: \%Apache::hint::match,
553: $parstack,$safeeval);
1.1 albertel 554: } elsif ($target eq 'edit') {
555: $result=&Apache::edit::end_table();
556: }
557: return $result;
558: }
559:
560: sub insert_conceptgroup {
561: my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
562: return $result;
563: }
564:
565: sub start_foil {
566: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
567: my $result='';
1.28 albertel 568: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.1 albertel 569: &Apache::lonxml::startredirection;
570: } elsif ($target eq 'edit') {
571: $result=&Apache::edit::tag_start($target,$token,"Foil");
572: my $level='-2';
573: if ($$tagstack[-2] eq 'conceptgroup') { $level = '-3'; }
574: $result.=&Apache::edit::text_arg('Name:','name',$token);
575: my @names;
576: if (defined(@{ $Apache::response::itemgroup{'names'} })) {
577: @names=@{ $Apache::response::itemgroup{'names'} };
578: }
579: $result.= &Apache::edit::select_or_text_arg('Correct Option:','value',['unused',@names],$token,'15');
580: my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
581: $safeeval,'-3');
582: if ($randomize ne 'no') {
583: $result.=&Apache::edit::select_arg('Location:','location',
584: ['random','top','bottom'],$token);
585: }
586: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
587: } elsif ($target eq 'modified') {
588: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
589: $safeeval,'value',
590: 'name','location');
591: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
592: }
593: return $result;
594: }
595:
596: sub end_foil {
597: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
598: my $text ='';
599: my $result = '';
1.28 albertel 600: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.1 albertel 601: $text=&Apache::lonxml::endredirection;
602: }
1.28 albertel 603: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
604: $target eq 'tex' || $target eq 'analyze') {
1.1 albertel 605: my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
606: if ($value ne 'unused') {
607: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
608: &Apache::lonxml::debug("Got a name of :$name:");
609: if (!$name) { $name=$Apache::lonxml::curdepth; }
610: &Apache::lonxml::debug("Using a name of :$name:");
1.37 albertel 611: if (defined($Apache::response::foilnames{$name})) {
612: &Apache::lonxml::error(&mt("Foil name <b><tt>[_1]</tt></b> appears more than once. Foil names need to be unique.",$name));
613: }
1.38 albertel 614: $Apache::response::foilnames{$name}++;
1.37 albertel 615: my $location =&Apache::lonxml::get_param('location',$parstack,
616: $safeeval);
1.1 albertel 617: if ( $Apache::matchresponse::conceptgroup
618: && !&Apache::response::showallfoils() ) {
619: push @{ $Apache::response::conceptgroup{'names'} }, $name;
620: $Apache::response::conceptgroup{"$name.value"} = $value;
1.19 sakharuk 621: if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
1.14 sakharuk 622: $Apache::response::conceptgroup{"$name.text"} = ' $\triangleright$ '.$text;
623: } else {
624: $Apache::response::conceptgroup{"$name.text"} = $text;
625: }
1.1 albertel 626: $Apache::response::conceptgroup{"$name.location"} = $location;
627: } else {
628: push @{ $Apache::response::foilgroup{'names'} }, $name;
629: $Apache::response::foilgroup{"$name.value"} = $value;
1.16 albertel 630: if ($Apache::lonhomework::type eq 'exam') {
1.19 sakharuk 631: if ($target eq 'tex') {
632: $Apache::response::foilgroup{"$name.text"} = '\vskip 5 mm $\triangleright$ '.$text;
633: } else {
634: $Apache::response::foilgroup{"$name.text"} = $text;
635: }
1.14 sakharuk 636: } else {
637: if ($target eq 'tex') {
1.24 sakharuk 638: $Apache::response::foilgroup{"$name.text"} = $text;
1.14 sakharuk 639: } else {
640: $Apache::response::foilgroup{"$name.text"} = $text;
641: }
642: }
1.1 albertel 643: $Apache::response::foilgroup{"$name.location"} = $location;
644: }
645: }
646: }
647: if ($target eq 'edit') {
648: $result.= &Apache::edit::tag_end($target,$token,'');
649: }
650: return $result;
651: }
652:
653: sub insert_foil {
654: return '
655: <foil name="" value="unused">
656: <startouttext />
657: <endouttext />
658: </foil>';
659: }
660: 1;
661: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>