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

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.16      albertel   17: 
1.5       albertel   18: sub get_target {
1.15      albertel   19:   if ( $ENV{'request.state'} eq "published") {
1.34      albertel   20:     if ( defined $ENV{'form.submitted'}) {
1.18      albertel   21:       return ('grade', 'web');
                     22:     } else {
                     23:       return ('web');
                     24:     }
1.15      albertel   25:   } elsif ($ENV{'request.state'} eq "construct") {
1.16      albertel   26:     if ( defined $ENV{'form.preview'}) {
1.34      albertel   27:       if ( defined $ENV{'form.submitted'}) {
1.18      albertel   28: 	return ('grade', 'web');
                     29:       } else {
                     30: 	return ('web');
                     31:       }
1.15      albertel   32:     } else {
1.40    ! albertel   33:       return ('grade','web');
        !            34:       if ( $ENV{'form.submitted'} eq 'edit' ) {
        !            35: 	return ('modified','edit');
        !            36:       } else {
        !            37: 	return ('edit');
        !            38:       }
1.15      albertel   39:     }
                     40:   }
1.18      albertel   41:   return ();
1.5       albertel   42: }
                     43: 
1.3       albertel   44: sub setup_vars {
1.5       albertel   45:   my ($target) = @_;
1.11      albertel   46:   return ';'
                     47: #  return ';$external::target='.$target.';';
1.2       albertel   48: }
                     49: 
                     50: sub send_header {
                     51:   my ($request)= @_;
1.14      albertel   52:   $request->print(&Apache::lontexconvert::header());
1.16      albertel   53:   $ENV{'request.uri'}=$request->uri;
                     54: #  $request->print('<form name='.$ENV{'form.request.prefix'}.'lonhomework method="POST" action="'.$request->uri.'">');
1.2       albertel   55: }
                     56: 
1.36      albertel   57: sub createmenu {
                     58:   my ($which,$request)=@_;
                     59:   if ($which eq 'grade') {
                     60:     $request->print('<script language="JavaScript"> 
1.38      albertel   61:           hwkmenu=window.open("/res/adm/pages/homeworkmenu.html","homeworkremote",
1.36      albertel   62:                  "height=350,width=150,menubar=no");
                     63:           </script>');
                     64:   }
                     65: }
                     66: 
1.2       albertel   67: sub send_footer {
                     68:   my ($request)= @_;
1.16      albertel   69: #  $request->print('</form>');
1.14      albertel   70:   $request->print(&Apache::lontexconvert::footer());
1.2       albertel   71: }
                     72: 
1.36      albertel   73: $Apache::lonxml::browse='';
1.20      albertel   74: sub check_date {
1.21      albertel   75:   my ($id) = @_;
1.20      albertel   76:   my $date ='';
1.21      albertel   77:   my $status = '';
                     78:   my $datemsg = '';
                     79:   my $lastdate = '';
                     80:   my $temp;
                     81:   my $type;
                     82:   my $passed;
                     83:   &Apache::lonxml::debug("checking for part :$id:");
                     84:   foreach $temp ("opendate","duedate","answerdate") {
                     85:     $lastdate = $date;
                     86:     $date = &Apache::lonnet::EXT("resource.$id.$temp");
                     87:     &Apache::lonxml::debug("found :$date: for :$temp:");
                     88:     if ($date eq '') {
                     89:       $date = "an unknown date"; $passed = 0;
1.30      albertel   90:     } elsif ($date eq 'con_lost') {
                     91:       $date = "an indeterminate date"; $passed = 0;
1.21      albertel   92:     } else {
                     93:       if (time < $date) { $passed = 0; } else { $passed = 1; }
                     94:       $date = localtime $date;
                     95:     }
                     96:     if (!$passed) { $type=$temp; last; }
1.20      albertel   97:   }
1.21      albertel   98:   &Apache::lonxml::debug("have :$type:$passed:");
                     99:   if ($passed) {
                    100:     $status='SHOW_ANSWER';
                    101:     $datemsg=$date;
                    102:   } elsif ($type eq 'opendate') {
                    103:     $status='CLOSED';
                    104:     $datemsg = "will open on $date";
                    105:   } elsif ($type eq 'duedate') {
                    106:     $status='CAN_ANSWER';
                    107:     $datemsg = "is due at $date";
                    108:   } elsif ($type eq 'answerdate') {
                    109:     $status='CLOSED';
1.32      albertel  110:     $datemsg = "was due on $lastdate, and answers will be available on $date";
1.20      albertel  111:   }
1.27      albertel  112:   if ($status eq 'CAN_ANSWER') {
                    113:     #check #tries
                    114:     my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
                    115:     my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
                    116:     if ( $tries eq '' ) { $tries = '0'; }
                    117:     if ( $maxtries eq '' ) { $maxtries = '2'; } 
                    118:     if ($tries >= $maxtries) { $status = 'CANNOT_ANSWER'; } 
                    119:   }
1.21      albertel  120:   &Apache::lonxml::debug("sending back :$status:$datemsg:");
1.36      albertel  121:   if (($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED')) {
                    122:     &Apache::lonxml::debug("should be allowed to browse a resource when closed");
                    123:     $status='CAN_ANSWER';
                    124:     $datemsg='is closed but you are allowed to view it';
                    125:   }
1.24      albertel  126:   if ($ENV{'request.state'} eq "construct") {
                    127:     &Apache::lonxml::debug("in construction ignoring dates");
                    128:     $status='CAN_ANSWER';
                    129:     $datemsg='is in under construction';
                    130:   }
1.21      albertel  131:   return ($status,$datemsg);
1.20      albertel  132: }
                    133: 
1.17      www       134: sub handler {
1.18      albertel  135:   my $request=$_[0];
1.37      albertel  136:   
1.40    ! albertel  137:  if ( $ENV{'user.name'} eq 'albertel' ) {$Apache::lonxml::debug=1;}
1.18      albertel  138:   
                    139:   my (@targets) = &get_target();
                    140: 
1.17      www       141:   if ($ENV{'browser.mathml'}) {
                    142:     $request->content_type('text/xml');
                    143:   } else {
                    144:     $request->content_type('text/html');
                    145:   }
                    146:   $request->send_http_header;
1.25      www       147:   return OK if $request->header_only;
1.35      albertel  148: 
1.36      albertel  149:   #setup permissions
1.37      albertel  150:   $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
                    151:   $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
1.36      albertel  152: 
1.35      albertel  153:   #check if we know where we are
                    154:   if ($ENV{'request.course.fn'} && !&Apache::lonnet::symbread()) { 
1.36      albertel  155:     # if we are browsing we might not be able to know where we are
                    156:     if ($Apache::lonhomework::browse ne 'F') {
                    157:       #should know where we are, so ask
                    158:       $request->internal_redirect('/adm/ambiguous'); return; 
                    159:     }
1.35      albertel  160:   }
                    161: 
1.17      www       162:   &Apache::lonhomework::send_header($request);
1.39      albertel  163:   &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:");
1.18      albertel  164:   
                    165:   foreach my $target (@targets) {
                    166:     my $file=&Apache::lonnet::filelocation("",$request->uri);
                    167:     my $problem=&Apache::lonnet::getfile($file);
                    168:     if ($problem == -1) {
                    169:       &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
                    170:       $problem='';
                    171:     }
                    172:     
                    173:     my %mystyle;
                    174:     my $result = '';
                    175:     &Apache::inputtags::initialize_inputtags;
                    176:     %Apache::lonhomework::results=();
1.40    ! albertel  177:     %Apache::lonhomework::history=&Apache::lonnet::restore();
1.18      albertel  178:     #ignore error conditions
                    179:     my ($temp)=keys %Apache::lonhomework::history ;
                    180:     if ($temp =~ m/^error:.*/) { %Apache::lonhomework::history=(); } 
1.36      albertel  181:     if ($target eq 'web') {
                    182:       if (&Apache::lonnet::symbread() eq '') {
                    183: 	if ($ENV{'request.state'} eq "construct") {
1.39      albertel  184: 	  $request->print("In construction space, submissions ignored<br />");
1.36      albertel  185: 	} else {
1.39      albertel  186: 	  $request->print("Browsing or <a href=\"/adm/ambiguous\">ambiguous</a> reference, submissions ignored<br />");
1.36      albertel  187: 	}
                    188:       }
                    189:       if ($Apache::lonhomework::viewgrades eq 'F') {&createmenu('grade',$request); }
1.20      albertel  190:     }
1.18      albertel  191:     if ($target eq 'grade') {
                    192:       my $resultkey;
                    193:       foreach $resultkey (sort keys %Apache::lonhomework::history) {
                    194: 	&Apache::lonxml::debug("$resultkey ---- $Apache::lonhomework::history{$resultkey}");
                    195:       }
1.39      albertel  196:       &Apache::lonxml::debug("\n<br />restored values^</br>\n");
1.18      albertel  197:     }
                    198:     my $default=&Apache::lonnet::getfile('/home/httpd/html/res/adm/includes/default_homework.lcpm');
                    199:     if ($default == -1) {
                    200:       &Apache::lonxml::error("<b>Unable to find <i>default_homework.lcpm</i></b>");
                    201:       $default='';
                    202:     }
                    203:     $result = &Apache::lonxml::xmlparse($target, $problem,
                    204: 			$default.&setup_vars($target),%mystyle);
1.17      www       205: 
1.18      albertel  206:     #$request->print("Result follows:");
1.40    ! albertel  207:     if ($target eq 'modified') {
        !           208:       my $fileout = &Apache::lonnet::filelocation("",$request->uri);
        !           209:       my $filesave=$fileout.".bak";
        !           210: 
        !           211:       my $fs=Apache::File->new(">$filesave");
        !           212:       print $fs $problem;
        !           213:       $request->print("<b>Making Backup to $filesave</b><br />");
        !           214: 
        !           215:       my $fh=Apache::File->new(">$fileout");
        !           216:       print $fh $result;
        !           217:       $request->print("<b>Saving Modifications to $fileout</b><br />");
        !           218:     } else {
        !           219:       $request->print($result);
        !           220:     }
1.18      albertel  221:     #$request->print(":Result ends");
                    222:     if ($target eq 'grade') {
1.27      albertel  223: 
1.18      albertel  224:       my $resultkey;
                    225:       foreach $resultkey (sort keys %Apache::lonhomework::results) {
                    226: 	&Apache::lonxml::debug("$resultkey ---- $Apache::lonhomework::results{$resultkey}");
                    227:       }
1.39      albertel  228:       &Apache::lonxml::debug("\n<br />storing values^</br>\n");
1.18      albertel  229:     }
                    230:     
                    231:     my ($temp) = keys %Apache::lonhomework::results;
                    232:     if ( $temp ne '' ) {
1.40    ! albertel  233:       &Apache::lonxml::debug("Store return message:".&Apache::lonnet::cstore(\%Apache::lonhomework::results));
1.18      albertel  234:     }
1.17      www       235:   }
                    236:   &Apache::lonhomework::send_footer($request);
1.40    ! albertel  237:   # always turn of debug messages
        !           238:   $Apache::lonxml::debug=0;
1.25      www       239:   return OK;
1.17      www       240: 
1.1       albertel  241: }
                    242: 
                    243: 1;
                    244: __END__

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