Annotation of loncom/homework/matchresponse.pm, revision 1.40
1.1 albertel 1: # The LearningOnline Network with CAPA
2: # Full matching style response
3: #
1.40 ! sakharuk 4: # $Id: matchresponse.pm,v 1.39 2004/04/02 17:58:16 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;
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;
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: }
269: return $result;
1.29 albertel 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);
1.1 albertel 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'} };
1.18 albertel 284: my @whichfoils = &Apache::response::whichorder(&getfoilcounts($max),
285: $randomize,
286: &Apache::response::showallfoils(),
287: \%Apache::response::foilgroup);
1.1 albertel 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)=@_;
1.18 albertel 306: my (@whichfoils)=&Apache::response::whichorder(&getfoilcounts($max),
307: $randomize,
308: &Apache::response::showallfoils(),
309: \%Apache::response::foilgroup);
1.1 albertel 310: if (!defined($ENV{'form.submitted'})) { return; }
311: my %responsehash;
312: my %grade;
1.33 albertel 313: my ($temp,$right,$wrong,$ignored)=(1,0,0,0);
1.1 albertel 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: }
1.30 albertel 319: my @items;
1.1 albertel 320: foreach my $name (@whichfoils) {
1.34 albertel 321: my $response = &Apache::response::getresponse($temp,'letter');
1.30 albertel 322: push(@items,$response);
1.1 albertel 323: my $responsename = $letter_name_map{$response};
324: my $value=$Apache::response::foilgroup{$name.'.value'};
325: if ( $response =~ /[^\s]/) {
1.31 albertel 326: $responsehash{$name}=$responsename;
1.33 albertel 327: &Apache::lonxml::debug("submitted a $response($responsename) for $value<br />\n");
1.1 albertel 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);
1.30 albertel 341: my $itemstr =&Apache::lonnet::array2str(@items);
1.1 albertel 342: my $gradestr =&Apache::lonnet::hash2str(%grade);
1.30 albertel 343: my %previous=&Apache::response::check_for_previous($responsestr,$part,$id);
1.1 albertel 344: &Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored ");
345: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
346: $responsestr;
1.30 albertel 347: $Apache::lonhomework::results{"resource.$part.$id.submissionitems"}=
348: $itemstr;
1.1 albertel 349: $Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=
350: $gradestr;
1.34 albertel 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"}=
1.35 albertel 375: $right/(scalar(@whichfoils));
1.34 albertel 376: $Apache::lonhomework::results{"resource.$part.$id.numfoils"}=
1.35 albertel 377: scalar(@whichfoils);
1.34 albertel 378: }
379: }
1.1 albertel 380: }
381:
1.4 albertel 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: }
1.1 albertel 393: sub displayfoils {
394: my ($target,$max,$randomize)=@_;
395: my $result;
1.4 albertel 396: my $question;
1.18 albertel 397: my (@whichfoils)=&Apache::response::whichorder(&getfoilcounts($max),
398: $randomize,
399: &Apache::response::showallfoils(),
400: \%Apache::response::foilgroup);
1.1 albertel 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: }
1.25 albertel 413: if ( &Apache::response::show_answer() && ($target ne 'tex')) {
1.1 albertel 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};
1.5 sakharuk 418: if ($target eq 'tex') {
419: $question.=' \\\\ '.$letter.':'.$text;
420: } else {
421: $question.='<br />'.$letter.':'.$text;
422: }
1.1 albertel 423: }
424: } else {
425: my $i = 0;
1.36 albertel 426: my $temp=1;
1.1 albertel 427: my $id=$Apache::inputtags::response[-1];
428: my $part=$Apache::inputtags::part;
429: my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
1.9 sakharuk 430: my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
431: my @alphabet=('A'..'Z');
1.15 albertel 432: my @used_letters=sort(keys(%letter_name_map));
1.20 sakharuk 433: my $internal_counter=$Apache::lonxml::counter;
1.1 albertel 434: foreach my $name (@whichfoils) {
435: my $lastopt=$lastresponse{$name};
436: my $last_letter=$name_letter_map{$lastopt};
1.5 sakharuk 437: my $optionlist = '';
1.7 sakharuk 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: }
1.1 albertel 445: my $option;
1.15 albertel 446: foreach $option (@used_letters) {
1.1 albertel 447: if ($option eq $last_letter) {
1.6 sakharuk 448: if ($target ne 'tex') {$optionlist.="<option selected=\"on\">$option</option>\n";}
1.1 albertel 449: } else {
1.6 sakharuk 450: if ($target ne 'tex') {$optionlist.="<option>$option</option>\n";}
1.1 albertel 451: }
452: }
1.19 sakharuk 453: if ($target ne 'tex' && $Apache::lonhomework::type ne 'exam') {
1.5 sakharuk 454: $optionlist='<select name="HWVAL_'.
455: $Apache::inputtags::response[-1].':'.$temp.'">'.
456: $optionlist."</select>\n";
457: }
1.1 albertel 458: my $text=$Apache::response::foilgroup{$name.'.text'};
1.5 sakharuk 459: if ($target ne 'tex') {
1.19 sakharuk 460: if ($Apache::lonhomework::type ne 'exam') {
461: $question.='<br />'.$optionlist.$text."\n";
462: } else {
463: $question.='<br />'.$text."\n";
464: }
1.6 sakharuk 465: if ($Apache::lonhomework::type eq 'exam') {
1.27 albertel 466: $question.=&Apache::optionresponse::webbubbles(\@used_letters,\@used_letters,$temp,$last_letter);
1.6 sakharuk 467: }
468: } else {
469: if ($Apache::lonhomework::type eq 'exam') {
470: $question.=' '.$optionlist.$text."\n";
1.13 sakharuk 471: my @emptyItems = ();
1.16 albertel 472: for (my $i=0;$i<=$#used_letters;$i++) {push @emptyItems, ' ';}
1.40 ! sakharuk 473: $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 474: $internal_counter++;
1.6 sakharuk 475: } else {
1.22 sakharuk 476: $question.=' '.$optionlist.$text.'\strut\\\\\strut '."\n";
1.6 sakharuk 477: }
1.5 sakharuk 478: }
1.1 albertel 479: $temp++;
480: }
1.4 albertel 481: }
482: if ($result=&itemdisplay('top')) {
483: $result.=$question;
484: } elsif ($result=&itemdisplay('bottom')) {
485: $result=$question.$result;
486: } elsif ($result=&itemdisplay('right')) {
1.32 sakharuk 487: if ($target ne 'tex') {
488: $result='<table><tr><td>'.$question.'</td><td>'.$result.
489: '</td></tr></table>';
490: } else {
1.39 sakharuk 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}';
1.32 sakharuk 496: }
1.4 albertel 497: } elsif ($result=&itemdisplay('left')) {
1.32 sakharuk 498: if ($target ne 'tex') {
499: $result='<table><tr><td>'.$result.'</td><td>'.$question.
500: '</td></tr></table>';
501: } else {
1.39 sakharuk 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}';
1.32 sakharuk 507: }
1.1 albertel 508: }
1.5 sakharuk 509: if ($target ne 'tex') {$result.="<br />";} else {$result.=' \\\\ ';}
1.1 albertel 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='';
1.28 albertel 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);
1.1 albertel 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='';
1.28 albertel 567: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.1 albertel 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 = '';
1.28 albertel 599: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.1 albertel 600: $text=&Apache::lonxml::endredirection;
601: }
1.28 albertel 602: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
603: $target eq 'tex' || $target eq 'analyze') {
1.1 albertel 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:");
1.37 albertel 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: }
1.38 albertel 613: $Apache::response::foilnames{$name}++;
1.37 albertel 614: my $location =&Apache::lonxml::get_param('location',$parstack,
615: $safeeval);
1.1 albertel 616: if ( $Apache::matchresponse::conceptgroup
617: && !&Apache::response::showallfoils() ) {
618: push @{ $Apache::response::conceptgroup{'names'} }, $name;
619: $Apache::response::conceptgroup{"$name.value"} = $value;
1.19 sakharuk 620: if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
1.14 sakharuk 621: $Apache::response::conceptgroup{"$name.text"} = ' $\triangleright$ '.$text;
622: } else {
623: $Apache::response::conceptgroup{"$name.text"} = $text;
624: }
1.1 albertel 625: $Apache::response::conceptgroup{"$name.location"} = $location;
626: } else {
627: push @{ $Apache::response::foilgroup{'names'} }, $name;
628: $Apache::response::foilgroup{"$name.value"} = $value;
1.16 albertel 629: if ($Apache::lonhomework::type eq 'exam') {
1.19 sakharuk 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: }
1.14 sakharuk 635: } else {
636: if ($target eq 'tex') {
1.24 sakharuk 637: $Apache::response::foilgroup{"$name.text"} = $text;
1.14 sakharuk 638: } else {
639: $Apache::response::foilgroup{"$name.text"} = $text;
640: }
641: }
1.1 albertel 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>