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