Annotation of loncom/homework/inputtags.pm, revision 1.164
1.43 albertel 1: # The LearningOnline Network with CAPA
2: # input definitons
1.47 albertel 3: #
1.164 ! albertel 4: # $Id: inputtags.pm,v 1.163 2005/03/31 21:34:34 albertel Exp $
1.47 albertel 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/
1.1 albertel 27:
28: package Apache::inputtags;
1.55 albertel 29: use HTML::Entities();
1.1 albertel 30: use strict;
1.82 www 31: use Apache::loncommon;
1.115 www 32: use Apache::lonlocal;
1.1 albertel 33:
1.50 harris41 34: BEGIN {
1.135 albertel 35: &Apache::lonxml::register('Apache::inputtags',('hiddenline','textfield','textline'));
1.1 albertel 36: }
37:
1.43 albertel 38:
1.1 albertel 39: sub initialize_inputtags {
1.135 albertel 40: # list of current input ids
41: @Apache::inputtags::input=();
42: # list of all input ids seen in this problem
43: @Apache::inputtags::inputlist=();
44: # list of all current response ids
45: @Apache::inputtags::response=();
46: # list of all response ids seen in this problem
47: @Apache::inputtags::responselist=();
48: # list of whether or not a specific response was previously used
49: @Apache::inputtags::previous=();
50: # submission it was used in
51: @Apache::inputtags::previous_version=();
52: # id of current part, 0 means that no part is current
53: # (inside <problem> only
54: $Apache::inputtags::part='';
55: # list of all part ids seen
56: @Apache::inputtags::partlist=();
57: # list of problem date statuses, the first element is for <problem>
58: # if there is a second element it is for the current <part>
59: @Apache::inputtags::status=();
60: # hash of defined params for the current response
61: %Apache::inputtags::params=();
62: # list of all ids, for <import>, these get join()ed and prepended
63: @Apache::inputtags::import=();
64: # list of all import ids seen
65: @Apache::inputtags::importlist=();
66: # just used to note whether we have seen a response that isn't in a part
67: $Apache::inputtags::response_with_no_part=0;
1.144 albertel 68: # storage location so the begin <*response> tag can generate the correct
69: # answer string for display by the <textline />
70: %Apache::inputtags::answertxt=();
1.103 albertel 71: }
72:
73: sub check_for_duplicate_ids {
74: my %check;
75: foreach my $id (@Apache::inputtags::partlist,
76: @Apache::inputtags::responselist,
77: @Apache::inputtags::importlist) {
78: $check{$id}++;
79: }
80: my @duplicates;
81: foreach my $id (sort(keys(%check))) {
82: if ($check{$id} > 1) {
83: push(@duplicates,$id);
84: }
85: }
86: if (@duplicates) {
87: &Apache::lonxml::error("Duplicated ids found, problem will operate incorrectly. Duplicated ids seen: ",join(', ',@duplicates));
88: }
1.1 albertel 89: }
90:
1.14 albertel 91: sub start_input {
1.135 albertel 92: my ($parstack,$safeeval)=@_;
93: my $id = &Apache::lonxml::get_param('id',$parstack,$safeeval);
94: if ($id eq '') { $id = $Apache::lonxml::curdepth; }
95: push (@Apache::inputtags::input,$id);
96: push (@Apache::inputtags::inputlist,$id);
97: return $id;
1.14 albertel 98: }
99:
100: sub end_input {
1.135 albertel 101: pop @Apache::inputtags::input;
102: return '';
1.14 albertel 103: }
104:
1.124 www 105: sub addchars {
106: my ($fieldid,$addchars)=@_;
107: my $output='';
108: foreach (split(/\,/,$addchars)) {
109: $output.='<a href="javascript:void(document.forms.lonhomework.'.
110: $fieldid.'.value+=\''.$_.'\')">'.$_.'</a> ';
111: }
112: return $output;
113: }
114:
1.48 albertel 115: sub start_textfield {
1.135 albertel 116: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
117: my $result = "";
118: my $id = &start_input($parstack,$safeeval);
119: my $resid=$Apache::inputtags::response[-1];
120: if ($target eq 'web') {
121: $Apache::lonxml::evaluate--;
122: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
123: my $partid=$Apache::inputtags::part;
1.138 albertel 124: my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$resid.submission"},'<>&"');
1.135 albertel 125: my $cols = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
126: if ( $cols eq '') { $cols = 80; }
127: my $rows = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
1.143 www 128: if ( $rows eq '') { $rows = 16; }
1.135 albertel 129: my $addchars=&Apache::lonxml::get_param('addchars',$parstack,$safeeval);
130: $result='';
131: if ($addchars) {
132: $result.=&addchars('HWVAL_'.$resid,$addchars);
133: }
1.143 www 134: push @Apache::lonxml::htmlareafields,'HWVAL_'.$resid;
135: $result.= '<textarea wrap="hard" name="HWVAL_'.$resid.'" id="HWVAL_'.$resid.'" '.
1.135 albertel 136: "rows=\"$rows\" cols=\"$cols\">".$oldresponse;
137: if ($oldresponse ne '') {
1.143 www 138:
1.135 albertel 139: #get rid of any startup text if the user has already responded
140: &Apache::lonxml::get_all_text("/textfield",$parser);
141: }
142: } else {
143: #right or wrong don't show it
144: #$result='<table border="1"><tr><td><i>'.$oldresponse.'</i></td></tr></table>';
145: $result='';
146: #get rid of any startup text
1.84 albertel 147: &Apache::lonxml::get_all_text("/textfield",$parser);
1.61 albertel 148: }
1.135 albertel 149: } elsif ($target eq 'grade') {
150: my $seedtext=&Apache::lonxml::get_all_text("/textfield",$parser);
151: if ($seedtext eq $ENV{'form.HWVAL_'.$resid}) {
152: # if the seed text is still there it wasn't a real submission
153: $ENV{'form.HWVAL_'.$resid}='';
154: }
155: } elsif ($target eq 'edit') {
156: $result.=&Apache::edit::tag_start($target,$token);
157: $result.=&Apache::edit::text_arg('Rows:','rows',$token,4);
158: $result.=&Apache::edit::text_arg('Columns:','cols',$token,4);
159: $result.=&Apache::edit::text_arg
160: ('Click-On Texts (comma sep):','addchars',$token,10);
161: my $bodytext=&Apache::lonxml::get_all_text("/textfield",$parser);
162: $result.=&Apache::edit::editfield($token->[1],$bodytext,'Text you want to appear by default:',80,2);
163: } elsif ($target eq 'modified') {
164: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
165: $safeeval,'rows','cols',
166: 'addchars');
167: if ($constructtag) {
168: $result = &Apache::edit::rebuild_tag($token);
169: } else {
170: $result=$token->[4];
171: }
172: $result.=&Apache::edit::modifiedfield("/textfield",$parser);
173: } elsif ($target eq 'tex') {
174: my $number_of_lines = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
175: my $width_of_box = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
176: if ($$tagstack[-2] eq 'essayresponse' and $Apache::lonhomework::type eq 'exam') {
177: $result = '\fbox{\fbox{\parbox{\textwidth-5mm}{';
178: for (my $i=0;$i<int $number_of_lines*2;$i++) {$result.='\strut \\\\ ';}
179: $result.='\strut \\\\\strut \\\\\strut \\\\\strut \\\\}}}';
180: } else {
181: my $TeXwidth=$width_of_box/80;
182: $result = '\vskip 1 mm \fbox{\fbox{\parbox{'.$TeXwidth.'\textwidth-5mm}{';
183: for (my $i=0;$i<int $number_of_lines*2;$i++) {$result.='\strut \\\\ ';}
184: $result.='}}}\vskip 2 mm ';
185: }
1.60 albertel 186: }
1.135 albertel 187: return $result;
1.6 albertel 188: }
189:
1.48 albertel 190: sub end_textfield {
1.135 albertel 191: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
192: my $result;
193: if ($target eq 'web') {
194: $Apache::lonxml::evaluate++;
195: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
196: return "</textarea>";
197: }
198: } elsif ($target eq 'edit') {
199: $result=&Apache::edit::end_table();
200: }
201: &end_input;
202: return $result;
1.6 albertel 203: }
204:
1.1 albertel 205: sub start_textline {
1.135 albertel 206: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
207: my $result = "";
208: if ($target eq 'web') {
209: $Apache::lonxml::evaluate--;
210: my $partid=$Apache::inputtags::part;
211: my $id=$Apache::inputtags::response[-1];
212: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
213: my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
214: my $maxlength;
215: if ($size eq '') { $size=20; } else {
216: if ($size < 20) { $maxlength=$size; }
217: }
1.138 albertel 218: my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$id.submission"},'<>&"');
1.135 albertel 219: if ($Apache::lonhomework::type ne 'exam') {
220: my $addchars=&Apache::lonxml::get_param('addchars',$parstack,$safeeval);
221: $result='';
222: if ($addchars) {
223: $result.=&addchars('HWVAL_'.$id,$addchars);
224: }
1.157 albertel 225: my $readonly=&Apache::lonxml::get_param('readonly',$parstack,
226: $safeeval);
227: if (lc($readonly) eq 'yes') {
228: $readonly=' readonly="readonly" ';
1.158 albertel 229: } else {
230: $readonly='';
1.157 albertel 231: }
232: $result.= '<input type="text" '.$readonly.' name="HWVAL_'.$id.'" value="'.
1.135 albertel 233: $oldresponse.'" size="'.$size.'" maxlength="'.$maxlength.'" />';
234: }
235: } else {
236: #right or wrong don't show what was last typed in.
1.144 albertel 237: $result='<i>'.$Apache::inputtags::answertxt{$id}.'</i>';
238: #$result='';
1.135 albertel 239: }
240: } elsif ($target eq 'edit') {
241: $result=&Apache::edit::tag_start($target,$token);
242: $result.=&Apache::edit::text_arg('Size:','size',$token,'5').
1.157 albertel 243: &Apache::edit::text_arg('Click-On Texts (comma sep):',
244: 'addchars',$token,10);
245: $result.=&Apache::edit::select_arg('Readonly:','readonly',
246: ['no','yes'],$token);
247: $result.=&Apache::edit::end_row();
248: $result.=&Apache::edit::end_table();
1.135 albertel 249: } elsif ($target eq 'modified') {
1.157 albertel 250: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
251: $safeeval,'size',
252: 'addchars','readonly');
1.135 albertel 253: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
254: } elsif ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
255: my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
256: if ($size != 0) {$size=$size*2; $size.=' mm';} else {$size='40 mm';}
257: $result='\framebox['.$size.'][s]{\tiny\strut}';
258: }
259: return $result;
1.1 albertel 260: }
261:
262: sub end_textline {
1.135 albertel 263: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
264: if ($target eq 'web') { $Apache::lonxml::evaluate++; }
265: elsif ($target eq 'edit') { return ('','no'); }
266: return "";
1.9 albertel 267: }
268:
1.98 albertel 269: sub start_hiddenline {
270: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
271: my $result = "";
272: if ($target eq 'web') {
273: $Apache::lonxml::evaluate--;
274: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
275: my $partid=$Apache::inputtags::part;
276: my $id=$Apache::inputtags::response[-1];
1.138 albertel 277: my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$id.submission"},'<>&"');
1.98 albertel 278: if ($Apache::lonhomework::type ne 'exam') {
279: $result= '<input type="hidden" name="HWVAL_'.$id.'" value="'.
280: $oldresponse.'" />';
281: }
282: }
283: } elsif ($target eq 'edit') {
284: $result=&Apache::edit::tag_start($target,$token);
285: $result.=&Apache::edit::end_table;
286: }
287: return $result;
288: }
289:
290: sub end_hiddenline {
1.135 albertel 291: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
292: if ($target eq 'web') { $Apache::lonxml::evaluate++; }
293: elsif ($target eq 'edit') { return ('','no'); }
294: return "";
1.98 albertel 295: }
296:
1.160 albertel 297: # $part -> partid
298: # $id -> responseid
299: # $uploadefiletypes -> comma seperated list of extensions allowed or * for any
300: # $which -> 'uploadedonly' -> only newly uploaded files
301: # 'portfolioonly' -> only allow files from portfolio
302: # 'both' -> allow files from either location
303: # returns a table row <tr>
304: sub file_selector {
305: my ($part,$id,$uploadedfiletypes,$which)=@_;
306: if (!$uploadedfiletypes) { return ''; }
307: my $result;
308:
1.162 albertel 309: $result.='<tr><td>';
310: if ($uploadedfiletypes ne '*') {
311: $result.=
312: &mt('Allowed filetypes: <b>[_1]</b>',$uploadedfiletypes).'<br />';
313: }
1.160 albertel 314: if ($which eq 'uploadonly' || $which eq 'both') {
315: $result.=&mt('Submit a file: (only one file can be uploaded)').
316: ' <br /><input type="file" size="50" name="HWFILE'.
317: $part.'_'.$id.'" /><br />';
318: my $uploadedfile= &HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.uploadedfile"},'<>&"');
319:
320: if ($uploadedfile) {
321: my $url=$Apache::lonhomework::history{"resource.$part.$id.uploadedurl"};
322: push (@Apache::lonxml::extlinks,$url);
323: &Apache::lonnet::allowuploaded('/adm/essayresponse',$url);
324: my $icon=&Apache::loncommon::icon($url);
325: my $curfile='<a href="'.$url.'"><img src="'.$icon.
326: '" border="0" />'.$uploadedfile.'</a>';
327: $result.=&mt('Currently submitted: <tt>[_1]</tt>',$curfile);
328: } else {
329: #$result.=&mt('(Hand in a file you have prepared on your computer)');
330: }
331: }
332: if ( $which eq 'both') {
333: $result.='<br />'.'<strong>'.&mt('OR:').'</strong><br />';
334: }
335: if ($which eq 'portfolioonly' || $which eq 'both') {
336: $result.='<a href='."'".'javascript:void(window.open("/adm/portfolio?mode=selectfile&fieldname=HWPORT'.$part.'_'.$id.'","cat","height=600,width=800,scrollbars=1,resizable=1,menubar=2,location=1"))'."'".'>'.
337: &mt('Select Portfolio Files').'</a><br />'.
338: '<input type="text" size="50" name="HWPORT'.$part.'_'.$id.'" value="" />'.
339: '<br />';
340: if ($Apache::lonhomework::history{"resource.$part.$id.portfiles"}=~/[^\s]/){
1.161 albertel 341: my @filelist;
1.160 albertel 342: foreach my $file (split(',',&Apache::lonnet::unescape($Apache::lonhomework::history{"resource.$part.$id.portfiles"}))) {
343: my (undef,undef,$domain,$user)=&Apache::lonxml::whichuser();
344: my $url="/uploaded/$domain/$user/portfolio$file";
345: my $icon=&Apache::loncommon::icon($url);
1.161 albertel 346: push(@filelist,'<a href="'.$url.'"><img src="'.$icon.
347: '" border="0" />'.$file.'</a>');
1.160 albertel 348: }
1.161 albertel 349: $result.=&mt("Portfolio files previously selected: <strong>[_1]</strong>",join(', ',@filelist));
1.160 albertel 350: }
351: }
352: $result.='</td></tr>';
353: return $result;
354: }
355:
1.136 albertel 356: sub checkstatus {
357: my ($value,$awardref,$msgref)=@_;
358: for (my $i=0;$i<=$#$awardref;$i++) {
359: if ($$awardref[$i] eq $value) {
360: return ($$awardref[$i],$$msgref[$i]);
361: }
362: }
363: return(undef,undef);
364: }
365:
1.9 albertel 366: sub finalizeawards {
1.136 albertel 367: my ($awardref,$msgref)=@_;
368: my $result=undef;
1.135 albertel 369: my $award;
1.136 albertel 370: my $msg;
371: if ($#$awardref == -1) { $result = "NO_RESPONSE"; }
1.135 albertel 372: if ($result eq '' ) {
373: my $blankcount;
1.136 albertel 374: foreach $award (@$awardref) {
1.135 albertel 375: if ($award eq '') {
376: $result='MISSING_ANSWER';
377: $blankcount++;
378: }
379: }
1.136 albertel 380: if ($blankcount == ($#$awardref + 1)) { $result = 'NO_RESPONSE'; }
1.135 albertel 381: }
1.136 albertel 382: if (defined($result)) { return ($result,$msg); }
383: foreach my $possibleaward ('MISSING_ANSWER', 'ERROR', 'NO_RESPONSE',
1.137 albertel 384: 'TOO_LONG', 'UNIT_INVALID_INSTRUCTOR',
385: 'UNIT_INVALID_STUDENT', 'UNIT_IRRECONCIBLE',
386: 'UNIT_FAIL', 'NO_UNIT',
1.136 albertel 387: 'UNIT_NOTNEEDED', 'WANTED_NUMERIC',
388: 'BAD_FORMULA', 'SIG_FAIL', 'INCORRECT',
389: 'MISORDERED_RANK', 'INVALID_FILETYPE',
390: 'DRAFT', 'SUBMITTED', 'ASSIGNED_SCORE',
1.153 albertel 391: 'APPROX_ANS', 'EXACT_ANS','COMMA_FAIL') {
1.136 albertel 392: ($result,$msg)=&checkstatus($possibleaward,$awardref,$msgref);
393: if (defined($result)) { return ($result,$msg); }
1.135 albertel 394: }
1.136 albertel 395: return ('ERROR',undef);
1.9 albertel 396: }
397:
1.10 albertel 398: sub decideoutput {
1.136 albertel 399: my ($award,$awardmsg,$solved,$previous,$target)=@_;
1.135 albertel 400: my $message='';
401: my $button=0;
402: my $previousmsg;
403: my $bgcolor='orange';
1.148 albertel 404: my $added_computer_text=0;
1.135 albertel 405: my %possiblecolors =
406: ( 'correct' => '#aaffaa',
407: 'charged_try' => '#ffaaaa',
408: 'not_charged_try' => '#ffffaa',
409: 'no_message' => '#fffff',
410: );
411: if ($previous) { $previousmsg=&mt('You have entered that answer before'); }
412:
413: if ($solved =~ /^correct/) {
414: if ($award eq 'ASSIGNED_SCORE') {
415: $message = &mt("A score has been assigned.");
416: } else {
417: if ($target eq 'tex') {
418: $message = '\textbf{'.&mt('You are correct.').'}';
419: } else {
420: $message = "<b>".&mt('You are correct.')."</b>";
1.150 albertel 421: $message.=" ".&mt("Computer's answer now shown above.");
1.135 albertel 422: }
1.148 albertel 423: $added_computer_text=1;
1.135 albertel 424: unless ($ENV{'course.'.
425: $ENV{'request.course.id'}.
426: '.disable_receipt_display'} eq 'yes') {
427: $message.=(($target eq 'web')?'<br />':' ').
428: &mt('Your receipt is').' '.&Apache::lonnet::receipt($Apache::inputtags::part).
429: (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'');
430: }
431: }
432: $bgcolor=$possiblecolors{'correct'};
433: $button=0;
434: $previousmsg='';
435: } elsif ($solved =~ /^excused/) {
436: if ($target eq 'tex') {
437: $message = ' \textbf{'.&mt('You are excused from the problem.').'} ';
438: } else {
439: $message = "<b>".&mt('You are excused from the problem.')."</b>";
440: }
441: $bgcolor=$possiblecolors{'charged_try'};
442: $button=0;
443: $previousmsg='';
444: } elsif ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
445: if ($solved =~ /^incorrect/ || $solved eq '') {
1.144 albertel 446: $message = &mt("Incorrect").".";
1.135 albertel 447: $bgcolor=$possiblecolors{'charged_try'};
448: $button=1;
449: } else {
1.144 albertel 450: if ($target eq 'tex') {
451: $message = '\textbf{'.&mt('You are correct.').'}';
452: } else {
453: $message = "<b>".&mt('You are correct.')."</b>";
1.150 albertel 454: $message.=" ".&mt("Computer's answer now shown above.");
1.144 albertel 455: }
1.148 albertel 456: $added_computer_text=1;
1.135 albertel 457: unless ($ENV{'course.'.
458: $ENV{'request.course.id'}.
459: '.disable_receipt_display'} eq 'yes') {
460: $message.=(($target eq 'web')?'<br />':' ').
461: 'Your receipt is '.&Apache::lonnet::receipt($Apache::inputtags::part).
462: (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'');
463: }
464: $bgcolor=$possiblecolors{'correct'};
465: $button=0;
466: $previousmsg='';
467: }
468: } elsif ($award eq 'NO_RESPONSE') {
469: $message = '';
470: $bgcolor=$possiblecolors{'no_feedback'};
471: $button=1;
472: } elsif ($award eq 'MISSING_ANSWER') {
473: $message = &mt('Some items were not submitted.');
474: $bgcolor=$possiblecolors{'not_charged_try'};
475: $button = 1;
476: } elsif ($award eq 'ERROR') {
477: $message = &mt('An error occured while grading your answer.');
478: $bgcolor=$possiblecolors{'not_charged_try'};
479: $button = 1;
480: } elsif ($award eq 'TOO_LONG') {
481: $message = &mt("The submitted answer was too long.");
482: $bgcolor=$possiblecolors{'not_charged_try'};
483: $button=1;
484: } elsif ($award eq 'WANTED_NUMERIC') {
485: $message = &mt("This question expects a numeric answer.");
486: $bgcolor=$possiblecolors{'not_charged_try'};
487: $button=1;
488: } elsif ($award eq 'MISORDERED_RANK') {
489: $message = &mt('You have provided an invalid ranking');
490: if ($target ne 'tex') {
1.159 albertel 491: $message.=', '.&mt('please refer to').' '.&Apache::loncommon::help_open_topic('Ranking_Problems','help on ranking problems');
1.135 albertel 492: }
493: $bgcolor=$possiblecolors{'not_charged_try'};
494: $button=1;
495: } elsif ($award eq 'INVALID_FILETYPE') {
496: $message = &mt('The filetype extension of the file you uploaded is not allowed.');
497: $bgcolor=$possiblecolors{'not_charged_try'};
498: $button=1;
499: } elsif ($award eq 'SIG_FAIL') {
1.145 albertel 500: my ($used,$min,$max)=split(':',$awardmsg);
501: my $word;
502: if ($used < $min) { $word=&mt('more'); }
503: if ($used > $max) { $word=&mt('fewer'); }
504: $message = &mt("Submission not graded. Use [_2] digits.",$used,$word);
1.135 albertel 505: $bgcolor=$possiblecolors{'not_charged_try'};
506: $button=1;
1.137 albertel 507: } elsif ($award eq 'UNIT_INVALID_INSTRUCTOR') {
508: $message = &mt('Error in instructor specifed unit. This error has been reported to the instructor.', $awardmsg);
509: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');}
510: $bgcolor=$possiblecolors{'not_charged_try'};
511: $button=1;
512: } elsif ($award eq 'UNIT_INVALID_STUDENT') {
1.155 albertel 513: $message = &mt('Unable to interpret units. Computer reads units as "[_1]".',&markup_unit($awardmsg,$target));
1.137 albertel 514: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');}
515: $bgcolor=$possiblecolors{'not_charged_try'};
516: $button=1;
1.140 matthew 517: } elsif ($award eq 'UNIT_FAIL' || $award eq 'UNIT_IRRECONCIBLE') {
1.155 albertel 518: $message = &mt('Incompatible units. No conversion found between "[_1]" and the required units.',&markup_unit($awardmsg,$target));
1.136 albertel 519: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');}
1.135 albertel 520: $bgcolor=$possiblecolors{'not_charged_try'};
521: $button=1;
522: } elsif ($award eq 'UNIT_NOTNEEDED') {
1.155 albertel 523: $message = &mt('Only a number required. Computer reads units of "[_1]".',&markup_unit($awardmsg,$target));
1.135 albertel 524: $bgcolor=$possiblecolors{'not_charged_try'};
525: $button=1;
526: } elsif ($award eq 'NO_UNIT') {
1.144 albertel 527: $message = &mt("Units required").'.';
1.135 albertel 528: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units')};
529: $bgcolor=$possiblecolors{'not_charged_try'};
530: $button=1;
1.153 albertel 531: } elsif ($award eq 'COMMA_FAIL') {
532: $message = &mt("Proper comma separation is required").'.';
533: $bgcolor=$possiblecolors{'not_charged_try'};
534: $button=1;
1.135 albertel 535: } elsif ($award eq 'BAD_FORMULA') {
536: $message = &mt("Unable to understand formula");
537: $bgcolor=$possiblecolors{'not_charged_try'};
538: $button=1;
539: } elsif ($award eq 'INCORRECT') {
1.144 albertel 540: $message = &mt("Incorrect").'.';
1.135 albertel 541: $bgcolor=$possiblecolors{'charged_try'};
542: $button=1;
543: } elsif ($award eq 'SUBMITTED') {
544: $message = &mt("Your submission has been recorded.");
545: $bgcolor=$possiblecolors{'correct'};
546: $button=1;
547: } elsif ($award eq 'DRAFT') {
1.144 albertel 548: $message = &mt("A draft copy has been saved.");
1.135 albertel 549: $bgcolor=$possiblecolors{'not_charged_try'};
550: $button=1;
551: } elsif ($award eq 'ASSIGNED_SCORE') {
1.144 albertel 552: $message = &mt("A score has been assigned.");
1.135 albertel 553: $bgcolor=$possiblecolors{'correct'};
554: $button=0;
1.144 albertel 555: } elsif ($award eq '') {
556: $bgcolor=$possiblecolors{'not_charged_try'};
557: $button=1;
1.135 albertel 558: } else {
559: $message = &mt("Unknown message").": $award";
560: $button=1;
561: }
562: if (lc($Apache::lonhomework::problemstatus) eq 'no' &&
563: $Apache::inputtags::status[-1] ne 'SHOW_ANSWER') {
564: $message = &mt("Answer Submitted: Your final submission will be graded after the due date.");
565: $bgcolor=$possiblecolors{'correct'};
566: $button=1;
567: }
1.148 albertel 568: if ($Apache::inputtags::status[-1] eq 'SHOW_ANSWER' &&
1.150 albertel 569: !$added_computer_text && $target ne 'tex') {
1.149 albertel 570: $message.=" ".&mt("Computer's answer now shown above.");
1.148 albertel 571: $added_computer_text=1;
1.144 albertel 572: }
1.135 albertel 573: return ($button,$bgcolor,$message,$previousmsg);
1.12 albertel 574: }
575:
1.155 albertel 576: sub markup_unit {
577: my ($unit,$target)=@_;
578: if ($target eq 'tex') {
579: return '\texttt{'.&Apache::lonxml::latex_special_symbols($unit).'}';
580: } else {
581: return "<tt>".$unit."</tt>";
582: }
583: }
584:
1.88 albertel 585: sub removealldata {
1.87 albertel 586: my ($id)=@_;
587: foreach my $key (keys(%Apache::lonhomework::results)) {
588: if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
589: &Apache::lonxml::debug("Removing $key");
590: delete($Apache::lonhomework::results{$key});
591: }
592: }
593: }
594:
1.142 albertel 595: sub hidealldata {
596: my ($id)=@_;
597: foreach my $key (keys(%Apache::lonhomework::results)) {
598: if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
599: &Apache::lonxml::debug("Hidding $key");
600: my $newkey=$key;
601: $newkey=~s/^(resource\.\Q$id\E\.[^\.]+\.)(.*)$/${1}hidden${2}/;
602: $Apache::lonhomework::results{$newkey}=
603: $Apache::lonhomework::results{$key};
604: delete($Apache::lonhomework::results{$key});
605: }
606: }
607: }
608:
1.12 albertel 609: sub setgradedata {
1.136 albertel 610: my ($award,$msg,$id,$previously_used) = @_;
1.154 albertel 611: if ($Apache::lonhomework::scantronmode &&
612: &Apache::lonnet::validCODE($ENV{'form.CODE'})) {
1.141 albertel 613: $Apache::lonhomework::results{"resource.CODE"}=$ENV{'form.CODE'};
1.154 albertel 614: } elsif ($Apache::lonhomework::scantronmode &&
615: $ENV{'form.CODE'} eq '' &&
616: $Apache::lonhomework::history{"resource.CODE"} ne '') {
617: $Apache::lonhomework::results{"resource.CODE"}='';
1.141 albertel 618: }
1.154 albertel 619:
1.135 albertel 620: if (!$Apache::lonhomework::scantronmode &&
621: $Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
622: $Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER') {
623: $Apache::lonhomework::results{"resource.$id.afterduedate"}=$award;
1.87 albertel 624: return '';
1.135 albertel 625: } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} !~
626: /^correct/ || $Apache::lonhomework::scantronmode ||
627: lc($Apache::lonhomework::problemstatus) eq 'no') {
1.154 albertel 628: # the student doesn't already have it correct,
629: # or we are in a mode (scantron orno problem status) where a correct
630: # can become incorrect
631: # handle assignment of tries and solved status
1.135 albertel 632: my $solvemsg;
633: if ($Apache::lonhomework::scantronmode) {
634: $solvemsg='correct_by_scantron';
635: } else {
636: $solvemsg='correct_by_student';
637: }
638: if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
639: $Apache::lonhomework::results{"resource.$id.afterduedate"}='';
640: }
641: if ( $award eq 'ASSIGNED_SCORE') {
642: $Apache::lonhomework::results{"resource.$id.tries"} =
643: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
644: $Apache::lonhomework::results{"resource.$id.solved"} =
645: $solvemsg;
646: my $numawards=scalar(@Apache::inputtags::response);
647: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
648: foreach my $res (@Apache::inputtags::response) {
649: $Apache::lonhomework::results{"resource.$id.awarded"}+=
650: $Apache::lonhomework::results{"resource.$id.$res.awarded"};
651: }
652: if ($numawards > 0) {
653: $Apache::lonhomework::results{"resource.$id.awarded"}/=
654: $numawards;
655: }
656: } elsif ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
657: $Apache::lonhomework::results{"resource.$id.tries"} =
658: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
659: $Apache::lonhomework::results{"resource.$id.solved"} =
660: $solvemsg;
661: $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
662: } elsif ( $award eq 'INCORRECT' ) {
663: $Apache::lonhomework::results{"resource.$id.tries"} =
664: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
1.152 albertel 665: if (lc($Apache::lonhomework::problemstatus) eq 'no' ||
666: $Apache::lonhomework::scantronmode) {
1.135 albertel 667: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
668: }
669: $Apache::lonhomework::results{"resource.$id.solved"} =
670: 'incorrect_attempted';
671: } elsif ( $award eq 'SUBMITTED' ) {
672: $Apache::lonhomework::results{"resource.$id.tries"} =
673: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
674: $Apache::lonhomework::results{"resource.$id.solved"} =
675: 'ungraded_attempted';
676: } elsif ( $award eq 'DRAFT' ) {
677: $Apache::lonhomework::results{"resource.$id.solved"} = '';
678: } elsif ( $award eq 'NO_RESPONSE' ) {
679: #no real response so delete any data that got stored
1.129 albertel 680: &removealldata($id);
681: return '';
682: } else {
1.135 albertel 683: $Apache::lonhomework::results{"resource.$id.solved"} =
684: 'incorrect_attempted';
1.152 albertel 685: if (lc($Apache::lonhomework::problemstatus) eq 'no' ||
686: $Apache::lonhomework::scantronmode) {
1.135 albertel 687: $Apache::lonhomework::results{"resource.$id.tries"} =
688: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
689: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
690: }
691: }
1.136 albertel 692: if (defined($msg)) {
693: $Apache::lonhomework::results{"resource.$id.awardmsg"} = $msg;
694: }
1.135 albertel 695: # did either of the overall awards chage? If so ignore the
696: # previous check
697: if (($Apache::lonhomework::results{"resource.$id.awarded"} eq
698: $Apache::lonhomework::history{"resource.$id.awarded"}) &&
699: ($Apache::lonhomework::results{"resource.$id.solved"} eq
700: $Apache::lonhomework::history{"resource.$id.solved"})) {
701: # check if this was a previous submission if it was delete the
702: # unneeded data and update the previously_used attribute
703: if ( $previously_used eq 'PREVIOUSLY_USED') {
704: if (lc($Apache::lonhomework::problemstatus) ne 'no') {
705: delete($Apache::lonhomework::results{"resource.$id.tries"});
706: $Apache::lonhomework::results{"resource.$id.previous"} = '1';
707: }
708: } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
709: #delete all data as they student didn't do anything, but save
710: #the list of collaborators.
711: &removealldata($id);
712: #and since they didn't do anything we were never here
713: return '';
714: } else {
715: $Apache::lonhomework::results{"resource.$id.previous"} = '0';
716: }
1.101 albertel 717: }
1.135 albertel 718: } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} =~
719: /^correct/ ) {
720: #delete all data as they student already has it correct
721: &removealldata($id);
722: #and since they didn't do anything we were never here
723: return '';
1.40 albertel 724: }
1.135 albertel 725: $Apache::lonhomework::results{"resource.$id.award"} = $award;
1.10 albertel 726: }
727:
1.9 albertel 728: sub grade {
1.135 albertel 729: my ($target) = @_;
730: my $id = $Apache::inputtags::part;
731: my $response='';
732: if ( defined $ENV{'form.submitted'}) {
1.136 albertel 733: my (@awards,@msgs);
1.135 albertel 734: foreach $response (@Apache::inputtags::response) {
735: &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
736: my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
737: &Apache::lonxml::debug("keeping $value from $response for $id");
738: push (@awards,$value);
1.136 albertel 739: $value=$Apache::lonhomework::results{"resource.$id.$response.awardmsg"};
740: &Apache::lonxml::debug("got message $value from $response for $id");
741: push (@msgs,$value);
1.135 albertel 742: }
1.136 albertel 743: my ($finalaward,$msg) = &finalizeawards(\@awards,\@msgs);
1.135 albertel 744: my $previously_used;
745: if ( $#Apache::inputtags::previous eq $#awards ) {
746: my $match=0;
747: my @matches;
748: foreach my $versionar (@Apache::inputtags::previous_version) {
749: foreach my $version (@$versionar) {
750: $matches[$version]++;
751: }
752: }
753: foreach my $elem (@matches) {if ($elem eq ($#awards+1)) {$match=1;}}
754: if ($match) {
755: $previously_used = 'PREVIOUSLY_LAST';
756: foreach my $value (@Apache::inputtags::previous) {
757: if ($value eq 'PREVIOUSLY_USED' ) {
758: $previously_used = $value;
759: last;
760: }
1.75 albertel 761: }
762: }
1.43 albertel 763: }
1.136 albertel 764: &Apache::lonxml::debug("final award $finalaward, $previously_used, message $msg");
765: &setgradedata($finalaward,$msg,$id,$previously_used);
1.43 albertel 766: }
1.135 albertel 767: return '';
1.1 albertel 768: }
769:
1.11 albertel 770: sub gradestatus {
1.135 albertel 771: my ($id,$target) = @_;
772: my $showbutton = 1;
773: my $bgcolor = '';
774: my $message = '';
775: my $latemessage = '';
776: my $trystr='';
777: my $button='';
778: my $previousmsg='';
779:
780: my $status = $Apache::inputtags::status['-1'];
781: &Apache::lonxml::debug("gradestatus has :$status:");
782: if ( $status ne 'CLOSED' && $status ne 'UNAVAILABLE' &&
783: $status ne 'INVALID_ACCESS') {
784: my $award = $Apache::lonhomework::history{"resource.$id.award"};
785: my $solved = $Apache::lonhomework::history{"resource.$id.solved"};
786: my $previous = $Apache::lonhomework::history{"resource.$id.previous"};
1.136 albertel 787: my $awardmsg = $Apache::lonhomework::history{"resource.$id.awardmsg"};
788: &Apache::lonxml::debug("Found Award |$award|$solved|$awardmsg");
1.144 albertel 789: if ( $award ne '' || $solved ne '' || $status eq 'SHOW_ANSWER') {
1.135 albertel 790: &Apache::lonxml::debug('Getting message');
791: ($showbutton,$bgcolor,$message,$previousmsg) =
1.136 albertel 792: &decideoutput($award,$awardmsg,$solved,$previous,$target);
1.135 albertel 793: if ($target eq 'tex') {
794: $message='\vskip 2 mm '.$message.' ';
795: } else {
796: $message="<td bgcolor=\"$bgcolor\">$message</td>";
797: if ($previousmsg) {
798: $previousmsg="<td bgcolor=\"#aaaaff\">$previousmsg</td>";
799: }
800: }
801: }
802: my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
803: my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
804: &Apache::lonxml::debug("got maxtries of :$maxtries:");
805: #if tries are set to negative turn off the Tries/Button and messages
806: if (defined($maxtries) && $maxtries < 0) { return ''; }
807: if ( $tries eq '' ) { $tries = '0'; }
808: if ( $maxtries eq '' ) { $maxtries = '2'; }
809: if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; }
810: my $tries_text=&mt('Tries');
1.164 ! albertel 811: if ( $Apache::lonhomework::type eq 'survey' ||
! 812: $Apache::lonhomework::parsing_a_task) {
! 813: $tries_text=&mt('Submissions');
! 814: }
1.135 albertel 815: if ( $showbutton ) {
816: if ($target eq 'tex') {
1.147 sakharuk 817: if ($ENV{'request.state'} ne "construct" && $Apache::lonhomework::type ne 'exam' && $ENV{'form.suppress_tries'} ne 'yes') {
1.135 albertel 818: $trystr = ' {\vskip 1 mm \small \textit{'.$tries_text.'} '.$tries.'/'.$maxtries.'} \vskip 2 mm ';
819: } else {
820: $trystr = '\vskip 0 mm ';
821: }
822: } else {
1.136 albertel 823: $trystr = "<td><nobr>".$tries_text." $tries";
1.164 ! albertel 824: if ($Apache::lonhomework::parsing_a_task) {
! 825: } elsif($ENV{'request.state'} ne 'construct') {
1.135 albertel 826: $trystr.="/$maxtries";
827: } else {
828: if (defined($Apache::inputtags::params{'maxtries'})) {
829: $trystr.="/".$Apache::inputtags::params{'maxtries'};
830: }
831: }
1.136 albertel 832: $trystr.="</nobr></td>";
1.135 albertel 833: }
834: }
835: if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {$showbutton = 0;}
836: if ( $showbutton ) {
837: if ($target ne 'tex') {
1.156 albertel 838: $button = '<input type="submit" name="submit_'.$id.'" value="'.&mt('Submit Answer').'" />';
1.135 albertel 839: }
840: }
841: if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
842: #last submissions was after due date
1.163 albertel 843: $latemessage=&mt(' The last submission was after the Due Date ');;
844: if ($target eq 'web') {
845: $latemessage='<td bgcolor="#ffaaaa">'.$latemessage.'</td>';
1.135 albertel 846: }
847: }
848: }
849: my $output= $previousmsg.$latemessage.$message.$trystr;
850: if ($output =~ /^\s*$/) {
851: return $button;
1.63 sakharuk 852: } else {
1.135 albertel 853: if ($target eq 'tex') {
854: return $button.' \vskip 0 mm '.$output.' ';
855: } else {
856: return '<table><tr><td>'.$button.'</td>'.$output.'</tr></table>';
857: }
1.63 sakharuk 858: }
1.11 albertel 859: }
1.1 albertel 860: 1;
861: __END__
1.43 albertel 862:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>