File:  [LON-CAPA] / loncom / homework / lonhomework.pm
Revision 1.36: download - view: text, annotated - select for diffs
Wed Feb 7 00:29:30 2001 UTC (23 years, 9 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- trying to add some support for viewing submissions

    1: # The LON-CAPA Homework handler
    2: # Guy Albertelli
    3: # 11/30 Gerd Kortemeyer
    4: # 6/1 Gerd Kortemeyer
    5: 
    6: package Apache::lonhomework;
    7: use strict;
    8: use Apache::style;
    9: use Apache::lonxml;
   10: use Apache::lonnet;
   11: use Apache::inputtags;
   12: use Apache::structuretags;
   13: use Apache::response;
   14: use Apache::hint;
   15: use Apache::outputtags;
   16: use Apache::Constants qw(:common);
   17: 
   18: sub get_target {
   19:   if ( $ENV{'request.state'} eq "published") {
   20:     if ( defined $ENV{'form.submitted'}) {
   21:       return ('grade', 'web');
   22:     } else {
   23:       return ('web');
   24:     }
   25:   } elsif ($ENV{'request.state'} eq "construct") {
   26:     if ( defined $ENV{'form.preview'}) {
   27:       if ( defined $ENV{'form.submitted'}) {
   28: 	return ('grade', 'web');
   29:       } else {
   30: 	return ('web');
   31:       }
   32:     } else {
   33:       #return ('edit');
   34:       #edit is currently broken
   35:       return ('web');
   36:     }
   37:   }
   38:   return ();
   39: }
   40: 
   41: sub setup_vars {
   42:   my ($target) = @_;
   43:   return ';'
   44: #  return ';$external::target='.$target.';';
   45: }
   46: 
   47: sub send_header {
   48:   my ($request)= @_;
   49:   $request->print(&Apache::lontexconvert::header());
   50:   $ENV{'request.uri'}=$request->uri;
   51: #  $request->print('<form name='.$ENV{'form.request.prefix'}.'lonhomework method="POST" action="'.$request->uri.'">');
   52: }
   53: 
   54: sub createmenu {
   55:   my ($which,$request)=@_;
   56:   if ($which eq 'grade') {
   57:     $request->print('<script language="JavaScript"> 
   58:           hwkmenu=window.open("/res/adm/pages/homeworkmenu.html","LONCAPAhomeworkmenu",
   59:                  "height=350,width=150,menubar=no");
   60:           </script>');
   61:   }
   62: }
   63: 
   64: sub send_footer {
   65:   my ($request)= @_;
   66: #  $request->print('</form>');
   67:   $request->print(&Apache::lontexconvert::footer());
   68: }
   69: 
   70: $Apache::lonxml::browse='';
   71: sub check_date {
   72:   my ($id) = @_;
   73:   my $date ='';
   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;
   87:     } elsif ($date eq 'con_lost') {
   88:       $date = "an indeterminate date"; $passed = 0;
   89:     } else {
   90:       if (time < $date) { $passed = 0; } else { $passed = 1; }
   91:       $date = localtime $date;
   92:     }
   93:     if (!$passed) { $type=$temp; last; }
   94:   }
   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';
  107:     $datemsg = "was due on $lastdate, and answers will be available on $date";
  108:   }
  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:   }
  117:   &Apache::lonxml::debug("sending back :$status:$datemsg:");
  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:   }
  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:   }
  128:   return ($status,$datemsg);
  129: }
  130: 
  131: sub handler {
  132:   my $request=$_[0];
  133: 
  134:   if ( $ENV{'user.name'} eq 'albertel' ) {$Apache::lonxml::debug=1;} else {$Apache::lonxml::debug=0;}
  135:   
  136:   my (@targets) = &get_target();
  137: 
  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;
  144:   return OK if $request->header_only;
  145: 
  146:   #setup permissions
  147:   $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre');
  148:   $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr');
  149:   &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:");
  150: 
  151:   #check if we know where we are
  152:   if ($ENV{'request.course.fn'} && !&Apache::lonnet::symbread()) { 
  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:     }
  158:   }
  159: 
  160:   &Apache::lonhomework::send_header($request);
  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=(); } 
  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); }
  187:     }
  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);
  202: 
  203:     #$request->print("Result follows:");
  204:     $request->print($result);
  205:     #$request->print(":Result ends");
  206:     if ($target eq 'grade') {
  207: 
  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:     }
  219:   }
  220:   &Apache::lonhomework::send_footer($request);
  221:   
  222:   return OK;
  223: 
  224: }
  225: 
  226: 1;
  227: __END__

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