Annotation of loncom/homework/rankresponse.pm, revision 1.75
1.1 albertel 1: # The LearningOnline Network with CAPA
2: # rank style response
3: #
1.75 ! raeburn 4: # $Id: rankresponse.pm,v 1.74 2018/06/07 15:58:11 raeburn Exp $
1.1 albertel 5: # Copyright Michigan State University Board of Trustees
6: #
7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
8: #
9: # LON-CAPA is free software; you can redistribute it and/or modify
10: # it under the terms of the GNU General Public License as published by
11: # the Free Software Foundation; either version 2 of the License, or
12: # (at your option) any later version.
13: #
14: # LON-CAPA is distributed in the hope that it will be useful,
15: # but WITHOUT ANY WARRANTY; without even the implied warranty of
16: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17: # GNU General Public License for more details.
18: #
19: # You should have received a copy of the GNU General Public License
20: # along with LON-CAPA; if not, write to the Free Software
21: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22: #
23: # /home/httpd/html/adm/gpl.txt
24: #
25: # http://www.lon-capa.org/
26: #
27:
28: package Apache::rankresponse;
29: use strict;
30: use HTML::Entities();
1.30 albertel 31: use Apache::optionresponse();
32: use Apache::lonlocal;
1.58 onken 33: use Apache::lonxml;
34: use Apache::lonnet;
1.1 albertel 35:
36: BEGIN {
37: &Apache::lonxml::register('Apache::rankresponse',('rankresponse'));
38: }
39:
40: sub start_rankresponse {
41: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
42: my $result;
43: #when in a rank response use these
44: &Apache::lonxml::register('Apache::rankresponse',
45: ('foilgroup','foil','conceptgroup'));
46: push (@Apache::lonxml::namespace,'rankresponse');
47: my $id = &Apache::response::start_response($parstack,$safeeval);
48: %Apache::hint::rank=();
1.30 albertel 49: undef(%Apache::response::foilnames);
1.1 albertel 50: if ($target eq 'meta') {
51: $result=&Apache::response::meta_package_write('rankresponse');
52: } elsif ($target eq 'edit' ) {
1.59 bisitz 53: $result.=&Apache::edit::start_table($token)
1.72 golterma 54: .'<tr><td>'.&Apache::loncommon::insert_folding_button()
55: .&Apache::lonxml::description($token).'</td>'
1.61 bisitz 56: .'<td><span class="LC_nobreak">'.&mt('Delete?').' '
1.59 bisitz 57: .&Apache::edit::deletelist($target,$token)
58: .'</span></td>'
1.60 raeburn 59: .'<td> '.&Apache::edit::end_row()
1.59 bisitz 60: .&Apache::edit::start_spanning_row();
1.1 albertel 61:
62: $result.=
63: &Apache::edit::text_arg('Max Number Of Shown Foils:','max',$token,'4').
1.59 bisitz 64: &Apache::edit::select_arg('Randomize Foil Order:','randomize',
1.1 albertel 65: ['yes','no'],$token).
66: &Apache::edit::end_row().&Apache::edit::start_spanning_row()."\n";
67: } elsif ($target eq 'modified') {
68: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
69: $safeeval,'max',
70: 'randomize');
71: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.25 albertel 72: } elsif ($target eq 'analyze') {
73: my $part_id="$Apache::inputtags::part.$id";
1.56 raeburn 74: $Apache::lonhomework::analyze{"$part_id.type"} = 'rankresponse';
1.25 albertel 75: push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
1.53 foxr 76: push (@{ $Apache::lonhomework::analyze{"$part_id.bubble_lines"} }, 1);
77:
1.1 albertel 78: }
79: return $result;
80: }
81:
82: sub end_rankresponse {
83: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
84: my $result;
85: if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
86: &Apache::response::end_response;
87: pop @Apache::lonxml::namespace;
88: &Apache::lonxml::deregister('Apache::rankresponse',
89: ('foilgroup','foil','conceptgroup'));
1.30 albertel 90: undef(%Apache::response::foilnames);
1.1 albertel 91: return $result;
92: }
93:
94: %Apache::response::foilgroup=();
95: sub start_foilgroup {
96: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
97: my $result;
98: %Apache::response::foilgroup=();
99: $Apache::rankresponse::conceptgroup=0;
1.64 raeburn 100: &Apache::response::pushrandomnumber(undef,$target);
1.1 albertel 101: return $result;
102: }
103:
104: sub end_foilgroup {
105: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
106: my $result;
1.26 albertel 107: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
108: $target eq 'tex' || $target eq 'analyze') {
1.1 albertel 109: my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
110: my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
111: $safeeval,'-2');
112: my $tol = &Apache::lonxml::get_param('tol',$parstack,$safeeval,'-2');
113: if (!defined($tol)) { $tol=0; }
1.3 sakharuk 114: if ($target eq 'web' || $target eq 'tex') {
1.1 albertel 115: $result=&displayfoils($target,$max,$randomize,$tol);
1.50 albertel 116: $Apache::lonxml::post_evaluate=0;
1.1 albertel 117: } elsif ($target eq 'answer' ) {
118: $result=&displayanswers($max,$randomize,$tol);
119: } elsif ( $target eq 'grade') {
120: &grade_response($max,$randomize,$tol);
1.25 albertel 121: } elsif ( $target eq 'analyze') {
122: my @shown = &whichfoils($max,$randomize);
123: &Apache::response::analyze_store_foilgroup(\@shown,
1.26 albertel 124: ['text','value','location']);
1.41 albertel 125: my $part_id=
126: "$Apache::inputtags::part.$Apache::inputtags::response[-1]";
127: $Apache::lonhomework::analyze{"$part_id.tol"}=$tol;
1.1 albertel 128: }
1.55 foxr 129: my $part = $Apache::inputtags::part;
130: my $id = $Apache::inputtags::response[-1];
1.66 raeburn 131: my ($numrows,$bubbles_per_row);
132: if (($target eq 'tex') && ($Apache::lonhomework::type eq 'exam')) {
133: my (@whichfoils)=&whichfoils($max,$randomize);
134: ($numrows,$bubbles_per_row) =
135: &Apache::optionresponse::getnumrows(scalar(@whichfoils));
136: }
137: if ($numrows < 1) {
138: $numrows = 1;
139: }
140: my $increment = &getfoilcounts($max) * $numrows;
141: &Apache::lonxml::increment_counter($increment,"$part.$id");
1.54 foxr 142: if ($target eq 'analyze') {
143: &Apache::lonhomework::set_bubble_lines();
144: }
1.14 albertel 145: } elsif ($target eq 'edit') {
146: $result=&Apache::edit::end_table();
1.1 albertel 147: }
1.34 albertel 148: &Apache::response::poprandomnumber();
1.1 albertel 149: return $result;
150: }
151:
152: sub get_correct_order {
153: my ($tol,@foils) =@_;
154: my @correctorder;
155: my @value_names;
156: foreach my $name (@foils) {
157: my @pair=($Apache::response::foilgroup{$name.'.value'},$name);
158: push(@value_names,\@pair);
159: }
160: @value_names =
161: sort {
1.17 albertel 162: if (abs($a->[0] - $b->[0]) > $tol) {return ($a->[0] <=> $b->[0]);}
1.1 albertel 163: return 0;
164: } @value_names;
165: my @value_names_tmp=@value_names;
166: my $firstpair=shift(@value_names_tmp);
167: my $order=1;
168: my %order;
169: my $count=1;
170: my $lastvalue=$firstpair->[0];
171: $order{$firstpair->[1]}=$order;
172: foreach my $pair (@value_names_tmp) {
173: $count++;
174: if (abs($pair->[0]-$lastvalue) > $tol ) {
175: $order=$count;
176: }
177: $order{$pair->[1]}=$order;
178: $lastvalue=$pair->[0];
179: }
180: foreach my $name (@foils) {
181: push(@correctorder,$order{$name});
182: }
183: &Apache::lonhomework::showhash('b' => \@value_names);
184: &Apache::lonhomework::showhash('b' => \@correctorder);
185: return @correctorder;
186: }
187:
188: sub displayanswers {
189: my ($max,$randomize,$tol,@opt)=@_;
1.68 raeburn 190: my @names;
191: if (ref($Apache::response::foilgroup{'names'}) eq 'ARRAY') {
192: @names = @{ $Apache::response::foilgroup{'names'} };
193: }
194: return if (!@names);
1.1 albertel 195: my @whichfoils = &whichfoils($max,$randomize);
196: my @correctorder=&get_correct_order($tol,@whichfoils);
1.49 albertel 197: my $result;
198: if ($Apache::lonhomework::type eq 'exam') {
199: my @alphabet = ('A'..'Z');
200: my $i=0;
201: foreach my $order (@correctorder) {
202: $result.=&Apache::response::answer_header('rankresponse',$i++);
203: $result.=&Apache::response::answer_part('rankresponse',
204: $alphabet[$order-1]);
205: $result.=&Apache::response::answer_part('rankresponse',$order);
206: $result.=&Apache::response::answer_footer('rankresponse');
207: }
208: } else {
209: $result=&Apache::response::answer_header('rankresponse');
210: foreach my $order (@correctorder) {
211: $result.=&Apache::response::answer_part('rankresponse',$order);
212: }
213: $result.=&Apache::response::answer_footer('rankresponse');
1.1 albertel 214: }
215: return $result;
216: }
217:
218: sub check_response_order {
219: my (%responsehash)=@_;
1.45 albertel 220: my @order=sort( {$a <=> $b} values(%responsehash));
1.1 albertel 221: my $lastvalue=0;
222: my $expected=1;
223: my $malformed=0;
224: foreach my $current (@order) {
225: &Apache::lonxml::debug("$lastvalue $expected $malformed");
226: if (!($current == $lastvalue || $current == $expected)) {
227: $malformed=1;
228: }
229: $expected++;
230: $lastvalue=$current;
231: }
232: return $malformed;
233: }
234:
235: sub grade_response {
236: my ($max,$randomize,$tol)=@_;
237: my (@whichfoils)=&whichfoils($max,$randomize);
1.44 albertel 238: if (!&Apache::response::submitted()) { return; }
1.1 albertel 239: my %responsehash;
240: my %grade;
1.28 albertel 241: my ($temp,$right,$wrong,$ignored)=(1,0,0,0);
1.1 albertel 242: my @correctorder=&get_correct_order($tol,@whichfoils);
1.66 raeburn 243: my ($numrows,$bubbles_per_row);
244: if ($Apache::lonhomework::scantronmode) {
245: my $numitems = scalar(@whichfoils);
246: ($numrows,$bubbles_per_row) =
247: &Apache::optionresponse::getnumrows($numitems);
248: }
249: if ($numrows < 1) {
250: $numrows = 1;
251: }
252:
1.1 albertel 253: foreach my $name (@whichfoils) {
1.66 raeburn 254: my $response;
255: if ($numrows > 1) {
256: my $num = $temp;
257: my $totalnum;
258: for (my $i=0; $i<$numrows; $i++) {
259: my $item = &Apache::response::getresponse($num,'A is 1');
260: if ($item =~ /^\d+$/) {
261: $totalnum = $i*$bubbles_per_row + $item;
262: }
263: $num ++;
264: }
265: $response = $totalnum;
266: $temp += $numrows;
267: } else {
268: $response = &Apache::response::getresponse($temp,'A is 1');
269: $temp ++;
270: }
1.1 albertel 271: my $value=shift(@correctorder);
272: if ( $response =~ /[^\s]/) {
1.27 albertel 273: $responsehash{$name}=$response;
1.1 albertel 274: &Apache::lonxml::debug("submitted a $response for $value<br />\n");
275: if ($value eq $response) {
276: $grade{$name}='1'; $right++;
277: } else {
278: $grade{$name}='0'; $wrong++;
279: }
280: } else {
281: $ignored++;
282: }
283: }
284: my $malformed=&check_response_order(%responsehash);
285: my $part=$Apache::inputtags::part;
286: my $id = $Apache::inputtags::response['-1'];
287: my $responsestr=&Apache::lonnet::hash2str(%responsehash);
288: my $gradestr =&Apache::lonnet::hash2str(%grade);
289: my %previous=&Apache::response::check_for_previous($responsestr,
290: $part,$id);
291: &Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored and was $malformed malformed");
292: my $ad;
293: if ($malformed) {
294: $ad='MISORDERED_RANK';
295: } elsif ($wrong==0 && $ignored==0) {
296: $ad='EXACT_ANS';
297: } elsif ($wrong==0 && $right==0) {
298: #nothing submitted
299: } else {
300: if ($ignored==0) {
301: $ad='INCORRECT';
302: } else {
303: $ad='MISSING_ANSWER';
304: }
305: }
1.62 raeburn 306: if (($ad eq 'INCORRECT' || $ad eq 'EXACT_ANS')) {
307: if ($Apache::lonhomework::type eq 'survey') {
308: $ad='SUBMITTED';
309: } elsif ($Apache::lonhomework::type eq 'surveycred') {
310: $ad='SUBMITTED_CREDIT';
311: } elsif ($Apache::lonhomework::type eq 'anonsurvey') {
312: $ad='ANONYMOUS';
313: } elsif ($Apache::lonhomework::type eq 'anonsurveycred') {
314: $ad='ANONYMOUS_CREDIT';
315: } else {
316: $Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=$gradestr;
317: }
1.40 albertel 318: } else {
319: $Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=$gradestr;
320: }
1.64 raeburn 321: if ($Apache::lonhomework::type eq 'randomizetry') {
322: if ($Apache::lonhomework::type eq 'randomizetry') {
323: $Apache::lonhomework::results{"resource.$part.$id.foilorder"} = &Apache::lonnet::array2str(@whichfoils);
324: }
325: }
1.1 albertel 326: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
327: $responsestr;
328: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
329: &Apache::response::handle_previous(\%previous,$ad);
330: }
331:
1.52 albertel 332: sub format_prior_answer {
333: my ($mode,$answer,$other_data) = @_;
334: my %lastresponse=&Apache::lonnet::str2hash($answer);
335: my $foil_order =$other_data->[0];
336: my %grading =&Apache::lonnet::str2hash($other_data->[1]);
337: my $output;
338: foreach my $name (@{ $foil_order }) {
1.71 bisitz 339: if (defined($lastresponse{$name})) {
340: $output .= '<tr><td>'.&HTML::Entities::encode($lastresponse{$name},'<>&"').'</td></tr>';
341: } else {
342: $output .= '<tr><td> </td></tr>';
343: }
1.52 albertel 344: }
345: return if (!defined($output));
346: $output =
347: '<table class="LC_prior_rank">'.$output.'</table>';
348: return $output;
349: }
350:
1.1 albertel 351: sub displayfoils {
352: my ($target,$max,$randomize,$tol)=@_;
353: my $result;
1.4 sakharuk 354: my @alphabet=('A'..'Z');
1.1 albertel 355: my (@whichfoils)=&whichfoils($max,$randomize);
356: my $part=$Apache::inputtags::part;
357: my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
358: my @whichopt=(1..($#whichfoils+1));
359: my @correctorder=&get_correct_order($tol,@whichfoils);
1.20 sakharuk 360: if ( &Apache::response::show_answer() && ($target ne 'tex')) {
1.1 albertel 361: foreach my $name (@whichfoils) {
362: my $text=$Apache::response::foilgroup{$name.'.text'};
363: my $value=shift(@correctorder);
1.46 albertel 364: if ($target eq 'web') {
1.73 damieng 365: $result.='<div class="LC_rankfoil"><b>'.$value.':</b> '.$text.'</div>';
1.46 albertel 366: } else {
367: $result.=' \strut\\\\\strut '.$value.':'.$text;
368: }
1.1 albertel 369: }
370: } else {
371: my $i = 0;
1.29 albertel 372: my $temp=1;
1.1 albertel 373: my $id=$Apache::inputtags::response[-1];
374: my $part=$Apache::inputtags::part;
1.64 raeburn 375: my ($lastresponse,$newvariation);
376: if ((($Apache::lonhomework::history{"resource.$part.type"} eq 'randomizetry') ||
377: ($Apache::lonhomework::type eq 'randomizetry')) &&
378: ($Apache::inputtags::status[-1] eq 'CAN_ANSWER')) {
379: if ($env{'form.'.$part.'.rndseed'} ne
380: $Apache::lonhomework::history{"resource.$part.rndseed"}) {
381: $newvariation = 1;
382: }
383: }
1.69 raeburn 384: unless ($newvariation) {
385: if ((($env{'form.grade_username'} eq '') && ($env{'form.grade_domain'} eq '')) ||
386: (($env{'form.grade_username'} eq $env{'user.name'}) &&
387: ($env{'form.grade_domain'} eq $env{'user.domain'}))) {
388: $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
389: } else {
390: unless (($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurvey') ||
391: ($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurveycred')) {
392: $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
393: }
394: }
1.62 raeburn 395: }
1.5 sakharuk 396: my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
397: my @alp = splice @alphabet, 0, $#whichopt + 1;
1.15 sakharuk 398: my $internal_counter=$Apache::lonxml::counter;
1.66 raeburn 399: my ($numrows,$bubbles_per_row);
400: if (($target eq 'tex') && ($Apache::lonhomework::type eq 'exam')) {
401: ($numrows,$bubbles_per_row) =
402: &Apache::optionresponse::getnumrows(scalar(@alp));
403: } else {
404: $numrows = 1;
405: }
1.58 onken 406: if($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes') {
407: $result .= '\strut \\\\ \strut \\\\' ;
408: }
409: foreach my $name (@whichfoils) {
1.1 albertel 410: my $lastopt=$lastresponse{$name};
1.75 ! raeburn 411: my $text=$Apache::response::foilgroup{$name.'.text'};
1.3 sakharuk 412: my $optionlist='';
1.58 onken 413: if ($target ne 'tex') {
1.74 raeburn 414: $optionlist = "<option value=\"\"></option>\n";
1.58 onken 415: }
416: if ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes') {
417: my $fieldname = $env{'request.symb'}.'&part_'.$Apache::inputtags::part.'&rankresponse'.'&HWVAL_'.$Apache::inputtags::response['-1'].':'.$temp;
418: $optionlist = &Apache::lonxml::print_pdf_start_combobox($fieldname);
419: }
1.1 albertel 420: my $option;
421: foreach $option (@whichopt) {
422: if ($option eq $lastopt) {
1.58 onken 423: if ($target ne 'tex' ) {
1.74 raeburn 424: $optionlist.="<option value=\"$option\" selected=\"selected\">$option</option>\n";
1.58 onken 425: } elsif ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes') {
426: $optionlist .= &Apache::lonxml::print_pdf_add_combobox_option($option);
427: }
1.1 albertel 428: } else {
1.58 onken 429: if ($target ne 'tex') {
1.74 raeburn 430: $optionlist.="<option value=\"$option\">$option</option>\n";
1.58 onken 431: } elsif ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes') {
432: $optionlist .= &Apache::lonxml::print_pdf_add_combobox_option($option);
433: }
434: }
1.1 albertel 435: }
1.12 sakharuk 436: if ($target ne 'tex' && $Apache::lonhomework::type ne 'exam') {
1.75 ! raeburn 437: my $labeltext = &mt('Choose ranking for:').' '.$text;
1.51 albertel 438: $optionlist='<select onchange="javascript:setSubmittedPart(\''.
439: $part.'\');" name="HWVAL_'.
1.75 ! raeburn 440: $Apache::inputtags::response[-1].':'.$temp.'" aria-label="'.$labeltext.'">'.
1.3 sakharuk 441: $optionlist."</select>\n";
1.58 onken 442: } elsif ($env{'form.pdfFormFields'} eq 'yes') {
443: #do nothing
444: } else {
1.3 sakharuk 445: $optionlist=' '.$temp.' '.$optionlist.' ';
446: }
447: if ($target ne 'tex') {
1.73 damieng 448: $result .= '<div class="LC_rankfoil">';
1.12 sakharuk 449: if ($Apache::lonhomework::type ne 'exam') {
1.73 damieng 450: $result.=$optionlist.$text."\n";
1.12 sakharuk 451: } else {
1.73 damieng 452: $result.=$text."\n";
1.12 sakharuk 453: }
1.4 sakharuk 454: if ($Apache::lonhomework::type eq 'exam') {
1.22 albertel 455: my @values=(1..scalar(@whichopt));
456: $result.=&Apache::optionresponse::webbubbles(\@values,\@whichopt,$temp,$lastopt);
1.4 sakharuk 457: }
1.73 damieng 458: $result .= '</div>';
1.4 sakharuk 459: } else {
460: if ($Apache::lonhomework::type eq 'exam') {
1.66 raeburn 461: my $itemlabel;
462: if ($numrows == 1) {
463: $itemlabel = '\item[\textbf{'.$internal_counter.'}]';
464: } else {
465: my $linetext;
466: for (my $i=0; $i<$numrows; $i++) {
467: $linetext .= $internal_counter+$i.', ';
468: }
469: $linetext =~ s/,\s$//;
470: $itemlabel = '\item[\small {\textbf{'.$linetext.'}}]'.
471: ' {\footnotesize '.
472: &mt('(Bubble once in [_1] lines)',$numrows).
473: '} \hspace*{\fill} \\\\';
474: }
1.63 foxr 475: $result .= '\vskip 0 mm '.$text.' \vskip 0 mm '."\n";
476: $result .= '\vskip -1 mm';
1.66 raeburn 477: $result .= '\begin{enumerate}'.$itemlabel;
478: $result .= &Apache::optionresponse::bubbles(\@alp,\@whichopt,'rankresponse',undef,$numrows,$bubbles_per_row,$internal_counter);
479: $result .= '\end{enumerate} \vskip -8 mm \strut ';
480: $internal_counter += $numrows;
1.4 sakharuk 481: } else {
1.58 onken 482: if($env{'form.pdfFormFields'} ne 'yes') {
483: $result.=' \vskip 0mm \framebox[5 mm][s]{\tiny\strut} '.$text."\n";
484: } else {
485: $result.= $optionlist.' '. &Apache::lonxml::print_pdf_end_combobox($text).'\strut \\\\';
486: }
1.4 sakharuk 487: }
488: }
1.1 albertel 489: $temp++;
490: }
491: }
1.52 albertel 492: if ($target eq 'web') {
1.64 raeburn 493: my $questiontype;
494: if ($Apache::lonhomework::type eq 'randomizetry') {
495: $questiontype = $Apache::lonhomework::type;
496: }
1.52 albertel 497: &Apache::response::setup_prior_tries_hash(\&format_prior_answer,
498: [\@whichfoils,
1.64 raeburn 499: 'submissiongrading'],
500: $questiontype);
1.52 albertel 501: }
1.5 sakharuk 502: if ($target ne 'tex') {$result.="<br />";} else {$result.=' \vskip 0 mm ';}
1.1 albertel 503: return $result;
504: }
505:
506: sub getfoilcounts {
507: my ($max)=@_;
508: # +1 since instructors will count from 1
509: my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
510: if (&Apache::response::showallfoils()) { $max=$count; }
511: if ($count>$max) { $count=$max }
512: &Apache::lonxml::debug("Count is $count from $max");
513: return $count;
514: }
515:
516: sub whichfoils {
517: my ($max,$randomize)=@_;
1.13 albertel 518: return &Apache::response::whichorder($max,$randomize,
519: &Apache::response::showallfoils(),
520: \%Apache::response::foilgroup);
1.1 albertel 521: }
522:
523: sub start_conceptgroup {
524: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
525: $Apache::rankresponse::conceptgroup=1;
526: %Apache::response::conceptgroup=();
527: my $result;
528: if ($target eq 'edit') {
529: $result.=&Apache::edit::tag_start($target,$token,
530: "Concept Grouped Foils");
531: $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
532: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
533: }
534: if ($target eq 'modified') {
535: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
536: $safeeval,'concept');
537: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
538: }
539: return $result;
540: }
541:
542: sub end_conceptgroup {
543: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
544: $Apache::rankresponse::conceptgroup=0;
545: my $result='';
1.25 albertel 546: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
547: $target eq 'tex' || $target eq 'analyze') {
1.1 albertel 548: #if not there aren't any foils to display and thus no question
1.25 albertel 549: &Apache::response::pick_foil_for_concept($target,
550: ['value','text','location'],
551: \%Apache::hint::rank,
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.25 albertel 567: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.1 albertel 568: &Apache::lonxml::startredirection;
1.39 albertel 569: if ($target eq 'analyze') {
570: &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
571: }
1.1 albertel 572: } elsif ($target eq 'edit') {
573: $result=&Apache::edit::tag_start($target,$token,"Foil");
574: my $level='-2';
575: if ($$tagstack[-2] eq 'conceptgroup') { $level = '-3'; }
576: $result.=&Apache::edit::text_arg('Name:','name',$token);
577: $result.= &Apache::edit::text_arg('Rank Value:','value',$token,'15');
578: my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
579: $safeeval,'-3');
580: if ($randomize ne 'no') {
581: $result.=&Apache::edit::select_arg('Location:','location',
582: ['random','top','bottom'],$token);
583: }
584: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
585: } elsif ($target eq 'modified') {
586: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
587: $safeeval,'value',
588: 'name','location');
589: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
590: }
591: return $result;
592: }
593:
594: sub end_foil {
595: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
596: my $text ='';
597: my $result = '';
1.25 albertel 598: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.1 albertel 599: $text=&Apache::lonxml::endredirection;
600: }
1.25 albertel 601: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
602: $target eq 'tex' || $target eq 'analyze') {
1.1 albertel 603: my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
1.42 albertel 604: if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
605: $text='\vskip 5mm $\triangleright$ '.$text;
606: }
1.1 albertel 607: if ($value ne 'unused') {
608: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
609: &Apache::lonxml::debug("Got a name of :$name:");
1.48 albertel 610: if ($name eq "") {
1.57 bisitz 611: &Apache::lonxml::warning(&mt('Foils without names exist. This can cause problems to malfunction.'));
1.43 albertel 612: $name=$Apache::lonxml::curdepth;
613: }
1.1 albertel 614: &Apache::lonxml::debug("Using a name of :$name:");
1.30 albertel 615: if (defined($Apache::response::foilnames{$name})) {
1.57 bisitz 616: &Apache::lonxml::error(&mt('Foil name [_1] appears more than once. Foil names need to be unique.','<b><tt>'.$name.'</tt></b>'));
1.30 albertel 617: }
1.31 albertel 618: $Apache::response::foilnames{$name}++;
1.30 albertel 619: my $location =&Apache::lonxml::get_param('location',$parstack,
620: $safeeval);
1.1 albertel 621: if ( $Apache::rankresponse::conceptgroup
622: && !&Apache::response::showallfoils() ) {
623: push @{ $Apache::response::conceptgroup{'names'} }, $name;
624: $Apache::response::conceptgroup{"$name.value"} = $value;
1.42 albertel 625: $Apache::response::conceptgroup{"$name.text"} = $text;
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.42 albertel 630: $Apache::response::foilgroup{"$name.text"} = $text;
1.1 albertel 631: $Apache::response::foilgroup{"$name.location"} = $location;
632: }
633: }
634: }
635: if ($target eq 'edit') {
636: $result.= &Apache::edit::tag_end($target,$token,'');
637: }
638: return $result;
639: }
640:
641: sub insert_foil {
642: return '
643: <foil name="" value="unused">
644: <startouttext />
645: <endouttext />
646: </foil>';
647: }
648: 1;
649: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>