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

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

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