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