Annotation of loncom/homework/lonhomework.pm, revision 1.68
1.63 albertel 1: # The LearningOnline Network with CAPA
1.52 albertel 2: # The LON-CAPA Homework handler
1.63 albertel 3: #
1.68 ! albertel 4: # $Id: lonhomework.pm,v 1.67 2001/12/21 20:06:25 matthew 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:
47: sub BEGIN {
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) = @_;
221: my $fileout = &Apache::lonnet::filelocation("",$request->uri);
222: my $filesave=$fileout.".bak";
1.64 albertel 223: my $error=0;
1.52 albertel 224:
225: if ($ENV{'form.Undo'} eq 'undo') {
226: if (copy($filesave,$fileout)) {
227: $request->print("<b>Undid changes, Copied $filesave to $fileout<br /></b>");
228: } else {
229: $request->print("<font color=\"red\" size=\"+1\"><b>Unable to undo, unable to copy $filesave to $fileout<br /></b></font>");
1.64 albertel 230: $error=1;
1.52 albertel 231: }
232: } else {
233: my $fs=Apache::File->new(">$filesave");
234: if (defined($fs)) {
235: print $fs $$problem;
236: $request->print("<b>Making Backup to $filesave</b><br />");
237: } else {
238: $request->print("<font color=\"red\" size=\"+1\"><b>Unable to make backup $filesave</b></font>");
1.64 albertel 239: $error=2;
1.52 albertel 240: }
241: my $fh=Apache::File->new(">$fileout");
242: if (defined($fh)) {
243: print $fh $$result;
244: $request->print("<b>Saving Modifications to $fileout</b><br />");
1.47 albertel 245: } else {
1.52 albertel 246: $request->print("<font color=\"red\" size=\"+1\"><b>Unable to write to $fileout</b></font>");
1.64 albertel 247: $error|=4;
1.47 albertel 248: }
1.52 albertel 249: }
1.64 albertel 250: return $error;
251: }
252:
253: sub editxmlmode {
254: my ($request,$file) = @_;
255: my $result;
256: my $problem=&Apache::lonnet::getfile($file);
257: if ($problem == -1) {
258: &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
259: $problem='';
260: }
261: if (defined($ENV{'form.editxmltext'}) || defined($ENV{'form.Undo'})) {
262: my $error=&handle_save_or_undo($request,\$problem,
263: \$ENV{'form.editxmltext'});
264: if (!$error) { $problem=&Apache::lonnet::getfile($file); }
265: }
266: my ($rows,$cols) = &Apache::edit::textarea_sizes(\$problem);
267: if ($cols > 80) { $cols = 80; }
268: $result.='<html><body bgcolor="#FFFFFF">
269: <form name="lonhomework" method="POST" action="'.
270: $ENV{'request.uri'}.'">
271: <input type="hidden" name="problemmode" value="EditXML" />
272: <input type="submit" name="problemmode" value="View" />
273: <input type="submit" name="problemmode" value="Edit" />
274: <hr />
275: <input type="submit" name="submit" value="Submit Changes" />
276: <input type="submit" name="Undo" value="undo" />
277: <hr />
278: <textarea rows="'.$rows.'" cols="'.$cols.'" name="editxmltext">'.
279: $problem.'</textarea>
280: </form></body></html>';
281: $request->print($result);
282: return '';
1.47 albertel 283: }
284:
1.41 albertel 285: sub renderpage {
1.52 albertel 286: my ($request,$file) = @_;
287:
288: my (@targets) = &get_target();
289: foreach my $target (@targets) {
290: #my $t0 = [&gettimeofday()];
291: my $problem=&Apache::lonnet::getfile($file);
292: if ($problem == -1) {
293: &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
294: $problem='';
295: }
296:
297: my %mystyle;
298: my $result = '';
299: &Apache::inputtags::initialize_inputtags;
300: &Apache::edit::initialize_edit;
301: if ($target eq 'web') {
302: if (&Apache::lonnet::symbread() eq '') {
303: if ($ENV{'request.state'} eq "construct") {
1.36 albertel 304: } else {
1.52 albertel 305: $request->print("Browsing or <a href=\"/adm/ambiguous\">ambiguous</a> reference, submissions ignored<br />");
1.51 harris41 306: }
1.52 albertel 307: }
308: #if ($Apache::lonhomework::viewgrades eq 'F') {&createmenu('grade',$request); }
309: }
1.64 albertel 310: #if ($target eq 'grade') { &showhash(%Apache::lonhomework::history); }
1.52 albertel 311:
312: my $default=&Apache::lonnet::getfile('/home/httpd/html/res/adm/includes/default_homework.lcpm');
313: if ($default == -1) {
314: &Apache::lonxml::error("<b>Unable to find <i>default_homework.lcpm</i></b>");
315: $default='';
316: }
317: $result = &Apache::lonxml::xmlparse($target, $problem,
318: $default.&setup_vars($target),%mystyle);
319:
320: #$request->print("Result follows:");
321: if ($target eq 'modified') {
322: &handle_save_or_undo($request,\$problem,\$result);
323: } else {
324: #my $td=&tv_interval($t0);
325: #if ( $Apache::lonxml::debug) {
326: #$result =~ s:</body>::;
327: #$result.="<br />Spent $td seconds processing target $target\n</body>";
328: #}
329: $request->print($result);
330: }
331: #$request->print(":Result ends");
332: #my $td=&tv_interval($t0);
333: }
1.41 albertel 334: }
335:
1.42 albertel 336: # with no arg it returns a HTML <option> list of the template titles
337: # with one arg it returns the filename associated with the arg passed
338: sub get_template_list {
1.52 albertel 339: my ($namewanted,$extension) = @_;
340: my $result;
341: &Apache::lonxml::debug("Looking for :$extension:");
342: foreach my $file (</home/httpd/html/res/adm/includes/templates/*.$extension>) {
343: my $name=&Apache::lonnet::metadata($file,'title');
344: if ($namewanted && ($name eq $namewanted)) {
345: $result=$file;
346: last;
347: } else {
348: $result.="<option>$name</option>";
1.42 albertel 349: }
1.52 albertel 350: }
351: return $result;
1.42 albertel 352: }
353:
354: sub newproblem {
1.65 matthew 355: my ($request) = @_;
356: my $extension=$request->uri;
357: $extension=~s:^.*\.([\w]+)$:$1:;
358: &Apache::lonxml::debug("Looking for :$extension:");
359: if ($ENV{'form.template'}) {
360: use File::Copy;
361: my $file = &get_template_list($ENV{'form.template'},$extension);
362: my $dest = &Apache::lonnet::filelocation("",$request->uri);
363: copy($file,$dest);
364: &renderpage($request,$dest);
365: } elsif($ENV{'form.newfile'}) {
366: # I don't like hard-coded filenames but for now, this will work.
367: use File::Copy;
368: my $templatefilename =
1.66 albertel 369: $request->dir_config('lonIncludes').'/templates/blank.problem';
370: &Apache::lonxml::debug("$templatefilename");
1.65 matthew 371: my $dest = &Apache::lonnet::filelocation("",$request->uri);
372: copy($templatefilename,$dest);
373: &renderpage($request,$dest);
374: }else {
375: my $templatelist=&get_template_list('',$extension);
376: my $url=$request->uri;
377: my $dest = &Apache::lonnet::filelocation("",$request->uri);
378: if (!defined($templatelist)) {
379: # We didn't find a template, so just create a blank problem.
380: $request->print(<<ENDNEWPROBLEM);
381: <body bgcolor="#FFFFFF">
382: The requested file $url doesn\'t exist. You can create a new $extension <br />
383: <form action="$url" method="POST">
384: <input type="submit" name="newfile" value="New $extension"><br />
385: </form>
386: </body>
387: ENDNEWPROBLEM
388: return '';
389: }
390: $request->print(<<ENDNEWPROBLEM);
1.42 albertel 391: <body bgcolor="#FFFFFF">
1.65 matthew 392: The requested file $url doesn\'t exist. You can create a new $extension <br />
1.42 albertel 393: <form action="$url" method="POST">
1.50 albertel 394: <input type="submit" value="New $extension"><br />
1.42 albertel 395: <select name="template">
396: $templatelist
397: </select>
398: </form>
399: </body>
400: ENDNEWPROBLEM
1.65 matthew 401: }
402: return '';
1.42 albertel 403: }
404:
405: sub view_or_edit_menu {
1.52 albertel 406: my ($request) = @_;
407: my $url=$request->uri;
408: $request->print(<<EDITMENU);
1.42 albertel 409: <body bgcolor="#FFFFFF">
410: <form action="$url" method="POST">
411: Would you like to <input type="submit" name="problemmode" value="View"> or
412: <input type="submit" name="problemmode" value="Edit"> the problem.
413: </form>
414: </body>
415: EDITMENU
416: }
417:
1.41 albertel 418: sub handler {
1.52 albertel 419: #my $t0 = [&gettimeofday()];
420: my $request=$_[0];
1.41 albertel 421:
1.52 albertel 422: if ( $ENV{'user.name'} eq 'albertel' ) {$Apache::lonxml::debug=1;}
1.41 albertel 423:
424: if (&setupheader($request)) { return OK; }
1.52 albertel 425: $ENV{'request.uri'}=$request->uri;
1.41 albertel 426:
427: #setup permissions
1.52 albertel 428: $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
429: $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
430: &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:");
1.64 albertel 431: &Apache::lonxml::debug("Problem Mode ".$ENV{'form.problemmode'});
1.52 albertel 432: my $file=&Apache::lonnet::filelocation("",$request->uri);
433:
434: #check if we know where we are
435: if ($ENV{'request.course.fn'} && !&Apache::lonnet::symbread()) {
436: # if we are browsing we might not be able to know where we are
437: if ($Apache::lonhomework::browse ne 'F') {
438: #should know where we are, so ask
439: $request->internal_redirect('/adm/ambiguous'); return;
1.41 albertel 440: }
1.52 albertel 441: }
1.41 albertel 442:
1.52 albertel 443: if ($ENV{'request.state'} eq "construct") {
1.62 albertel 444: if ($ENV{'form.resetdata'} eq 'Reset Submissions') {
445: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
446: &Apache::lonnet::tmpreset($symb,'',$domain,$name);
447: }
1.52 albertel 448: if ( -e $file ) {
449: if (!(defined $ENV{'form.problemmode'})) {
450: #first visit to problem in construction space
1.64 albertel 451: #&view_or_edit_menu($request);
452: $ENV{'form.problemmode'}='View';
453: &renderpage($request,$file);
454: } elsif ($ENV{'form.problemmode'} eq 'EditXML') {
455: &editxmlmode($request,$file);
1.52 albertel 456: } else {
457: &renderpage($request,$file);
1.41 albertel 458: }
459: } else {
1.52 albertel 460: # requested file doesn't exist in contruction space
461: &newproblem($request);
1.41 albertel 462: }
1.52 albertel 463: } else {
464: # just render the page normally outside of construction space
465: &renderpage($request,$file);
466: }
467: #my $td=&tv_interval($t0);
468: #&Apache::lonxml::debug("Spent $td seconds processing");
469: # &Apache::lonhomework::send_footer($request);
470: # always turn off debug messages
471: $Apache::lonxml::debug=0;
472: return OK;
473:
1.1 albertel 474: }
475:
476: 1;
477: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>