Annotation of loncom/homework/lonhomework.pm, revision 1.41
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.40 albertel 33: return ('grade','web');
34: if ( $ENV{'form.submitted'} eq 'edit' ) {
35: return ('modified','edit');
36: } else {
37: return ('edit');
38: }
1.15 albertel 39: }
40: }
1.18 albertel 41: return ();
1.5 albertel 42: }
43:
1.3 albertel 44: sub setup_vars {
1.5 albertel 45: my ($target) = @_;
1.11 albertel 46: return ';'
47: # return ';$external::target='.$target.';';
1.2 albertel 48: }
49:
50: sub send_header {
51: my ($request)= @_;
1.14 albertel 52: $request->print(&Apache::lontexconvert::header());
1.16 albertel 53: $ENV{'request.uri'}=$request->uri;
54: # $request->print('<form name='.$ENV{'form.request.prefix'}.'lonhomework method="POST" action="'.$request->uri.'">');
1.2 albertel 55: }
56:
1.36 albertel 57: sub createmenu {
58: my ($which,$request)=@_;
59: if ($which eq 'grade') {
60: $request->print('<script language="JavaScript">
1.38 albertel 61: hwkmenu=window.open("/res/adm/pages/homeworkmenu.html","homeworkremote",
1.36 albertel 62: "height=350,width=150,menubar=no");
63: </script>');
64: }
65: }
66:
1.2 albertel 67: sub send_footer {
68: my ($request)= @_;
1.16 albertel 69: # $request->print('</form>');
1.14 albertel 70: $request->print(&Apache::lontexconvert::footer());
1.2 albertel 71: }
72:
1.36 albertel 73: $Apache::lonxml::browse='';
1.20 albertel 74: sub check_date {
1.21 albertel 75: my ($id) = @_;
1.20 albertel 76: my $date ='';
1.21 albertel 77: my $status = '';
78: my $datemsg = '';
79: my $lastdate = '';
80: my $temp;
81: my $type;
82: my $passed;
83: &Apache::lonxml::debug("checking for part :$id:");
84: foreach $temp ("opendate","duedate","answerdate") {
85: $lastdate = $date;
86: $date = &Apache::lonnet::EXT("resource.$id.$temp");
87: &Apache::lonxml::debug("found :$date: for :$temp:");
88: if ($date eq '') {
89: $date = "an unknown date"; $passed = 0;
1.30 albertel 90: } elsif ($date eq 'con_lost') {
91: $date = "an indeterminate date"; $passed = 0;
1.21 albertel 92: } else {
93: if (time < $date) { $passed = 0; } else { $passed = 1; }
94: $date = localtime $date;
95: }
96: if (!$passed) { $type=$temp; last; }
1.20 albertel 97: }
1.21 albertel 98: &Apache::lonxml::debug("have :$type:$passed:");
99: if ($passed) {
100: $status='SHOW_ANSWER';
101: $datemsg=$date;
102: } elsif ($type eq 'opendate') {
103: $status='CLOSED';
104: $datemsg = "will open on $date";
105: } elsif ($type eq 'duedate') {
106: $status='CAN_ANSWER';
107: $datemsg = "is due at $date";
108: } elsif ($type eq 'answerdate') {
109: $status='CLOSED';
1.32 albertel 110: $datemsg = "was due on $lastdate, and answers will be available on $date";
1.20 albertel 111: }
1.27 albertel 112: if ($status eq 'CAN_ANSWER') {
113: #check #tries
114: my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
115: my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
116: if ( $tries eq '' ) { $tries = '0'; }
117: if ( $maxtries eq '' ) { $maxtries = '2'; }
118: if ($tries >= $maxtries) { $status = 'CANNOT_ANSWER'; }
119: }
1.21 albertel 120: &Apache::lonxml::debug("sending back :$status:$datemsg:");
1.36 albertel 121: if (($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED')) {
122: &Apache::lonxml::debug("should be allowed to browse a resource when closed");
123: $status='CAN_ANSWER';
124: $datemsg='is closed but you are allowed to view it';
125: }
1.24 albertel 126: if ($ENV{'request.state'} eq "construct") {
127: &Apache::lonxml::debug("in construction ignoring dates");
128: $status='CAN_ANSWER';
129: $datemsg='is in under construction';
130: }
1.21 albertel 131: return ($status,$datemsg);
1.20 albertel 132: }
133:
1.41 ! albertel 134: sub showhash {
! 135: my (%hash) = @_;
! 136: my $resultkey;
! 137: foreach $resultkey (sort keys %hash) {
! 138: &Apache::lonxml::debug("$resultkey ---- $hash{$resultkey}");
! 139: }
! 140: &Apache::lonxml::debug("\n<br />restored values^</br>\n");
! 141: return '';
! 142: }
! 143:
! 144: sub setuppermissions {
! 145: $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
! 146: $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
! 147: return ''
! 148: }
! 149:
! 150: sub setupheader {
1.18 albertel 151: my $request=$_[0];
1.17 www 152: if ($ENV{'browser.mathml'}) {
153: $request->content_type('text/xml');
154: } else {
155: $request->content_type('text/html');
156: }
157: $request->send_http_header;
1.25 www 158: return OK if $request->header_only;
1.41 ! albertel 159: &Apache::lonhomework::send_header($request);
! 160: return ''
! 161: }
1.35 albertel 162:
1.41 ! albertel 163: sub renderpage {
! 164: my ($request,$file) = @_;
1.36 albertel 165:
1.41 ! albertel 166: my (@targets) = &get_target();
1.18 albertel 167: foreach my $target (@targets) {
168: my $problem=&Apache::lonnet::getfile($file);
169: if ($problem == -1) {
170: &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
171: $problem='';
172: }
1.41 ! albertel 173:
1.18 albertel 174: my %mystyle;
175: my $result = '';
176: &Apache::inputtags::initialize_inputtags;
177: %Apache::lonhomework::results=();
1.40 albertel 178: %Apache::lonhomework::history=&Apache::lonnet::restore();
1.18 albertel 179: #ignore error conditions
180: my ($temp)=keys %Apache::lonhomework::history ;
181: if ($temp =~ m/^error:.*/) { %Apache::lonhomework::history=(); }
1.36 albertel 182: if ($target eq 'web') {
183: if (&Apache::lonnet::symbread() eq '') {
184: if ($ENV{'request.state'} eq "construct") {
1.39 albertel 185: $request->print("In construction space, submissions ignored<br />");
1.36 albertel 186: } else {
1.39 albertel 187: $request->print("Browsing or <a href=\"/adm/ambiguous\">ambiguous</a> reference, submissions ignored<br />");
1.36 albertel 188: }
189: }
190: if ($Apache::lonhomework::viewgrades eq 'F') {&createmenu('grade',$request); }
1.20 albertel 191: }
1.41 ! albertel 192: if ($target eq 'grade') { &showhash(%Apache::lonhomework::history); }
! 193:
1.18 albertel 194: my $default=&Apache::lonnet::getfile('/home/httpd/html/res/adm/includes/default_homework.lcpm');
195: if ($default == -1) {
196: &Apache::lonxml::error("<b>Unable to find <i>default_homework.lcpm</i></b>");
197: $default='';
198: }
199: $result = &Apache::lonxml::xmlparse($target, $problem,
200: $default.&setup_vars($target),%mystyle);
1.17 www 201:
1.18 albertel 202: #$request->print("Result follows:");
1.40 albertel 203: if ($target eq 'modified') {
204: my $fileout = &Apache::lonnet::filelocation("",$request->uri);
205: my $filesave=$fileout.".bak";
206:
207: my $fs=Apache::File->new(">$filesave");
208: print $fs $problem;
209: $request->print("<b>Making Backup to $filesave</b><br />");
210:
211: my $fh=Apache::File->new(">$fileout");
212: print $fh $result;
213: $request->print("<b>Saving Modifications to $fileout</b><br />");
214: } else {
215: $request->print($result);
216: }
1.18 albertel 217: #$request->print(":Result ends");
1.41 ! albertel 218: if ($target eq 'grade') { &showhash(%Apache::lonhomework::results); }
1.27 albertel 219:
1.41 ! albertel 220: #store new values
1.18 albertel 221: my ($temp) = keys %Apache::lonhomework::results;
222: if ( $temp ne '' ) {
1.41 ! albertel 223: &Apache::lonxml::debug("Store return message:".
! 224: &Apache::lonnet::cstore(\%Apache::lonhomework::results));
1.18 albertel 225: }
1.17 www 226: }
1.41 ! albertel 227: }
! 228:
! 229: sub handler {
! 230: my $request=$_[0];
! 231:
! 232: if ( $ENV{'user.name'} eq 'albertel' ) {$Apache::lonxml::debug=1;}
! 233:
! 234: if (&setupheader($request)) { return OK; }
! 235:
! 236: #setup permissions
! 237: $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
! 238: $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
! 239: &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:");
! 240:
! 241: my $file=&Apache::lonnet::filelocation("",$request->uri);
! 242:
! 243: #check if we know where we are
! 244: if ($ENV{'request.course.fn'} && !&Apache::lonnet::symbread()) {
! 245: # if we are browsing we might not be able to know where we are
! 246: if ($Apache::lonhomework::browse ne 'F') {
! 247: #should know where we are, so ask
! 248: $request->internal_redirect('/adm/ambiguous'); return;
! 249: }
! 250: }
! 251:
! 252: if ($ENV{'request.state'} eq "construct") {
! 253: if ( -e $file ) {
! 254: if (!(defined $ENV{'form.showproblem'})) {
! 255: #first visit to problem in construction space
! 256: &view_or_edit_menu($request);
! 257: } else {
! 258: &renderpage($request,$file);
! 259: }
! 260: } else {
! 261: # requested file doesn't exist in contruction space
! 262: &newproblem($request);
! 263: }
! 264: } else {
! 265: # just render the page normally outside of construction space
! 266: &renderpage($request,$file);
! 267: }
! 268:
1.17 www 269: &Apache::lonhomework::send_footer($request);
1.41 ! albertel 270: # always turn off debug messages
1.40 albertel 271: $Apache::lonxml::debug=0;
1.25 www 272: return OK;
1.17 www 273:
1.1 albertel 274: }
275:
276: 1;
277: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>