File:  [LON-CAPA] / loncom / interface / lonprintout.pm
Revision 1.287: download - view: text, annotated - select for diffs
Wed Mar 24 22:22:04 2004 UTC (20 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- add the ability to have .pages summarized
- Make it so that you can choose to print a page or not print a page, don't allow people to pick and choose resources gfrom inside of a .page
- BUG#2851,

    1: # The LearningOnline Network
    2: # Printout
    3: #
    4: # $Id: lonprintout.pm,v 1.287 2004/03/24 22:22:04 albertel Exp $
    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: #
   28: #
   29: package Apache::lonprintout;
   30: 
   31: use strict;
   32: use Apache::Constants qw(:common :http);
   33: use Apache::lonxml;
   34: use Apache::lonnet;
   35: use Apache::loncommon;
   36: use Apache::inputtags;
   37: use Apache::grades;
   38: use Apache::edit;
   39: use Apache::File();
   40: use Apache::lonnavmaps;
   41: use Apache::lonratedt;
   42: use POSIX qw(strftime);
   43: use Apache::lonlocal;
   44: use GDBM_File;
   45: 
   46: 
   47: my %hash;
   48: my $LaTeXwidth = 0;
   49: 
   50: 
   51: sub latex_header_footer_remove {
   52:     my $text = shift;
   53:     $text =~ s/\\end{document}//;
   54:     $text =~ s/\\documentclass([^&]*)\\begin{document}//;
   55:     return $text;
   56: }
   57: 
   58: 
   59: sub character_chart {
   60:     my $result = shift;	
   61:     $result =~ s/&\#0?0?(7|9);//g;
   62:     $result =~ s/&\#0?(10|13);//g;
   63:     $result =~ s/&\#0?32;/ /g;
   64:     $result =~ s/&\#0?33;/!/g;
   65:     $result =~ s/&(\#0?34|quot);/\"/g;
   66:     $result =~ s/&\#0?35;/\\\#/g;
   67:     $result =~ s/&\#0?36;/\\\$/g;
   68:     $result =~ s/&\#0?37;/\\%/g; 
   69:     $result =~ s/&(\#0?38|amp);/\\&/g; 
   70:     $result =~ s/&\#(0?39|146);/\'/g;
   71:     $result =~ s/&\#0?40;/(/g;
   72:     $result =~ s/&\#0?41;/)/g;
   73:     $result =~ s/&\#0?42;/\*/g;
   74:     $result =~ s/&\#0?43;/\+/g;
   75:     $result =~ s/&\#(0?44|130);/,/g;
   76:     $result =~ s/&\#0?45;/-/g;
   77:     $result =~ s/&\#0?46;/\./g;
   78:     $result =~ s/&\#0?47;/\//g;
   79:     $result =~ s/&\#0?48;/0/g;
   80:     $result =~ s/&\#0?49;/1/g;
   81:     $result =~ s/&\#0?50;/2/g;
   82:     $result =~ s/&\#0?51;/3/g;
   83:     $result =~ s/&\#0?52;/4/g;
   84:     $result =~ s/&\#0?53;/5/g;
   85:     $result =~ s/&\#0?54;/6/g;
   86:     $result =~ s/&\#0?55;/7/g;
   87:     $result =~ s/&\#0?56;/8/g;
   88:     $result =~ s/&\#0?57;/9/g;
   89:     $result =~ s/&\#0?58;/:/g;
   90:     $result =~ s/&\#0?59;/;/g;
   91:     $result =~ s/&(\#0?60|lt|\#139);/\$<\$/g;
   92:     $result =~ s/&\#0?61;/\\ensuremath\{=\}/g;
   93:     $result =~ s/&(\#0?62|gt|\#155);/\\ensuremath\{>\}/g;
   94:     $result =~ s/&\#0?63;/\?/g;
   95:     $result =~ s/&\#0?65;/A/g;
   96:     $result =~ s/&\#0?66;/B/g;
   97:     $result =~ s/&\#0?67;/C/g;
   98:     $result =~ s/&\#0?68;/D/g;
   99:     $result =~ s/&\#0?69;/E/g;
  100:     $result =~ s/&\#0?70;/F/g;
  101:     $result =~ s/&\#0?71;/G/g;
  102:     $result =~ s/&\#0?72;/H/g;
  103:     $result =~ s/&\#0?73;/I/g;
  104:     $result =~ s/&\#0?74;/J/g;
  105:     $result =~ s/&\#0?75;/K/g;
  106:     $result =~ s/&\#0?76;/L/g;
  107:     $result =~ s/&\#0?77;/M/g;
  108:     $result =~ s/&\#0?78;/N/g;
  109:     $result =~ s/&\#0?79;/O/g;
  110:     $result =~ s/&\#0?80;/P/g;
  111:     $result =~ s/&\#0?81;/Q/g;
  112:     $result =~ s/&\#0?82;/R/g;
  113:     $result =~ s/&\#0?83;/S/g;
  114:     $result =~ s/&\#0?84;/T/g;
  115:     $result =~ s/&\#0?85;/U/g;
  116:     $result =~ s/&\#0?86;/V/g;
  117:     $result =~ s/&\#0?87;/W/g;
  118:     $result =~ s/&\#0?88;/X/g;
  119:     $result =~ s/&\#0?89;/Y/g;
  120:     $result =~ s/&\#0?90;/Z/g;
  121:     $result =~ s/&\#0?91;/[/g;
  122:     $result =~ s/&\#0?92;/\\ensuremath\{\\setminus\}/g;
  123:     $result =~ s/&\#0?93;/]/g;
  124:     $result =~ s/&\#(0?94|136);/\\ensuremath\{\\wedge\}/g;
  125:     $result =~ s/&\#(0?95|138|154);/\\underline{\\makebox[2mm]{\\strut}}/g;
  126:     $result =~ s/&\#(0?96|145);/\`/g;
  127:     $result =~ s/&\#0?97;/a/g;
  128:     $result =~ s/&\#0?98;/b/g;
  129:     $result =~ s/&\#0?99;/c/g;
  130:     $result =~ s/&\#100;/d/g;
  131:     $result =~ s/&\#101;/e/g;
  132:     $result =~ s/&\#102;/f/g;
  133:     $result =~ s/&\#103;/g/g;
  134:     $result =~ s/&\#104;/h/g;
  135:     $result =~ s/&\#105;/i/g;
  136:     $result =~ s/&\#106;/j/g;
  137:     $result =~ s/&\#107;/k/g;
  138:     $result =~ s/&\#108;/l/g;
  139:     $result =~ s/&\#109;/m/g;
  140:     $result =~ s/&\#110;/n/g;
  141:     $result =~ s/&\#111;/o/g;
  142:     $result =~ s/&\#112;/p/g;
  143:     $result =~ s/&\#113;/q/g;
  144:     $result =~ s/&\#114;/r/g;
  145:     $result =~ s/&\#115;/s/g;
  146:     $result =~ s/&\#116;/t/g;
  147:     $result =~ s/&\#117;/u/g;
  148:     $result =~ s/&\#118;/v/g;
  149:     $result =~ s/&\#119;/w/g;
  150:     $result =~ s/&\#120;/x/g;
  151:     $result =~ s/&\#121;/y/g;
  152:     $result =~ s/&\#122;/z/g;
  153:     $result =~ s/&\#123;/\\{/g;
  154:     $result =~ s/&\#124;/\|/g;
  155:     $result =~ s/&\#125;/\\}/g;
  156:     $result =~ s/&\#126;/\~/g;
  157:     $result =~ s/&\#131;/\\textflorin /g;
  158:     $result =~ s/&\#132;/\"/g;
  159:     $result =~ s/&\#133;/\\ensuremath\{\\ldots\}/g;
  160:     $result =~ s/&\#134;/\\ensuremath\{\\dagger\}/g;
  161:     $result =~ s/&\#135;/\\ensuremath\{\\ddagger\}/g;
  162:     $result =~ s/&\#137;/\\textperthousand /g;
  163:     $result =~ s/&\#140;/{\\OE}/g;
  164:     $result =~ s/&\#147;/\`\`/g;
  165:     $result =~ s/&\#148;/\'\'/g;
  166:     $result =~ s/&\#149;/\\ensuremath\{\\bullet\}/g;
  167:     $result =~ s/&\#150;/--/g;
  168:     $result =~ s/&\#151;/---/g;
  169:     $result =~ s/&\#152;/\\ensuremath\{\\sim\}/g;
  170:     $result =~ s/&\#153;/\\texttrademark /g;
  171:     $result =~ s/&\#156;/\\oe/g;
  172:     $result =~ s/&\#159;/\\\"Y/g;
  173:     $result =~ s/&(\#160|nbsp);/~/g;
  174:     $result =~ s/&(\#161|iexcl);/!\`/g;
  175:     $result =~ s/&(\#162|cent);/\\textcent /g;
  176:     $result =~ s/&(\#163|pound);/\\pounds /g; 
  177:     $result =~ s/&(\#164|curren);/\\textcurrency /g;
  178:     $result =~ s/&(\#165|yen);/\\textyen /g;
  179:     $result =~ s/&(\#166|brvbar);/\\textbrokenbar /g;
  180:     $result =~ s/&(\#167|sect);/\\textsection /g;
  181:     $result =~ s/&(\#168|uml);/\\texthighdieresis /g;
  182:     $result =~ s/&(\#169|copy);/\\copyright /g;
  183:     $result =~ s/&(\#170|ordf);/\\textordfeminine /g;
  184:     $result =~ s/&(\#172|not);/\\ensuremath\{\\neg\}/g;
  185:     $result =~ s/&(\#173|shy);/ - /g;
  186:     $result =~ s/&(\#174|reg);/\\textregistered /g;
  187:     $result =~ s/&(\#175|macr);/\\ensuremath\{^{-}\}/g;
  188:     $result =~ s/&(\#176|deg);/\\ensuremath\{^{\\circ}\}/g;
  189:     $result =~ s/&(\#177|plusmn);/\\ensuremath\{\\pm\}/g;
  190:     $result =~ s/&(\#178|sup2);/\\ensuremath\{^2\}/g;
  191:     $result =~ s/&(\#179|sup3);/\\ensuremath\{^3\}/g;
  192:     $result =~ s/&(\#180|acute);/\\textacute /g;
  193:     $result =~ s/&(\#181|micro);/\\ensuremath\{\\mu\}/g;
  194:     $result =~ s/&(\#182|para);/\\P/g;
  195:     $result =~ s/&(\#183|middot);/\\ensuremath\{\\cdot\}/g;
  196:     $result =~ s/&(\#184|cedil);/\\c{\\strut}/g;
  197:     $result =~ s/&(\#185|sup1);/\\ensuremath\{^1\}/g;
  198:     $result =~ s/&(\#186|ordm);/\\textordmasculine /g;
  199:     $result =~ s/&(\#188|frac14);/\\textonequarter /g;
  200:     $result =~ s/&(\#189|frac12);/\\textonehalf /g;
  201:     $result =~ s/&(\#190|frac34);/\\textthreequarters /g;
  202:     $result =~ s/&(\#191|iquest);/?\`/g;   
  203:     $result =~ s/&(\#192|Agrave);/\\\`{A}/g;  
  204:     $result =~ s/&(\#193|Aacute);/\\\'{A}/g; 
  205:     $result =~ s/&(\#194|Acirc);/\\^{A}/g;
  206:     $result =~ s/&(\#195|Atilde);/\\~{A}/g;
  207:     $result =~ s/&(\#196|Auml);/\\\"{A}/g; 
  208:     $result =~ s/&(\#197|Aring);/{\\AA}/g;
  209:     $result =~ s/&(\#198|AElig);/{\\AE}/g;
  210:     $result =~ s/&(\#199|Ccedil);/\\c{c}/g;
  211:     $result =~ s/&(\#200|Egrave);/\\\`{E}/g;  
  212:     $result =~ s/&(\#201|Eacute);/\\\'{E}/g;    
  213:     $result =~ s/&(\#202|Ecirc);/\\^{E}/g;
  214:     $result =~ s/&(\#203|Euml);/\\\"{E}/g;
  215:     $result =~ s/&(\#204|Igrave);/\\\`{I}/g;
  216:     $result =~ s/&(\#205|Iacute);/\\\'{I}/g;    
  217:     $result =~ s/&(\#206|Icirc);/\\^{I}/g;
  218:     $result =~ s/&(\#207|Iuml);/\\\"{I}/g;    
  219:     $result =~ s/&(\#209|Ntilde);/\\~{N}/g;
  220:     $result =~ s/&(\#210|Ograve);/\\\`{O}/g;
  221:     $result =~ s/&(\#211|Oacute);/\\\'{O}/g;
  222:     $result =~ s/&(\#212|Ocirc);/\\^{O}/g;
  223:     $result =~ s/&(\#213|Otilde);/\\~{O}/g;
  224:     $result =~ s/&(\#214|Ouml);/\\\"{O}/g;    
  225:     $result =~ s/&(\#215|times);/\\ensuremath\{\\times\}/g;
  226:     $result =~ s/&(\#216|Oslash);/{\\O}/g;
  227:     $result =~ s/&(\#217|Ugrave);/\\\`{U}/g;    
  228:     $result =~ s/&(\#218|Uacute);/\\\'{U}/g;
  229:     $result =~ s/&(\#219|Ucirc);/\\^{U}/g;
  230:     $result =~ s/&(\#220|Uuml);/\\\"{U}/g;
  231:     $result =~ s/&(\#221|Yacute);/\\\'{Y}/g;
  232:     $result =~ s/&(\#223|szlig);/\\ss/g;
  233:     $result =~ s/&(\#224|agrave);/\\\`{a}/g;
  234:     $result =~ s/&(\#225|aacute);/\\\'{a}/g;
  235:     $result =~ s/&(\#226|acirc);/\\^{a}/g;
  236:     $result =~ s/&(\#227|atilde);/\\~{a}/g;
  237:     $result =~ s/&(\#228|auml);/\\\"{a}/g;
  238:     $result =~ s/&(\#229|aring);/{\\aa}/g;
  239:     $result =~ s/&(\#230|aelig);/{\\ae}/g;
  240:     $result =~ s/&(\#231|ccedil);/\\c{c}/g;
  241:     $result =~ s/&(\#232|egrave);/\\\`{e}/g;
  242:     $result =~ s/&(\#233|eacute);/\\\'{e}/g;
  243:     $result =~ s/&(\#234|ecirc);/\\^{e}/g;
  244:     $result =~ s/&(\#235|euml);/\\\"{e}/g;
  245:     $result =~ s/&(\#236|igrave);/\\\`{i}/g;
  246:     $result =~ s/&(\#237|iacute);/\\\'{i}/g;
  247:     $result =~ s/&(\#238|icirc);/\\^{i}/g;
  248:     $result =~ s/&(\#239|iuml);/\\\"{i}/g;
  249:     $result =~ s/&(\#240|eth);/\\ensuremath\{\\partial\}/g;
  250:     $result =~ s/&(\#241|ntilde);/\\~{n}/g;
  251:     $result =~ s/&(\#242|ograve);/\\\`{o}/g;
  252:     $result =~ s/&(\#243|oacute);/\\\'{o}/g;
  253:     $result =~ s/&(\#244|ocirc);/\\^{o}/g;
  254:     $result =~ s/&(\#245|otilde);/\\~{o}/g;
  255:     $result =~ s/&(\#246|ouml);/\\\"{o}/g;
  256:     $result =~ s/&(\#247|divide);/\\ensuremath\{\\div\}/g;
  257:     $result =~ s/&(\#248|oslash);/{\\o}/g;
  258:     $result =~ s/&(\#249|ugrave);/\\\`{u}/g; 
  259:     $result =~ s/&(\#250|uacute);/\\\'{u}/g;
  260:     $result =~ s/&(\#251|ucirc);/\\^{u}/g;
  261:     $result =~ s/&(\#252|uuml);/\\\"{u}/g;
  262:     $result =~ s/&(\#253|yacute);/\\\'{y}/g;
  263:     $result =~ s/&(\#255|yuml);/\\\"{y}/g;
  264:     $result =~ s/&\#952;/\\ensuremath\{\\theta\}/g;
  265: #Greek Alphabet
  266:     $result =~ s/&(alpha|\#945);/\\ensuremath\{\\alpha\}/g;
  267:     $result =~ s/&(beta|\#946);/\\ensuremath\{\\beta\}/g;
  268:     $result =~ s/&(gamma|\#947);/\\ensuremath\{\\gamma\}/g;
  269:     $result =~ s/&(delta|\#948);/\\ensuremath\{\\delta\}/g;
  270:     $result =~ s/&(epsilon|\#949);/\\ensuremath\{\\epsilon\}/g;
  271:     $result =~ s/&(zeta|\#950);/\\ensuremath\{\\zeta\}/g;
  272:     $result =~ s/&(eta|\#951);/\\ensuremath\{\\eta\}/g;
  273:     $result =~ s/&(theta|\#952);/\\ensuremath\{\\theta\}/g;
  274:     $result =~ s/&(iota|\#953);/\\ensuremath\{\\iota\}/g;
  275:     $result =~ s/&(kappa|\#954);/\\ensuremath\{\\kappa\}/g;
  276:     $result =~ s/&(lambda|\#955);/\\ensuremath\{\\lambda\}/g;
  277:     $result =~ s/&(mu|\#956);/\\ensuremath\{\\mu\}/g;
  278:     $result =~ s/&(nu|\#957);/\\ensuremath\{\\nu\}/g;
  279:     $result =~ s/&(xi|\#958);/\\ensuremath\{\\xi\}/g;
  280:     $result =~ s/&(omicron|\#959);/o/g;
  281:     $result =~ s/&(pi|\#960);/\\ensuremath\{\\pi\}/g;
  282:     $result =~ s/&(rho|\#961);/\\ensuremath\{\\rho\}/g;
  283:     $result =~ s/&(sigma|\#963);/\\ensuremath\{\\sigma\}/g;
  284:     $result =~ s/&(tau|\#964);/\\ensuremath\{\\tau\}/g;
  285:     $result =~ s/&(upsilon|\#965);/\\ensuremath\{\\upsilon\}/g;
  286:     $result =~ s/&(phi|\#966);/\\ensuremath\{\\phi\}/g;
  287:     $result =~ s/&(chi|\#967);/\\ensuremath\{\\chi\}/g;
  288:     $result =~ s/&(psi|\#968);/\\ensuremath\{\\psi\}/g;
  289:     $result =~ s/&(omega|\#969);/\\ensuremath\{\\omega\}/g;
  290:     $result =~ s/&(thetasym|\#977);/\\ensuremath\{\\vartheta\}/g;
  291:     $result =~ s/&(piv|\#982);/\\ensuremath\{\\varpi\}/g;
  292:     $result =~ s/&(Alpha|\#913);/A/g;
  293:     $result =~ s/&(Beta|\#914);/B/g;
  294:     $result =~ s/&(Gamma|\#915);/\\ensuremath\{\\Gamma\}/g;
  295:     $result =~ s/&(Delta|\#916);/\\ensuremath\{\\Delta\}/g;
  296:     $result =~ s/&(Epsilon|\#917);/E/g;
  297:     $result =~ s/&(Zeta|\#918);/Z/g;
  298:     $result =~ s/&(Eta|\#919);/H/g;
  299:     $result =~ s/&(Theta|\#920);/\\ensuremath\{\\Theta\}/g;
  300:     $result =~ s/&(Iota|\#921);/I/g;
  301:     $result =~ s/&(Kappa|\#922);/K/g;
  302:     $result =~ s/&(Lambda|\#923);/\\ensuremath\{\\Lambda\}/g;
  303:     $result =~ s/&(Mu|\#924);/M/g;
  304:     $result =~ s/&(Nu|\#925);/N/g;
  305:     $result =~ s/&(Xi|\#926);/\\ensuremath\{\\Xi\}/g;
  306:     $result =~ s/&(Omicron|\#927);/O/g;
  307:     $result =~ s/&(Pi|\#928);/\\ensuremath\{\\Pi\}/g;
  308:     $result =~ s/&(Rho|\#929);/P/g;
  309:     $result =~ s/&(Sigma|\#931);/\\ensuremath\{\\Sigma\}/g;
  310:     $result =~ s/&(Tau|\#932);/T/g;
  311:     $result =~ s/&(Upsilon|\#933);/\\ensuremath\{\\Upsilon\}/g;
  312:     $result =~ s/&(Phi|\#934);/\\ensuremath\{\\Phi\}/g;
  313:     $result =~ s/&(Chi|\#935);/X/g;
  314:     $result =~ s/&(Psi|\#936);/\\ensuremath\{\\Psi\}/g;
  315:     $result =~ s/&(Omega|\#937);/\\ensuremath\{\\Omega\}/g;
  316: #Arrows (extended HTML 4.01)
  317:     $result =~ s/&(larr|\#8592);/\\ensuremath\{\\leftarrow\}/g;
  318:     $result =~ s/&(uarr|\#8593);/\\ensuremath\{\\uparrow\}/g;
  319:     $result =~ s/&(rarr|\#8594);/\\ensuremath\{\\rightarrow\}/g;
  320:     $result =~ s/&(darr|\#8595);/\\ensuremath\{\\downarrow\}/g;
  321:     $result =~ s/&(harr|\#8596);/\\ensuremath\{\\leftrightarrow\}/g;
  322:     $result =~ s/&(lArr|\#8656);/\\ensuremath\{\\Leftarrow\}/g;
  323:     $result =~ s/&(uArr|\#8657);/\\ensuremath\{\\Uparrow\}/g;
  324:     $result =~ s/&(rArr|\#8658);/\\ensuremath\{\\Rightarrow\}/g;
  325:     $result =~ s/&(dArr|\#8659);/\\ensuremath\{\\Downarrow\}/g;
  326:     $result =~ s/&(hArr|\#8660);/\\ensuremath\{\\Leftrightarrow\}/g;
  327: #Mathematical Operators (extended HTML 4.01)
  328:     $result =~ s/&(forall|\#8704);/\\ensuremath\{\\forall\}/g;
  329:     $result =~ s/&(part|\#8706);/\\ensuremath\{\\partial\}/g;
  330:     $result =~ s/&(exist|\#8707);/\\ensuremath\{\\exists\}/g;
  331:     $result =~ s/&(empty|\#8709);/\\ensuremath\{\\emptyset\}/g;
  332:     $result =~ s/&(nabla|\#8711);/\\ensuremath\{\\nabla\}/g;
  333:     $result =~ s/&(isin|\#8712);/\\ensuremath\{\\in\}/g;
  334:     $result =~ s/&(notin|\#8713);/\\ensuremath\{\\notin\}/g;
  335:     $result =~ s/&(ni|\#8715);/\\ensuremath\{\\ni\}/g;
  336:     $result =~ s/&(prod|\#8719);/\\ensuremath\{\\prod\}/g;
  337:     $result =~ s/&(sum|\#8721);/\\ensuremath\{\\sum\}/g;
  338:     $result =~ s/&(minus|\#8722);/\\ensuremath\{-\}/g;
  339:     $result =~ s/&(lowast|\#8727);/\\ensuremath\{*\}/g;
  340:     $result =~ s/&(radic|\#8730);/\\ensuremath\{\\surd\}/g;
  341:     $result =~ s/&(prop|\#8733);/\\ensuremath\{\\propto\}/g;
  342:     $result =~ s/&(infin|\#8734);/\\ensuremath\{\\infty\}/g;
  343:     $result =~ s/&(ang|\#8736);/\\ensuremath\{\\angle\}/g;
  344:     $result =~ s/&(and|\#8743);/\\ensuremath\{\\wedge\}/g;
  345:     $result =~ s/&(or|\#8744);/\\ensuremath\{\\vee\}/g;
  346:     $result =~ s/&(cap|\#8745);/\\ensuremath\{\\cap\}/g;
  347:     $result =~ s/&(cup|\#8746);/\\ensuremath\{\\cup\}/g;
  348:     $result =~ s/&(int|\#8747);/\\ensuremath\{\\int\}/g;
  349:     $result =~ s/&(sim|\#8764);/\\ensuremath\{\\sim\}/g;
  350:     $result =~ s/&(cong|\#8773);/\\ensuremath\{\\cong\}/g;
  351:     $result =~ s/&(asymp|\#8776);/\\ensuremath\{\\approx\}/g;
  352:     $result =~ s/&(ne|\#8800);/\\ensuremath\{\\not=\}/g;
  353:     $result =~ s/&(equiv|\#8801);/\\ensuremath\{\\equiv\}/g;
  354:     $result =~ s/&(le|\#8804);/\\ensuremath\{\\leq\}/g;
  355:     $result =~ s/&(ge|\#8805);/\\ensuremath\{\\geq\}/g;
  356:     $result =~ s/&(sub|\#8834);/\\ensuremath\{\\subset\}/g;
  357:     $result =~ s/&(sup|\#8835);/\\ensuremath\{\\supset\}/g;
  358:     $result =~ s/&(nsub|\#8836);/\\ensuremath\{\\not\\subset\}/g;
  359:     $result =~ s/&(sube|\#8838);/\\ensuremath\{\\subseteq\}/g;
  360:     $result =~ s/&(supe|\#8839);/\\ensuremath\{\\supseteq\}/g;
  361:     $result =~ s/&(oplus|\#8853);/\\ensuremath\{\\oplus\}/g;
  362:     $result =~ s/&(otimes|\#8855);/\\ensuremath\{\\otimes\}/g;
  363:     $result =~ s/&(perp|\#8869);/\\ensuremath\{\\perp\}/g;
  364:     $result =~ s/&(sdot|\#8901);/\\ensuremath\{\\cdot\}/g;
  365: #Geometric Shapes (extended HTML 4.01)
  366:     $result =~ s/&(loz|\#9674);/\\ensuremath\{\\Diamond\}/g;
  367: #Miscellaneous Symbols (extended HTML 4.01)
  368:     $result =~ s/&(spades|\#9824);/\\ensuremath\{\\spadesuit\}/g;
  369:     $result =~ s/&(clubs|\#9827);/\\ensuremath\{\\clubsuit\}/g;
  370:     $result =~ s/&(hearts|\#9829);/\\ensuremath\{\\heartsuit\}/g;
  371:     $result =~ s/&(diams|\#9830);/\\ensuremath\{\\diamondsuit\}/g;
  372:     return $result;
  373: }
  374: 
  375: 
  376: sub page_format {
  377: #
  378: #Correspondence between $papersize variable and real paper format:
  379: # 0 - "Letter [8 1/2x11 in]"
  380: # 1 - "Legal [8 1/2x14 in]"
  381: # 2 - "Ledger/Tabloid [11x17 in]"
  382: # 3 - "Executive [7 1/2x10 in]"
  383: # 4 - "A2 [420x594 mm]"
  384: # 5 - "A3 [297x420 mm]"
  385: # 6 - "A4 [210x297 mm]"
  386: # 7 - "A5 [148x210 mm]"
  387: # 8 - "A6 [105x148 mm]"
  388: # 
  389:     my ($papersize,$layout,$numberofcolumns) = @_; 
  390:     my ($textwidth,$textheight,$oddoffset,$evenoffset,$topmargin) = (0,0,0,0,0);
  391:     if ($papersize eq '0') {
  392: 	if ($layout eq 'book') {
  393: 	    if ($numberofcolumns == 1) {
  394: 		$textwidth = '7.1 in';      #'18 cm';
  395: 		$textheight = '10.2 in';    #'25.9 cm';
  396: 		$oddoffset = '-0.57 in';
  397: 		$evenoffset = '-0.57 in';
  398: 	    } elsif ($numberofcolumns == 2) {
  399: 		$textwidth = '3.66 in';     #'93 mm';
  400: 		$textheight = '10.2 in';    #'25.9 cm';
  401: 		$oddoffset = '-0.57 in';
  402: 		$evenoffset = '-0.57 in';
  403: 	    }
  404: 	} elsif ($layout eq 'album') {
  405: 	    if ($numberofcolumns eq '1') {
  406: 		$textwidth = '8.8 in';
  407: 		$textheight = '6.8 in';
  408: 		$oddoffset = '-40 pt';
  409: 		$evenoffset = '-60 pt';
  410: 	    } elsif ($numberofcolumns == 2) {
  411: 		$textwidth = '4.4 in';
  412: 		$textheight = '6.8 in';
  413: 		$oddoffset = '-0.5 in';
  414: 		$evenoffset = '-1.5 in';
  415: 		$topmargin = '3.5 in';
  416: 	    }
  417: 	}
  418: #    } elsif($papersize eq '1') {
  419: #    } elsif($papersize eq '2') {
  420: #    } elsif($papersize eq '3'/) {
  421:     } elsif($papersize eq '6') {
  422: 	if ($layout eq 'book') {
  423: 	    if ($numberofcolumns == 1) {
  424: 		$textwidth = '18 cm';
  425: 		$textheight = '28 cm';
  426: 		$oddoffset = '-0.57 in';
  427: 		$evenoffset = '-0.57 in';
  428: 	    } elsif ($numberofcolumns == 2) {
  429: 		$textwidth = '96 mm';
  430: 		$textheight = '2 cm';
  431: 		$oddoffset = '-0.57 in';
  432: 		$evenoffset = '-0.57 in';
  433: 	    }
  434: 	} elsif ($layout eq 'album') {
  435: 	    if ($numberofcolumns eq '1') {
  436: 		$textwidth = '8.5 in';
  437: 		$textheight = '7.7 in';
  438: 		$oddoffset = '-40 pt';
  439: 		$evenoffset = '-60 pt';
  440: 	    } elsif ($numberofcolumns == 2) {
  441: 		$textwidth = '3.9 in';
  442: 		$textheight = '7.7 in';
  443: 		$oddoffset = '-40 pt';
  444: 		$evenoffset = '-60 pt';
  445: 	    }
  446: 	}
  447: #    } elsif($papersize eq '5') {
  448: #    } elsif($papersize eq '4') {
  449: #    } elsif($papersize eq '7') {
  450: #    } elsif($papersize eq '8') {
  451:     }
  452:     return $textwidth,$textheight,$oddoffset,$evenoffset,$topmargin;
  453: }
  454: 
  455: 
  456: sub get_name {
  457:     my ($uname,$udom)=@_;
  458:     if (!defined($uname)) { $uname=$ENV{'user.name'}; }
  459:     if (!defined($udom)) { $udom=$ENV{'user.domain'}; }
  460:     my $plainname=&Apache::loncommon::plainname($uname,$udom);
  461:     if ($plainname=~/^\s*$/) { $plainname=$uname.'@'.$udom; }
  462:     $plainname=&Apache::lonxml::latex_special_symbols($plainname,'header');
  463:     return $plainname;
  464: }
  465: 
  466: sub get_course {
  467:     my $courseidinfo;
  468:     if (defined($ENV{'request.course.id'})) {
  469: 	$courseidinfo = &Apache::lonxml::latex_special_symbols(&Apache::lonnet::unescape($ENV{'course.'.$ENV{'request.course.id'}.'.description'}),'header');
  470:     }
  471:     return $courseidinfo;
  472: }
  473: 
  474: sub page_format_transformation {
  475:     my ($papersize,$layout,$numberofcolumns,$choice,$text,$assignment,$tableofcontents,$indexlist) = @_; 
  476:     my ($textwidth,$textheight,$oddoffset,$evenoffset,$topmargin);
  477:     $assignment=&Apache::lonxml::latex_special_symbols($assignment,'header');
  478:     ($textwidth,$textheight,$oddoffset,$evenoffset,$topmargin) = &page_format($papersize,$layout,$numberofcolumns,$topmargin);
  479:     my $name = &get_name();
  480:     my $courseidinfo = &get_course();
  481:     if (defined($courseidinfo)) { $courseidinfo=' - '.$courseidinfo }
  482:     if ($layout eq 'album') {
  483: 	my $topmargintoinsert = '';
  484: 	if ($topmargin ne '0') {$topmargintoinsert='\setlength{\topmargin}{'.$topmargin.'}';}
  485: 	    $text =~ s/\\begin{document}/\\setlength{\\oddsidemargin}{$oddoffset}\\setlength{\\evensidemargin}{$evenoffset}$topmargintoinsert\\setlength{\\textwidth}{$textwidth}\\setlength{\\textheight}{$textheight}\\setlength{\\textfloatsep}{8pt plus 2\.0pt minus 4\.0pt}\\newlength{\\minipagewidth}\\setlength{\\minipagewidth}{\\textwidth\/\$number_of_columns-0\.2cm}\\usepackage{fancyhdr}\\pagestyle{fancy}\\rhead{}\\chead{}\\lhead{\\textbf{$name} $courseidinfo \\hfill \\thepage \\\\ \\textit{$assignment}} \\begin{document}\\voffset=-0\.8 cm\\setcounter{page}{1} /;
  486:     } elsif ($layout eq 'book') {
  487: 	if ($choice ne 'All class print') { 
  488: 	    $text =~ s/\\begin{document}/\\textheight $textheight\\oddsidemargin = $evenoffset\\evensidemargin = $evenoffset\\textwidth= $textwidth\\newlength{\\minipagewidth}\\setlength{\\minipagewidth}{\\textwidth\/\$number_of_columns-0\.2cm}\\renewcommand{\\ref}{\\keephidden\}\\usepackage{fancyhdr}\\pagestyle{fancy}\\rhead{}\\chead{}\\lhead{\\textbf{$name} $courseidinfo \\hfill \\thepage \\\\ \\textit{$assignment}}\\begin{document}\\voffset=-0\.8 cm\\setcounter{page}{1}/;
  489: 	} else {
  490: 	    $text =~ s/\\pagestyle{fancy}\\rhead{}\\chead{}\s*\\begin{document}/\\textheight = $textheight\\oddsidemargin = $evenoffset\\evensidemargin = $evenoffset\\textwidth= $textwidth\\newlength{\\minipagewidth}\\setlength{\\minipagewidth}{\\textwidth\/\$number_of_columns-0\.2cm}\\renewcommand{\\ref}{\\keephidden\}\\pagestyle{fancy}\\rhead{}\\chead{}\\begin{document}\\voffset=-0\.8cm\\setcounter{page}{1}  \\vskip 5 mm /;
  491: 	}
  492:     }
  493:     if ($tableofcontents eq 'yes') {$text=~s/(\\setcounter\{page\}\{1\})/$1 \\tableofcontents\\newpage /;}
  494:     if ($indexlist eq 'yes') {
  495: 	$text=~s/(\\begin{document})/\\makeindex $1/;
  496: 	$text=~s/(\\end{document})/\\strut\\\\\\strut\\printindex $1/;
  497:     }
  498:     return $text;
  499: }
  500: 
  501: 
  502: sub page_cleanup {
  503:     my $result = shift;	
  504:  
  505:     $result =~ m/\\end{document}(\d*)$/;
  506:     my $number_of_columns = $1;
  507:     my $insert = '{';
  508:     for (my $id=1;$id<=$number_of_columns;$id++) { $insert .='l'; }
  509:     $insert .= '}';
  510:     $result =~ s/(\\begin{longtable})INSERTTHEHEADOFLONGTABLE\\endfirsthead\\endhead/$1$insert/g;
  511:     $result =~ s/&\s*REMOVETHEHEADOFLONGTABLE\\\\/\\\\/g;
  512:     return $result,$number_of_columns;
  513: }
  514: 
  515: 
  516: sub details_for_menu {
  517: 
  518:     my $name_of_resourse = &Apache::lonnet::gettitle($ENV{'form.postdata'});
  519:     my $symbolic = &Apache::lonnet::symbread($ENV{'form.postdata'});
  520:     my ($map,$id,$resource)=&Apache::lonnet::decode_symb($symbolic);
  521:     $map=&Apache::lonnet::clutter($map);
  522:     my $name_of_sequence = &Apache::lonnet::gettitle($map);
  523:     if ($name_of_sequence =~ /^\s*$/) {
  524: 	$map =~ m|([^/]+)$|;
  525: 	$name_of_sequence = $1;
  526:     }
  527:     my $name_of_map = &Apache::lonnet::gettitle($ENV{'request.course.uri'});
  528:     if ($name_of_map =~ /^\s*$/) {
  529: 	$ENV{'request.course.uri'} =~ m|([^/]+)$|;
  530: 	$name_of_map = $1;
  531:     }
  532:     return ($name_of_resourse,$name_of_sequence,$name_of_map);
  533: 
  534: }
  535: 
  536: 
  537: sub latex_corrections {
  538: 
  539:     my ($number_of_columns,$result) = @_;
  540: 
  541: #    $result =~ s/\\includegraphics{/\\includegraphics\[width=\\minipagewidth\]{/g;
  542:     $result =~ s/\$number_of_columns/$number_of_columns/g;
  543:     $result =~ s/(\\end{document})/\\strut\\newline\\noindent\\makebox\[\\textwidth\/$number_of_columns\]\[b\]{\\hrulefill}\\newline\\noindent\\tiny Printed from LON-CAPA\\copyright MSU{\\hfill} Licensed under GNU General Public License  $1/;
  544:     $result =~ s/(\\end{longtable}\s*)(\\strut\\newline\\noindent\\makebox\[\\textwidth\/$number_of_columns\]\[b\]{\\hrulefill})/$2$1/g;
  545:     $result =~ s/(\\end{longtable}\s*)\\strut\\newline/$1/g;
  546: #-- LaTeX corrections     
  547:     my $first_comment = index($result,'<!--',0);
  548:     while ($first_comment != -1) {
  549: 	my $end_comment = index($result,'-->',$first_comment);
  550: 	substr($result,$first_comment,$end_comment-$first_comment+3) = '';
  551: 	$first_comment = index($result,'<!--',$first_comment);
  552:     }
  553:     $result =~ s/^\s+$//gm; #remove empty lines
  554:     $result =~ s/(\s)(\s+)/$1/g; #removes more than one empty space
  555:     $result =~ s/\\\\\s*\\vskip/\\vskip/gm;
  556:     $result =~ s/\\\\\s*\\noindent\s*(\\\\)+/\\\\\\noindent /g;
  557:     $result =~ s/{\\par }\s*\\\\/\\\\/gm;
  558: 	$result =~ s/\\\\\s+\[/ \[/g;
  559:     $result =~ s/\b__+\b/\\makebox\[1 cm\]\[b\]{\\hrulefill}/g;
  560:     #conversion of html characters to LaTeX equivalents
  561:     if ($result =~ m/&(\w+|#\d+);/) {
  562: 	$result = &character_chart($result);
  563:     }
  564:     $result =~ s/(\\end{tabular})\s*\\vskip 0 mm/$1/g;
  565:     $result =~ s/(\\begin{enumerate})\s*\\noindent/$1/g;
  566: 
  567:     return $result;
  568: }
  569: 
  570: 
  571: sub index_table {
  572:     my $currentURL = shift;
  573:     my $insex_string='';
  574:     $currentURL=~s/\.([^\/+])$/\.$1\.meta/;
  575:     $insex_string=&Apache::lonnet::metadata($currentURL,'keywords');
  576:     return $insex_string;
  577: }
  578: 
  579: 
  580: sub IndexCreation {
  581:     my ($texversion,$currentURL)=@_;
  582:     my @key_words=split(/,/,&index_table($currentURL));
  583:     my $chunk='';
  584:     my $st=index $texversion,'\addcontentsline{toc}{subsection}{';
  585:     if ($st>0) {
  586: 	for (my $i=0;$i<3;$i++) {$st=(index $texversion,'}',$st+1);}
  587: 	$chunk=substr($texversion,0,$st+1);
  588: 	substr($texversion,0,$st+1)=' ';
  589:     }
  590:     foreach my $key_word (@key_words) {
  591: 	if ($key_word=~/\S+/) {
  592: 	    $texversion=~s/\b($key_word)\b/$1 \\index{$key_word} /i;
  593: 	}
  594:     }			
  595:     if ($st>0) {substr($texversion,0,1)=$chunk;}
  596:     return $texversion;
  597: }
  598: 
  599: sub print_latex_header {
  600:     my $mode=shift;
  601:     my $output='\documentclass[letterpaper]{article}';
  602:     if ($mode eq 'batchmode') {
  603: 	$output.='\batchmode';
  604:     }
  605:     $output.='\newcommand{\keephidden}[1]{}\renewcommand{\deg}{$^{\circ}$}'.
  606: 	     '\usepackage{longtable}\usepackage{textcomp}\usepackage{makeidx}'.
  607: 	     '\usepackage[dvips]{graphicx}\usepackage{epsfig}\usepackage{calc}'.
  608: 	     '\newenvironment{choicelist}{\begin{list}{}{\setlength{\rightmargin}{0in}'.
  609: 	     '\setlength{\leftmargin}{0.13in}\setlength{\topsep}{0.05in}'.
  610: 	     '\setlength{\itemsep}{0.022in}\setlength{\parsep}{0in}'.
  611: 	     '\setlength{\belowdisplayskip}{0.04in}\setlength{\abovedisplayskip}{0.05in}'.
  612: 	     '\setlength{\abovedisplayshortskip}{-0.04in}'.
  613: 	     '\setlength{\belowdisplayshortskip}{0.04in}}}{\end{list}}'.
  614: 	     '\renewenvironment{theindex}{\begin{list}{}{{\vskip 1mm \noindent \large'.
  615: 	     '\textbf{Index}} \newline \setlength{\rightmargin}{0in}'.
  616: 	     '\setlength{\leftmargin}{0.13in}\setlength{\topsep}{0.01in}'.
  617: 	     '\setlength{\itemsep}{0.1in}\setlength{\parsep}{-0.02in}'.
  618: 	     '\setlength{\belowdisplayskip}{0.01in}\setlength{\abovedisplayskip}{0.01in}'.
  619: 	     '\setlength{\abovedisplayshortskip}{-0.04in}'.
  620: 	     '\setlength{\belowdisplayshortskip}{0.01in}}}{\end{list}}\begin{document}';
  621:     return $output;	     
  622: }
  623: 
  624: sub path_to_problem {
  625:     my ($urlp,$LaTeXwidth)=@_;
  626:     my $newurlp = '';
  627:     $LaTeXwidth=~s/\s*mm\s*$//;
  628:     my $HowMany = length($urlp)*2;
  629:     if ($HowMany > $LaTeXwidth) {
  630: 	my @temporrary = split '/',$urlp;
  631: 	my $HowManyNew = 0;
  632: 	for (my $ii=0;$ii<=$#temporrary;$ii++) {
  633: 	    if ($temporrary[$ii] ne '') {
  634: 		$HowManyNew += length($temporrary[$ii])*2;
  635: 		if ($HowManyNew < $LaTeXwidth ) {
  636: 		    $newurlp .=  '/'.$temporrary[$ii];
  637: 		} else {
  638: 		    $HowManyNew = 0;
  639: 		    $newurlp .=  '|\vskip -1 mm \noindent \verb|';
  640: 		    $ii--;
  641: 		}
  642: 	    }
  643: 	}
  644:     } else {
  645: 	$newurlp=$urlp;
  646:     }
  647:     return '{\small\noindent\verb|'.$newurlp.'|\vskip 0 mm}';
  648: }
  649: 
  650: sub recalcto_mm {
  651:     my $textwidth=shift;
  652:     my $LaTeXwidth;
  653:     if ($textwidth=~/(\d+\.?\d*)\s*cm/) {
  654: 	$LaTeXwidth = $1*10;
  655:     } elsif ($textwidth=~/(\d+\.?\d*)\s*mm/) {
  656: 	$LaTeXwidth = $1;
  657:     } elsif ($textwidth=~/(\d+\.?\d*)\s*in/) {
  658: 	$LaTeXwidth = $1*25.4;
  659:     }
  660:     $LaTeXwidth.=' mm';
  661:     return $LaTeXwidth;
  662: }
  663: 
  664: sub get_textwidth {
  665:     my ($helper,$LaTeXwidth)=@_;
  666:     my $textwidth=$LaTeXwidth;
  667:     if ($helper->{'VARS'}->{'pagesize.width'}=~/\d+/ &&
  668: 	$helper->{'VARS'}->{'pagesize.widthunit'}=~/\w+/) {
  669: 	$textwidth=&recalcto_mm($helper->{'VARS'}->{'pagesize.width'}.' '.
  670: 				$helper->{'VARS'}->{'pagesize.widthunit'});
  671:     }
  672:     return $textwidth;
  673: }
  674: 
  675: sub output_data {
  676:     my ($r,$helper,$rparmhash) = @_;
  677:     my %parmhash = %$rparmhash;
  678:     my $bodytag=&Apache::loncommon::bodytag('Preparing Printout');
  679:     $r->print(<<ENDPART);
  680: <html>
  681: <script type="text/javascript" language="Javascript" >
  682:     var editbrowser;
  683:     function openbrowser(formname,elementname,only,omit) {
  684:         var url = '/res/?';
  685:         if (editbrowser == null) {
  686:             url += 'launch=1&';
  687:         }
  688:         url += 'catalogmode=interactive&';
  689:         url += 'mode=parmset&';
  690:         url += 'form=' + formname + '&';
  691:         if (only != null) {
  692:             url += 'only=' + only + '&';
  693:         } 
  694:         if (omit != null) {
  695:             url += 'omit=' + omit + '&';
  696:         }
  697:         url += 'element=' + elementname + '';
  698:         var title = 'Browser';
  699:         var options = 'scrollbars=1,resizable=1,menubar=0';
  700:         options += ',width=700,height=600';
  701:         editbrowser = open(url,title,options,'1');
  702:         editbrowser.focus();
  703:     }
  704: </script>
  705: <head>
  706: <title>LON-CAPA output for printing</title>
  707: </head>
  708: $bodytag
  709: Please stand by while processing your print request, this may take some time ...
  710: ENDPART
  711: 
  712:     my $format_from_helper = $helper->{'VARS'}->{'FORMAT'};
  713:     my ($result,$selectionmade) = ('','');
  714:     my $number_of_columns = 1; #used only for pages to determine the width of the cell
  715:     my @temporary_array=split /\|/,$format_from_helper;
  716:     my ($laystyle,$numberofcolumns,$papersize)=@temporary_array;
  717:     if ($laystyle eq 'L') {
  718: 	$laystyle='album';
  719:     } else {
  720: 	$laystyle='book';
  721:     }
  722:     my ($textwidth,$textheight,$oddoffset,$evenoffset) = &page_format($papersize,$laystyle,$numberofcolumns);
  723:     my $assignment =  $ENV{'form.assignment'};
  724:     my $LaTeXwidth=&recalcto_mm($textwidth); 
  725:     my @print_array=();
  726:     my @student_names=();
  727:     
  728:     if ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'current_document') {
  729:       #-- single document - problem, page, html, xml, ...
  730: 	my $currentURL;
  731: 	if ($helper->{'VARS'}->{'construction'} ne '1') {
  732:             #prints published resource
  733: 	    $currentURL=$helper->{'VARS'}->{'postdata'};
  734: 	} else {
  735:             #prints resource from the construction space
  736: 	    $currentURL='/'.$helper->{'VARS'}->{'filename'};
  737: 	    if ($currentURL=~/([^?]+)/) {$currentURL=$1;}
  738: 	}
  739: 	$selectionmade = 1;
  740: 	if ($currentURL=~/\.(problem|exam|quiz|assess|survey|form|library|page|xml|html|htm|xhtml|xhtm)$/) {
  741: 	    my $rndseed=time;
  742: 	    my $texversion='';
  743: 	    if ($helper->{'VARS'}->{'ANSWER_TYPE'} ne 'only') {
  744: 		my %moreenv;
  745: 		$moreenv{'request.filename'}=$currentURL;
  746: 		if ($helper->{'VARS'}->{'style_file'}=~/\w/) {
  747: 		    $moreenv{'construct.style'}=$helper->{'VARS'}->{'style_file'};
  748: 		    my $dom = $ENV{'user.domain'};
  749: 		    my $user = $ENV{'user.name'};
  750: 		    my $put_result = &Apache::lonnet::put('environment',{'construct.style'=>$helper->{'VARS'}->{'style_file'}},$dom,$user);
  751: 		}
  752: 		my %form;
  753: 		$form{'grade_target'}='tex';
  754: 		$form{'textwidth'}=&get_textwidth($helper,$LaTeXwidth);
  755: 		$form{'problem_split'}=$parmhash{'problem_stream_switch'};
  756: 		$form{'latex_type'}=$helper->{'VARS'}->{'LATEX_TYPE'};
  757: 		if ($helper->{'VARS'}->{'curseed'}) {
  758: 		    $rndseed=$helper->{'VARS'}->{'curseed'};
  759: 		}
  760: 		$form{'rndseed'}=$rndseed;
  761: 		&Apache::lonnet::appenv(%moreenv);
  762: 		&Apache::lonnet::delenv('form.counter');
  763: 		&Apache::lonxml::init_counter();
  764: 		$texversion.=&Apache::lonnet::ssi($currentURL,%form);
  765: 		&Apache::lonnet::delenv('form.counter');
  766: 		&Apache::lonnet::delenv('request.filename');
  767: 	    }
  768: 	    if(($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') ||
  769: 	       ($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'only')) {
  770: 		my %form;
  771: 		$form{'grade_target'}='answer';
  772: 		$form{'answer_output_mode'}='tex';
  773: 		$form{'rndseed'}=$rndseed;
  774: 		my $answer=&Apache::lonnet::ssi($currentURL,%form);
  775: 		if ($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') {
  776: 		    $texversion=~s/(\\keephidden{ENDOFPROBLEM})/$answer$1/;
  777: 		} else {
  778: 		    $texversion=&print_latex_header($helper->{'VARS'}->{'LATEX_TYPE'});
  779: 		    if ($helper->{'VARS'}->{'construction'} ne '1') {
  780: 			$texversion.='\vskip 0 mm \noindent\textbf{'.&Apache::lonnet::gettitle($helper->{'VARS'}->{'symb'}).'}\vskip 0 mm ';
  781: 			$texversion.=&path_to_problem ($currentURL,$LaTeXwidth);
  782: 		    } else {
  783: 			$texversion.='\vskip 0 mm \noindent\textbf{Prints from construction space - there is no title.}\vskip 0 mm ';
  784: 			my $URLpath=$currentURL;
  785: 			$URLpath=~s/~([^\/]+)/public_html\/$1\/$1/;
  786: 			$texversion.=&path_to_problem ($URLpath,$LaTeXwidth);
  787: 		    }
  788: 		    $texversion.='\vskip 1 mm '.$answer.'\end{document}';
  789: 		}
  790: 	    }
  791: 	    if ($helper->{'VARS'}->{'TABLE_INDEX'} eq 'yes') {
  792: 		$texversion=&IndexCreation($texversion,$currentURL);
  793: 	    }
  794: 	    if ($helper->{'VARS'}->{'CONSTR_RESOURSE_URL'} eq 'yes') {
  795: 		$texversion=~s/(\\addcontentsline\{toc\}\{subsection\}\{[^\}]*\})/$1 URL: \\verb|$currentURL| \\strut\\\\\\strut /;
  796: 
  797: 	    }
  798: 	    $result .= $texversion;
  799: 	    if ($currentURL=~m/\.page\s*$/) {
  800: 		($result,$number_of_columns) = &page_cleanup($result);
  801: 	    }
  802:         } elsif ($currentURL=~/\.sequence$/ && $helper->{'VARS'}->{'construction'} eq '1') {
  803:             #printing content of sequence from the construction space	
  804: 	    my $flag_latex_header_remove = 'NO'; 
  805: 	    my $rndseed=time;
  806: 	    if ($helper->{'VARS'}->{'curseed'}) {
  807: 		$rndseed=$helper->{'VARS'}->{'curseed'};
  808: 	    }
  809: 	    $currentURL=~s|\/~([^\/]+)\/|\/home\/$1\/public_html\/|;
  810: 	    my $errtext=&Apache::lonratedt::mapread($currentURL);
  811: 	    for (my $member=0;$member<=$#Apache::lonratedt::order;$member++) {
  812: 		$Apache::lonratedt::resources[$Apache::lonratedt::order[$member]]=~/^([^:]*):([^:]*):/;
  813: 		my $urlp=$2;
  814: 		if ($urlp=~/\.(problem|exam|quiz|assess|survey|form|library|xml|html|htm|xhtml|xhtm)$/) {
  815: 		    my $texversion='';
  816: 		    if ($helper->{'VARS'}->{'ANSWER_TYPE'} ne 'only') {
  817: 			my %form;
  818: 			$form{'grade_target'}='tex';
  819: 			$form{'textwidth'}=&get_textwidth($helper,$LaTeXwidth);
  820: 			$form{'problem_split'}=$parmhash{'problem_stream_switch'};
  821: 			$form{'latex_type'}=$helper->{'VARS'}->{'LATEX_TYPE'};
  822: 			$form{'rndseed'}=$rndseed;
  823: 			$texversion=&Apache::lonnet::ssi($urlp,%form);
  824: 		    }
  825: 		    if((($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') ||
  826: 		       ($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'only')) && 
  827: 		       ($urlp=~/\.(problem|exam|quiz|assess|survey|form|library|page)$/)) {
  828: 			my %form;
  829: 			$form{'grade_target'}='answer';
  830: 			$form{'answer_output_mode'}='tex';
  831: 			$form{'rndseed'}=$rndseed;
  832:                         if ($urlp=~/\/res\//) {$ENV{'request.state'}='published';}
  833: 			my $answer=&Apache::lonnet::ssi($urlp,%form);
  834: 			if ($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') {
  835: 			    $texversion=~s/(\\keephidden{ENDOFPROBLEM})/$answer$1/;
  836: 			} else {
  837: 			    $texversion=&print_latex_header($helper->{'VARS'}->{'LATEX_TYPE'});
  838: 			    $texversion.='\vskip 0 mm \noindent\textbf{'.&Apache::lonnet::gettitle($helper->{'VARS'}->{'symb'}).'}\vskip 0 mm ';
  839: 			    $texversion.=&path_to_problem ($currentURL,$LaTeXwidth);
  840: 			    $texversion.='\vskip 1 mm '.$answer.'\end{document}';
  841: 			}
  842: 		    }
  843: 		    if ($flag_latex_header_remove ne 'NO') {
  844: 			$texversion = &latex_header_footer_remove($texversion);
  845: 		    } else {
  846: 			$texversion =~ s/\\end{document}//;
  847: 		    }
  848: 		    if ($helper->{'VARS'}->{'TABLE_INDEX'} eq 'yes') {
  849: 			$texversion=&IndexCreation($texversion,$urlp);
  850: 		    }
  851: 		    if ($helper->{'VARS'}->{'CONSTR_RESOURSE_URpL'} eq 'yes') {
  852: 			$texversion=~s/(\\addcontentsline\{toc\}\{subsection\}\{[^\}]*\})/$1 URL: \\verb|$urlp| \\strut\\\\\\strut /;
  853: 		    }
  854: 		    $result.=$texversion;
  855: 		    $flag_latex_header_remove = 'YES';
  856: 		} elsif ($urlp=~/\.(sequence|page)$/) {
  857: 		    $result.='\strut\newline\noindent Sequence/page '.$urlp.'\strut\newline\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}\newline\noindent ';
  858: 		}  
  859: 	    }
  860: 	    if ($helper->{VARS}->{'construction'} eq '1') {$result=~s/(\\begin{document})/$1 \\fbox\{RANDOM SEED IS $rndseed\} /;}
  861: 	    $result .= '\end{document}';   
  862: 	} elsif ($currentURL=~/\/smppg$/) { 
  863: 		my %form;
  864: 		$form{'grade_target'}='tex';
  865: 		$form{'textwidth'}=&get_textwidth($helper,$LaTeXwidth);
  866: 		$form{'latex_type'}=$helper->{'VARS'}->{'LATEX_TYPE'};
  867: 		my $texversion=&Apache::lonnet::ssi($currentURL,%form);
  868: 		$result .= $texversion;
  869: 	} else {
  870: 	  $result.='\documentclass[letterpaper]{article}\usepackage{calc}\begin{document}Printout of this type of document is currently not supported: ';
  871: 	  if ($currentURL=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) {
  872: 	      $result.=$1;
  873: 	  } elsif ($currentURL=~/\/ext\//) {
  874: 	      $result.=' \strut \\\\ THIS IS EXTERNAL RESOURCE WITH URL \strut \\\\ '.$currentURL.' ';
  875: 	  } else {
  876: 	      $result.=$currentURL;
  877: 	  }
  878: 	  $result.=' \end{document}'
  879: 	}
  880:     } elsif (($helper->{'VARS'}->{'PRINT_TYPE'} eq 'map_problems') or
  881:              ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'map_problems_pages') or
  882:              ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'all_problems') or
  883: 	     ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'select_sequences')) { 
  884:         #-- produce an output string
  885: 	my %form=();	   
  886: 	$form{'grade_target'}='tex';
  887: 	$form{'textwidth'}=&get_textwidth($helper,$LaTeXwidth);
  888: 	$form{'problem_split'}=$parmhash{'problem_stream_switch'};
  889: 	$form{'latex_type'}=$helper->{'VARS'}->{'LATEX_TYPE'};
  890: 	my $flag_latex_header_remove = 'NO';
  891: 	my $flag_page_in_sequence = 'NO';
  892: 	my @master_seq=split /\|\|\|/, $helper->{'VARS'}->{'RESOURCES'};
  893: 	my $prevassignment='';
  894: 	&Apache::lonnet::delenv('form.counter');
  895: 	&Apache::lonxml::init_counter();
  896: 	for (my $i=0;$i<=$#master_seq;$i++) {
  897: 	    my (undef,undef,$urlp)=&Apache::lonnet::decode_symb($master_seq[$i]);
  898: 	    $urlp=&Apache::lonnet::clutter($urlp);
  899: 	    if ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'map_problems') {
  900: 		$selectionmade = 2;
  901: 	    } elsif ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'map_problems_pages') {
  902: 		$selectionmade = 3;
  903: 	    } elsif ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'all_problems') {
  904: 		$selectionmade = 4;
  905: 	    } elsif ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'select_sequences') {
  906: 		$selectionmade = 7;
  907: 	    }
  908: 	    $form{'symb'}=$master_seq[$i];
  909: 	    my ($sequence)=&Apache::lonnet::decode_symb($master_seq[$i]);
  910: 	    my $assignment=&Apache::lonxml::latex_special_symbols(&Apache::lonnet::gettitle($sequence),'header'); #tittle of the assignment which contains this problem
  911: 	    if ($selectionmade==7) {$helper->{VARS}->{'assignment'}=$assignment;}
  912: 	    if ($i==0) {$prevassignment=$assignment;}
  913: 	    #&Apache::lonnet::logthis("Trying to get $urlp with symb $master_seq[$i]");
  914: 	    my $texversion.=&Apache::lonnet::ssi($urlp,%form);
  915: 	    if ($urlp=~/\.page$/) {
  916: 		($texversion,my $number_of_columns_page) = &page_cleanup($texversion);
  917: 		if ($number_of_columns_page > $number_of_columns) {$number_of_columns=$number_of_columns_page;} 
  918: 		$texversion =~ s/\\end{document}\d*/\\end{document}/;
  919: 		$flag_page_in_sequence = 'YES';
  920: 	    } 
  921: 	    my $lonidsdir=$r->dir_config('lonIDsDir');
  922: 	    my $envfile=$ENV{'user.environment'};
  923: 	    $envfile=~/\/([^\/]+)\.id$/;
  924:             $envfile=$1;
  925: 	    &Apache::lonnet::transfer_profile_to_env($lonidsdir,$envfile);
  926: 	    my $current_counter=$ENV{'form.counter'};
  927: 	    if(($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') ||
  928: 	       ($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'only')) {
  929: 		my %form;
  930: 		$form{'grade_target'}='answer';
  931: 		$form{'answer_output_mode'}='tex';
  932: 		my $answer=&Apache::lonnet::ssi($urlp,%form);
  933:                 &Apache::lonnet::appenv(('form.counter' => $current_counter));
  934: 		if ($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') {
  935: 		    $texversion=~s/(\\keephidden{ENDOFPROBLEM})/$answer$1/;
  936: 		} else {
  937: 		    if ($urlp=~/\.(problem|exam|quiz|library)$/) {
  938: 			$texversion=&print_latex_header($helper->{'VARS'}->{'LATEX_TYPE'});
  939: 			$texversion.='\vskip 0 mm \noindent\textbf{'.&Apache::lonnet::gettitle($urlp).'}\vskip 0 mm ';
  940: 			$texversion.=&path_to_problem ($urlp,$LaTeXwidth);
  941: 			$texversion.='\vskip 1 mm '.$answer;
  942: 		    } else {
  943: 			$texversion='';
  944: 		    }
  945: 		}
  946: 	    }
  947: 	    if ($flag_latex_header_remove ne 'NO') {
  948: 		$texversion = &latex_header_footer_remove($texversion);
  949: 	    } else {
  950: 		$texversion =~ s/\\end{document}//;
  951: 	    }
  952: 	    if ($helper->{'VARS'}->{'TABLE_INDEX'} eq 'yes') {
  953: 		$texversion=&IndexCreation($texversion,$urlp);
  954: 	    }
  955: 	    if (($selectionmade == 4) and ($assignment ne $prevassignment)) {
  956: 		my $name = &get_name();
  957: 		my $courseidinfo = &get_course();
  958: 		if (defined($courseidinfo)) { $courseidinfo=' - '.$courseidinfo }
  959: 		$prevassignment=$assignment;
  960: 		$result .='\newpage \noindent\parbox{\minipagewidth}{\noindent\\lhead{\\textit{\\textbf{'.$name.'}}'.$courseidinfo.' \\hfill \\thepage \\\\ \\textit{'.$assignment.'}}} \vskip -5 mm ';
  961: 	    }
  962: 	    $result .= $texversion;
  963: 	    $flag_latex_header_remove = 'YES';   
  964: 	}
  965: 	&Apache::lonnet::delenv('form.counter');
  966: 	if ($flag_page_in_sequence eq 'YES') {$result =~ s/\\usepackage{calc}/\\usepackage{calc}\\usepackage{longtable}/;}	
  967: 	$result .= '\end{document}';
  968:      } elsif (($helper->{'VARS'}->{'PRINT_TYPE'} eq 'problems_for_students') ||
  969: 	      ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'resources_for_students')){
  970:      #-- prints assignments for whole class or for selected students  
  971: 	 my $type;
  972: 	 if ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'problems_for_students') {
  973: 	     $selectionmade=5;
  974: 	     $type='problems';
  975: 	 } elsif ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'resources_for_students') {
  976: 	     $selectionmade=8;
  977: 	     $type='resources';
  978: 	 }
  979: 	 my @students=split /\|\|\|/, $helper->{'VARS'}->{'STUDENTS'};
  980:          if ($helper->{'VARS'}->{'NUMBER_TO_PRINT'} eq '0' ||
  981: 	     $helper->{'VARS'}->{'NUMBER_TO_PRINT'} eq 'all' ) {
  982: 	     $helper->{'VARS'}->{'NUMBER_TO_PRINT'}=$#students+1;
  983: 	 }
  984: 	 my @master_seq=split /\|\|\|/, $helper->{'VARS'}->{'RESOURCES'};
  985: 	 #loop over students
  986: 	 my $flag_latex_header_remove = 'NO'; 
  987: 	 my %moreenv;
  988: 	 $moreenv{'textwidth'}=&get_textwidth($helper,$LaTeXwidth);
  989: 	 my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin($r,'Print Status','Class Print Status',$#students+1);
  990: 	 my $student_counter=-1;
  991: 	 foreach my $person (@students) {
  992: 	     $student_counter++;
  993: 	     my $i=int($student_counter/$helper->{'VARS'}{'NUMBER_TO_PRINT'});
  994: 	     my ($output,$fullname)=&print_resources($r,$helper,$person,$type,\%moreenv,\@master_seq,$flag_latex_header_remove);
  995: 	     $print_array[$i].=$output;
  996: 	     $student_names[$i].=$person.':'.$fullname.'_END_';
  997: 	     &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,&mt('last student').' '.$fullname);
  998: 	     $flag_latex_header_remove = 'YES';
  999: 	 }
 1000: 	 &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
 1001: 	 $result .= $print_array[0].'  \end{document}';
 1002:      } elsif (($helper->{'VARS'}->{'PRINT_TYPE'} eq 'problems_for_anon')     ||
 1003: 	      ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'resources_for_anon')  ) { 
 1004: 	 $selectionmade="Seems to be useless";
 1005: 	 my @master_seq=split /\|\|\|/, $helper->{'VARS'}->{'RESOURCES'};
 1006: 	 
 1007:     } elsif ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'problems_from_directory') {      
 1008:     #prints selected problems from the subdirectory 
 1009: 	$selectionmade = 6;
 1010:         my @list_of_files=split /\|\|\|/, $helper->{'VARS'}->{'FILES'};
 1011: 	@list_of_files=sort @list_of_files;
 1012: 	my $flag_latex_header_remove = 'NO'; 
 1013: 	my $rndseed=time;
 1014: 	if ($helper->{'VARS'}->{'curseed'}) {
 1015: 	    $rndseed=$helper->{'VARS'}->{'curseed'};
 1016: 	}
 1017: 	for (my $i=0;$i<=$#list_of_files;$i++) {
 1018: 	    my $urlp = $list_of_files[$i];
 1019: 	    $urlp=~s|//|/|;
 1020: 	    if ($urlp=~/\//) {
 1021: 		my %form;
 1022: 		$form{'grade_target'}='tex';
 1023: 		$form{'textwidth'}=&get_textwidth($helper,$LaTeXwidth);
 1024: 		$form{'rndseed'}=$rndseed;
 1025: 		if ($urlp =~ m|/home/([^/]+)/public_html|) {
 1026: 		    $urlp =~ s|/home/([^/]*)/public_html|/~$1|;
 1027: 		} else {
 1028: 		    $urlp =~ s|^/home/httpd/html||;
 1029: 		}
 1030: 		my $texversion=&Apache::lonnet::ssi($urlp,%form);
 1031: 		if(($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') ||
 1032: 		   ($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'only')) {
 1033: 		    my %form;
 1034: 		    $form{'grade_target'}='answer';
 1035: 		    $form{'answer_output_mode'}='tex';
 1036: 		    $form{'latex_type'}=$helper->{'VARS'}->{'LATEX_TYPE'};
 1037: 		    $form{'rndseed'}=$rndseed;
 1038: 		    my $answer=&Apache::lonnet::ssi($urlp,%form);
 1039: 		    if ($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') {
 1040: 			$texversion=~s/(\\keephidden{ENDOFPROBLEM})/$answer$1/;
 1041: 		    } else {
 1042: 			$texversion=&print_latex_header($helper->{'VARS'}->{'LATEX_TYPE'});
 1043: 			if ($helper->{'VARS'}->{'construction'} ne '1') {
 1044: 			    $texversion.='\vskip 0 mm \noindent ';
 1045: 			    $texversion.=&path_to_problem ($urlp,$LaTeXwidth);
 1046: 			} else {
 1047: 			    $texversion.='\vskip 0 mm \noindent\textbf{Prints from construction space - there is no title.}\vskip 0 mm ';
 1048: 			    my $URLpath=$urlp;
 1049: 			    $URLpath=~s/~([^\/]+)/public_html\/$1\/$1/;
 1050: 			    $texversion.=&path_to_problem ($URLpath,$LaTeXwidth);
 1051: 			}
 1052: 			$texversion.='\vskip 1 mm '.$answer.'\end{document}';
 1053: 		    }
 1054: 		}
 1055:                 #this chunck is responsible for printing the path to problem
 1056: 		my $newurlp=$urlp;
 1057: 		if ($newurlp=~/~/) {$newurlp=~s|\/~([^\/]+)\/|\/home\/$1\/public_html\/|;}
 1058: 		$newurlp=&path_to_problem($newurlp,$LaTeXwidth);
 1059: 		$texversion =~ s/(\\begin{minipage}{\\textwidth})/$1 $newurlp/;
 1060: 		if ($flag_latex_header_remove ne 'NO') {
 1061: 		    $texversion = &latex_header_footer_remove($texversion);
 1062: 		} else {
 1063: 		    $texversion =~ s/\\end{document}//;
 1064: 		}
 1065: 		if ($helper->{'VARS'}->{'TABLE_INDEX'} eq 'yes') {
 1066: 		    $texversion=&IndexCreation($texversion,$urlp);
 1067: 		}
 1068: 		if ($helper->{'VARS'}->{'CONSTR_RESOURSE_URL'} eq 'yes') {
 1069: 		    $texversion=~s/(\\addcontentsline\{toc\}\{subsection\}\{[^\}]*\})/$1 URL: \\verb|$urlp| \\strut\\\\\\strut /;
 1070: 		    
 1071: 		}
 1072: 		$result .= $texversion;
 1073: 	    }
 1074: 	    $flag_latex_header_remove = 'YES';  
 1075: 	}
 1076: 	if ($helper->{VARS}->{'construction'} eq '1') {$result=~s/(\\typeout)/ RANDOM SEED IS $rndseed $1/;}
 1077: 	$result .= '\end{document}';      	
 1078:     }
 1079: #-------------------------------------------------------- corrections for the different page formats
 1080:     $result = &page_format_transformation($papersize,$laystyle,$numberofcolumns,$helper->{'VARS'}->{'PRINT_TYPE'},$result,$helper->{VARS}->{'assignment'},$helper->{'VARS'}->{'TABLE_CONTENTS'},$helper->{'VARS'}->{'TABLE_INDEX'});
 1081:     $result = &latex_corrections($number_of_columns,$result);
 1082:     for (my $i=1;$i<=$#print_array;$i++) {$print_array[$i] = &latex_corrections($number_of_columns,$print_array[$i]);}
 1083:     #changes page's parameters for the one column output 
 1084:     if ($numberofcolumns == 1) {
 1085: 	$result =~ s/\\textwidth\s*=\s*\d*\.?\d*\s*(cm|mm|in)/\\textwidth= $helper->{'VARS'}->{'pagesize.width'} $helper->{'VARS'}->{'pagesize.widthunit'} /;
 1086: 	$result =~ s/\\textheight\s*=\s*\d*\.?\d*\s*(cm|mm|in)/\\textheight $helper->{'VARS'}->{'pagesize.height'} $helper->{'VARS'}->{'pagesize.heightunit'} /;
 1087: 	$result =~ s/\\evensidemargin\s*=\s*\d*\.?\d*\s*(cm|mm|in)/\\evensidemargin= $helper->{'VARS'}->{'pagesize.lmargin'} $helper->{'VARS'}->{'pagesize.lmarginunit'} /;
 1088: 	$result =~ s/\\oddsidemargin\s*=\s*\d*\.?\d*\s*(cm|mm|in)/\\oddsidemargin= $helper->{'VARS'}->{'pagesize.lmargin'} $helper->{'VARS'}->{'pagesize.lmarginunit'} /;
 1089:     }
 1090: #-- writing .tex file in prtspool 
 1091:     my $temp_file;
 1092:     my $identifier = &Apache::loncommon::get_cgi_id();
 1093:     my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout_".$identifier.".tex";
 1094:     if (!($#print_array>0)) { 
 1095: 	unless ($temp_file = Apache::File->new('>'.$filename)) {
 1096: 	    $r->log_error("Couldn't open $filename for output $!");
 1097: 	    return SERVER_ERROR; 
 1098: 	}
 1099: 	print $temp_file $result;
 1100: 	my $begin=index($result,'\begin{document}',0);
 1101: 	my $inc=substr($result,0,$begin+16);
 1102:     } else {
 1103: 	my $begin=index($result,'\begin{document}',0);
 1104: 	my $inc=substr($result,0,$begin+16);
 1105:         for (my $i=0;$i<=$#print_array;$i++) {
 1106: 	    if ($i==0) {
 1107: 		$print_array[$i]=$result;
 1108: 	    } else {
 1109: 		my $anobegin=index($print_array[$i],'\setcounter{page}',0);
 1110: 		substr($print_array[$i],0,$anobegin)='';
 1111: 		$print_array[$i]=$inc.$print_array[$i].'\end{document}';
 1112: 	    }
 1113: 	    my $temp_file;
 1114: 	    my $newfilename=$filename;
 1115: 	    my $num=$i+1;
 1116: 	    $newfilename =~s/\.tex$/_$num\.tex/; 
 1117: 	    unless ($temp_file = Apache::File->new('>'.$newfilename)) {
 1118: 		$r->log_error("Couldn't open $newfilename for output $!");
 1119: 		return SERVER_ERROR; 
 1120: 	    }
 1121: 	    print $temp_file $print_array[$i];
 1122: 	}
 1123:     }
 1124:     my $student_names='';
 1125:     if ($#print_array>0) {
 1126: 	for (my $i=0;$i<=$#print_array;$i++) {
 1127: 	    $student_names.=$student_names[$i].'_ENDPERSON_';
 1128: 	}
 1129:     } else {
 1130: 	if ($#student_names>-1) {
 1131: 	    $student_names=$student_names[0].'_ENDPERSON_';
 1132: 	} else {
 1133: 	    my $fullname = &get_name($ENV{'user.name'},$ENV{'user.domain'});
 1134: 	    $student_names=join(':',$ENV{'user.name'},$ENV{'user.domain'},
 1135: 				$ENV{'request.course.sec'},$fullname).
 1136: 				    '_ENDPERSON_'.'_END_';
 1137: 	}
 1138:     }
 1139: 
 1140:     my $URLback=''; #link to original document
 1141:     if ($helper->{'VARS'}->{'construction'} ne '1') {
 1142: 	#prints published resource
 1143: 	$URLback=$helper->{'VARS'}->{'postdata'};
 1144:     } else {
 1145: 	#prints resource from the construction space
 1146: 	$URLback='/'.$helper->{'VARS'}->{'filename'};
 1147: 	if ($URLback=~/([^?]+)/) {
 1148: 	    $URLback=$1;
 1149: 	    $URLback=~s|^/~|/priv/|;
 1150: 	}
 1151:     }
 1152: 
 1153:     &Apache::lonnet::appenv('cgi.'.$identifier.'.file'   => $filename,
 1154:                             'cgi.'.$identifier.'.layout'  => $laystyle,
 1155:                             'cgi.'.$identifier.'.numcol'  => $numberofcolumns,
 1156:                             'cgi.'.$identifier.'.selection' => $selectionmade,
 1157: 			    'cgi.'.$identifier.'tableofcontents' => $helper->{'VARS'}->{'TABLE_CONTENTS'},
 1158: 			    'cgi.'.$identifier.'tableofindex' => $helper->{'VARS'}->{'TABLE_INDEX'},
 1159: 			    'cgi.'.$identifier.'role' => $ENV{'request.role.adv'},
 1160:                             'cgi.'.$identifier.'numberoffiles' => $#print_array,
 1161:                             'cgi.'.$identifier.'studentnames' => $student_names,
 1162:                             'cgi.'.$identifier.'backref' => $URLback,);
 1163:  
 1164: $r->print(<<FINALEND);
 1165: <meta http-equiv="Refresh" content="0; url=/cgi-bin/printout.pl?$identifier">
 1166: </body>
 1167: </html>
 1168: FINALEND
 1169: }
 1170: 
 1171: 
 1172: sub print_resources {
 1173:     my ($r,$helper,$person,$type,$moreenv,$master_seq,$remove_latex_header)=@_;
 1174:     my $current_output = ''; 
 1175:     my ($username,$userdomain,$usersection) = split /:/,$person;
 1176:     my $fullname = &get_name($username,$userdomain);
 1177:     
 1178:     #goes through all resources, checks if they are available for 
 1179:     #current student, and produces output   
 1180:     &Apache::lonnet::delenv('form.counter');
 1181:     &Apache::lonxml::init_counter();
 1182:     foreach my $curresline (@{$master_seq})  {
 1183: 	if ( !($type eq 'problems' && 
 1184: 	       ($curresline!~ m/\.(problem|exam|quiz|assess|survey|form|library)$/)) ) {
 1185: 	    my ($map,$id,$res_url) = &Apache::lonnet::decode_symb($curresline);
 1186: 	    if (&Apache::lonnet::allowed('bre',$res_url)) {
 1187: 		my $rendered = &Apache::loncommon::get_student_view($curresline,$username,$userdomain,$ENV{'request.course.id'},'tex',$moreenv);
 1188: 		my $lonidsdir=$r->dir_config('lonIDsDir');
 1189: 		my $envfile=$ENV{'user.environment'};
 1190: 		$envfile=~/\/([^\/]+)\.id$/;
 1191: 		$envfile=$1;
 1192: 		&Apache::lonnet::transfer_profile_to_env($lonidsdir,$envfile);
 1193: 		my $current_counter=$ENV{'form.counter'};
 1194: 		if(($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') ||
 1195: 		   ($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'only')) {
 1196: 		    my %form;
 1197: 		    $form{'answer_output_mode'}='tex';
 1198: 		    $form{'latex_type'}=$helper->{'VARS'}->{'LATEX_TYPE'};
 1199: 		    my $ansrendered = &Apache::loncommon::get_student_answers($curresline,$username,$userdomain,$ENV{'request.course.id'},%form);
 1200: 		    &Apache::lonnet::appenv(('form.counter' => $current_counter));
 1201: 		    if ($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') {
 1202: 			$rendered=~s/(\\keephidden{ENDOFPROBLEM})/$ansrendered$1/;
 1203: 		    } else {
 1204: 			$rendered=&print_latex_header($helper->{'VARS'}->{'LATEX_TYPE'});
 1205: 			$rendered.='\vskip 0 mm \noindent\textbf{'.&Apache::lonnet::gettitle($curresline).'}\vskip 0 mm ';
 1206: 			$rendered.=&path_to_problem ($curresline,$LaTeXwidth);
 1207: 			$rendered.='\vskip 1 mm '.$ansrendered;
 1208: 		    }
 1209: 		}
 1210: 		if ($remove_latex_header eq 'YES') {
 1211: 		    $rendered = &latex_header_footer_remove($rendered);
 1212: 		} else {
 1213: 		    $rendered =~ s/\\end{document}//;
 1214: 		}
 1215: 		$current_output .= $rendered;
 1216: 	    }
 1217: 	    $remove_latex_header = 'YES';
 1218: 	}
 1219:     }
 1220:     my $courseidinfo = &get_course();
 1221:     if (defined($courseidinfo)) { $courseidinfo=' - '.$courseidinfo }
 1222:     if ($usersection ne '') {$courseidinfo.=' - Sec. '.$usersection}
 1223:     my $currentassignment=&Apache::lonxml::latex_special_symbols($helper->{VARS}->{'assignment'},'header');
 1224:     if ($current_output=~/\\documentclass/) {
 1225: 	$current_output =~ s/\\begin{document}/\\setlength{\\topmargin}{1cm} \\begin{document}\\noindent\\lhead{\\textit{\\textbf{$fullname}}$courseidinfo \\hfill \\thepage \\\\ \\textit{$currentassignment}}\\vskip 3 mm /;
 1226:     } else {
 1227: 	my $blankpages = '';
 1228: 	for (my $j=0;$j<$helper->{'VARS'}->{'EMPTY_PAGES'};$j++) {$blankpages.='\clearpage\strut\clearpage';}
 1229: 	$current_output = '\strut\\newline\\noindent\\makebox[\\textwidth/$number_of_columns][b]{\\hrulefill}\\newline\\noindent{\\tiny Printed from LON-CAPA\\copyright MSU{\\hfill} Licensed under GNU General Public License }\\newpage '.$blankpages.'\setcounter{page}{1}\noindent\parbox{\minipagewidth}{\noindent\\lhead{\\textit{\\textbf{'.$fullname.'}}'.$courseidinfo.' \\hfill \\thepage \\\\ \\textit{'.$currentassignment.'}}} \vskip -5 mm '.$current_output;
 1230:     }
 1231:     return ($current_output,$fullname);
 1232: 
 1233: }
 1234: 
 1235: sub handler {
 1236: 
 1237:     my $r = shift;
 1238:     my $helper;
 1239: 
 1240: #    my $loaderror=&Apache::lonnet::overloaderror($r);
 1241: #    if ($loaderror) { return $loaderror; }
 1242: #    $loaderror=
 1243: #       &Apache::lonnet::overloaderror($r,
 1244: #         $ENV{'course.'.$ENV{'request.course.id'}.'.home'});
 1245: #    if ($loaderror) { return $loaderror; }
 1246: 
 1247:     my $result = printHelper($r);
 1248:     if (!ref($result)) {
 1249: 	return $result;
 1250:     }
 1251:     $helper = $result;
 1252:    
 1253: #	my $key; 
 1254: #	foreach $key (keys %{$helper->{'VARS'}}) {
 1255: #	    $r->print(' '.$key.'->'.$helper->{'VARS'}->{$key}.'<-<br />');
 1256: #	}
 1257: #	foreach $key (keys %ENV) {
 1258: #	    $r->print(' '.$key.'->'.$ENV{$key}.'<-<br />');
 1259: #	}
 1260: #	return OK;
 1261: 
 1262:     my %parmhash=&Apache::lonnet::coursedescription($ENV{'request.course.id'});
 1263:     
 1264: #	my $key; 
 1265: #	foreach $key (keys %parmhash) {
 1266: #	    $r->print(' '.$key.'->'.$parmhash{$key}.'<-<br />');
 1267: #	}
 1268: #	return OK;
 1269: 
 1270:     &output_data($r,$helper,\%parmhash);
 1271:     return OK;
 1272: } 
 1273: 
 1274: use Apache::lonhelper;
 1275: 
 1276: sub addMessage {
 1277:     my $text = shift;
 1278:     my $paramHash = Apache::lonhelper::getParamHash();
 1279:     $paramHash->{MESSAGE_TEXT} = $text;
 1280:     Apache::lonhelper::message->new();
 1281: }
 1282: 
 1283: use Data::Dumper;
 1284: 
 1285: sub printHelper {
 1286:     my $r = shift;
 1287: 
 1288:     if ($ENV{'request.course.id'}) {
 1289: 	my $fn=$ENV{'request.course.fn'};
 1290: 	tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER(),0640);
 1291:     }
 1292: 
 1293:     if ($r->header_only) {
 1294:         if ($ENV{'browser.mathml'}) {
 1295:             &Apache::loncommon::content_type($r,'text/xml');
 1296:         } else {
 1297:             &Apache::loncommon::content_type($r,'text/html');
 1298:         }
 1299:         $r->send_http_header;
 1300:         return OK;
 1301:     }
 1302: 
 1303:     # Send header, nocache
 1304:     if ($ENV{'browser.mathml'}) {
 1305:         &Apache::loncommon::content_type($r,'text/xml');
 1306:     } else {
 1307:         &Apache::loncommon::content_type($r,'text/html');
 1308:     }
 1309:     &Apache::loncommon::no_cache($r);
 1310:     $r->send_http_header;
 1311:     $r->rflush();
 1312: 
 1313:     # Unfortunately, this helper is so complicated we have to
 1314:     # write it by hand
 1315: 
 1316:     Apache::loncommon::get_unprocessed_cgi($ENV{QUERY_STRING});
 1317:     
 1318:     my $helper = Apache::lonhelper::helper->new("Printing Helper");
 1319:     $helper->declareVar('symb');
 1320:     $helper->declareVar('postdata');    
 1321:     $helper->declareVar('curseed');    
 1322:     $helper->declareVar('filename');
 1323:     $helper->declareVar('construction');
 1324:     $helper->declareVar('assignment');
 1325:     $helper->declareVar('style_file');
 1326:     
 1327:     # This will persistently load in the data we want from the
 1328:     # very first screen.
 1329:     # Detect whether we're coming from construction space
 1330:     if ($ENV{'form.postdata'}=~/^(?:http:\/\/[^\/]+\/|\/|)\~([^\/]+)\/(.*)$/) {
 1331:         $helper->{VARS}->{'filename'} = "~$1/$2";
 1332:         $helper->{VARS}->{'construction'} = 1;
 1333:     } else {
 1334:         if ($ENV{'form.postdata'}) {
 1335:             $helper->{VARS}->{'symb'} = &Apache::lonnet::symbread($ENV{'form.postdata'});
 1336:         }
 1337:         if ($ENV{'form.symb'}) {
 1338:             $helper->{VARS}->{'symb'} = $ENV{'form.symb'};
 1339:         }
 1340:         if ($ENV{'form.url'}) {
 1341:             $helper->{VARS}->{'symb'} = &Apache::lonnet::symbread($helper->{VARS}->{'postdata'});
 1342:         }
 1343:     }
 1344: 
 1345:     if ($ENV{'form.symb'}) {
 1346:         $helper->{VARS}->{'symb'} = $ENV{'form.symb'};
 1347:     }
 1348:     if ($ENV{'form.url'}) {
 1349:         $helper->{VARS}->{'symb'} = &Apache::lonnet::symbread($helper->{VARS}->{'postdata'});
 1350: 
 1351:     }
 1352:     
 1353:     my ($resourceTitle,$sequenceTitle,$mapTitle) = &details_for_menu;
 1354:     if ($sequenceTitle ne '') {$helper->{VARS}->{'assignment'}=$sequenceTitle;}
 1355: 
 1356:     
 1357:     # Extract map
 1358:     my $symb = $helper->{VARS}->{'symb'};
 1359:     my ($map, $id, $url);
 1360:     my $subdir;
 1361: 
 1362:     # Get the resource name from construction space
 1363:     if ($helper->{VARS}->{'construction'}) {
 1364:         $resourceTitle = substr($helper->{VARS}->{'filename'}, 
 1365:                                 rindex($helper->{VARS}->{'filename'}, '/')+1);
 1366:         $subdir = substr($helper->{VARS}->{'filename'},
 1367:                          0, rindex($helper->{VARS}->{'filename'}, '/') + 1);
 1368:     } else {
 1369:         ($map, $id, $url) = &Apache::lonnet::decode_symb($symb);
 1370:         $helper->{VARS}->{'postdata'} = Apache::lonnet::clutter($url);
 1371: 
 1372:         if (!$resourceTitle) { # if the resource doesn't have a title, use the filename
 1373:             my $postdata = $helper->{VARS}->{'postdata'};
 1374:             $resourceTitle = substr($postdata, rindex($postdata, '/') + 1);
 1375:         }
 1376:         $subdir = &Apache::lonnet::filelocation("", $url);
 1377:     }
 1378:     if (!$helper->{VARS}->{'curseed'} && $ENV{'form.curseed'}) {
 1379: 	$helper->{VARS}->{'curseed'}=$ENV{'form.curseed'};
 1380:     }
 1381: 
 1382:     my $userCanSeeHidden = Apache::lonnavmaps::advancedUser();
 1383:     my $userPriviledged = ($ENV{'request.role'}=~m/^cc\./ or
 1384: 			   $ENV{'request.role'}=~m/^in\./ or
 1385: 			   $ENV{'request.role'}=~m/^ta\./);
 1386: 
 1387:     Apache::lonhelper::registerHelperTags();
 1388: 
 1389:     # "Delete everything after the last slash."
 1390:     $subdir =~ s|/[^/]+$||;
 1391:     if (not $helper->{VARS}->{'construction'}) {
 1392: 	$subdir='/home/httpd/html/res/'.$subdir;
 1393:     }
 1394:     # "Remove all duplicate slashes."
 1395:     $subdir =~ s|/+|/|g;
 1396: 
 1397:     # What can be printed is a very dynamic decision based on
 1398:     # lots of factors. So we need to dynamically build this list.
 1399:     # To prevent security leaks, states are only added to the wizard
 1400:     # if they can be reached, which ensures manipulating the form input
 1401:     # won't allow anyone to reach states they shouldn't have permission
 1402:     # to reach.
 1403: 
 1404:     # printChoices is tracking the kind of printing the user can
 1405:     # do, and will be used in a choices construction later.
 1406:     # In the meantime we will be adding states and elements to
 1407:     # the helper by hand.
 1408:     my $printChoices = [];
 1409:     my $paramHash;
 1410: 
 1411:     if ($resourceTitle) {
 1412:         push @{$printChoices}, ["<b><i>$resourceTitle</i></b> (what you just saw on the screen)", 'current_document', 'PAGESIZE'];
 1413:     }
 1414: 
 1415: #    $r->print($helper->{VARS}->{'postdata'});
 1416: 
 1417:     # Useful filter strings
 1418:     my $isProblem = '($res->is_problem()||$res->contains_problem) ';
 1419:     $isProblem .= ' && !$res->randomout()' if !$userCanSeeHidden;
 1420:     my $isProblemOrMap = '$res->is_problem() || $res->contains_problem() || $res->is_sequence()';
 1421:     my $isNotMap = '!$res->is_sequence()';
 1422:     $isNotMap .= ' && !$res->randomout()' if !$userCanSeeHidden;
 1423:     my $isMap = '$res->is_map()';
 1424:     my $symbFilter = '$res->symb()';
 1425:     my $urlValue = '$res->src()';
 1426: 
 1427:     $helper->declareVar('SEQUENCE');
 1428: 
 1429:     # Useful for debugging: Dump the help vars
 1430:     #$r->print(Dumper($helper->{VARS}));
 1431:     #$r->print($map);
 1432: 
 1433:     # If we're in a sequence...
 1434:     if (($helper->{'VARS'}->{'construction'} ne '1') &&
 1435: 	$helper->{VARS}->{'postdata'} &&
 1436: 	$helper->{VARS}->{'assignment'}) {
 1437:         # Allow problems from sequence
 1438:         push @{$printChoices}, ["<b>Problems</b> in <b><i>$sequenceTitle</i></b>", 'map_problems', 'CHOOSE_PROBLEMS'];
 1439:         # Allow all resources from sequence
 1440:         push @{$printChoices}, ["<b>Resources</b> in <b><i>$sequenceTitle</i></b>", 'map_problems_pages', 'CHOOSE_PROBLEMS_HTML'];
 1441: 
 1442:         my $helperFragment = <<HELPERFRAGMENT;
 1443:   <state name="CHOOSE_PROBLEMS" title="Select Problem(s) to print">
 1444:     <message>(mark them then click "next" button) <br /></message>
 1445:     <resource variable="RESOURCES" multichoice="1" toponly='1' addstatus="1"
 1446:               closeallpages="1">
 1447:       <nextstate>PAGESIZE</nextstate>
 1448:       <filterfunc>return $isProblem;</filterfunc>
 1449:       <mapurl>$map</mapurl>
 1450:       <valuefunc>return $symbFilter;</valuefunc>
 1451:       </resource>
 1452:     </state>
 1453: 
 1454:   <state name="CHOOSE_PROBLEMS_HTML" title="Select Resource(s) to print">
 1455:     <message>(mark them then click "next" button) <br /></message>
 1456:     <resource variable="RESOURCES" multichoice="1" toponly='1' addstatus="1"
 1457:               closeallpages="1">
 1458:       <nextstate>PAGESIZE</nextstate>
 1459:       <filterfunc>return $isNotMap;</filterfunc>
 1460:       <mapurl>$map</mapurl>
 1461:       <valuefunc>return $symbFilter;</valuefunc>
 1462:       </resource>
 1463:     </state>
 1464: HELPERFRAGMENT
 1465: 
 1466:         &Apache::lonxml::xmlparse($r, 'helper', $helperFragment); 
 1467:     }
 1468: 
 1469:     # If the user is priviledged, allow them to print all 
 1470:     # problems in the course, optionally for selected students
 1471:     if ($userPriviledged && ($helper->{VARS}->{'postdata'}=~/\/res\//)) { 
 1472:         push @{$printChoices}, ['<b>Problems</b> from <b>entire course</b>', 'all_problems', 'ALL_PROBLEMS'];
 1473:          &Apache::lonxml::xmlparse($r, 'helper', <<ALL_PROBLEMS);
 1474:   <state name="ALL_PROBLEMS" title="Select Problem(s) to print">
 1475:     <message>(mark them then click "next" button) <br /></message>
 1476:     <resource variable="RESOURCES" toponly='0' multichoice="1"
 1477: 	suppressEmptySequences='0' addstatus="1" closeallpages="1">
 1478:       <nextstate>PAGESIZE</nextstate>
 1479:       <filterfunc>return $isProblemOrMap;</filterfunc>
 1480:       <choicefunc>return $isNotMap;</choicefunc>
 1481:       <valuefunc>return $symbFilter;</valuefunc>
 1482:     </resource>
 1483:   </state>
 1484: ALL_PROBLEMS
 1485: 
 1486: 	if ($helper->{VARS}->{'assignment'}) {
 1487: 	    push @{$printChoices}, ["<b>Problems</b> from <b><i>$sequenceTitle</i></b> for <b>selected students</b>", 'problems_for_students', 'CHOOSE_STUDENTS'];
 1488: 	    push @{$printChoices}, ["<b>Problems</b> from <b><i>$sequenceTitle</i></b> for <b>anonymous students</b>", 'problems_for_anonymous', 'CHOOSE_ANON1'];
 1489: 	}
 1490: 	my $resource_selector=<<RESOURCE_SELECTOR;
 1491:    <message><br /><big><i><b>Select resources for the assignment</b></i></big><br /></message>
 1492:     <resource variable="RESOURCES" multichoice="1" addstatus="1" 
 1493:               closeallpages="1">
 1494:       <filterfunc>return $isProblem;</filterfunc>
 1495:       <mapurl>$map</mapurl>
 1496:       <valuefunc>return $symbFilter;</valuefunc>
 1497:       </resource>
 1498:     <message><br /><big><i><b>How should the results be printed?</b></i></big><br /></message>
 1499:     <choices variable="EMPTY_PAGES">
 1500:       <choice computer='0'>Start each student\'s assignment on a new page/column (add a pagefeed after each assignment)</choice>
 1501:       <choice computer='1'>Add one empty page/column after each student\'s assignment</choice>
 1502:       <choice computer='2'>Add two empty pages/column after each student\'s assignment</choice>
 1503:       <choice computer='3'>Add three empty pages/column after each student\'s assignment</choice>
 1504:     </choices>
 1505:     <message><hr width='33%' /><b>Number of assignments printed at the same time: </b></message>
 1506:     <string variable="NUMBER_TO_PRINT" maxlength="5" size="5"><defaultvalue>"all"</defaultvalue></string>
 1507: RESOURCE_SELECTOR
 1508: 
 1509:         &Apache::lonxml::xmlparse($r, 'helper', <<CHOOSE_STUDENTS);
 1510:   <state name="CHOOSE_STUDENTS" title="Select Students and Resources">
 1511:       <student multichoice='1' variable="STUDENTS" nextstate="PAGESIZE" />
 1512:     $resource_selector
 1513:   </state>
 1514: CHOOSE_STUDENTS
 1515:         &Apache::lonxml::xmlparse($r, 'helper', <<CHOOSE_ANON1);
 1516:   <state name="CHOOSE_ANON1" title="Select Students and Resources">
 1517:     <message><hr width='33%' /><b>Number of anonymous assignments to print?</b></message>
 1518:     <string variable="NUMBER_TO_PRINT_TOTAL" maxlength="5" size="5"></string>
 1519:     
 1520:     <message><hr width='33%' /></message>
 1521:     $resource_selector
 1522:   </state>
 1523: CHOOSE_ANON1
 1524: 
 1525: 
 1526: 	if ($helper->{VARS}->{'assignment'}) {
 1527: 	    push @{$printChoices}, ["<b>Resources</b> from <b><i>$sequenceTitle</i></b> for <b>selected students</b>", 'resources_for_students', 'CHOOSE_STUDENTS1'];
 1528: 	    push @{$printChoices}, ["<b>Resources</b> from <b><i>$sequenceTitle</i></b> for <b>anonymous students</b>", 'resources_for_anonymous', 'CHOOSE_ANON2'];
 1529: 	}
 1530: 	    
 1531: 
 1532: 	$resource_selector=<<RESOURCE_SELECTOR;
 1533:     <message><br /><big><i><b>Select resources for the assignment</b></i></big><br /></message>
 1534:     <resource variable="RESOURCES" multichoice="1" addstatus="1" 
 1535:               closeallpages="1">
 1536:       <filterfunc>return $isNotMap;</filterfunc>
 1537:       <mapurl>$map</mapurl>
 1538:       <valuefunc>return $symbFilter;</valuefunc>
 1539:       </resource>
 1540:     <message><br /><big><i><b>How should the results be printed?</b></i></big><br /></message>
 1541:     <choices variable="EMPTY_PAGES">
 1542:       <choice computer='0'>Start each student\'s assignment on a new page/column (add a pagefeed after each assignment)</choice>
 1543:       <choice computer='1'>Add one empty page/column after each student\'s assignment</choice>
 1544:       <choice computer='2'>Add two empty pages/column after each student\'s assignment</choice>
 1545:       <choice computer='3'>Add three empty pages/column after each student\'s assignment</choice>
 1546:     </choices>
 1547:     <message><hr width='33%' /><b>Number of assignments printed at the same time: </b></message>
 1548:     <string variable="NUMBER_TO_PRINT" maxlength="5" size="5"><defaultvalue>"all"</defaultvalue></string>
 1549: RESOURCE_SELECTOR
 1550: 
 1551: 	&Apache::lonxml::xmlparse($r, 'helper', <<CHOOSE_STUDENTS1);
 1552:   <state name="CHOOSE_STUDENTS1" title="Select Students and Resources">
 1553:     <student multichoice='1' variable="STUDENTS" nextstate="PAGESIZE" />
 1554:     $resource_selector
 1555:     </state>
 1556: CHOOSE_STUDENTS1
 1557: 
 1558: 	&Apache::lonxml::xmlparse($r, 'helper', <<CHOOSE_ANON2);
 1559:   <state name="CHOOSE_ANON2" title="Select Students and Resources">
 1560:     <message><hr width='33%' /><b>How many Anonymous exams to print?</b></message>
 1561:     <string variable="NUMBER_TO_PRINT_TOTAL" maxlength="5" size="5"></string>
 1562:     <message><hr width='33%' /></message>
 1563:     $resource_selector
 1564:   </state>
 1565: CHOOSE_ANON2
 1566:     }
 1567: 
 1568:     # FIXME: That RE should come from a library somewhere.
 1569:     if ((((&Apache::lonnet::allowed('bre',$subdir) eq 'F') and ($helper->{VARS}->{'postdata'}=~/\.(problem|exam|quiz|assess|survey|form|library|page|xml|html|htm|xhtml|xhtm)/)) or defined $helper->{'VARS'}->{'construction'}) and $ENV{'request.role.adv'} and $subdir ne '/home/httpd/html/res/') {    
 1570:         push @{$printChoices}, ["<b>Problems</b> from current subdirectory <b><i>$subdir</i></b>", 'problems_from_directory', 'CHOOSE_FROM_SUBDIR'];
 1571: 
 1572:         my $f = '$filename';
 1573:         my $xmlfrag = <<CHOOSE_FROM_SUBDIR;
 1574:   <state name="CHOOSE_FROM_SUBDIR" title="Select File(s) from <b><small>$subdir</small></b> to print">
 1575:     <message>(mark them then click "next" button) <br /></message>
 1576:     <files variable="FILES" multichoice='1'>
 1577:       <nextstate>PAGESIZE</nextstate>
 1578:       <filechoice>return '$subdir';</filechoice>
 1579: CHOOSE_FROM_SUBDIR
 1580:         
 1581:         # this is broken up because I really want interpolation above,
 1582:         # and I really DON'T want it below
 1583:         $xmlfrag .= <<'CHOOSE_FROM_SUBDIR';
 1584:       <filefilter>return Apache::lonhelper::files::not_old_version($filename) &&
 1585: 	  $filename =~ m/\.(problem|exam|quiz|assess|survey|form|library)$/;
 1586:       </filefilter>
 1587:       </files>
 1588:     </state>
 1589: CHOOSE_FROM_SUBDIR
 1590:         &Apache::lonxml::xmlparse($r, 'helper', $xmlfrag);
 1591:     }
 1592: 
 1593:     # Allow the user to select any sequence in the course, feed it to
 1594:     # another resource selector for that sequence
 1595:     if (!$helper->{VARS}->{'construction'}) {
 1596: 	push @$printChoices, ["<b>Resources</b> from <b>selected sequence</b> in course",
 1597: 			      'select_sequences', 'CHOOSE_SEQUENCE'];
 1598: 	my $escapedSequenceName = $helper->{VARS}->{'SEQUENCE'};
 1599: 	#Escape apostrophes and backslashes for Perl
 1600: 	$escapedSequenceName =~ s/\\/\\\\/g;
 1601: 	$escapedSequenceName =~ s/'/\\'/g;
 1602: 	&Apache::lonxml::xmlparse($r, 'helper', <<CHOOSE_FROM_ANY_SEQUENCE);
 1603:   <state name="CHOOSE_SEQUENCE" title="Select Sequence To Print From">
 1604:     <message>Select the sequence to print resources from:</message>
 1605:     <resource variable="SEQUENCE">
 1606:       <nextstate>CHOOSE_FROM_ANY_SEQUENCE</nextstate>
 1607:       <filterfunc>return \$res->is_sequence;</filterfunc>
 1608:       <valuefunc>return $urlValue;</valuefunc>
 1609:       </resource>
 1610:     </state>
 1611:   <state name="CHOOSE_FROM_ANY_SEQUENCE" title="Select Resources To Print">
 1612:     <message>(mark desired resources then click "next" button) <br /></message>
 1613:     <resource variable="RESOURCES" multichoice="1" toponly='1' addstatus="1"
 1614:               closeallpages="1">
 1615:       <nextstate>PAGESIZE</nextstate>
 1616:       <filterfunc>return $isProblem</filterfunc>
 1617:       <mapurl evaluate='1'>return '$escapedSequenceName';</mapurl>
 1618:       <valuefunc>return $symbFilter;</valuefunc>
 1619:       </resource>
 1620:     </state>
 1621: CHOOSE_FROM_ANY_SEQUENCE
 1622: }
 1623: 
 1624:     # Generate the first state, to select which resources get printed.
 1625:     Apache::lonhelper::state->new("START", "Select Printing Options:");
 1626:     $paramHash = Apache::lonhelper::getParamHash();
 1627:     $paramHash->{MESSAGE_TEXT} = "";
 1628:     Apache::lonhelper::message->new();
 1629:     $paramHash = Apache::lonhelper::getParamHash();
 1630:     $paramHash->{'variable'} = 'PRINT_TYPE';
 1631:     $helper->declareVar('PRINT_TYPE');
 1632:     $paramHash->{CHOICES} = $printChoices;
 1633:     Apache::lonhelper::choices->new();
 1634: 
 1635:     my $startedTable = 0; # have we started an HTML table yet? (need
 1636:                           # to close it later)
 1637: 
 1638:     if (($ENV{'request.role.adv'} and &Apache::lonnet::allowed('vgr',$ENV{'request.course.id'})) or 
 1639: 	($helper->{VARS}->{'construction'} eq '1')) {
 1640: 	addMessage("<hr width='33%' /><table><tr><td align='right'>Print: </td><td>");
 1641:         $paramHash = Apache::lonhelper::getParamHash();
 1642: 	$paramHash->{'variable'} = 'ANSWER_TYPE';   
 1643: 	$helper->declareVar('ANSWER_TYPE');         
 1644:         $paramHash->{CHOICES} = [
 1645:                                    ['Without Answers', 'yes'],
 1646:                                    ['With Answers', 'no'],
 1647:                                    ['Only Answers', 'only'] ];
 1648:         Apache::lonhelper::dropdown->new();
 1649: 	addMessage("</td></tr>");
 1650: 	$startedTable = 1;
 1651:     }
 1652: 
 1653:     if ($ENV{'request.role.adv'}) {
 1654: 	if (!$startedTable) {
 1655: 	    addMessage("<hr width='33%' /><table><tr><td align='right'>LaTeX mode: </td><td>");
 1656: 	    $startedTable = 1;
 1657: 	} else {
 1658: 	    addMessage("<tr><td align='right'>LaTeX mode: </td><td>");
 1659: 	}
 1660:         $paramHash = Apache::lonhelper::getParamHash();
 1661: 	$paramHash->{'variable'} = 'LATEX_TYPE';   
 1662: 	$helper->declareVar('LATEX_TYPE');  
 1663: 	if ($helper->{VARS}->{'construction'} eq '1') {       
 1664: 	    $paramHash->{CHOICES} = [
 1665: 				     ['standard LaTeX mode', 'standard'], 
 1666: 				     ['LaTeX batchmode', 'batchmode'], ];
 1667: 	} else {
 1668: 	    $paramHash->{CHOICES} = [
 1669: 				     ['LaTeX batchmode', 'batchmode'],
 1670: 				     ['standard LaTeX mode', 'standard'] ];
 1671: 	}
 1672:         Apache::lonhelper::dropdown->new();
 1673:  
 1674: 	addMessage("</td></tr><tr><td align='right'>Print Table of Contents: </td><td>");
 1675:         $paramHash = Apache::lonhelper::getParamHash();
 1676: 	$paramHash->{'variable'} = 'TABLE_CONTENTS';   
 1677: 	$helper->declareVar('TABLE_CONTENTS');         
 1678:         $paramHash->{CHOICES} = [
 1679:                                    ['No', 'no'],
 1680:                                    ['Yes', 'yes'] ];
 1681:         Apache::lonhelper::dropdown->new();
 1682: 	addMessage("</td></tr>");
 1683:         
 1684: 	if (not $helper->{VARS}->{'construction'}) {
 1685: 	    addMessage("<tr><td align='right'>Print Index: </td><td>");
 1686: 	    $paramHash = Apache::lonhelper::getParamHash();
 1687: 	    $paramHash->{'variable'} = 'TABLE_INDEX';   
 1688: 	    $helper->declareVar('TABLE_INDEX');         
 1689: 	    $paramHash->{CHOICES} = [
 1690: 				     ['No', 'no'],
 1691: 				     ['Yes', 'yes'] ];
 1692: 	    Apache::lonhelper::dropdown->new();
 1693: 	    addMessage("</td></tr>");
 1694: 	}
 1695: 
 1696: 	if ($helper->{'VARS'}->{'construction'}) { 
 1697: 	    my $stylevalue=$ENV{'construct.style'};
 1698: 	    my $xmlfrag .= <<"RNDSEED";
 1699: 	    <message><tr><td align='right'>Use random seed: </td><td></message>
 1700: 	    <string variable="curseed" size="15" maxlength="15">
 1701: 		<defaultvalue>
 1702: 	            return $helper->{VARS}->{'curseed'};
 1703: 	        </defaultvalue>
 1704: 	    </string>
 1705: 	     <message></td></tr><tr><td align="right">Use style file:</td><td></message>
 1706:              <message><input type="text" size="40" name="style_file_value" value="$stylevalue"  />&nbsp; <a href="javascript:openbrowser('helpform','style_file','sty')">Select style file</a> </td><td></message>
 1707:              <message></td></tr></message>
 1708: RNDSEED
 1709:             &Apache::lonxml::xmlparse($r, 'helper', $xmlfrag);
 1710: 	    $helper->{'VARS'}->{'style_file'}=$ENV{'form.style_file_value'};
 1711: 	}
 1712:     }
 1713: 
 1714: 
 1715: 
 1716: 
 1717:     if ($startedTable) {
 1718: 	addMessage("</table>");
 1719:     }
 1720: 
 1721:     Apache::lonprintout::page_format_state->new("FORMAT");
 1722: 
 1723:     # Generate the PAGESIZE state which will offer the user the margin
 1724:     # choices if they select one column
 1725:     Apache::lonhelper::state->new("PAGESIZE", "Set Margins");
 1726:     Apache::lonprintout::page_size_state->new('pagesize', 'FORMAT', 'FINAL');
 1727: 
 1728: 
 1729:     $helper->process();
 1730: 
 1731:     # MANUAL BAILOUT CONDITION:
 1732:     # If we're in the "final" state, bailout and return to handler
 1733:     if ($helper->{STATE} eq 'FINAL') {
 1734:         return $helper;
 1735:     }    
 1736: 
 1737:     $r->print($helper->display());
 1738: 
 1739:     Apache::lonhelper::unregisterHelperTags();
 1740: 
 1741:     untie %hash;
 1742: 
 1743:     return OK;
 1744: }
 1745: 
 1746: 
 1747: 1;
 1748: 
 1749: package Apache::lonprintout::page_format_state;
 1750: 
 1751: =pod
 1752: 
 1753: =head1 Helper element: page_format_state
 1754: 
 1755: See lonhelper.pm documentation for discussion of the helper framework.
 1756: 
 1757: Apache::lonprintout::page_format_state is an element that gives the 
 1758: user an opportunity to select the page layout they wish to print 
 1759: with: Number of columns, portrait/landscape, and paper size. If you 
 1760: want to change the paper size choices, change the @paperSize array 
 1761: contents in this package.
 1762: 
 1763: page_format_state is always directly invoked in lonprintout.pm, so there
 1764: is no tag interface. You actually pass parameters to the constructor.
 1765: 
 1766: =over 4
 1767: 
 1768: =item * B<new>(varName): varName is where the print information will be stored in the format FIXME.
 1769: 
 1770: =back
 1771: 
 1772: =cut
 1773: 
 1774: use Apache::lonhelper;
 1775: 
 1776: no strict;
 1777: @ISA = ("Apache::lonhelper::element");
 1778: use strict;
 1779: use Apache::lonlocal;
 1780: 
 1781: my $maxColumns = 2;
 1782: my @paperSize = ("Letter [8 1/2x11 in]", "Legal [8 1/2x14 in]", 
 1783:                  "Ledger/Tabloid [11x17 in]", "Executive [7 1/2x10 in]",
 1784:                  "A2 [420x594 mm]", "A3 [297x420 mm]", "A4 [210x297 mm]", 
 1785:                  "A5 [148x210 mm]", "A6 [105x148 mm]" );
 1786: 
 1787: # Tentative format: Orientation (L = Landscape, P = portrait) | Colnum |
 1788: #                   Paper type
 1789: 
 1790: sub new { 
 1791:     my $self = Apache::lonhelper::element->new();
 1792: 
 1793:     shift;
 1794: 
 1795:     $self->{'variable'} = shift;
 1796:     my $helper = Apache::lonhelper::getHelper();
 1797:     $helper->declareVar($self->{'variable'});
 1798:     bless($self);
 1799:     return $self;
 1800: }
 1801: 
 1802: sub render {
 1803:     my $self = shift;
 1804:     my $helper = Apache::lonhelper::getHelper();
 1805:     my $result = '';
 1806:     my $var = $self->{'variable'};
 1807:     my $PageLayout=&mt('Page layout');
 1808:     my $NumberOfColumns=&mt('Number of columns');
 1809:     my $PaperType=&mt('Paper type');
 1810:     $result .= <<STATEHTML;
 1811: 
 1812: <hr width="33%" />
 1813: <table cellpadding="3">
 1814:   <tr>
 1815:     <td align="center"><b>$PageLayout</b></td>
 1816:     <td align="center"><b>$NumberOfColumns</b></td>
 1817:     <td align="center"><b>$PaperType</b></td>
 1818:   </tr>
 1819:   <tr>
 1820:     <td>
 1821:       <input type="radio" name="${var}.layout" value="L" /> Landscape<br />
 1822:       <input type="radio" name="${var}.layout" value="P" checked='1'  /> Portrait
 1823:     </td>
 1824:     <td align="center">
 1825:       <select name="${var}.cols">
 1826: STATEHTML
 1827: 
 1828:     my $i;
 1829:     for ($i = 1; $i <= $maxColumns; $i++) {
 1830:         if ($i == 2) {
 1831:             $result .= "<option value='$i' selected>$i</option>\n";
 1832:         } else {
 1833:             $result .= "<option value='$i'>$i</option>\n";
 1834:         }
 1835:     }
 1836: 
 1837:     $result .= "</select></td><td>\n";
 1838:     $result .= "<select name='${var}.paper'>\n";
 1839: 
 1840:     $i = 0;
 1841:     foreach (@paperSize) {
 1842:         if ($i == 0) {
 1843:             $result .= "<option selected value='$i'>" . $paperSize[$i] . "</option>\n";
 1844:         } else {
 1845:             $result .= "<option value='$i'>" . $paperSize[$i] . "</option>\n";
 1846:         }
 1847:         $i++;
 1848:     }
 1849: 
 1850:     $result .= "</select></td></tr></table>";
 1851:     return $result;
 1852: }
 1853: 
 1854: sub postprocess {
 1855:     my $self = shift;
 1856: 
 1857:     my $var = $self->{'variable'};
 1858:     my $helper = Apache::lonhelper->getHelper();
 1859:     $helper->{VARS}->{$var} = 
 1860:         $ENV{"form.$var.layout"} . '|' . $ENV{"form.$var.cols"} . '|' .
 1861:         $ENV{"form.$var.paper"};
 1862:     return 1;
 1863: }
 1864: 
 1865: 1;
 1866: 
 1867: package Apache::lonprintout::page_size_state;
 1868: 
 1869: =pod
 1870: 
 1871: =head1 Helper element: page_size_state
 1872: 
 1873: See lonhelper.pm documentation for discussion of the helper framework.
 1874: 
 1875: Apache::lonprintout::page_size_state is an element that gives the 
 1876: user the opportunity to further refine the page settings if they
 1877: select a single-column page.
 1878: 
 1879: page_size_state is always directly invoked in lonprintout.pm, so there
 1880: is no tag interface. You actually pass parameters to the constructor.
 1881: 
 1882: =over 4
 1883: 
 1884: =item * B<new>(varName): varName is where the print information will be stored in the format FIXME.
 1885: 
 1886: =back
 1887: 
 1888: =cut
 1889: 
 1890: use Apache::lonhelper;
 1891: 
 1892: no strict;
 1893: @ISA = ("Apache::lonhelper::element");
 1894: use strict;
 1895: 
 1896: 
 1897: 
 1898: sub new { 
 1899:     my $self = Apache::lonhelper::element->new();
 1900: 
 1901:     shift; # disturbs me (probably prevents subclassing) but works (drops
 1902:            # package descriptor)... - Jeremy
 1903: 
 1904:     $self->{'variable'} = shift;
 1905:     my $helper = Apache::lonhelper::getHelper();
 1906:     $helper->declareVar($self->{'variable'});
 1907: 
 1908:     # The variable name of the format element, so we can look into 
 1909:     # $helper->{VARS} to figure out whether the columns are one or two
 1910:     $self->{'formatvar'} = shift;
 1911: 
 1912:     # The state to transition to after selection, or after discovering
 1913:     # the cols are not set to 1
 1914:     $self->{NEXTSTATE} = shift;
 1915:     bless($self);
 1916:     return $self;
 1917: }
 1918: 
 1919: sub render {
 1920:     my $self = shift;
 1921:     my $helper = Apache::lonhelper::getHelper();
 1922:     my $result = '';
 1923:     my $var = $self->{'variable'};
 1924: 
 1925:     if (defined $self->{ERROR_MSG}) {
 1926:         $result .= '<br /><font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br />';
 1927:     }
 1928: 
 1929:     $result .= <<ELEMENTHTML;
 1930: 
 1931: <p>How should the column be formatted?</p>
 1932: 
 1933: <table cellpadding='3'>
 1934:   <tr>
 1935:     <td align='right'><b>Width</b>:</td>
 1936:     <td align='left'><input type='text' name='$var.width' value='18' size='4'></td>
 1937:     <td align='left'>
 1938:       <select name='$var.widthunit'>
 1939:         <option>cm</option><option>in</option>
 1940:       </select>
 1941:     </td>
 1942:   </tr>
 1943:   <tr>
 1944:     <td align='right'><b>Height</b>:</td>
 1945:     <td align='left'><input type='text' name="$var.height" value="25.9" size='4'></td>
 1946:     <td align='left'>
 1947:       <select name='$var.heightunit'>
 1948:         <option>cm</option><option>in</option>
 1949:       </select>
 1950:     </td>
 1951:   </tr>
 1952:   <tr>
 1953:     <td align='right'><b>Left margin</b>:</td>
 1954:     <td align='left'><input type='text' name='$var.lmargin' value='-1.5' size='4'></td>
 1955:     <td align='left'>
 1956:       <select name='$var.lmarginunit'>
 1957:         <option>cm</option><option>in</option>
 1958:       </select>
 1959:     </td>
 1960:   </tr>
 1961: </table>
 1962: 
 1963: <p>Hint: Some instructors like to leave scratch space for the student by
 1964: making the width much smaller than the width of the page.</p>
 1965: 
 1966: ELEMENTHTML
 1967: 
 1968:     return $result;
 1969: }
 1970: 
 1971: # If the user didn't select 1 column, skip this state.
 1972: sub preprocess {
 1973:     my $self = shift;
 1974:     my $helper = Apache::lonhelper::getHelper();
 1975: 
 1976:     my $format = $helper->{VARS}->{$self->{'formatvar'}};
 1977:     if (substr($format, 2, 1) ne '1') {
 1978:         $helper->changeState($self->{NEXTSTATE});
 1979:     }
 1980:    
 1981:     return 1;
 1982: }
 1983: 
 1984: sub postprocess {
 1985:     my $self = shift;
 1986: 
 1987:     my $var = $self->{'variable'};
 1988:     my $helper = Apache::lonhelper->getHelper();
 1989:     my $width = $helper->{VARS}->{$var .'.width'} = $ENV{"form.${var}.width"}; 
 1990:     my $height = $helper->{VARS}->{$var .'.height'} = $ENV{"form.${var}.height"}; 
 1991:     my $lmargin = $helper->{VARS}->{$var .'.lmargin'} = $ENV{"form.${var}.lmargin"}; 
 1992:     $helper->{VARS}->{$var .'.widthunit'} = $ENV{"form.${var}.widthunit"}; 
 1993:     $helper->{VARS}->{$var .'.heightunit'} = $ENV{"form.${var}.heightunit"}; 
 1994:     $helper->{VARS}->{$var .'.lmarginunit'} = $ENV{"form.${var}.lmarginunit"}; 
 1995: 
 1996:     my $error = '';
 1997: 
 1998:     # /^-?[0-9]+(\.[0-9]*)?$/ -> optional minus, at least on digit, followed 
 1999:     # by an optional period, followed by digits, ending the string
 2000: 
 2001:     if ($width !~  /^-?[0-9]+(\.[0-9]*)?$/) {
 2002:         $error .= "Invalid width; please type only a number.<br />\n";
 2003:     }
 2004:     if ($height !~  /^-?[0-9]+(\.[0-9]*)?$/) {
 2005:         $error .= "Invalid height; please type only a number.<br />\n";
 2006:     }
 2007:     if ($lmargin !~  /^-?[0-9]+(\.[0-9]*)?$/) {
 2008:         $error .= "Invalid left margin; please type only a number.<br />\n";
 2009:     }
 2010: 
 2011:     if (!$error) {
 2012:         Apache::lonhelper::getHelper()->changeState($self->{NEXTSTATE});
 2013:         return 1;
 2014:     } else {
 2015:         $self->{ERROR_MSG} = $error;
 2016:         return 0;
 2017:     }
 2018: }
 2019: 
 2020: 
 2021: 
 2022: __END__
 2023: 

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