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

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

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