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

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

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