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

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;
                    189:     %Apache::lonhomework::results=();
1.40      albertel  190:     %Apache::lonhomework::history=&Apache::lonnet::restore();
1.18      albertel  191:     #ignore error conditions
                    192:     my ($temp)=keys %Apache::lonhomework::history ;
                    193:     if ($temp =~ m/^error:.*/) { %Apache::lonhomework::history=(); } 
1.36      albertel  194:     if ($target eq 'web') {
                    195:       if (&Apache::lonnet::symbread() eq '') {
                    196: 	if ($ENV{'request.state'} eq "construct") {
1.39      albertel  197: 	  $request->print("In construction space, submissions ignored<br />");
1.36      albertel  198: 	} else {
1.39      albertel  199: 	  $request->print("Browsing or <a href=\"/adm/ambiguous\">ambiguous</a> reference, submissions ignored<br />");
1.36      albertel  200: 	}
                    201:       }
                    202:       if ($Apache::lonhomework::viewgrades eq 'F') {&createmenu('grade',$request); }
1.20      albertel  203:     }
1.41      albertel  204:     if ($target eq 'grade') { &showhash(%Apache::lonhomework::history); }
                    205: 
1.18      albertel  206:     my $default=&Apache::lonnet::getfile('/home/httpd/html/res/adm/includes/default_homework.lcpm');
                    207:     if ($default == -1) {
                    208:       &Apache::lonxml::error("<b>Unable to find <i>default_homework.lcpm</i></b>");
                    209:       $default='';
                    210:     }
                    211:     $result = &Apache::lonxml::xmlparse($target, $problem,
                    212: 			$default.&setup_vars($target),%mystyle);
1.17      www       213: 
1.18      albertel  214:     #$request->print("Result follows:");
1.40      albertel  215:     if ($target eq 'modified') {
                    216:       my $fileout = &Apache::lonnet::filelocation("",$request->uri);
                    217:       my $filesave=$fileout.".bak";
                    218: 
                    219:       my $fs=Apache::File->new(">$filesave");
                    220:       print $fs $problem;
                    221:       $request->print("<b>Making Backup to $filesave</b><br />");
                    222: 
                    223:       my $fh=Apache::File->new(">$fileout");
                    224:       print $fh $result;
                    225:       $request->print("<b>Saving Modifications to $fileout</b><br />");
                    226:     } else {
                    227:       $request->print($result);
                    228:     }
1.18      albertel  229:     #$request->print(":Result ends");
1.41      albertel  230:     if ($target eq 'grade') { &showhash(%Apache::lonhomework::results); }
1.27      albertel  231: 
1.41      albertel  232:     #store new values
1.18      albertel  233:     my ($temp) = keys %Apache::lonhomework::results;
                    234:     if ( $temp ne '' ) {
1.41      albertel  235:       &Apache::lonxml::debug("Store return message:".
                    236: 			     &Apache::lonnet::cstore(\%Apache::lonhomework::results));
1.18      albertel  237:     }
1.17      www       238:   }
1.41      albertel  239: }
                    240: 
1.42      albertel  241: # with no arg it returns a HTML <option> list of the template titles
                    242: # with one arg it returns the filename associated with the arg passed
                    243: sub get_template_list {
                    244:   my ($namewanted) = @_;
                    245:   my $result;
                    246:   foreach my $file (</home/httpd/html/res/adm/includes/templates/*.problem>) {
                    247:     my $name=&Apache::lonnet::metadata($file,'title');
                    248:     if ($namewanted && ($name eq $namewanted)) {
                    249:       $result=$file;
                    250:       last;
                    251:     } else {
                    252:       $result.="<option>$name</option>";
                    253:     }
                    254:   }
                    255:   return $result;
                    256: }
                    257: 
                    258: sub newproblem {
                    259:   my ($request) = @_;
                    260: 
                    261:   if ($ENV{'form.template'}) {
                    262:     use File::Copy;
                    263:     my $file = &get_template_list($ENV{'form.template'});
                    264:     my $dest = &Apache::lonnet::filelocation("",$request->uri);
                    265:     copy($file,$dest);
                    266:     &renderpage($request,$file);
                    267:   } else {
                    268:     my $templatelist=&get_template_list();
                    269:     my $url=$request->uri;
                    270:     $request->print(<<ENDNEWPROBLEM);
                    271: <body bgcolor="#FFFFFF">
                    272: The request file $url doesn\'t exist. You can create a new problem <br />
                    273: <form action="$url" method="POST">
                    274: <input type="submit" value="New Problem"><br />
                    275: <select name="template">
                    276: $templatelist
                    277: </select>
                    278: </form>
                    279: </body>
                    280: ENDNEWPROBLEM
                    281:   }
                    282: 
                    283:   return '';
                    284: }
                    285: 
                    286: sub view_or_edit_menu {
                    287:   my ($request) = @_;
                    288:   my $url=$request->uri;
                    289:   $request->print(<<EDITMENU);
                    290: <body bgcolor="#FFFFFF">
                    291: <form action="$url" method="POST">
                    292: Would you like to <input type="submit" name="problemmode" value="View"> or
                    293: <input type="submit" name="problemmode" value="Edit"> the problem.
                    294: </form>
                    295: </body>
                    296: EDITMENU
                    297: }
                    298: 
1.41      albertel  299: sub handler {
                    300:   my $request=$_[0];
                    301: 
                    302:   if ( $ENV{'user.name'} eq 'albertel' ) {$Apache::lonxml::debug=1;}
                    303: 
                    304:   if (&setupheader($request)) { return OK; }
1.42      albertel  305:   $ENV{'request.uri'}=$request->uri;
1.41      albertel  306: 
                    307:   #setup permissions
                    308:   $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
                    309:   $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
                    310:   &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:");
                    311: 
                    312:   my $file=&Apache::lonnet::filelocation("",$request->uri);
                    313: 
                    314:   #check if we know where we are
                    315:   if ($ENV{'request.course.fn'} && !&Apache::lonnet::symbread()) { 
                    316:     # if we are browsing we might not be able to know where we are
                    317:     if ($Apache::lonhomework::browse ne 'F') {
                    318:       #should know where we are, so ask
                    319:       $request->internal_redirect('/adm/ambiguous'); return;
                    320:     }
                    321:   }
                    322: 
                    323:   if ($ENV{'request.state'} eq "construct") {
                    324:     if ( -e $file ) {
1.42      albertel  325:       if (!(defined $ENV{'form.problemmode'})) {
1.41      albertel  326: 	#first visit to problem in construction space
                    327: 	&view_or_edit_menu($request);
1.42      albertel  328: 	#&renderpage($request,$file);
1.41      albertel  329:       } else {
                    330: 	&renderpage($request,$file);
                    331:       }
                    332:     } else {
                    333:       # requested file doesn't exist in contruction space
                    334:       &newproblem($request);
                    335:     }
                    336:   } else {
                    337:     # just render the page normally outside of construction space
                    338:     &renderpage($request,$file);
                    339:   }
                    340: 
1.17      www       341:   &Apache::lonhomework::send_footer($request);
1.41      albertel  342:   # always turn off debug messages
1.40      albertel  343:   $Apache::lonxml::debug=0;
1.25      www       344:   return OK;
1.17      www       345: 
1.1       albertel  346: }
                    347: 
                    348: 1;
                    349: __END__

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