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