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

1.63      albertel    1: # The LearningOnline Network with CAPA
1.52      albertel    2: # The LON-CAPA Homework handler
1.63      albertel    3: #
1.108   ! albertel    4: # $Id: lonhomework.pm,v 1.107 2003/01/28 00:13:37 albertel Exp $
1.63      albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.52      albertel   28: # Guy Albertelli
1.17      www        29: # 11/30 Gerd Kortemeyer
1.56      www        30: # 6/1,8/17,8/18 Gerd Kortemeyer
1.82      bowersj2   31: # 7/18 Jeremy Bowers
1.1       albertel   32: 
                     33: package Apache::lonhomework;
                     34: use strict;
1.73      albertel   35: use Apache::style();
                     36: use Apache::lonxml();
                     37: use Apache::lonnet();
                     38: use Apache::lonplot();
                     39: use Apache::inputtags();
                     40: use Apache::structuretags();
                     41: use Apache::randomlabel();
                     42: use Apache::response();
                     43: use Apache::hint();
                     44: use Apache::outputtags();
1.83      albertel   45: use Apache::caparesponse();
                     46: use Apache::radiobuttonresponse();
                     47: use Apache::optionresponse();
                     48: use Apache::imageresponse();
                     49: use Apache::essayresponse();
                     50: use Apache::externalresponse();
1.106     albertel   51: use Apache::rankresponse();
1.107     albertel   52: use Apache::matchresponse();
1.26      www        53: use Apache::Constants qw(:common);
1.73      albertel   54: use HTML::Entities();
1.83      albertel   55: use Apache::loncommon();
1.47      albertel   56: #use Time::HiRes qw( gettimeofday tv_interval );
1.43      albertel   57: 
1.69      harris41   58: BEGIN {
1.43      albertel   59:   &Apache::lonxml::register_insert();
                     60: }
                     61: 
1.5       albertel   62: sub get_target {
1.52      albertel   63:   if ( $ENV{'request.state'} eq "published") {
1.90      sakharuk   64:     if ( defined($ENV{'form.grade_target'}  ) 
                     65: 	 && ($ENV{'form.grade_target'} eq 'tex')) {
                     66:       return ($ENV{'form.grade_target'});
                     67:     } elsif ( defined($ENV{'form.grade_target'}  ) 
1.66      albertel   68: 	 && ($Apache::lonhomework::viewgrades == 'F' )) {
                     69:       return ($ENV{'form.grade_target'});
                     70:     }
1.90      sakharuk   71:  
1.62      albertel   72:     if ( defined($ENV{'form.submitted'})) {
1.52      albertel   73:       return ('grade', 'web');
                     74:     } else {
                     75:       return ('web');
                     76:     }
                     77:   } elsif ($ENV{'request.state'} eq "construct") {
1.74      albertel   78:     if ( defined($ENV{'form.grade_target'}) ) {
                     79:       return ($ENV{'form.grade_target'});
                     80:     }
1.62      albertel   81:     if ( defined($ENV{'form.preview'})) {
                     82:       if ( defined($ENV{'form.submitted'})) {
1.52      albertel   83: 	return ('grade', 'web');
                     84:       } else {
                     85: 	return ('web');
                     86:       }
                     87:     } else {
1.88      albertel   88:       if ( $ENV{'form.problemmode'} eq 'View' ||
                     89: 	   $ENV{'form.problemmode'} eq 'Discard Edits and View') {
1.62      albertel   90: 	if ( defined($ENV{'form.submitted'}) &&
                     91: 	     (!defined($ENV{'form.resetdata'})) ) {
1.60      albertel   92: 	  return ('grade', 'web','answer');
1.42      albertel   93: 	} else {
1.60      albertel   94: 	  return ('web','answer');
1.42      albertel   95: 	}
1.52      albertel   96:       } elsif ( $ENV{'form.problemmode'} eq 'Edit' ) {
                     97: 	if ( $ENV{'form.submitted'} eq 'edit' ) {
1.80      albertel   98: 	  if ( $ENV{'form.submit'} eq 'Submit Changes and View' ) {
1.81      albertel   99: 	    return ('modified','web','answer');
1.80      albertel  100: 	  } else {
                    101: 	    return ('modified','edit');
                    102: 	  }
1.42      albertel  103: 	} else {
1.52      albertel  104: 	  return ('edit');
1.42      albertel  105: 	}
1.52      albertel  106:       } else {
                    107: 	return ('web');
                    108:       }
1.15      albertel  109:     }
1.52      albertel  110:   }
                    111:   return ();
1.5       albertel  112: }
                    113: 
1.3       albertel  114: sub setup_vars {
1.52      albertel  115:   my ($target) = @_;
                    116:   return ';'
1.11      albertel  117: #  return ';$external::target='.$target.';';
1.2       albertel  118: }
                    119: 
                    120: sub send_header {
1.52      albertel  121:   my ($request)= @_;
                    122:   $request->print(&Apache::lontexconvert::header());
1.16      albertel  123: #  $request->print('<form name='.$ENV{'form.request.prefix'}.'lonhomework method="POST" action="'.$request->uri.'">');
1.2       albertel  124: }
                    125: 
1.36      albertel  126: sub createmenu {
1.52      albertel  127:   my ($which,$request)=@_;
                    128:   if ($which eq 'grade') {
                    129:     $request->print('<script language="JavaScript"> 
1.91      albertel  130:           hwkmenu=window.open("/res/adm/pages/homeworkmenu.html","homeworkremote",
1.52      albertel  131:                  "height=350,width=150,menubar=no");
                    132:           </script>');
                    133:   }
1.36      albertel  134: }
                    135: 
1.2       albertel  136: sub send_footer {
1.52      albertel  137:   my ($request)= @_;
1.16      albertel  138: #  $request->print('</form>');
1.52      albertel  139:   $request->print(&Apache::lontexconvert::footer());
1.2       albertel  140: }
                    141: 
1.52      albertel  142: $Apache::lonxml::browse='';
1.53      www       143: 
1.92      bowersj2  144: # JB, 9/24/2002: Any changes in this function may require a change
                    145: # in lonnavmaps::resource::getDateStatus.
1.53      www       146: sub check_access {
1.52      albertel  147:   my ($id) = @_;
                    148:   my $date ='';
1.103     albertel  149:   my $status;
1.52      albertel  150:   my $datemsg = '';
                    151:   my $lastdate = '';
                    152:   my $temp;
                    153:   my $type;
                    154:   my $passed;
1.106     albertel  155: 
                    156:   if ($ENV{'request.state'} eq "construct") {
                    157:     &Apache::lonxml::debug("in construction ignoring dates");
                    158:     $status='CAN_ANSWER';
                    159:     $datemsg='is in under construction';
                    160:     return ($status,$datemsg);
                    161:   }
                    162: 
1.52      albertel  163:   &Apache::lonxml::debug("checking for part :$id:");
1.73      albertel  164:   &Apache::lonxml::debug("time:".time);
1.52      albertel  165:   foreach $temp ("opendate","duedate","answerdate") {
                    166:     $lastdate = $date;
                    167:     $date = &Apache::lonnet::EXT("resource.$id.$temp");
1.87      www       168:     my $thistype = &Apache::lonnet::EXT("resource.$id.$temp.type");
1.103     albertel  169:     if ($thistype =~ /^(con_lost|no_such_host)/ ||
                    170: 	$date     =~ /^(con_lost|no_such_host)/) {
                    171: 	$status='UNAVAILABLE';
                    172: 	$date="may open later.";
                    173: 	return($status,$date);
                    174:     }
1.87      www       175:     if ($thistype eq 'date_interval') {
                    176: 	if ($temp eq 'opendate') {
                    177:            $date=&Apache::lonnet::EXT("resource.$id.duedate")-$date;
                    178:         }
                    179:         if ($temp eq 'answerdate') {
                    180:            $date=&Apache::lonnet::EXT("resource.$id.duedate")+$date;
                    181:         }
                    182:     }
1.52      albertel  183:     &Apache::lonxml::debug("found :$date: for :$temp:");
                    184:     if ($date eq '') {
                    185:       $date = "an unknown date"; $passed = 0;
                    186:     } elsif ($date eq 'con_lost') {
                    187:       $date = "an indeterminate date"; $passed = 0;
                    188:     } else {
                    189:       if (time < $date) { $passed = 0; } else { $passed = 1; }
                    190:       $date = localtime $date;
1.51      harris41  191:     }
1.52      albertel  192:     if (!$passed) { $type=$temp; last; }
                    193:   }
                    194:   &Apache::lonxml::debug("have :$type:$passed:");
                    195:   if ($passed) {
                    196:     $status='SHOW_ANSWER';
                    197:     $datemsg=$date;
                    198:   } elsif ($type eq 'opendate') {
                    199:     $status='CLOSED';
                    200:     $datemsg = "will open on $date";
                    201:   } elsif ($type eq 'duedate') {
                    202:     $status='CAN_ANSWER';
                    203:     $datemsg = "is due at $date";
                    204:   } elsif ($type eq 'answerdate') {
                    205:     $status='CLOSED';
                    206:     $datemsg = "was due on $lastdate, and answers will be available on $date";
                    207:   }
                    208:   if ($status eq 'CAN_ANSWER') {
                    209:     #check #tries
                    210:     my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
                    211:     my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
                    212:     if ( $tries eq '' ) { $tries = '0'; }
                    213:     if ( $maxtries eq '' ) { $maxtries = '2'; } 
                    214:     if ($tries >= $maxtries) { $status = 'CANNOT_ANSWER'; } 
                    215:   }
1.54      www       216: 
1.56      www       217:   if (($status ne 'CLOSED') && ($Apache::lonhomework::type eq 'exam') &&
                    218:       (!$Apache::lonhomework::history{"resource.0.outtoken"})) {
1.54      www       219:       return ('UNCHECKEDOUT','needs to be checked out');
                    220:   }
                    221: 
                    222: 
1.52      albertel  223:   &Apache::lonxml::debug("sending back :$status:$datemsg:");
                    224:   if (($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED')) {
1.91      albertel  225:     &Apache::lonxml::debug("should be allowed to browse a resource when closed");
1.52      albertel  226:     $status='CAN_ANSWER';
                    227:     $datemsg='is closed but you are allowed to view it';
                    228:   }
1.106     albertel  229: 
1.52      albertel  230:   return ($status,$datemsg);
1.20      albertel  231: }
                    232: 
1.41      albertel  233: sub showhash {
1.52      albertel  234:   my (%hash) = @_;
1.99      albertel  235:   &showhashsubset(\%hash,'.');
1.79      albertel  236:   return '';
                    237: }
                    238: 
1.106     albertel  239: sub showarray {
                    240:     my ($array)=@_;
                    241:     my $string="(";
                    242:     foreach my $elm (@{ $array }) {
                    243: 	if (ref($elm)) {
                    244: 	    if ($elm =~ /ARRAY/ ) {
                    245: 		$string.=&showarray($elm);
                    246: 	    }
                    247: 	} else {
                    248: 	    $string.="$elm,"
                    249: 	}
                    250:     }
                    251:     chop($string);
                    252:     $string.=")";
                    253:     return $string;
                    254: }
                    255: 
1.79      albertel  256: sub showhashsubset {
                    257:   my ($hash,$keyre) = @_;
1.52      albertel  258:   my $resultkey;
1.79      albertel  259:   foreach $resultkey (sort keys %$hash) {
                    260:     if ($resultkey =~ /$keyre/) {
                    261:       if (ref($$hash{$resultkey})) {
                    262: 	if ($$hash{$resultkey} =~ /ARRAY/ ) {
1.106     albertel  263: 	    &Apache::lonxml::debug("$resultkey ---- ".
                    264: 				   &showarray($$hash{$resultkey}));
                    265: 	} elsif ($$hash{$resultkey} =~ /HASH/ ) {
                    266: 	    &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
                    267: 	    &showhashsubset($$hash{$resultkey},'.');
1.79      albertel  268: 	} else {
1.106     albertel  269: 	    &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
1.73      albertel  270: 	}
                    271:       } else {
1.79      albertel  272: 	&Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
1.73      albertel  273:       }
                    274:     }
1.52      albertel  275:   }
                    276:   &Apache::lonxml::debug("\n<br />restored values^</br>\n");
                    277:   return '';
1.41      albertel  278: }
                    279: 
                    280: sub setuppermissions {
1.52      albertel  281:   $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
                    282:   $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
                    283:   return ''
1.41      albertel  284: }
                    285: 
                    286: sub setupheader {
1.52      albertel  287:   my $request=$_[0];
                    288:   if ($ENV{'browser.mathml'}) {
                    289:     $request->content_type('text/xml');
                    290:   } else {
                    291:     $request->content_type('text/html');
                    292:   }
1.68      albertel  293:   if (!$Apache::lonxml::debug && ($ENV{'REQUEST_METHOD'} eq 'GET')) {
                    294:     &Apache::loncommon::no_cache($request);
                    295:   }
1.52      albertel  296:   $request->send_http_header;
                    297:   return OK if $request->header_only;
                    298:   return ''
1.41      albertel  299: }
1.35      albertel  300: 
1.47      albertel  301: sub handle_save_or_undo {
1.52      albertel  302:   my ($request,$problem,$result) = @_;
1.70      albertel  303:   my $file    = &Apache::lonnet::filelocation("",$request->uri);
                    304:   my $filebak =$file.".bak";
                    305:   my $filetmp =$file.".tmp";
1.64      albertel  306:   my $error=0;
1.52      albertel  307: 
                    308:   if ($ENV{'form.Undo'} eq 'undo') {
1.70      albertel  309:     my $error=0;
                    310:     if (!copy($file,$filetmp)) { $error=1; }
                    311:     if ((!$error) && (!copy($filebak,$file))) { $error=1; }
                    312:     if ((!$error) && (!move($filetmp,$filebak))) { $error=1; }
                    313:     if (!$error) {
                    314:       $request->print("<p><b>Undid changes, Switched $filebak and $file</b></p>");
1.52      albertel  315:     } else {
1.70      albertel  316:       $request->print("<p><font color=\"red\" size=\"+1\"><b>Unable to undo, unable to switch $filebak and $file</b></font></p>");
1.64      albertel  317:       $error=1;
1.52      albertel  318:     }
                    319:   } else {
1.70      albertel  320:     my $fs=Apache::File->new(">$filebak");
1.52      albertel  321:     if (defined($fs)) {
                    322:       print $fs $$problem;
1.70      albertel  323:       $request->print("<b>Making Backup to $filebak</b><br />");
1.52      albertel  324:     } else {
1.70      albertel  325:       $request->print("<font color=\"red\" size=\"+1\"><b>Unable to make backup $filebak</b></font>");
1.64      albertel  326:       $error=2;
1.52      albertel  327:     }
1.70      albertel  328:     my $fh=Apache::File->new(">$file");
1.52      albertel  329:     if (defined($fh)) {
                    330:       print $fh $$result;
1.70      albertel  331:       $request->print("<b>Saving Modifications to $file</b><br />");
1.47      albertel  332:     } else {
1.70      albertel  333:       $request->print("<font color=\"red\" size=\"+1\"><b>Unable to write to $file</b></font>");
1.64      albertel  334:       $error|=4;
1.47      albertel  335:     }
1.52      albertel  336:   }
1.64      albertel  337:   return $error;
                    338: }
                    339: 
1.101     albertel  340: sub analyze_header {
                    341:     my ($request) = @_;
                    342:     my $result.='<html><body bgcolor="#FFFFFF">
                    343:             <form name="lonhomework" method="POST" action="'.
                    344: 	      $ENV{'request.uri'}.'">
                    345:             <input type="submit" name="problemmode" value="EditXML" />
                    346:             <input type="submit" name="problemmode" value="Edit" />
                    347:             <hr />
                    348:             <input type="submit" name="submit" value="View" />
                    349:             <hr />
                    350:             List of possible answers:
                    351:             </form>';
                    352:     $request->print($result);
                    353:     $request->rflush();
                    354: }
                    355: 
1.74      albertel  356: sub analyze {
1.101     albertel  357:     my ($request,$file) = @_;
                    358:     &Apache::lonxml::debug("Analyze");
                    359:     my $result;
                    360:     my %overall;
                    361:     my %allparts;
                    362:     my $rndseed=$ENV{'form.rndseed'};
                    363:     &analyze_header($request);
1.102     albertel  364:     for(my $i=1;$i<$ENV{'form.numtoanalyze'}+1;$i++) {
1.101     albertel  365: 	my $subresult=&Apache::lonnet::ssi($request->uri,
                    366: 					   ('grade_target' => 'analyze'),
1.102     albertel  367: 					   ('rndseed' => $i));
                    368: 	&Apache::lonxml::debug(":$subresult:");
1.101     albertel  369: 	(my $garbage,$subresult)=split(/_HASH_REF__/,$subresult,2);
                    370: 	my %analyze=&Apache::lonnet::str2hash($subresult);
                    371: 	&Apache::lonhomework::showhash(%analyze);
                    372: 	my @parts = @{ $analyze{'parts'} };
                    373: 	foreach my $part (@parts) {
                    374: 	    if (!exists($allparts{$part})) {$allparts{$part}=1;};
                    375: 	    if ($analyze{$part.'.type'} eq 'numericalresponse') {
                    376: 		push( @{ $overall{$part.'.answer'} },
                    377: 		      [@{ $analyze{$part.'.answer'} }]);
                    378: 	    }
                    379: 	}
                    380:     }
                    381:     foreach my $part (keys(%allparts)) {
                    382: 	$request->print('<table><tr><td>Part '.$part.'</td></tr>');
                    383: 	foreach my $answer (sort {$a->[0] <=> $b->[0]} (@{ $overall{$part.'.answer'} })) {
                    384: 	    $request->print('<tr><td>'.join('</td><td>',@{ $answer }).
                    385: 			    '</td></tr>');
                    386: 	}
                    387:     }
                    388:     &Apache::lonhomework::showhash(%overall);
                    389:     return $result;
1.74      albertel  390: }
                    391: 
1.64      albertel  392: sub editxmlmode {
                    393:   my ($request,$file) = @_;
                    394:   my $result;
                    395:   my $problem=&Apache::lonnet::getfile($file);
                    396:   if ($problem == -1) {
                    397:     &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
                    398:     $problem='';
                    399:   }
                    400:   if (defined($ENV{'form.editxmltext'}) || defined($ENV{'form.Undo'})) {
                    401:     my $error=&handle_save_or_undo($request,\$problem,
                    402: 				   \$ENV{'form.editxmltext'});
                    403:     if (!$error) { $problem=&Apache::lonnet::getfile($file); }
                    404:   }
1.80      albertel  405:   &Apache::lonhomework::showhashsubset(\%ENV,'^form');
                    406:   if ( $ENV{'form.submit'} eq 'Submit Changes and View' ) {
                    407:     &Apache::lonhomework::showhashsubset(\%ENV,'^form');
                    408:     $ENV{'form.problemmode'}='View';
                    409:     &renderpage($request,$file);
                    410:   } else {
                    411:     my ($rows,$cols) = &Apache::edit::textarea_sizes(\$problem);
1.82      bowersj2  412:     my $xml_help = Apache::loncommon::help_open_topic("Problem_Editor_XML_Index");
1.80      albertel  413:     if ($cols > 80) { $cols = 80; }
1.89      albertel  414:     if ($cols < 70) { $cols = 70; }
                    415:     if ($rows < 20) { $rows = 20; }
1.80      albertel  416:     $result.='<html><body bgcolor="#FFFFFF">
1.64      albertel  417:             <form name="lonhomework" method="POST" action="'.
                    418: 	      $ENV{'request.uri'}.'">
                    419:             <input type="hidden" name="problemmode" value="EditXML" />
1.80      albertel  420:             <input type="submit" name="problemmode" value="Discard Edits and View" />
1.64      albertel  421:             <input type="submit" name="problemmode" value="Edit" />
                    422:             <hr />
                    423:             <input type="submit" name="submit" value="Submit Changes" />
1.80      albertel  424:             <input type="submit" name="submit" value="Submit Changes and View" />
1.64      albertel  425:             <input type="submit" name="Undo" value="undo" />
                    426:             <hr />
1.82      bowersj2  427:             ' . $xml_help . ' Problem Help<br>
1.64      albertel  428:             <textarea rows="'.$rows.'" cols="'.$cols.'" name="editxmltext">'.
1.73      albertel  429: 	      &HTML::Entities::encode($problem).'</textarea>
1.64      albertel  430:             </form></body></html>';
1.80      albertel  431:     $request->print($result);
                    432:   }
1.64      albertel  433:   return '';
1.47      albertel  434: }
                    435: 
1.41      albertel  436: sub renderpage {
1.52      albertel  437:   my ($request,$file) = @_;
                    438: 
                    439:   my (@targets) = &get_target();
1.70      albertel  440:   &Apache::lonxml::debug("Running targets ".join(':',@targets));
1.52      albertel  441:   foreach my $target (@targets) {
                    442:     #my $t0 = [&gettimeofday()];
                    443:     my $problem=&Apache::lonnet::getfile($file);
                    444:     if ($problem == -1) {
                    445:       &Apache::lonxml::error("<b> Unable to find <i>$file</i></b>");
                    446:       $problem='';
                    447:     }
                    448: 
                    449:     my %mystyle;
                    450:     my $result = '';
1.74      albertel  451:     if ($target eq 'analyze') { %Apache::lonhomework::anaylze=(); }
1.85      albertel  452:     if ($target eq 'answer') { &showhash(%Apache::lonhomework::history); }
                    453:     if ($target eq 'web') {&Apache::lonhomework::showhashsubset(\%ENV,'^form');}
1.52      albertel  454: 
1.70      albertel  455:     &Apache::lonxml::debug("Should be parsing now");
1.78      albertel  456:     $result = &Apache::lonxml::xmlparse($request, $target, $problem,
1.98      albertel  457: 			&setup_vars($target),%mystyle);
1.52      albertel  458: 
                    459:     #$request->print("Result follows:");
                    460:     if ($target eq 'modified') {
                    461:       &handle_save_or_undo($request,\$problem,\$result);
                    462:     } else {
1.74      albertel  463:       if ($target eq 'analyze') {
                    464: 	$result=&Apache::lonnet::hashref2str(\%Apache::lonhomework::analyze);
1.75      albertel  465: 	undef(%Apache::lonhomework::analyze);
1.74      albertel  466:       }
1.52      albertel  467:       #my $td=&tv_interval($t0);
                    468:       #if ( $Apache::lonxml::debug) {
                    469: 	#$result =~ s:</body>::;
                    470: 	#$result.="<br />Spent $td seconds processing target $target\n</body>";
                    471:       #}
                    472:       $request->print($result);
                    473:     }
                    474:     #$request->print(":Result ends");
                    475:     #my $td=&tv_interval($t0);
                    476:   }
1.41      albertel  477: }
                    478: 
1.42      albertel  479: # with no arg it returns a HTML <option> list of the template titles
                    480: # with one arg it returns the filename associated with the arg passed
                    481: sub get_template_list {
1.52      albertel  482:   my ($namewanted,$extension) = @_;
                    483:   my $result;
1.85      albertel  484:   my @allnames;
1.52      albertel  485:   &Apache::lonxml::debug("Looking for :$extension:");
1.91      albertel  486:   foreach my $file (</home/httpd/html/res/adm/includes/templates/*.$extension>) {
1.52      albertel  487:     my $name=&Apache::lonnet::metadata($file,'title');
                    488:     if ($namewanted && ($name eq $namewanted)) {
                    489:       $result=$file;
                    490:       last;
                    491:     } else {
1.104     www       492: 	if ($name) { push (@allnames, $name); }
1.42      albertel  493:     }
1.52      albertel  494:   }
1.86      albertel  495:   if (@allnames && !$result) {
1.85      albertel  496:     $result="<option>Select a $extension type</option>\n<option>".
                    497:       join('</option><option>',sort(@allnames)).'</option>';
                    498:   }
1.52      albertel  499:   return $result;
1.42      albertel  500: }
                    501: 
                    502: sub newproblem {
1.65      matthew   503:     my ($request) = @_;
                    504:     my $extension=$request->uri;
                    505:     $extension=~s:^.*\.([\w]+)$:$1:;
                    506:     &Apache::lonxml::debug("Looking for :$extension:");
1.85      albertel  507:     if ($ENV{'form.template'} &&
                    508: 	$ENV{'form.template'} ne "Select a $extension type") {
1.65      matthew   509: 	use File::Copy;
                    510: 	my $file = &get_template_list($ENV{'form.template'},$extension);
                    511: 	my $dest = &Apache::lonnet::filelocation("",$request->uri);
                    512: 	copy($file,$dest);
                    513: 	&renderpage($request,$dest);
                    514:     } elsif($ENV{'form.newfile'}) {
                    515: 	# I don't like hard-coded filenames but for now, this will work.
                    516: 	use File::Copy;
                    517: 	my $templatefilename = 
1.66      albertel  518: 	    $request->dir_config('lonIncludes').'/templates/blank.problem';
                    519: 	&Apache::lonxml::debug("$templatefilename");
1.65      matthew   520: 	my $dest = &Apache::lonnet::filelocation("",$request->uri);
                    521: 	copy($templatefilename,$dest);
                    522: 	&renderpage($request,$dest);
1.85      albertel  523:     } else {
1.65      matthew   524: 	my $templatelist=&get_template_list('',$extension);
                    525: 	my $url=$request->uri;
                    526: 	my $dest = &Apache::lonnet::filelocation("",$request->uri);
1.85      albertel  527: 	my $instructions;
1.105     www       528: 	if ($templatelist) { $instructions=", select a template from the pull-down menu below.<br />Then";}
1.65      matthew   529: 	$request->print(<<ENDNEWPROBLEM);
1.42      albertel  530: <body bgcolor="#FFFFFF">
1.105     www       531: <h1>Creating a new $extension resource</h1>
                    532: The requested file <tt>$url</tt> currently does not exist.
                    533: <p>
                    534: To create a new $extension$instructions click on the "Create $extension" button.
                    535: </p>
                    536: <p><form action="$url" method="POST">
1.42      albertel  537: ENDNEWPROBLEM
1.85      albertel  538: 	if (defined($templatelist)) {
                    539: 	    $request->print("<select name=\"template\">$templatelist</select>");
                    540: 	}
                    541: 	$request->print("<br /><input type=\"submit\" name=\"newfile\" value=\"Create $extension\" />");
1.105     www       542: 	$request->print("</form></p></body>");
1.65      matthew   543:     }
                    544:     return '';
1.42      albertel  545: }
                    546: 
                    547: sub view_or_edit_menu {
1.52      albertel  548:   my ($request) = @_;
                    549:   my $url=$request->uri;
                    550:   $request->print(<<EDITMENU);
1.42      albertel  551: <body bgcolor="#FFFFFF">
                    552: <form action="$url" method="POST">
                    553: Would you like to <input type="submit" name="problemmode" value="View"> or
                    554: <input type="submit" name="problemmode" value="Edit"> the problem.
                    555: </form>
                    556: </body>
                    557: EDITMENU
                    558: }
                    559: 
1.41      albertel  560: sub handler {
1.52      albertel  561:   #my $t0 = [&gettimeofday()];
                    562:   my $request=$_[0];
1.41      albertel  563: 
1.95      albertel  564: #  if ( $ENV{'user.name'} eq 'albertel' ) {$Apache::lonxml::debug=1;}
                    565:   $Apache::lonxml::debug=$ENV{'user.debug'};
1.41      albertel  566: 
                    567:   if (&setupheader($request)) { return OK; }
1.52      albertel  568:   $ENV{'request.uri'}=$request->uri;
1.41      albertel  569: 
                    570:   #setup permissions
1.52      albertel  571:   $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$ENV{'request.filename'});
                    572:   $Apache::lonhomework::viewgrades=&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'});
                    573:   &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:");
1.71      albertel  574:   # some times multiple problemmodes are submitted, need to select
                    575:   # the last one
1.80      albertel  576:   &Apache::lonxml::debug("Problem Mode ".$ENV{'form.problemmode'});
1.72      albertel  577:   if ( defined($ENV{'form.problemmode'}) &&
1.77      albertel  578:        ref($ENV{'form.problemmode'}) ) {
1.80      albertel  579:     &Apache::lonxml::debug("Problem Mode ".join(",",@$ENV{'form.problemmode'}));
1.72      albertel  580:     my $mode=$ENV{'form.problemmode'}->[-1];
1.71      albertel  581:     undef $ENV{'form.problemmode'};
1.72      albertel  582:     $ENV{'form.problemmode'}=$mode;
1.71      albertel  583:   }
1.64      albertel  584:   &Apache::lonxml::debug("Problem Mode ".$ENV{'form.problemmode'});
1.52      albertel  585:   my $file=&Apache::lonnet::filelocation("",$request->uri);
                    586: 
                    587:   #check if we know where we are
                    588:   if ($ENV{'request.course.fn'} && !&Apache::lonnet::symbread()) { 
                    589:     # if we are browsing we might not be able to know where we are
                    590:     if ($Apache::lonhomework::browse ne 'F') {
                    591:       #should know where we are, so ask
                    592:       $request->internal_redirect('/adm/ambiguous'); return;
1.41      albertel  593:     }
1.52      albertel  594:   }
1.41      albertel  595: 
1.52      albertel  596:   if ($ENV{'request.state'} eq "construct") {
1.62      albertel  597:     if ($ENV{'form.resetdata'} eq 'Reset Submissions') {
                    598:       my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
                    599:       &Apache::lonnet::tmpreset($symb,'',$domain,$name);
                    600:     }
1.52      albertel  601:     if ( -e $file ) {
                    602:       if (!(defined $ENV{'form.problemmode'})) {
                    603: 	#first visit to problem in construction space
1.64      albertel  604: 	#&view_or_edit_menu($request);
                    605: 	$ENV{'form.problemmode'}='View';
                    606: 	&renderpage($request,$file);
                    607:       } elsif ($ENV{'form.problemmode'} eq 'EditXML') {
                    608: 	&editxmlmode($request,$file);
1.74      albertel  609:       } elsif ($ENV{'form.problemmode'} eq 'Answer Distribution') {
                    610: 	&analyze($request,$file);
1.52      albertel  611:       } else {
                    612: 	&renderpage($request,$file);
1.41      albertel  613:       }
                    614:     } else {
1.52      albertel  615:       # requested file doesn't exist in contruction space
                    616:       &newproblem($request);
1.41      albertel  617:     }
1.52      albertel  618:   } else {
                    619:     # just render the page normally outside of construction space
1.74      albertel  620:     &Apache::lonxml::debug("not construct");
1.52      albertel  621:     &renderpage($request,$file);
                    622:   }
                    623:   #my $td=&tv_interval($t0);
                    624:   #&Apache::lonxml::debug("Spent $td seconds processing");
                    625:   # &Apache::lonhomework::send_footer($request);
                    626:   # always turn off debug messages
                    627:   $Apache::lonxml::debug=0;
                    628:   return OK;
                    629: 
1.1       albertel  630: }
                    631: 
                    632: 1;
                    633: __END__

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