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

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.43      albertel   18: 
                     19: sub BEGIN {
                     20:   &Apache::lonxml::register_insert();
                     21: }
                     22: 
1.5       albertel   23: sub get_target {
1.15      albertel   24:   if ( $ENV{'request.state'} eq "published") {
1.34      albertel   25:     if ( defined $ENV{'form.submitted'}) {
1.18      albertel   26:       return ('grade', 'web');
                     27:     } else {
                     28:       return ('web');
                     29:     }
1.15      albertel   30:   } elsif ($ENV{'request.state'} eq "construct") {
1.16      albertel   31:     if ( defined $ENV{'form.preview'}) {
1.34      albertel   32:       if ( defined $ENV{'form.submitted'}) {
1.18      albertel   33: 	return ('grade', 'web');
                     34:       } else {
                     35: 	return ('web');
                     36:       }
1.15      albertel   37:     } else {
1.42      albertel   38:       if ( $ENV{'form.problemmode'} eq 'View' ) {
                     39: 	if ( defined $ENV{'form.submitted'}) {
                     40: 	  return ('grade', 'web');
                     41: 	} else {
                     42: 	  return ('web');
                     43: 	}
                     44:       } elsif ( $ENV{'form.problemmode'} eq 'Edit' ) {
                     45: 	if ( $ENV{'form.submitted'} eq 'edit' ) {
                     46: 	  return ('modified','edit');
                     47: 	} else {
                     48: 	  return ('edit');
                     49: 	}
1.40      albertel   50:       } else {
1.42      albertel   51: 	return ('web');
1.40      albertel   52:       }
1.15      albertel   53:     }
                     54:   }
1.18      albertel   55:   return ();
1.5       albertel   56: }
                     57: 
1.3       albertel   58: sub setup_vars {
1.5       albertel   59:   my ($target) = @_;
1.11      albertel   60:   return ';'
                     61: #  return ';$external::target='.$target.';';
1.2       albertel   62: }
                     63: 
                     64: sub send_header {
                     65:   my ($request)= @_;
1.14      albertel   66:   $request->print(&Apache::lontexconvert::header());
1.16      albertel   67: #  $request->print('<form name='.$ENV{'form.request.prefix'}.'lonhomework method="POST" action="'.$request->uri.'">');
1.2       albertel   68: }
                     69: 
1.36      albertel   70: sub createmenu {
                     71:   my ($which,$request)=@_;
                     72:   if ($which eq 'grade') {
                     73:     $request->print('<script language="JavaScript"> 
1.38      albertel   74:           hwkmenu=window.open("/res/adm/pages/homeworkmenu.html","homeworkremote",
1.36      albertel   75:                  "height=350,width=150,menubar=no");
                     76:           </script>');
                     77:   }
                     78: }
                     79: 
1.2       albertel   80: sub send_footer {
                     81:   my ($request)= @_;
1.16      albertel   82: #  $request->print('</form>');
1.14      albertel   83:   $request->print(&Apache::lontexconvert::footer());
1.2       albertel   84: }
                     85: 
1.36      albertel   86: $Apache::lonxml::browse='';
1.20      albertel   87: sub check_date {
1.21      albertel   88:   my ($id) = @_;
1.20      albertel   89:   my $date ='';
1.21      albertel   90:   my $status = '';
                     91:   my $datemsg = '';
                     92:   my $lastdate = '';
                     93:   my $temp;
                     94:   my $type;
                     95:   my $passed;
                     96:   &Apache::lonxml::debug("checking for part :$id:");
                     97:   foreach $temp ("opendate","duedate","answerdate") {
                     98:     $lastdate = $date;
                     99:     $date = &Apache::lonnet::EXT("resource.$id.$temp");
                    100:     &Apache::lonxml::debug("found :$date: for :$temp:");
                    101:     if ($date eq '') {
                    102:       $date = "an unknown date"; $passed = 0;
1.30      albertel  103:     } elsif ($date eq 'con_lost') {
                    104:       $date = "an indeterminate date"; $passed = 0;
1.21      albertel  105:     } else {
                    106:       if (time < $date) { $passed = 0; } else { $passed = 1; }
                    107:       $date = localtime $date;
                    108:     }
                    109:     if (!$passed) { $type=$temp; last; }
1.20      albertel  110:   }
1.21      albertel  111:   &Apache::lonxml::debug("have :$type:$passed:");
                    112:   if ($passed) {
                    113:     $status='SHOW_ANSWER';
                    114:     $datemsg=$date;
                    115:   } elsif ($type eq 'opendate') {
                    116:     $status='CLOSED';
                    117:     $datemsg = "will open on $date";
                    118:   } elsif ($type eq 'duedate') {
                    119:     $status='CAN_ANSWER';
                    120:     $datemsg = "is due at $date";
                    121:   } elsif ($type eq 'answerdate') {
                    122:     $status='CLOSED';
1.32      albertel  123:     $datemsg = "was due on $lastdate, and answers will be available on $date";
1.20      albertel  124:   }
1.27      albertel  125:   if ($status eq 'CAN_ANSWER') {
                    126:     #check #tries
                    127:     my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
                    128:     my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
                    129:     if ( $tries eq '' ) { $tries = '0'; }
                    130:     if ( $maxtries eq '' ) { $maxtries = '2'; } 
                    131:     if ($tries >= $maxtries) { $status = 'CANNOT_ANSWER'; } 
                    132:   }
1.21      albertel  133:   &Apache::lonxml::debug("sending back :$status:$datemsg:");
1.36      albertel  134:   if (($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED')) {
                    135:     &Apache::lonxml::debug("should be allowed to browse a resource when closed");
                    136:     $status='CAN_ANSWER';
                    137:     $datemsg='is closed but you are allowed to view it';
                    138:   }
1.24      albertel  139:   if ($ENV{'request.state'} eq "construct") {
                    140:     &Apache::lonxml::debug("in construction ignoring dates");
                    141:     $status='CAN_ANSWER';
                    142:     $datemsg='is in under construction';
                    143:   }
1.21      albertel  144:   return ($status,$datemsg);
1.20      albertel  145: }
                    146: 
1.41      albertel  147: sub showhash {
                    148:   my (%hash) = @_;
                    149:   my $resultkey;
                    150:   foreach $resultkey (sort keys %hash) {
                    151:     &Apache::lonxml::debug("$resultkey ---- $hash{$resultkey}");
                    152:   }
                    153:   &Apache::lonxml::debug("\n<br />restored values^</br>\n");
                    154:   return '';
                    155: }
                    156: 
                    157: sub setuppermissions {
                    158:   $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
                    159:   $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
                    160:   return ''
                    161: }
                    162: 
                    163: sub setupheader {
1.18      albertel  164:   my $request=$_[0];
1.17      www       165:   if ($ENV{'browser.mathml'}) {
                    166:     $request->content_type('text/xml');
                    167:   } else {
                    168:     $request->content_type('text/html');
                    169:   }
                    170:   $request->send_http_header;
1.25      www       171:   return OK if $request->header_only;
1.41      albertel  172:   return ''
                    173: }
1.35      albertel  174: 
1.41      albertel  175: sub renderpage {
                    176:   my ($request,$file) = @_;
1.36      albertel  177: 
1.41      albertel  178:   my (@targets) = &get_target();
1.18      albertel  179:   foreach my $target (@targets) {
                    180:     my $problem=&Apache::lonnet::getfile($file);
                    181:     if ($problem == -1) {
                    182:       &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
                    183:       $problem='';
                    184:     }
1.41      albertel  185: 
1.18      albertel  186:     my %mystyle;
                    187:     my $result = '';
                    188:     &Apache::inputtags::initialize_inputtags;
1.46    ! albertel  189:     &Apache::edit::initialize_edit;
1.18      albertel  190:     %Apache::lonhomework::results=();
1.40      albertel  191:     %Apache::lonhomework::history=&Apache::lonnet::restore();
1.18      albertel  192:     #ignore error conditions
                    193:     my ($temp)=keys %Apache::lonhomework::history ;
                    194:     if ($temp =~ m/^error:.*/) { %Apache::lonhomework::history=(); } 
1.36      albertel  195:     if ($target eq 'web') {
                    196:       if (&Apache::lonnet::symbread() eq '') {
                    197: 	if ($ENV{'request.state'} eq "construct") {
1.39      albertel  198: 	  $request->print("In construction space, submissions ignored<br />");
1.36      albertel  199: 	} else {
1.39      albertel  200: 	  $request->print("Browsing or <a href=\"/adm/ambiguous\">ambiguous</a> reference, submissions ignored<br />");
1.36      albertel  201: 	}
                    202:       }
1.45      albertel  203:       #if ($Apache::lonhomework::viewgrades eq 'F') {&createmenu('grade',$request); }
1.20      albertel  204:     }
1.41      albertel  205:     if ($target eq 'grade') { &showhash(%Apache::lonhomework::history); }
                    206: 
1.18      albertel  207:     my $default=&Apache::lonnet::getfile('/home/httpd/html/res/adm/includes/default_homework.lcpm');
                    208:     if ($default == -1) {
                    209:       &Apache::lonxml::error("<b>Unable to find <i>default_homework.lcpm</i></b>");
                    210:       $default='';
                    211:     }
                    212:     $result = &Apache::lonxml::xmlparse($target, $problem,
                    213: 			$default.&setup_vars($target),%mystyle);
1.17      www       214: 
1.18      albertel  215:     #$request->print("Result follows:");
1.40      albertel  216:     if ($target eq 'modified') {
                    217:       my $fileout = &Apache::lonnet::filelocation("",$request->uri);
                    218:       my $filesave=$fileout.".bak";
                    219: 
                    220:       my $fs=Apache::File->new(">$filesave");
                    221:       print $fs $problem;
                    222:       $request->print("<b>Making Backup to $filesave</b><br />");
                    223: 
                    224:       my $fh=Apache::File->new(">$fileout");
                    225:       print $fh $result;
                    226:       $request->print("<b>Saving Modifications to $fileout</b><br />");
                    227:     } else {
                    228:       $request->print($result);
                    229:     }
1.18      albertel  230:     #$request->print(":Result ends");
1.41      albertel  231:     if ($target eq 'grade') { &showhash(%Apache::lonhomework::results); }
1.27      albertel  232: 
1.41      albertel  233:     #store new values
1.18      albertel  234:     my ($temp) = keys %Apache::lonhomework::results;
                    235:     if ( $temp ne '' ) {
1.41      albertel  236:       &Apache::lonxml::debug("Store return message:".
                    237: 			     &Apache::lonnet::cstore(\%Apache::lonhomework::results));
1.18      albertel  238:     }
1.17      www       239:   }
1.41      albertel  240: }
                    241: 
1.42      albertel  242: # with no arg it returns a HTML <option> list of the template titles
                    243: # with one arg it returns the filename associated with the arg passed
                    244: sub get_template_list {
                    245:   my ($namewanted) = @_;
                    246:   my $result;
                    247:   foreach my $file (</home/httpd/html/res/adm/includes/templates/*.problem>) {
                    248:     my $name=&Apache::lonnet::metadata($file,'title');
                    249:     if ($namewanted && ($name eq $namewanted)) {
                    250:       $result=$file;
                    251:       last;
                    252:     } else {
                    253:       $result.="<option>$name</option>";
                    254:     }
                    255:   }
                    256:   return $result;
                    257: }
                    258: 
                    259: sub newproblem {
                    260:   my ($request) = @_;
                    261: 
                    262:   if ($ENV{'form.template'}) {
                    263:     use File::Copy;
                    264:     my $file = &get_template_list($ENV{'form.template'});
                    265:     my $dest = &Apache::lonnet::filelocation("",$request->uri);
                    266:     copy($file,$dest);
                    267:     &renderpage($request,$file);
                    268:   } else {
                    269:     my $templatelist=&get_template_list();
                    270:     my $url=$request->uri;
                    271:     $request->print(<<ENDNEWPROBLEM);
                    272: <body bgcolor="#FFFFFF">
                    273: The request file $url doesn\'t exist. You can create a new problem <br />
                    274: <form action="$url" method="POST">
                    275: <input type="submit" value="New Problem"><br />
                    276: <select name="template">
                    277: $templatelist
                    278: </select>
                    279: </form>
                    280: </body>
                    281: ENDNEWPROBLEM
                    282:   }
                    283: 
                    284:   return '';
                    285: }
                    286: 
                    287: sub view_or_edit_menu {
                    288:   my ($request) = @_;
                    289:   my $url=$request->uri;
                    290:   $request->print(<<EDITMENU);
                    291: <body bgcolor="#FFFFFF">
                    292: <form action="$url" method="POST">
                    293: Would you like to <input type="submit" name="problemmode" value="View"> or
                    294: <input type="submit" name="problemmode" value="Edit"> the problem.
                    295: </form>
                    296: </body>
                    297: EDITMENU
                    298: }
                    299: 
1.41      albertel  300: sub handler {
                    301:   my $request=$_[0];
                    302: 
1.45      albertel  303:   #if ( $ENV{'user.name'} eq 'albertel' ) {$Apache::lonxml::debug=1;}
1.41      albertel  304: 
                    305:   if (&setupheader($request)) { return OK; }
1.42      albertel  306:   $ENV{'request.uri'}=$request->uri;
1.41      albertel  307: 
                    308:   #setup permissions
                    309:   $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
                    310:   $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
                    311:   &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:");
                    312: 
                    313:   my $file=&Apache::lonnet::filelocation("",$request->uri);
                    314: 
                    315:   #check if we know where we are
                    316:   if ($ENV{'request.course.fn'} && !&Apache::lonnet::symbread()) { 
                    317:     # if we are browsing we might not be able to know where we are
                    318:     if ($Apache::lonhomework::browse ne 'F') {
                    319:       #should know where we are, so ask
                    320:       $request->internal_redirect('/adm/ambiguous'); return;
                    321:     }
                    322:   }
                    323: 
                    324:   if ($ENV{'request.state'} eq "construct") {
                    325:     if ( -e $file ) {
1.42      albertel  326:       if (!(defined $ENV{'form.problemmode'})) {
1.41      albertel  327: 	#first visit to problem in construction space
                    328: 	&view_or_edit_menu($request);
1.42      albertel  329: 	#&renderpage($request,$file);
1.41      albertel  330:       } else {
                    331: 	&renderpage($request,$file);
                    332:       }
                    333:     } else {
                    334:       # requested file doesn't exist in contruction space
                    335:       &newproblem($request);
                    336:     }
                    337:   } else {
                    338:     # just render the page normally outside of construction space
                    339:     &renderpage($request,$file);
                    340:   }
                    341: 
1.46    ! albertel  342:   # &Apache::lonhomework::send_footer($request);
1.41      albertel  343:   # always turn off debug messages
1.40      albertel  344:   $Apache::lonxml::debug=0;
1.25      www       345:   return OK;
1.17      www       346: 
1.1       albertel  347: }
                    348: 
                    349: 1;
                    350: __END__

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