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