Annotation of loncom/homework/lonhomework.pm, revision 1.35
1.1 albertel 1: # The LON-CAPA Homework handler
2: # Guy Albertelli
1.17 www 3: # 11/30 Gerd Kortemeyer
1.25 www 4: # 6/1 Gerd Kortemeyer
1.1 albertel 5:
6: package Apache::lonhomework;
7: use strict;
8: use Apache::style;
9: use Apache::lonxml;
1.2 albertel 10: use Apache::lonnet;
11: use Apache::inputtags;
12: use Apache::structuretags;
1.9 albertel 13: use Apache::response;
1.22 albertel 14: use Apache::hint;
1.31 albertel 15: use Apache::outputtags;
1.26 www 16: use Apache::Constants qw(:common);
1.16 albertel 17:
1.5 albertel 18: sub get_target {
1.15 albertel 19: if ( $ENV{'request.state'} eq "published") {
1.34 albertel 20: if ( defined $ENV{'form.submitted'}) {
1.18 albertel 21: return ('grade', 'web');
22: } else {
23: return ('web');
24: }
1.15 albertel 25: } elsif ($ENV{'request.state'} eq "construct") {
1.16 albertel 26: if ( defined $ENV{'form.preview'}) {
1.34 albertel 27: if ( defined $ENV{'form.submitted'}) {
1.18 albertel 28: return ('grade', 'web');
29: } else {
30: return ('web');
31: }
1.15 albertel 32: } else {
1.29 albertel 33: #return ('edit');
34: #edit is currently broken
35: return ('web');
1.15 albertel 36: }
37: }
1.18 albertel 38: return ();
1.5 albertel 39: }
40:
1.3 albertel 41: sub setup_vars {
1.5 albertel 42: my ($target) = @_;
1.11 albertel 43: return ';'
44: # return ';$external::target='.$target.';';
1.2 albertel 45: }
46:
47: sub send_header {
48: my ($request)= @_;
1.14 albertel 49: $request->print(&Apache::lontexconvert::header());
1.16 albertel 50: $ENV{'request.uri'}=$request->uri;
51: # $request->print('<form name='.$ENV{'form.request.prefix'}.'lonhomework method="POST" action="'.$request->uri.'">');
1.2 albertel 52: }
53:
54: sub send_footer {
55: my ($request)= @_;
1.16 albertel 56: # $request->print('</form>');
1.14 albertel 57: $request->print(&Apache::lontexconvert::footer());
1.2 albertel 58: }
59:
1.21 albertel 60: #@Apache::lonhomework::date_status=();
61: #@Apache::lonhomework::date_msg=();
1.20 albertel 62: sub check_date {
1.21 albertel 63: my ($id) = @_;
1.20 albertel 64: my $date ='';
1.21 albertel 65: my $status = '';
66: my $datemsg = '';
67: my $lastdate = '';
68: my $temp;
69: my $type;
70: my $passed;
71: &Apache::lonxml::debug("checking for part :$id:");
72: foreach $temp ("opendate","duedate","answerdate") {
73: $lastdate = $date;
74: $date = &Apache::lonnet::EXT("resource.$id.$temp");
75: &Apache::lonxml::debug("found :$date: for :$temp:");
76: if ($date eq '') {
77: $date = "an unknown date"; $passed = 0;
1.30 albertel 78: } elsif ($date eq 'con_lost') {
79: $date = "an indeterminate date"; $passed = 0;
1.21 albertel 80: } else {
81: if (time < $date) { $passed = 0; } else { $passed = 1; }
82: $date = localtime $date;
83: }
84: if (!$passed) { $type=$temp; last; }
1.20 albertel 85: }
1.21 albertel 86: &Apache::lonxml::debug("have :$type:$passed:");
87: if ($passed) {
88: $status='SHOW_ANSWER';
89: $datemsg=$date;
90: } elsif ($type eq 'opendate') {
91: $status='CLOSED';
92: $datemsg = "will open on $date";
93: } elsif ($type eq 'duedate') {
94: $status='CAN_ANSWER';
95: $datemsg = "is due at $date";
96: } elsif ($type eq 'answerdate') {
97: $status='CLOSED';
1.32 albertel 98: $datemsg = "was due on $lastdate, and answers will be available on $date";
1.20 albertel 99: }
1.27 albertel 100: if ($status eq 'CAN_ANSWER') {
101: #check #tries
102: my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
103: my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
104: if ( $tries eq '' ) { $tries = '0'; }
105: if ( $maxtries eq '' ) { $maxtries = '2'; }
106: if ($tries >= $maxtries) { $status = 'CANNOT_ANSWER'; }
107: }
1.21 albertel 108: &Apache::lonxml::debug("sending back :$status:$datemsg:");
1.24 albertel 109: if ($ENV{'request.state'} eq "construct") {
110: &Apache::lonxml::debug("in construction ignoring dates");
111: $status='CAN_ANSWER';
112: $datemsg='is in under construction';
113: }
1.21 albertel 114: return ($status,$datemsg);
1.20 albertel 115: }
116:
1.17 www 117: sub handler {
1.18 albertel 118: my $request=$_[0];
1.17 www 119:
1.33 albertel 120: if ( $ENV{'user.name'} eq 'albertel' ) {$Apache::lonxml::debug=1;} else {$Apache::lonxml::debug=0;}
1.18 albertel 121:
122: my (@targets) = &get_target();
123:
1.17 www 124: if ($ENV{'browser.mathml'}) {
125: $request->content_type('text/xml');
126: } else {
127: $request->content_type('text/html');
128: }
129: $request->send_http_header;
1.25 www 130: return OK if $request->header_only;
1.35 ! albertel 131:
! 132: #check if we know where we are
! 133: if ($ENV{'request.course.fn'} && !&Apache::lonnet::symbread()) {
! 134: $request->internal_redirect('/adm/ambiguous'); return;
! 135: }
! 136:
1.17 www 137: &Apache::lonhomework::send_header($request);
1.18 albertel 138:
1.20 albertel 139: #check if allowed to see
140:
1.18 albertel 141: foreach my $target (@targets) {
142: my $file=&Apache::lonnet::filelocation("",$request->uri);
143: my $problem=&Apache::lonnet::getfile($file);
144: if ($problem == -1) {
145: &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
146: $problem='';
147: }
148:
149: my %mystyle;
150: my $result = '';
151: &Apache::inputtags::initialize_inputtags;
152: %Apache::lonhomework::results=();
153: %Apache::lonhomework::history=&Apache::lonnet::restore;
154: #ignore error conditions
155: my ($temp)=keys %Apache::lonhomework::history ;
156: if ($temp =~ m/^error:.*/) { %Apache::lonhomework::history=(); }
1.20 albertel 157: if ($target eq 'web' && &Apache::lonnet::symbread() eq '') {
158: $request->print("Vieiwing this problem as not a student, submissions will be ignored.");
159: }
1.18 albertel 160: if ($target eq 'grade') {
161: my $resultkey;
162: foreach $resultkey (sort keys %Apache::lonhomework::history) {
163: &Apache::lonxml::debug("$resultkey ---- $Apache::lonhomework::history{$resultkey}");
164: }
165: &Apache::lonxml::debug("\n<br>restored values^</br>\n");
166: }
167: my $default=&Apache::lonnet::getfile('/home/httpd/html/res/adm/includes/default_homework.lcpm');
168: if ($default == -1) {
169: &Apache::lonxml::error("<b>Unable to find <i>default_homework.lcpm</i></b>");
170: $default='';
171: }
172: $result = &Apache::lonxml::xmlparse($target, $problem,
173: $default.&setup_vars($target),%mystyle);
1.17 www 174:
1.18 albertel 175: #$request->print("Result follows:");
176: $request->print($result);
177: #$request->print(":Result ends");
178: if ($target eq 'grade') {
1.27 albertel 179:
1.18 albertel 180: my $resultkey;
181: foreach $resultkey (sort keys %Apache::lonhomework::results) {
182: &Apache::lonxml::debug("$resultkey ---- $Apache::lonhomework::results{$resultkey}");
183: }
184: &Apache::lonxml::debug("\n<br>storing values^</br>\n");
185: }
186:
187: my ($temp) = keys %Apache::lonhomework::results;
188: if ( $temp ne '' ) {
189: &Apache::lonxml::debug("Store return message:".&Apache::lonnet::cstore(%Apache::lonhomework::results));
190: }
1.17 www 191: }
192: &Apache::lonhomework::send_footer($request);
1.18 albertel 193:
1.25 www 194: return OK;
1.17 www 195:
1.1 albertel 196: }
197:
198: 1;
199: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>