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

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

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