1: # The LearningOnline Network with CAPA
2: # input definitons
3: #
4: # $Id: inputtags.pm,v 1.96 2003/04/24 12:44:54 sakharuk Exp $
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/
27: #
28: # 2/19 Guy
29:
30: package Apache::inputtags;
31: use HTML::Entities();
32: use strict;
33: use Apache::loncommon;
34:
35: BEGIN {
36: &Apache::lonxml::register('Apache::inputtags',('textfield','textline'));
37: }
38:
39:
40: sub initialize_inputtags {
41: # list of current input ids
42: @Apache::inputtags::input=();
43: # list of all input ids seen in this problem
44: @Apache::inputtags::inputlist=();
45: # list of all current response ids
46: @Apache::inputtags::response=();
47: # list of all response ids seen in this problem
48: @Apache::inputtags::responselist=();
49: # list of whether or not a specific response was previously used
50: @Apache::inputtags::previous=();
51: # submission it was used in
52: @Apache::inputtags::previous_version=();
53: # id of current part, 0 means that no part is current (inside <problem> only
54: $Apache::inputtags::part='';
55: # list of problem date statuses, the first element is for <problem>
56: # if there is a second element it is for the current <part>
57: @Apache::inputtags::status=();
58: # hash of defined params for the current response
59: %Apache::inputtags::params=();
60: # list of all ids, for <import>, these get join()ed and prepended
61: @Apache::inputtags::import=();
62: }
63:
64: sub start_input {
65: my ($parstack,$safeeval)=@_;
66: my $id = &Apache::lonxml::get_param('id',$parstack,$safeeval);
67: if ($id eq '') { $id = $Apache::lonxml::curdepth; }
68: push (@Apache::inputtags::input,$id);
69: push (@Apache::inputtags::inputlist,$id);
70: return $id;
71: }
72:
73: sub end_input {
74: pop @Apache::inputtags::input;
75: return '';
76: }
77:
78: sub start_textfield {
79: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
80: my $result = "";
81: my $id = &start_input($parstack,$safeeval);
82: my $resid=$Apache::inputtags::response[-1];
83: if ($target eq 'web') {
84: $Apache::lonxml::evaluate--;
85: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
86: my $partid=$Apache::inputtags::part;
87: my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$resid.submission"});
88: my $cols = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
89: if ( $cols eq '') { $cols = 80; }
90: my $rows = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
91: if ( $rows eq '') { $rows = 10; }
92: $result= '<textarea name="HWVAL_'.$resid.'" '.
93: "rows=\"$rows\" cols=\"$cols\">".$oldresponse;
94: if ($oldresponse ne '') {
95: #get rid of any startup text if the user has already responded
96: &Apache::lonxml::get_all_text("/textfield",$parser);
97: }
98: } else {
99: #right or wrong don't show it
100: #$result='<table border="1"><tr><td><i>'.$oldresponse.'</i></td></tr></table>';
101: $result='';
102: #get rid of any startup text
103: &Apache::lonxml::get_all_text("/textfield",$parser);
104: }
105: } elsif ($target eq 'grade') {
106: my $seedtext=&Apache::lonxml::get_all_text("/textfield",$parser);
107: if ($seedtext eq $ENV{'form.HWVAL_'.$resid}) {
108: # if the seed text is still there it wasn't a real submission
109: $ENV{'form.HWVAL_'.$resid}='';
110: }
111: } elsif ($target eq 'edit') {
112: $result.=&Apache::edit::tag_start($target,$token);
113: $result.=&Apache::edit::text_arg('Rows:','rows',$token,4);
114: $result.=&Apache::edit::text_arg('Columns:','cols',$token,4);
115: my $bodytext=&Apache::lonxml::get_all_text("/textfield",$parser);
116: $result.=&Apache::edit::editfield($token->[1],$bodytext,'Text you want to appear by default:',80,2);
117: } elsif ($target eq 'modified') {
118: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
119: $safeeval,'rows','cols');
120: if ($constructtag) {
121: $result = &Apache::edit::rebuild_tag($token);
122: } else {
123: $result=$token->[4];
124: }
125: $result.=&Apache::edit::modifiedfield();
126: } elsif ($target eq 'tex') {
127: my $number_of_lines = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
128: my $width_of_box = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
129: if ($$tagstack[-2] eq 'essayresponse' and $Apache::lonhomework::type eq 'exam') {
130: $result = '\fbox{\fbox{\parbox{\textwidth-5mm}{';
131: for (my $i=0;$i<int $number_of_lines*2;$i++) {$result.='\strut \\\\ ';}
132: $result.='\strut \\\\\strut \\\\\strut \\\\\strut \\\\}}}';
133: } else {
134: my $TeXwidth=$width_of_box/80;
135: $result = '\vskip 1 mm \fbox{\fbox{\parbox{'.$TeXwidth.'\textwidth-5mm}{';
136: for (my $i=0;$i<int $number_of_lines*2;$i++) {$result.='\strut \\\\ ';}
137: $result.='}}}\vskip 2 mm ';
138: }
139: }
140: return $result;
141: }
142:
143: sub end_textfield {
144: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
145: my $result;
146: if ($target eq 'web') {
147: $Apache::lonxml::evaluate++;
148: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
149: return "</textarea>";
150: }
151: } elsif ($target eq 'edit') {
152: $result=&Apache::edit::end_table();
153: }
154: &end_input;
155: return $result;
156: }
157:
158: sub start_textline {
159: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
160: my $result = "";
161: if ($target eq 'web') {
162: $Apache::lonxml::evaluate--;
163: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
164: my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
165: my $maxlength;
166: if ($size eq '') { $size=20; } else {
167: if ($size < 20) { $maxlength=$size; }
168: }
169: my $partid=$Apache::inputtags::part;
170: my $id=$Apache::inputtags::response[-1];
171: my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$id.submission"});
172: if ($Apache::lonhomework::type ne 'exam') {
173: $result= '<input type="text" name="HWVAL_'.$id.'" value="'.
174: $oldresponse.'" size="'.$size.'" maxlength="'.$maxlength.'" />';
175: }
176: } else {
177: #right or wrong don't show what was last typed in.
178: #$result='<i>'.$oldresponse.'</i>';
179: $result='';
180: }
181: } elsif ($target eq 'edit') {
182: $result=&Apache::edit::tag_start($target,$token);
183: $result.=&Apache::edit::text_arg('Size:','size',$token,'5')."</td></tr>";
184: $result.=&Apache::edit::end_table;
185: } elsif ($target eq 'modified') {
186: my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'size');
187: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
188: } elsif ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
189: my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
190: if ($size != 0) {$size=$size*2; $size.=' mm';} else {$size='40 mm';}
191: $result='\framebox['.$size.'][s]{\tiny\strut}';
192: }
193: return $result;
194: }
195:
196: sub end_textline {
197: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
198: if ($target eq 'web') { $Apache::lonxml::evaluate++; }
199: elsif ($target eq 'edit') { return ('','no'); }
200: return "";
201: }
202:
203: sub finalizeawards {
204: my $result='';
205: my $award;
206: if ($#_ == '-1') { $result = "NO_RESPONSE"; }
207: if ($result eq '' ) {
208: my $blankcount;
209: foreach $award (@_) {
210: if ($award eq '') {
211: $result='MISSING_ANSWER';
212: $blankcount++;
213: }
214: }
215: if ($blankcount == ($#_ + 1)) { $result = 'NO_RESPONSE'; }
216: }
217: if ($result eq '' ) {
218: foreach $award (@_) { if ($award eq 'MISSING_ANSWER') {$result='MISSING_ANSWER'; last;}}
219: }
220: if ($result eq '' ) {
221: foreach $award (@_) { if ($award eq 'ERROR') {$result='ERROR'; last;}}
222: }
223: if ($result eq '' ) {
224: foreach $award (@_) { if ($award eq 'NO_RESPONSE') {$result='NO_RESPONSE'; last;} }
225: }
226:
227: if ($result eq '' ) {
228: foreach $award (@_) {
229: if ($award eq 'UNIT_FAIL' ||
230: $award eq 'NO_UNIT' ||
231: $award eq 'UNIT_NOTNEEDED') {
232: $result=$award; last;
233: }
234: }
235: }
236: if ($result eq '' ) {
237: foreach $award (@_) {
238: if ($award eq 'WANTED_NUMERIC' ||
239: $award eq 'BAD_FORMULA') {$result=$award; last;}
240: }
241: }
242: if ($result eq '' ) {
243: foreach $award (@_) { if ($award eq 'SIG_FAIL') {$result=$award; last;} }
244: }
245: if ($result eq '' ) {
246: foreach $award (@_) { if ($award eq 'INCORRECT') {$result=$award; last;} }
247: }
248: if ($result eq '' ) {
249: foreach $award (@_) { if ($award eq 'MISORDERED_RANK') {$result=$award; last;} }
250: }
251: if ($result eq '' ) {
252: foreach $award (@_) { if ($award eq 'INVALID_FILETYPE') {$result=$award; last;} }
253: }
254: if ($result eq '' ) {
255: foreach $award (@_) { if ($award eq 'DRAFT') {$result=$award; last;} }
256: }
257: if ($result eq '' ) {
258: foreach $award (@_) { if ($award eq 'SUBMITTED') {$result=$award; last;} }
259: }
260: if ($result eq '' ) {
261: foreach $award (@_) { if ($award eq 'ASSIGNED_SCORE') {$result=$award; last;} }
262: }
263: if ($result eq '' ) {
264: foreach $award (@_) { if ($award eq 'APPROX_ANS') {$result=$award; last;} }
265: }
266: if ($result eq '' ) { $result='EXACT_ANS'; }
267: return $result
268: }
269:
270: sub decideoutput {
271: my ($award,$solved,$previous,$target)=@_;
272: my $message='';
273: my $button=0;
274: my $previousmsg;
275:
276: if ($previous) { $previousmsg='You have entered that answer before'; }
277:
278: if ($solved =~ /^correct/) {
279: if ($award eq 'ASSIGNED_SCORE') {
280: $message = "A score has been assigned.";
281: } else {
282: if ($target eq 'tex') {
283: $message = '\textbf{You are correct}.';
284: } else {
285: $message = "<b>You are correct.</b>";
286: }
287: $message=' Your receipt is '.&Apache::lonnet::receipt;
288: }
289: $button=0;
290: $previousmsg='';
291: } elsif ($solved =~ /^excused/) {
292: $message = "<b>You are excused from the problem.</b>";
293: $button=0;
294: $previousmsg='';
295: } elsif ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
296: if ($solved =~ /^incorrect/ || $solved eq '') {
297: $message = "Incorrect";
298: $button=1;
299: } else {
300: $message = "<b>You are correct.</b> Your receipt is ".
301: &Apache::lonnet::receipt;
302: $button=0;
303: $previousmsg='';
304: }
305: } elsif ($award eq 'NO_RESPONSE') {
306: $message = '';
307: $button=1;
308: } elsif ($award eq 'MISSING_ANSWER') {
309: $message = 'Some parts were not submitted';
310: $button = 1;
311: } elsif ($award eq 'WANTED_NUMERIC') {
312: $message = "This question expects a numeric answer";
313: $button=1;
314: } elsif ($award eq 'MISORDERED_RANK') {
315: $message = 'You have provided an invalid ranking, please refer to '.
316: &Apache::loncommon::help_open_topic('Ranking_Problems','help on ranking problems').'.';
317: $button=1;
318: } elsif ($award eq 'INVALID_FILETYPE') {
319: $message = 'The filetype extension of the file you uploaded is not allowed.';
320: $button=1;
321: } elsif ($award eq 'SIG_FAIL') {
322: $message = "Please adjust significant figures.";# you provided %s significant figures";
323: $button=1;
324: } elsif ($award eq 'UNIT_FAIL') {
325: $message = "Units incorrect. ".
326: &Apache::loncommon::help_open_topic('Physical_Units'); #Computer reads units as %s";
327: $button=1;
328: } elsif ($award eq 'UNIT_NOTNEEDED') {
329: $message = "Only a number required.";# Computer reads units of %s";
330: $button=1;
331: } elsif ($award eq 'NO_UNIT') {
332: $message = "Units required".
333: &Apache::loncommon::help_open_topic('Physical_Units');
334: $button=1;
335: } elsif ($award eq 'BAD_FORMULA') {
336: $message = "Unable to understand formula";
337: $button=1;
338: } elsif ($award eq 'INCORRECT') {
339: $message = "Incorrect";
340: $button=1;
341: } elsif ($award eq 'SUBMITTED') {
342: $message = "Your submission has been recorded.";
343: $button=1;
344: } elsif ($award eq 'DRAFT') {
345: $message = "A draft copy has been saved.";
346: $button=1;
347: } elsif ($award eq 'ASSIGNED_SCORE') {
348: $message = "A score has been assigned.";
349: $button=0;
350: } else {
351: $message = "Unknown message: $award";
352: $button=1;
353: }
354: if (lc($Apache::lonhomework::problemstatus) ne 'yes') {
355: $message = "Answer Submitted";
356: $button=1;
357: }
358: return ($button,$message,$previousmsg);
359: }
360:
361: sub removealldata {
362: my ($id)=@_;
363: foreach my $key (keys(%Apache::lonhomework::results)) {
364: if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
365: &Apache::lonxml::debug("Removing $key");
366: delete($Apache::lonhomework::results{$key});
367: }
368: }
369: }
370:
371: sub setgradedata {
372: my ($award,$id,$previously_used) = @_;
373: # if the student already has it correct, don't modify the status
374: if ($Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
375: $Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER') {
376: $Apache::lonhomework::results{"resource.$id.afterduedate"}=$award;
377: return '';
378: } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} !~
379: /^correct/ || $Apache::lonhomework::scantronmode) {
380: #handle assignment of tries and solved status
381: my $solvemsg;
382: if ($Apache::lonhomework::scantronmode) {
383: $solvemsg='correct_by_scantron';
384: } else {
385: $solvemsg='correct_by_student';
386: }
387: if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
388: $Apache::lonhomework::results{"resource.$id.afterduedate"}='';
389: }
390: if ( $award eq 'ASSIGNED_SCORE') {
391: $Apache::lonhomework::results{"resource.$id.tries"} =
392: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
393: $Apache::lonhomework::results{"resource.$id.solved"} =
394: $solvemsg;
395: my $numawards=scalar(@Apache::inputtags::responselist);
396: &Apache::lonxml::debug("Whaaa!");
397: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
398: foreach my $res (@Apache::inputtags::responselist) {
399: $Apache::lonhomework::results{"resource.$id.awarded"}+=
400: $Apache::lonhomework::results{"resource.$id.$res.awarded"};
401: }
402: if ($numawards > 0) {
403: $Apache::lonhomework::results{"resource.$id.awarded"}/=
404: $numawards;
405: }
406: } elsif ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
407: $Apache::lonhomework::results{"resource.$id.tries"} =
408: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
409: $Apache::lonhomework::results{"resource.$id.solved"} =
410: $solvemsg;
411: $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
412: } elsif ( $award eq 'INCORRECT' ) {
413: $Apache::lonhomework::results{"resource.$id.tries"} =
414: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
415: $Apache::lonhomework::results{"resource.$id.solved"} =
416: 'incorrect_attempted'
417: } elsif ( $award eq 'SUBMITTED' ) {
418: $Apache::lonhomework::results{"resource.$id.tries"} =
419: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
420: $Apache::lonhomework::results{"resource.$id.solved"} =
421: 'ungraded_attempted';
422: } elsif ( $award eq 'DRAFT' ) {
423: $Apache::lonhomework::results{"resource.$id.solved"} = '';
424: } elsif ( $award eq 'NO_RESPONSE' ) {
425: #no real response so delete any data that got stored
426: &removealldata($id);
427: return '';
428: } else {
429: $Apache::lonhomework::results{"resource.$id.solved"} =
430: 'incorrect_attempted';
431: }
432:
433: # check if this was a previous submission if it was delete the
434: # unneeded data and update the previously_used attribute
435: if ( $previously_used eq 'PREVIOUSLY_USED') {
436: delete($Apache::lonhomework::results{"resource.$id.tries"});
437: $Apache::lonhomework::results{"resource.$id.previous"} = '1';
438: } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
439: #delete all data as they student didn't do anything, but save
440: #the list of collaborators.
441: &removealldata($id);
442: #and since they didn't do anything we were never here
443: return '';
444: } else {
445: $Apache::lonhomework::results{"resource.$id.previous"} = '0';
446: }
447: } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} =~
448: /^correct/ ) {
449: #delete all data as they student already has it correct
450: &removealldata($id);
451: #and since they didn't do anything we were never here
452: return '';
453: }
454: $Apache::lonhomework::results{"resource.$id.award"} = $award;
455: }
456:
457: sub grade {
458: my ($target) = @_;
459: my $id = $Apache::inputtags::part;
460: my $response='';
461: if ( defined $ENV{'form.submitted'}) {
462: my @awards = ();
463: foreach $response (@Apache::inputtags::responselist) {
464: &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
465: my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
466: &Apache::lonxml::debug("keeping $value from $response for $id");
467: push (@awards,$value);
468: }
469: my $finalaward = &finalizeawards(@awards);
470: my $previously_used;
471: if ( $#Apache::inputtags::previous eq $#awards ) {
472: my $match=0;
473: my @matches;
474: foreach my $versionar (@Apache::inputtags::previous_version) {
475: foreach my $version (@$versionar) {
476: $matches[$version]++;
477: }
478: }
479: foreach my $elem (@matches) {if ($elem eq ($#awards+1)) {$match=1;}}
480: if ($match) {
481: $previously_used = 'PREVIOUSLY_LAST';
482: foreach my $value (@Apache::inputtags::previous) {
483: if ($value eq 'PREVIOUSLY_USED' ) {
484: $previously_used = $value;
485: last;
486: }
487: }
488: }
489: }
490: &Apache::lonxml::debug("final award $finalaward, $previously_used");
491: &setgradedata($finalaward,$id,$previously_used);
492: }
493: return '';
494: }
495:
496: sub gradestatus {
497: my ($id,$target) = @_;
498: my $showbutton = 1;
499: my $message = '';
500: my $latemessage = '';
501: my $trystr='';
502: my $button='';
503: my $previousmsg='';
504:
505: my $status = $Apache::inputtags::status['-1'];
506: &Apache::lonxml::debug("gradestatus has :$status:");
507: if ( $status ne 'CLOSED' && $status ne 'UNAVAILABLE') {
508: my $award = $Apache::lonhomework::history{"resource.$id.award"};
509: my $solved = $Apache::lonhomework::history{"resource.$id.solved"};
510: my $previous = $Apache::lonhomework::history{"resource.$id.previous"};
511: &Apache::lonxml::debug("Found Award |$award|$solved|");
512: if ( $award ne '' ) {
513: &Apache::lonxml::debug('Getting message');
514: ($showbutton,$message,$previousmsg) =
515: &decideoutput($award,$solved,$previous,$target);
516: if ($target eq 'tex') {
517: $message=' '.$message.' ';
518: } else {
519: $message="<td bgcolor=\"#aaffaa\">$message</td>";
520: if ($previousmsg) {
521: $previousmsg="<td bgcolor=\"#ffaaaa\">$previousmsg</td>";
522: }
523: }
524: }
525: my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
526: my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
527: &Apache::lonxml::debug("got maxtries of :$maxtries:");
528: if ( $tries eq '' ) { $tries = '0'; }
529: if ( $maxtries eq '' ) { $maxtries = '2'; }
530: if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; }
531: if ( $showbutton ) {
532: if ($target eq 'tex') {
533: if ($ENV{'request.state'} ne "construct") {
534: $trystr = ' {\vskip -1 mm \small \textit{Tries} '.$tries.'/'.$maxtries.'} \vskip 2 mm ';
535: } else {
536: $trystr = '\vskip 0 mm ';
537: }
538: } else {
539: $trystr = "<td>Tries $tries/$maxtries</td>";
540: }
541: }
542: if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {$showbutton = 0;}
543: if ( $showbutton ) {
544: if ($target ne 'tex') {
545: $button = '<br /><input type="submit" name="submit" value="Submit Answer" />';
546: }
547: }
548: if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
549: #last submissions was after due date
550: if ($target eq 'tex') {
551: $latemessage=' The last submission was after the Due Date ';
552: } else {
553: $latemessage="<td bgcolor=\"#ffaaaa\">The last submission was after the Due Date</td>";
554: }
555: }
556: }
557: my $output= $previousmsg.$latemessage.$message.$trystr;
558: if ($output =~ /^\s*$/) {
559: return $button;
560: } else {
561: if ($target eq 'tex') {
562: return $button.' \vskip 0 mm '.$output.' ';
563: } else {
564: return $button.'<table><tr>'.$output.'</tr></table>';
565: }
566: }
567: }
568: 1;
569: __END__
570:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>