Annotation of loncom/homework/optionresponse.pm, revision 1.120
1.46 sakharuk 1: # LearningOnline Network with CAPA
1.22 albertel 2: # option list style responses
1.27 albertel 3: #
1.120 ! albertel 4: # $Id: optionresponse.pm,v 1.119 2004/08/25 20:00:31 albertel Exp $
1.27 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: #
1.1 albertel 28: package Apache::optionresponse;
29: use strict;
1.103 albertel 30: use Apache::response();
31: use Apache::lonlocal;
1.1 albertel 32:
1.31 harris41 33: BEGIN {
1.1 albertel 34: &Apache::lonxml::register('Apache::optionresponse',('optionresponse'));
35: }
36:
37: sub start_optionresponse {
1.22 albertel 38: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
39: my $result='';
1.29 albertel 40: #when in a option response use these
1.112 sakharuk 41: &Apache::lonxml::register('Apache::optionresponse',('foilgroup','foil','conceptgroup','drawoptionlist'));
1.22 albertel 42: push (@Apache::lonxml::namespace,'optionresponse');
43: my $id = &Apache::response::start_response($parstack,$safeeval);
1.57 albertel 44: %Apache::hint::option=();
1.103 albertel 45: undef(%Apache::response::foilnames);
1.22 albertel 46: if ($target eq 'edit') {
1.23 albertel 47: $result.=&Apache::edit::start_table($token).
1.98 www 48: "<tr><td>Multiple Option Response Question ".
49: &Apache::loncommon::help_open_topic('Option_Response_Problems')."</td><td>Delete:".
1.23 albertel 50: &Apache::edit::deletelist($target,$token)
1.30 matthew 51: ."</td><td> ".
52: &Apache::edit::end_row().
53: &Apache::edit::start_spanning_row().
54: "\n";
1.48 albertel 55: $result.=&Apache::edit::text_arg('Max Number Of Shown Foils:','max',
56: $token,'4').
57: &Apache::edit::select_arg('Randomize Foil Order','randomize',
58: ['yes','no'],$token).
1.108 albertel 59: &Apache::edit::select_arg(&mt('Display of options when printed'),'TeXlayout',
60: [['horizontal',&mt('Normal list')],
61: ['vertical',&mt('Listed in a vertical column')]],$token).
1.48 albertel 62: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.34 albertel 63: } elsif ($target eq 'modified') {
1.23 albertel 64: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.108 albertel 65: $safeeval,'max','randomize',
66: 'TeXlayout');
1.22 albertel 67: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.34 albertel 68: } elsif ($target eq 'meta') {
1.23 albertel 69: $result=&Apache::response::meta_package_write('optionresponse');
1.34 albertel 70: } elsif ($target eq 'analyze') {
1.93 albertel 71: my $part_id="$Apache::inputtags::part.$id";
1.34 albertel 72: push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
1.22 albertel 73: }
74: return $result;
1.1 albertel 75: }
76:
77: sub end_optionresponse {
1.22 albertel 78: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
79: &Apache::response::end_response;
80: pop @Apache::lonxml::namespace;
1.112 sakharuk 81: &Apache::lonxml::deregister('Apache::optionresponse',('foilgroup','foil','conceptgroup','drawoptionlist'));
1.22 albertel 82: my $result;
83: if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
1.103 albertel 84: undef(%Apache::response::foilnames);
1.22 albertel 85: return $result;
1.1 albertel 86: }
87:
1.44 albertel 88: %Apache::response::foilgroup=();
1.1 albertel 89: sub start_foilgroup {
1.22 albertel 90: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
91:
92: my $result='';
1.44 albertel 93: %Apache::response::foilgroup=();
1.22 albertel 94: $Apache::optionresponse::conceptgroup=0;
1.110 albertel 95: &Apache::response::pushrandomnumber();
1.22 albertel 96: if ($target eq 'edit') {
97: my $optionlist="<option></option>\n";
98: my $option;
99: my @opt;
100: eval '@opt ='. &Apache::lonxml::get_param('options',$parstack,$safeeval);
101: my $count=1;
102: foreach $option (@opt) {
103: $optionlist.="<option value=\"$count\">$option</option>\n";
104: $count++;
105: }
106: my $insertlist=&Apache::edit::insertlist($target,$token);
107: $result.=&Apache::edit::start_table($token);
108: $result.= (<<ENDTABLE);
109: <tr><td>Select Options</td>
1.13 albertel 110: <td>
111: Add new Option: <input type="text" name="$Apache::lonxml::curdepth.options" />
112: </td>
113: <td>Delete an Option:
114: <select name="$Apache::lonxml::curdepth.deleteopt">$optionlist</select>
115: ENDTABLE
1.30 matthew 116: $result.= &Apache::edit::end_row();
117: $result.= &Apache::edit::start_spanning_row();
118: $result.= $insertlist.'<br />';
1.22 albertel 119: }
120: if ($target eq 'modified') {
121: my @options;
122: my $optchanged=0;
123: eval '@options ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
124: if ($ENV{"form.$Apache::lonxml::curdepth.deleteopt"}) {
125: my $delopt=$ENV{"form.$Apache::lonxml::curdepth.deleteopt"};
126: &Apache::lonxml::debug("Deleting :$delopt:");
127: splice(@options,$delopt-1,1);
128: $optchanged=1;
129: }
130: if ($ENV{"form.$Apache::lonxml::curdepth.options"}) {
131: my $newopt = $ENV{"form.$Apache::lonxml::curdepth.options"};
132: if ($options[0]) {
133: push(@options,$newopt);
134: } else {
135: $options[0]=$newopt;
136: }
137: $optchanged=1;
138: }
139: if ($optchanged) {
140: $result = "<foilgroup options=\"(";
141: foreach my $option (@options) {
1.41 albertel 142: $option=~s/\'/\\\'/g;
1.22 albertel 143: &Apache::lonxml::debug("adding option :$option:");
144: $result .="'".$option."',";
145: }
146: chop $result;
147: $result.=')">';
148: } # else nothing changed so just use the default mechanism
149: }
1.68 sakharuk 150: if ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
1.109 albertel 151: $result .= ' \renewcommand{\labelenumi}{\Alph{enumi}.}
152: \begin{enumerate} ';
1.42 sakharuk 153: }
1.22 albertel 154: return $result;
1.1 albertel 155: }
156:
157: sub end_foilgroup {
1.22 albertel 158: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
159:
160: my $result;
1.94 albertel 161: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
162: $target eq 'tex' || $target eq 'analyze') {
1.81 sakharuk 163: my $tex_option_switch=&Apache::lonxml::get_param('texoptions',$parstack,$safeeval);
1.22 albertel 164: my $name;
165: my @opt;
166: eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
1.108 albertel 167: my $TeXlayout=&Apache::lonxml::get_param('TeXlayout',$parstack,$safeeval,
168: -2,0);
1.81 sakharuk 169: if ($target eq 'tex' && $tex_option_switch eq 'nochoice') {@opt=();}
1.22 albertel 170: &Apache::lonxml::debug("Options are $#opt");
1.48 albertel 171: my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
172: my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
173: $safeeval,'-2');
1.32 sakharuk 174: if ($target eq 'web' || $target eq 'tex') {
1.107 sakharuk 175: $result.=&displayfoils($target,$max,$randomize,$TeXlayout,@opt);
1.25 albertel 176: } elsif ( $target eq 'answer') {
1.48 albertel 177: $result.=&displayanswers($max,$randomize,@opt);
1.34 albertel 178: } elsif ( $target eq 'analyze') {
1.93 albertel 179: my @shown = &whichfoils($max,$randomize);
1.94 albertel 180: &Apache::response::analyze_store_foilgroup(\@shown,
181: ['text','value','location']);
1.93 albertel 182: my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
183: push (@{ $Apache::lonhomework::analyze{"$part_id.options"} },@opt);
1.22 albertel 184: } elsif ( $target eq 'grade') {
185: if ( defined $ENV{'form.submitted'}) {
1.48 albertel 186: my @whichopt = &whichfoils($max,$randomize);
1.22 albertel 187: my $temp=1;my $name;
1.26 albertel 188: my %responsehash;
1.57 albertel 189: my %grade;
1.22 albertel 190: my $right=0;
191: my $wrong=0;
192: my $ignored=0;
193: foreach $name (@whichopt) {
1.74 albertel 194: my $response=&Apache::response::getresponse($temp);
1.120 ! albertel 195: if ($ENV{'form.submitted'} eq 'scantron' && $response=~/\S/) {
1.74 albertel 196: $response = $opt[$response];
197: }
1.22 albertel 198: if ( $response =~ /[^\s]/) {
1.96 albertel 199: $responsehash{$name}=$response;
1.22 albertel 200: my $value=$Apache::response::foilgroup{$name.'.value'};
1.34 albertel 201: &Apache::lonxml::debug("submitted a $response for $value<br />\n");
1.57 albertel 202: if ($value eq $response) {
203: $grade{$name}='1'; $right++;
204: } else {
205: $grade{$name}='0'; $wrong++;
206: }
1.22 albertel 207: } else {
208: $ignored++;
209: }
210: $temp++;
211: }
1.28 albertel 212: my $part=$Apache::inputtags::part;
1.22 albertel 213: my $id = $Apache::inputtags::response['-1'];
1.28 albertel 214: my $responsestr=&Apache::lonnet::hash2str(%responsehash);
1.57 albertel 215: my $gradestr =&Apache::lonnet::hash2str(%grade);
1.28 albertel 216: my %previous=&Apache::response::check_for_previous($responsestr,
217: $part,$id);
1.22 albertel 218: &Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored");
1.75 albertel 219: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
220: $responsestr;
221: $Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=$gradestr;
222:
223: if (!$Apache::lonhomework::scantronmode) {
224: my $ad;
225: if ($wrong==0 && $ignored==0) {
226: $ad='EXACT_ANS';
227: } elsif ($wrong==0 && $right==0) {
228: #nothing submitted
229: } else {
230: if ($ignored==0) {
231: $ad='INCORRECT';
232: } else {
233: $ad='MISSING_ANSWER';
234: }
235: }
236: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
237: &Apache::response::handle_previous(\%previous,$ad);
1.22 albertel 238: } else {
1.75 albertel 239: my $ad;
240: if ($wrong==0 && $right==0) {
1.120 ! albertel 241: #nothing submitted only assign a score if we
! 242: #need to override a previous grade
! 243: if (defined($Apache::lonhomework::history{"resource.$part.$id.awarddetail"})) {
! 244: $ad='ASSIGNED_SCORE';
! 245: }
1.75 albertel 246: } else {
247: $ad='ASSIGNED_SCORE';
248: }
249: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
250: $Apache::lonhomework::results{"resource.$part.$id.awarded"}=
251: $right/(scalar(@whichopt));
1.76 albertel 252: $Apache::lonhomework::results{"resource.$part.$id.numfoils"}=
253: scalar(@whichopt);
1.1 albertel 254: }
1.22 albertel 255: }
1.1 albertel 256: }
1.92 sakharuk 257: &Apache::lonxml::increment_counter(&getfoilcounts($max));
1.25 albertel 258: } elsif ($target eq 'edit') {
1.22 albertel 259: $result.=&Apache::edit::end_table();
1.74 albertel 260: }
261: if ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
262: $result .= '\end{enumerate}';
263: }
1.110 albertel 264: &Apache::response::poprandomnumber();
1.22 albertel 265: return $result;
1.1 albertel 266: }
267:
268: sub getfoilcounts {
1.48 albertel 269: my ($max)=@_;
1.22 albertel 270: # +1 since instructors will count from 1
271: my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
1.39 albertel 272: if (&Apache::response::showallfoils()) { $max=$count; }
1.48 albertel 273: if ($count>$max) { $count=$max }
274: &Apache::lonxml::debug("Count is $count from $max");
275: return $count;
1.1 albertel 276: }
277:
278: sub whichfoils {
1.77 albertel 279: my ($max,$randomize)=@_;
280: return &Apache::response::whichorder($max,$randomize,
281: &Apache::response::showallfoils(),
282: \%Apache::response::foilgroup);
1.1 albertel 283: }
284:
1.25 albertel 285: sub displayanswers {
1.48 albertel 286: my ($max,$randomize,@opt)=@_;
1.67 albertel 287: if (!defined(@{ $Apache::response::foilgroup{'names'} })) {return;}
1.25 albertel 288: my @names = @{ $Apache::response::foilgroup{'names'} };
1.48 albertel 289: my @whichopt = &whichfoils($max,$randomize);
1.25 albertel 290: my $result=&Apache::response::answer_header('optionresponse');
291: foreach my $name (@whichopt) {
292: $result.=&Apache::response::answer_part('optionresponse',
293: $Apache::response::foilgroup{$name.'.value'})
294: }
295: $result.=&Apache::response::answer_footer('optionresponse');
296: return $result;
297: }
298:
1.106 albertel 299: sub check_for_invalid {
300: my ($names,$options) = @_;
301: my %bad_names;
302: foreach my $name (@{ $names }) {
303: my $value=$Apache::response::foilgroup{$name.'.value'};
304: my $found=0;
305: foreach my $option (@{ $options }) {
306: if ($value eq $option) { $found=1; }
307: }
308: if (!$found) { $bad_names{$name}=$value; }
309: }
310: if (%bad_names) {
311: my $error=&mt('The question can not be gotten correct, '.
312: 'the following foils in the <optionresponse> '.
313: 'have invalid correct options').' <br /><tt>'.
314: join('<br />',(map { $_=&mt("[_1] with value [_2]",$_,$bad_names{$_}) } (keys(%bad_names)))).
315: "</tt>";
316: &Apache::lonxml::error($error);
317: }
318: }
319:
1.1 albertel 320: sub displayfoils {
1.107 sakharuk 321: my ($target,$max,$randomize,$TeXlayout,@opt)=@_;
1.67 albertel 322: if (!defined(@{ $Apache::response::foilgroup{'names'} })) {return;}
1.22 albertel 323: my @names = @{ $Apache::response::foilgroup{'names'} };
324: my @truelist;
325: my @falselist;
1.79 sakharuk 326: my $result;
1.22 albertel 327: my $name;
1.101 albertel 328: my $displayoptionintex=1;
1.72 sakharuk 329: my @alphabet = ('A'..'Z');
1.48 albertel 330: my @whichopt = &whichfoils($max,$randomize);
1.106 albertel 331: &check_for_invalid(\@whichopt,\@opt);
1.28 albertel 332: my $part=$Apache::inputtags::part;
333: my $id=$Apache::inputtags::response[-1];
1.62 albertel 334: my $break;
1.65 albertel 335: my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
1.86 albertel 336: if ( ($target ne 'tex') &&
337: &Apache::response::show_answer() ) {
1.90 albertel 338: my $temp=1;
1.22 albertel 339: foreach $name (@whichopt) {
1.62 albertel 340: my $text=$Apache::response::foilgroup{$name.'.text'};
1.90 albertel 341: my %lastresponse=&Apache::lonnet::str2hash($Apache::lonhomework::history{"resource.$part.$id.submission"});
342: my $lastopt=$lastresponse{$name};
1.101 albertel 343: if ($text!~/^\s*$/) { $break='<br />'; }
1.62 albertel 344: $result.=$break;
1.113 sakharuk 345: if ($target eq 'web') {
1.62 albertel 346: my $value=$Apache::response::foilgroup{$name.'.value'};
347: if (!($text=~s|<drawoptionlist\s*/>|$value|)) {
348: if ($text=~/^\s*$/) {
349: $text=$value.$text;
350: } else {
351: $text=$value.': '.$text;
352: }
1.95 albertel 353: } else {
354: $text='•'.$text;
1.62 albertel 355: }
356: $result.=$text."\n";
1.112 sakharuk 357: }
1.58 sakharuk 358: if ($Apache::lonhomework::type eq 'exam') {
1.90 albertel 359: $result.=&webbubbles(\@opt,\@alphabet,$temp,$lastopt);
1.58 sakharuk 360: }
1.90 albertel 361: $temp++;
1.22 albertel 362: }
363: } else {
364: my $temp=1;
1.28 albertel 365: my %lastresponse=&Apache::lonnet::str2hash($Apache::lonhomework::history{"resource.$part.$id.submission"});
1.79 sakharuk 366: my $internal_counter=$Apache::lonxml::counter;
1.22 albertel 367: foreach $name (@whichopt) {
1.62 albertel 368: my $text=$Apache::response::foilgroup{$name.'.text'};
369: if ($text!~/^\s*$/) {
370: if ($target eq 'tex') {
371: $break='\vskip 0 mm ';
372: } elsif ($target eq 'web') {
373: $break='<br />';
374: }
375: }
1.28 albertel 376: my $lastopt=$lastresponse{$name};
377: my $optionlist="<option></option>\n";
378: my $option;
379: foreach $option (@opt) {
380: if ($option eq $lastopt) {
381: $optionlist.="<option selected=\"on\">$option</option>\n";
382: } else {
383: $optionlist.="<option>$option</option>\n";
384: }
385: }
1.32 sakharuk 386: if ($target ne 'tex') {
1.72 sakharuk 387: if ($Apache::lonhomework::type ne 'exam') {
1.101 albertel 388: $optionlist='<select name="HWVAL_'.
389: $Apache::inputtags::response['-1'].':'.$temp.'">'.
1.59 albertel 390: $optionlist."</select>\n";
1.101 albertel 391: } else {
392: $optionlist='<u>'.(' 'x10).'</u>';
1.72 sakharuk 393: }
1.101 albertel 394: if ($text=~s|<drawoptionlist\s*/>|$optionlist|) {
395: if ($Apache::lonhomework::type ne 'exam') {
396: $text='•'.$text;
397: }
1.95 albertel 398: } else {
1.101 albertel 399: if ($Apache::lonhomework::type ne 'exam') {
400: $text=$optionlist.$text;
401: }
1.59 albertel 402: }
1.62 albertel 403: $result.=$break.$text."\n";
1.49 sakharuk 404: if ($Apache::lonhomework::type eq 'exam') {
1.101 albertel 405: $result.=&webbubbles(\@opt,\@alphabet,$temp,$lastopt);
1.49 sakharuk 406: }
1.32 sakharuk 407: $temp++;
408: } else {
1.101 albertel 409: my $texoptionlist='';
410: if ($displayoptionintex &&
411: $Apache::lonhomework::type ne 'exam') {
1.107 sakharuk 412: $texoptionlist = &optionlist_correction($TeXlayout,@opt);
1.101 albertel 413: }
414: if ($text=~/<drawoptionlist\s*\/>/) {
415: $text=~s|<drawoptionlist\s*/>| \\makebox\[0\.3in\]\[b\]\{\\hrulefill\} |;
416: }
417:
418: if ($text=~m/\\item /) {
1.50 sakharuk 419: if ($Apache::lonhomework::type eq 'exam') {
1.101 albertel 420: $text=~s/\\item/\\vskip 2 mm/;
1.50 sakharuk 421: }
1.101 albertel 422: $result.= $texoptionlist.$text;
1.32 sakharuk 423: } else {
1.101 albertel 424: if ($Apache::lonhomework::type eq 'exam') {
425: $result.= $texoptionlist.' '.$text;
1.46 sakharuk 426: } else {
1.107 sakharuk 427: if ($text=~/\S/) {$result.= $texoptionlist.'\vspace*{-2 mm}\item '.$text;} else {$result.= $texoptionlist;}
1.50 sakharuk 428: }
1.32 sakharuk 429: }
1.101 albertel 430: if ($Apache::lonhomework::type eq 'exam') {
431: $result.='\vskip -1 mm\noindent\begin{enumerate}\item[\textbf{'.
432: $internal_counter.'}.]'.&bubbles(\@alphabet,\@opt).
433: '\end{enumerate} \vskip -8 mm \strut ';
434: $internal_counter++;
435: }
436: $displayoptionintex=0;
1.62 albertel 437: }
1.1 albertel 438: }
1.62 albertel 439: }
1.32 sakharuk 440: if ($target ne 'tex') {
1.62 albertel 441: return $result.$break;
442: } else {
1.32 sakharuk 443: return $result;
1.22 albertel 444: }
1.50 sakharuk 445: }
446:
447:
448: sub optionlist_correction {
1.107 sakharuk 449: my ($TeXlayout,@options) = @_;
1.101 albertel 450: my $texoptionlist='\\item [] Choices: ';
1.107 sakharuk 451: if ($TeXlayout eq 'vertical') {$texoptionlist='\\item []';}
1.101 albertel 452: if (scalar(@options) > 0) {
453: foreach my $option (@options) {
454: $texoptionlist.='{\bf '.
455: &Apache::lonxml::latex_special_symbols($option).
1.107 sakharuk 456: '}';
457: if ($TeXlayout eq 'vertical') {
458: $texoptionlist.=' \vskip 0 mm ',
459: } else {
1.115 sakharuk 460: $texoptionlist.=', ';
1.107 sakharuk 461: }
1.101 albertel 462: }
1.116 albertel 463: $texoptionlist=~s/, $//;
1.107 sakharuk 464: if ($TeXlayout ne 'vertical') {$texoptionlist.='.';}
1.80 sakharuk 465: } else {
1.107 sakharuk 466: if ($TeXlayout ne 'vertical') {$texoptionlist='\\item [] \\vskip -5 mm';}
1.80 sakharuk 467: }
1.50 sakharuk 468: return $texoptionlist;
1.58 sakharuk 469: }
470:
471:
472: sub webbubbles {
1.90 albertel 473: my ($ropt,$ralphabet,$temp,$lastopt)=@_;
1.58 sakharuk 474: my @opt=@$ropt;
475: my @alphabet=@$ralphabet;
476: my $result='';
1.90 albertel 477: my $number_of_bubbles = $#opt + 1;
478: $result.= '<table border="1"><tr>';
479: for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
480: my $checked='';
481: if ($lastopt eq $opt[$ind]) {
482: $checked=' checked="on" ';
1.58 sakharuk 483: }
1.90 albertel 484: $result.='<td><input type="radio" name="HWVAL_'.
485: $Apache::inputtags::response['-1'].':'.$temp.
1.111 albertel 486: '" value="'.$opt[$ind].'" '.$checked.' />';
487: if ($alphabet[$ind]) {
488: $result.=$alphabet[$ind].': ';
489: }
490: $result.=$opt[$ind].'</td>';
1.90 albertel 491: }
492: $result.='</tr></table>';
493: return $result;
1.50 sakharuk 494: }
495:
496:
497: sub bubbles {
1.119 albertel 498: my ($ralphabet,$ropt,$response) = @_;
499: my @alphabet = @$ralphabet;
1.50 sakharuk 500: my @opt = @$ropt;
1.51 sakharuk 501: my ($result,$head,$line) =('','','');
1.50 sakharuk 502: my $number_of_bubbles = $#opt + 1;
1.51 sakharuk 503: my $current_length = 0;
1.100 sakharuk 504: my $textwidth;
505: if ($ENV{'form.textwidth'} ne '') {
506: $ENV{'form.textwidth'}=~/(\d+)/;
507: $textwidth=$1;
508: } else {
1.102 sakharuk 509: $ENV{'textwidth'}=~/(\d*)\.?(\d*)/;
510: $textwidth=$1.'.'.$2;
1.100 sakharuk 511: }
1.51 sakharuk 512: for (my $ind=0;$ind<=$number_of_bubbles;$ind++) {
1.99 sakharuk 513: my $leftmargin;
1.101 albertel 514: $opt[$ind]=&Apache::lonxml::latex_special_symbols($opt[$ind]);
1.119 albertel 515: if ($response eq 'rankresponse') {$opt[$ind]='Rank '.$opt[$ind];}
1.99 sakharuk 516: if ($ind==0) {$leftmargin=6;} else {$leftmargin=10;}
1.102 sakharuk 517: $current_length += (length($opt[$ind])+length($alphabet[$ind])+4)*2;
1.99 sakharuk 518: if ($current_length<($textwidth-$leftmargin) and $ind!=$number_of_bubbles) {
1.51 sakharuk 519: $line.='\hskip -1 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$\hskip -1 mm & \hskip -3 mm {\small '.$opt[$ind].'} & ';
520: $head.='lr';
521: } else {
522: $line=~s/\&\s*$//;
1.107 sakharuk 523: $result.='\vskip -2 mm\noindent\begin{tabular}{'.$head.'}'.$line.'\\\\\end{tabular}\vskip 0 mm';
1.51 sakharuk 524: $line = '\hskip -1 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$\hskip -1 mm & \hskip -3 mm {\small '.$opt[$ind].'} & ';;
525: $head ='lr';
526: $current_length = (length($opt[$ind])+length($alphabet[$ind]))*2;
527: }
528:
1.50 sakharuk 529: }
530: return $result;
1.1 albertel 531: }
532:
1.22 albertel 533:
1.2 albertel 534: sub start_conceptgroup {
1.22 albertel 535: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
536: $Apache::optionresponse::conceptgroup=1;
1.44 albertel 537: %Apache::response::conceptgroup=();
1.22 albertel 538: my $result;
539: if ($target eq 'edit') {
540: $result.=&Apache::edit::tag_start($target,$token,"Concept Grouped Foils");
1.30 matthew 541: $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
542: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.22 albertel 543: }
544: if ($target eq 'modified') {
545: my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'concept');
546: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
547: }
548: return $result;
1.2 albertel 549: }
550:
551: sub end_conceptgroup {
1.22 albertel 552: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
553: $Apache::optionresponse::conceptgroup=0;
554: my $result='';
1.93 albertel 555: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
556: $target eq 'tex' || $target eq 'analyze') {
1.22 albertel 557: #if not there aren't any foils to display and thus no question
1.93 albertel 558: &Apache::response::pick_foil_for_concept($target,
559: ['value','text','location'],
560: \%Apache::hint::option,
561: $parstack,$safeeval);
1.34 albertel 562: } elsif ($target eq 'edit') {
1.22 albertel 563: $result=&Apache::edit::end_table();
564: }
565: return $result;
1.2 albertel 566: }
567:
1.16 albertel 568: sub insert_conceptgroup {
1.22 albertel 569: my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
570: return $result;
1.16 albertel 571: }
572:
1.1 albertel 573: sub start_foil {
1.22 albertel 574: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
575: my $result='';
1.34 albertel 576: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze' ) {
1.105 albertel 577: &Apache::lonxml::startredirection;
578: if ($target eq 'analyze') {
579: &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
580: }
1.25 albertel 581: } elsif ($target eq 'edit') {
1.22 albertel 582: $result=&Apache::edit::tag_start($target,$token,"Foil");
583: my $level='-2';
584: if ($$tagstack['-2'] eq 'conceptgroup') { $level = '-3'; }
585: my @opt;
586: eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval,$level);
587: $result.=&Apache::edit::text_arg('Name:','name',$token);
1.48 albertel 588: $result.= &Apache::edit::select_or_text_arg('Correct Option:','value',
589: ['unused',(@opt)],$token,'15');
590: my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
591: $safeeval,'-3');
592: if ($randomize ne 'no') {
593: $result.=&Apache::edit::select_arg('Location:','location',
594: ['random','top','bottom'],$token);
595: }
1.30 matthew 596: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.25 albertel 597: } elsif ($target eq 'modified') {
1.48 albertel 598: my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
599: 'value','name','location');
1.22 albertel 600: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.32 sakharuk 601: }
1.22 albertel 602: return $result;
1.1 albertel 603: }
604:
605: sub end_foil {
1.22 albertel 606: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
607: my $text ='';
608: my $result = '';
1.88 albertel 609: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.48 albertel 610: $text=&Apache::lonxml::endredirection;
1.114 sakharuk 611: if ($target eq 'tex') {$text=~s/\\strut\s*\\\\\s*\\strut/\\vskip 0 mm/;}
1.107 sakharuk 612: if (($target eq 'tex') and ($Apache::lonhomework::type ne 'exam') and ($text=~/\S/)) {$text = '\vspace*{-2 mm}\item '.$text;}
1.32 sakharuk 613: }
614: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer'
1.34 albertel 615: || $target eq 'tex' || $target eq 'analyze') {
1.22 albertel 616: my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
617: if ($value ne 'unused') {
618: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
619: &Apache::lonxml::debug("Got a name of :$name:");
620: if (!$name) { $name=$Apache::lonxml::curdepth; }
621: &Apache::lonxml::debug("Using a name of :$name:");
1.103 albertel 622: if (defined($Apache::response::foilnames{$name})) {
623: &Apache::lonxml::error(&mt("Foil name <b><tt>[_1]</tt></b> appears more than once. Foil names need to be unique.",$name));
624: }
1.104 albertel 625: $Apache::response::foilnames{$name}++;
1.103 albertel 626: my $location =&Apache::lonxml::get_param('location',$parstack,$safeeval);
1.39 albertel 627: if ( $Apache::optionresponse::conceptgroup
628: && !&Apache::response::showallfoils() ) {
1.22 albertel 629: push @{ $Apache::response::conceptgroup{'names'} }, $name;
630: $Apache::response::conceptgroup{"$name.value"} = $value;
1.70 albertel 631: if ($target eq 'tex' and $Apache::lonhomework::type eq 'exam') {
1.69 sakharuk 632: $Apache::response::conceptgroup{"$name.text"} = '\vskip 4 mm $\triangleright$ '.$text;
633: } else {
1.87 sakharuk 634: $Apache::response::conceptgroup{"$name.text"} = $text;
1.69 sakharuk 635: }
1.48 albertel 636: $Apache::response::conceptgroup{"$name.location"} = $location;
1.22 albertel 637: } else {
638: push @{ $Apache::response::foilgroup{'names'} }, $name;
1.48 albertel 639: $Apache::response::foilgroup{"$name.value"} = $value;
1.78 sakharuk 640: if ($target eq 'tex' and $Apache::lonhomework::type eq 'exam') {
1.69 sakharuk 641: $Apache::response::foilgroup{"$name.text"} = '\vskip 5 mm $\triangleright$ '.$text;
642: } else {
1.87 sakharuk 643: $Apache::response::foilgroup{"$name.text"} = $text;
1.69 sakharuk 644: }
1.48 albertel 645: $Apache::response::foilgroup{"$name.location"} = $location;
1.22 albertel 646: }
647: }
648: }
649: if ($target eq 'edit') {
650: $result.= &Apache::edit::tag_end($target,$token,'');
651: }
652: return $result;
1.1 albertel 653: }
654:
1.112 sakharuk 655: sub start_drawoptionlist {
656: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.117 albertel 657: if ($target ne 'meta') {
658: return $token->[4];
659: }
1.112 sakharuk 660: }
661:
662: sub end_drawoptionlist {
663: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.117 albertel 664: if ($target ne 'meta') {
665: return $token->[2];
666: }
1.112 sakharuk 667: }
668:
1.7 albertel 669: sub insert_foil {
1.22 albertel 670: return '
1.15 albertel 671: <foil name="" value="unused">
1.14 albertel 672: <startouttext />
673: <endouttext />
1.7 albertel 674: </foil>';
675: }
1.1 albertel 676: 1;
677: __END__
678:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>