File:  [LON-CAPA] / loncom / interface / lonprintout.pm
Revision 1.337: download - view: text, annotated - select for diffs
Sat Nov 13 09:30:26 2004 UTC (19 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- sort code set saved names

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

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