Annotation of loncom/homework/lonhomework.pm, revision 1.53
1.52 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.47 albertel 17: #use Time::HiRes qw( gettimeofday tv_interval );
1.43 albertel 18:
19: sub BEGIN {
20: &Apache::lonxml::register_insert();
21: }
22:
1.5 albertel 23: sub get_target {
1.52 albertel 24: if ( $ENV{'request.state'} eq "published") {
25: if ( defined $ENV{'form.submitted'}) {
26: return ('grade', 'web');
27: } else {
28: return ('web');
29: }
30: } elsif ($ENV{'request.state'} eq "construct") {
31: if ( defined $ENV{'form.preview'}) {
32: if ( defined $ENV{'form.submitted'}) {
33: return ('grade', 'web');
34: } else {
35: return ('web');
36: }
37: } else {
38: if ( $ENV{'form.problemmode'} eq 'View' ) {
1.42 albertel 39: if ( defined $ENV{'form.submitted'}) {
1.52 albertel 40: return ('grade', 'web');
1.42 albertel 41: } else {
1.52 albertel 42: return ('web');
1.42 albertel 43: }
1.52 albertel 44: } elsif ( $ENV{'form.problemmode'} eq 'Edit' ) {
45: if ( $ENV{'form.submitted'} eq 'edit' ) {
46: return ('modified','edit');
1.42 albertel 47: } else {
1.52 albertel 48: return ('edit');
1.42 albertel 49: }
1.52 albertel 50: } else {
51: return ('web');
52: }
1.15 albertel 53: }
1.52 albertel 54: }
55: return ();
1.5 albertel 56: }
57:
1.3 albertel 58: sub setup_vars {
1.52 albertel 59: my ($target) = @_;
60: return ';'
1.11 albertel 61: # return ';$external::target='.$target.';';
1.2 albertel 62: }
63:
64: sub send_header {
1.52 albertel 65: my ($request)= @_;
66: $request->print(&Apache::lontexconvert::header());
1.16 albertel 67: # $request->print('<form name='.$ENV{'form.request.prefix'}.'lonhomework method="POST" action="'.$request->uri.'">');
1.2 albertel 68: }
69:
1.36 albertel 70: sub createmenu {
1.52 albertel 71: my ($which,$request)=@_;
72: if ($which eq 'grade') {
73: $request->print('<script language="JavaScript">
74: hwkmenu=window.open("/res/adm/pages/homeworkmenu.html","homeworkremote",
75: "height=350,width=150,menubar=no");
76: </script>');
77: }
1.36 albertel 78: }
79:
1.2 albertel 80: sub send_footer {
1.52 albertel 81: my ($request)= @_;
1.16 albertel 82: # $request->print('</form>');
1.52 albertel 83: $request->print(&Apache::lontexconvert::footer());
1.2 albertel 84: }
85:
1.52 albertel 86: $Apache::lonxml::browse='';
1.53 ! www 87:
! 88: sub check_access {
1.52 albertel 89: my ($id) = @_;
90: my $date ='';
91: my $status = '';
92: my $datemsg = '';
93: my $lastdate = '';
94: my $temp;
95: my $type;
96: my $passed;
97: &Apache::lonxml::debug("checking for part :$id:");
98: foreach $temp ("opendate","duedate","answerdate") {
99: $lastdate = $date;
100: $date = &Apache::lonnet::EXT("resource.$id.$temp");
101: &Apache::lonxml::debug("found :$date: for :$temp:");
102: if ($date eq '') {
103: $date = "an unknown date"; $passed = 0;
104: } elsif ($date eq 'con_lost') {
105: $date = "an indeterminate date"; $passed = 0;
106: } else {
107: if (time < $date) { $passed = 0; } else { $passed = 1; }
108: $date = localtime $date;
1.51 harris41 109: }
1.52 albertel 110: if (!$passed) { $type=$temp; last; }
111: }
112: &Apache::lonxml::debug("have :$type:$passed:");
113: if ($passed) {
114: $status='SHOW_ANSWER';
115: $datemsg=$date;
116: } elsif ($type eq 'opendate') {
117: $status='CLOSED';
118: $datemsg = "will open on $date";
119: } elsif ($type eq 'duedate') {
120: $status='CAN_ANSWER';
121: $datemsg = "is due at $date";
122: } elsif ($type eq 'answerdate') {
123: $status='CLOSED';
124: $datemsg = "was due on $lastdate, and answers will be available on $date";
125: }
126: if ($status eq 'CAN_ANSWER') {
127: #check #tries
128: my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
129: my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
130: if ( $tries eq '' ) { $tries = '0'; }
131: if ( $maxtries eq '' ) { $maxtries = '2'; }
132: if ($tries >= $maxtries) { $status = 'CANNOT_ANSWER'; }
133: }
134: &Apache::lonxml::debug("sending back :$status:$datemsg:");
135: if (($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED')) {
136: &Apache::lonxml::debug("should be allowed to browse a resource when closed");
137: $status='CAN_ANSWER';
138: $datemsg='is closed but you are allowed to view it';
139: }
140: if ($ENV{'request.state'} eq "construct") {
141: &Apache::lonxml::debug("in construction ignoring dates");
142: $status='CAN_ANSWER';
143: $datemsg='is in under construction';
144: }
145: return ($status,$datemsg);
1.20 albertel 146: }
147:
1.41 albertel 148: sub showhash {
1.52 albertel 149: my (%hash) = @_;
150: my $resultkey;
151: foreach $resultkey (sort keys %hash) {
152: &Apache::lonxml::debug("$resultkey ---- $hash{$resultkey}");
153: }
154: &Apache::lonxml::debug("\n<br />restored values^</br>\n");
155: return '';
1.41 albertel 156: }
157:
158: sub setuppermissions {
1.52 albertel 159: $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
160: $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
161: return ''
1.41 albertel 162: }
163:
164: sub setupheader {
1.52 albertel 165: my $request=$_[0];
166: if ($ENV{'browser.mathml'}) {
167: $request->content_type('text/xml');
168: } else {
169: $request->content_type('text/html');
170: }
171: $request->send_http_header;
172: return OK if $request->header_only;
173: return ''
1.41 albertel 174: }
1.35 albertel 175:
1.47 albertel 176: sub handle_save_or_undo {
1.52 albertel 177: my ($request,$problem,$result) = @_;
178: my $fileout = &Apache::lonnet::filelocation("",$request->uri);
179: my $filesave=$fileout.".bak";
180:
181: if ($ENV{'form.Undo'} eq 'undo') {
182: if (copy($filesave,$fileout)) {
183: $request->print("<b>Undid changes, Copied $filesave to $fileout<br /></b>");
184: } else {
185: $request->print("<font color=\"red\" size=\"+1\"><b>Unable to undo, unable to copy $filesave to $fileout<br /></b></font>");
186: }
187: } else {
188: my $fs=Apache::File->new(">$filesave");
189: if (defined($fs)) {
190: print $fs $$problem;
191: $request->print("<b>Making Backup to $filesave</b><br />");
192: } else {
193: $request->print("<font color=\"red\" size=\"+1\"><b>Unable to make backup $filesave</b></font>");
194: }
195: my $fh=Apache::File->new(">$fileout");
196: if (defined($fh)) {
197: print $fh $$result;
198: $request->print("<b>Saving Modifications to $fileout</b><br />");
1.47 albertel 199: } else {
1.52 albertel 200: $request->print("<font color=\"red\" size=\"+1\"><b>Unable to write to $fileout</b></font>");
1.47 albertel 201: }
1.52 albertel 202: }
1.47 albertel 203: }
204:
1.41 albertel 205: sub renderpage {
1.52 albertel 206: my ($request,$file) = @_;
207:
208: my (@targets) = &get_target();
209: foreach my $target (@targets) {
210: #my $t0 = [&gettimeofday()];
211: my $problem=&Apache::lonnet::getfile($file);
212: if ($problem == -1) {
213: &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
214: $problem='';
215: }
216:
217: my %mystyle;
218: my $result = '';
219: &Apache::inputtags::initialize_inputtags;
220: &Apache::edit::initialize_edit;
221: %Apache::lonhomework::results=();
222: %Apache::lonhomework::history=&Apache::lonnet::restore();
223: #ignore error conditions
224: my ($temp)=keys %Apache::lonhomework::history ;
225: if ($temp =~ m/^error:.*/) { %Apache::lonhomework::history=(); }
226: if ($target eq 'web') {
227: if (&Apache::lonnet::symbread() eq '') {
228: if ($ENV{'request.state'} eq "construct") {
229: $request->print("In construction space, submissions ignored<br />");
1.36 albertel 230: } else {
1.52 albertel 231: $request->print("Browsing or <a href=\"/adm/ambiguous\">ambiguous</a> reference, submissions ignored<br />");
1.51 harris41 232: }
1.52 albertel 233: }
234: #if ($Apache::lonhomework::viewgrades eq 'F') {&createmenu('grade',$request); }
235: }
236: if ($target eq 'grade') { &showhash(%Apache::lonhomework::history); }
237:
238: my $default=&Apache::lonnet::getfile('/home/httpd/html/res/adm/includes/default_homework.lcpm');
239: if ($default == -1) {
240: &Apache::lonxml::error("<b>Unable to find <i>default_homework.lcpm</i></b>");
241: $default='';
242: }
243: $result = &Apache::lonxml::xmlparse($target, $problem,
244: $default.&setup_vars($target),%mystyle);
245:
246: #$request->print("Result follows:");
247: if ($target eq 'modified') {
248: &handle_save_or_undo($request,\$problem,\$result);
249: } else {
250: #my $td=&tv_interval($t0);
251: #if ( $Apache::lonxml::debug) {
252: #$result =~ s:</body>::;
253: #$result.="<br />Spent $td seconds processing target $target\n</body>";
254: #}
255: $request->print($result);
256: }
257: #$request->print(":Result ends");
258: if ($target eq 'grade') { &showhash(%Apache::lonhomework::results); }
259:
260: #store new values
261: my ($temp) = keys %Apache::lonhomework::results;
262: if ( $temp ne '' ) {
263: &Apache::lonxml::debug("Store return message:".
264: &Apache::lonnet::cstore(\%Apache::lonhomework::results));
1.20 albertel 265: }
1.52 albertel 266: #my $td=&tv_interval($t0);
267: }
1.41 albertel 268: }
269:
1.42 albertel 270: # with no arg it returns a HTML <option> list of the template titles
271: # with one arg it returns the filename associated with the arg passed
272: sub get_template_list {
1.52 albertel 273: my ($namewanted,$extension) = @_;
274: my $result;
275: &Apache::lonxml::debug("Looking for :$extension:");
276: foreach my $file (</home/httpd/html/res/adm/includes/templates/*.$extension>) {
277: my $name=&Apache::lonnet::metadata($file,'title');
278: if ($namewanted && ($name eq $namewanted)) {
279: $result=$file;
280: last;
281: } else {
282: $result.="<option>$name</option>";
1.42 albertel 283: }
1.52 albertel 284: }
285: return $result;
1.42 albertel 286: }
287:
288: sub newproblem {
1.52 albertel 289: my ($request) = @_;
290:
291: my $extension=$request->uri;
292: $extension=~s:^.*\.([\w]+)$:$1:;
293: &Apache::lonxml::debug("Looking for :$extension:");
294: if ($ENV{'form.template'}) {
295: use File::Copy;
296: my $file = &get_template_list($ENV{'form.template'},$extension);
297: my $dest = &Apache::lonnet::filelocation("",$request->uri);
298: copy($file,$dest);
299: &renderpage($request,$file);
300: } else {
301: my $templatelist=&get_template_list('',$extension);
302: my $url=$request->uri;
303: my $dest = &Apache::lonnet::filelocation("",$request->uri);
304: $request->print(<<ENDNEWPROBLEM);
1.42 albertel 305: <body bgcolor="#FFFFFF">
1.50 albertel 306: The request file $url doesn\'t exist. You can create a new $extension <br />
1.42 albertel 307: <form action="$url" method="POST">
1.50 albertel 308: <input type="submit" value="New $extension"><br />
1.42 albertel 309: <select name="template">
310: $templatelist
311: </select>
312: </form>
313: </body>
314: ENDNEWPROBLEM
315: }
316:
317: return '';
318: }
319:
320: sub view_or_edit_menu {
1.52 albertel 321: my ($request) = @_;
322: my $url=$request->uri;
323: $request->print(<<EDITMENU);
1.42 albertel 324: <body bgcolor="#FFFFFF">
325: <form action="$url" method="POST">
326: Would you like to <input type="submit" name="problemmode" value="View"> or
327: <input type="submit" name="problemmode" value="Edit"> the problem.
328: </form>
329: </body>
330: EDITMENU
331: }
332:
1.41 albertel 333: sub handler {
1.52 albertel 334: #my $t0 = [&gettimeofday()];
335: my $request=$_[0];
1.41 albertel 336:
1.52 albertel 337: if ( $ENV{'user.name'} eq 'albertel' ) {$Apache::lonxml::debug=1;}
1.41 albertel 338:
339: if (&setupheader($request)) { return OK; }
1.52 albertel 340: $ENV{'request.uri'}=$request->uri;
1.41 albertel 341:
342: #setup permissions
1.52 albertel 343: $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
344: $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
345: &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:");
346:
347: my $file=&Apache::lonnet::filelocation("",$request->uri);
348:
349: #check if we know where we are
350: if ($ENV{'request.course.fn'} && !&Apache::lonnet::symbread()) {
351: # if we are browsing we might not be able to know where we are
352: if ($Apache::lonhomework::browse ne 'F') {
353: #should know where we are, so ask
354: $request->internal_redirect('/adm/ambiguous'); return;
1.41 albertel 355: }
1.52 albertel 356: }
1.41 albertel 357:
1.52 albertel 358: if ($ENV{'request.state'} eq "construct") {
359: if ( -e $file ) {
360: if (!(defined $ENV{'form.problemmode'})) {
361: #first visit to problem in construction space
362: &view_or_edit_menu($request);
363: #&renderpage($request,$file);
364: } else {
365: &renderpage($request,$file);
1.41 albertel 366: }
367: } else {
1.52 albertel 368: # requested file doesn't exist in contruction space
369: &newproblem($request);
1.41 albertel 370: }
1.52 albertel 371: } else {
372: # just render the page normally outside of construction space
373: &renderpage($request,$file);
374: }
375: #my $td=&tv_interval($t0);
376: #&Apache::lonxml::debug("Spent $td seconds processing");
377: # &Apache::lonhomework::send_footer($request);
378: # always turn off debug messages
379: $Apache::lonxml::debug=0;
380: return OK;
381:
1.1 albertel 382: }
383:
384: 1;
385: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>