File:  [LON-CAPA] / loncom / interface / lonprintout.pm
Revision 1.225: download - view: text, annotated - select for diffs
Tue Sep 2 20:58:31 2003 UTC (20 years, 9 months ago) by bowersj2
Branches: MAIN
CVS tags: HEAD
Documentation updates for lonhelper.pm (apparently), and fixes bug 2104.

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

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