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