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