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

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

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