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

1.1       albertel    1: # The LON-CAPA Homework handler
                      2: # Guy Albertelli
1.17      www         3: # 11/30 Gerd Kortemeyer
1.25      www         4: # 6/1 Gerd Kortemeyer
1.1       albertel    5: 
                      6: package Apache::lonhomework;
                      7: use strict;
                      8: use Apache::style;
                      9: use Apache::lonxml;
1.2       albertel   10: use Apache::lonnet;
                     11: use Apache::inputtags;
                     12: use Apache::structuretags;
1.9       albertel   13: use Apache::response;
1.22      albertel   14: use Apache::hint;
1.31      albertel   15: use Apache::outputtags;
1.26      www        16: use Apache::Constants qw(:common);
1.47    ! albertel   17: #use Time::HiRes qw( gettimeofday tv_interval );
1.43      albertel   18: 
                     19: sub BEGIN {
                     20:   &Apache::lonxml::register_insert();
                     21: }
                     22: 
1.5       albertel   23: sub get_target {
1.15      albertel   24:   if ( $ENV{'request.state'} eq "published") {
1.34      albertel   25:     if ( defined $ENV{'form.submitted'}) {
1.18      albertel   26:       return ('grade', 'web');
                     27:     } else {
                     28:       return ('web');
                     29:     }
1.15      albertel   30:   } elsif ($ENV{'request.state'} eq "construct") {
1.16      albertel   31:     if ( defined $ENV{'form.preview'}) {
1.34      albertel   32:       if ( defined $ENV{'form.submitted'}) {
1.18      albertel   33: 	return ('grade', 'web');
                     34:       } else {
                     35: 	return ('web');
                     36:       }
1.15      albertel   37:     } else {
1.42      albertel   38:       if ( $ENV{'form.problemmode'} eq 'View' ) {
                     39: 	if ( defined $ENV{'form.submitted'}) {
                     40: 	  return ('grade', 'web');
                     41: 	} else {
                     42: 	  return ('web');
                     43: 	}
                     44:       } elsif ( $ENV{'form.problemmode'} eq 'Edit' ) {
                     45: 	if ( $ENV{'form.submitted'} eq 'edit' ) {
                     46: 	  return ('modified','edit');
                     47: 	} else {
                     48: 	  return ('edit');
                     49: 	}
1.40      albertel   50:       } else {
1.42      albertel   51: 	return ('web');
1.40      albertel   52:       }
1.15      albertel   53:     }
                     54:   }
1.18      albertel   55:   return ();
1.5       albertel   56: }
                     57: 
1.3       albertel   58: sub setup_vars {
1.5       albertel   59:   my ($target) = @_;
1.11      albertel   60:   return ';'
                     61: #  return ';$external::target='.$target.';';
1.2       albertel   62: }
                     63: 
                     64: sub send_header {
                     65:   my ($request)= @_;
1.14      albertel   66:   $request->print(&Apache::lontexconvert::header());
1.16      albertel   67: #  $request->print('<form name='.$ENV{'form.request.prefix'}.'lonhomework method="POST" action="'.$request->uri.'">');
1.2       albertel   68: }
                     69: 
1.36      albertel   70: sub createmenu {
                     71:   my ($which,$request)=@_;
                     72:   if ($which eq 'grade') {
                     73:     $request->print('<script language="JavaScript"> 
1.38      albertel   74:           hwkmenu=window.open("/res/adm/pages/homeworkmenu.html","homeworkremote",
1.36      albertel   75:                  "height=350,width=150,menubar=no");
                     76:           </script>');
                     77:   }
                     78: }
                     79: 
1.2       albertel   80: sub send_footer {
                     81:   my ($request)= @_;
1.16      albertel   82: #  $request->print('</form>');
1.14      albertel   83:   $request->print(&Apache::lontexconvert::footer());
1.2       albertel   84: }
                     85: 
1.36      albertel   86: $Apache::lonxml::browse='';
1.20      albertel   87: sub check_date {
1.21      albertel   88:   my ($id) = @_;
1.20      albertel   89:   my $date ='';
1.21      albertel   90:   my $status = '';
                     91:   my $datemsg = '';
                     92:   my $lastdate = '';
                     93:   my $temp;
                     94:   my $type;
                     95:   my $passed;
                     96:   &Apache::lonxml::debug("checking for part :$id:");
                     97:   foreach $temp ("opendate","duedate","answerdate") {
                     98:     $lastdate = $date;
                     99:     $date = &Apache::lonnet::EXT("resource.$id.$temp");
                    100:     &Apache::lonxml::debug("found :$date: for :$temp:");
                    101:     if ($date eq '') {
                    102:       $date = "an unknown date"; $passed = 0;
1.30      albertel  103:     } elsif ($date eq 'con_lost') {
                    104:       $date = "an indeterminate date"; $passed = 0;
1.21      albertel  105:     } else {
                    106:       if (time < $date) { $passed = 0; } else { $passed = 1; }
                    107:       $date = localtime $date;
                    108:     }
                    109:     if (!$passed) { $type=$temp; last; }
1.20      albertel  110:   }
1.21      albertel  111:   &Apache::lonxml::debug("have :$type:$passed:");
                    112:   if ($passed) {
                    113:     $status='SHOW_ANSWER';
                    114:     $datemsg=$date;
                    115:   } elsif ($type eq 'opendate') {
                    116:     $status='CLOSED';
                    117:     $datemsg = "will open on $date";
                    118:   } elsif ($type eq 'duedate') {
                    119:     $status='CAN_ANSWER';
                    120:     $datemsg = "is due at $date";
                    121:   } elsif ($type eq 'answerdate') {
                    122:     $status='CLOSED';
1.32      albertel  123:     $datemsg = "was due on $lastdate, and answers will be available on $date";
1.20      albertel  124:   }
1.27      albertel  125:   if ($status eq 'CAN_ANSWER') {
                    126:     #check #tries
                    127:     my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
                    128:     my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
                    129:     if ( $tries eq '' ) { $tries = '0'; }
                    130:     if ( $maxtries eq '' ) { $maxtries = '2'; } 
                    131:     if ($tries >= $maxtries) { $status = 'CANNOT_ANSWER'; } 
                    132:   }
1.21      albertel  133:   &Apache::lonxml::debug("sending back :$status:$datemsg:");
1.36      albertel  134:   if (($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED')) {
                    135:     &Apache::lonxml::debug("should be allowed to browse a resource when closed");
                    136:     $status='CAN_ANSWER';
                    137:     $datemsg='is closed but you are allowed to view it';
                    138:   }
1.24      albertel  139:   if ($ENV{'request.state'} eq "construct") {
                    140:     &Apache::lonxml::debug("in construction ignoring dates");
                    141:     $status='CAN_ANSWER';
                    142:     $datemsg='is in under construction';
                    143:   }
1.21      albertel  144:   return ($status,$datemsg);
1.20      albertel  145: }
                    146: 
1.41      albertel  147: sub showhash {
                    148:   my (%hash) = @_;
                    149:   my $resultkey;
                    150:   foreach $resultkey (sort keys %hash) {
                    151:     &Apache::lonxml::debug("$resultkey ---- $hash{$resultkey}");
                    152:   }
                    153:   &Apache::lonxml::debug("\n<br />restored values^</br>\n");
                    154:   return '';
                    155: }
                    156: 
                    157: sub setuppermissions {
                    158:   $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
                    159:   $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
                    160:   return ''
                    161: }
                    162: 
                    163: sub setupheader {
1.18      albertel  164:   my $request=$_[0];
1.17      www       165:   if ($ENV{'browser.mathml'}) {
                    166:     $request->content_type('text/xml');
                    167:   } else {
                    168:     $request->content_type('text/html');
                    169:   }
                    170:   $request->send_http_header;
1.25      www       171:   return OK if $request->header_only;
1.41      albertel  172:   return ''
                    173: }
1.35      albertel  174: 
1.47    ! albertel  175: sub handle_save_or_undo {
        !           176:   my ($request,$problem,$result) = @_;
        !           177:   my $fileout = &Apache::lonnet::filelocation("",$request->uri);
        !           178:   my $filesave=$fileout.".bak";
        !           179: 
        !           180:   if ($ENV{'form.Undo'} eq 'undo') {
        !           181:     if (copy($filesave,$fileout)) {
        !           182:       $request->print("<b>Undid changes, Copied $filesave to $fileout<br /></b>");
        !           183:     } else {
        !           184:       $request->print("<font color=\"red\" size=\"+1\"><b>Unable to undo, unable to copy $filesave to $fileout<br /></b></font>");
        !           185:     }
        !           186:   } else {
        !           187:     my $fs=Apache::File->new(">$filesave");
        !           188:     if (defined($fs)) {
        !           189:       print $fs $$problem;
        !           190:       $request->print("<b>Making Backup to $filesave</b><br />");
        !           191:     } else {
        !           192:       $request->print("<font color=\"red\" size=\"+1\"><b>Unable to make backup $filesave</b></font>");
        !           193:     }
        !           194:     my $fh=Apache::File->new(">$fileout");
        !           195:     if (defined($fh)) {
        !           196:       print $fh $$result;
        !           197:       $request->print("<b>Saving Modifications to $fileout</b><br />");
        !           198:     } else {
        !           199:       $request->print("<font color=\"red\" size=\"+1\"><b>Unable to write to $fileout</b></font>");
        !           200:     }
        !           201:   }
        !           202: }
        !           203: 
1.41      albertel  204: sub renderpage {
                    205:   my ($request,$file) = @_;
1.36      albertel  206: 
1.41      albertel  207:   my (@targets) = &get_target();
1.18      albertel  208:   foreach my $target (@targets) {
1.47    ! albertel  209:     #my $t0 = [&gettimeofday()];
1.18      albertel  210:     my $problem=&Apache::lonnet::getfile($file);
                    211:     if ($problem == -1) {
                    212:       &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
                    213:       $problem='';
                    214:     }
1.41      albertel  215: 
1.18      albertel  216:     my %mystyle;
                    217:     my $result = '';
                    218:     &Apache::inputtags::initialize_inputtags;
1.46      albertel  219:     &Apache::edit::initialize_edit;
1.18      albertel  220:     %Apache::lonhomework::results=();
1.40      albertel  221:     %Apache::lonhomework::history=&Apache::lonnet::restore();
1.18      albertel  222:     #ignore error conditions
                    223:     my ($temp)=keys %Apache::lonhomework::history ;
                    224:     if ($temp =~ m/^error:.*/) { %Apache::lonhomework::history=(); } 
1.36      albertel  225:     if ($target eq 'web') {
                    226:       if (&Apache::lonnet::symbread() eq '') {
                    227: 	if ($ENV{'request.state'} eq "construct") {
1.39      albertel  228: 	  $request->print("In construction space, submissions ignored<br />");
1.36      albertel  229: 	} else {
1.39      albertel  230: 	  $request->print("Browsing or <a href=\"/adm/ambiguous\">ambiguous</a> reference, submissions ignored<br />");
1.36      albertel  231: 	}
                    232:       }
1.45      albertel  233:       #if ($Apache::lonhomework::viewgrades eq 'F') {&createmenu('grade',$request); }
1.20      albertel  234:     }
1.41      albertel  235:     if ($target eq 'grade') { &showhash(%Apache::lonhomework::history); }
                    236: 
1.18      albertel  237:     my $default=&Apache::lonnet::getfile('/home/httpd/html/res/adm/includes/default_homework.lcpm');
                    238:     if ($default == -1) {
                    239:       &Apache::lonxml::error("<b>Unable to find <i>default_homework.lcpm</i></b>");
                    240:       $default='';
                    241:     }
                    242:     $result = &Apache::lonxml::xmlparse($target, $problem,
                    243: 			$default.&setup_vars($target),%mystyle);
1.17      www       244: 
1.18      albertel  245:     #$request->print("Result follows:");
1.40      albertel  246:     if ($target eq 'modified') {
1.47    ! albertel  247:       &handle_save_or_undo($request,\$problem,\$result);
1.40      albertel  248:     } else {
1.47    ! albertel  249:       #my $td=&tv_interval($t0);
        !           250:       #if ( $Apache::lonxml::debug) {
        !           251: 	#$result =~ s:</body>::;
        !           252: 	#$result.="<br />Spent $td seconds processing target $target\n</body>";
        !           253:       #}
1.40      albertel  254:       $request->print($result);
                    255:     }
1.18      albertel  256:     #$request->print(":Result ends");
1.41      albertel  257:     if ($target eq 'grade') { &showhash(%Apache::lonhomework::results); }
1.27      albertel  258: 
1.41      albertel  259:     #store new values
1.18      albertel  260:     my ($temp) = keys %Apache::lonhomework::results;
                    261:     if ( $temp ne '' ) {
1.41      albertel  262:       &Apache::lonxml::debug("Store return message:".
                    263: 			     &Apache::lonnet::cstore(\%Apache::lonhomework::results));
1.18      albertel  264:     }
1.47    ! albertel  265:     #my $td=&tv_interval($t0);
1.17      www       266:   }
1.41      albertel  267: }
                    268: 
1.42      albertel  269: # with no arg it returns a HTML <option> list of the template titles
                    270: # with one arg it returns the filename associated with the arg passed
                    271: sub get_template_list {
                    272:   my ($namewanted) = @_;
                    273:   my $result;
                    274:   foreach my $file (</home/httpd/html/res/adm/includes/templates/*.problem>) {
                    275:     my $name=&Apache::lonnet::metadata($file,'title');
                    276:     if ($namewanted && ($name eq $namewanted)) {
                    277:       $result=$file;
                    278:       last;
                    279:     } else {
                    280:       $result.="<option>$name</option>";
                    281:     }
                    282:   }
                    283:   return $result;
                    284: }
                    285: 
                    286: sub newproblem {
                    287:   my ($request) = @_;
                    288: 
                    289:   if ($ENV{'form.template'}) {
                    290:     use File::Copy;
                    291:     my $file = &get_template_list($ENV{'form.template'});
                    292:     my $dest = &Apache::lonnet::filelocation("",$request->uri);
                    293:     copy($file,$dest);
                    294:     &renderpage($request,$file);
                    295:   } else {
                    296:     my $templatelist=&get_template_list();
                    297:     my $url=$request->uri;
                    298:     $request->print(<<ENDNEWPROBLEM);
                    299: <body bgcolor="#FFFFFF">
                    300: The request file $url doesn\'t exist. You can create a new problem <br />
                    301: <form action="$url" method="POST">
                    302: <input type="submit" value="New Problem"><br />
                    303: <select name="template">
                    304: $templatelist
                    305: </select>
                    306: </form>
                    307: </body>
                    308: ENDNEWPROBLEM
                    309:   }
                    310: 
                    311:   return '';
                    312: }
                    313: 
                    314: sub view_or_edit_menu {
                    315:   my ($request) = @_;
                    316:   my $url=$request->uri;
                    317:   $request->print(<<EDITMENU);
                    318: <body bgcolor="#FFFFFF">
                    319: <form action="$url" method="POST">
                    320: Would you like to <input type="submit" name="problemmode" value="View"> or
                    321: <input type="submit" name="problemmode" value="Edit"> the problem.
                    322: </form>
                    323: </body>
                    324: EDITMENU
                    325: }
                    326: 
1.41      albertel  327: sub handler {
1.47    ! albertel  328:   #my $t0 = [&gettimeofday()];
1.41      albertel  329:   my $request=$_[0];
                    330: 
1.45      albertel  331:   #if ( $ENV{'user.name'} eq 'albertel' ) {$Apache::lonxml::debug=1;}
1.41      albertel  332: 
                    333:   if (&setupheader($request)) { return OK; }
1.42      albertel  334:   $ENV{'request.uri'}=$request->uri;
1.41      albertel  335: 
                    336:   #setup permissions
                    337:   $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
                    338:   $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
                    339:   &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:");
                    340: 
                    341:   my $file=&Apache::lonnet::filelocation("",$request->uri);
                    342: 
                    343:   #check if we know where we are
                    344:   if ($ENV{'request.course.fn'} && !&Apache::lonnet::symbread()) { 
                    345:     # if we are browsing we might not be able to know where we are
                    346:     if ($Apache::lonhomework::browse ne 'F') {
                    347:       #should know where we are, so ask
                    348:       $request->internal_redirect('/adm/ambiguous'); return;
                    349:     }
                    350:   }
                    351: 
                    352:   if ($ENV{'request.state'} eq "construct") {
                    353:     if ( -e $file ) {
1.42      albertel  354:       if (!(defined $ENV{'form.problemmode'})) {
1.41      albertel  355: 	#first visit to problem in construction space
                    356: 	&view_or_edit_menu($request);
1.42      albertel  357: 	#&renderpage($request,$file);
1.41      albertel  358:       } else {
                    359: 	&renderpage($request,$file);
                    360:       }
                    361:     } else {
                    362:       # requested file doesn't exist in contruction space
                    363:       &newproblem($request);
                    364:     }
                    365:   } else {
                    366:     # just render the page normally outside of construction space
                    367:     &renderpage($request,$file);
                    368:   }
1.47    ! albertel  369:   #my $td=&tv_interval($t0);
        !           370:   #&Apache::lonxml::debug("Spent $td seconds processing");
1.46      albertel  371:   # &Apache::lonhomework::send_footer($request);
1.41      albertel  372:   # always turn off debug messages
1.40      albertel  373:   $Apache::lonxml::debug=0;
1.25      www       374:   return OK;
1.17      www       375: 
1.1       albertel  376: }
                    377: 
                    378: 1;
                    379: __END__

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