Annotation of loncom/homework/lonhomework.pm, revision 1.55
1.52 albertel 1: # The LON-CAPA Homework handler
2: # Guy Albertelli
1.17 www 3: # 11/30 Gerd Kortemeyer
1.54 www 4: # 6/1,8/17 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: }
1.54 www 134:
1.55 ! www 135: if (($status ne 'CLOSED') && ($Apache::lonhomework::type eq 'exam')) {
1.54 www 136: return ('UNCHECKEDOUT','needs to be checked out');
137: }
138:
139:
1.52 albertel 140: &Apache::lonxml::debug("sending back :$status:$datemsg:");
141: if (($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED')) {
142: &Apache::lonxml::debug("should be allowed to browse a resource when closed");
143: $status='CAN_ANSWER';
144: $datemsg='is closed but you are allowed to view it';
145: }
146: if ($ENV{'request.state'} eq "construct") {
147: &Apache::lonxml::debug("in construction ignoring dates");
148: $status='CAN_ANSWER';
149: $datemsg='is in under construction';
150: }
151: return ($status,$datemsg);
1.20 albertel 152: }
153:
1.41 albertel 154: sub showhash {
1.52 albertel 155: my (%hash) = @_;
156: my $resultkey;
157: foreach $resultkey (sort keys %hash) {
158: &Apache::lonxml::debug("$resultkey ---- $hash{$resultkey}");
159: }
160: &Apache::lonxml::debug("\n<br />restored values^</br>\n");
161: return '';
1.41 albertel 162: }
163:
164: sub setuppermissions {
1.52 albertel 165: $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
166: $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
167: return ''
1.41 albertel 168: }
169:
170: sub setupheader {
1.52 albertel 171: my $request=$_[0];
172: if ($ENV{'browser.mathml'}) {
173: $request->content_type('text/xml');
174: } else {
175: $request->content_type('text/html');
176: }
177: $request->send_http_header;
178: return OK if $request->header_only;
179: return ''
1.41 albertel 180: }
1.35 albertel 181:
1.47 albertel 182: sub handle_save_or_undo {
1.52 albertel 183: my ($request,$problem,$result) = @_;
184: my $fileout = &Apache::lonnet::filelocation("",$request->uri);
185: my $filesave=$fileout.".bak";
186:
187: if ($ENV{'form.Undo'} eq 'undo') {
188: if (copy($filesave,$fileout)) {
189: $request->print("<b>Undid changes, Copied $filesave to $fileout<br /></b>");
190: } else {
191: $request->print("<font color=\"red\" size=\"+1\"><b>Unable to undo, unable to copy $filesave to $fileout<br /></b></font>");
192: }
193: } else {
194: my $fs=Apache::File->new(">$filesave");
195: if (defined($fs)) {
196: print $fs $$problem;
197: $request->print("<b>Making Backup to $filesave</b><br />");
198: } else {
199: $request->print("<font color=\"red\" size=\"+1\"><b>Unable to make backup $filesave</b></font>");
200: }
201: my $fh=Apache::File->new(">$fileout");
202: if (defined($fh)) {
203: print $fh $$result;
204: $request->print("<b>Saving Modifications to $fileout</b><br />");
1.47 albertel 205: } else {
1.52 albertel 206: $request->print("<font color=\"red\" size=\"+1\"><b>Unable to write to $fileout</b></font>");
1.47 albertel 207: }
1.52 albertel 208: }
1.47 albertel 209: }
210:
1.41 albertel 211: sub renderpage {
1.52 albertel 212: my ($request,$file) = @_;
213:
214: my (@targets) = &get_target();
215: foreach my $target (@targets) {
216: #my $t0 = [&gettimeofday()];
217: my $problem=&Apache::lonnet::getfile($file);
218: if ($problem == -1) {
219: &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
220: $problem='';
221: }
222:
223: my %mystyle;
224: my $result = '';
225: &Apache::inputtags::initialize_inputtags;
226: &Apache::edit::initialize_edit;
227: %Apache::lonhomework::results=();
228: %Apache::lonhomework::history=&Apache::lonnet::restore();
229: #ignore error conditions
230: my ($temp)=keys %Apache::lonhomework::history ;
231: if ($temp =~ m/^error:.*/) { %Apache::lonhomework::history=(); }
232: if ($target eq 'web') {
233: if (&Apache::lonnet::symbread() eq '') {
234: if ($ENV{'request.state'} eq "construct") {
235: $request->print("In construction space, submissions ignored<br />");
1.36 albertel 236: } else {
1.52 albertel 237: $request->print("Browsing or <a href=\"/adm/ambiguous\">ambiguous</a> reference, submissions ignored<br />");
1.51 harris41 238: }
1.52 albertel 239: }
240: #if ($Apache::lonhomework::viewgrades eq 'F') {&createmenu('grade',$request); }
241: }
242: if ($target eq 'grade') { &showhash(%Apache::lonhomework::history); }
243:
244: my $default=&Apache::lonnet::getfile('/home/httpd/html/res/adm/includes/default_homework.lcpm');
245: if ($default == -1) {
246: &Apache::lonxml::error("<b>Unable to find <i>default_homework.lcpm</i></b>");
247: $default='';
248: }
249: $result = &Apache::lonxml::xmlparse($target, $problem,
250: $default.&setup_vars($target),%mystyle);
251:
252: #$request->print("Result follows:");
253: if ($target eq 'modified') {
254: &handle_save_or_undo($request,\$problem,\$result);
255: } else {
256: #my $td=&tv_interval($t0);
257: #if ( $Apache::lonxml::debug) {
258: #$result =~ s:</body>::;
259: #$result.="<br />Spent $td seconds processing target $target\n</body>";
260: #}
261: $request->print($result);
262: }
263: #$request->print(":Result ends");
264: if ($target eq 'grade') { &showhash(%Apache::lonhomework::results); }
265:
266: #store new values
267: my ($temp) = keys %Apache::lonhomework::results;
268: if ( $temp ne '' ) {
269: &Apache::lonxml::debug("Store return message:".
270: &Apache::lonnet::cstore(\%Apache::lonhomework::results));
1.20 albertel 271: }
1.52 albertel 272: #my $td=&tv_interval($t0);
273: }
1.41 albertel 274: }
275:
1.42 albertel 276: # with no arg it returns a HTML <option> list of the template titles
277: # with one arg it returns the filename associated with the arg passed
278: sub get_template_list {
1.52 albertel 279: my ($namewanted,$extension) = @_;
280: my $result;
281: &Apache::lonxml::debug("Looking for :$extension:");
282: foreach my $file (</home/httpd/html/res/adm/includes/templates/*.$extension>) {
283: my $name=&Apache::lonnet::metadata($file,'title');
284: if ($namewanted && ($name eq $namewanted)) {
285: $result=$file;
286: last;
287: } else {
288: $result.="<option>$name</option>";
1.42 albertel 289: }
1.52 albertel 290: }
291: return $result;
1.42 albertel 292: }
293:
294: sub newproblem {
1.52 albertel 295: my ($request) = @_;
296:
297: my $extension=$request->uri;
298: $extension=~s:^.*\.([\w]+)$:$1:;
299: &Apache::lonxml::debug("Looking for :$extension:");
300: if ($ENV{'form.template'}) {
301: use File::Copy;
302: my $file = &get_template_list($ENV{'form.template'},$extension);
303: my $dest = &Apache::lonnet::filelocation("",$request->uri);
304: copy($file,$dest);
305: &renderpage($request,$file);
306: } else {
307: my $templatelist=&get_template_list('',$extension);
308: my $url=$request->uri;
309: my $dest = &Apache::lonnet::filelocation("",$request->uri);
310: $request->print(<<ENDNEWPROBLEM);
1.42 albertel 311: <body bgcolor="#FFFFFF">
1.50 albertel 312: The request file $url doesn\'t exist. You can create a new $extension <br />
1.42 albertel 313: <form action="$url" method="POST">
1.50 albertel 314: <input type="submit" value="New $extension"><br />
1.42 albertel 315: <select name="template">
316: $templatelist
317: </select>
318: </form>
319: </body>
320: ENDNEWPROBLEM
321: }
322:
323: return '';
324: }
325:
326: sub view_or_edit_menu {
1.52 albertel 327: my ($request) = @_;
328: my $url=$request->uri;
329: $request->print(<<EDITMENU);
1.42 albertel 330: <body bgcolor="#FFFFFF">
331: <form action="$url" method="POST">
332: Would you like to <input type="submit" name="problemmode" value="View"> or
333: <input type="submit" name="problemmode" value="Edit"> the problem.
334: </form>
335: </body>
336: EDITMENU
337: }
338:
1.41 albertel 339: sub handler {
1.52 albertel 340: #my $t0 = [&gettimeofday()];
341: my $request=$_[0];
1.41 albertel 342:
1.52 albertel 343: if ( $ENV{'user.name'} eq 'albertel' ) {$Apache::lonxml::debug=1;}
1.41 albertel 344:
345: if (&setupheader($request)) { return OK; }
1.52 albertel 346: $ENV{'request.uri'}=$request->uri;
1.41 albertel 347:
348: #setup permissions
1.52 albertel 349: $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
350: $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
351: &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:");
352:
353: my $file=&Apache::lonnet::filelocation("",$request->uri);
354:
355: #check if we know where we are
356: if ($ENV{'request.course.fn'} && !&Apache::lonnet::symbread()) {
357: # if we are browsing we might not be able to know where we are
358: if ($Apache::lonhomework::browse ne 'F') {
359: #should know where we are, so ask
360: $request->internal_redirect('/adm/ambiguous'); return;
1.41 albertel 361: }
1.52 albertel 362: }
1.41 albertel 363:
1.52 albertel 364: if ($ENV{'request.state'} eq "construct") {
365: if ( -e $file ) {
366: if (!(defined $ENV{'form.problemmode'})) {
367: #first visit to problem in construction space
368: &view_or_edit_menu($request);
369: #&renderpage($request,$file);
370: } else {
371: &renderpage($request,$file);
1.41 albertel 372: }
373: } else {
1.52 albertel 374: # requested file doesn't exist in contruction space
375: &newproblem($request);
1.41 albertel 376: }
1.52 albertel 377: } else {
378: # just render the page normally outside of construction space
379: &renderpage($request,$file);
380: }
381: #my $td=&tv_interval($t0);
382: #&Apache::lonxml::debug("Spent $td seconds processing");
383: # &Apache::lonhomework::send_footer($request);
384: # always turn off debug messages
385: $Apache::lonxml::debug=0;
386: return OK;
387:
1.1 albertel 388: }
389:
390: 1;
391: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>