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