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

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.26      www        15: use Apache::Constants qw(:common);
1.16      albertel   16: 
1.5       albertel   17: sub get_target {
1.15      albertel   18:   if ( $ENV{'request.state'} eq "published") {
1.18      albertel   19:     if ( defined $ENV{'form.submit'}) {
                     20:       return ('grade', 'web');
                     21:     } else {
                     22:       return ('web');
                     23:     }
1.15      albertel   24:   } elsif ($ENV{'request.state'} eq "construct") {
1.16      albertel   25:     if ( defined $ENV{'form.preview'}) {
1.18      albertel   26:       if ( defined $ENV{'form.submit'}) {
                     27: 	return ('grade', 'web');
                     28:       } else {
                     29: 	return ('web');
                     30:       }
1.15      albertel   31:     } else {
1.29    ! albertel   32:       #return ('edit');
        !            33:       #edit is currently broken
        !            34:       return ('web');
1.15      albertel   35:     }
                     36:   }
1.18      albertel   37:   return ();
1.5       albertel   38: }
                     39: 
1.3       albertel   40: sub setup_vars {
1.5       albertel   41:   my ($target) = @_;
1.11      albertel   42:   return ';'
                     43: #  return ';$external::target='.$target.';';
1.2       albertel   44: }
                     45: 
                     46: sub send_header {
                     47:   my ($request)= @_;
1.14      albertel   48:   $request->print(&Apache::lontexconvert::header());
1.16      albertel   49:   $ENV{'request.uri'}=$request->uri;
                     50: #  $request->print('<form name='.$ENV{'form.request.prefix'}.'lonhomework method="POST" action="'.$request->uri.'">');
1.2       albertel   51: }
                     52: 
                     53: sub send_footer {
                     54:   my ($request)= @_;
1.16      albertel   55: #  $request->print('</form>');
1.14      albertel   56:   $request->print(&Apache::lontexconvert::footer());
1.2       albertel   57: }
                     58: 
1.21      albertel   59: #@Apache::lonhomework::date_status=();
                     60: #@Apache::lonhomework::date_msg=();
1.20      albertel   61: sub check_date {
1.21      albertel   62:   my ($id) = @_;
1.20      albertel   63:   my $date ='';
1.21      albertel   64:   my $status = '';
                     65:   my $datemsg = '';
                     66:   my $lastdate = '';
                     67:   my $temp;
                     68:   my $type;
                     69:   my $passed;
                     70:   &Apache::lonxml::debug("checking for part :$id:");
                     71:   foreach $temp ("opendate","duedate","answerdate") {
                     72:     $lastdate = $date;
                     73:     $date = &Apache::lonnet::EXT("resource.$id.$temp");
                     74:     &Apache::lonxml::debug("found :$date: for :$temp:");
                     75:     if ($date eq '') {
                     76:       $date = "an unknown date"; $passed = 0;
                     77:     } else {
                     78:       if (time < $date) { $passed = 0; } else { $passed = 1; }
                     79:       $date = localtime $date;
                     80:     }
                     81:     if (!$passed) { $type=$temp; last; }
1.20      albertel   82:   }
1.21      albertel   83:   &Apache::lonxml::debug("have :$type:$passed:");
                     84:   if ($passed) {
                     85:     $status='SHOW_ANSWER';
                     86:     $datemsg=$date;
                     87:   } elsif ($type eq 'opendate') {
                     88:     $status='CLOSED';
                     89:     $datemsg = "will open on $date";
                     90:   } elsif ($type eq 'duedate') {
                     91:     $status='CAN_ANSWER';
                     92:     $datemsg = "is due at $date";
                     93:   } elsif ($type eq 'answerdate') {
                     94:     $status='CLOSED';
1.23      albertel   95:     $datemsg = "was due as $lastdate, and answers will be available at $date";
1.20      albertel   96:   }
1.27      albertel   97:   if ($status eq 'CAN_ANSWER') {
                     98:     #check #tries
                     99:     my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
                    100:     my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
                    101:     if ( $tries eq '' ) { $tries = '0'; }
                    102:     if ( $maxtries eq '' ) { $maxtries = '2'; } 
                    103:     if ($tries >= $maxtries) { $status = 'CANNOT_ANSWER'; } 
                    104:   }
1.21      albertel  105:   &Apache::lonxml::debug("sending back :$status:$datemsg:");
1.24      albertel  106:   if ($ENV{'request.state'} eq "construct") {
                    107:     &Apache::lonxml::debug("in construction ignoring dates");
                    108:     $status='CAN_ANSWER';
                    109:     $datemsg='is in under construction';
                    110:   }
1.21      albertel  111:   return ($status,$datemsg);
1.20      albertel  112: }
                    113: 
1.17      www       114: sub handler {
1.18      albertel  115:   my $request=$_[0];
1.17      www       116: 
1.20      albertel  117:   if ( $ENV{'user.name'} eq 'albertel' ) { $Apache::lonxml::debug=1; }
1.18      albertel  118:   
                    119:   my (@targets) = &get_target();
                    120: 
1.17      www       121:   if ($ENV{'browser.mathml'}) {
                    122:     $request->content_type('text/xml');
                    123:   } else {
                    124:     $request->content_type('text/html');
                    125:   }
                    126:   $request->send_http_header;
1.25      www       127:   return OK if $request->header_only;
1.18      albertel  128:   
1.17      www       129:   &Apache::lonhomework::send_header($request);
1.18      albertel  130:   
1.20      albertel  131:   #check if allowed to see
                    132:   
1.18      albertel  133:   foreach my $target (@targets) {
                    134:     my $file=&Apache::lonnet::filelocation("",$request->uri);
                    135:     my $problem=&Apache::lonnet::getfile($file);
                    136:     if ($problem == -1) {
                    137:       &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
                    138:       $problem='';
                    139:     }
                    140:     
                    141:     my %mystyle;
                    142:     my $result = '';
                    143:     &Apache::inputtags::initialize_inputtags;
                    144:     %Apache::lonhomework::results=();
                    145:     %Apache::lonhomework::history=&Apache::lonnet::restore;
                    146:     #ignore error conditions
                    147:     my ($temp)=keys %Apache::lonhomework::history ;
                    148:     if ($temp =~ m/^error:.*/) { %Apache::lonhomework::history=(); } 
1.20      albertel  149:     if ($target eq 'web' && &Apache::lonnet::symbread() eq '') {
                    150:       $request->print("Vieiwing this problem as not a student, submissions will be ignored.");
                    151:     }
1.18      albertel  152:     if ($target eq 'grade') {
                    153:       my $resultkey;
                    154:       foreach $resultkey (sort keys %Apache::lonhomework::history) {
                    155: 	&Apache::lonxml::debug("$resultkey ---- $Apache::lonhomework::history{$resultkey}");
                    156:       }
                    157:       &Apache::lonxml::debug("\n<br>restored values^</br>\n");
                    158:     }
                    159:     my $default=&Apache::lonnet::getfile('/home/httpd/html/res/adm/includes/default_homework.lcpm');
                    160:     if ($default == -1) {
                    161:       &Apache::lonxml::error("<b>Unable to find <i>default_homework.lcpm</i></b>");
                    162:       $default='';
                    163:     }
                    164:     $result = &Apache::lonxml::xmlparse($target, $problem,
                    165: 			$default.&setup_vars($target),%mystyle);
1.17      www       166: 
1.18      albertel  167:     #$request->print("Result follows:");
                    168:     $request->print($result);
                    169:     #$request->print(":Result ends");
                    170:     if ($target eq 'grade') {
1.27      albertel  171: 
1.18      albertel  172:       my $resultkey;
                    173:       foreach $resultkey (sort keys %Apache::lonhomework::results) {
                    174: 	&Apache::lonxml::debug("$resultkey ---- $Apache::lonhomework::results{$resultkey}");
                    175:       }
                    176:       &Apache::lonxml::debug("\n<br>storing values^</br>\n");
                    177:     }
                    178:     
                    179:     my ($temp) = keys %Apache::lonhomework::results;
                    180:     if ( $temp ne '' ) {
                    181:       &Apache::lonxml::debug("Store return message:".&Apache::lonnet::cstore(%Apache::lonhomework::results));
                    182:     }
1.17      www       183:   }
                    184:   &Apache::lonhomework::send_footer($request);
1.18      albertel  185:   
1.25      www       186:   return OK;
1.17      www       187: 
1.1       albertel  188: }
                    189: 
                    190: 1;
                    191: __END__

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