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

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

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