File:  [LON-CAPA] / loncom / interface / lonprintout.pm
Revision 1.340: download - view: text, annotated - select for diffs
Mon Dec 13 22:24:03 2004 UTC (19 years, 6 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
Defect 3618
Add radio buttons (as yet with no code behind them) to control the
sort order of print * for selected students etc.

    1: # The LearningOnline Network
    2: # Printout
    3: #
    4: # $Id: lonprintout.pm,v 1.340 2004/12/13 22:24:03 foxr 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','1 cm'],
  378: 	     '2' => ['3.66 in','10.2 in', '-0.57 in','-0.57 in','1 cm']
  379: 	 },
  380: 	 'album' => {
  381: 	     '1' => [ '8.8 in', '6.8 in','-40 pt in',  '-60 pt','1 cm'],
  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\n\\setlength{\\textwidth}{$textwidth}\\setlength{\\textheight}{$textheight}\\setlength{\\textfloatsep}{8pt plus 2\.0pt minus 4\.0pt}\n\\newlength{\\minipagewidth}\\setlength{\\minipagewidth}{\\textwidth\/\$number_of_columns-0\.2cm}\\usepackage{fancyhdr}\\addtolength{\\headheight}{\\baselineskip}\n\\pagestyle{fancy}$fancypagestatement\\begin{document}\\voffset=-0\.8 cm\\setcounter{page}{1}\n /;
  520:     } elsif ($layout eq 'book') {
  521: 	if ($choice ne 'All class print') { 
  522: 	    $text =~ s/\\begin{document}/\\textheight $textheight\\oddsidemargin = $evenoffset\\evensidemargin = $evenoffset $topmargintoinsert\n\\textwidth= $textwidth\\newlength{\\minipagewidth}\\setlength{\\minipagewidth}{\\textwidth\/\$number_of_columns-0\.2cm}\n\\renewcommand{\\ref}{\\keephidden\}\\usepackage{fancyhdr}\\addtolength{\\headheight}{\\baselineskip}\\pagestyle{fancy}$fancypagestatement\\begin{document}\n\\voffset=-0\.8 cm\\setcounter{page}{1}\n/;
  523: 	} else {
  524: 	    $text =~ s/\\pagestyle{fancy}\\rhead{}\\chead{}\s*\\begin{document}/\\textheight = $textheight\\oddsidemargin = $evenoffset\n\\evensidemargin = $evenoffset $topmargintoinsert\\textwidth= $textwidth\\newlength{\\minipagewidth}\n\\setlength{\\minipagewidth}{\\textwidth\/\$number_of_columns-0\.2cm}\\renewcommand{\\ref}{\\keephidden\}\\pagestyle{fancy}\\rhead{}\\chead{}\\begin{document}\\voffset=-0\.8cm\n\\setcounter{page}{1}  \\vskip 5 mm\n /;
  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}$}'."\n".
  647: 	     '\usepackage{longtable}\usepackage{textcomp}\usepackage{makeidx}'."\n".
  648: 	     '\usepackage[dvips]{graphicx}\usepackage{epsfig}\usepackage{calc}'."\n".
  649: 	     '\newenvironment{choicelist}{\begin{list}{}{\setlength{\rightmargin}{0in}'."\n".
  650: 	     '\setlength{\leftmargin}{0.13in}\setlength{\topsep}{0.05in}'."\n".
  651: 	     '\setlength{\itemsep}{0.022in}\setlength{\parsep}{0in}'."\n".
  652: 	     '\setlength{\belowdisplayskip}{0.04in}\setlength{\abovedisplayskip}{0.05in}'."\n".
  653: 	     '\setlength{\abovedisplayshortskip}{-0.04in}'."\n".
  654: 	     '\setlength{\belowdisplayshortskip}{0.04in}}}{\end{list}}'."\n".
  655: 	     '\renewenvironment{theindex}{\begin{list}{}{{\vskip 1mm \noindent \large'."\n".
  656: 	     '\textbf{Index}} \newline \setlength{\rightmargin}{0in}'."\n".
  657: 	     '\setlength{\leftmargin}{0.13in}\setlength{\topsep}{0.01in}'."\n".
  658: 	     '\setlength{\itemsep}{0.1in}\setlength{\parsep}{-0.02in}'."\n".
  659: 	     '\setlength{\belowdisplayskip}{0.01in}\setlength{\abovedisplayskip}{0.01in}'."\n".
  660: 	     '\setlength{\abovedisplayshortskip}{-0.04in}'."\n".
  661: 	     '\setlength{\belowdisplayshortskip}{0.01in}}}{\end{list}}\begin{document}'."\n";
  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: 	    my $texversion='';
  968: 	    if ($urlp=~/\.(problem|exam|quiz|assess|survey|form|library|page|xml|html|htm|xhtml|xhtm)$/) {
  969: 		$texversion.=&Apache::lonnet::ssi($urlp,%form);
  970: 		if ($urlp=~/\.page$/) {
  971: 		    ($texversion,my $number_of_columns_page) = &page_cleanup($texversion);
  972: 		    if ($number_of_columns_page > $number_of_columns) {$number_of_columns=$number_of_columns_page;} 
  973: 		    $texversion =~ s/\\end{document}\d*/\\end{document}/;
  974: 		    $flag_page_in_sequence = 'YES';
  975: 		} 
  976: 		my $lonidsdir=$r->dir_config('lonIDsDir');
  977: 		my $envfile=$ENV{'user.environment'};
  978: 		$envfile=~/\/([^\/]+)\.id$/;
  979: 		$envfile=$1;
  980: 		&Apache::lonnet::transfer_profile_to_env($lonidsdir,$envfile);
  981: 		my $current_counter=$ENV{'form.counter'};
  982: 		if(($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') ||
  983: 		   ($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'only')) {
  984: 		    my %form;
  985: 		    $form{'grade_target'}='answer';
  986: 		    $form{'answer_output_mode'}='tex';
  987: 		    my $answer=&Apache::lonnet::ssi($urlp,%form);
  988: 		    &Apache::lonnet::appenv(('form.counter' => $current_counter));
  989: 		    if ($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') {
  990: 			$texversion=~s/(\\keephidden{ENDOFPROBLEM})/$answer$1/;
  991: 		    } else {
  992: 			if ($urlp=~/\.(problem|exam|quiz|assess|survey|form|library)$/) {
  993: 			    $texversion=&print_latex_header($helper->{'VARS'}->{'LATEX_TYPE'});
  994: 			    $texversion.='\vskip 0 mm \noindent\textbf{'.&Apache::lonnet::gettitle($urlp).'}\vskip 0 mm ';
  995: 			    $texversion.=&path_to_problem ($urlp,$LaTeXwidth);
  996: 			    $texversion.='\vskip 1 mm '.$answer;
  997: 			} else {
  998: 			    $texversion='';
  999: 			}
 1000: 		    }
 1001: 		}
 1002: 		if ($flag_latex_header_remove ne 'NO') {
 1003: 		    $texversion = &latex_header_footer_remove($texversion);
 1004: 		} else {
 1005: 		    $texversion =~ s/\\end{document}//;
 1006: 		}
 1007: 		if ($helper->{'VARS'}->{'TABLE_INDEX'} eq 'yes') {
 1008: 		    $texversion=&IndexCreation($texversion,$urlp);
 1009: 		}
 1010: 		if (($selectionmade == 4) and ($assignment ne $prevassignment)) {
 1011: 		    my $name = &get_name();
 1012: 		    my $courseidinfo = &get_course();
 1013: 		    if (defined($courseidinfo)) { $courseidinfo=' - '.$courseidinfo }
 1014: 		    $prevassignment=$assignment;
 1015: 		    $result .='\newpage \noindent\parbox{\minipagewidth}{\noindent\\lhead{\\textit{\\textbf{'.$name.'}}'.$courseidinfo.' \\hfill \\thepage \\\\ \\textit{'.$assignment.'}}} \vskip 5 mm ';
 1016: 		}
 1017: 		$result .= $texversion;
 1018: 		$flag_latex_header_remove = 'YES';   
 1019: 	    } elsif ($urlp=~/\/(smppg|syllabus|aboutme|bulletinboard)$/) { 
 1020: 		my %form;
 1021: 		$form{'grade_target'}='tex';
 1022: 		$form{'textwidth'}=&get_textwidth($helper,$LaTeXwidth);
 1023: 		$form{'latex_type'}=$helper->{'VARS'}->{'LATEX_TYPE'};
 1024: 		if ($urlp=~/\/syllabus$/) {$urlp=~s/\/res//;}
 1025: 		my $texversion=&Apache::lonnet::ssi($urlp,%form);
 1026: 		if ($flag_latex_header_remove ne 'NO') {
 1027: 		    $texversion = &latex_header_footer_remove($texversion);
 1028: 		} else {
 1029: 		    $texversion =~ s/\\end{document}/\\vskip 0\.5mm\\noindent\\makebox\[\\textwidth\/\$number_of_columns\]\[b\]\{\\hrulefill\}/;
 1030: 		}
 1031: 		$result .= $texversion;
 1032: 		$flag_latex_header_remove = 'YES'; 
 1033: 	    } else {
 1034: 		$texversion=&unsupported($urlp,$helper->{'VARS'}->{'LATEX_TYPE'});
 1035: 		if ($flag_latex_header_remove ne 'NO') {
 1036: 		    $texversion = &latex_header_footer_remove($texversion);
 1037: 		} else {
 1038: 		    $texversion =~ s/\\end{document}//;
 1039: 		}
 1040: 		$result .= $texversion;
 1041: 		$flag_latex_header_remove = 'YES';   
 1042: 	    }	    
 1043: 	    if (&Apache::loncommon::connection_aborted($r)) { last; }
 1044: 	}
 1045: 	&Apache::lonnet::delenv('form.counter');
 1046: 	if ($flag_page_in_sequence eq 'YES') {$result =~ s/\\usepackage{calc}/\\usepackage{calc}\\usepackage{longtable}/;}	
 1047: 	$result .= '\end{document}';
 1048:      } elsif (($helper->{'VARS'}->{'PRINT_TYPE'} eq 'problems_for_students') ||
 1049: 	      ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'resources_for_students')){
 1050:      #-- prints assignments for whole class or for selected students  
 1051: 	 my $type;
 1052: 	 if ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'problems_for_students') {
 1053: 	     $selectionmade=5;
 1054: 	     $type='problems';
 1055: 	 } elsif ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'resources_for_students') {
 1056: 	     $selectionmade=8;
 1057: 	     $type='resources';
 1058: 	 }
 1059: 	 my @students=split /\|\|\|/, $helper->{'VARS'}->{'STUDENTS'};
 1060:          if ($helper->{'VARS'}->{'NUMBER_TO_PRINT'} eq '0' ||
 1061: 	     $helper->{'VARS'}->{'NUMBER_TO_PRINT'} eq 'all' ) {
 1062: 	     $helper->{'VARS'}->{'NUMBER_TO_PRINT'}=$#students+1;
 1063: 	 }
 1064: 	 my @master_seq=split /\|\|\|/, $helper->{'VARS'}->{'RESOURCES'};
 1065: 	 #loop over students
 1066: 	 my $flag_latex_header_remove = 'NO'; 
 1067: 	 my %moreenv;
 1068:          $moreenv{'instructor_comments'}='hide';
 1069: 	 $moreenv{'textwidth'}=&get_textwidth($helper,$LaTeXwidth);
 1070: 	 $moreenv{'print_discussions'}=$helper->{'VARS'}->{'PRINT_DISCUSSIONS'};
 1071: 	 if ($helper->{'VARS'}->{'PRINT_DISCUSSIONS'} eq 'yes') {$moreenv{'problem_split'}='yes';}
 1072: 	 my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin($r,'Print Status','Class Print Status',$#students+1,'inline','75');
 1073: 	 my $student_counter=-1;
 1074: 	 foreach my $person (@students) {
 1075:              my $duefile="/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout.due";
 1076: 	     if (-e $duefile) {
 1077: 		 my $temp_file = Apache::File->new('>>'.$duefile);
 1078: 		 print $temp_file "1969\n";
 1079: 	     }
 1080: 	     $student_counter++;
 1081: 	     my $i=int($student_counter/$helper->{'VARS'}{'NUMBER_TO_PRINT'});
 1082: 	     my ($output,$fullname)=&print_resources($r,$helper,$person,$type,\%moreenv,\@master_seq,$flag_latex_header_remove);
 1083: 	     $print_array[$i].=$output;
 1084: 	     $student_names[$i].=$person.':'.$fullname.'_END_';
 1085: 	     &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,&mt('last student').' '.$fullname);
 1086: 	     $flag_latex_header_remove = 'YES';
 1087: 	     if (&Apache::loncommon::connection_aborted($r)) { last; }
 1088: 	 }
 1089: 	 &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
 1090: 	 $result .= $print_array[0].'  \end{document}';
 1091:      } elsif (($helper->{'VARS'}->{'PRINT_TYPE'} eq 'problems_for_anon')     ||
 1092: 	      ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'resources_for_anon')  ) { 
 1093: 	 my $cdom =$ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
 1094: 	 my $cnum =$ENV{'course.'.$ENV{'request.course.id'}.'.num'};
 1095: 	 my $num_todo=$helper->{'VARS'}->{'NUMBER_TO_PRINT_TOTAL'};
 1096: 	 my $code_name=$helper->{'VARS'}->{'ANON_CODE_STORAGE_NAME'};
 1097: 	 my $old_name=$helper->{'VARS'}->{'REUSE_OLD_CODES'};
 1098: 	 my %moreenv = ('textwidth' => &get_textwidth($helper,$LaTeXwidth));
 1099: 	 my $seed=time+($$<<16)+($$);
 1100: 	 my @allcodes;
 1101: 	 if ($old_name) {
 1102: 	     my %result=&Apache::lonnet::get('CODEs',[$old_name],$cdom,$cnum);
 1103: 	     @allcodes=split(',',$result{$old_name});
 1104: 	     $num_todo=scalar(@allcodes);
 1105: 	 } else {
 1106: 	     my %allcodes;
 1107: 	     srand($seed);
 1108: 	     for (my $i=0;$i<$num_todo;$i++) {
 1109: 		 $moreenv{'CODE'}=&get_CODE(\%allcodes,$i,$seed,'6');
 1110: 	     }
 1111: 	     if ($code_name) {
 1112: 		 &Apache::lonnet::put('CODEs',
 1113: 				      {$code_name =>join(',',keys(%allcodes))},
 1114: 				      $cdom,$cnum);
 1115: 	     }
 1116: 	     @allcodes=keys(%allcodes);
 1117: 	 }
 1118: 	 my @master_seq=split /\|\|\|/, $helper->{'VARS'}->{'RESOURCES'};
 1119: 	 my ($type) = split(/_/,$helper->{'VARS'}->{'PRINT_TYPE'});
 1120: 	 my $number_per_page=$helper->{'VARS'}->{'NUMBER_TO_PRINT'};
 1121: 	 if ($number_per_page eq '0' || $number_per_page eq 'all') {
 1122: 	     $number_per_page=$num_todo;
 1123: 	 }
 1124: 	 my $flag_latex_header_remove = 'NO'; 
 1125: 	 my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin($r,'Print Status','Class Print Status',$num_todo,'inline','75');
 1126: 	 my $count=0;
 1127: 	 foreach my $code (sort(@allcodes)) {
 1128: 	     my $file_num=int($count/$number_per_page);
 1129: 	     $moreenv{'CODE'}=&num_to_letters($code);
 1130: 	     my ($output,$fullname)=
 1131: 		 &print_resources($r,$helper,'anonymous',$type,\%moreenv,
 1132: 				  \@master_seq,$flag_latex_header_remove);
 1133: 	     $print_array[$file_num].=$output;
 1134: 	     &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
 1135: 				       &mt('last assignment').' '.$fullname);
 1136: 	     $flag_latex_header_remove = 'YES';
 1137: 	     $count++;
 1138: 	     if (&Apache::loncommon::connection_aborted($r)) { last; }
 1139: 	 }
 1140: 	 &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
 1141: 	 $result .= $print_array[0].'  \end{document}';
 1142:      } elsif ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'problems_from_directory') {      
 1143:     #prints selected problems from the subdirectory 
 1144: 	$selectionmade = 6;
 1145:         my @list_of_files=split /\|\|\|/, $helper->{'VARS'}->{'FILES'};
 1146: 	@list_of_files=sort @list_of_files;
 1147: 	my $flag_latex_header_remove = 'NO'; 
 1148: 	my $rndseed=time;
 1149: 	if ($helper->{'VARS'}->{'curseed'}) {
 1150: 	    $rndseed=$helper->{'VARS'}->{'curseed'};
 1151: 	}
 1152: 	for (my $i=0;$i<=$#list_of_files;$i++) {
 1153: 	    my $urlp = $list_of_files[$i];
 1154: 	    $urlp=~s|//|/|;
 1155: 	    if ($urlp=~/\//) {
 1156: 		my %form;
 1157: 		$form{'grade_target'}='tex';
 1158: 		$form{'textwidth'}=&get_textwidth($helper,$LaTeXwidth);
 1159: 		$form{'rndseed'}=$rndseed;
 1160: 		if ($urlp =~ m|/home/([^/]+)/public_html|) {
 1161: 		    $urlp =~ s|/home/([^/]*)/public_html|/~$1|;
 1162: 		} else {
 1163: 		    $urlp =~ s|^$Apache::lonnet::perlvar{'lonDocRoot'}||;
 1164: 		}
 1165: 		my $texversion=&Apache::lonnet::ssi($urlp,%form);
 1166: 		if(($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') ||
 1167: 		   ($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'only')) {
 1168: 		    my %form;
 1169: 		    $form{'grade_target'}='answer';
 1170: 		    $form{'answer_output_mode'}='tex';
 1171: 		    $form{'latex_type'}=$helper->{'VARS'}->{'LATEX_TYPE'};
 1172: 		    $form{'rndseed'}=$rndseed;
 1173: 		    my $answer=&Apache::lonnet::ssi($urlp,%form);
 1174: 		    if ($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') {
 1175: 			$texversion=~s/(\\keephidden{ENDOFPROBLEM})/$answer$1/;
 1176: 		    } else {
 1177: 			$texversion=&print_latex_header($helper->{'VARS'}->{'LATEX_TYPE'});
 1178: 			if ($helper->{'VARS'}->{'construction'} ne '1') {
 1179: 			    $texversion.='\vskip 0 mm \noindent ';
 1180: 			    $texversion.=&path_to_problem ($urlp,$LaTeXwidth);
 1181: 			} else {
 1182: 			    $texversion.='\vskip 0 mm \noindent\textbf{Prints from construction space - there is no title.}\vskip 0 mm ';
 1183: 			    my $URLpath=$urlp;
 1184: 			    $URLpath=~s/~([^\/]+)/public_html\/$1\/$1/;
 1185: 			    $texversion.=&path_to_problem ($URLpath,$LaTeXwidth);
 1186: 			}
 1187: 			$texversion.='\vskip 1 mm '.$answer.'\end{document}';
 1188: 		    }
 1189: 		}
 1190:                 #this chunck is responsible for printing the path to problem
 1191: 		my $newurlp=$urlp;
 1192: 		if ($newurlp=~/~/) {$newurlp=~s|\/~([^\/]+)\/|\/home\/$1\/public_html\/|;}
 1193: 		$newurlp=&path_to_problem($newurlp,$LaTeXwidth);
 1194: 		$texversion =~ s/(\\begin{minipage}{\\textwidth})/$1 $newurlp/;
 1195: 		if ($flag_latex_header_remove ne 'NO') {
 1196: 		    $texversion = &latex_header_footer_remove($texversion);
 1197: 		} else {
 1198: 		    $texversion =~ s/\\end{document}//;
 1199: 		}
 1200: 		if ($helper->{'VARS'}->{'TABLE_INDEX'} eq 'yes') {
 1201: 		    $texversion=&IndexCreation($texversion,$urlp);
 1202: 		}
 1203: 		if ($helper->{'VARS'}->{'CONSTR_RESOURSE_URL'} eq 'yes') {
 1204: 		    $texversion=~s/(\\addcontentsline\{toc\}\{subsection\}\{[^\}]*\})/$1 URL: \\verb|$urlp| \\strut\\\\\\strut /;
 1205: 		    
 1206: 		}
 1207: 		$result .= $texversion;
 1208: 	    }
 1209: 	    $flag_latex_header_remove = 'YES';  
 1210: 	}
 1211: 	if ($helper->{VARS}->{'construction'} eq '1') {$result=~s/(\\typeout)/ RANDOM SEED IS $rndseed $1/;}
 1212: 	$result .= '\end{document}';      	
 1213:     }
 1214: #-------------------------------------------------------- corrections for the different page formats
 1215:     $result = &page_format_transformation($papersize,$laystyle,$numberofcolumns,$helper->{'VARS'}->{'PRINT_TYPE'},$result,$helper->{VARS}->{'assignment'},$helper->{'VARS'}->{'TABLE_CONTENTS'},$helper->{'VARS'}->{'TABLE_INDEX'},$selectionmade);
 1216:     $result = &latex_corrections($number_of_columns,$result,$selectionmade);
 1217:     for (my $i=1;$i<=$#print_array;$i++) {$print_array[$i] = &latex_corrections($number_of_columns,$print_array[$i],$selectionmade);}
 1218:     #changes page's parameters for the one column output 
 1219:     if ($numberofcolumns == 1) {
 1220: 	$result =~ s/\\textwidth\s*=\s*-?\d*\.?\d*\s*(cm|mm|in)/\\textwidth= $helper->{'VARS'}->{'pagesize.width'} $helper->{'VARS'}->{'pagesize.widthunit'} /;
 1221: 	$result =~ s/\\textheight\s*=\s*-?\d*\.?\d*\s*(cm|mm|in)/\\textheight $helper->{'VARS'}->{'pagesize.height'} $helper->{'VARS'}->{'pagesize.heightunit'} /;
 1222: 	$result =~ s/\\evensidemargin\s*=\s*-?\d*\.?\d*\s*(cm|mm|in)/\\evensidemargin= $helper->{'VARS'}->{'pagesize.lmargin'} $helper->{'VARS'}->{'pagesize.lmarginunit'} /;
 1223: 	$result =~ s/\\oddsidemargin\s*=\s*-?\d*\.?\d*\s*(cm|mm|in)/\\oddsidemargin= $helper->{'VARS'}->{'pagesize.lmargin'} $helper->{'VARS'}->{'pagesize.lmarginunit'} /;
 1224:     }
 1225: #-- writing .tex file in prtspool 
 1226:     my $temp_file;
 1227:     my $identifier = &Apache::loncommon::get_cgi_id();
 1228:     my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout_".$identifier.".tex";
 1229:     if (!($#print_array>0)) { 
 1230: 	unless ($temp_file = Apache::File->new('>'.$filename)) {
 1231: 	    $r->log_error("Couldn't open $filename for output $!");
 1232: 	    return SERVER_ERROR; 
 1233: 	}
 1234: 	print $temp_file $result;
 1235: 	my $begin=index($result,'\begin{document}',0);
 1236: 	my $inc=substr($result,0,$begin+16);
 1237:     } else {
 1238: 	my $begin=index($result,'\begin{document}',0);
 1239: 	my $inc=substr($result,0,$begin+16);
 1240:         for (my $i=0;$i<=$#print_array;$i++) {
 1241: 	    if ($i==0) {
 1242: 		$print_array[$i]=$result;
 1243: 	    } else {
 1244: 		my $anobegin=index($print_array[$i],'\setcounter{page}',0);
 1245: 		substr($print_array[$i],0,$anobegin)='';
 1246: 		$print_array[$i]=$inc.$print_array[$i].'\end{document}';
 1247: 	    }
 1248: 	    my $temp_file;
 1249: 	    my $newfilename=$filename;
 1250: 	    my $num=$i+1;
 1251: 	    $newfilename =~s/\.tex$/_$num\.tex/; 
 1252: 	    unless ($temp_file = Apache::File->new('>'.$newfilename)) {
 1253: 		$r->log_error("Couldn't open $newfilename for output $!");
 1254: 		return SERVER_ERROR; 
 1255: 	    }
 1256: 	    print $temp_file $print_array[$i];
 1257: 	}
 1258:     }
 1259:     my $student_names='';
 1260:     if ($#print_array>0) {
 1261: 	for (my $i=0;$i<=$#print_array;$i++) {
 1262: 	    $student_names.=$student_names[$i].'_ENDPERSON_';
 1263: 	}
 1264:     } else {
 1265: 	if ($#student_names>-1) {
 1266: 	    $student_names=$student_names[0].'_ENDPERSON_';
 1267: 	} else {
 1268: 	    my $fullname = &get_name($ENV{'user.name'},$ENV{'user.domain'});
 1269: 	    $student_names=join(':',$ENV{'user.name'},$ENV{'user.domain'},
 1270: 				$ENV{'request.course.sec'},$fullname).
 1271: 				    '_ENDPERSON_'.'_END_';
 1272: 	}
 1273:     }
 1274: 
 1275:     my $URLback=''; #link to original document
 1276:     if ($helper->{'VARS'}->{'construction'} ne '1') {
 1277: 	#prints published resource
 1278: 	$URLback=$helper->{'VARS'}->{'postdata'};
 1279:     } else {
 1280: 	#prints resource from the construction space
 1281: 	$URLback='/'.$helper->{'VARS'}->{'filename'};
 1282: 	if ($URLback=~/([^?]+)/) {
 1283: 	    $URLback=$1;
 1284: 	    $URLback=~s|^/~|/priv/|;
 1285: 	}
 1286:     }
 1287: 
 1288:     &Apache::lonnet::appenv('cgi.'.$identifier.'.file'   => $filename,
 1289:                             'cgi.'.$identifier.'.layout'  => $laystyle,
 1290:                             'cgi.'.$identifier.'.numcol'  => $numberofcolumns,
 1291: 			    'cgi.'.$identifier.'.paper'  => $papersize,
 1292:                             'cgi.'.$identifier.'.selection' => $selectionmade,
 1293: 			    'cgi.'.$identifier.'tableofcontents' => $helper->{'VARS'}->{'TABLE_CONTENTS'},
 1294: 			    'cgi.'.$identifier.'tableofindex' => $helper->{'VARS'}->{'TABLE_INDEX'},
 1295: 			    'cgi.'.$identifier.'role' => $ENV{'request.role.adv'},
 1296:                             'cgi.'.$identifier.'numberoffiles' => $#print_array,
 1297:                             'cgi.'.$identifier.'studentnames' => $student_names,
 1298:                             'cgi.'.$identifier.'backref' => $URLback,);
 1299:  
 1300: $r->print(<<FINALEND);
 1301: <br />
 1302: <meta http-equiv="Refresh" content="0; url=/cgi-bin/printout.pl?$identifier" />
 1303: <a href="/cgi-bin/printout.pl?$identifier">Continue</a>
 1304: </body>
 1305: </html>
 1306: FINALEND
 1307: }
 1308: 
 1309: sub num_to_letters {
 1310:     my ($num) = @_;
 1311:     my @nums= split('',$num);
 1312:     my @num_to_let=('A'..'Z');
 1313:     my $word;
 1314:     foreach my $digit (@nums) { $word.=$num_to_let[$digit]; }
 1315:     return $word;
 1316: }
 1317: 
 1318: sub get_CODE {
 1319:     my ($all_codes,$num,$seed,$size)=@_;
 1320:     my $max='1'.'0'x$size;
 1321:     my $newcode;
 1322:     while(1) {
 1323: 	$newcode=sprintf("%06d",int(rand($max)));
 1324: 	if (!exists($$all_codes{$newcode})) {
 1325: 	    $$all_codes{$newcode}=1;
 1326: 	    return &num_to_letters($newcode);
 1327: 	}
 1328:     }
 1329: }
 1330: 
 1331: sub print_resources {
 1332:     my ($r,$helper,$person,$type,$moreenv,$master_seq,$remove_latex_header)=@_;
 1333:     my $current_output = ''; 
 1334:     my ($username,$userdomain,$usersection) = split /:/,$person;
 1335:     my $fullname = &get_name($username,$userdomain);
 1336:     my $namepostfix;
 1337:     if ($person =~ 'anon') {
 1338: 	$namepostfix="\\\\Name: ";
 1339: 	$fullname = "CODE - ".$moreenv->{'CODE'};
 1340:     }
 1341:     #goes through all resources, checks if they are available for 
 1342:     #current student, and produces output   
 1343:     &Apache::lonnet::delenv('form.counter');
 1344:     &Apache::lonxml::init_counter();
 1345:     foreach my $curresline (@{$master_seq})  {
 1346: 	if ( !($type eq 'problems' && 
 1347: 	       ($curresline!~ m/\.(problem|exam|quiz|assess|survey|form|library)$/)) ) {
 1348: 	    my ($map,$id,$res_url) = &Apache::lonnet::decode_symb($curresline);
 1349: 	    if (&Apache::lonnet::allowed('bre',$res_url)) {
 1350: 		if ($res_url=~/\.(problem|exam|quiz|assess|survey|form|library|page|xml|html|htm|xhtml|xhtm)$/) {
 1351: 		    my $rendered = &Apache::loncommon::get_student_view($curresline,$username,$userdomain,$ENV{'request.course.id'},'tex',$moreenv);
 1352: 		    my $lonidsdir=$r->dir_config('lonIDsDir');
 1353: 		    my $envfile=$ENV{'user.environment'};
 1354: 		    $envfile=~/\/([^\/]+)\.id$/;
 1355: 		    $envfile=$1;
 1356: 		    &Apache::lonnet::transfer_profile_to_env($lonidsdir,$envfile);
 1357: 		    my $current_counter=$ENV{'form.counter'};
 1358: 		    if(($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') ||
 1359: 		       ($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'only')) {
 1360: 			$moreenv->{'answer_output_mode'}='tex';
 1361: 			$moreenv->{'latex_type'}=$helper->{'VARS'}->{'LATEX_TYPE'};
 1362: 			my $ansrendered = &Apache::loncommon::get_student_answers($curresline,$username,$userdomain,$ENV{'request.course.id'},%{$moreenv});
 1363: 			&Apache::lonnet::appenv(('form.counter' => $current_counter));
 1364: 			if ($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') {
 1365: 			    $rendered=~s/(\\keephidden{ENDOFPROBLEM})/$ansrendered$1/;
 1366: 			} else {
 1367: 			    $rendered=&print_latex_header($helper->{'VARS'}->{'LATEX_TYPE'});
 1368: 			    $rendered.='\vskip 0 mm \noindent\textbf{'.&Apache::lonnet::gettitle($curresline).'}\vskip 0 mm ';
 1369: 			    $rendered.=&path_to_problem ($curresline,$LaTeXwidth);
 1370: 			    $rendered.='\vskip 1 mm '.$ansrendered;
 1371: 			}
 1372: 		    }
 1373: 		    if ($remove_latex_header eq 'YES') {
 1374: 			$rendered = &latex_header_footer_remove($rendered);
 1375: 		    } else {
 1376: 			$rendered =~ s/\\end{document}//;
 1377: 		    }
 1378: 		    $current_output .= $rendered;		    
 1379: 		} elsif ($res_url=~/\/(smppg|syllabus|aboutme|bulletinboard)$/) {
 1380: 		    my $rendered = &Apache::loncommon::get_student_view($curresline,$username,$userdomain,$ENV{'request.course.id'},'tex',$moreenv);
 1381: 		    my $lonidsdir=$r->dir_config('lonIDsDir');
 1382: 		    my $envfile=$ENV{'user.environment'};
 1383: 		    $envfile=~/\/([^\/]+)\.id$/;
 1384: 		    $envfile=$1;
 1385: 		    &Apache::lonnet::transfer_profile_to_env($lonidsdir,$envfile);
 1386: 		    my $current_counter=$ENV{'form.counter'};
 1387: 		    if ($remove_latex_header eq 'YES') {
 1388: 			$rendered = &latex_header_footer_remove($rendered);
 1389: 		    } else {
 1390: 			$rendered =~ s/\\end{document}//;
 1391: 		    }
 1392: 		    $current_output .= $rendered.'\vskip 0.5mm\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}\strut \vskip 0 mm \strut ';
 1393: 		} else {
 1394: 		    my $rendered = &unsupported($res_url,$helper->{'VARS'}->{'LATEX_TYPE'});
 1395: 		    if ($remove_latex_header ne 'NO') {
 1396: 			$rendered = &latex_header_footer_remove($rendered);
 1397: 		    } else {
 1398: 			$rendered =~ s/\\end{document}//;
 1399: 		    }
 1400: 		    $current_output .= $rendered;
 1401: 		}
 1402: 	    }
 1403: 	    $remove_latex_header = 'YES';
 1404: 	}
 1405: 	if (&Apache::loncommon::connection_aborted($r)) { last; }
 1406:     }
 1407:     my $courseidinfo = &get_course();
 1408:     if (defined($courseidinfo)) { $courseidinfo=' - '.$courseidinfo }
 1409:     if ($usersection ne '') {$courseidinfo.=' - Sec. '.$usersection}
 1410:     my $currentassignment=&Apache::lonxml::latex_special_symbols($helper->{VARS}->{'assignment'},'header');
 1411:     if ($current_output=~/\\documentclass/) {
 1412: 	$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 /;
 1413:     } else {
 1414: 	my $blankpages = '';
 1415: 	for (my $j=0;$j<$helper->{'VARS'}->{'EMPTY_PAGES'};$j++) {$blankpages.='\clearpage\strut\clearpage';}
 1416: 	$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;
 1417:     }
 1418:     return ($current_output,$fullname);
 1419: 
 1420: }
 1421: 
 1422: sub handler {
 1423: 
 1424:     my $r = shift;
 1425:     my $helper;
 1426: 
 1427: #    my $loaderror=&Apache::lonnet::overloaderror($r);
 1428: #    if ($loaderror) { return $loaderror; }
 1429: #    $loaderror=
 1430: #       &Apache::lonnet::overloaderror($r,
 1431: #         $ENV{'course.'.$ENV{'request.course.id'}.'.home'});
 1432: #    if ($loaderror) { return $loaderror; }
 1433: 
 1434:     my $result = printHelper($r);
 1435:     if (!ref($result)) {
 1436: 	return $result;
 1437:     }
 1438:     $helper = $result;
 1439:    
 1440: #	my $key; 
 1441: #	foreach $key (keys %{$helper->{'VARS'}}) {
 1442: #	    $r->print(' '.$key.'->'.$helper->{'VARS'}->{$key}.'<-<br />');
 1443: #	}
 1444: #	foreach $key (keys %ENV) {
 1445: #	    $r->print(' '.$key.'->'.$ENV{$key}.'<-<br />');
 1446: #	}
 1447: #	return OK;
 1448: 
 1449:     my %parmhash=&Apache::lonnet::coursedescription($ENV{'request.course.id'});
 1450:     
 1451: #	my $key; 
 1452: #	foreach $key (keys %parmhash) {
 1453: #	    $r->print(' '.$key.'->'.$parmhash{$key}.'<-<br />');
 1454: #	}
 1455: #	return OK;
 1456: 
 1457:     &output_data($r,$helper,\%parmhash);
 1458:     return OK;
 1459: } 
 1460: 
 1461: use Apache::lonhelper;
 1462: 
 1463: sub addMessage {
 1464:     my $text = shift;
 1465:     my $paramHash = Apache::lonhelper::getParamHash();
 1466:     $paramHash->{MESSAGE_TEXT} = $text;
 1467:     Apache::lonhelper::message->new();
 1468: }
 1469: 
 1470: use Data::Dumper;
 1471: 
 1472: sub printHelper {
 1473:     my $r = shift;
 1474: 
 1475:     if ($r->header_only) {
 1476:         if ($ENV{'browser.mathml'}) {
 1477:             &Apache::loncommon::content_type($r,'text/xml');
 1478:         } else {
 1479:             &Apache::loncommon::content_type($r,'text/html');
 1480:         }
 1481:         $r->send_http_header;
 1482:         return OK;
 1483:     }
 1484: 
 1485:     # Send header, nocache
 1486:     if ($ENV{'browser.mathml'}) {
 1487:         &Apache::loncommon::content_type($r,'text/xml');
 1488:     } else {
 1489:         &Apache::loncommon::content_type($r,'text/html');
 1490:     }
 1491:     &Apache::loncommon::no_cache($r);
 1492:     $r->send_http_header;
 1493:     $r->rflush();
 1494: 
 1495:     # Unfortunately, this helper is so complicated we have to
 1496:     # write it by hand
 1497: 
 1498:     Apache::loncommon::get_unprocessed_cgi($ENV{QUERY_STRING});
 1499:     
 1500:     my $helper = Apache::lonhelper::helper->new("Printing Helper");
 1501:     $helper->declareVar('symb');
 1502:     $helper->declareVar('postdata');    
 1503:     $helper->declareVar('curseed'); 
 1504:     $helper->declareVar('probstatus');   
 1505:     $helper->declareVar('filename');
 1506:     $helper->declareVar('construction');
 1507:     $helper->declareVar('assignment');
 1508:     $helper->declareVar('style_file');
 1509:     $helper->declareVar('student_sort');
 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:     <choices variable='student_sort'>
 1699:       <choice computer='0'>Sort by section then student</choice>
 1700:       <choice computer='1'>Sort by students across sections.</choice>
 1701:     </choices>
 1702:     $resource_selector
 1703:   </state>
 1704: CHOOSE_STUDENTS
 1705: 
 1706: 	my $cdom = $ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
 1707: 	my $cnum = $ENV{'course.'.$ENV{'request.course.id'}.'.num'};
 1708:         my @names=&Apache::lonnet::getkeys('CODEs',$cdom,$cnum);
 1709: 	my $namechoice='<choice></choice>';
 1710: 	foreach my $name (sort {uc($a) cmp uc($b)} @names) {
 1711: 	    if ($name =~ /^error: 2 /) { next; }
 1712: 	    $namechoice.='<choice computer="'.$name.'">'.$name.'</choice>';
 1713: 	}
 1714:         &Apache::lonxml::xmlparse($r, 'helper', <<CHOOSE_ANON1);
 1715:   <state name="CHOOSE_ANON1" title="Select Students and Resources">
 1716:     <nextstate>PAGESIZE</nextstate>
 1717:     <message><hr width='33%' /><b>Number of anonymous assignments to print?</b></message>
 1718:     <string variable="NUMBER_TO_PRINT_TOTAL" maxlength="5" size="5"></string>
 1719:     <message><br /><b>Names to store the CODEs under for later:</b></message>
 1720:     <string variable="ANON_CODE_STORAGE_NAME" maxlength="50" size="20" />
 1721:     <message><hr width='33%' /></message>
 1722:     <message><b>Reprint a set of saved CODEs:</b></message>
 1723:     <dropdown variable="REUSE_OLD_CODES">
 1724:         $namechoice
 1725:     </dropdown>
 1726:     <message><hr width='33%' /></message>
 1727:     $resource_selector
 1728:   </state>
 1729: CHOOSE_ANON1
 1730: 
 1731: 
 1732: 	if ($helper->{VARS}->{'assignment'}) {
 1733: 	    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'];
 1734: 	    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'];
 1735: 	}
 1736: 	    
 1737: 
 1738: 	$resource_selector=<<RESOURCE_SELECTOR;
 1739:     <message><br /><big><i><b>Select resources for the assignment</b></i></big><br /></message>
 1740:     <resource variable="RESOURCES" multichoice="1" addstatus="1" 
 1741:               closeallpages="1">
 1742:       <filterfunc>return $isNotMap;</filterfunc>
 1743:       <mapurl>$map</mapurl>
 1744:       <valuefunc>return $symbFilter;</valuefunc>
 1745:       </resource>
 1746:     <message><br /><big><i><b>How should the results be printed?</b></i></big><br /></message>
 1747:     <choices variable="EMPTY_PAGES">
 1748:       <choice computer='0'>Start each student\'s assignment on a new page/column (add a pagefeed after each assignment)</choice>
 1749:       <choice computer='1'>Add one empty page/column after each student\'s assignment</choice>
 1750:       <choice computer='2'>Add two empty pages/column after each student\'s assignment</choice>
 1751:       <choice computer='3'>Add three empty pages/column after each student\'s assignment</choice>
 1752:     </choices>
 1753:     <message><hr width='33%' /><b>Number of assignments printed at the same time: </b></message>
 1754:     <string variable="NUMBER_TO_PRINT" maxlength="5" size="5"><defaultvalue>"all"</defaultvalue></string>
 1755: RESOURCE_SELECTOR
 1756: 
 1757: 	&Apache::lonxml::xmlparse($r, 'helper', <<CHOOSE_STUDENTS1);
 1758:   <state name="CHOOSE_STUDENTS1" title="Select Students and Resources">
 1759:     <student multichoice='1' variable="STUDENTS" nextstate="PAGESIZE" />
 1760:     <choices variable='student_sort'>
 1761:       <choice computer='0'>Sort by section then student</choice>
 1762:       <choice computer='1'>Sort by students across sections.</choice>
 1763:     </choices>
 1764: 
 1765:     $resource_selector
 1766:     </state>
 1767: CHOOSE_STUDENTS1
 1768: 
 1769: 	&Apache::lonxml::xmlparse($r, 'helper', <<CHOOSE_ANON2);
 1770:   <state name="CHOOSE_ANON2" title="Select Students and Resources">
 1771:     <nextstate>PAGESIZE</nextstate>
 1772:     <message><hr width='33%' /><b>Number of anonymous assignments to print?</b></message>
 1773:     <string variable="NUMBER_TO_PRINT_TOTAL" maxlength="5" size="5"></string>
 1774:     <message><br /><b>Names to store the CODEs under for later:</b></message>
 1775:     <string variable="ANON_CODE_STORAGE_NAME" maxlength="50" size="20" />
 1776:     <message><hr width='33%' /></message>
 1777:     <message><b>Reprint a set of saved CODEs:</b></message>
 1778:     <dropdown variable="REUSE_OLD_CODES">
 1779:         $namechoice
 1780:     </dropdown>
 1781:     <message><hr width='33%' /></message>
 1782:     $resource_selector
 1783:   </state>
 1784: CHOOSE_ANON2
 1785:     }
 1786: 
 1787:     # FIXME: That RE should come from a library somewhere.
 1788:     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/') {    
 1789:         push @{$printChoices}, ["<b>".&mt('Problems')."</b> ".&mt('from current subdirectory')." <b><i>$subdir</i></b>", 'problems_from_directory', 'CHOOSE_FROM_SUBDIR'];
 1790: 
 1791:         my $f = '$filename';
 1792:         my $xmlfrag = <<CHOOSE_FROM_SUBDIR;
 1793:   <state name="CHOOSE_FROM_SUBDIR" title="Select File(s) from <b><small>$subdir</small></b> to print">
 1794:     <message>(mark them then click "next" button) <br /></message>
 1795:     <files variable="FILES" multichoice='1'>
 1796:       <nextstate>PAGESIZE</nextstate>
 1797:       <filechoice>return '$subdir';</filechoice>
 1798: CHOOSE_FROM_SUBDIR
 1799:         
 1800:         # this is broken up because I really want interpolation above,
 1801:         # and I really DON'T want it below
 1802:         $xmlfrag .= <<'CHOOSE_FROM_SUBDIR';
 1803:       <filefilter>return Apache::lonhelper::files::not_old_version($filename) &&
 1804: 	  $filename =~ m/\.(problem|exam|quiz|assess|survey|form|library)$/;
 1805:       </filefilter>
 1806:       </files>
 1807:     </state>
 1808: CHOOSE_FROM_SUBDIR
 1809:         &Apache::lonxml::xmlparse($r, 'helper', $xmlfrag);
 1810:     }
 1811: 
 1812:     # Allow the user to select any sequence in the course, feed it to
 1813:     # another resource selector for that sequence
 1814:     if (!$helper->{VARS}->{'construction'}) {
 1815: 	push @$printChoices, ["<b>Resources</b> from <b>selected sequence</b> in course",
 1816: 			      'select_sequences', 'CHOOSE_SEQUENCE'];
 1817: 	my $escapedSequenceName = $helper->{VARS}->{'SEQUENCE'};
 1818: 	#Escape apostrophes and backslashes for Perl
 1819: 	$escapedSequenceName =~ s/\\/\\\\/g;
 1820: 	$escapedSequenceName =~ s/'/\\'/g;
 1821: 	&Apache::lonxml::xmlparse($r, 'helper', <<CHOOSE_FROM_ANY_SEQUENCE);
 1822:   <state name="CHOOSE_SEQUENCE" title="Select Sequence To Print From">
 1823:     <message>Select the sequence to print resources from:</message>
 1824:     <resource variable="SEQUENCE">
 1825:       <nextstate>CHOOSE_FROM_ANY_SEQUENCE</nextstate>
 1826:       <filterfunc>return \$res->is_sequence;</filterfunc>
 1827:       <valuefunc>return $urlValue;</valuefunc>
 1828:       </resource>
 1829:     </state>
 1830:   <state name="CHOOSE_FROM_ANY_SEQUENCE" title="Select Resources To Print">
 1831:     <message>(mark desired resources then click "next" button) <br /></message>
 1832:     <resource variable="RESOURCES" multichoice="1" toponly='1' addstatus="1"
 1833:               closeallpages="1">
 1834:       <nextstate>PAGESIZE</nextstate>
 1835:       <filterfunc>return $isProblem</filterfunc>
 1836:       <mapurl evaluate='1'>return '$escapedSequenceName';</mapurl>
 1837:       <valuefunc>return $symbFilter;</valuefunc>
 1838:       </resource>
 1839:     </state>
 1840: CHOOSE_FROM_ANY_SEQUENCE
 1841: }
 1842: 
 1843:     # Generate the first state, to select which resources get printed.
 1844:     Apache::lonhelper::state->new("START", "Select Printing Options:");
 1845:     $paramHash = Apache::lonhelper::getParamHash();
 1846:     $paramHash->{MESSAGE_TEXT} = "";
 1847:     Apache::lonhelper::message->new();
 1848:     $paramHash = Apache::lonhelper::getParamHash();
 1849:     $paramHash->{'variable'} = 'PRINT_TYPE';
 1850:     $helper->declareVar('PRINT_TYPE');
 1851:     $paramHash->{CHOICES} = $printChoices;
 1852:     Apache::lonhelper::choices->new();
 1853: 
 1854:     my $startedTable = 0; # have we started an HTML table yet? (need
 1855:                           # to close it later)
 1856: 
 1857:     if (($ENV{'request.role.adv'} and &Apache::lonnet::allowed('vgr',$ENV{'request.course.id'})) or 
 1858: 	($helper->{VARS}->{'construction'} eq '1')) {
 1859: 	addMessage("<hr width='33%' /><table><tr><td align='right'>Print: </td><td>");
 1860:         $paramHash = Apache::lonhelper::getParamHash();
 1861: 	$paramHash->{'variable'} = 'ANSWER_TYPE';   
 1862: 	$helper->declareVar('ANSWER_TYPE');         
 1863:         $paramHash->{CHOICES} = [
 1864:                                    ['Without Answers', 'yes'],
 1865:                                    ['With Answers', 'no'],
 1866:                                    ['Only Answers', 'only'],
 1867:                                    ['As Exam Problem', 'exam']
 1868:                                 ];
 1869:         Apache::lonhelper::dropdown->new();
 1870: 	addMessage("</td></tr>");
 1871: 	$startedTable = 1;
 1872:     }
 1873: 
 1874:     if ($ENV{'request.role.adv'}) {
 1875: 	if (!$startedTable) {
 1876: 	    addMessage("<hr width='33%' /><table><tr><td align='right'>LaTeX mode: </td><td>");
 1877: 	    $startedTable = 1;
 1878: 	} else {
 1879: 	    addMessage("<tr><td align='right'>LaTeX mode: </td><td>");
 1880: 	}
 1881:         $paramHash = Apache::lonhelper::getParamHash();
 1882: 	$paramHash->{'variable'} = 'LATEX_TYPE';   
 1883: 	$helper->declareVar('LATEX_TYPE');  
 1884: 	if ($helper->{VARS}->{'construction'} eq '1') {       
 1885: 	    $paramHash->{CHOICES} = [
 1886: 				     ['standard LaTeX mode', 'standard'], 
 1887: 				     ['LaTeX batchmode', 'batchmode'], ];
 1888: 	} else {
 1889: 	    $paramHash->{CHOICES} = [
 1890: 				     ['LaTeX batchmode', 'batchmode'],
 1891: 				     ['standard LaTeX mode', 'standard'] ];
 1892: 	}
 1893:         Apache::lonhelper::dropdown->new();
 1894:  
 1895: 	addMessage("</td></tr><tr><td align='right'>Print Table of Contents: </td><td>");
 1896:         $paramHash = Apache::lonhelper::getParamHash();
 1897: 	$paramHash->{'variable'} = 'TABLE_CONTENTS';   
 1898: 	$helper->declareVar('TABLE_CONTENTS');         
 1899:         $paramHash->{CHOICES} = [
 1900:                                    ['No', 'no'],
 1901:                                    ['Yes', 'yes'] ];
 1902:         Apache::lonhelper::dropdown->new();
 1903: 	addMessage("</td></tr>");
 1904:         
 1905: 	if (not $helper->{VARS}->{'construction'}) {
 1906: 	    addMessage("<tr><td align='right'>Print Index: </td><td>");
 1907: 	    $paramHash = Apache::lonhelper::getParamHash();
 1908: 	    $paramHash->{'variable'} = 'TABLE_INDEX';   
 1909: 	    $helper->declareVar('TABLE_INDEX');         
 1910: 	    $paramHash->{CHOICES} = [
 1911: 				     ['No', 'no'],
 1912: 				     ['Yes', 'yes'] ];
 1913: 	    Apache::lonhelper::dropdown->new();
 1914: 	    addMessage("</td></tr>");
 1915: 	    addMessage("<tr><td align='right'>Print Discussions: </td><td>");
 1916: 	    $paramHash = Apache::lonhelper::getParamHash();
 1917: 	    $paramHash->{'variable'} = 'PRINT_DISCUSSIONS';   
 1918: 	    $helper->declareVar('PRINT_DISCUSSIONS');         
 1919: 	    $paramHash->{CHOICES} = [
 1920: 				     ['No', 'no'],
 1921: 				     ['Yes', 'yes'] ];
 1922: 	    Apache::lonhelper::dropdown->new();
 1923: 	    addMessage("</td></tr>");
 1924: 	}
 1925: 
 1926: 	if ($helper->{'VARS'}->{'construction'}) { 
 1927: 	    my $stylevalue=$ENV{'construct.style'};
 1928: 	    my $xmlfrag .= <<"RNDSEED";
 1929: 	    <message><tr><td align='right'>Use random seed:  </td><td></message>
 1930: 	    <string variable="curseed" size="15" maxlength="15">
 1931: 		<defaultvalue>
 1932: 	            return $helper->{VARS}->{'curseed'};
 1933: 	        </defaultvalue>
 1934: 	    </string>
 1935: 	     <message></td></tr><tr><td align="right">Use style file:</td><td></message>
 1936:              <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>
 1937:              <message></td></tr></message>
 1938: RNDSEED
 1939:             &Apache::lonxml::xmlparse($r, 'helper', $xmlfrag);
 1940: 	    $helper->{'VARS'}->{'style_file'}=$ENV{'form.style_file_value'};
 1941: 	}
 1942:     }
 1943: 
 1944: 
 1945: 
 1946: 
 1947:     if ($startedTable) {
 1948: 	addMessage("</table>");
 1949:     }
 1950: 
 1951:     Apache::lonprintout::page_format_state->new("FORMAT");
 1952: 
 1953:     # Generate the PAGESIZE state which will offer the user the margin
 1954:     # choices if they select one column
 1955:     Apache::lonhelper::state->new("PAGESIZE", "Set Margins");
 1956:     Apache::lonprintout::page_size_state->new('pagesize', 'FORMAT', 'FINAL');
 1957: 
 1958: 
 1959:     $helper->process();
 1960: 
 1961:     # MANUAL BAILOUT CONDITION:
 1962:     # If we're in the "final" state, bailout and return to handler
 1963:     if ($helper->{STATE} eq 'FINAL') {
 1964:         return $helper;
 1965:     }    
 1966: 
 1967:     $r->print($helper->display());
 1968: 
 1969:     &Apache::lonhelper::unregisterHelperTags();
 1970: 
 1971:     return OK;
 1972: }
 1973: 
 1974: 
 1975: 1;
 1976: 
 1977: package Apache::lonprintout::page_format_state;
 1978: 
 1979: =pod
 1980: 
 1981: =head1 Helper element: page_format_state
 1982: 
 1983: See lonhelper.pm documentation for discussion of the helper framework.
 1984: 
 1985: Apache::lonprintout::page_format_state is an element that gives the 
 1986: user an opportunity to select the page layout they wish to print 
 1987: with: Number of columns, portrait/landscape, and paper size. If you 
 1988: want to change the paper size choices, change the @paperSize array 
 1989: contents in this package.
 1990: 
 1991: page_format_state is always directly invoked in lonprintout.pm, so there
 1992: is no tag interface. You actually pass parameters to the constructor.
 1993: 
 1994: =over 4
 1995: 
 1996: =item * B<new>(varName): varName is where the print information will be stored in the format FIXME.
 1997: 
 1998: =back
 1999: 
 2000: =cut
 2001: 
 2002: use Apache::lonhelper;
 2003: 
 2004: no strict;
 2005: @ISA = ("Apache::lonhelper::element");
 2006: use strict;
 2007: use Apache::lonlocal;
 2008: 
 2009: my $maxColumns = 2;
 2010: my @paperSize = ("letter [8 1/2x11 in]", "legal [8 1/2x14 in]", 
 2011:                  "tabloid (ledger) [11x17 in]", "executive [7 1/2x10 in]",
 2012:                  "a2 [420x594 mm]", "a3 [297x420 mm]", "a4 [210x297 mm]", 
 2013:                  "a5 [148x210 mm]", "a6 [105x148 mm]" );
 2014: 
 2015: # Tentative format: Orientation (L = Landscape, P = portrait) | Colnum |
 2016: #                   Paper type
 2017: 
 2018: sub new { 
 2019:     my $self = Apache::lonhelper::element->new();
 2020: 
 2021:     shift;
 2022: 
 2023:     $self->{'variable'} = shift;
 2024:     my $helper = Apache::lonhelper::getHelper();
 2025:     $helper->declareVar($self->{'variable'});
 2026:     bless($self);
 2027:     return $self;
 2028: }
 2029: 
 2030: sub render {
 2031:     my $self = shift;
 2032:     my $helper = Apache::lonhelper::getHelper();
 2033:     my $result = '';
 2034:     my $var = $self->{'variable'};
 2035:     my $PageLayout=&mt('Page layout');
 2036:     my $NumberOfColumns=&mt('Number of columns');
 2037:     my $PaperType=&mt('Paper type');
 2038:     $result .= <<STATEHTML;
 2039: 
 2040: <hr width="33%" />
 2041: <table cellpadding="3">
 2042:   <tr>
 2043:     <td align="center"><b>$PageLayout</b></td>
 2044:     <td align="center"><b>$NumberOfColumns</b></td>
 2045:     <td align="center"><b>$PaperType</b></td>
 2046:   </tr>
 2047:   <tr>
 2048:     <td>
 2049:       <input type="radio" name="${var}.layout" value="L" /> Landscape<br />
 2050:       <input type="radio" name="${var}.layout" value="P" checked='1'  /> Portrait
 2051:     </td>
 2052:     <td align="center">
 2053:       <select name="${var}.cols">
 2054: STATEHTML
 2055: 
 2056:     my $i;
 2057:     for ($i = 1; $i <= $maxColumns; $i++) {
 2058:         if ($i == 2) {
 2059:             $result .= "<option value='$i' selected>$i</option>\n";
 2060:         } else {
 2061:             $result .= "<option value='$i'>$i</option>\n";
 2062:         }
 2063:     }
 2064: 
 2065:     $result .= "</select></td><td>\n";
 2066:     $result .= "<select name='${var}.paper'>\n";
 2067: 
 2068:     my %parmhash=&Apache::lonnet::coursedescription($ENV{'request.course.id'});
 2069:     my $DefaultPaperSize=$parmhash{'default_paper_size'};
 2070:     if ($DefaultPaperSize eq '') {$DefaultPaperSize='letter';}
 2071:     $i = 0;
 2072:     foreach (@paperSize) {
 2073: 	$_=~/(\w+)/;
 2074: 	my $papersize=$1;
 2075:         if ($paperSize[$i]=~/$DefaultPaperSize/) {
 2076:             $result .= "<option selected value='$papersize'>" . $paperSize[$i] . "</option>\n";
 2077:         } else {
 2078:             $result .= "<option value='$papersize'>" . $paperSize[$i] . "</option>\n";
 2079:         }
 2080:         $i++;
 2081:     }
 2082:     $result .= "</select></td></tr></table>";
 2083:     return $result;
 2084: }
 2085: 
 2086: sub postprocess {
 2087:     my $self = shift;
 2088: 
 2089:     my $var = $self->{'variable'};
 2090:     my $helper = Apache::lonhelper->getHelper();
 2091:     $helper->{VARS}->{$var} = 
 2092:         $ENV{"form.$var.layout"} . '|' . $ENV{"form.$var.cols"} . '|' .
 2093:         $ENV{"form.$var.paper"};
 2094:     return 1;
 2095: }
 2096: 
 2097: 1;
 2098: 
 2099: package Apache::lonprintout::page_size_state;
 2100: 
 2101: =pod
 2102: 
 2103: =head1 Helper element: page_size_state
 2104: 
 2105: See lonhelper.pm documentation for discussion of the helper framework.
 2106: 
 2107: Apache::lonprintout::page_size_state is an element that gives the 
 2108: user the opportunity to further refine the page settings if they
 2109: select a single-column page.
 2110: 
 2111: page_size_state is always directly invoked in lonprintout.pm, so there
 2112: is no tag interface. You actually pass parameters to the constructor.
 2113: 
 2114: =over 4
 2115: 
 2116: =item * B<new>(varName): varName is where the print information will be stored in the format FIXME.
 2117: 
 2118: =back
 2119: 
 2120: =cut
 2121: 
 2122: use Apache::lonhelper;
 2123: 
 2124: no strict;
 2125: @ISA = ("Apache::lonhelper::element");
 2126: use strict;
 2127: 
 2128: 
 2129: 
 2130: sub new { 
 2131:     my $self = Apache::lonhelper::element->new();
 2132: 
 2133:     shift; # disturbs me (probably prevents subclassing) but works (drops
 2134:            # package descriptor)... - Jeremy
 2135: 
 2136:     $self->{'variable'} = shift;
 2137:     my $helper = Apache::lonhelper::getHelper();
 2138:     $helper->declareVar($self->{'variable'});
 2139: 
 2140:     # The variable name of the format element, so we can look into 
 2141:     # $helper->{VARS} to figure out whether the columns are one or two
 2142:     $self->{'formatvar'} = shift;
 2143: 
 2144:     # The state to transition to after selection, or after discovering
 2145:     # the cols are not set to 1
 2146:     $self->{NEXTSTATE} = shift;
 2147:     bless($self);
 2148:     return $self;
 2149: }
 2150: 
 2151: sub render {
 2152:     my $self = shift;
 2153:     my $helper = Apache::lonhelper::getHelper();
 2154:     my $result = '';
 2155:     my $var = $self->{'variable'};
 2156: 
 2157:     if (defined $self->{ERROR_MSG}) {
 2158:         $result .= '<br /><font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br />';
 2159:     }
 2160: 
 2161:     $result .= <<ELEMENTHTML;
 2162: 
 2163: <p>How should the column be formatted?</p>
 2164: 
 2165: <table cellpadding='3'>
 2166:   <tr>
 2167:     <td align='right'><b>Width</b>:</td>
 2168:     <td align='left'><input type='text' name='$var.width' value='18' size='4'></td>
 2169:     <td align='left'>
 2170:       <select name='$var.widthunit'>
 2171:         <option>cm</option><option>in</option>
 2172:       </select>
 2173:     </td>
 2174:   </tr>
 2175:   <tr>
 2176:     <td align='right'><b>Height</b>:</td>
 2177:     <td align='left'><input type='text' name="$var.height" value="25.9" size='4'></td>
 2178:     <td align='left'>
 2179:       <select name='$var.heightunit'>
 2180:         <option>cm</option><option>in</option>
 2181:       </select>
 2182:     </td>
 2183:   </tr>
 2184:   <tr>
 2185:     <td align='right'><b>Left margin</b>:</td>
 2186:     <td align='left'><input type='text' name='$var.lmargin' value='-1.5' size='4'></td>
 2187:     <td align='left'>
 2188:       <select name='$var.lmarginunit'>
 2189:         <option>cm</option><option>in</option>
 2190:       </select>
 2191:     </td>
 2192:   </tr>
 2193: </table>
 2194: 
 2195: <p>Hint: Some instructors like to leave scratch space for the student by
 2196: making the width much smaller than the width of the page.</p>
 2197: 
 2198: ELEMENTHTML
 2199: 
 2200:     return $result;
 2201: }
 2202: 
 2203: # If the user didn't select 1 column, skip this state.
 2204: sub preprocess {
 2205:     my $self = shift;
 2206:     my $helper = Apache::lonhelper::getHelper();
 2207: 
 2208:     my $format = $helper->{VARS}->{$self->{'formatvar'}};
 2209:     if (substr($format, 2, 1) ne '1') {
 2210:         $helper->changeState($self->{NEXTSTATE});
 2211:     }
 2212:    
 2213:     return 1;
 2214: }
 2215: 
 2216: sub postprocess {
 2217:     my $self = shift;
 2218: 
 2219:     my $var = $self->{'variable'};
 2220:     my $helper = Apache::lonhelper->getHelper();
 2221:     my $width = $helper->{VARS}->{$var .'.width'} = $ENV{"form.${var}.width"}; 
 2222:     my $height = $helper->{VARS}->{$var .'.height'} = $ENV{"form.${var}.height"}; 
 2223:     my $lmargin = $helper->{VARS}->{$var .'.lmargin'} = $ENV{"form.${var}.lmargin"}; 
 2224:     $helper->{VARS}->{$var .'.widthunit'} = $ENV{"form.${var}.widthunit"}; 
 2225:     $helper->{VARS}->{$var .'.heightunit'} = $ENV{"form.${var}.heightunit"}; 
 2226:     $helper->{VARS}->{$var .'.lmarginunit'} = $ENV{"form.${var}.lmarginunit"}; 
 2227: 
 2228:     my $error = '';
 2229: 
 2230:     # /^-?[0-9]+(\.[0-9]*)?$/ -> optional minus, at least on digit, followed 
 2231:     # by an optional period, followed by digits, ending the string
 2232: 
 2233:     if ($width !~  /^-?[0-9]+(\.[0-9]*)?$/) {
 2234:         $error .= "Invalid width; please type only a number.<br />\n";
 2235:     }
 2236:     if ($height !~  /^-?[0-9]+(\.[0-9]*)?$/) {
 2237:         $error .= "Invalid height; please type only a number.<br />\n";
 2238:     }
 2239:     if ($lmargin !~  /^-?[0-9]+(\.[0-9]*)?$/) {
 2240:         $error .= "Invalid left margin; please type only a number.<br />\n";
 2241:     }
 2242: 
 2243:     if (!$error) {
 2244:         Apache::lonhelper::getHelper()->changeState($self->{NEXTSTATE});
 2245:         return 1;
 2246:     } else {
 2247:         $self->{ERROR_MSG} = $error;
 2248:         return 0;
 2249:     }
 2250: }
 2251: 
 2252: 
 2253: 
 2254: __END__
 2255: 

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