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