Annotation of loncom/homework/inputtags.pm, revision 1.240
1.43 albertel 1: # The LearningOnline Network with CAPA
2: # input definitons
1.47 albertel 3: #
1.240 ! www 4: # $Id: inputtags.pm,v 1.239 2008/03/03 11:22:54 bisitz 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.192 www 34: use LONCAPA;
35:
1.1 albertel 36:
1.50 harris41 37: BEGIN {
1.135 albertel 38: &Apache::lonxml::register('Apache::inputtags',('hiddenline','textfield','textline'));
1.1 albertel 39: }
40:
1.177 foxr 41: # Initializes a set of global variables used during the parse of the problem.
42: #
1.178 albertel 43: # @Apache::inputtags::input - List of current input ids.
44: # @Apache::inputtags::inputlist - List of all input ids seen this problem.
45: # @Apache::inputtags::response - List of all current resopnse ids.
46: # @Apache::inputtags::responselist - List of all response ids seen this
47: # problem.
48: # @Apache::inputtags::hint - List of all hint ids.
49: # @Apache::inputtags::hintlist - List of all hint ids seen this problem.
50: # @Apache::inputtags::previous - List describing if specific responseds
51: # have been used
52: # @Apache::inputtags::previous_version - Submission responses were used in.
53: # $Apache::inputtags::part - Current part id (valid only in
54: # <problem>)
55: # 0 if not in a part.
56: # @Apache::inputtags::partlist - List of part ids seen in the current
57: # <problem>
58: # @Apache::inputtags::status - List of problem statuses. First
59: # element is the status of the <problem>
60: # the remainder are for individual <part>s.
61: # %Apache::inputtags::params - Hash of defined parameters for the
62: # current response.
63: # @Apache::inputtags::import - List of all ids for <import> thes get
64: # join()ed and prepended.
65: # @Apache::inputtags::importlist - List of all import ids seen.
66: # $Apache::inputtags::response_with_no_part
67: # - Flag set true if we have seen a response
68: # that is not inside a <part>
69: # %Apache::inputtags::answertxt - <*response> tags store correct
70: # answer strings for display by <textline/>
71: # in this hash.
1.217 albertel 72: # %Apache::inputtags::submission_display
73: # - <*response> tags store improved display
74: # of submission strings for display by part
75: # end.
1.178 albertel 76:
1.1 albertel 77: sub initialize_inputtags {
1.135 albertel 78: @Apache::inputtags::input=();
79: @Apache::inputtags::inputlist=();
1.174 albertel 80: @Apache::inputtags::response=();
1.135 albertel 81: @Apache::inputtags::responselist=();
1.174 albertel 82: @Apache::inputtags::hint=();
1.173 albertel 83: @Apache::inputtags::hintlist=();
1.135 albertel 84: @Apache::inputtags::previous=();
85: @Apache::inputtags::previous_version=();
86: $Apache::inputtags::part='';
87: @Apache::inputtags::partlist=();
88: @Apache::inputtags::status=();
89: %Apache::inputtags::params=();
90: @Apache::inputtags::import=();
91: @Apache::inputtags::importlist=();
92: $Apache::inputtags::response_with_no_part=0;
1.144 albertel 93: %Apache::inputtags::answertxt=();
1.217 albertel 94: %Apache::inputtags::submission_display=();
1.103 albertel 95: }
96:
97: sub check_for_duplicate_ids {
98: my %check;
99: foreach my $id (@Apache::inputtags::partlist,
100: @Apache::inputtags::responselist,
1.173 albertel 101: @Apache::inputtags::hintlist,
1.103 albertel 102: @Apache::inputtags::importlist) {
103: $check{$id}++;
104: }
105: my @duplicates;
106: foreach my $id (sort(keys(%check))) {
107: if ($check{$id} > 1) {
108: push(@duplicates,$id);
109: }
110: }
111: if (@duplicates) {
112: &Apache::lonxml::error("Duplicated ids found, problem will operate incorrectly. Duplicated ids seen: ",join(', ',@duplicates));
113: }
1.1 albertel 114: }
115:
1.14 albertel 116: sub start_input {
1.135 albertel 117: my ($parstack,$safeeval)=@_;
1.228 albertel 118: my $id = &Apache::lonxml::get_id($parstack,$safeeval);
1.135 albertel 119: push (@Apache::inputtags::input,$id);
120: push (@Apache::inputtags::inputlist,$id);
121: return $id;
1.14 albertel 122: }
123:
124: sub end_input {
1.135 albertel 125: pop @Apache::inputtags::input;
126: return '';
1.14 albertel 127: }
128:
1.124 www 129: sub addchars {
130: my ($fieldid,$addchars)=@_;
131: my $output='';
132: foreach (split(/\,/,$addchars)) {
133: $output.='<a href="javascript:void(document.forms.lonhomework.'.
134: $fieldid.'.value+=\''.$_.'\')">'.$_.'</a> ';
135: }
136: return $output;
137: }
138:
1.48 albertel 139: sub start_textfield {
1.185 albertel 140: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.135 albertel 141: my $result = "";
142: my $id = &start_input($parstack,$safeeval);
143: my $resid=$Apache::inputtags::response[-1];
144: if ($target eq 'web') {
145: $Apache::lonxml::evaluate--;
1.205 albertel 146: my $partid=$Apache::inputtags::part;
147: my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$resid.submission"},'<>&"');
1.135 albertel 148: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
149: my $cols = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
150: if ( $cols eq '') { $cols = 80; }
151: my $rows = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
1.143 www 152: if ( $rows eq '') { $rows = 16; }
1.135 albertel 153: my $addchars=&Apache::lonxml::get_param('addchars',$parstack,$safeeval);
154: $result='';
155: if ($addchars) {
156: $result.=&addchars('HWVAL_'.$resid,$addchars);
157: }
1.191 albertel 158: &Apache::lonhtmlcommon::add_htmlareafields('HWVAL_'.$resid);
1.143 www 159: $result.= '<textarea wrap="hard" name="HWVAL_'.$resid.'" id="HWVAL_'.$resid.'" '.
1.135 albertel 160: "rows=\"$rows\" cols=\"$cols\">".$oldresponse;
161: if ($oldresponse ne '') {
1.143 www 162:
1.135 albertel 163: #get rid of any startup text if the user has already responded
1.185 albertel 164: &Apache::lonxml::get_all_text("/textfield",$parser,$style);
1.135 albertel 165: }
166: } else {
1.205 albertel 167: #show past answer in the essayresponse case
168: if ($oldresponse =~ /\S/
169: && &Apache::londefdef::is_inside_of($tagstack,
170: 'essayresponse') ) {
171: $result='<table class="LC_pastsubmission"><tr><td>'.
172: $oldresponse.'</td></tr></table>';
173: }
1.135 albertel 174: #get rid of any startup text
1.185 albertel 175: &Apache::lonxml::get_all_text("/textfield",$parser,$style);
1.61 albertel 176: }
1.135 albertel 177: } elsif ($target eq 'grade') {
1.185 albertel 178: my $seedtext=&Apache::lonxml::get_all_text("/textfield",$parser,
179: $style);
1.165 albertel 180: if ($seedtext eq $env{'form.HWVAL_'.$resid}) {
1.135 albertel 181: # if the seed text is still there it wasn't a real submission
1.165 albertel 182: $env{'form.HWVAL_'.$resid}='';
1.135 albertel 183: }
184: } elsif ($target eq 'edit') {
185: $result.=&Apache::edit::tag_start($target,$token);
186: $result.=&Apache::edit::text_arg('Rows:','rows',$token,4);
187: $result.=&Apache::edit::text_arg('Columns:','cols',$token,4);
188: $result.=&Apache::edit::text_arg
189: ('Click-On Texts (comma sep):','addchars',$token,10);
1.185 albertel 190: my $bodytext=&Apache::lonxml::get_all_text("/textfield",$parser,
191: $style);
1.135 albertel 192: $result.=&Apache::edit::editfield($token->[1],$bodytext,'Text you want to appear by default:',80,2);
193: } elsif ($target eq 'modified') {
194: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
195: $safeeval,'rows','cols',
196: 'addchars');
197: if ($constructtag) {
198: $result = &Apache::edit::rebuild_tag($token);
199: } else {
200: $result=$token->[4];
201: }
202: $result.=&Apache::edit::modifiedfield("/textfield",$parser);
203: } elsif ($target eq 'tex') {
204: my $number_of_lines = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
205: my $width_of_box = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
206: if ($$tagstack[-2] eq 'essayresponse' and $Apache::lonhomework::type eq 'exam') {
207: $result = '\fbox{\fbox{\parbox{\textwidth-5mm}{';
208: for (my $i=0;$i<int $number_of_lines*2;$i++) {$result.='\strut \\\\ ';}
209: $result.='\strut \\\\\strut \\\\\strut \\\\\strut \\\\}}}';
210: } else {
211: my $TeXwidth=$width_of_box/80;
212: $result = '\vskip 1 mm \fbox{\fbox{\parbox{'.$TeXwidth.'\textwidth-5mm}{';
213: for (my $i=0;$i<int $number_of_lines*2;$i++) {$result.='\strut \\\\ ';}
214: $result.='}}}\vskip 2 mm ';
215: }
1.60 albertel 216: }
1.135 albertel 217: return $result;
1.6 albertel 218: }
219:
1.48 albertel 220: sub end_textfield {
1.135 albertel 221: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
222: my $result;
223: if ($target eq 'web') {
224: $Apache::lonxml::evaluate++;
225: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
226: return "</textarea>";
227: }
228: } elsif ($target eq 'edit') {
229: $result=&Apache::edit::end_table();
230: }
231: &end_input;
232: return $result;
1.6 albertel 233: }
234:
1.190 albertel 235: sub exam_score_line {
1.188 albertel 236: my ($target) = @_;
1.190 albertel 237:
1.188 albertel 238: my $result;
239: if ($target eq 'tex') {
240: my $repetition = &Apache::response::repetition();
241: $result.='\begin{enumerate}';
1.190 albertel 242: if ($env{'request.state'} eq "construct" ) {$result.='\item[\strut]';}
1.188 albertel 243: foreach my $i (0..$repetition-1) {
244: $result.='\item[\textbf{'.
245: ($Apache::lonxml::counter+$i).
246: '}.]\textit{Leave blank on scoring form}\vskip 0 mm';
247: }
248: $result.= '\end{enumerate}';
1.190 albertel 249: }
250:
251: return $result;
252: }
253:
254: sub exam_box {
255: my ($target) = @_;
256: my $result;
1.188 albertel 257:
1.190 albertel 258: if ($target eq 'tex') {
259: $result .= '\fbox{\fbox{\parbox{\textwidth-5mm}{\strut\\\\\strut\\\\\strut\\\\\strut\\\\}}}';
260: $result .= &exam_score_line($target);
1.188 albertel 261: } elsif ($target eq 'web') {
262: my $id=$Apache::inputtags::response[-1];
263: $result.= '<br /><br />
264: <textarea name="HWVAL_'.$id.'" rows="4" cols="50">
265: </textarea> <br /><br />';
266: }
267: return $result;
268: }
269:
270: sub needs_exam_box {
271: my ($tagstack) = @_;
272: my @tags = ('formularesponse',
273: 'stringresponse',
274: 'reactionresponse',
275: 'organicresponse',
276: );
277:
278: foreach my $tag (@tags) {
279: if (grep(/\Q$tag\E/,@$tagstack)) {
280: return 1;
281: }
282: }
283: return 0;
284: }
285:
1.1 albertel 286: sub start_textline {
1.135 albertel 287: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
288: my $result = "";
1.210 albertel 289: my $input_id = &start_input($parstack,$safeeval);
1.135 albertel 290: if ($target eq 'web') {
291: $Apache::lonxml::evaluate--;
292: my $partid=$Apache::inputtags::part;
293: my $id=$Apache::inputtags::response[-1];
1.204 albertel 294: if (!&Apache::response::show_answer()) {
1.135 albertel 295: my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
296: my $maxlength;
297: if ($size eq '') { $size=20; } else {
1.214 albertel 298: if ($size < 20) {
299: $maxlength = ' maxlength="'.$size.'"';
300: }
1.135 albertel 301: }
1.210 albertel 302: my $oldresponse = $Apache::lonhomework::history{"resource.$partid.$id.submission"};
303: &Apache::lonxml::debug("oldresponse $oldresponse is ".ref($oldresponse));
304:
305: if (ref($oldresponse) eq 'ARRAY') {
306: $oldresponse = $oldresponse->[$#Apache::inputtags::inputlist];
307: }
308: $oldresponse = &HTML::Entities::encode($oldresponse,'<>&"');
309:
1.135 albertel 310: if ($Apache::lonhomework::type ne 'exam') {
311: my $addchars=&Apache::lonxml::get_param('addchars',$parstack,$safeeval);
312: $result='';
313: if ($addchars) {
314: $result.=&addchars('HWVAL_'.$id,$addchars);
315: }
1.157 albertel 316: my $readonly=&Apache::lonxml::get_param('readonly',$parstack,
317: $safeeval);
1.193 albertel 318: if (lc($readonly) eq 'yes'
319: || $Apache::inputtags::status[-1] eq 'CANNOT_ANSWER') {
1.157 albertel 320: $readonly=' readonly="readonly" ';
1.158 albertel 321: } else {
322: $readonly='';
1.157 albertel 323: }
1.193 albertel 324: my $name = 'HWVAL_'.$id;
325: if ($Apache::inputtags::status[-1] eq 'CANNOT_ANSWER') {
326: $name = "none";
327: }
1.214 albertel 328: $result.= '<input onkeydown="javascript:setSubmittedPart(\''.$partid.'\');" type="text" '.$readonly.' name="'.$name.'" value="'.
329: $oldresponse.'" size="'.$size.'"'.$maxlength.' />';
1.135 albertel 330: }
1.188 albertel 331: if ($Apache::lonhomework::type eq 'exam'
332: && &needs_exam_box($tagstack)) {
333: $result.=&exam_box($target);
334: }
1.135 albertel 335: } else {
336: #right or wrong don't show what was last typed in.
1.208 albertel 337: my $count = scalar(@Apache::inputtags::inputlist)-1;
338: $result='<b>'.$Apache::inputtags::answertxt{$id}[$count].'</b>';
1.144 albertel 339: #$result='';
1.135 albertel 340: }
341: } elsif ($target eq 'edit') {
342: $result=&Apache::edit::tag_start($target,$token);
343: $result.=&Apache::edit::text_arg('Size:','size',$token,'5').
1.157 albertel 344: &Apache::edit::text_arg('Click-On Texts (comma sep):',
345: 'addchars',$token,10);
346: $result.=&Apache::edit::select_arg('Readonly:','readonly',
347: ['no','yes'],$token);
348: $result.=&Apache::edit::end_row();
349: $result.=&Apache::edit::end_table();
1.135 albertel 350: } elsif ($target eq 'modified') {
1.157 albertel 351: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
352: $safeeval,'size',
353: 'addchars','readonly');
1.135 albertel 354: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.188 albertel 355: } elsif ($target eq 'tex'
356: && $Apache::lonhomework::type ne 'exam') {
1.135 albertel 357: my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
358: if ($size != 0) {$size=$size*2; $size.=' mm';} else {$size='40 mm';}
359: $result='\framebox['.$size.'][s]{\tiny\strut}';
1.188 albertel 360:
361: } elsif ($target eq 'tex'
362: && $Apache::lonhomework::type eq 'exam'
363: && &needs_exam_box($tagstack)) {
364: $result.=&exam_box($target);
1.135 albertel 365: }
366: return $result;
1.1 albertel 367: }
368:
369: sub end_textline {
1.135 albertel 370: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
371: if ($target eq 'web') { $Apache::lonxml::evaluate++; }
372: elsif ($target eq 'edit') { return ('','no'); }
1.208 albertel 373: &end_input();
1.135 albertel 374: return "";
1.9 albertel 375: }
376:
1.98 albertel 377: sub start_hiddenline {
378: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
379: my $result = "";
1.211 albertel 380: my $input_id = &start_input($parstack,$safeeval);
1.98 albertel 381: if ($target eq 'web') {
382: $Apache::lonxml::evaluate--;
383: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
384: my $partid=$Apache::inputtags::part;
385: my $id=$Apache::inputtags::response[-1];
1.211 albertel 386: my $oldresponse = $Apache::lonhomework::history{"resource.$partid.$id.submission"};
387: if (ref($oldresponse) eq 'ARRAY') {
388: $oldresponse = $oldresponse->[$#Apache::inputtags::inputlist];
389: }
390: $oldresponse = &HTML::Entities::encode($oldresponse,'<>&"');
391:
1.98 albertel 392: if ($Apache::lonhomework::type ne 'exam') {
393: $result= '<input type="hidden" name="HWVAL_'.$id.'" value="'.
394: $oldresponse.'" />';
395: }
396: }
397: } elsif ($target eq 'edit') {
398: $result=&Apache::edit::tag_start($target,$token);
399: $result.=&Apache::edit::end_table;
400: }
1.189 albertel 401:
402: if ( ($target eq 'web' || $target eq 'tex')
403: && $Apache::lonhomework::type eq 'exam'
404: && &needs_exam_box($tagstack)) {
405: $result.=&exam_box($target);
406: }
1.98 albertel 407: return $result;
408: }
409:
410: sub end_hiddenline {
1.135 albertel 411: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
412: if ($target eq 'web') { $Apache::lonxml::evaluate++; }
413: elsif ($target eq 'edit') { return ('','no'); }
1.211 albertel 414: &end_input();
1.135 albertel 415: return "";
1.98 albertel 416: }
417:
1.160 albertel 418: # $part -> partid
419: # $id -> responseid
420: # $uploadefiletypes -> comma seperated list of extensions allowed or * for any
421: # $which -> 'uploadedonly' -> only newly uploaded files
422: # 'portfolioonly' -> only allow files from portfolio
423: # 'both' -> allow files from either location
1.175 albertel 424: # $extratext -> additional text to go between the link and the input box
1.160 albertel 425: # returns a table row <tr>
426: sub file_selector {
1.175 albertel 427: my ($part,$id,$uploadedfiletypes,$which,$extratext)=@_;
1.160 albertel 428: if (!$uploadedfiletypes) { return ''; }
1.167 albertel 429:
430: my $jspart=$part;
431: $jspart=~s/\./_/g;
432:
1.160 albertel 433: my $result;
434:
1.162 albertel 435: $result.='<tr><td>';
436: if ($uploadedfiletypes ne '*') {
437: $result.=
438: &mt('Allowed filetypes: <b>[_1]</b>',$uploadedfiletypes).'<br />';
439: }
1.160 albertel 440: if ($which eq 'uploadonly' || $which eq 'both') {
441: $result.=&mt('Submit a file: (only one file can be uploaded)').
442: ' <br /><input type="file" size="50" name="HWFILE'.
1.167 albertel 443: $jspart.'_'.$id.'" /><br />';
1.205 albertel 444: $result .= &show_past_file_submission($part,$id);
1.160 albertel 445: }
446: if ( $which eq 'both') {
447: $result.='<br />'.'<strong>'.&mt('OR:').'</strong><br />';
448: }
449: if ($which eq 'portfolioonly' || $which eq 'both') {
1.227 albertel 450: $result.=$extratext.'<a href='."'".'javascript:void(window.open("/adm/portfolio?mode=selectfile&fieldname='.$env{'form.request.prefix'}.'HWPORT'.$jspart.'_'.$id.'","cat","height=600,width=800,scrollbars=1,resizable=1,menubar=2,location=1"))'."'".'>'.
1.160 albertel 451: &mt('Select Portfolio Files').'</a><br />'.
1.167 albertel 452: '<input type="text" size="50" name="HWPORT'.$jspart.'_'.$id.'" value="" />'.
1.160 albertel 453: '<br />';
1.205 albertel 454: $result .= &show_past_portfile_submission($part,$id);
455:
1.160 albertel 456: }
457: $result.='</td></tr>';
458: return $result;
459: }
460:
1.205 albertel 461: sub show_past_file_submission {
462: my ($part,$id) = @_;
463: my $uploadedfile= &HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.uploadedfile"},'<>&"');
464:
465: return if (!$uploadedfile);
466:
467: my $url=$Apache::lonhomework::history{"resource.$part.$id.uploadedurl"};
468: &Apache::lonxml::extlink($url);
469: &Apache::lonnet::allowuploaded('/adm/essayresponse',$url);
470: my $icon=&Apache::loncommon::icon($url);
471: my $curfile='<a href="'.$url.'"><img src="'.$icon.
472: '" border="0" />'.$uploadedfile.'</a>';
473: return &mt('Currently submitted: <tt>[_1]</tt>',$curfile);
474:
475: }
476:
477: sub show_past_portfile_submission {
478: my ($part,$id) = @_;
479: if ($Apache::lonhomework::history{"resource.$part.$id.portfiles"}!~/[^\s]/){
480: return;
481: }
482: my (@file_list,@bad_file_list);
483: foreach my $file (split(/\s*,\s*/,&unescape($Apache::lonhomework::history{"resource.$part.$id.portfiles"}))) {
1.209 albertel 484: my (undef,undef,$domain,$user)=&Apache::lonnet::whichuser();
1.205 albertel 485: my $url="/uploaded/$domain/$user/portfolio$file";
486: my $icon=&Apache::loncommon::icon($url);
487: push(@file_list,'<a href="'.$url.'"><img src="'.$icon.
488: '" border="0" />'.$file.'</a>');
489: if (! &Apache::lonnet::stat_file($url)) {
490: &Apache::lonnet::logthis("bad file is $url");
491: push(@bad_file_list,'<a href="'.$url.'"><img src="'.$icon.
492: '" border="0" />'.$file.'</a>');
493: }
494: }
495: my $files = '<span class="LC_filename">'.
496: join('</span>, <span class="LC_filename">',@file_list).
497: '</span>';
498: my $result = &mt("Portfolio files previously selected: [_1]",$files);
499: if (@bad_file_list) {
500: my $bad_files = '<span class="LC_filename">'.
501: join('</span>, <span class="LC_filename">',@bad_file_list).
502: '</span>';
503: $result.='<br />'.&mt('<span class="LC_error">These file(s) don\'t exist:</span> [_1]',$bad_files);
504: }
505: return $result;
506:
507: }
508:
1.179 albertel 509: sub valid_award {
510: my ($award) =@_;
1.182 albertel 511: foreach my $possibleaward ('EXTRA_ANSWER','MISSING_ANSWER', 'ERROR',
512: 'NO_RESPONSE',
1.179 albertel 513: 'TOO_LONG', 'UNIT_INVALID_INSTRUCTOR',
514: 'UNIT_INVALID_STUDENT', 'UNIT_IRRECONCIBLE',
515: 'UNIT_FAIL', 'NO_UNIT',
516: 'UNIT_NOTNEEDED', 'WANTED_NUMERIC',
517: 'BAD_FORMULA', 'SIG_FAIL', 'INCORRECT',
518: 'MISORDERED_RANK', 'INVALID_FILETYPE',
519: 'DRAFT', 'SUBMITTED', 'ASSIGNED_SCORE',
520: 'APPROX_ANS', 'EXACT_ANS','COMMA_FAIL') {
521: if ($award eq $possibleaward) { return 1; }
522: }
523: return 0;
524: }
525:
1.207 albertel 526: {
527: my @awards = ('EXTRA_ANSWER', 'MISSING_ANSWER', 'ERROR', 'NO_RESPONSE',
528: 'TOO_LONG',
529: 'UNIT_INVALID_INSTRUCTOR', 'UNIT_INVALID_STUDENT',
530: 'UNIT_IRRECONCIBLE', 'UNIT_FAIL', 'NO_UNIT',
531: 'UNIT_NOTNEEDED', 'WANTED_NUMERIC', 'BAD_FORMULA',
532: 'COMMA_FAIL', 'SIG_FAIL', 'INCORRECT', 'MISORDERED_RANK',
533: 'INVALID_FILETYPE', 'DRAFT', 'SUBMITTED', 'ASSIGNED_SCORE',
534: 'APPROX_ANS', 'EXACT_ANS');
535: my $i=0;
536: my %fwd_awards = map { ($_,$i++) } @awards;
537: my $max=scalar(@awards);
538: @awards=reverse(@awards);
1.208 albertel 539: $i=0;
1.207 albertel 540: my %rev_awards = map { ($_,$i++) } @awards;
541:
1.232 albertel 542: sub awarddetail_to_awarded {
543: my ($awarddetail) = @_;
544: if ($awarddetail eq 'EXACT_ANS'
545: || $awarddetail eq 'APPROX_ANS') {
546: return 1;
547: }
548: return 0;
549: }
550:
1.233 albertel 551: sub hide_award {
552: my ($award) = @_;
553: if (&Apache::lonhomework::show_no_problem_status()) {
554: return 1;
555: }
556: if ($award =~
557: /^(?:EXACT_ANS|APPROX_ANS|SUBMITTED|ASSIGNED_SCORE|INCORRECT)/) {
558: return 1;
559: }
560: return 0;
561: }
562:
1.9 albertel 563: sub finalizeawards {
1.232 albertel 564: my ($awardref,$msgref,$nameref,$reverse,$final_scantron)=@_;
1.207 albertel 565: my $result;
1.136 albertel 566: if ($#$awardref == -1) { $result = "NO_RESPONSE"; }
1.135 albertel 567: if ($result eq '' ) {
568: my $blankcount;
1.207 albertel 569: foreach my $award (@$awardref) {
1.135 albertel 570: if ($award eq '') {
571: $result='MISSING_ANSWER';
572: $blankcount++;
573: }
574: }
1.232 albertel 575: if ($blankcount == ($#$awardref + 1)) {
576: return ('NO_RESPONSE');
577: }
1.135 albertel 578: }
1.232 albertel 579: if (!$final_scantron && defined($result)) { return ($result); }
1.181 albertel 580:
1.232 albertel 581: # if in scantron mode, if the award for any response is
582: # assigned score, then the part gets an assigned score
583: if ($final_scantron
584: && grep {$_ eq 'ASSIGNED_SCORE'} (@$awardref)) {
585: return ('ASSIGNED_SCORE');
586: }
587:
588: # if in scantron mode, if the award for any response is
589: # correct and there are non-correct responses,
590: # then the part gets an assigned score
591: if ($final_scantron
592: && (grep { $_ eq 'EXACT_ANS' ||
593: $_ eq 'APPROX_ANS' } (@$awardref))
594: && (grep { $_ ne 'EXACT_ANS' &&
595: $_ ne 'APPROX_ANS' } (@$awardref))) {
596: return ('ASSIGNED_SCORE');
597: }
1.181 albertel 598: # these awards are ordered from most important error through best correct
1.207 albertel 599: my $awards = (!$reverse) ? \%fwd_awards : \%rev_awards ;
600:
601: my $best = $max;
602: my $j=0;
603: my $which;
604: foreach my $award (@$awardref) {
605: if ($awards->{$award} < $best) {
606: $best = $awards->{$award};
607: $which = $j;
608: }
609: $j++;
610: }
1.232 albertel 611:
1.207 albertel 612: if (defined($which)) {
613: if (ref($nameref)) {
614: return ($$awardref[$which],$$msgref[$which],$$nameref[$which]);
615: } else {
616: return ($$awardref[$which],$$msgref[$which]);
617: }
1.135 albertel 618: }
1.136 albertel 619: return ('ERROR',undef);
1.9 albertel 620: }
1.207 albertel 621: }
1.9 albertel 622:
1.10 albertel 623: sub decideoutput {
1.169 albertel 624: my ($award,$awarded,$awardmsg,$solved,$previous,$target)=@_;
1.135 albertel 625: my $message='';
626: my $button=0;
627: my $previousmsg;
1.221 albertel 628: my $css_class='orange';
1.148 albertel 629: my $added_computer_text=0;
1.221 albertel 630: my %possible_class =
631: ( 'correct' => 'LC_answer_correct',
632: 'charged_try' => 'LC_answer_charged_try',
633: 'not_charged_try' => 'LC_answer_not_charged_try',
634: 'no_grade' => 'LC_answer_no_grade',
635: 'no_message' => 'LC_no_message',
1.135 albertel 636: );
1.169 albertel 637:
1.180 albertel 638: my $part = $Apache::inputtags::part;
1.236 raeburn 639: my $tohandgrade = &Apache::lonnet::EXT("resource.$part.handgrade");
640: my $handgrade = ('yes' eq lc($tohandgrade));
1.180 albertel 641:
642: my $computer = ($handgrade)? ''
1.203 www 643: : " ".&mt("Computer's answer now shown above.");
1.180 albertel 644: &Apache::lonxml::debug("handgrade has :$handgrade:");
645:
1.135 albertel 646: if ($previous) { $previousmsg=&mt('You have entered that answer before'); }
647:
1.194 banghart 648: if ($solved =~ /^correct/) {
1.221 albertel 649: $css_class=$possible_class{'correct'};
1.170 albertel 650: $message=&mt('You are correct.');
651: if ($awarded < 1 && $awarded > 0) {
652: $message=&mt('You are partially correct.');
1.221 albertel 653: $css_class=$possible_class{'not_charged_try'};
1.170 albertel 654: } elsif ($awarded < 1) {
655: $message=&mt('Incorrect.');
1.221 albertel 656: $css_class=$possible_class{'charged_try'};
1.170 albertel 657: }
1.172 albertel 658: if ($env{'request.filename'} =~
659: m|/res/lib/templates/examupload.problem$|) {
660: $message = &mt("A score has been assigned.");
661: $added_computer_text=1;
1.135 albertel 662: } else {
1.172 albertel 663: if ($target eq 'tex') {
664: $message = '\textbf{'.$message.'}';
665: } else {
666: $message = "<b>".$message."</b>";
1.180 albertel 667: $message.= $computer;
1.135 albertel 668: }
1.172 albertel 669: $added_computer_text=1;
1.235 albertel 670: if ($awarded > 0) {
671: my ($symb) = &Apache::lonnet::whichuser();
672: if (($symb ne '')
673: &&
674: ($env{'course.'.$env{'request.course.id'}.
1.237 www 675: '.disable_receipt_display'} ne 'yes') &&
676: ($Apache::lonhomework::type ne 'practice')) {
1.235 albertel 677: $message.=(($target eq 'web')?'<br />':' ').
678: &mt('Your receipt is [_1]',
679: (&Apache::lonnet::receipt($Apache::inputtags::part).
680: (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'')));
681: }
1.135 albertel 682: }
683: }
684: $button=0;
685: $previousmsg='';
686: } elsif ($solved =~ /^excused/) {
687: if ($target eq 'tex') {
688: $message = ' \textbf{'.&mt('You are excused from the problem.').'} ';
689: } else {
690: $message = "<b>".&mt('You are excused from the problem.')."</b>";
691: }
1.221 albertel 692: $css_class=$possible_class{'charged_try'};
1.135 albertel 693: $button=0;
694: $previousmsg='';
695: } elsif ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
696: if ($solved =~ /^incorrect/ || $solved eq '') {
1.144 albertel 697: $message = &mt("Incorrect").".";
1.221 albertel 698: $css_class=$possible_class{'charged_try'};
1.135 albertel 699: $button=1;
700: } else {
1.144 albertel 701: if ($target eq 'tex') {
702: $message = '\textbf{'.&mt('You are correct.').'}';
703: } else {
704: $message = "<b>".&mt('You are correct.')."</b>";
1.180 albertel 705: $message.= $computer;
1.144 albertel 706: }
1.148 albertel 707: $added_computer_text=1;
1.235 albertel 708: if ($awarded > 0
709: && $env{'course.'.
1.165 albertel 710: $env{'request.course.id'}.
1.235 albertel 711: '.disable_receipt_display'} ne 'yes') {
1.135 albertel 712: $message.=(($target eq 'web')?'<br />':' ').
1.235 albertel 713: &mt('Your receipt is [_1]',
714: (&Apache::lonnet::receipt($Apache::inputtags::part).
715: (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'')));
1.135 albertel 716: }
1.221 albertel 717: $css_class=$possible_class{'correct'};
1.135 albertel 718: $button=0;
719: $previousmsg='';
720: }
721: } elsif ($award eq 'NO_RESPONSE') {
722: $message = '';
1.221 albertel 723: $css_class=$possible_class{'no_feedback'};
1.135 albertel 724: $button=1;
1.182 albertel 725: } elsif ($award eq 'EXTRA_ANSWER') {
726: $message = &mt('Some extra items were submitted.');
1.221 albertel 727: $css_class=$possible_class{'not_charged_try'};
1.182 albertel 728: $button = 1;
1.135 albertel 729: } elsif ($award eq 'MISSING_ANSWER') {
730: $message = &mt('Some items were not submitted.');
1.221 albertel 731: $css_class=$possible_class{'not_charged_try'};
1.135 albertel 732: $button = 1;
733: } elsif ($award eq 'ERROR') {
734: $message = &mt('An error occured while grading your answer.');
1.221 albertel 735: $css_class=$possible_class{'not_charged_try'};
1.135 albertel 736: $button = 1;
737: } elsif ($award eq 'TOO_LONG') {
738: $message = &mt("The submitted answer was too long.");
1.221 albertel 739: $css_class=$possible_class{'not_charged_try'};
1.135 albertel 740: $button=1;
741: } elsif ($award eq 'WANTED_NUMERIC') {
742: $message = &mt("This question expects a numeric answer.");
1.221 albertel 743: $css_class=$possible_class{'not_charged_try'};
1.135 albertel 744: $button=1;
745: } elsif ($award eq 'MISORDERED_RANK') {
746: $message = &mt('You have provided an invalid ranking');
747: if ($target ne 'tex') {
1.239 bisitz 748: $message.=', '.&mt('please refer to').' '.&Apache::loncommon::help_open_topic('Ranking_Problems',&mt('help on ranking problems'));
1.135 albertel 749: }
1.221 albertel 750: $css_class=$possible_class{'not_charged_try'};
1.135 albertel 751: $button=1;
752: } elsif ($award eq 'INVALID_FILETYPE') {
1.166 albertel 753: $message = &mt('Submission won\'t be graded. The type of file submitted is not allowed.');
1.221 albertel 754: $css_class=$possible_class{'not_charged_try'};
1.135 albertel 755: $button=1;
756: } elsif ($award eq 'SIG_FAIL') {
1.145 albertel 757: my ($used,$min,$max)=split(':',$awardmsg);
1.212 albertel 758: my $word = ($used < $min) ? 'more' : 'fewer';
759: $message = &mt("Submission not graded. Use $word digits.",$used);
1.221 albertel 760: $css_class=$possible_class{'not_charged_try'};
1.135 albertel 761: $button=1;
1.137 albertel 762: } elsif ($award eq 'UNIT_INVALID_INSTRUCTOR') {
763: $message = &mt('Error in instructor specifed unit. This error has been reported to the instructor.', $awardmsg);
764: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');}
1.221 albertel 765: $css_class=$possible_class{'not_charged_try'};
1.137 albertel 766: $button=1;
767: } elsif ($award eq 'UNIT_INVALID_STUDENT') {
1.155 albertel 768: $message = &mt('Unable to interpret units. Computer reads units as "[_1]".',&markup_unit($awardmsg,$target));
1.137 albertel 769: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');}
1.221 albertel 770: $css_class=$possible_class{'not_charged_try'};
1.137 albertel 771: $button=1;
1.140 matthew 772: } elsif ($award eq 'UNIT_FAIL' || $award eq 'UNIT_IRRECONCIBLE') {
1.155 albertel 773: $message = &mt('Incompatible units. No conversion found between "[_1]" and the required units.',&markup_unit($awardmsg,$target));
1.136 albertel 774: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');}
1.221 albertel 775: $css_class=$possible_class{'not_charged_try'};
1.135 albertel 776: $button=1;
777: } elsif ($award eq 'UNIT_NOTNEEDED') {
1.155 albertel 778: $message = &mt('Only a number required. Computer reads units of "[_1]".',&markup_unit($awardmsg,$target));
1.221 albertel 779: $css_class=$possible_class{'not_charged_try'};
1.135 albertel 780: $button=1;
781: } elsif ($award eq 'NO_UNIT') {
1.144 albertel 782: $message = &mt("Units required").'.';
1.135 albertel 783: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units')};
1.221 albertel 784: $css_class=$possible_class{'not_charged_try'};
1.135 albertel 785: $button=1;
1.153 albertel 786: } elsif ($award eq 'COMMA_FAIL') {
787: $message = &mt("Proper comma separation is required").'.';
1.221 albertel 788: $css_class=$possible_class{'not_charged_try'};
1.153 albertel 789: $button=1;
1.135 albertel 790: } elsif ($award eq 'BAD_FORMULA') {
1.240 ! www 791: $message = &mt("Unable to understand formula").'.';
! 792: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Formula_Answers')};
1.221 albertel 793: $css_class=$possible_class{'not_charged_try'};
1.135 albertel 794: $button=1;
795: } elsif ($award eq 'INCORRECT') {
1.144 albertel 796: $message = &mt("Incorrect").'.';
1.221 albertel 797: $css_class=$possible_class{'charged_try'};
1.135 albertel 798: $button=1;
799: } elsif ($award eq 'SUBMITTED') {
800: $message = &mt("Your submission has been recorded.");
1.221 albertel 801: $css_class=$possible_class{'no_grade'};
1.135 albertel 802: $button=1;
803: } elsif ($award eq 'DRAFT') {
1.144 albertel 804: $message = &mt("A draft copy has been saved.");
1.221 albertel 805: $css_class=$possible_class{'not_charged_try'};
1.135 albertel 806: $button=1;
807: } elsif ($award eq 'ASSIGNED_SCORE') {
1.144 albertel 808: $message = &mt("A score has been assigned.");
1.221 albertel 809: $css_class=$possible_class{'correct'};
1.135 albertel 810: $button=0;
1.144 albertel 811: } elsif ($award eq '') {
1.186 albertel 812: if ($handgrade && $Apache::inputtags::status[-1] eq 'SHOW_ANSWER') {
813: $message = &mt("Nothing submitted.");
1.221 albertel 814: $css_class=$possible_class{'charged_try'};
1.186 albertel 815: } else {
1.221 albertel 816: $css_class=$possible_class{'not_charged_try'};
1.186 albertel 817: }
1.144 albertel 818: $button=1;
1.135 albertel 819: } else {
820: $message = &mt("Unknown message").": $award";
821: $button=1;
822: }
1.209 albertel 823: my (undef,undef,$domain,$user)=&Apache::lonnet::whichuser();
1.194 banghart 824: foreach my $resid(@Apache::inputtags::response){
825: if ($Apache::lonhomework::history{"resource.$part.$resid.handback"}) {
1.198 albertel 826: $message.='<br />';
827: my @files = split(/\s*,\s*/,
828: $Apache::lonhomework::history{"resource.$part.$resid.handback"});
829: my $file_msg;
830: foreach my $file (@files) {
831: $file_msg.= '<br /><a href="/uploaded/'."$domain/$user".'/'.$file.'">'.$file.'</a>';
832: }
833: $message .= &mt('Returned file(s): [_1]',$file_msg);
834: }
1.194 banghart 835: }
836:
1.233 albertel 837: if (&Apache::lonhomework::hide_problem_status()
838: && $Apache::inputtags::status[-1] ne 'SHOW_ANSWER'
839: && &hide_award($award)) {
1.135 albertel 840: $message = &mt("Answer Submitted: Your final submission will be graded after the due date.");
1.221 albertel 841: $css_class=$possible_class{'no_grade'};
1.135 albertel 842: $button=1;
843: }
1.148 albertel 844: if ($Apache::inputtags::status[-1] eq 'SHOW_ANSWER' &&
1.150 albertel 845: !$added_computer_text && $target ne 'tex') {
1.180 albertel 846: $message.= $computer;
1.148 albertel 847: $added_computer_text=1;
1.144 albertel 848: }
1.237 www 849: if ($Apache::lonhomework::type eq 'practice') {
850: $message.='<br />'.&mt('Submissions to practice problems are not permanently recorded.');
851: }
852:
1.221 albertel 853: return ($button,$css_class,$message,$previousmsg);
1.12 albertel 854: }
855:
1.155 albertel 856: sub markup_unit {
857: my ($unit,$target)=@_;
858: if ($target eq 'tex') {
859: return '\texttt{'.&Apache::lonxml::latex_special_symbols($unit).'}';
860: } else {
861: return "<tt>".$unit."</tt>";
862: }
863: }
864:
1.88 albertel 865: sub removealldata {
1.87 albertel 866: my ($id)=@_;
867: foreach my $key (keys(%Apache::lonhomework::results)) {
868: if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
869: &Apache::lonxml::debug("Removing $key");
870: delete($Apache::lonhomework::results{$key});
871: }
872: }
873: }
874:
1.142 albertel 875: sub hidealldata {
876: my ($id)=@_;
877: foreach my $key (keys(%Apache::lonhomework::results)) {
878: if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
879: &Apache::lonxml::debug("Hidding $key");
880: my $newkey=$key;
881: $newkey=~s/^(resource\.\Q$id\E\.[^\.]+\.)(.*)$/${1}hidden${2}/;
882: $Apache::lonhomework::results{$newkey}=
883: $Apache::lonhomework::results{$key};
884: delete($Apache::lonhomework::results{$key});
885: }
886: }
887: }
888:
1.12 albertel 889: sub setgradedata {
1.136 albertel 890: my ($award,$msg,$id,$previously_used) = @_;
1.154 albertel 891: if ($Apache::lonhomework::scantronmode &&
1.165 albertel 892: &Apache::lonnet::validCODE($env{'form.CODE'})) {
893: $Apache::lonhomework::results{"resource.CODE"}=$env{'form.CODE'};
1.154 albertel 894: } elsif ($Apache::lonhomework::scantronmode &&
1.165 albertel 895: $env{'form.CODE'} eq '' &&
1.154 albertel 896: $Apache::lonhomework::history{"resource.CODE"} ne '') {
897: $Apache::lonhomework::results{"resource.CODE"}='';
1.141 albertel 898: }
1.154 albertel 899:
1.135 albertel 900: if (!$Apache::lonhomework::scantronmode &&
901: $Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
902: $Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER') {
903: $Apache::lonhomework::results{"resource.$id.afterduedate"}=$award;
1.87 albertel 904: return '';
1.135 albertel 905: } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} !~
1.233 albertel 906: /^correct/
907: || $Apache::lonhomework::scantronmode
908: || &Apache::lonhomework::hide_problem_status() ) {
1.154 albertel 909: # the student doesn't already have it correct,
910: # or we are in a mode (scantron orno problem status) where a correct
911: # can become incorrect
912: # handle assignment of tries and solved status
1.135 albertel 913: my $solvemsg;
914: if ($Apache::lonhomework::scantronmode) {
915: $solvemsg='correct_by_scantron';
916: } else {
917: $solvemsg='correct_by_student';
918: }
919: if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
920: $Apache::lonhomework::results{"resource.$id.afterduedate"}='';
921: }
922: if ( $award eq 'ASSIGNED_SCORE') {
923: $Apache::lonhomework::results{"resource.$id.tries"} =
924: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
925: $Apache::lonhomework::results{"resource.$id.solved"} =
926: $solvemsg;
927: my $numawards=scalar(@Apache::inputtags::response);
928: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
929: foreach my $res (@Apache::inputtags::response) {
1.232 albertel 930: if (defined($Apache::lonhomework::results{"resource.$id.$res.awarded"})) {
931: $Apache::lonhomework::results{"resource.$id.awarded"}+=
932: $Apache::lonhomework::results{"resource.$id.$res.awarded"};
933: } else {
934: $Apache::lonhomework::results{"resource.$id.awarded"}+=
935: &awarddetail_to_awarded($Apache::lonhomework::results{"resource.$id.$res.awarddetail"});
936: }
1.135 albertel 937: }
938: if ($numawards > 0) {
939: $Apache::lonhomework::results{"resource.$id.awarded"}/=
940: $numawards;
941: }
942: } elsif ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
943: $Apache::lonhomework::results{"resource.$id.tries"} =
944: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
945: $Apache::lonhomework::results{"resource.$id.solved"} =
946: $solvemsg;
947: $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
948: } elsif ( $award eq 'INCORRECT' ) {
949: $Apache::lonhomework::results{"resource.$id.tries"} =
950: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
1.233 albertel 951: if (&Apache::lonhomework::hide_problem_status()
952: || $Apache::lonhomework::scantronmode) {
1.135 albertel 953: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
954: }
955: $Apache::lonhomework::results{"resource.$id.solved"} =
956: 'incorrect_attempted';
957: } elsif ( $award eq 'SUBMITTED' ) {
958: $Apache::lonhomework::results{"resource.$id.tries"} =
959: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
960: $Apache::lonhomework::results{"resource.$id.solved"} =
961: 'ungraded_attempted';
962: } elsif ( $award eq 'DRAFT' ) {
963: $Apache::lonhomework::results{"resource.$id.solved"} = '';
964: } elsif ( $award eq 'NO_RESPONSE' ) {
965: #no real response so delete any data that got stored
1.129 albertel 966: &removealldata($id);
967: return '';
968: } else {
1.135 albertel 969: $Apache::lonhomework::results{"resource.$id.solved"} =
970: 'incorrect_attempted';
1.233 albertel 971: if (&Apache::lonhomework::show_no_problem_status()
972: || $Apache::lonhomework::scantronmode) {
1.135 albertel 973: $Apache::lonhomework::results{"resource.$id.tries"} =
974: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
975: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
976: }
1.233 albertel 977:
978: if (&Apache::lonhomework::show_some_problem_status()) {
979: # clear out the awarded if they had gotten it wrong/right
980: # and are now in an error mode
981: $Apache::lonhomework::results{"resource.$id.awarded"} = '';
982: }
1.135 albertel 983: }
1.136 albertel 984: if (defined($msg)) {
985: $Apache::lonhomework::results{"resource.$id.awardmsg"} = $msg;
986: }
1.135 albertel 987: # did either of the overall awards chage? If so ignore the
988: # previous check
989: if (($Apache::lonhomework::results{"resource.$id.awarded"} eq
990: $Apache::lonhomework::history{"resource.$id.awarded"}) &&
991: ($Apache::lonhomework::results{"resource.$id.solved"} eq
992: $Apache::lonhomework::history{"resource.$id.solved"})) {
993: # check if this was a previous submission if it was delete the
994: # unneeded data and update the previously_used attribute
995: if ( $previously_used eq 'PREVIOUSLY_USED') {
1.233 albertel 996: if (&Apache::lonhomework::show_problem_status()) {
1.135 albertel 997: delete($Apache::lonhomework::results{"resource.$id.tries"});
998: $Apache::lonhomework::results{"resource.$id.previous"} = '1';
999: }
1000: } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
1001: #delete all data as they student didn't do anything, but save
1002: #the list of collaborators.
1003: &removealldata($id);
1004: #and since they didn't do anything we were never here
1005: return '';
1006: } else {
1007: $Apache::lonhomework::results{"resource.$id.previous"} = '0';
1008: }
1.101 albertel 1009: }
1.135 albertel 1010: } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} =~
1011: /^correct/ ) {
1012: #delete all data as they student already has it correct
1013: &removealldata($id);
1014: #and since they didn't do anything we were never here
1015: return '';
1.40 albertel 1016: }
1.135 albertel 1017: $Apache::lonhomework::results{"resource.$id.award"} = $award;
1.184 albertel 1018: if ($award eq 'SUBMITTED') {
1019: &Apache::response::add_to_gradingqueue();
1020: }
1.10 albertel 1021: }
1022:
1.219 albertel 1023: sub find_which_previous {
1024: my ($version) = @_;
1025: my $part = $Apache::inputtags::part;
1026: my (@previous_version);
1027: foreach my $resp (@Apache::inputtags::response) {
1028: my $key = "$version:resource.$part.$resp.submission";
1029: my $submission = $Apache::lonhomework::history{$key};
1030: my %previous = &Apache::response::check_for_previous($submission,
1031: $part,$resp,
1032: $version);
1033: push(@previous_version,$previous{'version'});
1034: }
1035: return &previous_match(\@previous_version,
1036: scalar(@Apache::inputtags::response));
1037: }
1038:
1039: sub previous_match {
1040: my ($previous_array,$count) = @_;
1041: my $match = 0;
1042: my @matches;
1043: foreach my $versionar (@$previous_array) {
1044: foreach my $version (@$versionar) {
1045: $matches[$version]++;
1046: }
1047: }
1048: my $which=0;
1049: foreach my $elem (@matches) {
1050: if ($elem eq $count) {
1051: $match=1;
1052: last;
1053: }
1054: $which++;
1055: }
1056: return ($match,$which);
1057: }
1058:
1.9 albertel 1059: sub grade {
1.135 albertel 1060: my ($target) = @_;
1061: my $id = $Apache::inputtags::part;
1062: my $response='';
1.165 albertel 1063: if ( defined $env{'form.submitted'}) {
1.136 albertel 1064: my (@awards,@msgs);
1.135 albertel 1065: foreach $response (@Apache::inputtags::response) {
1066: &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
1067: my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
1068: &Apache::lonxml::debug("keeping $value from $response for $id");
1069: push (@awards,$value);
1.136 albertel 1070: $value=$Apache::lonhomework::results{"resource.$id.$response.awardmsg"};
1071: &Apache::lonxml::debug("got message $value from $response for $id");
1072: push (@msgs,$value);
1.135 albertel 1073: }
1.232 albertel 1074: my ($finalaward,$msg) =
1075: &finalizeawards(\@awards,\@msgs,undef,undef,
1076: $Apache::lonhomework::scantronmode);
1.135 albertel 1077: my $previously_used;
1078: if ( $#Apache::inputtags::previous eq $#awards ) {
1.219 albertel 1079: my ($match) =
1080: &previous_match(\@Apache::inputtags::previous_version,
1081: scalar(@Apache::inputtags::response));
1082:
1.135 albertel 1083: if ($match) {
1084: $previously_used = 'PREVIOUSLY_LAST';
1085: foreach my $value (@Apache::inputtags::previous) {
1086: if ($value eq 'PREVIOUSLY_USED' ) {
1087: $previously_used = $value;
1088: last;
1089: }
1.75 albertel 1090: }
1091: }
1.43 albertel 1092: }
1.136 albertel 1093: &Apache::lonxml::debug("final award $finalaward, $previously_used, message $msg");
1094: &setgradedata($finalaward,$msg,$id,$previously_used);
1.43 albertel 1095: }
1.135 albertel 1096: return '';
1.1 albertel 1097: }
1098:
1.217 albertel 1099: sub get_grade_messages {
1100: my ($id,$prefix,$target,$status) = @_;
1101:
1102: my ($message,$latemessage,$trystr,$previousmsg);
1103: my $showbutton = 1;
1104:
1105: my $award = $Apache::lonhomework::history{"$prefix.award"};
1106: my $awarded = $Apache::lonhomework::history{"$prefix.awarded"};
1107: my $solved = $Apache::lonhomework::history{"$prefix.solved"};
1108: my $previous = $Apache::lonhomework::history{"$prefix.previous"};
1109: my $awardmsg = $Apache::lonhomework::history{"$prefix.awardmsg"};
1110: &Apache::lonxml::debug("Found Award |$award|$solved|$awardmsg");
1111: if ( $award ne '' || $solved ne '' || $status eq 'SHOW_ANSWER') {
1112: &Apache::lonxml::debug('Getting message');
1.221 albertel 1113: ($showbutton,my $css_class,$message,$previousmsg) =
1.217 albertel 1114: &decideoutput($award,$awarded,$awardmsg,$solved,$previous,
1115: $target);
1116: if ($target eq 'tex') {
1117: $message='\vskip 2 mm '.$message.' ';
1118: } else {
1.221 albertel 1119: $message="<td class=\"$css_class\">$message</td>";
1.217 albertel 1120: if ($previousmsg) {
1.221 albertel 1121: $previousmsg="<td class=\"LC_answer_previous\">$previousmsg</td>";
1.217 albertel 1122: }
1123: }
1124: }
1125: my $tries = $Apache::lonhomework::history{"$prefix.tries"};
1126: my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
1127: &Apache::lonxml::debug("got maxtries of :$maxtries:");
1128: #if tries are set to negative turn off the Tries/Button and messages
1129: if (defined($maxtries) && $maxtries < 0) { return ''; }
1130: if ( $tries eq '' ) { $tries = '0'; }
1131: if ( $maxtries eq '' ) { $maxtries = '2'; }
1132: if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; }
1133: my $tries_text=&mt('Tries');
1134: if ( $Apache::lonhomework::type eq 'survey' ||
1135: $Apache::lonhomework::parsing_a_task) {
1136: $tries_text=&mt('Submissions');
1137: }
1138:
1139: if ($showbutton) {
1140: if ($target eq 'tex') {
1141: if ($env{'request.state'} ne "construct"
1142: && $Apache::lonhomework::type ne 'exam'
1143: && $env{'form.suppress_tries'} ne 'yes') {
1144: $trystr = ' {\vskip 1 mm \small \textit{'.$tries_text.'} '.
1145: $tries.'/'.$maxtries.'} \vskip 2 mm ';
1146: } else {
1147: $trystr = '\vskip 0 mm ';
1148: }
1149: } else {
1150: $trystr = "<td><nobr>".$tries_text." $tries";
1151: if ($Apache::lonhomework::parsing_a_task) {
1152: } elsif($env{'request.state'} ne 'construct') {
1153: $trystr.="/$maxtries";
1154: } else {
1155: if (defined($Apache::inputtags::params{'maxtries'})) {
1156: $trystr.="/".$Apache::inputtags::params{'maxtries'};
1157: }
1158: }
1159: $trystr.="</nobr></td>";
1160: }
1161: }
1.221 albertel 1162:
1.217 albertel 1163: if ($Apache::lonhomework::history{"$prefix.afterduedate"}) {
1164: #last submissions was after due date
1165: $latemessage=&mt(' The last submission was after the Due Date ');;
1166: if ($target eq 'web') {
1.221 albertel 1167: $latemessage='<td class="LC_answer_late">'.$latemessage.'</td>';
1.217 albertel 1168: }
1169: }
1170: return ($previousmsg,$latemessage,$message,$trystr,$showbutton);
1171: }
1172:
1.11 albertel 1173: sub gradestatus {
1.223 albertel 1174: my ($id,$target,$no_previous) = @_;
1.135 albertel 1175: my $showbutton = 1;
1176: my $message = '';
1177: my $latemessage = '';
1178: my $trystr='';
1179: my $button='';
1180: my $previousmsg='';
1181:
1182: my $status = $Apache::inputtags::status['-1'];
1183: &Apache::lonxml::debug("gradestatus has :$status:");
1.183 albertel 1184: if ( $status ne 'CLOSED'
1185: && $status ne 'UNAVAILABLE'
1186: && $status ne 'INVALID_ACCESS'
1187: && $status ne 'NEEDS_CHECKIN'
1188: && $status ne 'NOT_IN_A_SLOT') {
1.217 albertel 1189:
1190: ($previousmsg,$latemessage,$message,$trystr) =
1191: &get_grade_messages($id,"resource.$id",$target,$status,
1192: $showbutton);
1193: if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {
1194: $showbutton = 0;
1.164 albertel 1195: }
1.218 albertel 1196: if ( $status eq 'SHOW_ANSWER') {
1197: undef($previousmsg);
1198: }
1.135 albertel 1199: if ( $showbutton ) {
1200: if ($target ne 'tex') {
1.230 albertel 1201: $button =
1202: '<input
1203: onmouseup="javascript:setSubmittedPart(\''.$id.'\')"
1204: onsubmit="javascript:setSubmittedPart(\''.$id.'\')"
1205: type="submit" name="submit_'.$id.'"
1206: value="'.&mt('Submit Answer').'" />';
1.135 albertel 1207: }
1208: }
1.217 albertel 1209:
1.135 albertel 1210: }
1211: my $output= $previousmsg.$latemessage.$message.$trystr;
1212: if ($output =~ /^\s*$/) {
1213: return $button;
1.63 sakharuk 1214: } else {
1.135 albertel 1215: if ($target eq 'tex') {
1216: return $button.' \vskip 0 mm '.$output.' ';
1217: } else {
1.223 albertel 1218: $output =
1219: '<table><tr><td>'.$button.'</td>'.$output;
1220: if (!$no_previous) {
1221: $output.='<td>'.&previous_tries($id,$target).'</td>';
1222: }
1223: $output.= '</tr></table>';
1224: return $output;
1.135 albertel 1225: }
1.63 sakharuk 1226: }
1.11 albertel 1227: }
1.217 albertel 1228:
1229: sub previous_tries {
1230: my ($id,$target) = @_;
1231: my $output;
1232: my $status = $Apache::inputtags::status['-1'];
1.219 albertel 1233:
1234: my $count;
1235: my %count_lookup;
1236:
1.217 albertel 1237: foreach my $i (1..$Apache::lonhomework::history{'version'}) {
1238: my $prefix = $i.":resource.$id";
1239:
1240: next if (!exists($Apache::lonhomework::history{"$prefix.award"}));
1.219 albertel 1241: $count++;
1242: $count_lookup{$i} = $count;
1243:
1.217 albertel 1244: my ($previousmsg,$latemessage,$message,$trystr);
1245:
1246: ($previousmsg,$latemessage,$message,$trystr) =
1247: &get_grade_messages($id,"$prefix",$target,$status);
1248:
1.219 albertel 1249: if ($previousmsg ne '') {
1250: my ($match,$which) = &find_which_previous($i);
1251: $message=$previousmsg;
1252: my $previous = $count_lookup{$which};
1.226 albertel 1253: $message =~ s{(</td>)}{ as submission \# $previous $1};
1.221 albertel 1254: } elsif ($Apache::lonhomework::history{"$prefix.tries"}) {
1.233 albertel 1255: if (!(&Apache::lonhomework::hide_problem_status()
1.225 albertel 1256: && $Apache::inputtags::status[-1] ne 'SHOW_ANSWER')
1257: && $Apache::lonhomework::history{"$prefix.solved"} =~/^correct/
1258: ) {
1259:
1.238 bisitz 1260: my $txt_correct = &mt('Correct');
1.221 albertel 1261: $message =~ s{(<td.*?>)(.*?)(</td>)}
1.238 bisitz 1262: {$1 <strong>$txt_correct</strong>. $3}s;
1.221 albertel 1263: }
1.238 bisitz 1264: my $trystr = "(".&mt('Try [_1]',$Apache::lonhomework::history{"$prefix.tries"}).")";
1.221 albertel 1265: $message =~ s{(</td>)}{ $trystr $1};
1.219 albertel 1266: }
1.221 albertel 1267: my ($class) = ($message =~ m{<td.*class="([^"]*)"}); #"
1268: $message =~ s{(<td.*?>)}{<td>};
1269:
1.219 albertel 1270:
1.221 albertel 1271: $output.='<tr class="'.$class.'">';
1.223 albertel 1272: $output.='<td align="center">'.$count.'</td>';
1.219 albertel 1273: $output.=$message;
1.217 albertel 1274:
1275: foreach my $resid (@Apache::inputtags::response) {
1276: my $prefix = $prefix.".$resid";
1277: if (exists($Apache::lonhomework::history{"$prefix.submission"})) {
1278: my $submission =
1279: $Apache::inputtags::submission_display{"$prefix.submission"};
1280: if (!defined($submission)) {
1281: $submission =
1282: $Apache::lonhomework::history{"$prefix.submission"};
1283: }
1284: $output.='<td>'.$submission.'</td>';
1285: } else {
1286: $output.='<td></td>';
1287: }
1288: }
1.221 albertel 1289: $output.=&Apache::loncommon::end_data_table_row()."\n";
1.217 albertel 1290: }
1291: return if ($output eq '');
1.219 albertel 1292: my $headers =
1.222 albertel 1293: '<tr>'.'<th>'.&mt('Submission #').'</th><th>'.&mt('Try').
1.219 albertel 1294: '</th><th colspan="'.scalar(@Apache::inputtags::response).'">'.
1295: &mt('Submitted Answer').'</th>';
1296: $output ='<table class="LC_prior_tries">'.$headers.$output.'</table>';
1.217 albertel 1297: #return $output;
1.229 albertel 1298: $output = &Apache::loncommon::js_ready($output);
1.226 albertel 1299: $output.='<br /><form action=""><center><input type="button" name="close" value="'.&mt('Close Window').'" onClick="window.close()" /></center></form>';
1300:
1.217 albertel 1301: my $windowopen=&Apache::lonhtmlcommon::javascript_docopen();
1302: my $start_page =
1303: &Apache::loncommon::start_page('Previous Tries', undef,
1.229 albertel 1304: {'only_body' => 1,
1305: 'bgcolor' => '#FFFFFF',
1306: 'js_ready' => 1,
1307: 'inherit_jsmath' => 1, });
1.217 albertel 1308: my $end_page =
1309: &Apache::loncommon::end_page({'js_ready' => 1,});
1.231 albertel 1310: my $prefix = $env{'form.request.prefix'};
1311: $prefix =~ tr{.}{_};
1312: my $function_name = "LONCAPA_previous_tries_".$prefix.
1.234 albertel 1313: $Apache::lonxml::curdepth.'_'.$env{'form.counter'};
1.217 albertel 1314: my $result ="<script type=\"text/javascript\">
1315: // <![CDATA[
1.231 albertel 1316: function $function_name() {newWindow=open('','new_W','width=500,height=500,scrollbars=1,resizable=yes');newWindow.$windowopen;newWindow.document.writeln('$start_page $output $end_page');newWindow.document.close();newWindow.focus()}
1.217 albertel 1317: // ]]>
1.231 albertel 1318: </script><a href=\"javascript:$function_name();void(0);\">".&mt("Previous Tries")."</a><br />";
1.217 albertel 1319: #use Data::Dumper;
1320: #&Apache::lonnet::logthis(&Dumper(\%Apache::inputtags::submission_display));
1321: return $result;
1322: }
1323:
1.1 albertel 1324: 1;
1325: __END__
1.43 albertel 1326:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>