1: # The LearningOnline Network with CAPA
2: # input definitons
3: #
4: # $Id: inputtags.pm,v 1.192 2006/05/30 12:45:37 www Exp $
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: package Apache::inputtags;
29: use HTML::Entities();
30: use strict;
31: use Apache::loncommon;
32: use Apache::lonlocal;
33: use Apache::lonnet;
34: use lib '/home/httpd/lib/perl/';
35: use LONCAPA;
36:
37:
38: BEGIN {
39: &Apache::lonxml::register('Apache::inputtags',('hiddenline','textfield','textline'));
40: }
41:
42: # Initializes a set of global variables used during the parse of the problem.
43: #
44: # @Apache::inputtags::input - List of current input ids.
45: # @Apache::inputtags::inputlist - List of all input ids seen this problem.
46: # @Apache::inputtags::response - List of all current resopnse ids.
47: # @Apache::inputtags::responselist - List of all response ids seen this
48: # problem.
49: # @Apache::inputtags::hint - List of all hint ids.
50: # @Apache::inputtags::hintlist - List of all hint ids seen this problem.
51: # @Apache::inputtags::previous - List describing if specific responseds
52: # have been used
53: # @Apache::inputtags::previous_version - Submission responses were used in.
54: # $Apache::inputtags::part - Current part id (valid only in
55: # <problem>)
56: # 0 if not in a part.
57: # @Apache::inputtags::partlist - List of part ids seen in the current
58: # <problem>
59: # @Apache::inputtags::status - List of problem statuses. First
60: # element is the status of the <problem>
61: # the remainder are for individual <part>s.
62: # %Apache::inputtags::params - Hash of defined parameters for the
63: # current response.
64: # @Apache::inputtags::import - List of all ids for <import> thes get
65: # join()ed and prepended.
66: # @Apache::inputtags::importlist - List of all import ids seen.
67: # $Apache::inputtags::response_with_no_part
68: # - Flag set true if we have seen a response
69: # that is not inside a <part>
70: # %Apache::inputtags::answertxt - <*response> tags store correct
71: # answer strings for display by <textline/>
72: # in this hash.
73:
74: sub initialize_inputtags {
75: @Apache::inputtags::input=();
76: @Apache::inputtags::inputlist=();
77: @Apache::inputtags::response=();
78: @Apache::inputtags::responselist=();
79: @Apache::inputtags::hint=();
80: @Apache::inputtags::hintlist=();
81: @Apache::inputtags::previous=();
82: @Apache::inputtags::previous_version=();
83: $Apache::inputtags::part='';
84: @Apache::inputtags::partlist=();
85: @Apache::inputtags::status=();
86: %Apache::inputtags::params=();
87: @Apache::inputtags::import=();
88: @Apache::inputtags::importlist=();
89: $Apache::inputtags::response_with_no_part=0;
90: %Apache::inputtags::answertxt=();
91: }
92:
93: sub check_for_duplicate_ids {
94: my %check;
95: foreach my $id (@Apache::inputtags::partlist,
96: @Apache::inputtags::responselist,
97: @Apache::inputtags::hintlist,
98: @Apache::inputtags::importlist) {
99: $check{$id}++;
100: }
101: my @duplicates;
102: foreach my $id (sort(keys(%check))) {
103: if ($check{$id} > 1) {
104: push(@duplicates,$id);
105: }
106: }
107: if (@duplicates) {
108: &Apache::lonxml::error("Duplicated ids found, problem will operate incorrectly. Duplicated ids seen: ",join(', ',@duplicates));
109: }
110: }
111:
112: sub start_input {
113: my ($parstack,$safeeval)=@_;
114: my $id = &Apache::lonxml::get_param('id',$parstack,$safeeval);
115: if ($id eq '') { $id = $Apache::lonxml::curdepth; }
116: push (@Apache::inputtags::input,$id);
117: push (@Apache::inputtags::inputlist,$id);
118: return $id;
119: }
120:
121: sub end_input {
122: pop @Apache::inputtags::input;
123: return '';
124: }
125:
126: sub addchars {
127: my ($fieldid,$addchars)=@_;
128: my $output='';
129: foreach (split(/\,/,$addchars)) {
130: $output.='<a href="javascript:void(document.forms.lonhomework.'.
131: $fieldid.'.value+=\''.$_.'\')">'.$_.'</a> ';
132: }
133: return $output;
134: }
135:
136: sub start_textfield {
137: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
138: my $result = "";
139: my $id = &start_input($parstack,$safeeval);
140: my $resid=$Apache::inputtags::response[-1];
141: if ($target eq 'web') {
142: $Apache::lonxml::evaluate--;
143: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
144: my $partid=$Apache::inputtags::part;
145: my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$resid.submission"},'<>&"');
146: my $cols = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
147: if ( $cols eq '') { $cols = 80; }
148: my $rows = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
149: if ( $rows eq '') { $rows = 16; }
150: my $addchars=&Apache::lonxml::get_param('addchars',$parstack,$safeeval);
151: $result='';
152: if ($addchars) {
153: $result.=&addchars('HWVAL_'.$resid,$addchars);
154: }
155: &Apache::lonhtmlcommon::add_htmlareafields('HWVAL_'.$resid);
156: $result.= '<textarea wrap="hard" name="HWVAL_'.$resid.'" id="HWVAL_'.$resid.'" '.
157: "rows=\"$rows\" cols=\"$cols\">".$oldresponse;
158: if ($oldresponse ne '') {
159:
160: #get rid of any startup text if the user has already responded
161: &Apache::lonxml::get_all_text("/textfield",$parser,$style);
162: }
163: } else {
164: #right or wrong don't show it
165: #$result='<table border="1"><tr><td><i>'.$oldresponse.'</i></td></tr></table>';
166: $result='';
167: #get rid of any startup text
168: &Apache::lonxml::get_all_text("/textfield",$parser,$style);
169: }
170: } elsif ($target eq 'grade') {
171: my $seedtext=&Apache::lonxml::get_all_text("/textfield",$parser,
172: $style);
173: if ($seedtext eq $env{'form.HWVAL_'.$resid}) {
174: # if the seed text is still there it wasn't a real submission
175: $env{'form.HWVAL_'.$resid}='';
176: }
177: } elsif ($target eq 'edit') {
178: $result.=&Apache::edit::tag_start($target,$token);
179: $result.=&Apache::edit::text_arg('Rows:','rows',$token,4);
180: $result.=&Apache::edit::text_arg('Columns:','cols',$token,4);
181: $result.=&Apache::edit::text_arg
182: ('Click-On Texts (comma sep):','addchars',$token,10);
183: my $bodytext=&Apache::lonxml::get_all_text("/textfield",$parser,
184: $style);
185: $result.=&Apache::edit::editfield($token->[1],$bodytext,'Text you want to appear by default:',80,2);
186: } elsif ($target eq 'modified') {
187: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
188: $safeeval,'rows','cols',
189: 'addchars');
190: if ($constructtag) {
191: $result = &Apache::edit::rebuild_tag($token);
192: } else {
193: $result=$token->[4];
194: }
195: $result.=&Apache::edit::modifiedfield("/textfield",$parser);
196: } elsif ($target eq 'tex') {
197: my $number_of_lines = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
198: my $width_of_box = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
199: if ($$tagstack[-2] eq 'essayresponse' and $Apache::lonhomework::type eq 'exam') {
200: $result = '\fbox{\fbox{\parbox{\textwidth-5mm}{';
201: for (my $i=0;$i<int $number_of_lines*2;$i++) {$result.='\strut \\\\ ';}
202: $result.='\strut \\\\\strut \\\\\strut \\\\\strut \\\\}}}';
203: } else {
204: my $TeXwidth=$width_of_box/80;
205: $result = '\vskip 1 mm \fbox{\fbox{\parbox{'.$TeXwidth.'\textwidth-5mm}{';
206: for (my $i=0;$i<int $number_of_lines*2;$i++) {$result.='\strut \\\\ ';}
207: $result.='}}}\vskip 2 mm ';
208: }
209: }
210: return $result;
211: }
212:
213: sub end_textfield {
214: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
215: my $result;
216: if ($target eq 'web') {
217: $Apache::lonxml::evaluate++;
218: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
219: return "</textarea>";
220: }
221: } elsif ($target eq 'edit') {
222: $result=&Apache::edit::end_table();
223: }
224: &end_input;
225: return $result;
226: }
227:
228: sub exam_score_line {
229: my ($target) = @_;
230:
231: my $result;
232: if ($target eq 'tex') {
233: my $repetition = &Apache::response::repetition();
234: $result.='\begin{enumerate}';
235: if ($env{'request.state'} eq "construct" ) {$result.='\item[\strut]';}
236: foreach my $i (0..$repetition-1) {
237: $result.='\item[\textbf{'.
238: ($Apache::lonxml::counter+$i).
239: '}.]\textit{Leave blank on scoring form}\vskip 0 mm';
240: }
241: $result.= '\end{enumerate}';
242: }
243:
244: return $result;
245: }
246:
247: sub exam_box {
248: my ($target) = @_;
249: my $result;
250:
251: if ($target eq 'tex') {
252: $result .= '\fbox{\fbox{\parbox{\textwidth-5mm}{\strut\\\\\strut\\\\\strut\\\\\strut\\\\}}}';
253: $result .= &exam_score_line($target);
254: } elsif ($target eq 'web') {
255: my $id=$Apache::inputtags::response[-1];
256: $result.= '<br /><br />
257: <textarea name="HWVAL_'.$id.'" rows="4" cols="50">
258: </textarea> <br /><br />';
259: }
260: return $result;
261: }
262:
263: sub needs_exam_box {
264: my ($tagstack) = @_;
265: my @tags = ('formularesponse',
266: 'stringresponse',
267: 'reactionresponse',
268: 'organicresponse',
269: );
270:
271: foreach my $tag (@tags) {
272: if (grep(/\Q$tag\E/,@$tagstack)) {
273: return 1;
274: }
275: }
276: return 0;
277: }
278:
279: sub start_textline {
280: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
281: my $result = "";
282: if ($target eq 'web') {
283: $Apache::lonxml::evaluate--;
284: my $partid=$Apache::inputtags::part;
285: my $id=$Apache::inputtags::response[-1];
286: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
287: my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
288: my $maxlength;
289: if ($size eq '') { $size=20; } else {
290: if ($size < 20) { $maxlength=$size; }
291: }
292: my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$id.submission"},'<>&"');
293: if ($Apache::lonhomework::type ne 'exam') {
294: my $addchars=&Apache::lonxml::get_param('addchars',$parstack,$safeeval);
295: $result='';
296: if ($addchars) {
297: $result.=&addchars('HWVAL_'.$id,$addchars);
298: }
299: my $readonly=&Apache::lonxml::get_param('readonly',$parstack,
300: $safeeval);
301: if (lc($readonly) eq 'yes') {
302: $readonly=' readonly="readonly" ';
303: } else {
304: $readonly='';
305: }
306: $result.= '<input type="text" '.$readonly.' name="HWVAL_'.$id.'" value="'.
307: $oldresponse.'" size="'.$size.'" maxlength="'.$maxlength.'" />';
308: }
309: if ($Apache::lonhomework::type eq 'exam'
310: && &needs_exam_box($tagstack)) {
311: $result.=&exam_box($target);
312: }
313: } else {
314: #right or wrong don't show what was last typed in.
315: $result='<b>'.$Apache::inputtags::answertxt{$id}.'</b>';
316: #$result='';
317: }
318: } elsif ($target eq 'edit') {
319: $result=&Apache::edit::tag_start($target,$token);
320: $result.=&Apache::edit::text_arg('Size:','size',$token,'5').
321: &Apache::edit::text_arg('Click-On Texts (comma sep):',
322: 'addchars',$token,10);
323: $result.=&Apache::edit::select_arg('Readonly:','readonly',
324: ['no','yes'],$token);
325: $result.=&Apache::edit::end_row();
326: $result.=&Apache::edit::end_table();
327: } elsif ($target eq 'modified') {
328: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
329: $safeeval,'size',
330: 'addchars','readonly');
331: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
332: } elsif ($target eq 'tex'
333: && $Apache::lonhomework::type ne 'exam') {
334: my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
335: if ($size != 0) {$size=$size*2; $size.=' mm';} else {$size='40 mm';}
336: $result='\framebox['.$size.'][s]{\tiny\strut}';
337:
338: } elsif ($target eq 'tex'
339: && $Apache::lonhomework::type eq 'exam'
340: && &needs_exam_box($tagstack)) {
341: $result.=&exam_box($target);
342: }
343: return $result;
344: }
345:
346: sub end_textline {
347: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
348: if ($target eq 'web') { $Apache::lonxml::evaluate++; }
349: elsif ($target eq 'edit') { return ('','no'); }
350: return "";
351: }
352:
353: sub start_hiddenline {
354: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
355: my $result = "";
356: if ($target eq 'web') {
357: $Apache::lonxml::evaluate--;
358: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
359: my $partid=$Apache::inputtags::part;
360: my $id=$Apache::inputtags::response[-1];
361: my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$id.submission"},'<>&"');
362: if ($Apache::lonhomework::type ne 'exam') {
363: $result= '<input type="hidden" name="HWVAL_'.$id.'" value="'.
364: $oldresponse.'" />';
365: }
366: }
367: } elsif ($target eq 'edit') {
368: $result=&Apache::edit::tag_start($target,$token);
369: $result.=&Apache::edit::end_table;
370: }
371:
372: if ( ($target eq 'web' || $target eq 'tex')
373: && $Apache::lonhomework::type eq 'exam'
374: && &needs_exam_box($tagstack)) {
375: $result.=&exam_box($target);
376: }
377: return $result;
378: }
379:
380: sub end_hiddenline {
381: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
382: if ($target eq 'web') { $Apache::lonxml::evaluate++; }
383: elsif ($target eq 'edit') { return ('','no'); }
384: return "";
385: }
386:
387: # $part -> partid
388: # $id -> responseid
389: # $uploadefiletypes -> comma seperated list of extensions allowed or * for any
390: # $which -> 'uploadedonly' -> only newly uploaded files
391: # 'portfolioonly' -> only allow files from portfolio
392: # 'both' -> allow files from either location
393: # $extratext -> additional text to go between the link and the input box
394: # returns a table row <tr>
395: sub file_selector {
396: my ($part,$id,$uploadedfiletypes,$which,$extratext)=@_;
397: if (!$uploadedfiletypes) { return ''; }
398:
399: my $jspart=$part;
400: $jspart=~s/\./_/g;
401:
402: my $result;
403:
404: $result.='<tr><td>';
405: if ($uploadedfiletypes ne '*') {
406: $result.=
407: &mt('Allowed filetypes: <b>[_1]</b>',$uploadedfiletypes).'<br />';
408: }
409: if ($which eq 'uploadonly' || $which eq 'both') {
410: $result.=&mt('Submit a file: (only one file can be uploaded)').
411: ' <br /><input type="file" size="50" name="HWFILE'.
412: $jspart.'_'.$id.'" /><br />';
413: my $uploadedfile= &HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.uploadedfile"},'<>&"');
414:
415: if ($uploadedfile) {
416: my $url=$Apache::lonhomework::history{"resource.$part.$id.uploadedurl"};
417: &Apache::lonxml::extlink($url);
418: &Apache::lonnet::allowuploaded('/adm/essayresponse',$url);
419: my $icon=&Apache::loncommon::icon($url);
420: my $curfile='<a href="'.$url.'"><img src="'.$icon.
421: '" border="0" />'.$uploadedfile.'</a>';
422: $result.=&mt('Currently submitted: <tt>[_1]</tt>',$curfile);
423: } else {
424: #$result.=&mt('(Hand in a file you have prepared on your computer)');
425: }
426: }
427: if ( $which eq 'both') {
428: $result.='<br />'.'<strong>'.&mt('OR:').'</strong><br />';
429: }
430: if ($which eq 'portfolioonly' || $which eq 'both') {
431: $result.=$extratext.'<a href='."'".'javascript:void(window.open("/adm/portfolio?mode=selectfile&fieldname=HWPORT'.$jspart.'_'.$id.'","cat","height=600,width=800,scrollbars=1,resizable=1,menubar=2,location=1"))'."'".'>'.
432: &mt('Select Portfolio Files').'</a><br />'.
433: '<input type="text" size="50" name="HWPORT'.$jspart.'_'.$id.'" value="" />'.
434: '<br />';
435: if ($Apache::lonhomework::history{"resource.$part.$id.portfiles"}=~/[^\s]/){
436: my (@filelist,@bad_file_list);
437: foreach my $file (split(',',&unescape($Apache::lonhomework::history{"resource.$part.$id.portfiles"}))) {
438: my (undef,undef,$domain,$user)=&Apache::lonxml::whichuser();
439: my $url="/uploaded/$domain/$user/portfolio$file";
440: my $icon=&Apache::loncommon::icon($url);
441: push(@filelist,'<a href="'.$url.'"><img src="'.$icon.
442: '" border="0" />'.$file.'</a>');
443: if (! &Apache::lonnet::stat_file($url)) {
444: push(@bad_file_list,'<a href="'.$url.'"><img src="'.$icon.
445: '" border="0" />'.$file.'</a>');
446: }
447: }
448: $result.=&mt("Portfolio files previously selected: <strong>[_1]</strong>",join(', ',@filelist));
449: if (@bad_file_list) {
450: $result.='<br />'.&mt('<font color="red">These file(s) don\'t exist:</font> <strong>[_1]</strong>',join(', ',@bad_file_list));
451: }
452: }
453: }
454: $result.='</td></tr>';
455: return $result;
456: }
457:
458: sub checkstatus {
459: my ($value,$awardref,$msgref)=@_;
460: for (my $i=0;$i<=$#$awardref;$i++) {
461: if ($$awardref[$i] eq $value) {
462: return ($$awardref[$i],$$msgref[$i]);
463: }
464: }
465: return(undef,undef);
466: }
467:
468: sub valid_award {
469: my ($award) =@_;
470: foreach my $possibleaward ('EXTRA_ANSWER','MISSING_ANSWER', 'ERROR',
471: 'NO_RESPONSE',
472: 'TOO_LONG', 'UNIT_INVALID_INSTRUCTOR',
473: 'UNIT_INVALID_STUDENT', 'UNIT_IRRECONCIBLE',
474: 'UNIT_FAIL', 'NO_UNIT',
475: 'UNIT_NOTNEEDED', 'WANTED_NUMERIC',
476: 'BAD_FORMULA', 'SIG_FAIL', 'INCORRECT',
477: 'MISORDERED_RANK', 'INVALID_FILETYPE',
478: 'DRAFT', 'SUBMITTED', 'ASSIGNED_SCORE',
479: 'APPROX_ANS', 'EXACT_ANS','COMMA_FAIL') {
480: if ($award eq $possibleaward) { return 1; }
481: }
482: return 0;
483: }
484:
485: sub finalizeawards {
486: my ($awardref,$msgref,$nameref,$reverse)=@_;
487: my $result=undef;
488: my $award;
489: my $msg;
490: if ($#$awardref == -1) { $result = "NO_RESPONSE"; }
491: if ($result eq '' ) {
492: my $blankcount;
493: foreach $award (@$awardref) {
494: if ($award eq '') {
495: $result='MISSING_ANSWER';
496: $blankcount++;
497: }
498: }
499: if ($blankcount == ($#$awardref + 1)) { $result = 'NO_RESPONSE'; }
500: }
501: if (defined($result)) { return ($result,$msg); }
502:
503: # these awards are ordered from most important error through best correct
504:
505: my @awards = ('EXTRA_ANSWER', 'MISSING_ANSWER', 'ERROR', 'NO_RESPONSE',
506: 'TOO_LONG',
507: 'UNIT_INVALID_INSTRUCTOR', 'UNIT_INVALID_STUDENT',
508: 'UNIT_IRRECONCIBLE', 'UNIT_FAIL', 'NO_UNIT',
509: 'UNIT_NOTNEEDED', 'WANTED_NUMERIC', 'BAD_FORMULA',
510: 'COMMA_FAIL', 'SIG_FAIL', 'INCORRECT', 'MISORDERED_RANK',
511: 'INVALID_FILETYPE', 'DRAFT', 'SUBMITTED', 'ASSIGNED_SCORE',
512: 'APPROX_ANS', 'EXACT_ANS');
513: if ($reverse) { @awards=reverse(@awards); }
514: foreach my $possibleaward (@awards) {
515: ($result,$msg)=&checkstatus($possibleaward,$awardref,$msgref);
516: if (defined($result)) { return ($result,$msg); }
517: }
518: return ('ERROR',undef);
519: }
520:
521: sub decideoutput {
522: my ($award,$awarded,$awardmsg,$solved,$previous,$target)=@_;
523: my $message='';
524: my $button=0;
525: my $previousmsg;
526: my $bgcolor='orange';
527: my $added_computer_text=0;
528: my %possiblecolors =
529: ( 'correct' => '#aaffaa',
530: 'charged_try' => '#ffaaaa',
531: 'not_charged_try' => '#ffffaa',
532: 'no_message' => '#fffff',
533: );
534:
535: my $part = $Apache::inputtags::part;
536: my $handgrade =
537: ('yes' eq lc(&Apache::lonnet::EXT("resource.$part.handgrade")));
538:
539: my $computer = ($handgrade)? ''
540: : " ".&mt("Computer's answer now shown above.");
541: &Apache::lonxml::debug("handgrade has :$handgrade:");
542:
543: if ($previous) { $previousmsg=&mt('You have entered that answer before'); }
544:
545: if ($solved =~ /^correct/) {
546: $bgcolor=$possiblecolors{'correct'};
547: $message=&mt('You are correct.');
548: if ($awarded < 1 && $awarded > 0) {
549: $message=&mt('You are partially correct.');
550: $bgcolor=$possiblecolors{'not_charged_try'};
551: } elsif ($awarded < 1) {
552: $message=&mt('Incorrect.');
553: $bgcolor=$possiblecolors{'charged_try'};
554: }
555: if ($env{'request.filename'} =~
556: m|/res/lib/templates/examupload.problem$|) {
557: $message = &mt("A score has been assigned.");
558: $added_computer_text=1;
559: } else {
560: if ($target eq 'tex') {
561: $message = '\textbf{'.$message.'}';
562: } else {
563: $message = "<b>".$message."</b>";
564: $message.= $computer;
565: }
566: $added_computer_text=1;
567: unless ($env{'course.'.
568: $env{'request.course.id'}.
569: '.disable_receipt_display'} eq 'yes') {
570: $message.=(($target eq 'web')?'<br />':' ').
571: &mt('Your receipt is').' '.&Apache::lonnet::receipt($Apache::inputtags::part).
572: (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'');
573: }
574: }
575: $button=0;
576: $previousmsg='';
577: } elsif ($solved =~ /^excused/) {
578: if ($target eq 'tex') {
579: $message = ' \textbf{'.&mt('You are excused from the problem.').'} ';
580: } else {
581: $message = "<b>".&mt('You are excused from the problem.')."</b>";
582: }
583: $bgcolor=$possiblecolors{'charged_try'};
584: $button=0;
585: $previousmsg='';
586: } elsif ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
587: if ($solved =~ /^incorrect/ || $solved eq '') {
588: $message = &mt("Incorrect").".";
589: $bgcolor=$possiblecolors{'charged_try'};
590: $button=1;
591: } else {
592: if ($target eq 'tex') {
593: $message = '\textbf{'.&mt('You are correct.').'}';
594: } else {
595: $message = "<b>".&mt('You are correct.')."</b>";
596: $message.= $computer;
597: }
598: $added_computer_text=1;
599: unless ($env{'course.'.
600: $env{'request.course.id'}.
601: '.disable_receipt_display'} eq 'yes') {
602: $message.=(($target eq 'web')?'<br />':' ').
603: 'Your receipt is '.&Apache::lonnet::receipt($Apache::inputtags::part).
604: (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'');
605: }
606: $bgcolor=$possiblecolors{'correct'};
607: $button=0;
608: $previousmsg='';
609: }
610: } elsif ($award eq 'NO_RESPONSE') {
611: $message = '';
612: $bgcolor=$possiblecolors{'no_feedback'};
613: $button=1;
614: } elsif ($award eq 'EXTRA_ANSWER') {
615: $message = &mt('Some extra items were submitted.');
616: $bgcolor=$possiblecolors{'not_charged_try'};
617: $button = 1;
618: } elsif ($award eq 'MISSING_ANSWER') {
619: $message = &mt('Some items were not submitted.');
620: $bgcolor=$possiblecolors{'not_charged_try'};
621: $button = 1;
622: } elsif ($award eq 'ERROR') {
623: $message = &mt('An error occured while grading your answer.');
624: $bgcolor=$possiblecolors{'not_charged_try'};
625: $button = 1;
626: } elsif ($award eq 'TOO_LONG') {
627: $message = &mt("The submitted answer was too long.");
628: $bgcolor=$possiblecolors{'not_charged_try'};
629: $button=1;
630: } elsif ($award eq 'WANTED_NUMERIC') {
631: $message = &mt("This question expects a numeric answer.");
632: $bgcolor=$possiblecolors{'not_charged_try'};
633: $button=1;
634: } elsif ($award eq 'MISORDERED_RANK') {
635: $message = &mt('You have provided an invalid ranking');
636: if ($target ne 'tex') {
637: $message.=', '.&mt('please refer to').' '.&Apache::loncommon::help_open_topic('Ranking_Problems','help on ranking problems');
638: }
639: $bgcolor=$possiblecolors{'not_charged_try'};
640: $button=1;
641: } elsif ($award eq 'INVALID_FILETYPE') {
642: $message = &mt('Submission won\'t be graded. The type of file submitted is not allowed.');
643: $bgcolor=$possiblecolors{'not_charged_try'};
644: $button=1;
645: } elsif ($award eq 'SIG_FAIL') {
646: my ($used,$min,$max)=split(':',$awardmsg);
647: my $word;
648: if ($used < $min) { $word=&mt('more'); }
649: if ($used > $max) { $word=&mt('fewer'); }
650: $message = &mt("Submission not graded. Use [_2] digits.",$used,$word);
651: $bgcolor=$possiblecolors{'not_charged_try'};
652: $button=1;
653: } elsif ($award eq 'UNIT_INVALID_INSTRUCTOR') {
654: $message = &mt('Error in instructor specifed unit. This error has been reported to the instructor.', $awardmsg);
655: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');}
656: $bgcolor=$possiblecolors{'not_charged_try'};
657: $button=1;
658: } elsif ($award eq 'UNIT_INVALID_STUDENT') {
659: $message = &mt('Unable to interpret units. Computer reads units as "[_1]".',&markup_unit($awardmsg,$target));
660: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');}
661: $bgcolor=$possiblecolors{'not_charged_try'};
662: $button=1;
663: } elsif ($award eq 'UNIT_FAIL' || $award eq 'UNIT_IRRECONCIBLE') {
664: $message = &mt('Incompatible units. No conversion found between "[_1]" and the required units.',&markup_unit($awardmsg,$target));
665: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');}
666: $bgcolor=$possiblecolors{'not_charged_try'};
667: $button=1;
668: } elsif ($award eq 'UNIT_NOTNEEDED') {
669: $message = &mt('Only a number required. Computer reads units of "[_1]".',&markup_unit($awardmsg,$target));
670: $bgcolor=$possiblecolors{'not_charged_try'};
671: $button=1;
672: } elsif ($award eq 'NO_UNIT') {
673: $message = &mt("Units required").'.';
674: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units')};
675: $bgcolor=$possiblecolors{'not_charged_try'};
676: $button=1;
677: } elsif ($award eq 'COMMA_FAIL') {
678: $message = &mt("Proper comma separation is required").'.';
679: $bgcolor=$possiblecolors{'not_charged_try'};
680: $button=1;
681: } elsif ($award eq 'BAD_FORMULA') {
682: $message = &mt("Unable to understand formula");
683: $bgcolor=$possiblecolors{'not_charged_try'};
684: $button=1;
685: } elsif ($award eq 'INCORRECT') {
686: $message = &mt("Incorrect").'.';
687: $bgcolor=$possiblecolors{'charged_try'};
688: $button=1;
689: } elsif ($award eq 'SUBMITTED') {
690: $message = &mt("Your submission has been recorded.");
691: $bgcolor=$possiblecolors{'correct'};
692: $button=1;
693: } elsif ($award eq 'DRAFT') {
694: $message = &mt("A draft copy has been saved.");
695: $bgcolor=$possiblecolors{'not_charged_try'};
696: $button=1;
697: } elsif ($award eq 'ASSIGNED_SCORE') {
698: $message = &mt("A score has been assigned.");
699: $bgcolor=$possiblecolors{'correct'};
700: $button=0;
701: } elsif ($award eq '') {
702: if ($handgrade && $Apache::inputtags::status[-1] eq 'SHOW_ANSWER') {
703: $message = &mt("Nothing submitted.");
704: $bgcolor=$possiblecolors{'charged_try'};
705: } else {
706: $bgcolor=$possiblecolors{'not_charged_try'};
707: }
708: $button=1;
709: } else {
710: $message = &mt("Unknown message").": $award";
711: $button=1;
712: }
713: if (lc($Apache::lonhomework::problemstatus) eq 'no' &&
714: $Apache::inputtags::status[-1] ne 'SHOW_ANSWER') {
715: $message = &mt("Answer Submitted: Your final submission will be graded after the due date.");
716: $bgcolor=$possiblecolors{'correct'};
717: $button=1;
718: }
719: if ($Apache::inputtags::status[-1] eq 'SHOW_ANSWER' &&
720: !$added_computer_text && $target ne 'tex') {
721: $message.= $computer;
722: $added_computer_text=1;
723: }
724: return ($button,$bgcolor,$message,$previousmsg);
725: }
726:
727: sub markup_unit {
728: my ($unit,$target)=@_;
729: if ($target eq 'tex') {
730: return '\texttt{'.&Apache::lonxml::latex_special_symbols($unit).'}';
731: } else {
732: return "<tt>".$unit."</tt>";
733: }
734: }
735:
736: sub removealldata {
737: my ($id)=@_;
738: foreach my $key (keys(%Apache::lonhomework::results)) {
739: if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
740: &Apache::lonxml::debug("Removing $key");
741: delete($Apache::lonhomework::results{$key});
742: }
743: }
744: }
745:
746: sub hidealldata {
747: my ($id)=@_;
748: foreach my $key (keys(%Apache::lonhomework::results)) {
749: if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
750: &Apache::lonxml::debug("Hidding $key");
751: my $newkey=$key;
752: $newkey=~s/^(resource\.\Q$id\E\.[^\.]+\.)(.*)$/${1}hidden${2}/;
753: $Apache::lonhomework::results{$newkey}=
754: $Apache::lonhomework::results{$key};
755: delete($Apache::lonhomework::results{$key});
756: }
757: }
758: }
759:
760: sub setgradedata {
761: my ($award,$msg,$id,$previously_used) = @_;
762: if ($Apache::lonhomework::scantronmode &&
763: &Apache::lonnet::validCODE($env{'form.CODE'})) {
764: $Apache::lonhomework::results{"resource.CODE"}=$env{'form.CODE'};
765: } elsif ($Apache::lonhomework::scantronmode &&
766: $env{'form.CODE'} eq '' &&
767: $Apache::lonhomework::history{"resource.CODE"} ne '') {
768: $Apache::lonhomework::results{"resource.CODE"}='';
769: }
770:
771: if (!$Apache::lonhomework::scantronmode &&
772: $Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
773: $Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER') {
774: $Apache::lonhomework::results{"resource.$id.afterduedate"}=$award;
775: return '';
776: } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} !~
777: /^correct/ || $Apache::lonhomework::scantronmode ||
778: lc($Apache::lonhomework::problemstatus) eq 'no') {
779: # the student doesn't already have it correct,
780: # or we are in a mode (scantron orno problem status) where a correct
781: # can become incorrect
782: # handle assignment of tries and solved status
783: my $solvemsg;
784: if ($Apache::lonhomework::scantronmode) {
785: $solvemsg='correct_by_scantron';
786: } else {
787: $solvemsg='correct_by_student';
788: }
789: if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
790: $Apache::lonhomework::results{"resource.$id.afterduedate"}='';
791: }
792: if ( $award eq 'ASSIGNED_SCORE') {
793: $Apache::lonhomework::results{"resource.$id.tries"} =
794: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
795: $Apache::lonhomework::results{"resource.$id.solved"} =
796: $solvemsg;
797: my $numawards=scalar(@Apache::inputtags::response);
798: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
799: foreach my $res (@Apache::inputtags::response) {
800: $Apache::lonhomework::results{"resource.$id.awarded"}+=
801: $Apache::lonhomework::results{"resource.$id.$res.awarded"};
802: }
803: if ($numawards > 0) {
804: $Apache::lonhomework::results{"resource.$id.awarded"}/=
805: $numawards;
806: }
807: } elsif ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
808: $Apache::lonhomework::results{"resource.$id.tries"} =
809: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
810: $Apache::lonhomework::results{"resource.$id.solved"} =
811: $solvemsg;
812: $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
813: } elsif ( $award eq 'INCORRECT' ) {
814: $Apache::lonhomework::results{"resource.$id.tries"} =
815: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
816: if (lc($Apache::lonhomework::problemstatus) eq 'no' ||
817: $Apache::lonhomework::scantronmode) {
818: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
819: }
820: $Apache::lonhomework::results{"resource.$id.solved"} =
821: 'incorrect_attempted';
822: } elsif ( $award eq 'SUBMITTED' ) {
823: $Apache::lonhomework::results{"resource.$id.tries"} =
824: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
825: $Apache::lonhomework::results{"resource.$id.solved"} =
826: 'ungraded_attempted';
827: } elsif ( $award eq 'DRAFT' ) {
828: $Apache::lonhomework::results{"resource.$id.solved"} = '';
829: } elsif ( $award eq 'NO_RESPONSE' ) {
830: #no real response so delete any data that got stored
831: &removealldata($id);
832: return '';
833: } else {
834: $Apache::lonhomework::results{"resource.$id.solved"} =
835: 'incorrect_attempted';
836: if (lc($Apache::lonhomework::problemstatus) eq 'no' ||
837: $Apache::lonhomework::scantronmode) {
838: $Apache::lonhomework::results{"resource.$id.tries"} =
839: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
840: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
841: }
842: }
843: if (defined($msg)) {
844: $Apache::lonhomework::results{"resource.$id.awardmsg"} = $msg;
845: }
846: # did either of the overall awards chage? If so ignore the
847: # previous check
848: if (($Apache::lonhomework::results{"resource.$id.awarded"} eq
849: $Apache::lonhomework::history{"resource.$id.awarded"}) &&
850: ($Apache::lonhomework::results{"resource.$id.solved"} eq
851: $Apache::lonhomework::history{"resource.$id.solved"})) {
852: # check if this was a previous submission if it was delete the
853: # unneeded data and update the previously_used attribute
854: if ( $previously_used eq 'PREVIOUSLY_USED') {
855: if (lc($Apache::lonhomework::problemstatus) ne 'no') {
856: delete($Apache::lonhomework::results{"resource.$id.tries"});
857: $Apache::lonhomework::results{"resource.$id.previous"} = '1';
858: }
859: } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
860: #delete all data as they student didn't do anything, but save
861: #the list of collaborators.
862: &removealldata($id);
863: #and since they didn't do anything we were never here
864: return '';
865: } else {
866: $Apache::lonhomework::results{"resource.$id.previous"} = '0';
867: }
868: }
869: } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} =~
870: /^correct/ ) {
871: #delete all data as they student already has it correct
872: &removealldata($id);
873: #and since they didn't do anything we were never here
874: return '';
875: }
876: $Apache::lonhomework::results{"resource.$id.award"} = $award;
877: if ($award eq 'SUBMITTED') {
878: &Apache::response::add_to_gradingqueue();
879: }
880: }
881:
882: sub grade {
883: my ($target) = @_;
884: my $id = $Apache::inputtags::part;
885: my $response='';
886: if ( defined $env{'form.submitted'}) {
887: my (@awards,@msgs);
888: foreach $response (@Apache::inputtags::response) {
889: &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
890: my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
891: &Apache::lonxml::debug("keeping $value from $response for $id");
892: push (@awards,$value);
893: $value=$Apache::lonhomework::results{"resource.$id.$response.awardmsg"};
894: &Apache::lonxml::debug("got message $value from $response for $id");
895: push (@msgs,$value);
896: }
897: my ($finalaward,$msg) = &finalizeawards(\@awards,\@msgs);
898: my $previously_used;
899: if ( $#Apache::inputtags::previous eq $#awards ) {
900: my $match=0;
901: my @matches;
902: foreach my $versionar (@Apache::inputtags::previous_version) {
903: foreach my $version (@$versionar) {
904: $matches[$version]++;
905: }
906: }
907: foreach my $elem (@matches) {if ($elem eq ($#awards+1)) {$match=1;}}
908: if ($match) {
909: $previously_used = 'PREVIOUSLY_LAST';
910: foreach my $value (@Apache::inputtags::previous) {
911: if ($value eq 'PREVIOUSLY_USED' ) {
912: $previously_used = $value;
913: last;
914: }
915: }
916: }
917: }
918: &Apache::lonxml::debug("final award $finalaward, $previously_used, message $msg");
919: &setgradedata($finalaward,$msg,$id,$previously_used);
920: }
921: return '';
922: }
923:
924: sub gradestatus {
925: my ($id,$target) = @_;
926: my $showbutton = 1;
927: my $bgcolor = '';
928: my $message = '';
929: my $latemessage = '';
930: my $trystr='';
931: my $button='';
932: my $previousmsg='';
933:
934: my $status = $Apache::inputtags::status['-1'];
935: &Apache::lonxml::debug("gradestatus has :$status:");
936: if ( $status ne 'CLOSED'
937: && $status ne 'UNAVAILABLE'
938: && $status ne 'INVALID_ACCESS'
939: && $status ne 'NEEDS_CHECKIN'
940: && $status ne 'NOT_IN_A_SLOT') {
941: my $award = $Apache::lonhomework::history{"resource.$id.award"};
942: my $awarded = $Apache::lonhomework::history{"resource.$id.awarded"};
943: my $solved = $Apache::lonhomework::history{"resource.$id.solved"};
944: my $previous = $Apache::lonhomework::history{"resource.$id.previous"};
945: my $awardmsg = $Apache::lonhomework::history{"resource.$id.awardmsg"};
946: &Apache::lonxml::debug("Found Award |$award|$solved|$awardmsg");
947: if ( $award ne '' || $solved ne '' || $status eq 'SHOW_ANSWER') {
948: &Apache::lonxml::debug('Getting message');
949: ($showbutton,$bgcolor,$message,$previousmsg) =
950: &decideoutput($award,$awarded,$awardmsg,$solved,$previous,
951: $target);
952: if ($target eq 'tex') {
953: $message='\vskip 2 mm '.$message.' ';
954: } else {
955: $message="<td bgcolor=\"$bgcolor\">$message</td>";
956: if ($previousmsg) {
957: $previousmsg="<td bgcolor=\"#aaaaff\">$previousmsg</td>";
958: }
959: }
960: }
961: my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
962: my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
963: &Apache::lonxml::debug("got maxtries of :$maxtries:");
964: #if tries are set to negative turn off the Tries/Button and messages
965: if (defined($maxtries) && $maxtries < 0) { return ''; }
966: if ( $tries eq '' ) { $tries = '0'; }
967: if ( $maxtries eq '' ) { $maxtries = '2'; }
968: if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; }
969: my $tries_text=&mt('Tries');
970: if ( $Apache::lonhomework::type eq 'survey' ||
971: $Apache::lonhomework::parsing_a_task) {
972: $tries_text=&mt('Submissions');
973: }
974: if ( $showbutton ) {
975: if ($target eq 'tex') {
976: if ($env{'request.state'} ne "construct" && $Apache::lonhomework::type ne 'exam' && $env{'form.suppress_tries'} ne 'yes') {
977: $trystr = ' {\vskip 1 mm \small \textit{'.$tries_text.'} '.$tries.'/'.$maxtries.'} \vskip 2 mm ';
978: } else {
979: $trystr = '\vskip 0 mm ';
980: }
981: } else {
982: $trystr = "<td><nobr>".$tries_text." $tries";
983: if ($Apache::lonhomework::parsing_a_task) {
984: } elsif($env{'request.state'} ne 'construct') {
985: $trystr.="/$maxtries";
986: } else {
987: if (defined($Apache::inputtags::params{'maxtries'})) {
988: $trystr.="/".$Apache::inputtags::params{'maxtries'};
989: }
990: }
991: $trystr.="</nobr></td>";
992: }
993: }
994: if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {$showbutton = 0;}
995: if ( $showbutton ) {
996: if ($target ne 'tex') {
997: $button = '<input type="submit" name="submit_'.$id.'" value="'.&mt('Submit Answer').'" />';
998: }
999: }
1000: if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
1001: #last submissions was after due date
1002: $latemessage=&mt(' The last submission was after the Due Date ');;
1003: if ($target eq 'web') {
1004: $latemessage='<td bgcolor="#ffaaaa">'.$latemessage.'</td>';
1005: }
1006: }
1007: }
1008: my $output= $previousmsg.$latemessage.$message.$trystr;
1009: if ($output =~ /^\s*$/) {
1010: return $button;
1011: } else {
1012: if ($target eq 'tex') {
1013: return $button.' \vskip 0 mm '.$output.' ';
1014: } else {
1015: return '<table><tr><td>'.$button.'</td>'.$output.'</tr></table>';
1016: }
1017: }
1018: }
1019: 1;
1020: __END__
1021:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>