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