1: # The LON-CAPA Homework handler
2: # Guy Albertelli
3: # 11/30 Gerd Kortemeyer
4: # 6/1 Gerd Kortemeyer
5:
6: package Apache::lonhomework;
7: use strict;
8: use Apache::style;
9: use Apache::lonxml;
10: use Apache::lonnet;
11: use Apache::inputtags;
12: use Apache::structuretags;
13: use Apache::response;
14: use Apache::hint;
15: use Apache::outputtags;
16: use Apache::Constants qw(:common);
17:
18: sub get_target {
19: if ( $ENV{'request.state'} eq "published") {
20: if ( defined $ENV{'form.submitted'}) {
21: return ('grade', 'web');
22: } else {
23: return ('web');
24: }
25: } elsif ($ENV{'request.state'} eq "construct") {
26: if ( defined $ENV{'form.preview'}) {
27: if ( defined $ENV{'form.submitted'}) {
28: return ('grade', 'web');
29: } else {
30: return ('web');
31: }
32: } else {
33: #return ('edit');
34: #edit is currently broken
35: return ('web');
36: }
37: }
38: return ();
39: }
40:
41: sub setup_vars {
42: my ($target) = @_;
43: return ';'
44: # return ';$external::target='.$target.';';
45: }
46:
47: sub send_header {
48: my ($request)= @_;
49: $request->print(&Apache::lontexconvert::header());
50: $ENV{'request.uri'}=$request->uri;
51: # $request->print('<form name='.$ENV{'form.request.prefix'}.'lonhomework method="POST" action="'.$request->uri.'">');
52: }
53:
54: sub send_footer {
55: my ($request)= @_;
56: # $request->print('</form>');
57: $request->print(&Apache::lontexconvert::footer());
58: }
59:
60: #@Apache::lonhomework::date_status=();
61: #@Apache::lonhomework::date_msg=();
62: sub check_date {
63: my ($id) = @_;
64: my $date ='';
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;
78: } elsif ($date eq 'con_lost') {
79: $date = "an indeterminate date"; $passed = 0;
80: } else {
81: if (time < $date) { $passed = 0; } else { $passed = 1; }
82: $date = localtime $date;
83: }
84: if (!$passed) { $type=$temp; last; }
85: }
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';
98: $datemsg = "was due on $lastdate, and answers will be available on $date";
99: }
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: }
108: &Apache::lonxml::debug("sending back :$status:$datemsg:");
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: }
114: return ($status,$datemsg);
115: }
116:
117: sub handler {
118: my $request=$_[0];
119:
120: if ( $ENV{'user.name'} eq 'albertel' ) {$Apache::lonxml::debug=1;} else {$Apache::lonxml::debug=0;}
121:
122: my (@targets) = &get_target();
123:
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;
130: return OK if $request->header_only;
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:
137: &Apache::lonhomework::send_header($request);
138:
139: #check if allowed to see
140:
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=(); }
157: if ($target eq 'web' && &Apache::lonnet::symbread() eq '') {
158: $request->print("Vieiwing this problem as not a student, submissions will be ignored.");
159: }
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);
174:
175: #$request->print("Result follows:");
176: $request->print($result);
177: #$request->print(":Result ends");
178: if ($target eq 'grade') {
179:
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: }
191: }
192: &Apache::lonhomework::send_footer($request);
193:
194: return OK;
195:
196: }
197:
198: 1;
199: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>