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

1.52    ! 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.47      albertel   17: #use Time::HiRes qw( gettimeofday tv_interval );
1.43      albertel   18: 
                     19: sub BEGIN {
                     20:   &Apache::lonxml::register_insert();
                     21: }
                     22: 
1.5       albertel   23: sub get_target {
1.52    ! albertel   24:   if ( $ENV{'request.state'} eq "published") {
        !            25:     if ( defined $ENV{'form.submitted'}) {
        !            26:       return ('grade', 'web');
        !            27:     } else {
        !            28:       return ('web');
        !            29:     }
        !            30:   } elsif ($ENV{'request.state'} eq "construct") {
        !            31:     if ( defined $ENV{'form.preview'}) {
        !            32:       if ( defined $ENV{'form.submitted'}) {
        !            33: 	return ('grade', 'web');
        !            34:       } else {
        !            35: 	return ('web');
        !            36:       }
        !            37:     } else {
        !            38:       if ( $ENV{'form.problemmode'} eq 'View' ) {
1.42      albertel   39: 	if ( defined $ENV{'form.submitted'}) {
1.52    ! albertel   40: 	  return ('grade', 'web');
1.42      albertel   41: 	} else {
1.52    ! albertel   42: 	  return ('web');
1.42      albertel   43: 	}
1.52    ! albertel   44:       } elsif ( $ENV{'form.problemmode'} eq 'Edit' ) {
        !            45: 	if ( $ENV{'form.submitted'} eq 'edit' ) {
        !            46: 	  return ('modified','edit');
1.42      albertel   47: 	} else {
1.52    ! albertel   48: 	  return ('edit');
1.42      albertel   49: 	}
1.52    ! albertel   50:       } else {
        !            51: 	return ('web');
        !            52:       }
1.15      albertel   53:     }
1.52    ! albertel   54:   }
        !            55:   return ();
1.5       albertel   56: }
                     57: 
1.3       albertel   58: sub setup_vars {
1.52    ! albertel   59:   my ($target) = @_;
        !            60:   return ';'
1.11      albertel   61: #  return ';$external::target='.$target.';';
1.2       albertel   62: }
                     63: 
                     64: sub send_header {
1.52    ! albertel   65:   my ($request)= @_;
        !            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 {
1.52    ! albertel   71:   my ($which,$request)=@_;
        !            72:   if ($which eq 'grade') {
        !            73:     $request->print('<script language="JavaScript"> 
        !            74:           hwkmenu=window.open("/res/adm/pages/homeworkmenu.html","homeworkremote",
        !            75:                  "height=350,width=150,menubar=no");
        !            76:           </script>');
        !            77:   }
1.36      albertel   78: }
                     79: 
1.2       albertel   80: sub send_footer {
1.52    ! albertel   81:   my ($request)= @_;
1.16      albertel   82: #  $request->print('</form>');
1.52    ! albertel   83:   $request->print(&Apache::lontexconvert::footer());
1.2       albertel   84: }
                     85: 
1.52    ! albertel   86: $Apache::lonxml::browse='';
1.20      albertel   87: sub check_date {
1.52    ! albertel   88:   my ($id) = @_;
        !            89:   my $date ='';
        !            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;
        !           103:     } elsif ($date eq 'con_lost') {
        !           104:       $date = "an indeterminate date"; $passed = 0;
        !           105:     } else {
        !           106:       if (time < $date) { $passed = 0; } else { $passed = 1; }
        !           107:       $date = localtime $date;
1.51      harris41  108:     }
1.52    ! albertel  109:     if (!$passed) { $type=$temp; last; }
        !           110:   }
        !           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';
        !           123:     $datemsg = "was due on $lastdate, and answers will be available on $date";
        !           124:   }
        !           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:   }
        !           133:   &Apache::lonxml::debug("sending back :$status:$datemsg:");
        !           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:   }
        !           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:   }
        !           144:   return ($status,$datemsg);
1.20      albertel  145: }
                    146: 
1.41      albertel  147: sub showhash {
1.52    ! albertel  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 '';
1.41      albertel  155: }
                    156: 
                    157: sub setuppermissions {
1.52    ! albertel  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 ''
1.41      albertel  161: }
                    162: 
                    163: sub setupheader {
1.52    ! albertel  164:   my $request=$_[0];
        !           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;
        !           171:   return OK if $request->header_only;
        !           172:   return ''
1.41      albertel  173: }
1.35      albertel  174: 
1.47      albertel  175: sub handle_save_or_undo {
1.52    ! albertel  176:   my ($request,$problem,$result) = @_;
        !           177:   my $fileout = &Apache::lonnet::filelocation("",$request->uri);
        !           178:   my $filesave=$fileout.".bak";
        !           179: 
        !           180:   if ($ENV{'form.Undo'} eq 'undo') {
        !           181:     if (copy($filesave,$fileout)) {
        !           182:       $request->print("<b>Undid changes, Copied $filesave to $fileout<br /></b>");
        !           183:     } else {
        !           184:       $request->print("<font color=\"red\" size=\"+1\"><b>Unable to undo, unable to copy $filesave to $fileout<br /></b></font>");
        !           185:     }
        !           186:   } else {
        !           187:     my $fs=Apache::File->new(">$filesave");
        !           188:     if (defined($fs)) {
        !           189:       print $fs $$problem;
        !           190:       $request->print("<b>Making Backup to $filesave</b><br />");
        !           191:     } else {
        !           192:       $request->print("<font color=\"red\" size=\"+1\"><b>Unable to make backup $filesave</b></font>");
        !           193:     }
        !           194:     my $fh=Apache::File->new(">$fileout");
        !           195:     if (defined($fh)) {
        !           196:       print $fh $$result;
        !           197:       $request->print("<b>Saving Modifications to $fileout</b><br />");
1.47      albertel  198:     } else {
1.52    ! albertel  199:       $request->print("<font color=\"red\" size=\"+1\"><b>Unable to write to $fileout</b></font>");
1.47      albertel  200:     }
1.52    ! albertel  201:   }
1.47      albertel  202: }
                    203: 
1.41      albertel  204: sub renderpage {
1.52    ! albertel  205:   my ($request,$file) = @_;
        !           206: 
        !           207:   my (@targets) = &get_target();
        !           208:   foreach my $target (@targets) {
        !           209:     #my $t0 = [&gettimeofday()];
        !           210:     my $problem=&Apache::lonnet::getfile($file);
        !           211:     if ($problem == -1) {
        !           212:       &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
        !           213:       $problem='';
        !           214:     }
        !           215: 
        !           216:     my %mystyle;
        !           217:     my $result = '';
        !           218:     &Apache::inputtags::initialize_inputtags;
        !           219:     &Apache::edit::initialize_edit;
        !           220:     %Apache::lonhomework::results=();
        !           221:     %Apache::lonhomework::history=&Apache::lonnet::restore();
        !           222:     #ignore error conditions
        !           223:     my ($temp)=keys %Apache::lonhomework::history ;
        !           224:     if ($temp =~ m/^error:.*/) { %Apache::lonhomework::history=(); } 
        !           225:     if ($target eq 'web') {
        !           226:       if (&Apache::lonnet::symbread() eq '') {
        !           227: 	if ($ENV{'request.state'} eq "construct") {
        !           228: 	  $request->print("In construction space, submissions ignored<br />");
1.36      albertel  229: 	} else {
1.52    ! albertel  230: 	  $request->print("Browsing or <a href=\"/adm/ambiguous\">ambiguous</a> reference, submissions ignored<br />");
1.51      harris41  231: 	}
1.52    ! albertel  232:       }
        !           233:       #if ($Apache::lonhomework::viewgrades eq 'F') {&createmenu('grade',$request); }
        !           234:     }
        !           235:     if ($target eq 'grade') { &showhash(%Apache::lonhomework::history); }
        !           236: 
        !           237:     my $default=&Apache::lonnet::getfile('/home/httpd/html/res/adm/includes/default_homework.lcpm');
        !           238:     if ($default == -1) {
        !           239:       &Apache::lonxml::error("<b>Unable to find <i>default_homework.lcpm</i></b>");
        !           240:       $default='';
        !           241:     }
        !           242:     $result = &Apache::lonxml::xmlparse($target, $problem,
        !           243: 			$default.&setup_vars($target),%mystyle);
        !           244: 
        !           245:     #$request->print("Result follows:");
        !           246:     if ($target eq 'modified') {
        !           247:       &handle_save_or_undo($request,\$problem,\$result);
        !           248:     } else {
        !           249:       #my $td=&tv_interval($t0);
        !           250:       #if ( $Apache::lonxml::debug) {
        !           251: 	#$result =~ s:</body>::;
        !           252: 	#$result.="<br />Spent $td seconds processing target $target\n</body>";
        !           253:       #}
        !           254:       $request->print($result);
        !           255:     }
        !           256:     #$request->print(":Result ends");
        !           257:     if ($target eq 'grade') { &showhash(%Apache::lonhomework::results); }
        !           258: 
        !           259:     #store new values
        !           260:     my ($temp) = keys %Apache::lonhomework::results;
        !           261:     if ( $temp ne '' ) {
        !           262:       &Apache::lonxml::debug("Store return message:".
        !           263: 			     &Apache::lonnet::cstore(\%Apache::lonhomework::results));
1.20      albertel  264:     }
1.52    ! albertel  265:     #my $td=&tv_interval($t0);
        !           266:   }
1.41      albertel  267: }
                    268: 
1.42      albertel  269: # with no arg it returns a HTML <option> list of the template titles
                    270: # with one arg it returns the filename associated with the arg passed
                    271: sub get_template_list {
1.52    ! albertel  272:   my ($namewanted,$extension) = @_;
        !           273:   my $result;
        !           274:   &Apache::lonxml::debug("Looking for :$extension:");
        !           275:   foreach my $file (</home/httpd/html/res/adm/includes/templates/*.$extension>) {
        !           276:     my $name=&Apache::lonnet::metadata($file,'title');
        !           277:     if ($namewanted && ($name eq $namewanted)) {
        !           278:       $result=$file;
        !           279:       last;
        !           280:     } else {
        !           281:       $result.="<option>$name</option>";
1.42      albertel  282:     }
1.52    ! albertel  283:   }
        !           284:   return $result;
1.42      albertel  285: }
                    286: 
                    287: sub newproblem {
1.52    ! albertel  288:   my ($request) = @_;
        !           289: 
        !           290:   my $extension=$request->uri;
        !           291:   $extension=~s:^.*\.([\w]+)$:$1:;
        !           292:   &Apache::lonxml::debug("Looking for :$extension:");
        !           293:   if ($ENV{'form.template'}) {
        !           294:     use File::Copy;
        !           295:     my $file = &get_template_list($ENV{'form.template'},$extension);
        !           296:     my $dest = &Apache::lonnet::filelocation("",$request->uri);
        !           297:     copy($file,$dest);
        !           298:     &renderpage($request,$file);
        !           299:   } else {
        !           300:     my $templatelist=&get_template_list('',$extension);
        !           301:     my $url=$request->uri;
        !           302:     my $dest = &Apache::lonnet::filelocation("",$request->uri);
        !           303:     $request->print(<<ENDNEWPROBLEM);
1.42      albertel  304: <body bgcolor="#FFFFFF">
1.50      albertel  305: The request file $url doesn\'t exist. You can create a new $extension <br />
1.42      albertel  306: <form action="$url" method="POST">
1.50      albertel  307: <input type="submit" value="New $extension"><br />
1.42      albertel  308: <select name="template">
                    309: $templatelist
                    310: </select>
                    311: </form>
                    312: </body>
                    313: ENDNEWPROBLEM
                    314:   }
                    315: 
                    316:   return '';
                    317: }
                    318: 
                    319: sub view_or_edit_menu {
1.52    ! albertel  320:   my ($request) = @_;
        !           321:   my $url=$request->uri;
        !           322:   $request->print(<<EDITMENU);
1.42      albertel  323: <body bgcolor="#FFFFFF">
                    324: <form action="$url" method="POST">
                    325: Would you like to <input type="submit" name="problemmode" value="View"> or
                    326: <input type="submit" name="problemmode" value="Edit"> the problem.
                    327: </form>
                    328: </body>
                    329: EDITMENU
                    330: }
                    331: 
1.41      albertel  332: sub handler {
1.52    ! albertel  333:   #my $t0 = [&gettimeofday()];
        !           334:   my $request=$_[0];
1.41      albertel  335: 
1.52    ! albertel  336:   if ( $ENV{'user.name'} eq 'albertel' ) {$Apache::lonxml::debug=1;}
1.41      albertel  337: 
                    338:   if (&setupheader($request)) { return OK; }
1.52    ! albertel  339:   $ENV{'request.uri'}=$request->uri;
1.41      albertel  340: 
                    341:   #setup permissions
1.52    ! albertel  342:   $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
        !           343:   $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
        !           344:   &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:");
        !           345: 
        !           346:   my $file=&Apache::lonnet::filelocation("",$request->uri);
        !           347: 
        !           348:   #check if we know where we are
        !           349:   if ($ENV{'request.course.fn'} && !&Apache::lonnet::symbread()) { 
        !           350:     # if we are browsing we might not be able to know where we are
        !           351:     if ($Apache::lonhomework::browse ne 'F') {
        !           352:       #should know where we are, so ask
        !           353:       $request->internal_redirect('/adm/ambiguous'); return;
1.41      albertel  354:     }
1.52    ! albertel  355:   }
1.41      albertel  356: 
1.52    ! albertel  357:   if ($ENV{'request.state'} eq "construct") {
        !           358:     if ( -e $file ) {
        !           359:       if (!(defined $ENV{'form.problemmode'})) {
        !           360: 	#first visit to problem in construction space
        !           361: 	&view_or_edit_menu($request);
        !           362: 	#&renderpage($request,$file);
        !           363:       } else {
        !           364: 	&renderpage($request,$file);
1.41      albertel  365:       }
                    366:     } else {
1.52    ! albertel  367:       # requested file doesn't exist in contruction space
        !           368:       &newproblem($request);
1.41      albertel  369:     }
1.52    ! albertel  370:   } else {
        !           371:     # just render the page normally outside of construction space
        !           372:     &renderpage($request,$file);
        !           373:   }
        !           374:   #my $td=&tv_interval($t0);
        !           375:   #&Apache::lonxml::debug("Spent $td seconds processing");
        !           376:   # &Apache::lonhomework::send_footer($request);
        !           377:   # always turn off debug messages
        !           378:   $Apache::lonxml::debug=0;
        !           379:   return OK;
        !           380: 
1.1       albertel  381: }
                    382: 
                    383: 1;
                    384: __END__

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