Annotation of loncom/interface/printout.pl, revision 1.103

1.1       sakharuk    1: #!/usr/bin/perl
1.37      albertel    2: # CGI-script to run LaTeX, dvips, ps2ps, ps2pdf etc.
                      3: #
                      4: #
                      5: # Copyright Michigan State University Board of Trustees
                      6: #
                      7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      8: #
                      9: # LON-CAPA is free software; you can redistribute it and/or modify
                     10: # it under the terms of the GNU General Public License as published by
                     11: # the Free Software Foundation; either version 2 of the License, or
                     12: # (at your option) any later version.
                     13: #
                     14: # LON-CAPA is distributed in the hope that it will be useful,
                     15: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     16: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     17: # GNU General Public License for more details.
                     18: #
                     19: # You should have received a copy of the GNU General Public License
                     20: # along with LON-CAPA; if not, write to the Free Software
                     21: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     22: #
                     23: # /home/httpd/html/adm/gpl.txt
                     24: #
                     25: # http://www.lon-capa.org/
                     26: #
                     27: 
1.39      sakharuk   28: use lib '/home/httpd/lib/perl';
1.76      albertel   29: use LONCAPA::loncgi;
1.38      sakharuk   30: use File::Path;
1.88      foxr       31: use File::Basename;
1.6       sakharuk   32: use IO::File;
1.7       sakharuk   33: use Image::Magick;
1.97      albertel   34: use Apache::lonhtmlcommon();
1.80      albertel   35: use Apache::lonnet;
1.97      albertel   36: use Apache::loncommon();
1.51      albertel   37: use Apache::lonlocal;
1.97      albertel   38: use Apache::lonmsg();
1.77      foxr       39: use LONCAPA::Enrollment;
1.47      sakharuk   40: 
1.59      albertel   41: use strict;
1.77      foxr       42: 
1.81      foxr       43: 
1.77      foxr       44: #   Determine if a user is operating as a student for this course/domain.
                     45: #Parameters:
1.92      albertel   46: #    none
1.77      foxr       47: #Implicit:
                     48: #    $env{request.role} contains the role under which this user operated this
                     49: #                       this request.
                     50: sub is_student {
1.92      albertel   51:     return ($env{'request.role'}=~/^st\./);
1.77      foxr       52: }
                     53: 
                     54: #
                     55: #   Debugging:  Dump the environment for debugging.
                     56: #
                     57: sub dumpenv  {
1.84      albertel   58:     print "<br />-------------------<br />";
1.77      foxr       59:     foreach my $key (sort (keys %env)) {
1.84      albertel   60: 	print "<br />$key -> $env{$key}";
1.77      foxr       61:     }
1.84      albertel   62:     print "<br />-------------------<br />";
1.77      foxr       63: }
                     64: 
                     65: #
                     66: #   This sub sends a message to the appropriate person if there was an error
                     67: #   rendering the latex  At present, there's only one case to consider:
                     68: #   a student printing inside a course results in messages to the course coordinator.
                     69: #Parmaeters:
                     70: #    identifier -  The unique identifier of this cgi request.
1.81      foxr       71: #    badresource-  Filepath to most likely failing 
1.77      foxr       72: #    logfile    -  The contents of the log file (included in the message).
                     73: #    texfile    -  reference to an array containing the LaTeX input file
                     74: #                  (included in the message).
                     75: #Implicit inputs:
                     76: #   From the environment:
                     77: #       cgi.$identifier.user     - User doing the printing.
                     78: #       cgi.$identifier.domain   - Domain the user is logged in on with printing.
                     79: #       cgi.$identifier.courseid - Id of the course (if this is a course).
                     80: #       cgi.$identifier.coursedom- Domain in which course was constituted.
                     81: #       cgi.$identifier.resources - List of resource URL's for which the print
                     82: #                                  was attempted.
                     83: # 
                     84: sub send_error_mail {
1.81      foxr       85:     my ($identifier, $badresource, $logfile, $texfile) = @_;
1.77      foxr       86:     my $user     = $env{"cgi.$identifier.user"};
                     87:     my $domain   = $env{"cgi.$identifier.domain"};
                     88:     my $courseid = $env{"cgi.$identifier.courseid"};
                     89:     my $coursedom= $env{"cgi.$identifier.coursedom"};
                     90:     my $resources= $env{"cgi.$identifier.resources"};
                     91: 
1.81      foxr       92:     #  resource file->URL
                     93:     #
                     94:     my $badurl = &Apache::lonnet::declutter($badresource);
                     95: 
                     96:     # &dumpenv();
1.77      foxr       97: 
                     98: 
                     99: 
                    100:     #  Only continue if there is a user, domain, courseid, course domain
                    101:     #  and resources:
                    102: 
                    103:     if(defined($user)       && defined($domain) && defined($courseid) &&
                    104:        defined($coursedom)  && defined($resources) ){
                    105: 	   
                    106: 	#  Only mail if the conditions are ripe for it:
                    107: 	#  The user is a student in the course:
                    108: 	#
                    109: 	
1.92      albertel  110: 	if (&is_student()) {
1.77      foxr      111: 	    # build the subject and message body:
1.84      albertel  112: 	    # print "sending message to course coordinators.<br />";
1.81      foxr      113: 
                    114: 	    # Todo: Convert badurl into a url from file path:
                    115: 
                    116: 	    my $subject  = "Error [$badurl] Print failed for $user".'@'.$domain;
1.77      foxr      117: 	    my $message .= "Print failed to render LaTeX for $user".'@'."$domain\n";
                    118: 	    $message    .= "  User was attempting to print: \n";
1.91      albertel  119: 	    foreach my $resource (split(/:/,$resources)) {
                    120: 		$message    .= "       $resource\n";
                    121: 	    }
1.77      foxr      122: 	    $message    .= "--------------------LaTeX logfile:------------ \n";
                    123: 	    $message    .= $logfile;
                    124: 	    $message    .= "-----------------LaTeX source file: ------------\n";
                    125: 	    
                    126: 	    foreach my $line (@$texfile) {
                    127: 		$message .= "$line\n";
                    128: 	    }
1.81      foxr      129: 	    my (undef, %receivers) = &Apache::lonfeedback::decide_receiver(undef, 0,
                    130: 									  1,1,1);
1.84      albertel  131: 	    # print "<br /> sending...section:  $env{'request.course.sec'}";
1.81      foxr      132: 	    foreach my $dest (keys %receivers) {
1.84      albertel  133: 		# print "<br /> dest is $dest";
1.77      foxr      134: 		my @destinfo = split(/:/,$dest);
                    135: 		my $user = $destinfo[0];
                    136: 		my $dom  = $destinfo[1];
                    137: 
                    138: 		&Apache::lonmsg::user_normal_msg($user, $dom,
                    139: 						 $subject, $message);
                    140: 		
                    141: 		# No point in looking at the return status as there's no good
                    142: 		# error action I can think of right now (log maybe?).
                    143: 	    }
                    144: 	}
                    145:     }
                    146: }
                    147: 
1.49      albertel  148: $|=1;
1.39      sakharuk  149: if (! &LONCAPA::loncgi::check_cookie_and_load_env()) {
                    150:     print <<END;
                    151: Content-type: text/html
                    152: 
                    153: <html>
                    154: <head><title>Bad Cookie</title></head>
                    155: <body>
1.40      sakharuk  156: Your cookie information is incorrect.
1.39      sakharuk  157: </body>
                    158: </html>
                    159: END
                    160:     return;
                    161: }
1.51      albertel  162:  &Apache::lonlocal::get_language_handle();
                    163:  &Apache::loncommon::content_type(undef,'text/html');
1.101     albertel  164: 
                    165:   print(&Apache::loncommon::start_page('Creating PDF'));
1.39      sakharuk  166: 
1.40      sakharuk  167:   my $identifier = $ENV{'QUERY_STRING'};
1.76      albertel  168:   my $texfile = $env{'cgi.'.$identifier.'.file'};
                    169:   my $laystyle = $env{'cgi.'.$identifier.'.layout'};
                    170:   my $numberofcolumns = $env{'cgi.'.$identifier.'.numcol'};
                    171:   my $paper = $env{'cgi.'.$identifier.'.paper'};
                    172:   my $selectionmade = $env{'cgi.'.$identifier.'.selection'};
1.79      albertel  173:   my $tableofcontents = $env{'cgi.'.$identifier.'.tableofcontents'};
                    174:   my $tableofindex = $env{'cgi.'.$identifier.'.tableofindex'};
                    175:   my $advanced_role = $env{'cgi.'.$identifier.'.role'};
                    176:   my $number_of_files = $env{'cgi.'.$identifier.'.numberoffiles'}+1;
                    177:   my $student_names = $env{'cgi.'.$identifier.'.studentnames'};
                    178:   my $backref = &Apache::lonnet::unescape($env{'cgi.'.$identifier.'.backref'});
1.43      sakharuk  179: 
1.49      albertel  180:   
1.44      sakharuk  181:   my @names_pack=();
                    182:   if ($student_names=~/_END_/) {  
                    183:       @names_pack=split(/_ENDPERSON_/,$student_names);
                    184:   }
1.39      sakharuk  185: 
1.94      albertel  186: print "<a href=\"$backref\"><b>Return</b></a> to last resource.<br /><br />";
                    187: 
1.14      sakharuk  188:   my $figfile = $texfile;
                    189:   $figfile =~ s/^([^\.]+printout)[^t]+\.tex/$1\.dat/;
                    190:   my $duefile = $texfile;
                    191:   $duefile =~ s/^([^\.]+printout)[^t]+\.tex/$1\.due/;
                    192:   #do we have figures?
1.73      foxr      193:   # print "Figure file: $figfile\n";
1.14      sakharuk  194:   if (-e $figfile) {
1.73      foxr      195:       # print "$figfile exists\n";
1.42      albertel  196:       my %done_conversion;
1.69      matthew   197:       my $temporary_file=IO::File->new($figfile) || die "Couldn't open fig file $figfile for reading: $!\n";
1.14      sakharuk  198:       my @content_of_file = <$temporary_file>;
                    199:       close $temporary_file;  
                    200:       my $noteps;
1.49      albertel  201:       my %prog_state;
1.92      albertel  202:       if ($advanced_role) { %prog_state=&Apache::lonhtmlcommon::Create_PrgWin('','Coverting Images to EPS','Picture Conversion Status',$#content_of_file,'inline','80');  }
1.94      albertel  203:       print('<br />');
1.59      albertel  204:       foreach my $not_eps (@content_of_file) {
1.49      albertel  205: 	  chomp($not_eps);
1.14      sakharuk  206: 	  if ($not_eps ne '') {
1.73      foxr      207: 	      # print "Converting $not_eps"; # Debugging.
1.44      sakharuk  208:               my $status_statement='EPS picture for '.$not_eps;
1.73      foxr      209: 	      # print "$status_statement\n";
1.41      sakharuk  210: 	      $not_eps=~s|\/\.\/|\/|g;
1.14      sakharuk  211: 	      my $eps_f = $not_eps;
1.85      foxr      212: 	      # $eps_f =~ s/\.[^.]*$/\.eps/i;
                    213: 	      $eps_f .= '.eps';	# Just append the eps ext.
1.41      sakharuk  214: 	      if ($eps_f=~/\/home\/([^\/]+)\/public_html\//) {
                    215:                   $eps_f=~s/\/home\/([^\/]+)\/public_html/$1/;
                    216: 		  $eps_f = '/home/httpd/prtspool/'.$eps_f;
1.58      sakharuk  217: 	      } elsif ($eps_f=~/$Apache::lonnet::perlvar{'lonDocRoot'}\/res\//) {
                    218: 		  $eps_f=~m/$Apache::lonnet::perlvar{'lonDocRoot'}\/res\/(.+)/;
1.41      sakharuk  219: 		  $eps_f = '/home/httpd/prtspool/'.$1;
1.58      sakharuk  220: 	      } elsif ($eps_f=~/$Apache::lonnet::perlvar{'lonUsersDir'}\//) {
1.57      sakharuk  221: 		  $eps_f=~/$Apache::lonnet::perlvar{'lonUsersDir'}\/([^\/]+)\/\w\/\w\/\w\/(.+)/;
1.56      sakharuk  222: 		  $eps_f = '/home/httpd/prtspool/'.$1.'/'.$2;
1.41      sakharuk  223: 	      }
1.74      foxr      224: 	      $eps_f  =~ s/ /\_/g; # Spaces are problematic for system commands and LaTeX.
1.38      sakharuk  225: 	      my $path=$eps_f;
1.74      foxr      226: 	      $path =~ s/\/([^\/]+)\.eps$//;
1.73      foxr      227: 	      # print "Final file path: $path "; # Debugging
1.38      sakharuk  228: 	      File::Path::mkpath($path,0,0777);
1.14      sakharuk  229: 	      $not_eps =~ s/^\s+//;
                    230: 	      $not_eps =~ s/\s+$//;
1.74      foxr      231: 	      $not_eps =~ s/ /\\ /g;
1.72      albertel  232: 	      if ( exists($done_conversion{$not_eps})) { next; }
1.92      albertel  233: 	      if ($advanced_role) {
1.49      albertel  234: 		  my $prettyname=$not_eps;
                    235: 		  $prettyname=~s|/home/([^/]+)/public_html|/priv/$1|;
1.58      sakharuk  236: 		  $prettyname=~s|$Apache::lonnet::perlvar{'lonDocRoot'}/|/|;
1.72      albertel  237: 		  &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,'Converting to EPS '.$prettyname);
                    238: 	      }
1.42      albertel  239: 	      $done_conversion{$not_eps}=1;
1.73      foxr      240: 	      # print "Converting $not_eps -> $eps_f"; # Debugging
1.72      albertel  241: 	      system("convert $not_eps $eps_f");
1.98      albertel  242:               # check is eps exist in prtspool
                    243:               if (not -e $eps_f) {
                    244: 		  # converting an animated gif creates either:
                    245:                   # anim.gif.eps.0
                    246:                   # or
                    247:                   # anim.gif-0.eps
1.20      sakharuk  248: 		  for (my $i=0;$i<10000;$i++) {
                    249: 		      if (-e $eps_f.'.'.$i) {
1.98      albertel  250: 			  rename($eps_f.'.'.$i, $eps_f);
1.20      sakharuk  251: 			  last;
                    252: 		      }
1.98      albertel  253: 		      my $anim_eps = $eps_f;
                    254: 		      $anim_eps =~ s/(\.[^.]*)\.eps$/$1-$i\.eps/i;
                    255: 		      if (-e $anim_eps) {
                    256: 			  rename($anim_eps, $eps_f);
                    257: 			  last;
                    258: 		      }
                    259: 		  }
                    260: 	      }
                    261: 	      # imagemagick 6.2.0-6.2.7 fails to properly handle
                    262:               # convert anim.gif anim.gif.eps
                    263:               # it creates anim.eps instead. 
                    264:               if (not -e $eps_f) {
                    265: 		  my $eps_f2 = $eps_f;
                    266: 		  $eps_f2 =~ s/\.[^.]*\.eps$/\.eps/i;
                    267: 		  if(-e $eps_f2) {
                    268: 		      rename($eps_f2,$eps_f);
1.20      sakharuk  269: 		  }
1.98      albertel  270: 	      }
                    271: 
1.14      sakharuk  272: 	  }
                    273:       }
1.92      albertel  274:       if ($advanced_role) { 
1.74      foxr      275: 	  &Apache::lonhtmlcommon::Close_PrgWin('',\%prog_state); 
                    276:       }
1.52      albertel  277:       unlink($figfile);
1.14      sakharuk  278:   }
1.43      sakharuk  279:   #print "$texfile\n"; #name of the tex file for debugging only   
                    280:   my @texfile=($texfile);
                    281:   if ($number_of_files>1) {
1.49      albertel  282:       @texfile=();
                    283:       for (my $i=1;$i<=$number_of_files;$i++) {
1.43      sakharuk  284: 	  my $new_texfile=$texfile;
1.86      foxr      285: 	  $new_texfile=~s/\.tex//;
                    286: 	  $new_texfile = sprintf("%s_%03d.tex", $new_texfile,$i);
1.43      sakharuk  287: 	  push @texfile,$new_texfile;
                    288:       } 
                    289:   }
1.49      albertel  290: 
1.44      sakharuk  291: my $ind=-1;
1.49      albertel  292: my %prog_state;
1.92      albertel  293: if ($advanced_role) { %prog_state=&Apache::lonhtmlcommon::Create_PrgWin('','Print Status','Class Print Status',$number_of_files,'inline','80'); }
1.61      albertel  294: print "<br />";
1.88      foxr      295: my $num_files = @texfile;
1.43      sakharuk  296: foreach $texfile (@texfile) {
1.49      albertel  297:   my $status_statement='';
                    298:   my $link_text='download PDF';
                    299:   $ind++;
                    300:   my @stud_info=split(/_END_/,$names_pack[$ind]);
                    301:   my @tempo_array=split(/:/,$stud_info[0]);
                    302:   my $name;
1.88      foxr      303:   my $name_range='';
1.49      albertel  304:   if ($tempo_array[3]) {
                    305:       $name=$tempo_array[3];
1.89      foxr      306:       ($name_range) = split(/,/,$name, 2);
1.49      albertel  307:   } else {
                    308:       $name=$tempo_array[0].'@'.$tempo_array[1];
1.88      foxr      309:       $name_range = $tempo_array[0];
                    310:   }
                    311:   if (($name ne "") && ($name ne '@') ) { # Could be printing codes...
                    312:       $link_text='<b>'.$name.'</b>';
                    313:       $status_statement.=$name;
1.49      albertel  314:   }
                    315:   if ($#stud_info>0) {
                    316:       @tempo_array=split(/:/,$stud_info[-1]);
1.48      albertel  317:       if ($tempo_array[3]) {
                    318: 	  $name=$tempo_array[3];
1.89      foxr      319: 	  my ($lastname) = split(/,/, $name,2);
                    320: 	  $name_range .= "-".$lastname;
1.48      albertel  321:       } else {
                    322: 	  $name=$tempo_array[0].'@'.$tempo_array[1];
1.88      foxr      323: 	  $name_range .= '-'.$tempo_array[0];
                    324:       }
                    325:       if (($name ne "") && ($name ne '@')) {
                    326: 	  $link_text.=' - <b>'.$name.'</b>';
                    327: 	  $status_statement.=' -  '.$name;
                    328:   
1.48      albertel  329:       }
1.88      foxr      330:   }
                    331:   if(($num_files > 1) && ($link_text eq 'download PDF')) { # Printing codes
                    332:       $link_text = '<b>'.basename($texfile,'.tex').'.pdf</b>';
                    333:       $status_statement .= basename($texfile);
                    334:   }
                    335:   $name_range =~ s/'//g;	# O'Neil -> ONeil e.g.
                    336:   print "<br/>";
1.92      albertel  337:   if ($advanced_role) { &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,'Creating PDF for '.$status_statement); }
1.88      foxr      338:   #  This little piece of dirt puts username ranges into the original tex
                    339:   #  Tex filename from which they'll propagate into the other filenames as well.
                    340:   #
1.29      sakharuk  341:   if (-e $texfile) {
1.88      foxr      342:       if (($name_range ne '') && ($num_files > 1)) {
                    343: 	  my $newtexfile = $texfile;
                    344: 	  $newtexfile    =~ s/\.tex/$name_range\.tex/;
                    345: 	  rename($texfile, $newtexfile);
                    346: 	  $texfile       = $newtexfile;
                    347:       }
1.29      sakharuk  348:       $texfile =~ m/^(.*)\/([^\/]+)$/; 
                    349:       my $name_file = $2;
                    350:       my $path_file = $1.'/';
                    351:       chdir $path_file;
1.88      foxr      352:       my $dvi_file= $name_file; $dvi_file =~ s/\.tex/$name_range\.dvi/;
1.59      albertel  353:       &busy_wait_command("latex $name_file 1>/dev/null 2>/dev/null",
                    354: 			 "for $status_statement now LaTeXing file",
                    355: 			 \%prog_state,$dvi_file);
1.40      sakharuk  356:       if ($tableofcontents eq 'yes') {
1.63      sakharuk  357:       &busy_wait_command("latex $name_file 1>/dev/null 2>/dev/null",
1.95      foxr      358: 			 "for $status_statement First LaTeX of file for table of contents",
                    359: 			 \%prog_state,$dvi_file);
                    360:       &busy_wait_command("latex $name_file 1>/dev/null 2>/dev/null",
                    361: 			 "for $status_statement Second LaTeX of file for table of contents",
1.63      sakharuk  362: 			 \%prog_state,$dvi_file);
1.40      sakharuk  363:       } #to create table of contents
1.33      sakharuk  364:       my $idxname=$name_file;
                    365:       $idxname=~s/\.tex$/\.idx/;
1.40      sakharuk  366:       if ($tableofindex eq 'yes') {
1.63      sakharuk  367: 	  &busy_wait_command("makeindex $idxname",
                    368: 			     "making index file",
                    369: 			     \%prog_state,$idxname);
                    370: 	  &busy_wait_command("latex $name_file 1>/dev/null 2>/dev/null",
                    371: 			     "for $status_statement now LaTeXing file for index section",
                    372: 			     \%prog_state,$dvi_file);
1.33      sakharuk  373:       } #to create index
1.29      sakharuk  374:       #Do we have a latex error in the log file?
1.67      sakharuk  375:       my $logfilename = $texfile; $logfilename =~ s/\.tex$/\.log/;
1.69      matthew   376:       my $temporary_file=IO::File->new($logfilename) || die "Couldn't open log file $logfilename for reading: $!\n";
1.29      sakharuk  377:       my @content_of_file = <$temporary_file>;
                    378:       close $temporary_file; 
1.30      sakharuk  379:       my $body_log_file = join(' ',@content_of_file);
                    380:       $logfilename =~ s/\.log$/\.html/;
                    381:       $temporary_file = IO::File->new('>'.$logfilename); 
                    382:       print $temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_log_file.'</pre></body></html>'."\n";
1.29      sakharuk  383:       if ($body_log_file=~m/!\s+Emergency stop/) {
                    384: 	  my $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART';
                    385: 	  my $whereitends = rindex $body_log_file,'STAMPOFPASSEDRESOURCEEND';
1.81      foxr      386: 	  my $badresource;
1.83      foxr      387: 	  my $badtext;
1.29      sakharuk  388: 	  if ($whereitbegins!=-1 and $whereitends!=-1) {
1.83      foxr      389: 	      $badtext = substr($body_log_file,$whereitbegins+26, $whereitends-$whereitbegins-26);
1.81      foxr      390: 	      $whereitbegins  = rindex $badtext,'located in';
                    391: 	      if ($whereitbegins != -1) {
                    392: 		  
                    393: 		  $badresource = substr($badtext, $whereitbegins+27, 
                    394: 					length($badtext) - $whereitbegins - 48);
1.84      albertel  395: 		  # print "<br />failing resourcename: $badresource<br />";
1.81      foxr      396: 	      }
1.29      sakharuk  397: 	  }
1.83      foxr      398: 	  
1.75      albertel  399:           if ($advanced_role) {  
1.83      foxr      400: 	      #LaTeX failed to parse tex file 
                    401: 	      print "<h2>LaTeX could not successfully parse your tex file.</h2>";
                    402: 	      print "It probably has errors in it.<br />";
                    403: 	      print "With very high probability this error occured in ".$badtext."<br /><br />";
1.84      albertel  404: 	      print "Here are the error messages in the LaTeX log file<br /><pre>";
1.90      foxr      405: 
1.83      foxr      406: 	      my $sygnal = 0;
                    407: 	      for (my $i=0;$i<=$#content_of_file;$i++) {
                    408: 		  if ($content_of_file[$i]=~m/^Runaway argument?/ or $content_of_file[$i]=~m/^!/) {
                    409: 		      $sygnal = 1;
                    410: 		  } 
                    411: 		  if ($content_of_file[$i]=~m/Here is how much of/) {
                    412: 		      $sygnal = 0;
                    413: 		  } 
                    414: 		  if ($sygnal) {
                    415: 		      print "$content_of_file[$i]";
                    416: 		  }  
                    417: 	      }
                    418: 	      print "</pre>\n";
1.84      albertel  419: 	      # print "<br /> Advanced role <br />";
1.35      sakharuk  420:               print "<b><big>The link to ";
                    421:               $logfilename=~s/\/home\/httpd//;
                    422: 	      print "<a href=\"$logfilename\">Your log file </a></big></b>";
                    423: 	      print "\n";
                    424:               #link tooriginal LaTeX file (included according Michael Hamlin desire)
1.69      matthew   425: 	      my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open tex file $texfile for reading: $!\n";
1.35      sakharuk  426: 	      my @tex_content_of_file = <$tex_temporary_file>;
                    427: 	      close $tex_temporary_file; 
                    428: 	      my $body_tex_file = join(' ',@tex_content_of_file);
                    429: 	      $texfile =~ s/\.tex$/aaaaa\.html/;
                    430: 	      $tex_temporary_file = IO::File->new('>'.$texfile); 
                    431: 	      print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
                    432: 	      print "<br /><br />";
                    433: 	      print "<b><big>The link to ";
                    434: 	      $texfile=~s/\/home\/httpd//;
                    435: 	      print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
                    436: 	      print "\n";
1.90      foxr      437: 	      my $help_text = &Apache::loncommon::help_open_topic("Print_Resource", "Help on printing");
                    438: 	      print ("$help_text");
                    439: 
1.77      foxr      440: 	  } else {		# Student role...
                    441: 	      #  at this point:
                    442: 	      #    $body_log_file - contains the log file.
                    443:               #    $name_file     - is the name of the LaTeX file.
                    444:               #    $identifier    - is the unique LaTeX identifier.l
                    445: 
1.84      albertel  446: 	      print "<br />There are errors in $badtext";
                    447: 	      print "<br />These errors prevent this resource from printing correctly";
1.77      foxr      448: 	      my $tex_handle = IO::File->new($name_file);
                    449: 	      my @tex_contents = <$tex_handle>;
1.81      foxr      450: 	      &send_error_mail($identifier, $badresource, $body_log_file, \@tex_contents);
1.90      foxr      451: 	      print "<br />A message has been sent to the instructor describing this failure<br />";
                    452: 	      my $help_text = &Apache::loncommon::help_open_topic("Print_Resource", "Help on printing");
                    453: 	      print  ("$help_text");
                    454: 
1.36      sakharuk  455: 	  }
1.35      sakharuk  456: 
1.29      sakharuk  457:       } elsif ($body_log_file=~m/<inserted text>/) {
                    458: 	  my $whereitbegins = index $body_log_file,'<inserted text>';
1.59      albertel  459: 	  print "You are running LaTeX in <b>batch mode</b>.";
1.30      sakharuk  460: 	  while ($whereitbegins != -1) {
                    461: 	      my $tempobegin=$whereitbegins;
                    462: 	      $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART',$whereitbegins;
                    463: 	      my $whereitends = index $body_log_file,'STAMPOFPASSEDRESOURCEEND',$whereitbegins;
1.34      sakharuk  464: 	      print "<br />It has found an error in".substr($body_log_file,$whereitbegins+26,$whereitends-$whereitbegins-26)." <br /> and corrected it.\n";
1.30      sakharuk  465: 	      print "Usually this correction is valid but you probably need to check the indicated resource one more time and implement neccessary corrections by yourself.\n";
                    466: 	      $whereitbegins = index $body_log_file,'<inserted text>',$tempobegin+10;
                    467: 	  }
1.29      sakharuk  468: 	  $name_file =~ s/\.tex/\.dvi/;
                    469: 	  my $new_name_file = $name_file;
                    470: 	  $new_name_file =~ s/\.dvi/\.ps/;
1.67      sakharuk  471: 	  my $papera=$paper;
1.62      sakharuk  472: 	  if ($papera eq 'letter') {$papera='';}
                    473: 	  if ($papera ne '') {$papera='-t'.$papera;}
                    474: 	  my $comma = "dvips $papera -Ppdf -G0 -o $new_name_file";
1.59      albertel  475: 	  &busy_wait_command("$comma $name_file 1>/dev/null 2>/dev/null",
                    476: 			     "for $status_statement now Converting to PS",
                    477: 			     \%prog_state,$new_name_file);
1.29      sakharuk  478: 	  if (-e $new_name_file) {
1.102     foxr      479: 	      my $latex_file = $name_file;
                    480: 	      $latex_file    =~ s/\.dvi/\.tex/;
                    481: 	      &repaginate($new_name_file, $latex_file, $numberofcolumns);
                    482: 	      #
                    483: 	      #  Now have to re-latex, re dvips again to 
                    484: 	      #  get the repaginated postscript.
                    485: 	      #
                    486: 	      &busy_wait_command("latex $latex_file 1>/dev/null 2>/dev/null",
                    487: 				 "for $status_statement first latex to repaginate",
                    488: 				 \%prog_state, $name_file);
                    489: 	      &busy_wait_command("latex $latex_file 1>/dev/null 2>/dev/null",
                    490: 				 "for $status_statement second latex to repaginate",
                    491: 				 \%prog_state, $name_file);
                    492: 	      &busy_wait_command("latex $latex_file 1>/dev/null 2>/dev/null",
                    493: 				 "for $status_statement third latex to repaginate",
                    494: 				 \%prog_state, $name_file);
                    495: 	      &busy_wait_command("$comma $name_file 1>dev/null 2>/dev/null",
                    496: 				 "for $status_statement dvips to repaginate",
                    497: 				 \%prog_state, $new_name_file);
                    498: 
                    499: 	      print "\n<h1>PDF output file (see link below)</h1>\n";
1.29      sakharuk  500: 	      $new_name_file =~ m/^(.*)\./;
1.59      albertel  501: 	      my $ps_file = my $tempo_file = $1.'temporar.ps';
                    502: 	      my $pdf_file = $1.'.pdf';
1.29      sakharuk  503: 	      if ($laystyle eq 'album' and $numberofcolumns eq '2') {
1.82      albertel  504: 		  my $papera=$paper;
                    505:                   if ($papera eq 'letter') {$papera='';}
                    506: 		  if ($papera ne '') {$papera='-p'.$papera;}
                    507: 		  $comma = "psnup $papera -2 -s1.0 $new_name_file";
1.59      albertel  508: 		  &busy_wait_command("$comma $tempo_file 1>/dev/null 2>/dev/null",
                    509: 				     "for $status_statement now Modifying PS layout",
                    510: 				     \%prog_state,$tempo_file); 
1.29      sakharuk  511: 	      } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
1.67      sakharuk  512: 		  my $papera=$paper;
                    513:                   if ($papera eq 'letter') {$papera='';}
1.62      sakharuk  514: 		  if ($papera ne '') {$papera='-p'.$papera;}
                    515: 		  $comma = 'pstops '.$papera.' "2:0+1(0.48w,0)"';
1.63      sakharuk  516: 		  &busy_wait_command("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null",
                    517: 				     "for $status_statement now Modifying PS layout",
                    518: 				     \%prog_state,$tempo_file); 
                    519: 
1.29      sakharuk  520: 	      } else {
1.59      albertel  521: 		  $ps_file=$new_name_file;
1.29      sakharuk  522: 	      }	    
1.59      albertel  523: 	      &busy_wait_command("ps2pdf $ps_file $pdf_file 1>/dev/null 2>/dev/null",
                    524: 				 "for $status_statement now Converting PS to PDF",
                    525: 				 \%prog_state, $pdf_file);
                    526: 
1.29      sakharuk  527: 	      my $texlog = $texfile;
                    528: 	      my $texaux = $texfile;
                    529: 	      my $texdvi = $texfile;
                    530: 	      my $texps = $texfile;
                    531: 	      $texlog =~ s/\.tex/\.log/;
                    532: 	      $texaux =~ s/\.tex/\.aux/;
                    533: 	      $texdvi =~ s/\.tex/\.dvi/;
                    534: 	      $texps =~ s/\.tex/\.ps/;
1.30      sakharuk  535: 	      my @garb = ($texaux,$texdvi,$texps);
1.29      sakharuk  536: #	  unlink @garb;
                    537: 	      unlink $duefile;
1.59      albertel  538: 	      print "<a href=\"/prtspool/$pdf_file\">Your PDF document</a>";
1.75      albertel  539: 	  }
                    540: 	  if ($advanced_role) {  
                    541: 	      print "<br /><br />";
                    542: 	      print "<b><big>The link to ";
                    543: 	      $logfilename=~s/\/home\/httpd//;
                    544: 	      print "<a href=\"$logfilename\">Your log file </a></big></b>";
                    545: 	      print "\n";
                    546: 	      #link tooriginal LaTeX file (included according Michael Hamlin desire)
                    547: 	      my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open tex file $texfile for reading: $!\n";
                    548: 	      my @tex_content_of_file = <$tex_temporary_file>;
                    549: 	      close $tex_temporary_file; 
                    550: 	      my $body_tex_file = join(' ',@tex_content_of_file);
                    551: 	      $texfile =~ s/\.tex$/aaaaa\.html/;
                    552: 	      $tex_temporary_file = IO::File->new('>'.$texfile); 
                    553: 	      print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
                    554: 	      print "<br /><br />";
                    555: 	      print "<b><big>The link to ";
                    556: 	      $texfile=~s/\/home\/httpd//;
                    557: 	      print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
                    558: 	      print "\n";
1.29      sakharuk  559: 	  }
                    560:       } else {
                    561: 	  #LaTeX successfully parsed tex file 
                    562: 	  $name_file =~ s/\.tex/\.dvi/;
                    563: 	  my $new_name_file = $name_file;
                    564: 	  $new_name_file =~ s/\.dvi/\.ps/;
1.67      sakharuk  565: 	  my $papera=$paper;
1.62      sakharuk  566: 	  if ($papera eq 'letter') {$papera='';}
                    567: 	  if ($papera ne '') {$papera='-t'.$papera;}
                    568: 	  my $comma = "dvips $papera -Ppdf -G0 -o $new_name_file";
1.59      albertel  569: 	  &busy_wait_command("$comma $name_file 1>/dev/null 2>/dev/null",
                    570: 			     "for $status_statement now Converting to PS",
                    571: 			     \%prog_state,$new_name_file);
1.29      sakharuk  572: 	  if (-e $new_name_file) {
1.102     foxr      573: 	      my $latex_file = $name_file;
                    574: 	      $latex_file =~ s/\.dvi/\.tex/;
                    575: 	      &repaginate($new_name_file, $latex_file,  $numberofcolumns);
                    576: 	      &busy_wait_command("latex $latex_file 1>/dev/null 2>/dev/null",
                    577: 				 "for $status_statement first latex to repaginate",
                    578: 				 \%prog_state, $name_file);
                    579: 	      &busy_wait_command("latex $latex_file 1>/dev/null 2>/dev/null",
                    580: 				 "for $status_statement second latex to repaginate",
                    581: 				 \%prog_state, $name_file);
                    582: 	      &busy_wait_command("latex $latex_file 1>/dev/null 2>/dev/null",
                    583: 				 "for $status_statement third latex to repaginate",
                    584: 				 \%prog_state, $name_file);
                    585: 	      &busy_wait_command("$comma $name_file 1>/dev/null 2>/dev/null",
                    586: 				 "for $status_statement dvips to repaginate",
                    587: 				 \%prog_state, $new_name_file);
1.61      albertel  588: 	      print "<br />";
1.29      sakharuk  589: 	      $new_name_file =~ m/^(.*)\./;
1.59      albertel  590: 	      my $ps_file = my $tempo_file = $1.'temporar.ps';
                    591: 	      my $pdf_file = $1.'.pdf';
1.82      albertel  592: 	      $papera=~s/t/p/;
1.29      sakharuk  593: 	      if ($laystyle eq 'album' and $numberofcolumns eq '2') {
1.82      albertel  594: 		  $comma = "psnup $papera -2 -s1.0 $new_name_file";
1.59      albertel  595: 		  &busy_wait_command("$comma $tempo_file 1>/dev/null 2>/dev/null",
                    596: 				     "for $status_statement now Modifying PS layout",
                    597: 				     \%prog_state,$tempo_file);
1.29      sakharuk  598: 	      } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
1.65      sakharuk  599: 		  $comma = 'pstops '.$papera.' "2:0+1(0.48w,0)"';
1.59      albertel  600: 		  &busy_wait_command("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null",
                    601: 				     "for $status_statement now Modifying PS layout",
                    602: 				     \%prog_state,$tempo_file); 
1.29      sakharuk  603: 	      } else {
1.59      albertel  604: 		  $ps_file=$new_name_file;
                    605: 	      }
1.67      sakharuk  606: 	      my $addtoPSfile={'legal'=>'<< /PageSize [612 1008] >> setpagedevice',
                    607:                                'tabloid'=>'<< /PageSize [792 1224] >> setpagedevice',
                    608:                                'executive'=>,'<< /PageSize [540 720] >> setpagedevice',
                    609:                                'a2'=>'<< /PageSize [1195.02 1690.09] >> setpagedevice',
                    610:                                'a3'=>'<< /PageSize [842 1195.02] >> setpagedevice',
                    611:                                'a4'=>'<< /PageSize [595.2 842] >> setpagedevice',
                    612:                                'a5'=>'<< /PageSize [421.1 595.2] >> setpagedevice',
                    613:                                'a6'=>'<< /PageSize [298.75 421.1] >> setpagedevice',
                    614: 			   };
                    615: 	      if ($paper ne 'letter') {
1.69      matthew   616: 		  open(FFH,'<',$ps_file) || die "Couldn't open ps file $ps_file for reading: $!\n";
1.63      sakharuk  617: 		  my $new_ps_file='new'.$ps_file;
1.69      matthew   618: 		  open(FFHS,'>',$new_ps_file) || die "Couldn't open new ps file $new_ps_file for reading: $!\n";
1.67      sakharuk  619: 		  print FFHS $addtoPSfile->{$paper}."\n";
1.63      sakharuk  620: 		  while (<FFH>) {
                    621: 		      print FFHS $_;
                    622: 		  }
1.62      sakharuk  623: 		  close(FFH);
1.63      sakharuk  624: 		  close(FFHS);
                    625: 		  $ps_file=$new_ps_file;	  
1.62      sakharuk  626: 	      }
1.59      albertel  627: 	      &busy_wait_command("ps2pdf $ps_file $pdf_file 1>/dev/null 2>/dev/null",
                    628: 				 "for $status_statement now Converting PS to PDF",
                    629: 				 \%prog_state,$pdf_file);
                    630: 	    
1.29      sakharuk  631: 	      my $texlog = $texfile;
                    632: 	      my $texaux = $texfile;
                    633: 	      my $texdvi = $texfile;
                    634: 	      my $texps = $texfile;
                    635: 	      $texlog =~ s/\.tex/\.log/;
                    636: 	      $texaux =~ s/\.tex/\.aux/;
                    637: 	      $texdvi =~ s/\.tex/\.dvi/;
                    638: 	      $texps =~ s/\.tex/\.ps/;
                    639: 	      my @garb = ($texlog,$texaux,$texdvi,$texps);
1.22      sakharuk  640: #	  unlink @garb;
1.29      sakharuk  641: 	      unlink $duefile;
1.68      sakharuk  642: 	      print "<a href=\"/prtspool/$pdf_file\">$link_text - click here to download pdf</a>";
1.29      sakharuk  643: 	      print "\n";
                    644: 	  }
1.63      sakharuk  645:       }  
1.29      sakharuk  646:   } else {
                    647:       print "LaTeX file $texfile was not created successfully";
1.14      sakharuk  648:   }
1.43      sakharuk  649: }
1.45      sakharuk  650: print "<br />";
1.44      sakharuk  651: if ($number_of_files>1) {
1.45      sakharuk  652:     my $zipfile=$texfile[0];
                    653:     $zipfile=~s/\.tex/\.zip/;
                    654:     my $statement="zip $zipfile";
1.44      sakharuk  655:     foreach my $file (@texfile) {
1.45      sakharuk  656: 	$file=~s/\.tex/.\pdf/;
                    657: 	$statement.=' '.$file; 
1.44      sakharuk  658:     }
1.49      albertel  659:     print("<pre>Zip Output:\n");
                    660:     system($statement);
                    661:     print("</pre>");
1.45      sakharuk  662:     $zipfile=~s/\/home\/httpd//;
1.49      albertel  663:     print "<br /> A <a href=\"$zipfile\">ZIP file</a> of all the PDFs.";
1.44      sakharuk  664: }
1.92      albertel  665: if ($advanced_role) { &Apache::lonhtmlcommon::Close_PrgWin('',\%prog_state); }
1.97      albertel  666: print(&Apache::loncommon::end_page());
1.59      albertel  667: my $done;
1.102     foxr      668: 
1.59      albertel  669: sub REAPER {
                    670:     $done=1;
                    671: }
                    672: 
                    673: sub busy_wait_command {
                    674:     my ($command,$message,$progress_win,$output_file)=@_;
                    675:     
                    676:     $SIG{CHLD} = \&REAPER;
                    677:     $done=0;
1.101     albertel  678:     print("<p> Doing : <tt>$command</tt>");
1.59      albertel  679:     my $pid=open(CMD,"$command |");
1.92      albertel  680:     if ($advanced_role) {
1.60      albertel  681: 	&Apache::lonhtmlcommon::Update_PrgWin('',$progress_win,$message);
                    682:     }
1.59      albertel  683:     while(!$done) {
                    684: 	sleep 1;
                    685: 	my $extra_msg;
                    686: 	if ($output_file) {
                    687: 	    my $size=(stat($output_file))[7];
                    688: 	    $extra_msg=", $size bytes generated";
                    689: 	}
1.92      albertel  690: 	if ($advanced_role) {
1.60      albertel  691: 	    &Apache::lonhtmlcommon::Update_PrgWin('',$progress_win,
                    692: 						  $message.$extra_msg);
                    693: 	}
1.59      albertel  694:     }
                    695:     $SIG{CHLD}='IGNORE';
                    696:     close(CMD);
                    697: }
1.101     albertel  698:     print("<p> End</p>");
1.1       sakharuk  699: 
1.102     foxr      700: #  Repagninate
1.99      foxr      701: #  What we need to do:
                    702: #   - Count the number of pages in each student.
1.102     foxr      703: #   - Rewrite the latex file replacing the \specials that
                    704: #     mark the end of student with an appropriate number of newlines.
                    705: #   parameters:
                    706: #     psfile     - Postscript filename
                    707: #     latexfile  - LaTeX filename
                    708: #     columns    - number of columns.
                    709: sub repaginate {
1.100     foxr      710: 
                    711:     # We will try to do this in 2 passes through the postscript since
                    712:     # the postscript is potentially large, to do 2 passes, the first pass
                    713:     # must be able to calculate the total number of document pages so that
                    714:     # at the beginning of the second pass we already know how to replace
                    715:     #  %%Pages:
                    716: 
                    717:     #  Figure out
                    718:     #    1. Number of pages in the document
                    719:     #    2. Maximum number of pages in a student
                    720:     #    3. Number of pages in each student.
1.99      foxr      721: 
1.102     foxr      722:     my ($postscript_filename, $latex_filename, $num_columns) = @_;
1.99      foxr      723:     open(PSFILE, "<$postscript_filename");
1.100     foxr      724:     my $line;
                    725:     my $total_pages;		# Total pages in document.
                    726:     my $seen_pages        = 0;	# There are several %%Pages only the first is useful
                    727:     my $student_number    = 0;	# Index of student we're working on.
                    728:     my @pages_in_student;	# For each student his/her initial page count.
                    729:     my $max_pages = 0;		# Pages in 'longest' student.
1.102     foxr      730:     my $page_number = 0;
1.100     foxr      731:     while ($line = <PSFILE>) {
                    732: 	
                    733: 	# Check for total pages (%%Pages:)
                    734: 
1.101     albertel  735: 	if (($line =~ /^%%Pages:/) && (!$seen_pages)) {
1.100     foxr      736: 	    my @pageinfo = split(/ /,$line);
                    737: 	    $total_pages = $pageinfo[1];
                    738: 	    $seen_pages  = 1;
                    739: 	}
                    740: 	#  Check for %%Page: n m  $page_number will be the
                    741: 	#  biggest of these until we see an endofstudent.
                    742: 	#  Note that minipages generate spurious %Page: 1 1's so
                    743: 	#  we only are looking for the largest n (n is page number at the
                    744: 	#  bottom of the page, m the page number within the document.
                    745: 	#
1.102     foxr      746: 
1.101     albertel  747: 	if ($line =~ /^%%Page:/) {
1.100     foxr      748: 	    my @pageinfo = split(/ /, $line);
                    749: 	    if ($page_number < $pageinfo[1]) {
                    750: 		$page_number = $pageinfo[1];
                    751: 	    }
                    752: 	}
                    753: 	#  ENDOFSTUDENTSTAMP - save the page_number, reset and, if necessary
                    754: 	#                      udpate max_pages.
                    755: 	#
1.101     albertel  756: 	if ($line =~ /ENDOFSTUDENTSTAMP/) {
1.100     foxr      757: 	    $pages_in_student[$student_number] = $page_number;
                    758: 	    $student_number++;
                    759: 
                    760: 	    if ($page_number > $max_pages) {
                    761: 		$max_pages = $page_number;
                    762: 	    }
                    763: 	    $page_number = 0;
                    764: 	 
                    765: 	}
                    766: 
                    767: 	
                    768:     }
1.99      foxr      769:     close(PSFILE);
1.102     foxr      770: 
                    771:     #  If 2 columns, max_pages must go to an even number of columns:
1.103   ! foxr      772: 
        !           773:     &Apache::lonnet::logthis("Before 2 col adjust max-pages = $max_pages");
1.102     foxr      774:    
                    775:     if ($num_columns == 2) {
                    776: 	if ($max_pages % 2) {
                    777: 	    $max_pages++;
                    778: 	}
                    779:     }
1.103   ! foxr      780:     &Apache::lonnet::logthis("after adjust: $max_pages");
1.100     foxr      781:     
1.102     foxr      782:     #  Now rewrite the LaTex file, substituting our \special
                    783:     #  with an appropriate number of \newpage directives.
                    784: 
                    785:     my $outfilename = $latex_filename."temp";
                    786: 
                    787:     open(LATEXIN, "<$latex_filename");
                    788:     open(LATEXOUT, ">$outfilename");
                    789: 
                    790: 
                    791:     $student_number = 0;	# first student...
                    792: 
                    793:     while (my $line = <LATEXIN>) {
                    794: 	if ($line eq "\\special{ps:ENDOFSTUDENTSTAMP}\n") {
                    795: 	    # only end of student stamp if next line is ENDOFSTUDENTSTAMP:
                    796: 
                    797: 
                    798: 	    # End of student replace with 0 or more newpages.
                    799: 	    
                    800: 	    my $addlines = $max_pages - $pages_in_student[$student_number];
1.103   ! foxr      801: 	    &Apache::lonnet::logthis("student: $pages_in_student[$student_number] add: $addlines");
1.102     foxr      802: 	    while($addlines)  {
1.103   ! foxr      803: 		print LATEXOUT '\pagebreak';
1.102     foxr      804: 		$addlines--;
1.100     foxr      805: 	    }
1.102     foxr      806: 	    
1.100     foxr      807: 	    $student_number++;
1.102     foxr      808: 	    
                    809: 	} else {
                    810: 	    print LATEXOUT $line;
1.100     foxr      811: 	}
                    812:     }
                    813: 
1.102     foxr      814:     close(LATEXIN);
                    815:     close(LATEXOUT);
                    816:    rename($outfilename, $latex_filename);
1.100     foxr      817: 
1.99      foxr      818: }
1.4       sakharuk  819: 

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