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

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.47      sakharuk   34: use Apache::lonhtmlcommon;
1.80      albertel   35: use Apache::lonnet;
1.51      albertel   36: use Apache::loncommon;
                     37: use Apache::lonlocal;
1.77      foxr       38: use Apache::lonmsg;
                     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: #
                     45: #   Determine if a user is operating as a student for this course/domain.
                     46: #
                     47: #
                     48: #Parameters:
                     49: #   course  - The course id.
                     50: #   cdom    - The course domain.
                     51: #
                     52: #Implicit:
                     53: #    $env{request.role} contains the role under which this user operated this
                     54: #                       this request.
                     55: sub is_student {
1.81      foxr       56:     return (! $env{'request.role.adv'});
1.77      foxr       57: }
                     58: 
                     59: #
                     60: #   Debugging:  Dump the environment for debugging.
                     61: #
                     62: sub dumpenv  {
1.84      albertel   63:     print "<br />-------------------<br />";
1.77      foxr       64:     foreach my $key (sort (keys %env)) {
1.84      albertel   65: 	print "<br />$key -> $env{$key}";
1.77      foxr       66:     }
1.84      albertel   67:     print "<br />-------------------<br />";
1.77      foxr       68: }
                     69: 
                     70: #
                     71: #   This sub sends a message to the appropriate person if there was an error
                     72: #   rendering the latex  At present, there's only one case to consider:
                     73: #   a student printing inside a course results in messages to the course coordinator.
                     74: #Parmaeters:
                     75: #    identifier -  The unique identifier of this cgi request.
1.81      foxr       76: #    badresource-  Filepath to most likely failing 
1.77      foxr       77: #    logfile    -  The contents of the log file (included in the message).
                     78: #    texfile    -  reference to an array containing the LaTeX input file
                     79: #                  (included in the message).
                     80: #Implicit inputs:
                     81: #   From the environment:
                     82: #       cgi.$identifier.user     - User doing the printing.
                     83: #       cgi.$identifier.domain   - Domain the user is logged in on with printing.
                     84: #       cgi.$identifier.courseid - Id of the course (if this is a course).
                     85: #       cgi.$identifier.coursedom- Domain in which course was constituted.
                     86: #       cgi.$identifier.resources - List of resource URL's for which the print
                     87: #                                  was attempted.
                     88: # 
                     89: sub send_error_mail {
1.81      foxr       90:     my ($identifier, $badresource, $logfile, $texfile) = @_;
1.77      foxr       91:     my $user     = $env{"cgi.$identifier.user"};
                     92:     my $domain   = $env{"cgi.$identifier.domain"};
                     93:     my $courseid = $env{"cgi.$identifier.courseid"};
                     94:     my $coursedom= $env{"cgi.$identifier.coursedom"};
                     95:     my $resources= $env{"cgi.$identifier.resources"};
                     96: 
1.81      foxr       97:     #  resource file->URL
                     98:     #
                     99:     my $badurl = &Apache::lonnet::declutter($badresource);
                    100: 
                    101:     # &dumpenv();
1.77      foxr      102: 
                    103: 
                    104: 
                    105:     #  Only continue if there is a user, domain, courseid, course domain
                    106:     #  and resources:
                    107: 
                    108:     if(defined($user)       && defined($domain) && defined($courseid) &&
                    109:        defined($coursedom)  && defined($resources) ){
                    110: 	   
                    111: 	#  Only mail if the conditions are ripe for it:
                    112: 	#  The user is a student in the course:
                    113: 	#
                    114: 	
                    115: 	if (&is_student( $courseid, $coursedom)) {
                    116: 	    # build the subject and message body:
1.84      albertel  117: 	    # print "sending message to course coordinators.<br />";
1.81      foxr      118: 
                    119: 	    # Todo: Convert badurl into a url from file path:
                    120: 
                    121: 	    my $subject  = "Error [$badurl] Print failed for $user".'@'.$domain;
1.77      foxr      122: 	    my $message .= "Print failed to render LaTeX for $user".'@'."$domain\n";
                    123: 	    $message    .= "  User was attempting to print: \n";
1.91    ! albertel  124: 	    foreach my $resource (split(/:/,$resources)) {
        !           125: 		$message    .= "       $resource\n";
        !           126: 	    }
1.77      foxr      127: 	    $message    .= "--------------------LaTeX logfile:------------ \n";
                    128: 	    $message    .= $logfile;
                    129: 	    $message    .= "-----------------LaTeX source file: ------------\n";
                    130: 	    
                    131: 	    foreach my $line (@$texfile) {
                    132: 		$message .= "$line\n";
                    133: 	    }
1.81      foxr      134: 	    my (undef, %receivers) = &Apache::lonfeedback::decide_receiver(undef, 0,
                    135: 									  1,1,1);
1.84      albertel  136: 	    # print "<br /> sending...section:  $env{'request.course.sec'}";
1.81      foxr      137: 	    foreach my $dest (keys %receivers) {
1.84      albertel  138: 		# print "<br /> dest is $dest";
1.77      foxr      139: 		my @destinfo = split(/:/,$dest);
                    140: 		my $user = $destinfo[0];
                    141: 		my $dom  = $destinfo[1];
                    142: 
                    143: 		&Apache::lonmsg::user_normal_msg($user, $dom,
                    144: 						 $subject, $message);
                    145: 		
                    146: 		# No point in looking at the return status as there's no good
                    147: 		# error action I can think of right now (log maybe?).
                    148: 	    }
                    149: 	}
                    150:     }
                    151: }
                    152: 
1.49      albertel  153: $|=1;
1.39      sakharuk  154: if (! &LONCAPA::loncgi::check_cookie_and_load_env()) {
                    155:     print <<END;
                    156: Content-type: text/html
                    157: 
                    158: <html>
                    159: <head><title>Bad Cookie</title></head>
                    160: <body>
1.40      sakharuk  161: Your cookie information is incorrect.
1.39      sakharuk  162: </body>
                    163: </html>
                    164: END
                    165:     return;
                    166: }
1.51      albertel  167:  &Apache::lonlocal::get_language_handle();
                    168:  &Apache::loncommon::content_type(undef,'text/html');
                    169:  my $bodytag=&Apache::loncommon::bodytag('Creating PDF','','');
                    170:  print $bodytag;
1.39      sakharuk  171: 
1.40      sakharuk  172:   my $identifier = $ENV{'QUERY_STRING'};
1.76      albertel  173:   my $texfile = $env{'cgi.'.$identifier.'.file'};
                    174:   my $laystyle = $env{'cgi.'.$identifier.'.layout'};
                    175:   my $numberofcolumns = $env{'cgi.'.$identifier.'.numcol'};
                    176:   my $paper = $env{'cgi.'.$identifier.'.paper'};
                    177:   my $selectionmade = $env{'cgi.'.$identifier.'.selection'};
1.79      albertel  178:   my $tableofcontents = $env{'cgi.'.$identifier.'.tableofcontents'};
                    179:   my $tableofindex = $env{'cgi.'.$identifier.'.tableofindex'};
                    180:   my $advanced_role = $env{'cgi.'.$identifier.'.role'};
                    181:   my $number_of_files = $env{'cgi.'.$identifier.'.numberoffiles'}+1;
                    182:   my $student_names = $env{'cgi.'.$identifier.'.studentnames'};
                    183:   my $backref = &Apache::lonnet::unescape($env{'cgi.'.$identifier.'.backref'});
1.43      sakharuk  184: 
1.62      sakharuk  185: 
1.76      albertel  186:   my $adv = $env{'request.role.adv'};
1.49      albertel  187:   
1.44      sakharuk  188:   my @names_pack=();
                    189:   if ($student_names=~/_END_/) {  
                    190:       @names_pack=split(/_ENDPERSON_/,$student_names);
                    191:   }
1.39      sakharuk  192: 
1.14      sakharuk  193:   my $figfile = $texfile;
                    194:   $figfile =~ s/^([^\.]+printout)[^t]+\.tex/$1\.dat/;
                    195:   my $duefile = $texfile;
                    196:   $duefile =~ s/^([^\.]+printout)[^t]+\.tex/$1\.due/;
                    197:   #do we have figures?
1.73      foxr      198:   # print "Figure file: $figfile\n";
1.14      sakharuk  199:   if (-e $figfile) {
1.73      foxr      200:       # print "$figfile exists\n";
1.42      albertel  201:       my %done_conversion;
1.69      matthew   202:       my $temporary_file=IO::File->new($figfile) || die "Couldn't open fig file $figfile for reading: $!\n";
1.14      sakharuk  203:       my @content_of_file = <$temporary_file>;
                    204:       close $temporary_file;  
                    205:       my $noteps;
1.49      albertel  206:       my %prog_state;
1.59      albertel  207:       if ($adv) { %prog_state=&Apache::lonhtmlcommon::Create_PrgWin('','Coverting Images to EPS','Picture Conversion Status',$#content_of_file,'inline','80');  }
                    208:       foreach my $not_eps (@content_of_file) {
1.49      albertel  209: 	  chomp($not_eps);
1.14      sakharuk  210: 	  if ($not_eps ne '') {
1.73      foxr      211: 	      # print "Converting $not_eps"; # Debugging.
1.44      sakharuk  212:               my $status_statement='EPS picture for '.$not_eps;
1.73      foxr      213: 	      # print "$status_statement\n";
1.41      sakharuk  214: 	      $not_eps=~s|\/\.\/|\/|g;
1.14      sakharuk  215: 	      my $eps_f = $not_eps;
1.85      foxr      216: 	      # $eps_f =~ s/\.[^.]*$/\.eps/i;
                    217: 	      $eps_f .= '.eps';	# Just append the eps ext.
1.41      sakharuk  218: 	      if ($eps_f=~/\/home\/([^\/]+)\/public_html\//) {
                    219:                   $eps_f=~s/\/home\/([^\/]+)\/public_html/$1/;
                    220: 		  $eps_f = '/home/httpd/prtspool/'.$eps_f;
1.58      sakharuk  221: 	      } elsif ($eps_f=~/$Apache::lonnet::perlvar{'lonDocRoot'}\/res\//) {
                    222: 		  $eps_f=~m/$Apache::lonnet::perlvar{'lonDocRoot'}\/res\/(.+)/;
1.41      sakharuk  223: 		  $eps_f = '/home/httpd/prtspool/'.$1;
1.58      sakharuk  224: 	      } elsif ($eps_f=~/$Apache::lonnet::perlvar{'lonUsersDir'}\//) {
1.57      sakharuk  225: 		  $eps_f=~/$Apache::lonnet::perlvar{'lonUsersDir'}\/([^\/]+)\/\w\/\w\/\w\/(.+)/;
1.56      sakharuk  226: 		  $eps_f = '/home/httpd/prtspool/'.$1.'/'.$2;
1.41      sakharuk  227: 	      }
1.74      foxr      228: 	      $eps_f  =~ s/ /\_/g; # Spaces are problematic for system commands and LaTeX.
1.38      sakharuk  229: 	      my $path=$eps_f;
1.74      foxr      230: 	      $path =~ s/\/([^\/]+)\.eps$//;
1.73      foxr      231: 	      # print "Final file path: $path "; # Debugging
1.38      sakharuk  232: 	      File::Path::mkpath($path,0,0777);
1.14      sakharuk  233: 	      $not_eps =~ s/^\s+//;
                    234: 	      $not_eps =~ s/\s+$//;
1.74      foxr      235: 	      $not_eps =~ s/ /\\ /g;
1.72      albertel  236: 	      if ( exists($done_conversion{$not_eps})) { next; }
1.49      albertel  237: 	      if ($adv) {
                    238: 		  my $prettyname=$not_eps;
                    239: 		  $prettyname=~s|/home/([^/]+)/public_html|/priv/$1|;
1.58      sakharuk  240: 		  $prettyname=~s|$Apache::lonnet::perlvar{'lonDocRoot'}/|/|;
1.72      albertel  241: 		  &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,'Converting to EPS '.$prettyname);
                    242: 	      }
1.42      albertel  243: 	      $done_conversion{$not_eps}=1;
1.73      foxr      244: 	      # print "Converting $not_eps -> $eps_f"; # Debugging
1.72      albertel  245: 	      system("convert $not_eps $eps_f");
1.20      sakharuk  246:               #check is eps exist in prtspool
                    247:               if(not -e $eps_f) {
                    248: 		  for (my $i=0;$i<10000;$i++) {
                    249: 		      if (-e $eps_f.'.'.$i) {
                    250: 			  rename $eps_f.'.'.$i, $eps_f;
                    251: 			  last;
                    252: 		      }
                    253: 		  }
                    254: 	      }  
1.14      sakharuk  255: 	  }
                    256:       }
1.74      foxr      257:       if ($adv) { 
                    258: 	  &Apache::lonhtmlcommon::Close_PrgWin('',\%prog_state); 
                    259:       }
1.52      albertel  260:       unlink($figfile);
1.14      sakharuk  261:   }
1.43      sakharuk  262:   #print "$texfile\n"; #name of the tex file for debugging only   
                    263:   my @texfile=($texfile);
                    264:   if ($number_of_files>1) {
1.49      albertel  265:       @texfile=();
                    266:       for (my $i=1;$i<=$number_of_files;$i++) {
1.43      sakharuk  267: 	  my $new_texfile=$texfile;
1.86      foxr      268: 	  $new_texfile=~s/\.tex//;
                    269: 	  $new_texfile = sprintf("%s_%03d.tex", $new_texfile,$i);
1.43      sakharuk  270: 	  push @texfile,$new_texfile;
                    271:       } 
                    272:   }
1.49      albertel  273: 
1.44      sakharuk  274: my $ind=-1;
1.49      albertel  275: my %prog_state;
1.61      albertel  276: print "<a href=\"$backref\"><b>Return</b></a> to last resource.<br /><br />";
1.59      albertel  277: if ($adv) { %prog_state=&Apache::lonhtmlcommon::Create_PrgWin('','Print Status','Class Print Status',$number_of_files,'inline','80'); }
1.61      albertel  278: print "<br />";
1.88      foxr      279: my $num_files = @texfile;
1.43      sakharuk  280: foreach $texfile (@texfile) {
1.49      albertel  281:   my $status_statement='';
                    282:   my $link_text='download PDF';
                    283:   $ind++;
                    284:   my @stud_info=split(/_END_/,$names_pack[$ind]);
                    285:   my @tempo_array=split(/:/,$stud_info[0]);
                    286:   my $name;
1.88      foxr      287:   my $name_range='';
1.49      albertel  288:   if ($tempo_array[3]) {
                    289:       $name=$tempo_array[3];
1.89      foxr      290:       ($name_range) = split(/,/,$name, 2);
1.49      albertel  291:   } else {
                    292:       $name=$tempo_array[0].'@'.$tempo_array[1];
1.88      foxr      293:       $name_range = $tempo_array[0];
                    294:   }
                    295:   if (($name ne "") && ($name ne '@') ) { # Could be printing codes...
                    296:       $link_text='<b>'.$name.'</b>';
                    297:       $status_statement.=$name;
1.49      albertel  298:   }
                    299:   if ($#stud_info>0) {
                    300:       @tempo_array=split(/:/,$stud_info[-1]);
1.48      albertel  301:       if ($tempo_array[3]) {
                    302: 	  $name=$tempo_array[3];
1.89      foxr      303: 	  my ($lastname) = split(/,/, $name,2);
                    304: 	  $name_range .= "-".$lastname;
1.48      albertel  305:       } else {
                    306: 	  $name=$tempo_array[0].'@'.$tempo_array[1];
1.88      foxr      307: 	  $name_range .= '-'.$tempo_array[0];
                    308:       }
                    309:       if (($name ne "") && ($name ne '@')) {
                    310: 	  $link_text.=' - <b>'.$name.'</b>';
                    311: 	  $status_statement.=' -  '.$name;
                    312:   
1.48      albertel  313:       }
1.88      foxr      314:   }
                    315:   if(($num_files > 1) && ($link_text eq 'download PDF')) { # Printing codes
                    316:       $link_text = '<b>'.basename($texfile,'.tex').'.pdf</b>';
                    317:       $status_statement .= basename($texfile);
                    318:   }
                    319:   $name_range =~ s/'//g;	# O'Neil -> ONeil e.g.
                    320:   print "<br/>";
1.49      albertel  321:   if ($adv) { &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,'Creating PDF for '.$status_statement); }
1.88      foxr      322:   #  This little piece of dirt puts username ranges into the original tex
                    323:   #  Tex filename from which they'll propagate into the other filenames as well.
                    324:   #
1.29      sakharuk  325:   if (-e $texfile) {
1.88      foxr      326:       if (($name_range ne '') && ($num_files > 1)) {
                    327: 	  my $newtexfile = $texfile;
                    328: 	  $newtexfile    =~ s/\.tex/$name_range\.tex/;
                    329: 	  rename($texfile, $newtexfile);
                    330: 	  $texfile       = $newtexfile;
                    331:       }
1.29      sakharuk  332:       $texfile =~ m/^(.*)\/([^\/]+)$/; 
                    333:       my $name_file = $2;
                    334:       my $path_file = $1.'/';
                    335:       chdir $path_file;
1.88      foxr      336:       my $dvi_file= $name_file; $dvi_file =~ s/\.tex/$name_range\.dvi/;
1.59      albertel  337:       &busy_wait_command("latex $name_file 1>/dev/null 2>/dev/null",
                    338: 			 "for $status_statement now LaTeXing file",
                    339: 			 \%prog_state,$dvi_file);
1.40      sakharuk  340:       if ($tableofcontents eq 'yes') {
1.63      sakharuk  341:       &busy_wait_command("latex $name_file 1>/dev/null 2>/dev/null",
                    342: 			 "for $status_statement now LaTeXing file for table of contents",
                    343: 			 \%prog_state,$dvi_file);
1.40      sakharuk  344:       } #to create table of contents
1.33      sakharuk  345:       my $idxname=$name_file;
                    346:       $idxname=~s/\.tex$/\.idx/;
1.40      sakharuk  347:       if ($tableofindex eq 'yes') {
1.63      sakharuk  348: 	  &busy_wait_command("makeindex $idxname",
                    349: 			     "making index file",
                    350: 			     \%prog_state,$idxname);
                    351: 	  &busy_wait_command("latex $name_file 1>/dev/null 2>/dev/null",
                    352: 			     "for $status_statement now LaTeXing file for index section",
                    353: 			     \%prog_state,$dvi_file);
1.33      sakharuk  354:       } #to create index
1.29      sakharuk  355:       #Do we have a latex error in the log file?
1.67      sakharuk  356:       my $logfilename = $texfile; $logfilename =~ s/\.tex$/\.log/;
1.69      matthew   357:       my $temporary_file=IO::File->new($logfilename) || die "Couldn't open log file $logfilename for reading: $!\n";
1.29      sakharuk  358:       my @content_of_file = <$temporary_file>;
                    359:       close $temporary_file; 
1.30      sakharuk  360:       my $body_log_file = join(' ',@content_of_file);
                    361:       $logfilename =~ s/\.log$/\.html/;
                    362:       $temporary_file = IO::File->new('>'.$logfilename); 
                    363:       print $temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_log_file.'</pre></body></html>'."\n";
1.29      sakharuk  364:       if ($body_log_file=~m/!\s+Emergency stop/) {
                    365: 	  my $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART';
                    366: 	  my $whereitends = rindex $body_log_file,'STAMPOFPASSEDRESOURCEEND';
1.81      foxr      367: 	  my $badresource;
1.83      foxr      368: 	  my $badtext;
1.29      sakharuk  369: 	  if ($whereitbegins!=-1 and $whereitends!=-1) {
1.83      foxr      370: 	      $badtext = substr($body_log_file,$whereitbegins+26, $whereitends-$whereitbegins-26);
1.81      foxr      371: 	      $whereitbegins  = rindex $badtext,'located in';
                    372: 	      if ($whereitbegins != -1) {
                    373: 		  
                    374: 		  $badresource = substr($badtext, $whereitbegins+27, 
                    375: 					length($badtext) - $whereitbegins - 48);
1.84      albertel  376: 		  # print "<br />failing resourcename: $badresource<br />";
1.81      foxr      377: 	      }
1.29      sakharuk  378: 	  }
1.83      foxr      379: 	  
1.75      albertel  380:           if ($advanced_role) {  
1.83      foxr      381: 	      #LaTeX failed to parse tex file 
                    382: 	      print "<h2>LaTeX could not successfully parse your tex file.</h2>";
                    383: 	      print "It probably has errors in it.<br />";
                    384: 	      print "With very high probability this error occured in ".$badtext."<br /><br />";
1.84      albertel  385: 	      print "Here are the error messages in the LaTeX log file<br /><pre>";
1.90      foxr      386: 
1.83      foxr      387: 	      my $sygnal = 0;
                    388: 	      for (my $i=0;$i<=$#content_of_file;$i++) {
                    389: 		  if ($content_of_file[$i]=~m/^Runaway argument?/ or $content_of_file[$i]=~m/^!/) {
                    390: 		      $sygnal = 1;
                    391: 		  } 
                    392: 		  if ($content_of_file[$i]=~m/Here is how much of/) {
                    393: 		      $sygnal = 0;
                    394: 		  } 
                    395: 		  if ($sygnal) {
                    396: 		      print "$content_of_file[$i]";
                    397: 		  }  
                    398: 	      }
                    399: 	      print "</pre>\n";
1.84      albertel  400: 	      # print "<br /> Advanced role <br />";
1.35      sakharuk  401:               print "<b><big>The link to ";
                    402:               $logfilename=~s/\/home\/httpd//;
                    403: 	      print "<a href=\"$logfilename\">Your log file </a></big></b>";
                    404: 	      print "\n";
                    405:               #link tooriginal LaTeX file (included according Michael Hamlin desire)
1.69      matthew   406: 	      my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open tex file $texfile for reading: $!\n";
1.35      sakharuk  407: 	      my @tex_content_of_file = <$tex_temporary_file>;
                    408: 	      close $tex_temporary_file; 
                    409: 	      my $body_tex_file = join(' ',@tex_content_of_file);
                    410: 	      $texfile =~ s/\.tex$/aaaaa\.html/;
                    411: 	      $tex_temporary_file = IO::File->new('>'.$texfile); 
                    412: 	      print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
                    413: 	      print "<br /><br />";
                    414: 	      print "<b><big>The link to ";
                    415: 	      $texfile=~s/\/home\/httpd//;
                    416: 	      print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
                    417: 	      print "\n";
1.90      foxr      418: 	      my $help_text = &Apache::loncommon::help_open_topic("Print_Resource", "Help on printing");
                    419: 	      print ("$help_text");
                    420: 
1.77      foxr      421: 	  } else {		# Student role...
                    422: 	      #  at this point:
                    423: 	      #    $body_log_file - contains the log file.
                    424:               #    $name_file     - is the name of the LaTeX file.
                    425:               #    $identifier    - is the unique LaTeX identifier.l
                    426: 
1.84      albertel  427: 	      print "<br />There are errors in $badtext";
                    428: 	      print "<br />These errors prevent this resource from printing correctly";
1.77      foxr      429: 	      my $tex_handle = IO::File->new($name_file);
                    430: 	      my @tex_contents = <$tex_handle>;
1.81      foxr      431: 	      &send_error_mail($identifier, $badresource, $body_log_file, \@tex_contents);
1.90      foxr      432: 	      print "<br />A message has been sent to the instructor describing this failure<br />";
                    433: 	      my $help_text = &Apache::loncommon::help_open_topic("Print_Resource", "Help on printing");
                    434: 	      print  ("$help_text");
                    435: 
1.36      sakharuk  436: 	  }
1.35      sakharuk  437: 
1.29      sakharuk  438:       } elsif ($body_log_file=~m/<inserted text>/) {
                    439: 	  my $whereitbegins = index $body_log_file,'<inserted text>';
1.59      albertel  440: 	  print "You are running LaTeX in <b>batch mode</b>.";
1.30      sakharuk  441: 	  while ($whereitbegins != -1) {
                    442: 	      my $tempobegin=$whereitbegins;
                    443: 	      $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART',$whereitbegins;
                    444: 	      my $whereitends = index $body_log_file,'STAMPOFPASSEDRESOURCEEND',$whereitbegins;
1.34      sakharuk  445: 	      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  446: 	      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";
                    447: 	      $whereitbegins = index $body_log_file,'<inserted text>',$tempobegin+10;
                    448: 	  }
1.29      sakharuk  449: 	  $name_file =~ s/\.tex/\.dvi/;
                    450: 	  my $new_name_file = $name_file;
                    451: 	  $new_name_file =~ s/\.dvi/\.ps/;
1.67      sakharuk  452: 	  my $papera=$paper;
1.62      sakharuk  453: 	  if ($papera eq 'letter') {$papera='';}
                    454: 	  if ($papera ne '') {$papera='-t'.$papera;}
                    455: 	  my $comma = "dvips $papera -Ppdf -G0 -o $new_name_file";
1.59      albertel  456: 	  &busy_wait_command("$comma $name_file 1>/dev/null 2>/dev/null",
                    457: 			     "for $status_statement now Converting to PS",
                    458: 			     \%prog_state,$new_name_file);
1.29      sakharuk  459: 	  if (-e $new_name_file) {
                    460: 	      print "<h1>PDF output file (see link below)</h1>\n";
                    461: 	      $new_name_file =~ m/^(.*)\./;
1.59      albertel  462: 	      my $ps_file = my $tempo_file = $1.'temporar.ps';
                    463: 	      my $pdf_file = $1.'.pdf';
1.29      sakharuk  464: 	      if ($laystyle eq 'album' and $numberofcolumns eq '2') {
1.82      albertel  465: 		  my $papera=$paper;
                    466:                   if ($papera eq 'letter') {$papera='';}
                    467: 		  if ($papera ne '') {$papera='-p'.$papera;}
                    468: 		  $comma = "psnup $papera -2 -s1.0 $new_name_file";
1.59      albertel  469: 		  &busy_wait_command("$comma $tempo_file 1>/dev/null 2>/dev/null",
                    470: 				     "for $status_statement now Modifying PS layout",
                    471: 				     \%prog_state,$tempo_file); 
1.29      sakharuk  472: 	      } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
1.67      sakharuk  473: 		  my $papera=$paper;
                    474:                   if ($papera eq 'letter') {$papera='';}
1.62      sakharuk  475: 		  if ($papera ne '') {$papera='-p'.$papera;}
                    476: 		  $comma = 'pstops '.$papera.' "2:0+1(0.48w,0)"';
1.63      sakharuk  477: 		  &busy_wait_command("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null",
                    478: 				     "for $status_statement now Modifying PS layout",
                    479: 				     \%prog_state,$tempo_file); 
                    480: 
1.29      sakharuk  481: 	      } else {
1.59      albertel  482: 		  $ps_file=$new_name_file;
1.29      sakharuk  483: 	      }	    
1.59      albertel  484: 	      &busy_wait_command("ps2pdf $ps_file $pdf_file 1>/dev/null 2>/dev/null",
                    485: 				 "for $status_statement now Converting PS to PDF",
                    486: 				 \%prog_state, $pdf_file);
                    487: 
1.29      sakharuk  488: 	      my $texlog = $texfile;
                    489: 	      my $texaux = $texfile;
                    490: 	      my $texdvi = $texfile;
                    491: 	      my $texps = $texfile;
                    492: 	      $texlog =~ s/\.tex/\.log/;
                    493: 	      $texaux =~ s/\.tex/\.aux/;
                    494: 	      $texdvi =~ s/\.tex/\.dvi/;
                    495: 	      $texps =~ s/\.tex/\.ps/;
1.30      sakharuk  496: 	      my @garb = ($texaux,$texdvi,$texps);
1.29      sakharuk  497: #	  unlink @garb;
                    498: 	      unlink $duefile;
1.59      albertel  499: 	      print "<a href=\"/prtspool/$pdf_file\">Your PDF document</a>";
1.75      albertel  500: 	  }
                    501: 	  if ($advanced_role) {  
                    502: 	      print "<br /><br />";
                    503: 	      print "<b><big>The link to ";
                    504: 	      $logfilename=~s/\/home\/httpd//;
                    505: 	      print "<a href=\"$logfilename\">Your log file </a></big></b>";
                    506: 	      print "\n";
                    507: 	      #link tooriginal LaTeX file (included according Michael Hamlin desire)
                    508: 	      my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open tex file $texfile for reading: $!\n";
                    509: 	      my @tex_content_of_file = <$tex_temporary_file>;
                    510: 	      close $tex_temporary_file; 
                    511: 	      my $body_tex_file = join(' ',@tex_content_of_file);
                    512: 	      $texfile =~ s/\.tex$/aaaaa\.html/;
                    513: 	      $tex_temporary_file = IO::File->new('>'.$texfile); 
                    514: 	      print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
                    515: 	      print "<br /><br />";
                    516: 	      print "<b><big>The link to ";
                    517: 	      $texfile=~s/\/home\/httpd//;
                    518: 	      print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
                    519: 	      print "\n";
1.29      sakharuk  520: 	  }
                    521:       } else {
                    522: 	  #LaTeX successfully parsed tex file 
                    523: 	  $name_file =~ s/\.tex/\.dvi/;
                    524: 	  my $new_name_file = $name_file;
                    525: 	  $new_name_file =~ s/\.dvi/\.ps/;
1.67      sakharuk  526: 	  my $papera=$paper;
1.62      sakharuk  527: 	  if ($papera eq 'letter') {$papera='';}
                    528: 	  if ($papera ne '') {$papera='-t'.$papera;}
                    529: 	  my $comma = "dvips $papera -Ppdf -G0 -o $new_name_file";
1.59      albertel  530: 	  &busy_wait_command("$comma $name_file 1>/dev/null 2>/dev/null",
                    531: 			     "for $status_statement now Converting to PS",
                    532: 			     \%prog_state,$new_name_file);
1.29      sakharuk  533: 	  if (-e $new_name_file) {
1.61      albertel  534: 	      print "<br />";
1.29      sakharuk  535: 	      $new_name_file =~ m/^(.*)\./;
1.59      albertel  536: 	      my $ps_file = my $tempo_file = $1.'temporar.ps';
                    537: 	      my $pdf_file = $1.'.pdf';
1.82      albertel  538: 	      $papera=~s/t/p/;
1.29      sakharuk  539: 	      if ($laystyle eq 'album' and $numberofcolumns eq '2') {
1.82      albertel  540: 		  $comma = "psnup $papera -2 -s1.0 $new_name_file";
1.59      albertel  541: 		  &busy_wait_command("$comma $tempo_file 1>/dev/null 2>/dev/null",
                    542: 				     "for $status_statement now Modifying PS layout",
                    543: 				     \%prog_state,$tempo_file);
1.29      sakharuk  544: 	      } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
1.65      sakharuk  545: 		  $comma = 'pstops '.$papera.' "2:0+1(0.48w,0)"';
1.59      albertel  546: 		  &busy_wait_command("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null",
                    547: 				     "for $status_statement now Modifying PS layout",
                    548: 				     \%prog_state,$tempo_file); 
1.29      sakharuk  549: 	      } else {
1.59      albertel  550: 		  $ps_file=$new_name_file;
                    551: 	      }
1.67      sakharuk  552: 	      my $addtoPSfile={'legal'=>'<< /PageSize [612 1008] >> setpagedevice',
                    553:                                'tabloid'=>'<< /PageSize [792 1224] >> setpagedevice',
                    554:                                'executive'=>,'<< /PageSize [540 720] >> setpagedevice',
                    555:                                'a2'=>'<< /PageSize [1195.02 1690.09] >> setpagedevice',
                    556:                                'a3'=>'<< /PageSize [842 1195.02] >> setpagedevice',
                    557:                                'a4'=>'<< /PageSize [595.2 842] >> setpagedevice',
                    558:                                'a5'=>'<< /PageSize [421.1 595.2] >> setpagedevice',
                    559:                                'a6'=>'<< /PageSize [298.75 421.1] >> setpagedevice',
                    560: 			   };
                    561: 	      if ($paper ne 'letter') {
1.69      matthew   562: 		  open(FFH,'<',$ps_file) || die "Couldn't open ps file $ps_file for reading: $!\n";
1.63      sakharuk  563: 		  my $new_ps_file='new'.$ps_file;
1.69      matthew   564: 		  open(FFHS,'>',$new_ps_file) || die "Couldn't open new ps file $new_ps_file for reading: $!\n";
1.67      sakharuk  565: 		  print FFHS $addtoPSfile->{$paper}."\n";
1.63      sakharuk  566: 		  while (<FFH>) {
                    567: 		      print FFHS $_;
                    568: 		  }
1.62      sakharuk  569: 		  close(FFH);
1.63      sakharuk  570: 		  close(FFHS);
                    571: 		  $ps_file=$new_ps_file;	  
1.62      sakharuk  572: 	      }
1.59      albertel  573: 	      &busy_wait_command("ps2pdf $ps_file $pdf_file 1>/dev/null 2>/dev/null",
                    574: 				 "for $status_statement now Converting PS to PDF",
                    575: 				 \%prog_state,$pdf_file);
                    576: 	    
1.29      sakharuk  577: 	      my $texlog = $texfile;
                    578: 	      my $texaux = $texfile;
                    579: 	      my $texdvi = $texfile;
                    580: 	      my $texps = $texfile;
                    581: 	      $texlog =~ s/\.tex/\.log/;
                    582: 	      $texaux =~ s/\.tex/\.aux/;
                    583: 	      $texdvi =~ s/\.tex/\.dvi/;
                    584: 	      $texps =~ s/\.tex/\.ps/;
                    585: 	      my @garb = ($texlog,$texaux,$texdvi,$texps);
1.22      sakharuk  586: #	  unlink @garb;
1.29      sakharuk  587: 	      unlink $duefile;
1.68      sakharuk  588: 	      print "<a href=\"/prtspool/$pdf_file\">$link_text - click here to download pdf</a>";
1.29      sakharuk  589: 	      print "\n";
                    590: 	  }
1.63      sakharuk  591:       }  
1.29      sakharuk  592:   } else {
                    593:       print "LaTeX file $texfile was not created successfully";
1.14      sakharuk  594:   }
1.43      sakharuk  595: }
1.45      sakharuk  596: print "<br />";
1.44      sakharuk  597: if ($number_of_files>1) {
1.45      sakharuk  598:     my $zipfile=$texfile[0];
                    599:     $zipfile=~s/\.tex/\.zip/;
                    600:     my $statement="zip $zipfile";
1.44      sakharuk  601:     foreach my $file (@texfile) {
1.45      sakharuk  602: 	$file=~s/\.tex/.\pdf/;
                    603: 	$statement.=' '.$file; 
1.44      sakharuk  604:     }
1.49      albertel  605:     print("<pre>Zip Output:\n");
                    606:     system($statement);
                    607:     print("</pre>");
1.45      sakharuk  608:     $zipfile=~s/\/home\/httpd//;
1.49      albertel  609:     print "<br /> A <a href=\"$zipfile\">ZIP file</a> of all the PDFs.";
1.44      sakharuk  610: }
1.49      albertel  611: if ($adv) { &Apache::lonhtmlcommon::Close_PrgWin('',\%prog_state); }
1.1       sakharuk  612: 
1.59      albertel  613: my $done;
                    614: sub REAPER {
                    615:     $done=1;
                    616: }
                    617: 
                    618: sub busy_wait_command {
                    619:     my ($command,$message,$progress_win,$output_file)=@_;
                    620:     
                    621:     $SIG{CHLD} = \&REAPER;
                    622:     $done=0;
                    623:     my $pid=open(CMD,"$command |");
1.60      albertel  624:     if ($adv) {
                    625: 	&Apache::lonhtmlcommon::Update_PrgWin('',$progress_win,$message);
                    626:     }
1.59      albertel  627:     while(!$done) {
                    628: 	sleep 1;
                    629: 	my $extra_msg;
                    630: 	if ($output_file) {
                    631: 	    my $size=(stat($output_file))[7];
                    632: 	    $extra_msg=", $size bytes generated";
                    633: 	}
1.60      albertel  634: 	if ($adv) {
                    635: 	    &Apache::lonhtmlcommon::Update_PrgWin('',$progress_win,
                    636: 						  $message.$extra_msg);
                    637: 	}
1.59      albertel  638:     }
                    639:     $SIG{CHLD}='IGNORE';
                    640:     close(CMD);
                    641: }
1.1       sakharuk  642: 
                    643: 
                    644: 
1.4       sakharuk  645: 

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