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

1.1       albertel    1: # The LON-CAPA Homework handler
                      2: # Guy Albertelli
                      3: 
                      4: package Apache::lonhomework;
                      5: use strict;
                      6: use Apache::style;
                      7: use Apache::lonxml;
1.2       albertel    8: use Apache::lonnet;
                      9: use Apache::inputtags;
                     10: use Apache::structuretags;
1.9       albertel   11: use Apache::response;
1.1       albertel   12: 
1.16    ! albertel   13: 
1.1       albertel   14: sub handler {
                     15:   my $request=shift;
1.15      albertel   16:   
1.5       albertel   17:   my $target=&get_target();
1.12      albertel   18:   $Apache::lonxml::debug=1;
1.14      albertel   19:   if ($ENV{'browser.mathml'}) {
                     20:     $request->content_type('text/xml');
                     21:   } else {
                     22:     $request->content_type('text/html');
                     23:   }
1.2       albertel   24:   $request->send_http_header;
1.14      albertel   25:   return 'OK' if $request->header_only;
                     26: 
1.2       albertel   27:   &Apache::lonhomework::send_header($request);
1.15      albertel   28:   my $file = &Apache::lonnet::filelocation("",$request->uri);
1.2       albertel   29:   my %mystyle;
1.5       albertel   30:   my $result = '';
1.2       albertel   31:   &Apache::inputtags::initialize_inputtags;
1.16    ! albertel   32:   %Apache::lonhomework::results=();
        !            33:   %Apache::lonhomework::history=&Apache::lonnet::restore;
        !            34:   #ignore error conditions
        !            35:   my ($temp)=keys %Apache::lonhomework::history ;
        !            36:   if ($temp =~ m/^error:.*/) { %Apache::lonhomework::history=(); } 
        !            37: 
        !            38:   my $resultkey;
        !            39:   foreach $resultkey (sort keys %Apache::lonhomework::history) {
        !            40:     &Apache::lonxml::debug("$resultkey ---- $Apache::lonhomework::history{$resultkey}");
        !            41:   }
        !            42:   &Apache::lonxml::debug("\n<br>restored values^</br>\n");
1.13      albertel   43: 
                     44:   my $problem=&Apache::lonnet::getfile($file);
                     45:   if ($problem == -1) {
                     46:     &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
                     47:     $problem='';
                     48:   }
                     49:   my $default=&Apache::lonnet::getfile('/home/httpd/html/res/adm/includes/default_homework.lcpm');
                     50:   if ($default == -1) {
                     51:     &Apache::lonxml::error("<b>Unable to find <i>$file</i></b>");
                     52:     $default='';
                     53:   }
                     54: 
                     55:   $result = Apache::lonxml::xmlparse($target, $problem,
                     56: 				     $default.&setup_vars($target),%mystyle);
1.2       albertel   57:   #$request->print("Result follows:");
                     58:   $request->print($result);
                     59:   #$request->print(":Result ends");
1.16    ! albertel   60: 
        !            61:   foreach $resultkey (sort keys %Apache::lonhomework::results) {
        !            62:     &Apache::lonxml::debug("$resultkey ---- $Apache::lonhomework::results{$resultkey}");
        !            63:   }
        !            64:   &Apache::lonxml::debug("\n<br>storing values^</br>\n");
        !            65: 
1.2       albertel   66:   &Apache::lonhomework::send_footer($request);
1.16    ! albertel   67:   ($temp) = keys %Apache::lonhomework::results;
        !            68:   if ( $temp ne '' ) {
        !            69:     &Apache::lonxml::debug("Store return message:".&Apache::lonnet::cstore(%Apache::lonhomework::results));
        !            70:   }
1.14      albertel   71:   return 'OK';
1.3       albertel   72: }
                     73: 
1.5       albertel   74: sub get_target {
1.15      albertel   75:   if ( $ENV{'request.state'} eq "published") {
                     76:     return 'web';
                     77:   } elsif ($ENV{'request.state'} eq "construct") {
1.16    ! albertel   78:     if ( defined $ENV{'form.preview'}) {
1.15      albertel   79:       return 'web';
                     80:     } else {
                     81:       return 'edit';
                     82:     }
                     83:   }
1.5       albertel   84: }
                     85: 
1.3       albertel   86: sub setup_vars {
1.5       albertel   87:   my ($target) = @_;
1.11      albertel   88:   return ';'
                     89: #  return ';$external::target='.$target.';';
1.2       albertel   90: }
                     91: 
                     92: sub send_header {
                     93:   my ($request)= @_;
1.14      albertel   94:   $request->print(&Apache::lontexconvert::header());
1.16    ! albertel   95:   $ENV{'request.uri'}=$request->uri;
        !            96: #  $request->print('<form name='.$ENV{'form.request.prefix'}.'lonhomework method="POST" action="'.$request->uri.'">');
1.2       albertel   97: }
                     98: 
                     99: sub send_footer {
                    100:   my ($request)= @_;
1.16    ! albertel  101: #  $request->print('</form>');
1.14      albertel  102:   $request->print(&Apache::lontexconvert::footer());
1.2       albertel  103: }
                    104: 
1.15      albertel  105: sub getfilenothere {
1.2       albertel  106:   my ($filename) = @_;
                    107:   my $a="";
1.8       albertel  108:   if (! -e $filename ) {
                    109:     &Apache::lonnet::subscribe($filename);
                    110:     &Apache::lonnet::repcopy($filename);
                    111:   }
1.1       albertel  112:   {
1.8       albertel  113:     my $fh=Apache::File->new($filename);
1.2       albertel  114: 
                    115:     while (<$fh>) {
                    116:       $a .=$_;
                    117:     }
1.1       albertel  118:   }
1.2       albertel  119:   return $a
1.1       albertel  120: }
                    121: 
                    122: 1;
                    123: __END__

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