File:
[LON-CAPA] /
loncom /
homework /
lonhomework.pm
Revision
1.78:
download - view:
text,
annotated -
select for diffs
Thu May 23 21:12:44 2002 UTC (22 years, 3 months ago) by
albertel
Branches:
MAIN
CVS tags:
HEAD
- &Apache::lonxml::xmlparse now wants a request obect passed along
- modified all known calls of xmlparse to take pass a request object
- added code to timeout a <script> block that runs more that the configured timeout parameter of seconds. (LON-CAPA defaults to 300)
1: # The LearningOnline Network with CAPA
2: # The LON-CAPA Homework handler
3: #
4: # $Id: lonhomework.pm,v 1.78 2002/05/23 21:12:44 albertel Exp $
5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28: # Guy Albertelli
29: # 11/30 Gerd Kortemeyer
30: # 6/1,8/17,8/18 Gerd Kortemeyer
31:
32: package Apache::lonhomework;
33: use strict;
34: use Apache::style();
35: use Apache::lonxml();
36: use Apache::lonnet();
37: use Apache::lonplot();
38: use Apache::inputtags();
39: use Apache::structuretags();
40: use Apache::randomlabel();
41: use Apache::response();
42: use Apache::hint();
43: use Apache::outputtags();
44: use Apache::Constants qw(:common);
45: use HTML::Entities();
46: #use Time::HiRes qw( gettimeofday tv_interval );
47:
48: BEGIN {
49: &Apache::lonxml::register_insert();
50: }
51:
52: sub get_target {
53: if ( $ENV{'request.state'} eq "published") {
54: if ( defined($ENV{'form.grade_target'})
55: && ($Apache::lonhomework::viewgrades == 'F' )) {
56: return ($ENV{'form.grade_target'});
57: }
58: if ( defined($ENV{'form.submitted'})) {
59: return ('grade', 'web');
60: } else {
61: return ('web');
62: }
63: } elsif ($ENV{'request.state'} eq "construct") {
64: if ( defined($ENV{'form.grade_target'}) ) {
65: return ($ENV{'form.grade_target'});
66: }
67: if ( defined($ENV{'form.preview'})) {
68: if ( defined($ENV{'form.submitted'})) {
69: return ('grade', 'web');
70: } else {
71: return ('web');
72: }
73: } else {
74: if ( $ENV{'form.problemmode'} eq 'View' ) {
75: if ( defined($ENV{'form.submitted'}) &&
76: (!defined($ENV{'form.resetdata'})) ) {
77: return ('grade', 'web','answer');
78: } else {
79: return ('web','answer');
80: }
81: } elsif ( $ENV{'form.problemmode'} eq 'Edit' ) {
82: if ( $ENV{'form.submitted'} eq 'edit' ) {
83: return ('modified','edit');
84: } else {
85: return ('edit');
86: }
87: } else {
88: return ('web');
89: }
90: }
91: }
92: return ();
93: }
94:
95: sub setup_vars {
96: my ($target) = @_;
97: return ';'
98: # return ';$external::target='.$target.';';
99: }
100:
101: sub send_header {
102: my ($request)= @_;
103: $request->print(&Apache::lontexconvert::header());
104: # $request->print('<form name='.$ENV{'form.request.prefix'}.'lonhomework method="POST" action="'.$request->uri.'">');
105: }
106:
107: sub createmenu {
108: my ($which,$request)=@_;
109: if ($which eq 'grade') {
110: $request->print('<script language="JavaScript">
111: hwkmenu=window.open("/res/adm/pages/homeworkmenu.html","homeworkremote",
112: "height=350,width=150,menubar=no");
113: </script>');
114: }
115: }
116:
117: sub send_footer {
118: my ($request)= @_;
119: # $request->print('</form>');
120: $request->print(&Apache::lontexconvert::footer());
121: }
122:
123: $Apache::lonxml::browse='';
124:
125: sub check_access {
126: my ($id) = @_;
127: my $date ='';
128: my $status = '';
129: my $datemsg = '';
130: my $lastdate = '';
131: my $temp;
132: my $type;
133: my $passed;
134: &Apache::lonxml::debug("checking for part :$id:");
135: &Apache::lonxml::debug("time:".time);
136: foreach $temp ("opendate","duedate","answerdate") {
137: $lastdate = $date;
138: $date = &Apache::lonnet::EXT("resource.$id.$temp");
139: &Apache::lonxml::debug("found :$date: for :$temp:");
140: if ($date eq '') {
141: $date = "an unknown date"; $passed = 0;
142: } elsif ($date eq 'con_lost') {
143: $date = "an indeterminate date"; $passed = 0;
144: } else {
145: if (time < $date) { $passed = 0; } else { $passed = 1; }
146: $date = localtime $date;
147: }
148: if (!$passed) { $type=$temp; last; }
149: }
150: &Apache::lonxml::debug("have :$type:$passed:");
151: if ($passed) {
152: $status='SHOW_ANSWER';
153: $datemsg=$date;
154: } elsif ($type eq 'opendate') {
155: $status='CLOSED';
156: $datemsg = "will open on $date";
157: } elsif ($type eq 'duedate') {
158: $status='CAN_ANSWER';
159: $datemsg = "is due at $date";
160: } elsif ($type eq 'answerdate') {
161: $status='CLOSED';
162: $datemsg = "was due on $lastdate, and answers will be available on $date";
163: }
164: if ($status eq 'CAN_ANSWER') {
165: #check #tries
166: my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
167: my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
168: if ( $tries eq '' ) { $tries = '0'; }
169: if ( $maxtries eq '' ) { $maxtries = '2'; }
170: if ($tries >= $maxtries) { $status = 'CANNOT_ANSWER'; }
171: }
172:
173: if (($status ne 'CLOSED') && ($Apache::lonhomework::type eq 'exam') &&
174: (!$Apache::lonhomework::history{"resource.0.outtoken"})) {
175: return ('UNCHECKEDOUT','needs to be checked out');
176: }
177:
178:
179: &Apache::lonxml::debug("sending back :$status:$datemsg:");
180: if (($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED')) {
181: &Apache::lonxml::debug("should be allowed to browse a resource when closed");
182: $status='CAN_ANSWER';
183: $datemsg='is closed but you are allowed to view it';
184: }
185: if ($ENV{'request.state'} eq "construct") {
186: &Apache::lonxml::debug("in construction ignoring dates");
187: $status='CAN_ANSWER';
188: $datemsg='is in under construction';
189: }
190: return ($status,$datemsg);
191: }
192:
193: sub showhash {
194: my (%hash) = @_;
195: my $resultkey;
196: foreach $resultkey (sort keys %hash) {
197: if (ref($hash{$resultkey})) {
198: if ($hash{$resultkey} =~ /ARRAY/ ) {
199: my $string="$resultkey ---- (";
200: foreach my $elm (@{ $hash{$resultkey} }) {
201: $string.="$elm,";
202: }
203: chop($string);
204: &Apache::lonxml::debug("$string)");
205: } else {
206: &Apache::lonxml::debug("$resultkey ---- $hash{$resultkey}");
207: }
208: } else {
209: &Apache::lonxml::debug("$resultkey ---- $hash{$resultkey}");
210: }
211: }
212: &Apache::lonxml::debug("\n<br />restored values^</br>\n");
213: return '';
214: }
215:
216: sub setuppermissions {
217: $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
218: $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
219: return ''
220: }
221:
222: sub setupheader {
223: my $request=$_[0];
224: if ($ENV{'browser.mathml'}) {
225: $request->content_type('text/xml');
226: } else {
227: $request->content_type('text/html');
228: }
229: if (!$Apache::lonxml::debug && ($ENV{'REQUEST_METHOD'} eq 'GET')) {
230: &Apache::loncommon::no_cache($request);
231: }
232: $request->send_http_header;
233: return OK if $request->header_only;
234: return ''
235: }
236:
237: sub handle_save_or_undo {
238: my ($request,$problem,$result) = @_;
239: my $file = &Apache::lonnet::filelocation("",$request->uri);
240: my $filebak =$file.".bak";
241: my $filetmp =$file.".tmp";
242: my $error=0;
243:
244: if ($ENV{'form.Undo'} eq 'undo') {
245: my $error=0;
246: if (!copy($file,$filetmp)) { $error=1; }
247: if ((!$error) && (!copy($filebak,$file))) { $error=1; }
248: if ((!$error) && (!move($filetmp,$filebak))) { $error=1; }
249: if (!$error) {
250: $request->print("<p><b>Undid changes, Switched $filebak and $file</b></p>");
251: } else {
252: $request->print("<p><font color=\"red\" size=\"+1\"><b>Unable to undo, unable to switch $filebak and $file</b></font></p>");
253: $error=1;
254: }
255: } else {
256: my $fs=Apache::File->new(">$filebak");
257: if (defined($fs)) {
258: print $fs $$problem;
259: $request->print("<b>Making Backup to $filebak</b><br />");
260: } else {
261: $request->print("<font color=\"red\" size=\"+1\"><b>Unable to make backup $filebak</b></font>");
262: $error=2;
263: }
264: my $fh=Apache::File->new(">$file");
265: if (defined($fh)) {
266: print $fh $$result;
267: $request->print("<b>Saving Modifications to $file</b><br />");
268: } else {
269: $request->print("<font color=\"red\" size=\"+1\"><b>Unable to write to $file</b></font>");
270: $error|=4;
271: }
272: }
273: return $error;
274: }
275:
276: sub analyze {
277: my ($request,$file) = @_;
278: &Apache::lonxml::debug("Analyze");
279: my $result=&Apache::lonnet::ssi($request->uri,('grade_target' => 'analyze'));
280: &Apache::lonxml::debug(":$result:");
281: (my $garbage,$result)=split(/_HASH_REF__/,$result,2);
282: &showhash(&Apache::lonnet::str2hash($result));
283: return $result;
284: }
285:
286: sub editxmlmode {
287: my ($request,$file) = @_;
288: my $result;
289: my $problem=&Apache::lonnet::getfile($file);
290: if ($problem == -1) {
291: &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
292: $problem='';
293: }
294: if (defined($ENV{'form.editxmltext'}) || defined($ENV{'form.Undo'})) {
295: my $error=&handle_save_or_undo($request,\$problem,
296: \$ENV{'form.editxmltext'});
297: if (!$error) { $problem=&Apache::lonnet::getfile($file); }
298: }
299: my ($rows,$cols) = &Apache::edit::textarea_sizes(\$problem);
300: if ($cols > 80) { $cols = 80; }
301: $result.='<html><body bgcolor="#FFFFFF">
302: <form name="lonhomework" method="POST" action="'.
303: $ENV{'request.uri'}.'">
304: <input type="hidden" name="problemmode" value="EditXML" />
305: <input type="submit" name="problemmode" value="View" />
306: <input type="submit" name="problemmode" value="Edit" />
307: <hr />
308: <input type="submit" name="submit" value="Submit Changes" />
309: <input type="submit" name="Undo" value="undo" />
310: <hr />
311: <textarea rows="'.$rows.'" cols="'.$cols.'" name="editxmltext">'.
312: &HTML::Entities::encode($problem).'</textarea>
313: </form></body></html>';
314: $request->print($result);
315: return '';
316: }
317:
318: sub renderpage {
319: my ($request,$file) = @_;
320:
321: my (@targets) = &get_target();
322: &Apache::lonxml::debug("Running targets ".join(':',@targets));
323: foreach my $target (@targets) {
324: #my $t0 = [&gettimeofday()];
325: my $problem=&Apache::lonnet::getfile($file);
326: if ($problem == -1) {
327: &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
328: $problem='';
329: }
330:
331: my %mystyle;
332: my $result = '';
333: &Apache::inputtags::initialize_inputtags;
334: &Apache::edit::initialize_edit;
335: if ($target eq 'analyze') { %Apache::lonhomework::anaylze=(); }
336: if ($target eq 'web') {
337: my ($symb)=&Apache::lonxml::whichuser();
338: if ($symb eq '') {
339: if ($ENV{'request.state'} eq "construct") {
340: } else {
341: $request->print("Browsing or <a href=\"/adm/ambiguous\">ambiguous</a> reference, submissions ignored<br />");
342: }
343: }
344: #if ($Apache::lonhomework::viewgrades eq 'F') {&createmenu('grade',$request); }
345: }
346: #if ($target eq 'grade') { &showhash(%Apache::lonhomework::history); }
347:
348: my $default=&Apache::lonnet::getfile('/home/httpd/html/res/adm/includes/default_homework.lcpm');
349: if ($default == -1) {
350: &Apache::lonxml::error("<b>Unable to find <i>default_homework.lcpm</i></b>");
351: $default='';
352: }
353: &Apache::lonxml::debug("Should be parsing now");
354: $result = &Apache::lonxml::xmlparse($request, $target, $problem,
355: $default.&setup_vars($target),%mystyle);
356:
357: #$request->print("Result follows:");
358: if ($target eq 'modified') {
359: &handle_save_or_undo($request,\$problem,\$result);
360: } else {
361: if ($target eq 'analyze') {
362: $result=&Apache::lonnet::hashref2str(\%Apache::lonhomework::analyze);
363: undef(%Apache::lonhomework::analyze);
364: }
365: #my $td=&tv_interval($t0);
366: #if ( $Apache::lonxml::debug) {
367: #$result =~ s:</body>::;
368: #$result.="<br />Spent $td seconds processing target $target\n</body>";
369: #}
370: $request->print($result);
371: }
372: #$request->print(":Result ends");
373: #my $td=&tv_interval($t0);
374: }
375: }
376:
377: # with no arg it returns a HTML <option> list of the template titles
378: # with one arg it returns the filename associated with the arg passed
379: sub get_template_list {
380: my ($namewanted,$extension) = @_;
381: my $result;
382: &Apache::lonxml::debug("Looking for :$extension:");
383: foreach my $file (</home/httpd/html/res/adm/includes/templates/*.$extension>) {
384: my $name=&Apache::lonnet::metadata($file,'title');
385: if ($namewanted && ($name eq $namewanted)) {
386: $result=$file;
387: last;
388: } else {
389: $result.="<option>$name</option>";
390: }
391: }
392: return $result;
393: }
394:
395: sub newproblem {
396: my ($request) = @_;
397: my $extension=$request->uri;
398: $extension=~s:^.*\.([\w]+)$:$1:;
399: &Apache::lonxml::debug("Looking for :$extension:");
400: if ($ENV{'form.template'}) {
401: use File::Copy;
402: my $file = &get_template_list($ENV{'form.template'},$extension);
403: my $dest = &Apache::lonnet::filelocation("",$request->uri);
404: copy($file,$dest);
405: &renderpage($request,$dest);
406: } elsif($ENV{'form.newfile'}) {
407: # I don't like hard-coded filenames but for now, this will work.
408: use File::Copy;
409: my $templatefilename =
410: $request->dir_config('lonIncludes').'/templates/blank.problem';
411: &Apache::lonxml::debug("$templatefilename");
412: my $dest = &Apache::lonnet::filelocation("",$request->uri);
413: copy($templatefilename,$dest);
414: &renderpage($request,$dest);
415: }else {
416: my $templatelist=&get_template_list('',$extension);
417: my $url=$request->uri;
418: my $dest = &Apache::lonnet::filelocation("",$request->uri);
419: if (!defined($templatelist)) {
420: # We didn't find a template, so just create a blank problem.
421: $request->print(<<ENDNEWPROBLEM);
422: <body bgcolor="#FFFFFF">
423: The requested file $url doesn\'t exist. You can create a new $extension <br />
424: <form action="$url" method="POST">
425: <input type="submit" name="newfile" value="New $extension"><br />
426: </form>
427: </body>
428: ENDNEWPROBLEM
429: return '';
430: }
431: $request->print(<<ENDNEWPROBLEM);
432: <body bgcolor="#FFFFFF">
433: The requested file $url doesn\'t exist. You can create a new $extension <br />
434: <form action="$url" method="POST">
435: <input type="submit" value="New $extension"><br />
436: <select name="template">
437: $templatelist
438: </select>
439: </form>
440: </body>
441: ENDNEWPROBLEM
442: }
443: return '';
444: }
445:
446: sub view_or_edit_menu {
447: my ($request) = @_;
448: my $url=$request->uri;
449: $request->print(<<EDITMENU);
450: <body bgcolor="#FFFFFF">
451: <form action="$url" method="POST">
452: Would you like to <input type="submit" name="problemmode" value="View"> or
453: <input type="submit" name="problemmode" value="Edit"> the problem.
454: </form>
455: </body>
456: EDITMENU
457: }
458:
459: sub handler {
460: #my $t0 = [&gettimeofday()];
461: my $request=$_[0];
462:
463: if ( $ENV{'user.name'} eq 'albertel' ) {$Apache::lonxml::debug=1;}
464:
465: if (&setupheader($request)) { return OK; }
466: $ENV{'request.uri'}=$request->uri;
467:
468: #setup permissions
469: $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
470: $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
471: &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:");
472: # some times multiple problemmodes are submitted, need to select
473: # the last one
474: if ( defined($ENV{'form.problemmode'}) &&
475: ref($ENV{'form.problemmode'}) ) {
476: my $mode=$ENV{'form.problemmode'}->[-1];
477: undef $ENV{'form.problemmode'};
478: $ENV{'form.problemmode'}=$mode;
479: }
480: &Apache::lonxml::debug("Problem Mode ".$ENV{'form.problemmode'});
481: my $file=&Apache::lonnet::filelocation("",$request->uri);
482:
483: #check if we know where we are
484: if ($ENV{'request.course.fn'} && !&Apache::lonnet::symbread()) {
485: # if we are browsing we might not be able to know where we are
486: if ($Apache::lonhomework::browse ne 'F') {
487: #should know where we are, so ask
488: $request->internal_redirect('/adm/ambiguous'); return;
489: }
490: }
491:
492: if ($ENV{'request.state'} eq "construct") {
493: if ($ENV{'form.resetdata'} eq 'Reset Submissions') {
494: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
495: &Apache::lonnet::tmpreset($symb,'',$domain,$name);
496: }
497: if ( -e $file ) {
498: if (!(defined $ENV{'form.problemmode'})) {
499: #first visit to problem in construction space
500: #&view_or_edit_menu($request);
501: $ENV{'form.problemmode'}='View';
502: &renderpage($request,$file);
503: } elsif ($ENV{'form.problemmode'} eq 'EditXML') {
504: &editxmlmode($request,$file);
505: } elsif ($ENV{'form.problemmode'} eq 'Answer Distribution') {
506: &analyze($request,$file);
507: } else {
508: &renderpage($request,$file);
509: }
510: } else {
511: # requested file doesn't exist in contruction space
512: &newproblem($request);
513: }
514: } else {
515: # just render the page normally outside of construction space
516: &Apache::lonxml::debug("not construct");
517: &renderpage($request,$file);
518: }
519: #my $td=&tv_interval($t0);
520: #&Apache::lonxml::debug("Spent $td seconds processing");
521: # &Apache::lonhomework::send_footer($request);
522: # always turn off debug messages
523: $Apache::lonxml::debug=0;
524: return OK;
525:
526: }
527:
528: 1;
529: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>