Annotation of loncom/homework/lonhomework.pm, revision 1.65

1.63      albertel    1: # The LearningOnline Network with CAPA
1.52      albertel    2: # The LON-CAPA Homework handler
1.63      albertel    3: #
1.65    ! matthew     4: # $Id: lonhomework.pm,v 1.64 2001/12/04 14:51:19 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.65    ! matthew   348:     my ($request) = @_;
        !           349:     my $extension=$request->uri;
        !           350:     $extension=~s:^.*\.([\w]+)$:$1:;
        !           351:     &Apache::lonxml::debug("Looking for :$extension:");
        !           352:     if ($ENV{'form.template'}) {
        !           353: 	use File::Copy;
        !           354: 	my $file = &get_template_list($ENV{'form.template'},$extension);
        !           355: 	my $dest = &Apache::lonnet::filelocation("",$request->uri);
        !           356: 	copy($file,$dest);
        !           357: 	&renderpage($request,$dest);
        !           358:     } elsif($ENV{'form.newfile'}) {
        !           359: 	# I don't like hard-coded filenames but for now, this will work.
        !           360: 	use File::Copy;
        !           361: 	my $templatefilename = 
        !           362: 	    "/home/httpd/html/res/adm/includes/templates/blank.problem";
        !           363: 	my $dest = &Apache::lonnet::filelocation("",$request->uri);
        !           364: 	copy($templatefilename,$dest);
        !           365: 	&renderpage($request,$dest);
        !           366:     }else {
        !           367: 	my $templatelist=&get_template_list('',$extension);
        !           368: 	my $url=$request->uri;
        !           369: 	my $dest = &Apache::lonnet::filelocation("",$request->uri);
        !           370: 	if (!defined($templatelist)) {
        !           371: 	    # We didn't find a template, so just create a blank problem.
        !           372: 	    $request->print(<<ENDNEWPROBLEM);
        !           373: <body bgcolor="#FFFFFF">
        !           374: The requested file $url doesn\'t exist. You can create a new $extension <br />
        !           375: <form action="$url" method="POST">
        !           376: <input type="submit" name="newfile" value="New $extension"><br />
        !           377: </form>
        !           378: </body>
        !           379: ENDNEWPROBLEM
        !           380:             return '';
        !           381:         }
        !           382: 	$request->print(<<ENDNEWPROBLEM);
1.42      albertel  383: <body bgcolor="#FFFFFF">
1.65    ! matthew   384: The requested file $url doesn\'t exist. You can create a new $extension <br />
1.42      albertel  385: <form action="$url" method="POST">
1.50      albertel  386: <input type="submit" value="New $extension"><br />
1.42      albertel  387: <select name="template">
                    388: $templatelist
                    389: </select>
                    390: </form>
                    391: </body>
                    392: ENDNEWPROBLEM
1.65    ! matthew   393:     }
        !           394:     return '';
1.42      albertel  395: }
                    396: 
                    397: sub view_or_edit_menu {
1.52      albertel  398:   my ($request) = @_;
                    399:   my $url=$request->uri;
                    400:   $request->print(<<EDITMENU);
1.42      albertel  401: <body bgcolor="#FFFFFF">
                    402: <form action="$url" method="POST">
                    403: Would you like to <input type="submit" name="problemmode" value="View"> or
                    404: <input type="submit" name="problemmode" value="Edit"> the problem.
                    405: </form>
                    406: </body>
                    407: EDITMENU
                    408: }
                    409: 
1.41      albertel  410: sub handler {
1.52      albertel  411:   #my $t0 = [&gettimeofday()];
                    412:   my $request=$_[0];
1.41      albertel  413: 
1.52      albertel  414:   if ( $ENV{'user.name'} eq 'albertel' ) {$Apache::lonxml::debug=1;}
1.41      albertel  415: 
                    416:   if (&setupheader($request)) { return OK; }
1.52      albertel  417:   $ENV{'request.uri'}=$request->uri;
1.41      albertel  418: 
                    419:   #setup permissions
1.52      albertel  420:   $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
                    421:   $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
                    422:   &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:");
1.64      albertel  423:   &Apache::lonxml::debug("Problem Mode ".$ENV{'form.problemmode'});
1.52      albertel  424:   my $file=&Apache::lonnet::filelocation("",$request->uri);
                    425: 
                    426:   #check if we know where we are
                    427:   if ($ENV{'request.course.fn'} && !&Apache::lonnet::symbread()) { 
                    428:     # if we are browsing we might not be able to know where we are
                    429:     if ($Apache::lonhomework::browse ne 'F') {
                    430:       #should know where we are, so ask
                    431:       $request->internal_redirect('/adm/ambiguous'); return;
1.41      albertel  432:     }
1.52      albertel  433:   }
1.41      albertel  434: 
1.52      albertel  435:   if ($ENV{'request.state'} eq "construct") {
1.62      albertel  436:     if ($ENV{'form.resetdata'} eq 'Reset Submissions') {
                    437:       my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
                    438:       &Apache::lonnet::tmpreset($symb,'',$domain,$name);
                    439:     }
1.52      albertel  440:     if ( -e $file ) {
                    441:       if (!(defined $ENV{'form.problemmode'})) {
                    442: 	#first visit to problem in construction space
1.64      albertel  443: 	#&view_or_edit_menu($request);
                    444: 	$ENV{'form.problemmode'}='View';
                    445: 	&renderpage($request,$file);
                    446:       } elsif ($ENV{'form.problemmode'} eq 'EditXML') {
                    447: 	&editxmlmode($request,$file);
1.52      albertel  448:       } else {
                    449: 	&renderpage($request,$file);
1.41      albertel  450:       }
                    451:     } else {
1.52      albertel  452:       # requested file doesn't exist in contruction space
                    453:       &newproblem($request);
1.41      albertel  454:     }
1.52      albertel  455:   } else {
                    456:     # just render the page normally outside of construction space
                    457:     &renderpage($request,$file);
                    458:   }
                    459:   #my $td=&tv_interval($t0);
                    460:   #&Apache::lonxml::debug("Spent $td seconds processing");
                    461:   # &Apache::lonhomework::send_footer($request);
                    462:   # always turn off debug messages
                    463:   $Apache::lonxml::debug=0;
                    464:   return OK;
                    465: 
1.1       albertel  466: }
                    467: 
                    468: 1;
                    469: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>