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