File:  [LON-CAPA] / loncom / homework / lonhomework.pm
Revision 1.71: download - view: text, annotated - select for diffs
Wed Feb 27 20:23:26 2002 UTC (22 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- form.problemmode can have multiple values submitted, always selct the last one

    1: # The LearningOnline Network with CAPA
    2: # The LON-CAPA Homework handler
    3: #
    4: # $Id: lonhomework.pm,v 1.71 2002/02/27 20:23:26 albertel Exp $
    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: #
   28: # Guy Albertelli
   29: # 11/30 Gerd Kortemeyer
   30: # 6/1,8/17,8/18 Gerd Kortemeyer
   31: 
   32: package Apache::lonhomework;
   33: use strict;
   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;
   44: use Apache::Constants qw(:common);
   45: #use Time::HiRes qw( gettimeofday tv_interval );
   46: 
   47: BEGIN {
   48:   &Apache::lonxml::register_insert();
   49: }
   50: 
   51: sub get_target {
   52:   if ( $ENV{'request.state'} eq "published") {
   53:     if ( defined($ENV{'form.grade_target'}) 
   54: 	 && ($Apache::lonhomework::viewgrades == 'F' )) {
   55:       return ($ENV{'form.grade_target'});
   56:     }
   57:     if ( defined($ENV{'form.submitted'})) {
   58:       return ('grade', 'web');
   59:     } else {
   60:       return ('web');
   61:     }
   62:   } elsif ($ENV{'request.state'} eq "construct") {
   63:     if ( defined($ENV{'form.preview'})) {
   64:       if ( defined($ENV{'form.submitted'})) {
   65: 	return ('grade', 'web');
   66:       } else {
   67: 	return ('web');
   68:       }
   69:     } else {
   70:       if ( $ENV{'form.problemmode'} eq 'View' ) {
   71: 	if ( defined($ENV{'form.submitted'}) &&
   72: 	     (!defined($ENV{'form.resetdata'})) ) {
   73: 	  return ('grade', 'web','answer');
   74: 	} else {
   75: 	  return ('web','answer');
   76: 	}
   77:       } elsif ( $ENV{'form.problemmode'} eq 'Edit' ) {
   78: 	if ( $ENV{'form.submitted'} eq 'edit' ) {
   79: 	  return ('modified','edit');
   80: 	} else {
   81: 	  return ('edit');
   82: 	}
   83:       } else {
   84: 	return ('web');
   85:       }
   86:     }
   87:   }
   88:   return ();
   89: }
   90: 
   91: sub setup_vars {
   92:   my ($target) = @_;
   93:   return ';'
   94: #  return ';$external::target='.$target.';';
   95: }
   96: 
   97: sub send_header {
   98:   my ($request)= @_;
   99:   $request->print(&Apache::lontexconvert::header());
  100: #  $request->print('<form name='.$ENV{'form.request.prefix'}.'lonhomework method="POST" action="'.$request->uri.'">');
  101: }
  102: 
  103: sub createmenu {
  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:   }
  111: }
  112: 
  113: sub send_footer {
  114:   my ($request)= @_;
  115: #  $request->print('</form>');
  116:   $request->print(&Apache::lontexconvert::footer());
  117: }
  118: 
  119: $Apache::lonxml::browse='';
  120: 
  121: sub check_access {
  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;
  142:     }
  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:   }
  167: 
  168:   if (($status ne 'CLOSED') && ($Apache::lonhomework::type eq 'exam') &&
  169:       (!$Apache::lonhomework::history{"resource.0.outtoken"})) {
  170:       return ('UNCHECKEDOUT','needs to be checked out');
  171:   }
  172: 
  173: 
  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);
  186: }
  187: 
  188: sub showhash {
  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 '';
  196: }
  197: 
  198: sub setuppermissions {
  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 ''
  202: }
  203: 
  204: sub setupheader {
  205:   my $request=$_[0];
  206:   if ($ENV{'browser.mathml'}) {
  207:     $request->content_type('text/xml');
  208:   } else {
  209:     $request->content_type('text/html');
  210:   }
  211:   if (!$Apache::lonxml::debug && ($ENV{'REQUEST_METHOD'} eq 'GET')) {
  212:     &Apache::loncommon::no_cache($request);
  213:   }
  214:   $request->send_http_header;
  215:   return OK if $request->header_only;
  216:   return ''
  217: }
  218: 
  219: sub handle_save_or_undo {
  220:   my ($request,$problem,$result) = @_;
  221:   my $file    = &Apache::lonnet::filelocation("",$request->uri);
  222:   my $filebak =$file.".bak";
  223:   my $filetmp =$file.".tmp";
  224:   my $error=0;
  225: 
  226:   if ($ENV{'form.Undo'} eq 'undo') {
  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>");
  233:     } else {
  234:       $request->print("<p><font color=\"red\" size=\"+1\"><b>Unable to undo, unable to switch $filebak and $file</b></font></p>");
  235:       $error=1;
  236:     }
  237:   } else {
  238:     my $fs=Apache::File->new(">$filebak");
  239:     if (defined($fs)) {
  240:       print $fs $$problem;
  241:       $request->print("<b>Making Backup to $filebak</b><br />");
  242:     } else {
  243:       $request->print("<font color=\"red\" size=\"+1\"><b>Unable to make backup $filebak</b></font>");
  244:       $error=2;
  245:     }
  246:     my $fh=Apache::File->new(">$file");
  247:     if (defined($fh)) {
  248:       print $fh $$result;
  249:       $request->print("<b>Saving Modifications to $file</b><br />");
  250:     } else {
  251:       $request->print("<font color=\"red\" size=\"+1\"><b>Unable to write to $file</b></font>");
  252:       $error|=4;
  253:     }
  254:   }
  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 '';
  288: }
  289: 
  290: sub renderpage {
  291:   my ($request,$file) = @_;
  292: 
  293:   my (@targets) = &get_target();
  294:   &Apache::lonxml::debug("Running targets ".join(':',@targets));
  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") {
  310: 	} else {
  311: 	  $request->print("Browsing or <a href=\"/adm/ambiguous\">ambiguous</a> reference, submissions ignored<br />");
  312: 	}
  313:       }
  314:       #if ($Apache::lonhomework::viewgrades eq 'F') {&createmenu('grade',$request); }
  315:     }
  316:     #if ($target eq 'grade') { &showhash(%Apache::lonhomework::history); }
  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:     }
  323:     &Apache::lonxml::debug("Should be parsing now");
  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:   }
  341: }
  342: 
  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 {
  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>";
  356:     }
  357:   }
  358:   return $result;
  359: }
  360: 
  361: sub newproblem {
  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 = 
  376: 	    $request->dir_config('lonIncludes').'/templates/blank.problem';
  377: 	&Apache::lonxml::debug("$templatefilename");
  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);
  398: <body bgcolor="#FFFFFF">
  399: The requested file $url doesn\'t exist. You can create a new $extension <br />
  400: <form action="$url" method="POST">
  401: <input type="submit" value="New $extension"><br />
  402: <select name="template">
  403: $templatelist
  404: </select>
  405: </form>
  406: </body>
  407: ENDNEWPROBLEM
  408:     }
  409:     return '';
  410: }
  411: 
  412: sub view_or_edit_menu {
  413:   my ($request) = @_;
  414:   my $url=$request->uri;
  415:   $request->print(<<EDITMENU);
  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: 
  425: sub handler {
  426:   #my $t0 = [&gettimeofday()];
  427:   my $request=$_[0];
  428: 
  429:   if ( $ENV{'user.name'} eq 'albertel' ) {$Apache::lonxml::debug=1;}
  430: 
  431:   if (&setupheader($request)) { return OK; }
  432:   $ENV{'request.uri'}=$request->uri;
  433: 
  434:   #setup permissions
  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:");
  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:   }
  446:   &Apache::lonxml::debug("Problem Mode ".$ENV{'form.problemmode'});
  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;
  455:     }
  456:   }
  457: 
  458:   if ($ENV{'request.state'} eq "construct") {
  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:     }
  463:     if ( -e $file ) {
  464:       if (!(defined $ENV{'form.problemmode'})) {
  465: 	#first visit to problem in construction space
  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);
  471:       } else {
  472: 	&renderpage($request,$file);
  473:       }
  474:     } else {
  475:       # requested file doesn't exist in contruction space
  476:       &newproblem($request);
  477:     }
  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: 
  489: }
  490: 
  491: 1;
  492: __END__

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