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

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

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