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

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

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