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

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.20    ! albertel   55: sub check_date {
        !            56:   my ($type,$id) = @_;
        !            57:   my $date ='';
        !            58:   my $passed = '';
        !            59:   &Apache::lonxml::debug("checking :$type: for part :$id:");
        !            60:   if ($type eq 'OPEN_DATE') {
        !            61:     $date = &Apache::lonnet::EXT("resource.$id.opendate");
        !            62:   } elsif ($type eq 'DUE_DATE') {
        !            63:     $date = &Apache::lonnet::EXT("resource.$id.duedate");
        !            64:   } elsif ($type eq 'START_DATE') {
        !            65:     $date = &Apache::lonnet::EXT("resource.$id.duedate");
        !            66:   } else {
        !            67:     &Apache::lonxml::error("Unknown date request $type");
        !            68:     $date = '';
        !            69:   }
        !            70:   &Apache::lonxml::debug("found :$date:");
        !            71:   if ($date eq '') {
        !            72:     $date = "an unknown date"; $passed = 0;
        !            73:   } else {
        !            74:     if (time < $date) { $passed = 0; } else { $passed = 1; }
        !            75:     $date = localtime $date;
        !            76:   }
        !            77:   &Apache::lonxml::debug("sending back :$date:$passed:");
        !            78:   return ($date,$passed);
        !            79: }
        !            80: 
1.17      www        81: sub handler {
1.18      albertel   82:   my $request=$_[0];
1.17      www        83: 
1.20    ! albertel   84:   if ( $ENV{'user.name'} eq 'albertel' ) { $Apache::lonxml::debug=1; }
1.18      albertel   85:   
                     86:   my (@targets) = &get_target();
                     87: 
1.17      www        88:   if ($ENV{'browser.mathml'}) {
                     89:     $request->content_type('text/xml');
                     90:   } else {
                     91:     $request->content_type('text/html');
                     92:   }
                     93:   $request->send_http_header;
                     94:   return 'OK' if $request->header_only;
1.18      albertel   95:   
1.17      www        96:   &Apache::lonhomework::send_header($request);
1.18      albertel   97:   
1.20    ! albertel   98:   #check if allowed to see
        !            99:   
1.18      albertel  100:   foreach my $target (@targets) {
                    101:     my $file=&Apache::lonnet::filelocation("",$request->uri);
                    102:     my $problem=&Apache::lonnet::getfile($file);
                    103:     if ($problem == -1) {
                    104:       &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
                    105:       $problem='';
                    106:     }
                    107:     
                    108:     my %mystyle;
                    109:     my $result = '';
                    110:     &Apache::inputtags::initialize_inputtags;
                    111:     %Apache::lonhomework::results=();
                    112:     %Apache::lonhomework::history=&Apache::lonnet::restore;
                    113:     #ignore error conditions
                    114:     my ($temp)=keys %Apache::lonhomework::history ;
                    115:     if ($temp =~ m/^error:.*/) { %Apache::lonhomework::history=(); } 
1.20    ! albertel  116:     if ($target eq 'web' && &Apache::lonnet::symbread() eq '') {
        !           117:       $request->print("Vieiwing this problem as not a student, submissions will be ignored.");
        !           118:     }
1.18      albertel  119:     if ($target eq 'grade') {
                    120:       my $resultkey;
                    121:       foreach $resultkey (sort keys %Apache::lonhomework::history) {
                    122: 	&Apache::lonxml::debug("$resultkey ---- $Apache::lonhomework::history{$resultkey}");
                    123:       }
                    124:       &Apache::lonxml::debug("\n<br>restored values^</br>\n");
                    125:     }
                    126:     my $default=&Apache::lonnet::getfile('/home/httpd/html/res/adm/includes/default_homework.lcpm');
                    127:     if ($default == -1) {
                    128:       &Apache::lonxml::error("<b>Unable to find <i>default_homework.lcpm</i></b>");
                    129:       $default='';
                    130:     }
                    131:     if ( $target eq 'grade') { 
                    132:       $Apache::lonxml::textredirection = 0; 
                    133:     } else {
                    134:       $Apache::lonxml::textredirection = 1; 
                    135:     }
                    136:     $result = &Apache::lonxml::xmlparse($target, $problem,
                    137: 			$default.&setup_vars($target),%mystyle);
1.17      www       138: 
1.18      albertel  139:     #$request->print("Result follows:");
                    140:     $request->print($result);
                    141:     #$request->print(":Result ends");
                    142:     if ($target eq 'grade') {
                    143:       my $resultkey;
                    144:       foreach $resultkey (sort keys %Apache::lonhomework::results) {
                    145: 	&Apache::lonxml::debug("$resultkey ---- $Apache::lonhomework::results{$resultkey}");
                    146:       }
                    147:       &Apache::lonxml::debug("\n<br>storing values^</br>\n");
                    148:     }
                    149:     
                    150:     my ($temp) = keys %Apache::lonhomework::results;
                    151:     if ( $temp ne '' ) {
                    152:       &Apache::lonxml::debug("Store return message:".&Apache::lonnet::cstore(%Apache::lonhomework::results));
                    153:     }
1.17      www       154:   }
                    155:   &Apache::lonhomework::send_footer($request);
1.18      albertel  156:   
1.17      www       157:   return 'OK';
                    158: 
1.1       albertel  159: }
                    160: 
                    161: 1;
                    162: __END__

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