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