Annotation of loncom/homework/lonhomework.pm, revision 1.21
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.1 albertel 13:
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';
! 91: $datemsg = "was due as $lastdate, and asnswers will be available at $date";
1.20 albertel 92: }
1.21 ! albertel 93: &Apache::lonxml::debug("sending back :$status:$datemsg:");
! 94: return ($status,$datemsg);
1.20 albertel 95: }
96:
1.17 www 97: sub handler {
1.18 albertel 98: my $request=$_[0];
1.17 www 99:
1.20 albertel 100: if ( $ENV{'user.name'} eq 'albertel' ) { $Apache::lonxml::debug=1; }
1.18 albertel 101:
102: my (@targets) = &get_target();
103:
1.17 www 104: if ($ENV{'browser.mathml'}) {
105: $request->content_type('text/xml');
106: } else {
107: $request->content_type('text/html');
108: }
109: $request->send_http_header;
110: return 'OK' if $request->header_only;
1.18 albertel 111:
1.17 www 112: &Apache::lonhomework::send_header($request);
1.18 albertel 113:
1.20 albertel 114: #check if allowed to see
115:
1.18 albertel 116: foreach my $target (@targets) {
117: my $file=&Apache::lonnet::filelocation("",$request->uri);
118: my $problem=&Apache::lonnet::getfile($file);
119: if ($problem == -1) {
120: &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
121: $problem='';
122: }
123:
124: my %mystyle;
125: my $result = '';
126: &Apache::inputtags::initialize_inputtags;
127: %Apache::lonhomework::results=();
128: %Apache::lonhomework::history=&Apache::lonnet::restore;
129: #ignore error conditions
130: my ($temp)=keys %Apache::lonhomework::history ;
131: if ($temp =~ m/^error:.*/) { %Apache::lonhomework::history=(); }
1.20 albertel 132: if ($target eq 'web' && &Apache::lonnet::symbread() eq '') {
133: $request->print("Vieiwing this problem as not a student, submissions will be ignored.");
134: }
1.18 albertel 135: if ($target eq 'grade') {
136: my $resultkey;
137: foreach $resultkey (sort keys %Apache::lonhomework::history) {
138: &Apache::lonxml::debug("$resultkey ---- $Apache::lonhomework::history{$resultkey}");
139: }
140: &Apache::lonxml::debug("\n<br>restored values^</br>\n");
141: }
142: my $default=&Apache::lonnet::getfile('/home/httpd/html/res/adm/includes/default_homework.lcpm');
143: if ($default == -1) {
144: &Apache::lonxml::error("<b>Unable to find <i>default_homework.lcpm</i></b>");
145: $default='';
146: }
147: if ( $target eq 'grade') {
148: $Apache::lonxml::textredirection = 0;
149: } else {
150: $Apache::lonxml::textredirection = 1;
151: }
152: $result = &Apache::lonxml::xmlparse($target, $problem,
153: $default.&setup_vars($target),%mystyle);
1.17 www 154:
1.18 albertel 155: #$request->print("Result follows:");
156: $request->print($result);
157: #$request->print(":Result ends");
158: if ($target eq 'grade') {
159: my $resultkey;
160: foreach $resultkey (sort keys %Apache::lonhomework::results) {
161: &Apache::lonxml::debug("$resultkey ---- $Apache::lonhomework::results{$resultkey}");
162: }
163: &Apache::lonxml::debug("\n<br>storing values^</br>\n");
164: }
165:
166: my ($temp) = keys %Apache::lonhomework::results;
167: if ( $temp ne '' ) {
168: &Apache::lonxml::debug("Store return message:".&Apache::lonnet::cstore(%Apache::lonhomework::results));
169: }
1.17 www 170: }
171: &Apache::lonhomework::send_footer($request);
1.18 albertel 172:
1.17 www 173: return 'OK';
174:
1.1 albertel 175: }
176:
177: 1;
178: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>