Annotation of loncom/homework/matchresponse.pm, revision 1.42
1.1 albertel 1: # The LearningOnline Network with CAPA
2: # Full matching style response
3: #
1.42 ! albertel 4: # $Id: matchresponse.pm,v 1.41 2004/05/27 04:32:19 albertel 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.42 ! albertel 467: my @blank;
! 468: $question.=&Apache::optionresponse::webbubbles(\@used_letters,\@blank,$temp,$last_letter);
1.6 sakharuk 469: }
470: } else {
471: if ($Apache::lonhomework::type eq 'exam') {
472: $question.=' '.$optionlist.$text."\n";
1.13 sakharuk 473: my @emptyItems = ();
1.16 albertel 474: for (my $i=0;$i<=$#used_letters;$i++) {push @emptyItems, ' ';}
1.40 sakharuk 475: $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 476: $internal_counter++;
1.6 sakharuk 477: } else {
1.22 sakharuk 478: $question.=' '.$optionlist.$text.'\strut\\\\\strut '."\n";
1.6 sakharuk 479: }
1.5 sakharuk 480: }
1.1 albertel 481: $temp++;
482: }
1.4 albertel 483: }
484: if ($result=&itemdisplay('top')) {
485: $result.=$question;
486: } elsif ($result=&itemdisplay('bottom')) {
487: $result=$question.$result;
488: } elsif ($result=&itemdisplay('right')) {
1.32 sakharuk 489: if ($target ne 'tex') {
490: $result='<table><tr><td>'.$question.'</td><td>'.$result.
491: '</td></tr></table>';
492: } else {
1.39 sakharuk 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}';
1.32 sakharuk 498: }
1.4 albertel 499: } elsif ($result=&itemdisplay('left')) {
1.32 sakharuk 500: if ($target ne 'tex') {
501: $result='<table><tr><td>'.$result.'</td><td>'.$question.
502: '</td></tr></table>';
503: } else {
1.39 sakharuk 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}';
1.32 sakharuk 509: }
1.1 albertel 510: }
1.5 sakharuk 511: if ($target ne 'tex') {$result.="<br />";} else {$result.=' \\\\ ';}
1.1 albertel 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='';
1.28 albertel 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);
1.1 albertel 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='';
1.28 albertel 569: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.1 albertel 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 = '';
1.28 albertel 601: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.1 albertel 602: $text=&Apache::lonxml::endredirection;
603: }
1.28 albertel 604: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
605: $target eq 'tex' || $target eq 'analyze') {
1.1 albertel 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:");
1.37 albertel 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: }
1.38 albertel 615: $Apache::response::foilnames{$name}++;
1.37 albertel 616: my $location =&Apache::lonxml::get_param('location',$parstack,
617: $safeeval);
1.1 albertel 618: if ( $Apache::matchresponse::conceptgroup
619: && !&Apache::response::showallfoils() ) {
620: push @{ $Apache::response::conceptgroup{'names'} }, $name;
621: $Apache::response::conceptgroup{"$name.value"} = $value;
1.19 sakharuk 622: if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
1.14 sakharuk 623: $Apache::response::conceptgroup{"$name.text"} = ' $\triangleright$ '.$text;
624: } else {
625: $Apache::response::conceptgroup{"$name.text"} = $text;
626: }
1.1 albertel 627: $Apache::response::conceptgroup{"$name.location"} = $location;
628: } else {
629: push @{ $Apache::response::foilgroup{'names'} }, $name;
630: $Apache::response::foilgroup{"$name.value"} = $value;
1.16 albertel 631: if ($Apache::lonhomework::type eq 'exam') {
1.19 sakharuk 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: }
1.14 sakharuk 637: } else {
638: if ($target eq 'tex') {
1.24 sakharuk 639: $Apache::response::foilgroup{"$name.text"} = $text;
1.14 sakharuk 640: } else {
641: $Apache::response::foilgroup{"$name.text"} = $text;
642: }
643: }
1.1 albertel 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>