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