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