Annotation of loncom/homework/inputtags.pm, revision 1.43
1.43 ! albertel 1: # The LearningOnline Network with CAPA
! 2: # input definitons
! 3: # 2/19 Guy
1.1 albertel 4:
5: package Apache::inputtags;
6: use strict;
7:
8: sub BEGIN {
1.43 ! albertel 9: &Apache::lonxml::register('Apache::inputtags',('textarea','textline','datasubmission'));
1.1 albertel 10: }
11:
1.43 ! albertel 12:
1.1 albertel 13: sub initialize_inputtags {
1.43 ! albertel 14: # list of current input ids
! 15: @Apache::inputtags::input=();
! 16: # list of all input ids seen in this problem
! 17: @Apache::inputtags::inputlist=();
! 18: # list of all current response ids
! 19: @Apache::inputtags::response=();
! 20: # list of all response ids seen in this problem
! 21: @Apache::inputtags::responselist=();
! 22: # list of whether or not a specific response was previously used
! 23: @Apache::inputtags::previous=();
! 24: # id of current part, 0 means that no part is current (inside <problem> only
! 25: $Apache::inputtags::part='';
! 26: # list of problem date statuses, the first element is for <problem>
! 27: #if there is a second element it is for the current <part>
! 28: @Apache::inputtags::status=();
! 29: #hash of defined params for the current response
! 30: %Apache::inputtags::params=();
1.1 albertel 31: }
32:
1.14 albertel 33: sub start_input {
1.43 ! albertel 34: my ($parstack,$safeeval)=@_;
! 35: my $id = &Apache::lonxml::get_param('id',$parstack,$safeeval);
! 36: if ($id eq '') { $id = $Apache::lonxml::curdepth; }
! 37: push (@Apache::inputtags::input,$id);
! 38: push (@Apache::inputtags::inputlist,$id);
! 39: return $id;
1.14 albertel 40: }
41:
42: sub end_input {
1.43 ! albertel 43: pop @Apache::inputtags::input;
! 44: return '';
1.14 albertel 45: }
46:
1.6 albertel 47: sub start_textarea {
1.43 ! albertel 48: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
! 49: my $result = "";
! 50: my $id = &start_input($parstack,$safeeval);
! 51: if ($target eq 'web') {
! 52: my $oldresponse = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.$Apache::inputtags::response['-1'].submission"};
! 53: my $cols = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
! 54: if ( $cols eq '') { $cols = 80; }
! 55: my $rows = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
! 56: if ( $rows eq '') { $rows = 10; }
! 57: $result= '<textarea name="HWVAL'.$Apache::inputtags::response['-1'].'" '.
! 58: "rows=\"$rows\" cols=\"$cols\">".$oldresponse;
! 59: if ($oldresponse ne '') {
! 60: #get rid of any startup text if the user has already responded
! 61: &Apache::lonxml::get_all_text("/textarea",$$parser[$#$parser]);
1.30 albertel 62: }
1.43 ! albertel 63: }
! 64: return $result;
1.6 albertel 65: }
66:
67: sub end_textarea {
1.43 ! albertel 68: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
! 69: if ($target eq 'web') {
! 70: return "</textarea>";
! 71: }
! 72: &end_input;
! 73: return '';
1.6 albertel 74: }
75:
1.1 albertel 76: sub start_textline {
1.43 ! albertel 77: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
! 78: my $result = "";
! 79: if ($target eq 'web') {
! 80: my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
! 81: if ($size eq '') { $size=20; }
! 82: my $oldresponse = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.$Apache::inputtags::response['-1'].submission"};
! 83: $result= '<input type="text" name="HWVAL'.$Apache::inputtags::response['-1'].'" value="'.$oldresponse.'" size="'.$size.'" />';
! 84: }
! 85: if ($target eq 'edit') {
! 86: $result.=&Apache::edit::tag_start($target,$token,&Apache::lonxml::description($token));
! 87: $result.=&Apache::edit::text_arg('Size:','size',$token,'5')."</td></tr>";
! 88: $result.=&Apache::edit::end_table;
! 89: }
! 90: if ($target eq 'modified') {
! 91: my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'size');
! 92: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
! 93: }
! 94: return $result;
1.1 albertel 95: }
96:
97: sub end_textline {
1.43 ! albertel 98: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
! 99: if ($target eq 'edit') { return ('','no'); }
! 100: return "";
1.1 albertel 101: }
102:
1.6 albertel 103: sub start_datasubmission {
1.43 ! albertel 104: return '';
1.6 albertel 105: }
106:
107: sub end_datasubmission {
1.43 ! albertel 108: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
! 109: if ( $target == 'web' ) {
! 110: return '<input type="submit" name="submit" value="Submit All Data" />';
! 111: }
! 112: return '';
1.9 albertel 113: }
114:
115: sub finalizeawards {
1.43 ! albertel 116: my $result='';
! 117: my $award;
! 118: if ($#_ == '-1') { $result = "NO_RESPONSE"; }
! 119: if ($result eq '' ) {
! 120: foreach $award (@_) { if ($award eq '') {$result='MISSING_ANSWER'; last;}}
! 121: }
! 122: if ($result eq '' ) {
! 123: foreach $award (@_) { if ($award eq 'ERROR') {$result='ERROR'; last;}}
! 124: }
! 125: if ($result eq '' ) {
! 126: foreach $award (@_) { if ($award eq 'NO_RESPONSE') {$result='NO_RESPONSE'; last;} }
! 127: }
! 128:
! 129: if ($result eq '' ) {
! 130: foreach $award (@_) {
! 131: if ($award eq 'UNIT_FAIL' ||
! 132: $award eq 'NO_UNIT' ||
! 133: $award eq 'UNIT_NOTNEEDED') {
! 134: $result=$award; last;
! 135: }
! 136: }
! 137: }
! 138: if ($result eq '' ) {
! 139: foreach $award (@_) {
! 140: if ($award eq 'WANTED_NUMERIC' ||
! 141: $award eq 'BAD_FORMULA') {$result=$award; last;}
! 142: }
! 143: }
! 144: if ($result eq '' ) {
! 145: foreach $award (@_) { if ($award eq 'SIG_FAIL') {$result=$award; last;} }
! 146: }
! 147: if ($result eq '' ) {
! 148: foreach $award (@_) { if ($award eq 'INCORRECT') {$result=$award; last;} }
! 149: }
! 150: if ($result eq '' ) {
! 151: foreach $award (@_) { if ($award eq 'SUBMITTED') {$result=$award; last;} }
! 152: }
! 153: if ($result eq '' ) {
! 154: foreach $award (@_) { if ($award eq 'APPROX_ANS') {$result=$award; last;} }
! 155: }
! 156: if ($result eq '' ) { $result='EXACT_ANS'; }
! 157: return $result
1.9 albertel 158: }
159:
1.10 albertel 160: sub decideoutput {
1.43 ! albertel 161: my ($award,$solved,$previous)=@_;
! 162: my $message='';
! 163: my $button=0;
! 164: my $previousmsg;
! 165:
! 166: if ($previous) { $previousmsg='You have entered that answer before'; }
! 167:
! 168: if ($solved =~ /^correct/) {
! 169: $message = "<b>You are correct.</b> Your receipt is ".
! 170: &Apache::lonnet::receipt;
! 171: $button=0;
! 172: $previousmsg='';
! 173: } elsif ($solved =~ /^excused/) {
! 174: $message = "<b>You are excused from the problem.</b>";
! 175: $button=0;
! 176: $previousmsg='';
! 177: } elsif ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
! 178: if ($solved =~ /^incorrect/ || $solved eq '') {
! 179: $message = "Incorrect";
! 180: $button=1;
1.37 albertel 181: } else {
1.43 ! albertel 182: $message = "<b>You are correct.</b> Your receipt is ".
! 183: &Apache::lonnet::receipt;
! 184: $button=0;
! 185: $previousmsg='';
! 186: }
! 187: } elsif ($award eq 'NO_RESPONSE') {
! 188: $message = '';
! 189: $button=1;
! 190: } elsif ($award eq 'MISSING_ANSWER') {
! 191: $message = 'Some parts were not submitted';
! 192: $button = 1;
! 193: } elsif ($award eq 'WANTED_NUMERIC') {
! 194: $message = "This question expects a numeric answer";
! 195: $button=1;
! 196: } elsif ($award eq 'SIG_FAIL') {
! 197: $message = "Please adjust significant figures.";# you provided %s significant figures";
! 198: $button=1;
! 199: } elsif ($award eq 'UNIT_FAIL') {
! 200: $message = "Units incorrect."; #Computer reads units as %s";
! 201: $button=1;
! 202: } elsif ($award eq 'UNIT_NOTNEEDED') {
! 203: $message = "Only a number required.";# Computer reads units of %s";
! 204: $button=1;
! 205: } elsif ($award eq 'NO_UNIT') {
! 206: $message = "Units required";
! 207: $button=1;
! 208: } elsif ($award eq 'BAD_FORMULA') {
! 209: $message = "Unable to understand formula";
! 210: $button=1;
! 211: } elsif ($award eq 'INCORRECT') {
! 212: $message = "Incorrect";
! 213: $button=1;
! 214: } elsif ($award eq 'SUBMITTED') {
! 215: $message = "Your submission has been recorded.";
! 216: $button=1;
! 217: } else {
! 218: $message = "Unknown message: $award";
! 219: $button=1;
! 220: }
! 221: return ($button,$message,$previousmsg);
1.12 albertel 222: }
223:
224: sub setgradedata {
1.43 ! albertel 225: my ($award,$id,$previously_used) = @_;
! 226: # if the student already has it correct, don't modify the status
! 227: if ( $Apache::lonhomework::history{"resource.$id.solved"} !~
! 228: /^correct/ ) {
! 229: #handle assignment of tries and solved status
! 230: if ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
! 231: $Apache::lonhomework::results{"resource.$id.tries"} =
! 232: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
! 233: $Apache::lonhomework::results{"resource.$id.solved"} =
! 234: 'correct_by_student';
! 235: $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
! 236: } elsif ( $award eq 'INCORRECT' ) {
! 237: $Apache::lonhomework::results{"resource.$id.tries"} =
! 238: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
! 239: $Apache::lonhomework::results{"resource.$id.solved"} =
! 240: 'incorrect_attempted';
! 241: } elsif ( $award eq 'SUBMITTED' ) {
! 242: $Apache::lonhomework::results{"resource.$id.tries"} =
! 243: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
! 244: $Apache::lonhomework::results{"resource.$id.solved"} =
! 245: 'ungraded_attempted';
! 246: } elsif ( $award eq 'NO_RESPONSE' ) {
! 247: return '';
! 248: } else {
! 249: $Apache::lonhomework::results{"resource.$id.solved"} =
! 250: 'incorrect_attempted';
! 251: }
! 252:
! 253: # check if this was a previous submission if it was delete the
! 254: # unneeded data and update the previously_used attribute
! 255: if ( $previously_used eq 'PREVIOUSLY_USED') {
! 256: delete($Apache::lonhomework::results{"resource.$id.tries"});
! 257: $Apache::lonhomework::results{"resource.$id.previous"} = '1';
! 258: } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
! 259: #delete all data as they student didn't do anything
! 260: foreach my $key (keys(%Apache::lonhomework::results)) {
! 261: if ($key =~ /^resource\.$id\./) {
! 262: &Apache::lonxml::debug("Removing $key");
! 263: delete($Apache::lonhomework::results{$key});
! 264: }
! 265: }
! 266: #and since they didn't do anything we were never here
! 267: return '';
! 268: } else {
! 269: $Apache::lonhomework::results{"resource.$id.previous"} = '0';
1.40 albertel 270: }
1.43 ! albertel 271: }
! 272: $Apache::lonhomework::results{"resource.$id.award"} = $award;
1.10 albertel 273: }
274:
1.9 albertel 275: sub grade {
1.43 ! albertel 276: my ($target) = @_;
! 277: my $id = $Apache::inputtags::part;
! 278: my $response='';
! 279: if ( defined $ENV{'form.submitted'}) {
! 280: my @awards = ();
! 281: foreach $response (@Apache::inputtags::responselist) {
! 282: &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
! 283: my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
! 284: if ( $value ne '' ) {
! 285: &Apache::lonxml::debug("keeping $value from $response for $id");
! 286: push (@awards,$value);
! 287: } else {
! 288: &Apache::lonxml::debug("skipping $value from $response for $id");
! 289: }
! 290: }
! 291: my $finalaward = &finalizeawards(@awards);
! 292: my $previously_used;
! 293: if ( $#Apache::inputtags::previous eq $#awards ) {
! 294: $previously_used = 'PREVIOUSLY_LAST';
! 295: foreach my $value (@Apache::inputtags::previous) {
! 296: if ($value eq 'PREVIOUSLY_USED' ) {
! 297: $previously_used = $value;
! 298: last;
! 299: }
! 300: }
! 301: }
! 302: &Apache::lonxml::debug("final award $finalaward, $previously_used");
! 303: &setgradedata($finalaward,$id,$previously_used);
! 304: }
! 305: return '';
1.1 albertel 306: }
307:
1.11 albertel 308: sub gradestatus {
1.43 ! albertel 309: my ($id) = @_;
! 310: my $showbutton = 1;
! 311: my $message = '';
! 312: my $trystr='';
! 313: my $button='';
! 314: my $previousmsg='';
! 315:
! 316: my $status = $Apache::inputtags::status['-1'];
! 317: &Apache::lonxml::debug("gradestatus has :$status:");
! 318: if ( $status ne 'CLOSED' ) {
! 319: my $award = $Apache::lonhomework::history{"resource.$id.award"};
! 320: my $solved = $Apache::lonhomework::history{"resource.$id.solved"};
! 321: my $previous = $Apache::lonhomework::history{"resource.$id.previous"};
! 322: &Apache::lonxml::debug("Found Award |$award|$solved|");
! 323: if ( $award ne '' ) {
! 324: &Apache::lonxml::debug('Getting message');
! 325: ($showbutton,$message,$previousmsg) =
! 326: &decideoutput($award,$solved,$previous);
! 327: $message="<td bgcolor=\"#aaffaa\">$message</td>";
! 328: if ($previousmsg) {
! 329: $previousmsg="<td bgcolor=\"#ffaaaa\">$previousmsg</td>";
! 330: }
! 331: }
! 332: my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
! 333: my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
! 334: &Apache::lonxml::debug("got maxtries of :$maxtries:");
! 335: if ( $tries eq '' ) { $tries = '0'; }
! 336: if ( $maxtries eq '' ) { $maxtries = '2'; }
! 337: if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; }
! 338: if ( $showbutton ) {
! 339: $trystr = "<td>Tries $tries/$maxtries</td>";
! 340: }
! 341: if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {$showbutton = 0;}
! 342: if ( $showbutton ) {
! 343: $button = '<br /><input type="submit" name="submit" value="Submit All Answers" />';
! 344: }
! 345: }
! 346: my $output= $previousmsg.$message.$trystr;
! 347: if ($output =~ /^\s*$/) {
! 348: return $button;
! 349: } else {
! 350: return $button.'<table><tr>'.$previousmsg.$message.$trystr.'</tr></table>';
! 351: }
1.11 albertel 352: }
1.1 albertel 353: 1;
354: __END__
1.43 ! albertel 355:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>