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