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

1.63    ! albertel    1: # The LearningOnline Network with CAPA
1.52      albertel    2: # The LON-CAPA Homework handler
1.63    ! albertel    3: #
        !             4: # $Id: gplheader.pl,v 1.1 2001/11/29 18:19:27 www 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: #
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.61      albertel  206:   &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";
                    216: 
                    217:   if ($ENV{'form.Undo'} eq 'undo') {
                    218:     if (copy($filesave,$fileout)) {
                    219:       $request->print("<b>Undid changes, Copied $filesave to $fileout<br /></b>");
                    220:     } else {
                    221:       $request->print("<font color=\"red\" size=\"+1\"><b>Unable to undo, unable to copy $filesave to $fileout<br /></b></font>");
                    222:     }
                    223:   } else {
                    224:     my $fs=Apache::File->new(">$filesave");
                    225:     if (defined($fs)) {
                    226:       print $fs $$problem;
                    227:       $request->print("<b>Making Backup to $filesave</b><br />");
                    228:     } else {
                    229:       $request->print("<font color=\"red\" size=\"+1\"><b>Unable to make backup $filesave</b></font>");
                    230:     }
                    231:     my $fh=Apache::File->new(">$fileout");
                    232:     if (defined($fh)) {
                    233:       print $fh $$result;
                    234:       $request->print("<b>Saving Modifications to $fileout</b><br />");
1.47      albertel  235:     } else {
1.52      albertel  236:       $request->print("<font color=\"red\" size=\"+1\"><b>Unable to write to $fileout</b></font>");
1.47      albertel  237:     }
1.52      albertel  238:   }
1.47      albertel  239: }
                    240: 
1.41      albertel  241: sub renderpage {
1.52      albertel  242:   my ($request,$file) = @_;
                    243: 
                    244:   my (@targets) = &get_target();
                    245:   foreach my $target (@targets) {
                    246:     #my $t0 = [&gettimeofday()];
                    247:     my $problem=&Apache::lonnet::getfile($file);
                    248:     if ($problem == -1) {
                    249:       &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
                    250:       $problem='';
                    251:     }
                    252: 
                    253:     my %mystyle;
                    254:     my $result = '';
                    255:     &Apache::inputtags::initialize_inputtags;
                    256:     &Apache::edit::initialize_edit;
                    257:     if ($target eq 'web') {
                    258:       if (&Apache::lonnet::symbread() eq '') {
                    259: 	if ($ENV{'request.state'} eq "construct") {
1.36      albertel  260: 	} else {
1.52      albertel  261: 	  $request->print("Browsing or <a href=\"/adm/ambiguous\">ambiguous</a> reference, submissions ignored<br />");
1.51      harris41  262: 	}
1.52      albertel  263:       }
                    264:       #if ($Apache::lonhomework::viewgrades eq 'F') {&createmenu('grade',$request); }
                    265:     }
                    266:     if ($target eq 'grade') { &showhash(%Apache::lonhomework::history); }
                    267: 
                    268:     my $default=&Apache::lonnet::getfile('/home/httpd/html/res/adm/includes/default_homework.lcpm');
                    269:     if ($default == -1) {
                    270:       &Apache::lonxml::error("<b>Unable to find <i>default_homework.lcpm</i></b>");
                    271:       $default='';
                    272:     }
                    273:     $result = &Apache::lonxml::xmlparse($target, $problem,
                    274: 			$default.&setup_vars($target),%mystyle);
                    275: 
                    276:     #$request->print("Result follows:");
                    277:     if ($target eq 'modified') {
                    278:       &handle_save_or_undo($request,\$problem,\$result);
                    279:     } else {
                    280:       #my $td=&tv_interval($t0);
                    281:       #if ( $Apache::lonxml::debug) {
                    282: 	#$result =~ s:</body>::;
                    283: 	#$result.="<br />Spent $td seconds processing target $target\n</body>";
                    284:       #}
                    285:       $request->print($result);
                    286:     }
                    287:     #$request->print(":Result ends");
                    288:     #my $td=&tv_interval($t0);
                    289:   }
1.41      albertel  290: }
                    291: 
1.42      albertel  292: # with no arg it returns a HTML <option> list of the template titles
                    293: # with one arg it returns the filename associated with the arg passed
                    294: sub get_template_list {
1.52      albertel  295:   my ($namewanted,$extension) = @_;
                    296:   my $result;
                    297:   &Apache::lonxml::debug("Looking for :$extension:");
                    298:   foreach my $file (</home/httpd/html/res/adm/includes/templates/*.$extension>) {
                    299:     my $name=&Apache::lonnet::metadata($file,'title');
                    300:     if ($namewanted && ($name eq $namewanted)) {
                    301:       $result=$file;
                    302:       last;
                    303:     } else {
                    304:       $result.="<option>$name</option>";
1.42      albertel  305:     }
1.52      albertel  306:   }
                    307:   return $result;
1.42      albertel  308: }
                    309: 
                    310: sub newproblem {
1.52      albertel  311:   my ($request) = @_;
                    312: 
                    313:   my $extension=$request->uri;
                    314:   $extension=~s:^.*\.([\w]+)$:$1:;
                    315:   &Apache::lonxml::debug("Looking for :$extension:");
                    316:   if ($ENV{'form.template'}) {
                    317:     use File::Copy;
                    318:     my $file = &get_template_list($ENV{'form.template'},$extension);
                    319:     my $dest = &Apache::lonnet::filelocation("",$request->uri);
                    320:     copy($file,$dest);
                    321:     &renderpage($request,$file);
                    322:   } else {
                    323:     my $templatelist=&get_template_list('',$extension);
                    324:     my $url=$request->uri;
                    325:     my $dest = &Apache::lonnet::filelocation("",$request->uri);
                    326:     $request->print(<<ENDNEWPROBLEM);
1.42      albertel  327: <body bgcolor="#FFFFFF">
1.50      albertel  328: The request file $url doesn\'t exist. You can create a new $extension <br />
1.42      albertel  329: <form action="$url" method="POST">
1.50      albertel  330: <input type="submit" value="New $extension"><br />
1.42      albertel  331: <select name="template">
                    332: $templatelist
                    333: </select>
                    334: </form>
                    335: </body>
                    336: ENDNEWPROBLEM
                    337:   }
                    338: 
                    339:   return '';
                    340: }
                    341: 
                    342: sub view_or_edit_menu {
1.52      albertel  343:   my ($request) = @_;
                    344:   my $url=$request->uri;
                    345:   $request->print(<<EDITMENU);
1.42      albertel  346: <body bgcolor="#FFFFFF">
                    347: <form action="$url" method="POST">
                    348: Would you like to <input type="submit" name="problemmode" value="View"> or
                    349: <input type="submit" name="problemmode" value="Edit"> the problem.
                    350: </form>
                    351: </body>
                    352: EDITMENU
                    353: }
                    354: 
1.41      albertel  355: sub handler {
1.52      albertel  356:   #my $t0 = [&gettimeofday()];
                    357:   my $request=$_[0];
1.41      albertel  358: 
1.52      albertel  359:   if ( $ENV{'user.name'} eq 'albertel' ) {$Apache::lonxml::debug=1;}
1.41      albertel  360: 
                    361:   if (&setupheader($request)) { return OK; }
1.52      albertel  362:   $ENV{'request.uri'}=$request->uri;
1.41      albertel  363: 
                    364:   #setup permissions
1.52      albertel  365:   $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
                    366:   $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
                    367:   &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:");
                    368: 
                    369:   my $file=&Apache::lonnet::filelocation("",$request->uri);
                    370: 
                    371:   #check if we know where we are
                    372:   if ($ENV{'request.course.fn'} && !&Apache::lonnet::symbread()) { 
                    373:     # if we are browsing we might not be able to know where we are
                    374:     if ($Apache::lonhomework::browse ne 'F') {
                    375:       #should know where we are, so ask
                    376:       $request->internal_redirect('/adm/ambiguous'); return;
1.41      albertel  377:     }
1.52      albertel  378:   }
1.41      albertel  379: 
1.52      albertel  380:   if ($ENV{'request.state'} eq "construct") {
1.62      albertel  381:     if ($ENV{'form.resetdata'} eq 'Reset Submissions') {
                    382:       my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
                    383:       &Apache::lonnet::tmpreset($symb,'',$domain,$name);
                    384:     }
1.52      albertel  385:     if ( -e $file ) {
                    386:       if (!(defined $ENV{'form.problemmode'})) {
                    387: 	#first visit to problem in construction space
                    388: 	&view_or_edit_menu($request);
                    389: 	#&renderpage($request,$file);
                    390:       } else {
                    391: 	&renderpage($request,$file);
1.41      albertel  392:       }
                    393:     } else {
1.52      albertel  394:       # requested file doesn't exist in contruction space
                    395:       &newproblem($request);
1.41      albertel  396:     }
1.52      albertel  397:   } else {
                    398:     # just render the page normally outside of construction space
                    399:     &renderpage($request,$file);
                    400:   }
                    401:   #my $td=&tv_interval($t0);
                    402:   #&Apache::lonxml::debug("Spent $td seconds processing");
                    403:   # &Apache::lonhomework::send_footer($request);
                    404:   # always turn off debug messages
                    405:   $Apache::lonxml::debug=0;
                    406:   return OK;
                    407: 
1.1       albertel  408: }
                    409: 
                    410: 1;
                    411: __END__

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