Annotation of loncom/homework/inputtags.pm, revision 1.186
1.43 albertel 1: # The LearningOnline Network with CAPA
2: # input definitons
1.47 albertel 3: #
1.186 ! albertel 4: # $Id: inputtags.pm,v 1.185 2005/12/01 18:46:31 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 '') {
1.186 ! albertel 625: if ($handgrade && $Apache::inputtags::status[-1] eq 'SHOW_ANSWER') {
! 626: $message = &mt("Nothing submitted.");
! 627: $bgcolor=$possiblecolors{'charged_try'};
! 628: } else {
! 629: $bgcolor=$possiblecolors{'not_charged_try'};
! 630: }
1.144 albertel 631: $button=1;
1.135 albertel 632: } else {
633: $message = &mt("Unknown message").": $award";
634: $button=1;
635: }
636: if (lc($Apache::lonhomework::problemstatus) eq 'no' &&
637: $Apache::inputtags::status[-1] ne 'SHOW_ANSWER') {
638: $message = &mt("Answer Submitted: Your final submission will be graded after the due date.");
639: $bgcolor=$possiblecolors{'correct'};
640: $button=1;
641: }
1.148 albertel 642: if ($Apache::inputtags::status[-1] eq 'SHOW_ANSWER' &&
1.150 albertel 643: !$added_computer_text && $target ne 'tex') {
1.180 albertel 644: $message.= $computer;
1.148 albertel 645: $added_computer_text=1;
1.144 albertel 646: }
1.135 albertel 647: return ($button,$bgcolor,$message,$previousmsg);
1.12 albertel 648: }
649:
1.155 albertel 650: sub markup_unit {
651: my ($unit,$target)=@_;
652: if ($target eq 'tex') {
653: return '\texttt{'.&Apache::lonxml::latex_special_symbols($unit).'}';
654: } else {
655: return "<tt>".$unit."</tt>";
656: }
657: }
658:
1.88 albertel 659: sub removealldata {
1.87 albertel 660: my ($id)=@_;
661: foreach my $key (keys(%Apache::lonhomework::results)) {
662: if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
663: &Apache::lonxml::debug("Removing $key");
664: delete($Apache::lonhomework::results{$key});
665: }
666: }
667: }
668:
1.142 albertel 669: sub hidealldata {
670: my ($id)=@_;
671: foreach my $key (keys(%Apache::lonhomework::results)) {
672: if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
673: &Apache::lonxml::debug("Hidding $key");
674: my $newkey=$key;
675: $newkey=~s/^(resource\.\Q$id\E\.[^\.]+\.)(.*)$/${1}hidden${2}/;
676: $Apache::lonhomework::results{$newkey}=
677: $Apache::lonhomework::results{$key};
678: delete($Apache::lonhomework::results{$key});
679: }
680: }
681: }
682:
1.12 albertel 683: sub setgradedata {
1.136 albertel 684: my ($award,$msg,$id,$previously_used) = @_;
1.154 albertel 685: if ($Apache::lonhomework::scantronmode &&
1.165 albertel 686: &Apache::lonnet::validCODE($env{'form.CODE'})) {
687: $Apache::lonhomework::results{"resource.CODE"}=$env{'form.CODE'};
1.154 albertel 688: } elsif ($Apache::lonhomework::scantronmode &&
1.165 albertel 689: $env{'form.CODE'} eq '' &&
1.154 albertel 690: $Apache::lonhomework::history{"resource.CODE"} ne '') {
691: $Apache::lonhomework::results{"resource.CODE"}='';
1.141 albertel 692: }
1.154 albertel 693:
1.135 albertel 694: if (!$Apache::lonhomework::scantronmode &&
695: $Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
696: $Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER') {
697: $Apache::lonhomework::results{"resource.$id.afterduedate"}=$award;
1.87 albertel 698: return '';
1.135 albertel 699: } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} !~
700: /^correct/ || $Apache::lonhomework::scantronmode ||
701: lc($Apache::lonhomework::problemstatus) eq 'no') {
1.154 albertel 702: # the student doesn't already have it correct,
703: # or we are in a mode (scantron orno problem status) where a correct
704: # can become incorrect
705: # handle assignment of tries and solved status
1.135 albertel 706: my $solvemsg;
707: if ($Apache::lonhomework::scantronmode) {
708: $solvemsg='correct_by_scantron';
709: } else {
710: $solvemsg='correct_by_student';
711: }
712: if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
713: $Apache::lonhomework::results{"resource.$id.afterduedate"}='';
714: }
715: if ( $award eq 'ASSIGNED_SCORE') {
716: $Apache::lonhomework::results{"resource.$id.tries"} =
717: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
718: $Apache::lonhomework::results{"resource.$id.solved"} =
719: $solvemsg;
720: my $numawards=scalar(@Apache::inputtags::response);
721: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
722: foreach my $res (@Apache::inputtags::response) {
723: $Apache::lonhomework::results{"resource.$id.awarded"}+=
724: $Apache::lonhomework::results{"resource.$id.$res.awarded"};
725: }
726: if ($numawards > 0) {
727: $Apache::lonhomework::results{"resource.$id.awarded"}/=
728: $numawards;
729: }
730: } elsif ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
731: $Apache::lonhomework::results{"resource.$id.tries"} =
732: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
733: $Apache::lonhomework::results{"resource.$id.solved"} =
734: $solvemsg;
735: $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
736: } elsif ( $award eq 'INCORRECT' ) {
737: $Apache::lonhomework::results{"resource.$id.tries"} =
738: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
1.152 albertel 739: if (lc($Apache::lonhomework::problemstatus) eq 'no' ||
740: $Apache::lonhomework::scantronmode) {
1.135 albertel 741: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
742: }
743: $Apache::lonhomework::results{"resource.$id.solved"} =
744: 'incorrect_attempted';
745: } elsif ( $award eq 'SUBMITTED' ) {
746: $Apache::lonhomework::results{"resource.$id.tries"} =
747: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
748: $Apache::lonhomework::results{"resource.$id.solved"} =
749: 'ungraded_attempted';
750: } elsif ( $award eq 'DRAFT' ) {
751: $Apache::lonhomework::results{"resource.$id.solved"} = '';
752: } elsif ( $award eq 'NO_RESPONSE' ) {
753: #no real response so delete any data that got stored
1.129 albertel 754: &removealldata($id);
755: return '';
756: } else {
1.135 albertel 757: $Apache::lonhomework::results{"resource.$id.solved"} =
758: 'incorrect_attempted';
1.152 albertel 759: if (lc($Apache::lonhomework::problemstatus) eq 'no' ||
760: $Apache::lonhomework::scantronmode) {
1.135 albertel 761: $Apache::lonhomework::results{"resource.$id.tries"} =
762: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
763: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
764: }
765: }
1.136 albertel 766: if (defined($msg)) {
767: $Apache::lonhomework::results{"resource.$id.awardmsg"} = $msg;
768: }
1.135 albertel 769: # did either of the overall awards chage? If so ignore the
770: # previous check
771: if (($Apache::lonhomework::results{"resource.$id.awarded"} eq
772: $Apache::lonhomework::history{"resource.$id.awarded"}) &&
773: ($Apache::lonhomework::results{"resource.$id.solved"} eq
774: $Apache::lonhomework::history{"resource.$id.solved"})) {
775: # check if this was a previous submission if it was delete the
776: # unneeded data and update the previously_used attribute
777: if ( $previously_used eq 'PREVIOUSLY_USED') {
778: if (lc($Apache::lonhomework::problemstatus) ne 'no') {
779: delete($Apache::lonhomework::results{"resource.$id.tries"});
780: $Apache::lonhomework::results{"resource.$id.previous"} = '1';
781: }
782: } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
783: #delete all data as they student didn't do anything, but save
784: #the list of collaborators.
785: &removealldata($id);
786: #and since they didn't do anything we were never here
787: return '';
788: } else {
789: $Apache::lonhomework::results{"resource.$id.previous"} = '0';
790: }
1.101 albertel 791: }
1.135 albertel 792: } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} =~
793: /^correct/ ) {
794: #delete all data as they student already has it correct
795: &removealldata($id);
796: #and since they didn't do anything we were never here
797: return '';
1.40 albertel 798: }
1.135 albertel 799: $Apache::lonhomework::results{"resource.$id.award"} = $award;
1.184 albertel 800: if ($award eq 'SUBMITTED') {
801: &Apache::response::add_to_gradingqueue();
802: }
1.10 albertel 803: }
804:
1.9 albertel 805: sub grade {
1.135 albertel 806: my ($target) = @_;
807: my $id = $Apache::inputtags::part;
808: my $response='';
1.165 albertel 809: if ( defined $env{'form.submitted'}) {
1.136 albertel 810: my (@awards,@msgs);
1.135 albertel 811: foreach $response (@Apache::inputtags::response) {
812: &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
813: my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
814: &Apache::lonxml::debug("keeping $value from $response for $id");
815: push (@awards,$value);
1.136 albertel 816: $value=$Apache::lonhomework::results{"resource.$id.$response.awardmsg"};
817: &Apache::lonxml::debug("got message $value from $response for $id");
818: push (@msgs,$value);
1.135 albertel 819: }
1.136 albertel 820: my ($finalaward,$msg) = &finalizeawards(\@awards,\@msgs);
1.135 albertel 821: my $previously_used;
822: if ( $#Apache::inputtags::previous eq $#awards ) {
823: my $match=0;
824: my @matches;
825: foreach my $versionar (@Apache::inputtags::previous_version) {
826: foreach my $version (@$versionar) {
827: $matches[$version]++;
828: }
829: }
830: foreach my $elem (@matches) {if ($elem eq ($#awards+1)) {$match=1;}}
831: if ($match) {
832: $previously_used = 'PREVIOUSLY_LAST';
833: foreach my $value (@Apache::inputtags::previous) {
834: if ($value eq 'PREVIOUSLY_USED' ) {
835: $previously_used = $value;
836: last;
837: }
1.75 albertel 838: }
839: }
1.43 albertel 840: }
1.136 albertel 841: &Apache::lonxml::debug("final award $finalaward, $previously_used, message $msg");
842: &setgradedata($finalaward,$msg,$id,$previously_used);
1.43 albertel 843: }
1.135 albertel 844: return '';
1.1 albertel 845: }
846:
1.11 albertel 847: sub gradestatus {
1.135 albertel 848: my ($id,$target) = @_;
849: my $showbutton = 1;
850: my $bgcolor = '';
851: my $message = '';
852: my $latemessage = '';
853: my $trystr='';
854: my $button='';
855: my $previousmsg='';
856:
857: my $status = $Apache::inputtags::status['-1'];
858: &Apache::lonxml::debug("gradestatus has :$status:");
1.183 albertel 859: if ( $status ne 'CLOSED'
860: && $status ne 'UNAVAILABLE'
861: && $status ne 'INVALID_ACCESS'
862: && $status ne 'NEEDS_CHECKIN'
863: && $status ne 'NOT_IN_A_SLOT') {
1.135 albertel 864: my $award = $Apache::lonhomework::history{"resource.$id.award"};
1.169 albertel 865: my $awarded = $Apache::lonhomework::history{"resource.$id.awarded"};
1.135 albertel 866: my $solved = $Apache::lonhomework::history{"resource.$id.solved"};
867: my $previous = $Apache::lonhomework::history{"resource.$id.previous"};
1.136 albertel 868: my $awardmsg = $Apache::lonhomework::history{"resource.$id.awardmsg"};
869: &Apache::lonxml::debug("Found Award |$award|$solved|$awardmsg");
1.144 albertel 870: if ( $award ne '' || $solved ne '' || $status eq 'SHOW_ANSWER') {
1.135 albertel 871: &Apache::lonxml::debug('Getting message');
872: ($showbutton,$bgcolor,$message,$previousmsg) =
1.169 albertel 873: &decideoutput($award,$awarded,$awardmsg,$solved,$previous,
874: $target);
1.135 albertel 875: if ($target eq 'tex') {
876: $message='\vskip 2 mm '.$message.' ';
877: } else {
878: $message="<td bgcolor=\"$bgcolor\">$message</td>";
879: if ($previousmsg) {
880: $previousmsg="<td bgcolor=\"#aaaaff\">$previousmsg</td>";
881: }
882: }
883: }
884: my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
885: my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
886: &Apache::lonxml::debug("got maxtries of :$maxtries:");
887: #if tries are set to negative turn off the Tries/Button and messages
888: if (defined($maxtries) && $maxtries < 0) { return ''; }
889: if ( $tries eq '' ) { $tries = '0'; }
890: if ( $maxtries eq '' ) { $maxtries = '2'; }
891: if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; }
892: my $tries_text=&mt('Tries');
1.164 albertel 893: if ( $Apache::lonhomework::type eq 'survey' ||
894: $Apache::lonhomework::parsing_a_task) {
895: $tries_text=&mt('Submissions');
896: }
1.135 albertel 897: if ( $showbutton ) {
898: if ($target eq 'tex') {
1.165 albertel 899: if ($env{'request.state'} ne "construct" && $Apache::lonhomework::type ne 'exam' && $env{'form.suppress_tries'} ne 'yes') {
1.135 albertel 900: $trystr = ' {\vskip 1 mm \small \textit{'.$tries_text.'} '.$tries.'/'.$maxtries.'} \vskip 2 mm ';
901: } else {
902: $trystr = '\vskip 0 mm ';
903: }
904: } else {
1.136 albertel 905: $trystr = "<td><nobr>".$tries_text." $tries";
1.164 albertel 906: if ($Apache::lonhomework::parsing_a_task) {
1.165 albertel 907: } elsif($env{'request.state'} ne 'construct') {
1.135 albertel 908: $trystr.="/$maxtries";
909: } else {
910: if (defined($Apache::inputtags::params{'maxtries'})) {
911: $trystr.="/".$Apache::inputtags::params{'maxtries'};
912: }
913: }
1.136 albertel 914: $trystr.="</nobr></td>";
1.135 albertel 915: }
916: }
917: if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {$showbutton = 0;}
918: if ( $showbutton ) {
919: if ($target ne 'tex') {
1.156 albertel 920: $button = '<input type="submit" name="submit_'.$id.'" value="'.&mt('Submit Answer').'" />';
1.135 albertel 921: }
922: }
923: if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
924: #last submissions was after due date
1.163 albertel 925: $latemessage=&mt(' The last submission was after the Due Date ');;
926: if ($target eq 'web') {
927: $latemessage='<td bgcolor="#ffaaaa">'.$latemessage.'</td>';
1.135 albertel 928: }
929: }
930: }
931: my $output= $previousmsg.$latemessage.$message.$trystr;
932: if ($output =~ /^\s*$/) {
933: return $button;
1.63 sakharuk 934: } else {
1.135 albertel 935: if ($target eq 'tex') {
936: return $button.' \vskip 0 mm '.$output.' ';
937: } else {
938: return '<table><tr><td>'.$button.'</td>'.$output.'</tr></table>';
939: }
1.63 sakharuk 940: }
1.11 albertel 941: }
1.1 albertel 942: 1;
943: __END__
1.43 albertel 944:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>