Annotation of loncom/homework/inputtags.pm, revision 1.181
1.43 albertel 1: # The LearningOnline Network with CAPA
2: # input definitons
1.47 albertel 3: #
1.181 ! albertel 4: # $Id: inputtags.pm,v 1.180 2005/11/15 22:28:30 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) =@_;
391: foreach my $possibleaward ('MISSING_ANSWER', 'ERROR', 'NO_RESPONSE',
392: 'TOO_LONG', 'UNIT_INVALID_INSTRUCTOR',
393: 'UNIT_INVALID_STUDENT', 'UNIT_IRRECONCIBLE',
394: 'UNIT_FAIL', 'NO_UNIT',
395: 'UNIT_NOTNEEDED', 'WANTED_NUMERIC',
396: 'BAD_FORMULA', 'SIG_FAIL', 'INCORRECT',
397: 'MISORDERED_RANK', 'INVALID_FILETYPE',
398: 'DRAFT', 'SUBMITTED', 'ASSIGNED_SCORE',
399: 'APPROX_ANS', 'EXACT_ANS','COMMA_FAIL') {
400: if ($award eq $possibleaward) { return 1; }
401: }
402: return 0;
403: }
404:
1.9 albertel 405: sub finalizeawards {
1.181 ! albertel 406: my ($awardref,$msgref,$nameref,$reverse)=@_;
1.136 albertel 407: my $result=undef;
1.135 albertel 408: my $award;
1.136 albertel 409: my $msg;
410: if ($#$awardref == -1) { $result = "NO_RESPONSE"; }
1.135 albertel 411: if ($result eq '' ) {
412: my $blankcount;
1.136 albertel 413: foreach $award (@$awardref) {
1.135 albertel 414: if ($award eq '') {
415: $result='MISSING_ANSWER';
416: $blankcount++;
417: }
418: }
1.136 albertel 419: if ($blankcount == ($#$awardref + 1)) { $result = 'NO_RESPONSE'; }
1.135 albertel 420: }
1.136 albertel 421: if (defined($result)) { return ($result,$msg); }
1.181 ! albertel 422:
! 423: # these awards are ordered from most important error through best correct
! 424:
! 425: my @awards = ('MISSING_ANSWER', 'ERROR', 'NO_RESPONSE', 'TOO_LONG',
! 426: 'UNIT_INVALID_INSTRUCTOR', 'UNIT_INVALID_STUDENT',
! 427: 'UNIT_IRRECONCIBLE', 'UNIT_FAIL', 'NO_UNIT',
! 428: 'UNIT_NOTNEEDED', 'WANTED_NUMERIC', 'BAD_FORMULA',
! 429: 'COMMA_FAIL', 'SIG_FAIL', 'INCORRECT', 'MISORDERED_RANK',
! 430: 'INVALID_FILETYPE', 'DRAFT', 'SUBMITTED', 'ASSIGNED_SCORE',
! 431: 'APPROX_ANS', 'EXACT_ANS');
! 432: if ($reverse) { @awards=reverse(@awards); }
! 433: foreach my $possibleaward (@awards) {
1.136 albertel 434: ($result,$msg)=&checkstatus($possibleaward,$awardref,$msgref);
435: if (defined($result)) { return ($result,$msg); }
1.135 albertel 436: }
1.136 albertel 437: return ('ERROR',undef);
1.9 albertel 438: }
439:
1.10 albertel 440: sub decideoutput {
1.169 albertel 441: my ($award,$awarded,$awardmsg,$solved,$previous,$target)=@_;
1.135 albertel 442: my $message='';
443: my $button=0;
444: my $previousmsg;
445: my $bgcolor='orange';
1.148 albertel 446: my $added_computer_text=0;
1.135 albertel 447: my %possiblecolors =
448: ( 'correct' => '#aaffaa',
449: 'charged_try' => '#ffaaaa',
450: 'not_charged_try' => '#ffffaa',
451: 'no_message' => '#fffff',
452: );
1.169 albertel 453:
1.180 albertel 454: my $part = $Apache::inputtags::part;
455: my $handgrade =
456: ('yes' eq lc(&Apache::lonnet::EXT("resource.$part.handgrade")));
457:
458: my $computer = ($handgrade)? ''
459: : " ".&mt("Computer's answer now shown above.");
460: &Apache::lonxml::debug("handgrade has :$handgrade:");
461:
1.135 albertel 462: if ($previous) { $previousmsg=&mt('You have entered that answer before'); }
463:
464: if ($solved =~ /^correct/) {
1.169 albertel 465: $bgcolor=$possiblecolors{'correct'};
1.170 albertel 466: $message=&mt('You are correct.');
467: if ($awarded < 1 && $awarded > 0) {
468: $message=&mt('You are partially correct.');
469: $bgcolor=$possiblecolors{'not_charged_try'};
470: } elsif ($awarded < 1) {
471: $message=&mt('Incorrect.');
472: $bgcolor=$possiblecolors{'charged_try'};
473: }
1.172 albertel 474: if ($env{'request.filename'} =~
475: m|/res/lib/templates/examupload.problem$|) {
476: $message = &mt("A score has been assigned.");
477: $added_computer_text=1;
1.135 albertel 478: } else {
1.172 albertel 479: if ($target eq 'tex') {
480: $message = '\textbf{'.$message.'}';
481: } else {
482: $message = "<b>".$message."</b>";
1.180 albertel 483: $message.= $computer;
1.135 albertel 484: }
1.172 albertel 485: $added_computer_text=1;
1.165 albertel 486: unless ($env{'course.'.
487: $env{'request.course.id'}.
1.135 albertel 488: '.disable_receipt_display'} eq 'yes') {
489: $message.=(($target eq 'web')?'<br />':' ').
490: &mt('Your receipt is').' '.&Apache::lonnet::receipt($Apache::inputtags::part).
491: (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'');
492: }
493: }
494: $button=0;
495: $previousmsg='';
496: } elsif ($solved =~ /^excused/) {
497: if ($target eq 'tex') {
498: $message = ' \textbf{'.&mt('You are excused from the problem.').'} ';
499: } else {
500: $message = "<b>".&mt('You are excused from the problem.')."</b>";
501: }
502: $bgcolor=$possiblecolors{'charged_try'};
503: $button=0;
504: $previousmsg='';
505: } elsif ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
506: if ($solved =~ /^incorrect/ || $solved eq '') {
1.144 albertel 507: $message = &mt("Incorrect").".";
1.135 albertel 508: $bgcolor=$possiblecolors{'charged_try'};
509: $button=1;
510: } else {
1.144 albertel 511: if ($target eq 'tex') {
512: $message = '\textbf{'.&mt('You are correct.').'}';
513: } else {
514: $message = "<b>".&mt('You are correct.')."</b>";
1.180 albertel 515: $message.= $computer;
1.144 albertel 516: }
1.148 albertel 517: $added_computer_text=1;
1.165 albertel 518: unless ($env{'course.'.
519: $env{'request.course.id'}.
1.135 albertel 520: '.disable_receipt_display'} eq 'yes') {
521: $message.=(($target eq 'web')?'<br />':' ').
522: 'Your receipt is '.&Apache::lonnet::receipt($Apache::inputtags::part).
523: (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'');
524: }
525: $bgcolor=$possiblecolors{'correct'};
526: $button=0;
527: $previousmsg='';
528: }
529: } elsif ($award eq 'NO_RESPONSE') {
530: $message = '';
531: $bgcolor=$possiblecolors{'no_feedback'};
532: $button=1;
533: } elsif ($award eq 'MISSING_ANSWER') {
534: $message = &mt('Some items were not submitted.');
535: $bgcolor=$possiblecolors{'not_charged_try'};
536: $button = 1;
537: } elsif ($award eq 'ERROR') {
538: $message = &mt('An error occured while grading your answer.');
539: $bgcolor=$possiblecolors{'not_charged_try'};
540: $button = 1;
541: } elsif ($award eq 'TOO_LONG') {
542: $message = &mt("The submitted answer was too long.");
543: $bgcolor=$possiblecolors{'not_charged_try'};
544: $button=1;
545: } elsif ($award eq 'WANTED_NUMERIC') {
546: $message = &mt("This question expects a numeric answer.");
547: $bgcolor=$possiblecolors{'not_charged_try'};
548: $button=1;
549: } elsif ($award eq 'MISORDERED_RANK') {
550: $message = &mt('You have provided an invalid ranking');
551: if ($target ne 'tex') {
1.159 albertel 552: $message.=', '.&mt('please refer to').' '.&Apache::loncommon::help_open_topic('Ranking_Problems','help on ranking problems');
1.135 albertel 553: }
554: $bgcolor=$possiblecolors{'not_charged_try'};
555: $button=1;
556: } elsif ($award eq 'INVALID_FILETYPE') {
1.166 albertel 557: $message = &mt('Submission won\'t be graded. The type of file submitted is not allowed.');
1.135 albertel 558: $bgcolor=$possiblecolors{'not_charged_try'};
559: $button=1;
560: } elsif ($award eq 'SIG_FAIL') {
1.145 albertel 561: my ($used,$min,$max)=split(':',$awardmsg);
562: my $word;
563: if ($used < $min) { $word=&mt('more'); }
564: if ($used > $max) { $word=&mt('fewer'); }
565: $message = &mt("Submission not graded. Use [_2] digits.",$used,$word);
1.135 albertel 566: $bgcolor=$possiblecolors{'not_charged_try'};
567: $button=1;
1.137 albertel 568: } elsif ($award eq 'UNIT_INVALID_INSTRUCTOR') {
569: $message = &mt('Error in instructor specifed unit. This error has been reported to the instructor.', $awardmsg);
570: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');}
571: $bgcolor=$possiblecolors{'not_charged_try'};
572: $button=1;
573: } elsif ($award eq 'UNIT_INVALID_STUDENT') {
1.155 albertel 574: $message = &mt('Unable to interpret units. Computer reads units as "[_1]".',&markup_unit($awardmsg,$target));
1.137 albertel 575: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');}
576: $bgcolor=$possiblecolors{'not_charged_try'};
577: $button=1;
1.140 matthew 578: } elsif ($award eq 'UNIT_FAIL' || $award eq 'UNIT_IRRECONCIBLE') {
1.155 albertel 579: $message = &mt('Incompatible units. No conversion found between "[_1]" and the required units.',&markup_unit($awardmsg,$target));
1.136 albertel 580: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');}
1.135 albertel 581: $bgcolor=$possiblecolors{'not_charged_try'};
582: $button=1;
583: } elsif ($award eq 'UNIT_NOTNEEDED') {
1.155 albertel 584: $message = &mt('Only a number required. Computer reads units of "[_1]".',&markup_unit($awardmsg,$target));
1.135 albertel 585: $bgcolor=$possiblecolors{'not_charged_try'};
586: $button=1;
587: } elsif ($award eq 'NO_UNIT') {
1.144 albertel 588: $message = &mt("Units required").'.';
1.135 albertel 589: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units')};
590: $bgcolor=$possiblecolors{'not_charged_try'};
591: $button=1;
1.153 albertel 592: } elsif ($award eq 'COMMA_FAIL') {
593: $message = &mt("Proper comma separation is required").'.';
594: $bgcolor=$possiblecolors{'not_charged_try'};
595: $button=1;
1.135 albertel 596: } elsif ($award eq 'BAD_FORMULA') {
597: $message = &mt("Unable to understand formula");
598: $bgcolor=$possiblecolors{'not_charged_try'};
599: $button=1;
600: } elsif ($award eq 'INCORRECT') {
1.144 albertel 601: $message = &mt("Incorrect").'.';
1.135 albertel 602: $bgcolor=$possiblecolors{'charged_try'};
603: $button=1;
604: } elsif ($award eq 'SUBMITTED') {
605: $message = &mt("Your submission has been recorded.");
606: $bgcolor=$possiblecolors{'correct'};
607: $button=1;
608: } elsif ($award eq 'DRAFT') {
1.144 albertel 609: $message = &mt("A draft copy has been saved.");
1.135 albertel 610: $bgcolor=$possiblecolors{'not_charged_try'};
611: $button=1;
612: } elsif ($award eq 'ASSIGNED_SCORE') {
1.144 albertel 613: $message = &mt("A score has been assigned.");
1.135 albertel 614: $bgcolor=$possiblecolors{'correct'};
615: $button=0;
1.144 albertel 616: } elsif ($award eq '') {
617: $bgcolor=$possiblecolors{'not_charged_try'};
618: $button=1;
1.135 albertel 619: } else {
620: $message = &mt("Unknown message").": $award";
621: $button=1;
622: }
623: if (lc($Apache::lonhomework::problemstatus) eq 'no' &&
624: $Apache::inputtags::status[-1] ne 'SHOW_ANSWER') {
625: $message = &mt("Answer Submitted: Your final submission will be graded after the due date.");
626: $bgcolor=$possiblecolors{'correct'};
627: $button=1;
628: }
1.148 albertel 629: if ($Apache::inputtags::status[-1] eq 'SHOW_ANSWER' &&
1.150 albertel 630: !$added_computer_text && $target ne 'tex') {
1.180 albertel 631: $message.= $computer;
1.148 albertel 632: $added_computer_text=1;
1.144 albertel 633: }
1.135 albertel 634: return ($button,$bgcolor,$message,$previousmsg);
1.12 albertel 635: }
636:
1.155 albertel 637: sub markup_unit {
638: my ($unit,$target)=@_;
639: if ($target eq 'tex') {
640: return '\texttt{'.&Apache::lonxml::latex_special_symbols($unit).'}';
641: } else {
642: return "<tt>".$unit."</tt>";
643: }
644: }
645:
1.88 albertel 646: sub removealldata {
1.87 albertel 647: my ($id)=@_;
648: foreach my $key (keys(%Apache::lonhomework::results)) {
649: if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
650: &Apache::lonxml::debug("Removing $key");
651: delete($Apache::lonhomework::results{$key});
652: }
653: }
654: }
655:
1.142 albertel 656: sub hidealldata {
657: my ($id)=@_;
658: foreach my $key (keys(%Apache::lonhomework::results)) {
659: if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
660: &Apache::lonxml::debug("Hidding $key");
661: my $newkey=$key;
662: $newkey=~s/^(resource\.\Q$id\E\.[^\.]+\.)(.*)$/${1}hidden${2}/;
663: $Apache::lonhomework::results{$newkey}=
664: $Apache::lonhomework::results{$key};
665: delete($Apache::lonhomework::results{$key});
666: }
667: }
668: }
669:
1.12 albertel 670: sub setgradedata {
1.136 albertel 671: my ($award,$msg,$id,$previously_used) = @_;
1.154 albertel 672: if ($Apache::lonhomework::scantronmode &&
1.165 albertel 673: &Apache::lonnet::validCODE($env{'form.CODE'})) {
674: $Apache::lonhomework::results{"resource.CODE"}=$env{'form.CODE'};
1.154 albertel 675: } elsif ($Apache::lonhomework::scantronmode &&
1.165 albertel 676: $env{'form.CODE'} eq '' &&
1.154 albertel 677: $Apache::lonhomework::history{"resource.CODE"} ne '') {
678: $Apache::lonhomework::results{"resource.CODE"}='';
1.141 albertel 679: }
1.154 albertel 680:
1.135 albertel 681: if (!$Apache::lonhomework::scantronmode &&
682: $Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
683: $Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER') {
684: $Apache::lonhomework::results{"resource.$id.afterduedate"}=$award;
1.87 albertel 685: return '';
1.135 albertel 686: } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} !~
687: /^correct/ || $Apache::lonhomework::scantronmode ||
688: lc($Apache::lonhomework::problemstatus) eq 'no') {
1.154 albertel 689: # the student doesn't already have it correct,
690: # or we are in a mode (scantron orno problem status) where a correct
691: # can become incorrect
692: # handle assignment of tries and solved status
1.135 albertel 693: my $solvemsg;
694: if ($Apache::lonhomework::scantronmode) {
695: $solvemsg='correct_by_scantron';
696: } else {
697: $solvemsg='correct_by_student';
698: }
699: if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
700: $Apache::lonhomework::results{"resource.$id.afterduedate"}='';
701: }
702: if ( $award eq 'ASSIGNED_SCORE') {
703: $Apache::lonhomework::results{"resource.$id.tries"} =
704: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
705: $Apache::lonhomework::results{"resource.$id.solved"} =
706: $solvemsg;
707: my $numawards=scalar(@Apache::inputtags::response);
708: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
709: foreach my $res (@Apache::inputtags::response) {
710: $Apache::lonhomework::results{"resource.$id.awarded"}+=
711: $Apache::lonhomework::results{"resource.$id.$res.awarded"};
712: }
713: if ($numawards > 0) {
714: $Apache::lonhomework::results{"resource.$id.awarded"}/=
715: $numawards;
716: }
717: } elsif ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
718: $Apache::lonhomework::results{"resource.$id.tries"} =
719: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
720: $Apache::lonhomework::results{"resource.$id.solved"} =
721: $solvemsg;
722: $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
723: } elsif ( $award eq 'INCORRECT' ) {
724: $Apache::lonhomework::results{"resource.$id.tries"} =
725: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
1.152 albertel 726: if (lc($Apache::lonhomework::problemstatus) eq 'no' ||
727: $Apache::lonhomework::scantronmode) {
1.135 albertel 728: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
729: }
730: $Apache::lonhomework::results{"resource.$id.solved"} =
731: 'incorrect_attempted';
732: } elsif ( $award eq 'SUBMITTED' ) {
733: $Apache::lonhomework::results{"resource.$id.tries"} =
734: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
735: $Apache::lonhomework::results{"resource.$id.solved"} =
736: 'ungraded_attempted';
737: } elsif ( $award eq 'DRAFT' ) {
738: $Apache::lonhomework::results{"resource.$id.solved"} = '';
739: } elsif ( $award eq 'NO_RESPONSE' ) {
740: #no real response so delete any data that got stored
1.129 albertel 741: &removealldata($id);
742: return '';
743: } else {
1.135 albertel 744: $Apache::lonhomework::results{"resource.$id.solved"} =
745: 'incorrect_attempted';
1.152 albertel 746: if (lc($Apache::lonhomework::problemstatus) eq 'no' ||
747: $Apache::lonhomework::scantronmode) {
1.135 albertel 748: $Apache::lonhomework::results{"resource.$id.tries"} =
749: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
750: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
751: }
752: }
1.136 albertel 753: if (defined($msg)) {
754: $Apache::lonhomework::results{"resource.$id.awardmsg"} = $msg;
755: }
1.135 albertel 756: # did either of the overall awards chage? If so ignore the
757: # previous check
758: if (($Apache::lonhomework::results{"resource.$id.awarded"} eq
759: $Apache::lonhomework::history{"resource.$id.awarded"}) &&
760: ($Apache::lonhomework::results{"resource.$id.solved"} eq
761: $Apache::lonhomework::history{"resource.$id.solved"})) {
762: # check if this was a previous submission if it was delete the
763: # unneeded data and update the previously_used attribute
764: if ( $previously_used eq 'PREVIOUSLY_USED') {
765: if (lc($Apache::lonhomework::problemstatus) ne 'no') {
766: delete($Apache::lonhomework::results{"resource.$id.tries"});
767: $Apache::lonhomework::results{"resource.$id.previous"} = '1';
768: }
769: } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
770: #delete all data as they student didn't do anything, but save
771: #the list of collaborators.
772: &removealldata($id);
773: #and since they didn't do anything we were never here
774: return '';
775: } else {
776: $Apache::lonhomework::results{"resource.$id.previous"} = '0';
777: }
1.101 albertel 778: }
1.135 albertel 779: } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} =~
780: /^correct/ ) {
781: #delete all data as they student already has it correct
782: &removealldata($id);
783: #and since they didn't do anything we were never here
784: return '';
1.40 albertel 785: }
1.135 albertel 786: $Apache::lonhomework::results{"resource.$id.award"} = $award;
1.10 albertel 787: }
788:
1.9 albertel 789: sub grade {
1.135 albertel 790: my ($target) = @_;
791: my $id = $Apache::inputtags::part;
792: my $response='';
1.165 albertel 793: if ( defined $env{'form.submitted'}) {
1.136 albertel 794: my (@awards,@msgs);
1.135 albertel 795: foreach $response (@Apache::inputtags::response) {
796: &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
797: my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
798: &Apache::lonxml::debug("keeping $value from $response for $id");
799: push (@awards,$value);
1.136 albertel 800: $value=$Apache::lonhomework::results{"resource.$id.$response.awardmsg"};
801: &Apache::lonxml::debug("got message $value from $response for $id");
802: push (@msgs,$value);
1.135 albertel 803: }
1.136 albertel 804: my ($finalaward,$msg) = &finalizeawards(\@awards,\@msgs);
1.135 albertel 805: my $previously_used;
806: if ( $#Apache::inputtags::previous eq $#awards ) {
807: my $match=0;
808: my @matches;
809: foreach my $versionar (@Apache::inputtags::previous_version) {
810: foreach my $version (@$versionar) {
811: $matches[$version]++;
812: }
813: }
814: foreach my $elem (@matches) {if ($elem eq ($#awards+1)) {$match=1;}}
815: if ($match) {
816: $previously_used = 'PREVIOUSLY_LAST';
817: foreach my $value (@Apache::inputtags::previous) {
818: if ($value eq 'PREVIOUSLY_USED' ) {
819: $previously_used = $value;
820: last;
821: }
1.75 albertel 822: }
823: }
1.43 albertel 824: }
1.136 albertel 825: &Apache::lonxml::debug("final award $finalaward, $previously_used, message $msg");
826: &setgradedata($finalaward,$msg,$id,$previously_used);
1.43 albertel 827: }
1.135 albertel 828: return '';
1.1 albertel 829: }
830:
1.11 albertel 831: sub gradestatus {
1.135 albertel 832: my ($id,$target) = @_;
833: my $showbutton = 1;
834: my $bgcolor = '';
835: my $message = '';
836: my $latemessage = '';
837: my $trystr='';
838: my $button='';
839: my $previousmsg='';
840:
841: my $status = $Apache::inputtags::status['-1'];
842: &Apache::lonxml::debug("gradestatus has :$status:");
843: if ( $status ne 'CLOSED' && $status ne 'UNAVAILABLE' &&
844: $status ne 'INVALID_ACCESS') {
845: my $award = $Apache::lonhomework::history{"resource.$id.award"};
1.169 albertel 846: my $awarded = $Apache::lonhomework::history{"resource.$id.awarded"};
1.135 albertel 847: my $solved = $Apache::lonhomework::history{"resource.$id.solved"};
848: my $previous = $Apache::lonhomework::history{"resource.$id.previous"};
1.136 albertel 849: my $awardmsg = $Apache::lonhomework::history{"resource.$id.awardmsg"};
850: &Apache::lonxml::debug("Found Award |$award|$solved|$awardmsg");
1.144 albertel 851: if ( $award ne '' || $solved ne '' || $status eq 'SHOW_ANSWER') {
1.135 albertel 852: &Apache::lonxml::debug('Getting message');
853: ($showbutton,$bgcolor,$message,$previousmsg) =
1.169 albertel 854: &decideoutput($award,$awarded,$awardmsg,$solved,$previous,
855: $target);
1.135 albertel 856: if ($target eq 'tex') {
857: $message='\vskip 2 mm '.$message.' ';
858: } else {
859: $message="<td bgcolor=\"$bgcolor\">$message</td>";
860: if ($previousmsg) {
861: $previousmsg="<td bgcolor=\"#aaaaff\">$previousmsg</td>";
862: }
863: }
864: }
865: my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
866: my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
867: &Apache::lonxml::debug("got maxtries of :$maxtries:");
868: #if tries are set to negative turn off the Tries/Button and messages
869: if (defined($maxtries) && $maxtries < 0) { return ''; }
870: if ( $tries eq '' ) { $tries = '0'; }
871: if ( $maxtries eq '' ) { $maxtries = '2'; }
872: if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; }
873: my $tries_text=&mt('Tries');
1.164 albertel 874: if ( $Apache::lonhomework::type eq 'survey' ||
875: $Apache::lonhomework::parsing_a_task) {
876: $tries_text=&mt('Submissions');
877: }
1.135 albertel 878: if ( $showbutton ) {
879: if ($target eq 'tex') {
1.165 albertel 880: if ($env{'request.state'} ne "construct" && $Apache::lonhomework::type ne 'exam' && $env{'form.suppress_tries'} ne 'yes') {
1.135 albertel 881: $trystr = ' {\vskip 1 mm \small \textit{'.$tries_text.'} '.$tries.'/'.$maxtries.'} \vskip 2 mm ';
882: } else {
883: $trystr = '\vskip 0 mm ';
884: }
885: } else {
1.136 albertel 886: $trystr = "<td><nobr>".$tries_text." $tries";
1.164 albertel 887: if ($Apache::lonhomework::parsing_a_task) {
1.165 albertel 888: } elsif($env{'request.state'} ne 'construct') {
1.135 albertel 889: $trystr.="/$maxtries";
890: } else {
891: if (defined($Apache::inputtags::params{'maxtries'})) {
892: $trystr.="/".$Apache::inputtags::params{'maxtries'};
893: }
894: }
1.136 albertel 895: $trystr.="</nobr></td>";
1.135 albertel 896: }
897: }
898: if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {$showbutton = 0;}
899: if ( $showbutton ) {
900: if ($target ne 'tex') {
1.156 albertel 901: $button = '<input type="submit" name="submit_'.$id.'" value="'.&mt('Submit Answer').'" />';
1.135 albertel 902: }
903: }
904: if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
905: #last submissions was after due date
1.163 albertel 906: $latemessage=&mt(' The last submission was after the Due Date ');;
907: if ($target eq 'web') {
908: $latemessage='<td bgcolor="#ffaaaa">'.$latemessage.'</td>';
1.135 albertel 909: }
910: }
911: }
912: my $output= $previousmsg.$latemessage.$message.$trystr;
913: if ($output =~ /^\s*$/) {
914: return $button;
1.63 sakharuk 915: } else {
1.135 albertel 916: if ($target eq 'tex') {
917: return $button.' \vskip 0 mm '.$output.' ';
918: } else {
919: return '<table><tr><td>'.$button.'</td>'.$output.'</tr></table>';
920: }
1.63 sakharuk 921: }
1.11 albertel 922: }
1.1 albertel 923: 1;
924: __END__
1.43 albertel 925:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>