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