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