1: # The LearningOnline Network with CAPA
2: # input definitons
3: #
4: # $Id: inputtags.pm,v 1.199 2006/06/30 04:56:56 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: 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: || ($Apache::inputtags::status[-1] eq 'CANNOT_ANSWER' &&
288: $Apache::lonhomework::history{"resource.$partid.solved"} !~ /^correct/ )) {
289: my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
290: my $maxlength;
291: if ($size eq '') { $size=20; } else {
292: if ($size < 20) { $maxlength=$size; }
293: }
294: my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$id.submission"},'<>&"');
295: if ($Apache::lonhomework::type ne 'exam') {
296: my $addchars=&Apache::lonxml::get_param('addchars',$parstack,$safeeval);
297: $result='';
298: if ($addchars) {
299: $result.=&addchars('HWVAL_'.$id,$addchars);
300: }
301: my $readonly=&Apache::lonxml::get_param('readonly',$parstack,
302: $safeeval);
303: if (lc($readonly) eq 'yes'
304: || $Apache::inputtags::status[-1] eq 'CANNOT_ANSWER') {
305: $readonly=' readonly="readonly" ';
306: } else {
307: $readonly='';
308: }
309: my $name = 'HWVAL_'.$id;
310: if ($Apache::inputtags::status[-1] eq 'CANNOT_ANSWER') {
311: $name = "none";
312: }
313: $result.= '<input type="text" '.$readonly.' name="'.$name.'" value="'.
314: $oldresponse.'" size="'.$size.'" maxlength="'.$maxlength.'" />';
315: }
316: if ($Apache::lonhomework::type eq 'exam'
317: && &needs_exam_box($tagstack)) {
318: $result.=&exam_box($target);
319: }
320: } else {
321: #right or wrong don't show what was last typed in.
322: $result='<b>'.$Apache::inputtags::answertxt{$id}.'</b>';
323: #$result='';
324: }
325: } elsif ($target eq 'edit') {
326: $result=&Apache::edit::tag_start($target,$token);
327: $result.=&Apache::edit::text_arg('Size:','size',$token,'5').
328: &Apache::edit::text_arg('Click-On Texts (comma sep):',
329: 'addchars',$token,10);
330: $result.=&Apache::edit::select_arg('Readonly:','readonly',
331: ['no','yes'],$token);
332: $result.=&Apache::edit::end_row();
333: $result.=&Apache::edit::end_table();
334: } elsif ($target eq 'modified') {
335: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
336: $safeeval,'size',
337: 'addchars','readonly');
338: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
339: } elsif ($target eq 'tex'
340: && $Apache::lonhomework::type ne 'exam') {
341: my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
342: if ($size != 0) {$size=$size*2; $size.=' mm';} else {$size='40 mm';}
343: $result='\framebox['.$size.'][s]{\tiny\strut}';
344:
345: } elsif ($target eq 'tex'
346: && $Apache::lonhomework::type eq 'exam'
347: && &needs_exam_box($tagstack)) {
348: $result.=&exam_box($target);
349: }
350: return $result;
351: }
352:
353: sub end_textline {
354: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
355: if ($target eq 'web') { $Apache::lonxml::evaluate++; }
356: elsif ($target eq 'edit') { return ('','no'); }
357: return "";
358: }
359:
360: sub start_hiddenline {
361: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
362: my $result = "";
363: if ($target eq 'web') {
364: $Apache::lonxml::evaluate--;
365: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
366: my $partid=$Apache::inputtags::part;
367: my $id=$Apache::inputtags::response[-1];
368: my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$id.submission"},'<>&"');
369: if ($Apache::lonhomework::type ne 'exam') {
370: $result= '<input type="hidden" name="HWVAL_'.$id.'" value="'.
371: $oldresponse.'" />';
372: }
373: }
374: } elsif ($target eq 'edit') {
375: $result=&Apache::edit::tag_start($target,$token);
376: $result.=&Apache::edit::end_table;
377: }
378:
379: if ( ($target eq 'web' || $target eq 'tex')
380: && $Apache::lonhomework::type eq 'exam'
381: && &needs_exam_box($tagstack)) {
382: $result.=&exam_box($target);
383: }
384: return $result;
385: }
386:
387: sub end_hiddenline {
388: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
389: if ($target eq 'web') { $Apache::lonxml::evaluate++; }
390: elsif ($target eq 'edit') { return ('','no'); }
391: return "";
392: }
393:
394: # $part -> partid
395: # $id -> responseid
396: # $uploadefiletypes -> comma seperated list of extensions allowed or * for any
397: # $which -> 'uploadedonly' -> only newly uploaded files
398: # 'portfolioonly' -> only allow files from portfolio
399: # 'both' -> allow files from either location
400: # $extratext -> additional text to go between the link and the input box
401: # returns a table row <tr>
402: sub file_selector {
403: my ($part,$id,$uploadedfiletypes,$which,$extratext)=@_;
404: if (!$uploadedfiletypes) { return ''; }
405:
406: my $jspart=$part;
407: $jspart=~s/\./_/g;
408:
409: my $result;
410:
411: $result.='<tr><td>';
412: if ($uploadedfiletypes ne '*') {
413: $result.=
414: &mt('Allowed filetypes: <b>[_1]</b>',$uploadedfiletypes).'<br />';
415: }
416: if ($which eq 'uploadonly' || $which eq 'both') {
417: $result.=&mt('Submit a file: (only one file can be uploaded)').
418: ' <br /><input type="file" size="50" name="HWFILE'.
419: $jspart.'_'.$id.'" /><br />';
420: my $uploadedfile= &HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.uploadedfile"},'<>&"');
421:
422: if ($uploadedfile) {
423: my $url=$Apache::lonhomework::history{"resource.$part.$id.uploadedurl"};
424: &Apache::lonxml::extlink($url);
425: &Apache::lonnet::allowuploaded('/adm/essayresponse',$url);
426: my $icon=&Apache::loncommon::icon($url);
427: my $curfile='<a href="'.$url.'"><img src="'.$icon.
428: '" border="0" />'.$uploadedfile.'</a>';
429: $result.=&mt('Currently submitted: <tt>[_1]</tt>',$curfile);
430: } else {
431: #$result.=&mt('(Hand in a file you have prepared on your computer)');
432: }
433: }
434: if ( $which eq 'both') {
435: $result.='<br />'.'<strong>'.&mt('OR:').'</strong><br />';
436: }
437: if ($which eq 'portfolioonly' || $which eq 'both') {
438: $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"))'."'".'>'.
439: &mt('Select Portfolio Files').'</a><br />'.
440: '<input type="text" size="50" name="HWPORT'.$jspart.'_'.$id.'" value="" />'.
441: '<br />';
442: if ($Apache::lonhomework::history{"resource.$part.$id.portfiles"}=~/[^\s]/){
443: my (@file_list,@bad_file_list);
444: foreach my $file (split(/\s*,\s*/,&unescape($Apache::lonhomework::history{"resource.$part.$id.portfiles"}))) {
445: my (undef,undef,$domain,$user)=&Apache::lonxml::whichuser();
446: my $url="/uploaded/$domain/$user/portfolio$file";
447: my $icon=&Apache::loncommon::icon($url);
448: push(@file_list,'<a href="'.$url.'"><img src="'.$icon.
449: '" border="0" />'.$file.'</a>');
450: if (! &Apache::lonnet::stat_file($url)) {
451: &Apache::lonnet::logthis("bad file is $url");
452: push(@bad_file_list,'<a href="'.$url.'"><img src="'.$icon.
453: '" border="0" />'.$file.'</a>');
454: }
455: }
456: my $files = '<span class="LC_filename">'.
457: join('</span>, <span class="LC_filename">',@file_list).
458: '</span>';
459: $result.=&mt("Portfolio files previously selected: [_1]",$files);
460: if (@bad_file_list) {
461: my $bad_files = '<span class="LC_filename">'.
462: join('</span>, <span class="LC_filename">',@bad_file_list).
463: '</span>';
464: $result.='<br />'.&mt('<span class="LC_error">These file(s) don\'t exist:</span> [_1]',$bad_files);
465: }
466: }
467: }
468: $result.='</td></tr>';
469: return $result;
470: }
471:
472: sub checkstatus {
473: my ($value,$awardref,$msgref)=@_;
474: for (my $i=0;$i<=$#$awardref;$i++) {
475: if ($$awardref[$i] eq $value) {
476: return ($$awardref[$i],$$msgref[$i]);
477: }
478: }
479: return(undef,undef);
480: }
481:
482: sub valid_award {
483: my ($award) =@_;
484: foreach my $possibleaward ('EXTRA_ANSWER','MISSING_ANSWER', 'ERROR',
485: 'NO_RESPONSE',
486: 'TOO_LONG', 'UNIT_INVALID_INSTRUCTOR',
487: 'UNIT_INVALID_STUDENT', 'UNIT_IRRECONCIBLE',
488: 'UNIT_FAIL', 'NO_UNIT',
489: 'UNIT_NOTNEEDED', 'WANTED_NUMERIC',
490: 'BAD_FORMULA', 'SIG_FAIL', 'INCORRECT',
491: 'MISORDERED_RANK', 'INVALID_FILETYPE',
492: 'DRAFT', 'SUBMITTED', 'ASSIGNED_SCORE',
493: 'APPROX_ANS', 'EXACT_ANS','COMMA_FAIL') {
494: if ($award eq $possibleaward) { return 1; }
495: }
496: return 0;
497: }
498:
499: sub finalizeawards {
500: my ($awardref,$msgref,$nameref,$reverse)=@_;
501: my $result=undef;
502: my $award;
503: my $msg;
504: if ($#$awardref == -1) { $result = "NO_RESPONSE"; }
505: if ($result eq '' ) {
506: my $blankcount;
507: foreach $award (@$awardref) {
508: if ($award eq '') {
509: $result='MISSING_ANSWER';
510: $blankcount++;
511: }
512: }
513: if ($blankcount == ($#$awardref + 1)) { $result = 'NO_RESPONSE'; }
514: }
515: if (defined($result)) { return ($result,$msg); }
516:
517: # these awards are ordered from most important error through best correct
518:
519: my @awards = ('EXTRA_ANSWER', 'MISSING_ANSWER', 'ERROR', 'NO_RESPONSE',
520: 'TOO_LONG',
521: 'UNIT_INVALID_INSTRUCTOR', 'UNIT_INVALID_STUDENT',
522: 'UNIT_IRRECONCIBLE', 'UNIT_FAIL', 'NO_UNIT',
523: 'UNIT_NOTNEEDED', 'WANTED_NUMERIC', 'BAD_FORMULA',
524: 'COMMA_FAIL', 'SIG_FAIL', 'INCORRECT', 'MISORDERED_RANK',
525: 'INVALID_FILETYPE', 'DRAFT', 'SUBMITTED', 'ASSIGNED_SCORE',
526: 'APPROX_ANS', 'EXACT_ANS');
527: if ($reverse) { @awards=reverse(@awards); }
528: foreach my $possibleaward (@awards) {
529: ($result,$msg)=&checkstatus($possibleaward,$awardref,$msgref);
530: if (defined($result)) { return ($result,$msg); }
531: }
532: return ('ERROR',undef);
533: }
534:
535: sub decideoutput {
536: my ($award,$awarded,$awardmsg,$solved,$previous,$target)=@_;
537: my $message='';
538: my $button=0;
539: my $previousmsg;
540: my $bgcolor='orange';
541: my $added_computer_text=0;
542: my %possiblecolors =
543: ( 'correct' => '#aaffaa',
544: 'charged_try' => '#ffaaaa',
545: 'not_charged_try' => '#ffffaa',
546: 'no_message' => '#fffff',
547: );
548:
549: my $part = $Apache::inputtags::part;
550: my $handgrade =
551: ('yes' eq lc(&Apache::lonnet::EXT("resource.$part.handgrade")));
552:
553: my $computer = ($handgrade)? ''
554: : " ".&mt("Computer's answer now shown above.");
555: &Apache::lonxml::debug("handgrade has :$handgrade:");
556:
557: if ($previous) { $previousmsg=&mt('You have entered that answer before'); }
558:
559: if ($solved =~ /^correct/) {
560: $bgcolor=$possiblecolors{'correct'};
561: $message=&mt('You are correct.');
562: if ($awarded < 1 && $awarded > 0) {
563: $message=&mt('You are partially correct.');
564: $bgcolor=$possiblecolors{'not_charged_try'};
565: } elsif ($awarded < 1) {
566: $message=&mt('Incorrect.');
567: $bgcolor=$possiblecolors{'charged_try'};
568: }
569: if ($env{'request.filename'} =~
570: m|/res/lib/templates/examupload.problem$|) {
571: $message = &mt("A score has been assigned.");
572: $added_computer_text=1;
573: } else {
574: if ($target eq 'tex') {
575: $message = '\textbf{'.$message.'}';
576: } else {
577: $message = "<b>".$message."</b>";
578: $message.= $computer;
579: }
580: $added_computer_text=1;
581: unless ($env{'course.'.
582: $env{'request.course.id'}.
583: '.disable_receipt_display'} eq 'yes') {
584: $message.=(($target eq 'web')?'<br />':' ').
585: &mt('Your receipt is').' '.&Apache::lonnet::receipt($Apache::inputtags::part).
586: (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'');
587: }
588: }
589: $button=0;
590: $previousmsg='';
591: } elsif ($solved =~ /^excused/) {
592: if ($target eq 'tex') {
593: $message = ' \textbf{'.&mt('You are excused from the problem.').'} ';
594: } else {
595: $message = "<b>".&mt('You are excused from the problem.')."</b>";
596: }
597: $bgcolor=$possiblecolors{'charged_try'};
598: $button=0;
599: $previousmsg='';
600: } elsif ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
601: if ($solved =~ /^incorrect/ || $solved eq '') {
602: $message = &mt("Incorrect").".";
603: $bgcolor=$possiblecolors{'charged_try'};
604: $button=1;
605: } else {
606: if ($target eq 'tex') {
607: $message = '\textbf{'.&mt('You are correct.').'}';
608: } else {
609: $message = "<b>".&mt('You are correct.')."</b>";
610: $message.= $computer;
611: }
612: $added_computer_text=1;
613: unless ($env{'course.'.
614: $env{'request.course.id'}.
615: '.disable_receipt_display'} eq 'yes') {
616: $message.=(($target eq 'web')?'<br />':' ').
617: 'Your receipt is '.&Apache::lonnet::receipt($Apache::inputtags::part).
618: (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'');
619: }
620: $bgcolor=$possiblecolors{'correct'};
621: $button=0;
622: $previousmsg='';
623: }
624: } elsif ($award eq 'NO_RESPONSE') {
625: $message = '';
626: $bgcolor=$possiblecolors{'no_feedback'};
627: $button=1;
628: } elsif ($award eq 'EXTRA_ANSWER') {
629: $message = &mt('Some extra items were submitted.');
630: $bgcolor=$possiblecolors{'not_charged_try'};
631: $button = 1;
632: } elsif ($award eq 'MISSING_ANSWER') {
633: $message = &mt('Some items were not submitted.');
634: $bgcolor=$possiblecolors{'not_charged_try'};
635: $button = 1;
636: } elsif ($award eq 'ERROR') {
637: $message = &mt('An error occured while grading your answer.');
638: $bgcolor=$possiblecolors{'not_charged_try'};
639: $button = 1;
640: } elsif ($award eq 'TOO_LONG') {
641: $message = &mt("The submitted answer was too long.");
642: $bgcolor=$possiblecolors{'not_charged_try'};
643: $button=1;
644: } elsif ($award eq 'WANTED_NUMERIC') {
645: $message = &mt("This question expects a numeric answer.");
646: $bgcolor=$possiblecolors{'not_charged_try'};
647: $button=1;
648: } elsif ($award eq 'MISORDERED_RANK') {
649: $message = &mt('You have provided an invalid ranking');
650: if ($target ne 'tex') {
651: $message.=', '.&mt('please refer to').' '.&Apache::loncommon::help_open_topic('Ranking_Problems','help on ranking problems');
652: }
653: $bgcolor=$possiblecolors{'not_charged_try'};
654: $button=1;
655: } elsif ($award eq 'INVALID_FILETYPE') {
656: $message = &mt('Submission won\'t be graded. The type of file submitted is not allowed.');
657: $bgcolor=$possiblecolors{'not_charged_try'};
658: $button=1;
659: } elsif ($award eq 'SIG_FAIL') {
660: my ($used,$min,$max)=split(':',$awardmsg);
661: my $word;
662: if ($used < $min) { $word=&mt('more'); }
663: if ($used > $max) { $word=&mt('fewer'); }
664: $message = &mt("Submission not graded. Use [_2] digits.",$used,$word);
665: $bgcolor=$possiblecolors{'not_charged_try'};
666: $button=1;
667: } elsif ($award eq 'UNIT_INVALID_INSTRUCTOR') {
668: $message = &mt('Error in instructor specifed unit. This error has been reported to the instructor.', $awardmsg);
669: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');}
670: $bgcolor=$possiblecolors{'not_charged_try'};
671: $button=1;
672: } elsif ($award eq 'UNIT_INVALID_STUDENT') {
673: $message = &mt('Unable to interpret units. Computer reads units as "[_1]".',&markup_unit($awardmsg,$target));
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 'UNIT_FAIL' || $award eq 'UNIT_IRRECONCIBLE') {
678: $message = &mt('Incompatible units. No conversion found between "[_1]" and the required units.',&markup_unit($awardmsg,$target));
679: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');}
680: $bgcolor=$possiblecolors{'not_charged_try'};
681: $button=1;
682: } elsif ($award eq 'UNIT_NOTNEEDED') {
683: $message = &mt('Only a number required. Computer reads units of "[_1]".',&markup_unit($awardmsg,$target));
684: $bgcolor=$possiblecolors{'not_charged_try'};
685: $button=1;
686: } elsif ($award eq 'NO_UNIT') {
687: $message = &mt("Units required").'.';
688: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units')};
689: $bgcolor=$possiblecolors{'not_charged_try'};
690: $button=1;
691: } elsif ($award eq 'COMMA_FAIL') {
692: $message = &mt("Proper comma separation is required").'.';
693: $bgcolor=$possiblecolors{'not_charged_try'};
694: $button=1;
695: } elsif ($award eq 'BAD_FORMULA') {
696: $message = &mt("Unable to understand formula");
697: $bgcolor=$possiblecolors{'not_charged_try'};
698: $button=1;
699: } elsif ($award eq 'INCORRECT') {
700: $message = &mt("Incorrect").'.';
701: $bgcolor=$possiblecolors{'charged_try'};
702: $button=1;
703: } elsif ($award eq 'SUBMITTED') {
704: $message = &mt("Your submission has been recorded.");
705: $bgcolor=$possiblecolors{'correct'};
706: $button=1;
707: } elsif ($award eq 'DRAFT') {
708: $message = &mt("A draft copy has been saved.");
709: $bgcolor=$possiblecolors{'not_charged_try'};
710: $button=1;
711: } elsif ($award eq 'ASSIGNED_SCORE') {
712: $message = &mt("A score has been assigned.");
713: $bgcolor=$possiblecolors{'correct'};
714: $button=0;
715: } elsif ($award eq '') {
716: if ($handgrade && $Apache::inputtags::status[-1] eq 'SHOW_ANSWER') {
717: $message = &mt("Nothing submitted.");
718: $bgcolor=$possiblecolors{'charged_try'};
719: } else {
720: $bgcolor=$possiblecolors{'not_charged_try'};
721: }
722: $button=1;
723: } else {
724: $message = &mt("Unknown message").": $award";
725: $button=1;
726: }
727: my (undef,undef,$domain,$user)=&Apache::lonxml::whichuser();
728: foreach my $resid(@Apache::inputtags::response){
729: if ($Apache::lonhomework::history{"resource.$part.$resid.handback"}) {
730: $message.='<br />';
731: my @files = split(/\s*,\s*/,
732: $Apache::lonhomework::history{"resource.$part.$resid.handback"});
733: my $file_msg;
734: foreach my $file (@files) {
735: $file_msg.= '<br /><a href="/uploaded/'."$domain/$user".'/'.$file.'">'.$file.'</a>';
736: }
737: $message .= &mt('Returned file(s): [_1]',$file_msg);
738: }
739: }
740:
741: if (lc($Apache::lonhomework::problemstatus) eq 'no' &&
742: $Apache::inputtags::status[-1] ne 'SHOW_ANSWER') {
743: $message = &mt("Answer Submitted: Your final submission will be graded after the due date.");
744: $bgcolor=$possiblecolors{'correct'};
745: $button=1;
746: }
747: if ($Apache::inputtags::status[-1] eq 'SHOW_ANSWER' &&
748: !$added_computer_text && $target ne 'tex') {
749: $message.= $computer;
750: $added_computer_text=1;
751: }
752: return ($button,$bgcolor,$message,$previousmsg);
753: }
754:
755: sub markup_unit {
756: my ($unit,$target)=@_;
757: if ($target eq 'tex') {
758: return '\texttt{'.&Apache::lonxml::latex_special_symbols($unit).'}';
759: } else {
760: return "<tt>".$unit."</tt>";
761: }
762: }
763:
764: sub removealldata {
765: my ($id)=@_;
766: foreach my $key (keys(%Apache::lonhomework::results)) {
767: if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
768: &Apache::lonxml::debug("Removing $key");
769: delete($Apache::lonhomework::results{$key});
770: }
771: }
772: }
773:
774: sub hidealldata {
775: my ($id)=@_;
776: foreach my $key (keys(%Apache::lonhomework::results)) {
777: if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
778: &Apache::lonxml::debug("Hidding $key");
779: my $newkey=$key;
780: $newkey=~s/^(resource\.\Q$id\E\.[^\.]+\.)(.*)$/${1}hidden${2}/;
781: $Apache::lonhomework::results{$newkey}=
782: $Apache::lonhomework::results{$key};
783: delete($Apache::lonhomework::results{$key});
784: }
785: }
786: }
787:
788: sub setgradedata {
789: my ($award,$msg,$id,$previously_used) = @_;
790: if ($Apache::lonhomework::scantronmode &&
791: &Apache::lonnet::validCODE($env{'form.CODE'})) {
792: $Apache::lonhomework::results{"resource.CODE"}=$env{'form.CODE'};
793: } elsif ($Apache::lonhomework::scantronmode &&
794: $env{'form.CODE'} eq '' &&
795: $Apache::lonhomework::history{"resource.CODE"} ne '') {
796: $Apache::lonhomework::results{"resource.CODE"}='';
797: }
798:
799: if (!$Apache::lonhomework::scantronmode &&
800: $Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
801: $Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER') {
802: $Apache::lonhomework::results{"resource.$id.afterduedate"}=$award;
803: return '';
804: } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} !~
805: /^correct/ || $Apache::lonhomework::scantronmode ||
806: lc($Apache::lonhomework::problemstatus) eq 'no') {
807: # the student doesn't already have it correct,
808: # or we are in a mode (scantron orno problem status) where a correct
809: # can become incorrect
810: # handle assignment of tries and solved status
811: my $solvemsg;
812: if ($Apache::lonhomework::scantronmode) {
813: $solvemsg='correct_by_scantron';
814: } else {
815: $solvemsg='correct_by_student';
816: }
817: if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
818: $Apache::lonhomework::results{"resource.$id.afterduedate"}='';
819: }
820: if ( $award eq 'ASSIGNED_SCORE') {
821: $Apache::lonhomework::results{"resource.$id.tries"} =
822: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
823: $Apache::lonhomework::results{"resource.$id.solved"} =
824: $solvemsg;
825: my $numawards=scalar(@Apache::inputtags::response);
826: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
827: foreach my $res (@Apache::inputtags::response) {
828: $Apache::lonhomework::results{"resource.$id.awarded"}+=
829: $Apache::lonhomework::results{"resource.$id.$res.awarded"};
830: }
831: if ($numawards > 0) {
832: $Apache::lonhomework::results{"resource.$id.awarded"}/=
833: $numawards;
834: }
835: } elsif ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
836: $Apache::lonhomework::results{"resource.$id.tries"} =
837: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
838: $Apache::lonhomework::results{"resource.$id.solved"} =
839: $solvemsg;
840: $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
841: } elsif ( $award eq 'INCORRECT' ) {
842: $Apache::lonhomework::results{"resource.$id.tries"} =
843: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
844: if (lc($Apache::lonhomework::problemstatus) eq 'no' ||
845: $Apache::lonhomework::scantronmode) {
846: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
847: }
848: $Apache::lonhomework::results{"resource.$id.solved"} =
849: 'incorrect_attempted';
850: } elsif ( $award eq 'SUBMITTED' ) {
851: $Apache::lonhomework::results{"resource.$id.tries"} =
852: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
853: $Apache::lonhomework::results{"resource.$id.solved"} =
854: 'ungraded_attempted';
855: } elsif ( $award eq 'DRAFT' ) {
856: $Apache::lonhomework::results{"resource.$id.solved"} = '';
857: } elsif ( $award eq 'NO_RESPONSE' ) {
858: #no real response so delete any data that got stored
859: &removealldata($id);
860: return '';
861: } else {
862: $Apache::lonhomework::results{"resource.$id.solved"} =
863: 'incorrect_attempted';
864: if (lc($Apache::lonhomework::problemstatus) eq 'no' ||
865: $Apache::lonhomework::scantronmode) {
866: $Apache::lonhomework::results{"resource.$id.tries"} =
867: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
868: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
869: }
870: }
871: if (defined($msg)) {
872: $Apache::lonhomework::results{"resource.$id.awardmsg"} = $msg;
873: }
874: # did either of the overall awards chage? If so ignore the
875: # previous check
876: if (($Apache::lonhomework::results{"resource.$id.awarded"} eq
877: $Apache::lonhomework::history{"resource.$id.awarded"}) &&
878: ($Apache::lonhomework::results{"resource.$id.solved"} eq
879: $Apache::lonhomework::history{"resource.$id.solved"})) {
880: # check if this was a previous submission if it was delete the
881: # unneeded data and update the previously_used attribute
882: if ( $previously_used eq 'PREVIOUSLY_USED') {
883: if (lc($Apache::lonhomework::problemstatus) ne 'no') {
884: delete($Apache::lonhomework::results{"resource.$id.tries"});
885: $Apache::lonhomework::results{"resource.$id.previous"} = '1';
886: }
887: } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
888: #delete all data as they student didn't do anything, but save
889: #the list of collaborators.
890: &removealldata($id);
891: #and since they didn't do anything we were never here
892: return '';
893: } else {
894: $Apache::lonhomework::results{"resource.$id.previous"} = '0';
895: }
896: }
897: } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} =~
898: /^correct/ ) {
899: #delete all data as they student already has it correct
900: &removealldata($id);
901: #and since they didn't do anything we were never here
902: return '';
903: }
904: $Apache::lonhomework::results{"resource.$id.award"} = $award;
905: if ($award eq 'SUBMITTED') {
906: &Apache::response::add_to_gradingqueue();
907: }
908: }
909:
910: sub grade {
911: my ($target) = @_;
912: my $id = $Apache::inputtags::part;
913: my $response='';
914: if ( defined $env{'form.submitted'}) {
915: my (@awards,@msgs);
916: foreach $response (@Apache::inputtags::response) {
917: &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
918: my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
919: &Apache::lonxml::debug("keeping $value from $response for $id");
920: push (@awards,$value);
921: $value=$Apache::lonhomework::results{"resource.$id.$response.awardmsg"};
922: &Apache::lonxml::debug("got message $value from $response for $id");
923: push (@msgs,$value);
924: }
925: my ($finalaward,$msg) = &finalizeawards(\@awards,\@msgs);
926: my $previously_used;
927: if ( $#Apache::inputtags::previous eq $#awards ) {
928: my $match=0;
929: my @matches;
930: foreach my $versionar (@Apache::inputtags::previous_version) {
931: foreach my $version (@$versionar) {
932: $matches[$version]++;
933: }
934: }
935: foreach my $elem (@matches) {if ($elem eq ($#awards+1)) {$match=1;}}
936: if ($match) {
937: $previously_used = 'PREVIOUSLY_LAST';
938: foreach my $value (@Apache::inputtags::previous) {
939: if ($value eq 'PREVIOUSLY_USED' ) {
940: $previously_used = $value;
941: last;
942: }
943: }
944: }
945: }
946: &Apache::lonxml::debug("final award $finalaward, $previously_used, message $msg");
947: &setgradedata($finalaward,$msg,$id,$previously_used);
948: }
949: return '';
950: }
951:
952: sub gradestatus {
953: my ($id,$target) = @_;
954: my $showbutton = 1;
955: my $bgcolor = '';
956: my $message = '';
957: my $latemessage = '';
958: my $trystr='';
959: my $button='';
960: my $previousmsg='';
961:
962: my $status = $Apache::inputtags::status['-1'];
963: &Apache::lonxml::debug("gradestatus has :$status:");
964: if ( $status ne 'CLOSED'
965: && $status ne 'UNAVAILABLE'
966: && $status ne 'INVALID_ACCESS'
967: && $status ne 'NEEDS_CHECKIN'
968: && $status ne 'NOT_IN_A_SLOT') {
969: my $award = $Apache::lonhomework::history{"resource.$id.award"};
970: my $awarded = $Apache::lonhomework::history{"resource.$id.awarded"};
971: my $solved = $Apache::lonhomework::history{"resource.$id.solved"};
972: my $previous = $Apache::lonhomework::history{"resource.$id.previous"};
973: my $awardmsg = $Apache::lonhomework::history{"resource.$id.awardmsg"};
974: &Apache::lonxml::debug("Found Award |$award|$solved|$awardmsg");
975: if ( $award ne '' || $solved ne '' || $status eq 'SHOW_ANSWER') {
976: &Apache::lonxml::debug('Getting message');
977: ($showbutton,$bgcolor,$message,$previousmsg) =
978: &decideoutput($award,$awarded,$awardmsg,$solved,$previous,
979: $target);
980: if ($target eq 'tex') {
981: $message='\vskip 2 mm '.$message.' ';
982: } else {
983: $message="<td bgcolor=\"$bgcolor\">$message</td>";
984: if ($previousmsg) {
985: $previousmsg="<td bgcolor=\"#aaaaff\">$previousmsg</td>";
986: }
987: }
988: }
989: my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
990: my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
991: &Apache::lonxml::debug("got maxtries of :$maxtries:");
992: #if tries are set to negative turn off the Tries/Button and messages
993: if (defined($maxtries) && $maxtries < 0) { return ''; }
994: if ( $tries eq '' ) { $tries = '0'; }
995: if ( $maxtries eq '' ) { $maxtries = '2'; }
996: if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; }
997: my $tries_text=&mt('Tries');
998: if ( $Apache::lonhomework::type eq 'survey' ||
999: $Apache::lonhomework::parsing_a_task) {
1000: $tries_text=&mt('Submissions');
1001: }
1002: if ( $showbutton ) {
1003: if ($target eq 'tex') {
1004: if ($env{'request.state'} ne "construct" && $Apache::lonhomework::type ne 'exam' && $env{'form.suppress_tries'} ne 'yes') {
1005: $trystr = ' {\vskip 1 mm \small \textit{'.$tries_text.'} '.$tries.'/'.$maxtries.'} \vskip 2 mm ';
1006: } else {
1007: $trystr = '\vskip 0 mm ';
1008: }
1009: } else {
1010: $trystr = "<td><nobr>".$tries_text." $tries";
1011: if ($Apache::lonhomework::parsing_a_task) {
1012: } elsif($env{'request.state'} ne 'construct') {
1013: $trystr.="/$maxtries";
1014: } else {
1015: if (defined($Apache::inputtags::params{'maxtries'})) {
1016: $trystr.="/".$Apache::inputtags::params{'maxtries'};
1017: }
1018: }
1019: $trystr.="</nobr></td>";
1020: }
1021: }
1022: if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {$showbutton = 0;}
1023: if ( $showbutton ) {
1024: if ($target ne 'tex') {
1025: $button = '<input type="submit" name="submit_'.$id.'" value="'.&mt('Submit Answer').'" />';
1026: }
1027: }
1028: if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
1029: #last submissions was after due date
1030: $latemessage=&mt(' The last submission was after the Due Date ');;
1031: if ($target eq 'web') {
1032: $latemessage='<td bgcolor="#ffaaaa">'.$latemessage.'</td>';
1033: }
1034: }
1035: }
1036: my $output= $previousmsg.$latemessage.$message.$trystr;
1037: if ($output =~ /^\s*$/) {
1038: return $button;
1039: } else {
1040: if ($target eq 'tex') {
1041: return $button.' \vskip 0 mm '.$output.' ';
1042: } else {
1043: return '<table><tr><td>'.$button.'</td>'.$output.'</tr></table>';
1044: }
1045: }
1046: }
1047: 1;
1048: __END__
1049:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>