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