File:  [LON-CAPA] / loncom / interface / lonprintout.pm
Revision 1.200: download - view: text, annotated - select for diffs
Mon Jun 30 20:24:58 2003 UTC (20 years, 11 months ago) by sakharuk
Branches: MAIN
CVS tags: HEAD
 Single resource can be printed on a few pages.

    1: # The LearningOnline Network
    2: # Printout
    3: #
    4: # $Id: lonprintout.pm,v 1.200 2003/06/30 20:24:58 sakharuk 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) = (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.2 in';
  421: 		$textheight = '6.8 in';
  422: 		$oddoffset = '-40 pt';
  423: 		$evenoffset = '-60 pt';
  424: 	    }
  425: 	}
  426: #    } elsif($papersize eq '1') {
  427: #    } elsif($papersize eq '2') {
  428: #    } elsif($papersize eq '3'/) {
  429:     } elsif($papersize eq '6') {
  430: 	if ($layout eq 'book') {
  431: 	    if ($numberofcolumns == 1) {
  432: 		$textwidth = '18 cm';
  433: 		$textheight = '28 cm';
  434: 		$oddoffset = '-0.57 in';
  435: 		$evenoffset = '-0.57 in';
  436: 	    } elsif ($numberofcolumns == 2) {
  437: 		$textwidth = '96 mm';
  438: 		$textheight = '2 cm';
  439: 		$oddoffset = '-0.57 in';
  440: 		$evenoffset = '-0.57 in';
  441: 	    }
  442: 	} elsif ($layout eq 'album') {
  443: 	    if ($numberofcolumns eq '1') {
  444: 		$textwidth = '8.5 in';
  445: 		$textheight = '7.7 in';
  446: 		$oddoffset = '-40 pt';
  447: 		$evenoffset = '-60 pt';
  448: 	    } elsif ($numberofcolumns == 2) {
  449: 		$textwidth = '3.9 in';
  450: 		$textheight = '7.7 in';
  451: 		$oddoffset = '-40 pt';
  452: 		$evenoffset = '-60 pt';
  453: 	    }
  454: 	}
  455: #    } elsif($papersize eq '5') {
  456: #    } elsif($papersize eq '4') {
  457: #    } elsif($papersize eq '7') {
  458: #    } elsif($papersize eq '8') {
  459:     }
  460:     return $textwidth,$textheight,$oddoffset,$evenoffset;
  461: }
  462: 
  463: 
  464: sub get_name {
  465:     my ($uname,$udom)=@_;
  466:     if (!defined($uname)) { $uname=$ENV{'user.name'}; }
  467:     if (!defined($udom)) { $uname=$ENV{'user.domain'}; }
  468:     my $plainname=&Apache::loncommon::plainname($uname,$udom);
  469:     if ($plainname=~/^\s*$/) { $plainname=$uname; }
  470:     return &Apache::lonxml::latex_special_symbols($plainname,undef,undef,
  471: 						  'header');
  472: }
  473: 
  474: 
  475: sub page_format_transformation {
  476:     my ($papersize,$layout,$numberofcolumns,$choice,$text,$assignment) = @_; 
  477:     my ($textwidth,$textheight,$oddoffset,$evenoffset);
  478: 	$assignment=~s/_/ /g;
  479:         if ($numberofcolumns != 1) {
  480:           ($textwidth,$textheight,$oddoffset,$evenoffset) = &page_format($papersize,$layout,$numberofcolumns);
  481: 	} else {
  482: 	  ($textwidth,$textheight,$oddoffset,$evenoffset) = ($ENV{'form.width'},$ENV{'form.height'},$ENV{'form.leftmargin'},$ENV{'form.leftmargin'});
  483: 	}
  484:     my $name = &get_name();
  485:     if ($name =~ /^\s*$/) {
  486: 	$name=&Apache::lonxml::latex_special_symbols($ENV{'user.name'},undef,
  487: 						     undef,'header');
  488:     }
  489:     my $courseidinfo = &Apache::lonxml::latex_special_symbols(&Apache::lonnet::unescape($ENV{'course.'.$ENV{'request.course.id'}.'.description'}),'','','header');
  490:     if ($layout eq 'album') {
  491: 	    $text =~ s/\\begin{document}/\\setlength{\\oddsidemargin}{$oddoffset}\\setlength{\\evensidemargin}{$evenoffset}\\setlength{\\topmargin}{200pt}\\setlength{\\textwidth}{$textwidth}\\setlength{\\textheight}{$textheight}\\setlength{\\parindent}{20pt}\\setlength{\\marginparwidth}{90pt}\\setlength{\\textfloatsep}{8pt plus 2\.0pt minus 4\.0pt}\\newlength{\\minipagewidth}\\setlength{\\minipagewidth}{\\textwidth\/\$number_of_columns-0\.2cm} \\begin{document}\\setcounter{page}{1}\\noindent\\parbox{\\minipagewidth}{\\noindent\\fbox{\\textbf{$name}} \\hfill  $courseidinfo} \\vskip 5 mm /;
  492:     } elsif ($layout eq 'book') {
  493: 	if ($choice ne 'All class print') { 
  494: 	    $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}/;
  495: 	} else {
  496: 	    $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 /;
  497: 	}
  498:     }
  499:     return $text;
  500: }
  501: 
  502: 
  503: sub page_cleanup {
  504:     my $result = shift;	
  505:  
  506:     $result =~ m/\\end{document}(\d*)$/;
  507:     my $number_of_columns = $1;
  508:     my $insert = '{';
  509:     for (my $id=1;$id<=$number_of_columns;$id++) { $insert .='l'; }
  510:     $insert .= '}';
  511:     $result =~ s/(\\begin{longtable})INSERTTHEHEADOFLONGTABLE\\endfirsthead\\endhead/$1$insert/g;
  512:     $result =~ s/&\s*REMOVETHEHEADOFLONGTABLE\\\\/\\\\/g;
  513:     return $result,$number_of_columns;
  514: }
  515: 
  516: 
  517: sub details_for_menu {
  518: 
  519:     my $name_of_resourse = $hash{'title_'.$hash{'ids_'.$ENV{'form.postdata'}}};
  520:     my $symbolic = &Apache::lonnet::symbread($ENV{'form.postdata'});
  521:     my ($map,$id,$resource)=split(/___/,$symbolic);
  522:     $map=&Apache::lonnet::clutter($map);
  523:     my $name_of_sequence;
  524:     $name_of_sequence = $hash{'title_'.$hash{'ids_'.$map}};
  525:     if ($name_of_sequence =~ /^\s*$/) {
  526: 	$map =~ m|([^/]+)$|;
  527: 	$name_of_sequence = $1;
  528:     }
  529:     my $name_of_map = $hash{'title_'.$hash{'ids_'.&Apache::lonnet::clutter($ENV{'request.course.uri'})}};
  530:     if ($name_of_map =~ /^\s*$/) {
  531: 	$ENV{'request.course.uri'} =~ m|([^/]+)$|;
  532: 	$name_of_map = $1;
  533:     }
  534:     return ($name_of_resourse,$name_of_sequence,$name_of_map);
  535: 
  536: }
  537: 
  538: 
  539: sub latex_corrections {
  540: 
  541:     my ($number_of_columns,$result) = @_;
  542: 
  543: #    $result =~ s/\\includegraphics{/\\includegraphics\[width=\\minipagewidth\]{/g;
  544:     $result =~ s/\$number_of_columns/$number_of_columns/g;
  545:     $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/;
  546:     $result =~ s/(\\end{longtable}\s*)(\\strut\\newline\\noindent\\makebox\[\\textwidth\/$number_of_columns\]\[b\]{\\hrulefill})/$2$1/g;
  547:     $result =~ s/(\\end{longtable}\s*)\\strut\\newline/$1/g;
  548: #-- LaTeX corrections     
  549:     my $first_comment = index($result,'<!--',0);
  550:     while ($first_comment != -1) {
  551: 	my $end_comment = index($result,'-->',$first_comment);
  552: 	substr($result,$first_comment,$end_comment-$first_comment+3) = '';
  553: 	$first_comment = index($result,'<!--',$first_comment);
  554:     }
  555:     $result =~ s/^\s+$//gm; #remove empty lines
  556:     $result =~ s/(\s)(\s+)/$1/g; #removes more than one empty space
  557:     $result =~ s/\\\\\s*\\vskip/\\vskip/gm;
  558:     $result =~ s/\\\\\s*\\noindent\s*(\\\\)+/\\\\\\noindent /g;
  559:     $result =~ s/{\\par }\s*\\\\/\\\\/gm;
  560: 	$result =~ s/\\\\\s+\[/ \[/g;
  561:     $result =~ s/\b__+\b/\\makebox\[1 cm\]\[b\]{\\hrulefill}/g;
  562:     #conversion of html characters to LaTeX equivalents
  563:     if ($result =~ m/&(\w+|#\d+);/) {
  564: 	$result = &character_chart($result);
  565:     }
  566:     $result =~ s/(\\end{tabular})\s*\\vskip 0 mm/$1/g;
  567:     $result =~ s/(\\begin{enumerate})\s*\\noindent/$1/g;
  568: 
  569:     return $result;
  570: }
  571: 
  572: 
  573: sub output_data {
  574:     my ($r,$helper,$rparmhash) = @_;
  575:     my %parmhash = %$rparmhash;
  576:     my $bodytag=&Apache::loncommon::bodytag('Preparing Printout');
  577:     $r->print(<<ENDPART);
  578: <html>
  579: <head>
  580: <title>LON-CAPA output for printing</title>
  581: </head>
  582: $bodytag
  583: Please stand by while processing your print request, this may take some time ...
  584: ENDPART
  585: 
  586:     my $format_from_helper = $helper->{'VARS'}->{'FORMAT'};
  587:     my ($result,$selectionmade) = ('','');
  588:     my $number_of_columns = 1; #used only for pages to determine the width of the cell
  589:     my @temporary_array=split /\|/,$format_from_helper;
  590:     my ($laystyle,$numberofcolumns,$papersize)=@temporary_array;
  591:     if ($laystyle eq 'L') {
  592: 	$laystyle='album';
  593:     } else {
  594: 	$laystyle='book';
  595:     }
  596:     my ($textwidth,$textheight,$oddoffset,$evenoffset) = &page_format($papersize,$laystyle,$numberofcolumns);
  597:     my $assignment =  $ENV{'form.assignment'};
  598:     my $LaTeXwidth; 
  599:     if ($textwidth=~/(\d+\.?\d*)\s*cm/) {
  600: 	$LaTeXwidth = $1*10;
  601:     } elsif ($textwidth=~/(\d+\.?\d*)\s*mm/) {
  602: 	$LaTeXwidth = $1;
  603:     } elsif ($textwidth=~/(\d+\.?\d*)\s*in/) {
  604: 	$LaTeXwidth = $1*25.4;
  605:     }
  606:     $LaTeXwidth.=' mm';
  607:     
  608:     if ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'current_document') {
  609:       #-- single document - problem, page, html, xml, ...
  610: 	my $currentURL;
  611: 	if ($helper->{'VARS'}->{'construction'} ne '1') {
  612:             #prints published resource
  613: 	    $currentURL=$helper->{'VARS'}->{'postdata'};
  614: 	} else {
  615:             #prints resource from the construction space
  616: 	    $currentURL=$helper->{'VARS'}->{'filename'};
  617: 	    $currentURL=~s/\/home\//\/~/;
  618: 	    $currentURL=~s/public_html\///;
  619: 	}
  620: 	$selectionmade = 1;
  621: 	if ($currentURL=~/\.(problem|exam|quiz|assess|survey|form|library|page|xml|html|htm|xhtml|xhtm)$/) {
  622: 	    my %moreenv;
  623: 	    $moreenv{'request.filename'}=$currentURL;
  624: 	    my %form;
  625: 	    $form{'grade_target'}='tex';
  626: 	    $form{'textwidth'}=$LaTeXwidth;	
  627: 	    $form{'problem_split'}=$parmhash{'problem_stream_switch'}; 
  628: 	    my $rndseed=time;
  629: 	    $form{'rndseed'}=$rndseed;
  630: 	    &Apache::lonnet::appenv(%moreenv);
  631: 	    my $texversion=&Apache::lonnet::ssi($currentURL,%form);
  632: 	    &Apache::lonnet::delenv('form.counter');
  633: 	    &Apache::lonnet::delenv('request.filename');
  634: 	    if($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') {
  635: 		my %form;
  636: 		$form{'grade_target'}='answer';
  637: 		$form{'answer_output_mode'}='tex';
  638: 		$form{'rndseed'}=$rndseed;
  639: 		my $answer=&Apache::lonnet::ssi($currentURL,%form);
  640: 		$texversion=~s/(\\keephidden{ENDOFPROBLEM})/$answer$1/;
  641: 	    }
  642: 	    $result .= $texversion;
  643: 	    if ($currentURL=~m/\.page\s*$/) {
  644: 		($result,$number_of_columns) = &page_cleanup($result);
  645: 	    }
  646: 	} else {
  647: #	  $result='\documentclass[letterpaper]{article}\usepackage{calc}\begin{document}Printout of this type of document is currently not supported: ';
  648: #	  if ($ENV{'form.url'}=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) {
  649: #	      $result.=$1;
  650: #	  } else {
  651: #	      $result.=$ENV{'form.url'};
  652: #	  }
  653: #	  $result.=' \end{document}'
  654: 	}
  655:     } elsif (($helper->{'VARS'}->{'PRINT_TYPE'} eq 'map_problems') or
  656:              ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'map_problems_pages') or
  657:              ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'all_problems')) { 
  658:         #-- produce an output string
  659: 	my %form=();	   
  660: 	$form{'grade_target'}='tex';
  661: 	$form{'textwidth'}=$LaTeXwidth;
  662: 	$form{'problem_split'}=$parmhash{'problem_stream_switch'};
  663: 	my $flag_latex_header_remove = 'NO';
  664: 	my $flag_page_in_sequence = 'NO';
  665: 	my @master_seq=split /\|\|\|/, $helper->{'VARS'}->{'RESOURCES'};
  666: 	my $prevassignment='';
  667: 	for (my $i=0;$i<=$#master_seq;$i++) {
  668: 	    $master_seq[$i]=~/___\d+___(.*)$/;
  669: 	    my $urlp='/res/'.$1;
  670: 	    if ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'map_problems') {
  671: 		$selectionmade = 2;
  672: 	    } elsif ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'map_problems_pages') {
  673: 		$selectionmade = 3;
  674: 	    } elsif ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'all_problems') {
  675: 		$selectionmade = 4;
  676: 	    }
  677: 	    $form{'symb'}=$master_seq[$i];
  678: 	    $master_seq[$i]=~/(.*)___\d*___/;
  679: 	    my $assignment=&Apache::lonnet::gettitle($1); #tittle of the assignment which contains this problem
  680: 	    #&Apache::lonnet::logthis("Trying to get $urlp with symb $master_seq[$i]");
  681: 	    my $texversion=&Apache::lonnet::ssi($urlp,%form);
  682: 	    if ($urlp=~/\.page$/) {
  683: 		($texversion,my $number_of_columns_page) = &page_cleanup($texversion);
  684: 		if ($number_of_columns_page > $number_of_columns) {$number_of_columns=$number_of_columns_page;} 
  685: 		$texversion =~ s/\\end{document}\d*/\\end{document}/;
  686: 		$flag_page_in_sequence = 'YES';
  687: 	    } 
  688: 	    if ($flag_latex_header_remove ne 'NO') {
  689: 		$texversion = &latex_header_footer_remove($texversion);
  690: 	    } else {
  691: 		$texversion =~ s/\\end{document}//;
  692: 	    }
  693: 	    if($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') {
  694: 		my %form;
  695: 		$form{'grade_target'}='answer';
  696: 		$form{'answer_output_mode'}='tex';
  697: 		my $answer=&Apache::lonnet::ssi($urlp,%form);
  698: 		$texversion=~s/(\\keephidden{ENDOFPROBLEM})/$answer$1/;
  699: 	    }
  700: #	    $result .= $texversion;
  701: 	    if (($selectionmade == 4) and ($assignment ne $prevassignment) and ($i>=1)) {
  702: 		my $name = &get_name();
  703: 		if ($name =~ /^\s*$/) {
  704: 		    $name=&Apache::lonxml::latex_special_symbols($ENV{'user.name'},undef,
  705: 								 undef,'header');
  706: 		}
  707: 		my $courseidinfo = &Apache::lonxml::latex_special_symbols(&Apache::lonnet::unescape($ENV{'course.'.$ENV{'request.course.id'}.'.description'}),'','','header');
  708: 		$prevassignment=$assignment;
  709: 		$result .='\newpage \noindent\parbox{\minipagewidth}{\noindent\\lhead{\\textit{\\textbf{'.$name.'}} - '.$courseidinfo.' \\hfill \\thepage \\\\ \\textit{'.$assignment.'}}} \vskip -5 mm ';
  710: 	    }
  711: 	    $result .= $texversion;
  712: 	    $flag_latex_header_remove = 'YES';   
  713: 	}
  714: 	&Apache::lonnet::delenv('form.counter');
  715: 	if ($flag_page_in_sequence eq 'YES') {$result =~ s/\\usepackage{calc}/\\usepackage{calc}\\usepackage{longtable}/;}	
  716: 	$result .= '\end{document}';
  717:      } elsif ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'problems_for_students') { 
  718:      #-- prints assignments for whole class or for selected students  
  719: 	 $selectionmade=5;
  720: 	 my @students=split /\|\|\|/, $helper->{'VARS'}->{'STUDENTS'};
  721: 	 my @master_seq=split /\|\|\|/, $helper->{'VARS'}->{'RESOURCES'};
  722: 	 #loop over students
  723: 	 my $flag_latex_header_remove = 'NO'; 
  724: 	 my %moreenv;
  725: 	 $moreenv{'form.textwidth'}=$LaTeXwidth;
  726: 	 &Apache::lonnet::appenv(%moreenv);
  727: 	 my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin($r,'Print Status','Class Print Status',$#students+1);
  728: 	 foreach my $person (@students) {
  729: 	    my $current_output = ''; 
  730: #	    my ($usersection,$username,$userdomain) = split /:/,$person;
  731: 	    my ($username,$userdomain) = split /:/,$person;
  732: 	    my $fullname = &get_name($username,$userdomain);
  733: 
  734: 	    if ($parmhash{'anonymous_quiz'}=~/yes/) {$fullname=' ';} #put here something reasonable for anonymous exams or quiz
  735:             #goes through all resources, checks if they are available for current student, and produces output   
  736: 	    foreach my $curresline (@master_seq)  {
  737: 		if ($curresline=~ m/\.(problem|exam|quiz|assess|survey|form|library)$/) {
  738: 		    my ($map,$id,$res_url) = split(/___/,$curresline);
  739: 		    if (&Apache::lonnet::allowed('bre',$res_url)) {
  740: 			my $rendered = &Apache::loncommon::get_student_view($curresline,$username,$userdomain,
  741:                                                                         $ENV{'request.course.id'},'tex');
  742: 			if ($flag_latex_header_remove eq 'YES') {
  743: 			    $rendered = &latex_header_footer_remove($rendered);
  744: 			} else {
  745: 			    $rendered =~ s/\\end{document}//;
  746: 			}
  747: 			if($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') {
  748: 			    my %form;
  749: 			    $form{'answer_output_mode'}='tex';
  750: 			    my $ansrendered = &Apache::loncommon::get_student_answers($curresline,$username,$userdomain,$ENV{'request.course.id'},%form);
  751: 			    $rendered=~s/(\\keephidden{ENDOFPROBLEM})/$ansrendered$1/;
  752: 			}
  753: 			$current_output .= $rendered;
  754: 		    }
  755: 		    $flag_latex_header_remove = 'YES';
  756: 		}
  757: 	    }
  758: 	    my $courseidinfo = $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
  759: 	    if ($current_output=~/\\documentclass/) {
  760: 		$current_output =~ s/\\begin{document}/\\setlength{\\topmargin}{1cm} \\begin{document}\\noindent\\lhead{\\textit{\\textbf{$fullname}} - $courseidinfo \\hfill \\thepage \\\\ \\textit{$helper->{VARS}->{'assignment'}}}\\vskip 3 mm /;
  761: 	    } else {
  762: 		my $blanspages = '';
  763: 		for (my $j=0;$j<$helper->{'VARS'}->{'EMPTY_PAGES'};$j++) {$blanspages.='\clearpage\strut\clearpage';}
  764: 		$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{'.$helper->{VARS}->{'assignment'}.'}}} \vskip -5 mm '.$current_output;
  765: 	    }
  766: 	    $result .= $current_output;
  767: 	    &Apache::lonnet::delenv('form.counter');	    
  768: 	    &Apache::lonxml::init_counter(); 
  769: 	    &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
  770: 						    'last student '.$fullname);
  771: 	}
  772: 	&Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  773: 	$result .= '\end{document}';
  774: 	&Apache::lonnet::delenv('form.textwidth');
  775:     } elsif ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'problems_from_directory') {      
  776:     #prints selected problems from the subdirectory 
  777: 	$selectionmade = 6;
  778:         my @list_of_files=split /\|\|\|/, $helper->{'VARS'}->{'FILES'};
  779: 	@list_of_files=sort @list_of_files;
  780: 	my $flag_latex_header_remove = 'NO'; 
  781: 	my $rndseed=time;
  782: 	for (my $i=0;$i<=$#list_of_files;$i++) {
  783: 	    my $urlp = $list_of_files[$i];
  784: 	    if ($urlp=~/\//) {
  785: 		my %form;
  786: 		$form{'grade_target'}='tex';
  787: 		$form{'textwidth'}=$LaTeXwidth;
  788: 		$form{'rndseed'}=$rndseed;
  789: 		if ($urlp =~ m|/home/([^/]+)/public_html|) {
  790: 		    $urlp =~ s|/home/([^/]*)/public_html|/~$1|;
  791: 		} else {
  792: 		    $urlp =~ s|^/home/httpd/html||;
  793: 		}
  794: 		my $texversion=&Apache::lonnet::ssi($urlp,%form);
  795: 		if($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') {
  796: 		    my %form;
  797: 		    $form{'grade_target'}='answer';
  798: 		    $form{'answer_output_mode'}='tex';
  799: 		    $form{'rndseed'}=$rndseed;
  800: 		    my $answer=&Apache::lonnet::ssi($urlp,%form);
  801: 		    $texversion=~s/(\\keephidden{ENDOFPROBLEM})/$answer$1/;
  802: 		}
  803:                 #this chunck is responsible for printing the path to problem
  804: 		my $newurlp = '';
  805: 		my $HowMany = length($urlp)*2;
  806: 		if ($HowMany > $LaTeXwidth) {
  807: 		    my @temporrary = split '/',$urlp;
  808: 		    my $HowManyNew = 0;
  809: 		    for (my $ii=0;$ii<=$#temporrary;$ii++) {
  810: 			if ($temporrary[$ii] ne '') {
  811: 			    $HowManyNew += length($temporrary[$ii])*2;
  812: 			    if ($HowManyNew < $LaTeXwidth ) {
  813: 				$newurlp .=  '/'.$temporrary[$ii];
  814: 			    } else {
  815: 				$HowManyNew = 0;
  816: 				$newurlp .=  '|\vskip -1 mm \noindent \verb|';
  817: 				$ii--;
  818: 			    }
  819: 			}
  820: 		    }
  821: 		}
  822: 		$texversion =~ s/(\\begin{minipage}{\\textwidth})/$1 {\\small\\noindent\\verb|$newurlp\|\\vskip 0 mm}/;
  823: 		if ($flag_latex_header_remove ne 'NO') {
  824: 		    $texversion = &latex_header_footer_remove($texversion);
  825: 		} else {
  826: 		    $texversion =~ s/\\end{document}//;
  827: 		}
  828: 		$result .= $texversion;
  829: 	    }
  830: 	    $flag_latex_header_remove = 'YES';  
  831: 	}
  832: 	if ($helper->{VARS}->{'construction'} eq '1') {$result=~s/(\\typeout)/ RANDOM SEED IS $rndseed $1/;}
  833: 	$result .= '\end{document}';      	
  834:     }
  835: #-------------------------------------------------------- corrections for the different page formats
  836:     $result = &page_format_transformation($papersize,$laystyle,$numberofcolumns,$helper->{'VARS'}->{'PRINT_TYPE'},$result,$helper->{VARS}->{'assignment'});
  837:     $result = &latex_corrections($number_of_columns,$result);
  838:     #changes page's parameters for the one column output 
  839:     if ($numberofcolumns == 1) {
  840: 	$result =~ s/\\textwidth= 9cm/\\textwidth= $helper->{'VARS'}->{'pagesize.width'} $helper->{'VARS'}->{'pagesize.widthunit'} /;
  841: 	$result =~ s/\\textheight 25\.9cm/\\textheight $helper->{'VARS'}->{'pagesize.height'} $helper->{'VARS'}->{'pagesize.heightunit'} /;
  842: 	$result =~ s/\\evensidemargin = -0\.57in/\\evensidemargin= $helper->{'VARS'}->{'pagesize.lmargin'} $helper->{'VARS'}->{'pagesize.lmarginunit'} /;
  843: 	$result =~ s/\\oddsidemargin = -0\.57in/\\oddsidemargin= $helper->{'VARS'}->{'pagesize.lmargin'} $helper->{'VARS'}->{'pagesize.lmarginunit'} /;
  844:     }
  845: #-- writing .tex file in prtspool 
  846:     my $temp_file;
  847:     my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout_".time."_".rand(10000000).".tex";
  848:     unless ($temp_file = Apache::File->new('>'.$filename)) {
  849: 	$r->log_error("Couldn't open $filename for output $!");
  850: 	return SERVER_ERROR; 
  851:     } 
  852:     print $temp_file $result;
  853: 
  854: #<meta http-equiv="Refresh" content="0; url=/cgi-bin/printout.pl?$filename&$laystyle&$numberofcolumns&$selectionmade">
  855: $r->print(<<FINALEND);
  856: <meta http-equiv="Refresh" content="0; url=/cgi-bin/printout.pl?$filename&$laystyle&$numberofcolumns&$selectionmade">
  857: </body>
  858: </html>
  859: FINALEND
  860: }
  861: 
  862: 
  863: 
  864: sub handler {
  865: 
  866:     my $r = shift;
  867:     my $helper;
  868: 
  869: #    my $loaderror=&Apache::lonnet::overloaderror($r);
  870: #    if ($loaderror) { return $loaderror; }
  871: #    $loaderror=
  872: #       &Apache::lonnet::overloaderror($r,
  873: #         $ENV{'course.'.$ENV{'request.course.id'}.'.home'});
  874: #    if ($loaderror) { return $loaderror; }
  875: 
  876:     my $result = printHelper($r);
  877:     if (!ref($result)) {
  878: 	return $result;
  879:     }
  880:     $helper = $result;
  881:    
  882: #	my $key; 
  883: #	foreach $key (keys %{$helper->{'VARS'}}) {
  884: #	    $r->print(' '.$key.'->'.$helper->{'VARS'}->{$key}.'<-<br />');
  885: #	}
  886: #	return OK;
  887: 
  888:     my %parmhash=&Apache::lonnet::coursedescription($ENV{'request.course.id'});
  889:     
  890: #	my $key; 
  891: #	foreach $key (keys %parmhash) {
  892: #	    $r->print(' '.$key.'->'.$parmhash{$key}.'<-<br />');
  893: #	}
  894: #	return OK;
  895: 
  896:     &output_data($r,$helper,\%parmhash);
  897:     return OK;
  898: } 
  899: 
  900: use Apache::lonhelper;
  901: 
  902: sub printHelper {
  903:     my $r = shift;
  904: 
  905:     if ($ENV{'request.course.id'}) {
  906: 	my $fn=$ENV{'request.course.fn'};
  907: 	tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER(),0640);
  908:     }
  909: 
  910:     if ($r->header_only) {
  911:         if ($ENV{'browser.mathml'}) {
  912:             $r->content_type('text/xml');
  913:         } else {
  914:             $r->content_type('text/html');
  915:         }
  916:         $r->send_http_header;
  917:         return OK;
  918:     }
  919: 
  920:     # Send header, nocache
  921:     if ($ENV{'browser.mathml'}) {
  922:         $r->content_type('text/xml');
  923:     } else {
  924:         $r->content_type('text/html');
  925:     }
  926:     &Apache::loncommon::no_cache($r);
  927:     $r->send_http_header;
  928:     $r->rflush();
  929: 
  930:     # Unfortunately, this helper is so complicated we have to
  931:     # write it by hand
  932: 
  933:     Apache::loncommon::get_unprocessed_cgi($ENV{QUERY_STRING});
  934:     
  935:     my $helper = Apache::lonhelper::helper->new("Printing Helper");
  936:     $helper->declareVar('symb');
  937:     $helper->declareVar('postdata');    
  938:     $helper->declareVar('filename');
  939:     $helper->declareVar('construction');
  940:     $helper->declareVar('assignment');
  941:     
  942:     # This will persistently load in the data we want from the
  943:     # very first screen.
  944:     # Detect whether we're coming from construction space
  945:     if ($ENV{'form.postdata'} =~ /http:\/\// ) {
  946:         $ENV{'form.postdata'} =~ /http:\/\/[^\/]+\/~([^\/]+)\/(.*)/;
  947:         $helper->{VARS}->{'filename'} = "/home/$1/public_html/$2";
  948:         $helper->{VARS}->{'construction'} = 1;
  949:     } else {
  950:         if ($ENV{'form.postdata'}) {
  951:             $helper->{VARS}->{'symb'} = &Apache::lonnet::symbread($ENV{'form.postdata'});
  952:         }
  953:         if ($ENV{'form.symb'}) {
  954:             $helper->{VARS}->{'symb'} = $ENV{'form.symb'};
  955:         }
  956:         if ($ENV{'form.url'}) {
  957:             $helper->{VARS}->{'symb'} = &Apache::lonnet::symbread($helper->{VARS}->{'postdata'});
  958:         }
  959:     }
  960: 
  961:     if ($ENV{'form.symb'}) {
  962:         $helper->{VARS}->{'symb'} = $ENV{'form.symb'};
  963:     }
  964:     if ($ENV{'form.url'}) {
  965:         $helper->{VARS}->{'symb'} = &Apache::lonnet::symbread($helper->{VARS}->{'postdata'});
  966: 
  967:     }
  968:     
  969:     my ($resourceTitle,$sequenceTitle,$mapTitle) = &details_for_menu;
  970:     if ($sequenceTitle ne '') {$helper->{VARS}->{'assignment'}=$sequenceTitle;}
  971: 
  972:     
  973:     # Extract map
  974:     my $symb = $helper->{VARS}->{'symb'};
  975:     my ($map, $id, $url);
  976:     my $subdir;
  977: 
  978:     # Get the resource name from construction space
  979:     if ($helper->{VARS}->{'construction'}) {
  980:         $resourceTitle = substr($helper->{VARS}->{'filename'}, 
  981:                                 rindex($helper->{VARS}->{'filename'}, '/')+1);
  982:         $subdir = substr($helper->{VARS}->{'filename'},
  983:                          0, rindex($helper->{VARS}->{'filename'}, '/') + 1);
  984:     } else {
  985:         ($map, $id, $url) = split(/___/, $symb);
  986:         $helper->{VARS}->{'postdata'} = Apache::lonnet::clutter($url);
  987: 
  988:         if (!$resourceTitle) { # if the resource doesn't have a title, use the filename
  989:             my $url = $helper->{VARS}->{'postdata'};
  990:             $resourceTitle = substr($url, rindex($url, '/') + 1);
  991:         }
  992:         $subdir = &Apache::lonnet::filelocation("", $url);
  993:     }
  994: 
  995:     my $userCanSeeHidden = Apache::lonnavmaps::advancedUser();
  996: 
  997:     Apache::lonhelper::registerHelperTags();
  998: 
  999:     # "Delete everything after the last slash."
 1000:     $subdir =~ s|/[^/]+$||;
 1001:     if (not $helper->{VARS}->{'construction'}) {
 1002: 	$subdir='/home/httpd/html/res/'.$subdir;
 1003:     }
 1004:     # "Remove all duplicate slashes."
 1005:     $subdir =~ s|/+|/|g;
 1006: 
 1007:     # What can be printed is a very dynamic decision based on
 1008:     # lots of factors. So we need to dynamically build this list.
 1009:     # To prevent security leaks, states are only added to the wizard
 1010:     # if they can be reached, which ensures manipulating the form input
 1011:     # won't allow anyone to reach states they shouldn't have permission
 1012:     # to reach.
 1013: 
 1014:     # printChoices is tracking the kind of printing the user can
 1015:     # do, and will be used in a choices construction later.
 1016:     # In the meantime we will be adding states and elements to
 1017:     # the helper by hand.
 1018:     my $printChoices = [];
 1019:     my $paramHash;
 1020: 
 1021:     if ($resourceTitle) {
 1022:         push @{$printChoices}, ["<b>$resourceTitle</b> (prints what you just saw on the screen)", 'current_document', 'PAGESIZE'];
 1023:     }
 1024: 
 1025: #    $r->print($helper->{VARS}->{'postdata'});
 1026: 
 1027:     # If we're in a sequence...
 1028:     if ($helper->{'VARS'}->{'construction'} ne '1') {
 1029:         # Allow problems from sequence
 1030:         push @{$printChoices}, ["Problem(s) from <b>$sequenceTitle</b>", 'map_problems', 'CHOOSE_PROBLEMS'];
 1031:         # Allow all resources from sequence
 1032:         push @{$printChoices}, ["Everything (problem(s), page(s), html/xml file(s)) from <b>$sequenceTitle</b>", 'map_problems_pages', 'CHOOSE_PROBLEMS_HTML'];
 1033: 
 1034:         my $isProblem = '$res->is_problem()';
 1035: 	$isProblem .= ' && !$res->randomout()' if !$userCanSeeHidden;
 1036:         my $isProblemOrMap = '$res->is_problem() || $res->is_map()';
 1037:         my $isNotMap = '!$res->is_map()';
 1038: 	$isNotMap .= ' && !$res->randomout()' if !$userCanSeeHidden;
 1039:         my $symb = '$res->symb()';
 1040:         my $helperFragment = <<HELPERFRAGMENT;
 1041:   <state name="CHOOSE_PROBLEMS" title="Select Problem(s) to print">
 1042:     <message>(mark them then click "next" button) <br /></message>
 1043:     <resource variable="RESOURCES" multichoice="1" toponly='1'>
 1044:       <nextstate>PAGESIZE</nextstate>
 1045:       <filterfunc>return $isProblem;</filterfunc>
 1046:       <mapurl>$map</mapurl>
 1047:       <valuefunc>return $symb;</valuefunc>
 1048:       </resource>
 1049:     </state>
 1050: 
 1051:   <state name="CHOOSE_PROBLEMS_HTML" title="Select Resource(s) to print">
 1052:     <message>(mark them then click "next" button) <br /></message>
 1053:     <resource variable="RESOURCES" multichoice="1" toponly='1'>
 1054:       <nextstate>PAGESIZE</nextstate>
 1055:       <filterfunc>return $isNotMap;</filterfunc>
 1056:       <mapurl>$map</mapurl>
 1057:       <valuefunc>return $symb;</valuefunc>
 1058:       </resource>
 1059:     </state>
 1060: HELPERFRAGMENT
 1061: 
 1062:         &Apache::lonxml::xmlparse($r, 'helper', $helperFragment); 
 1063:     }
 1064: 
 1065:     # If the user is priviledged, allow them to print all 
 1066:     # problems in the course, optionally for selected students
 1067:     if (($ENV{'request.role'}=~m/^cc\./ or $ENV{'request.role'}=~m/^in\./ or $ENV{'request.role'}=~m/^ta\./) and ($helper->{VARS}->{'postdata'}=~/\/res\//)) { 
 1068:         push @{$printChoices}, ['<b>Problems</b> in this course', 'all_problems', 'ALL_PROBLEMS'];
 1069:         push @{$printChoices}, ["Problems from <b>$sequenceTitle</b> for selected students", 'problems_for_students', 'CHOOSE_STUDENTS'];
 1070: 
 1071:         my $isProblem = '$res->is_problem()';
 1072: 	$isProblem .= ' && !$res->randomout()' if !$userCanSeeHidden;
 1073:         my $isProblemOrMap = '($res->is_problem() || $res->is_map())';
 1074: 	$isProblemOrMap .= ' && !$res->randomout()' if !$userCanSeeHidden;
 1075:         my $symb = '$res->symb()';
 1076:         &Apache::lonxml::xmlparse($r, 'helper', <<CHOOSE_STUDENTS);
 1077:   <state name="ALL_PROBLEMS" title="Select Problem(s) to print">
 1078:     <message>(mark them then click "next" button) <br /></message>
 1079:     <resource variable="RESOURCES" multichoice="1" suppressEmptySequences='1'>
 1080:       <nextstate>PAGESIZE</nextstate>
 1081:       <filterfunc>return $isProblemOrMap;</filterfunc>
 1082:       <choicefunc>return $isProblem;</choicefunc>
 1083:       <valuefunc>return $symb;</valuefunc>
 1084:       </resource>
 1085:     </state>
 1086: 
 1087:   <state name="CHOOSE_STUDENTS" title="Choose Students whose assignments you want to print">
 1088:     <student multichoice='1' variable="STUDENTS" nextstate="PAGESIZE" />
 1089:     <message><br /><big><i><b>Select resources for the assignment</b></i></big><br /></message>
 1090:     <resource variable="RESOURCES" multichoice="1">
 1091:       <filterfunc>return $isProblem</filterfunc>
 1092:       <mapurl>$map</mapurl>
 1093:       <valuefunc>return $symb</valuefunc>
 1094:       </resource>
 1095:     <message><br /><big><i><b>How should the results be printed?</b></i></big><br /></message>
 1096:     <choices variable="EMPTY_PAGES">
 1097:       <choice computer='0'>Start each student\'s assignment on a new page (add a pagefeed after each assignment)</choice>
 1098:       <choice computer='1'>Add one empty page after each student\'s assignment</choice>
 1099:       <choice computer='2'>Add two empty pages after each student\'s assignment</choice>
 1100:       </choices>
 1101:     </state>
 1102: CHOOSE_STUDENTS
 1103:     }
 1104: 
 1105:     # FIXME: That RE should come from a library somewhere.
 1106:     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'}) {    
 1107:         push @{$printChoices}, ["Problems from current subdirectory <b>$subdir</b>", 'problems_from_directory', 'CHOOSE_FROM_SUBDIR'];
 1108:         
 1109:         my $f = '$filename';
 1110:         my $xmlfrag = <<CHOOSE_FROM_SUBDIR;
 1111:   <state name="CHOOSE_FROM_SUBDIR" title="Select File(s) from <b><small>$subdir</small></b> to print">
 1112:     <message>(mark them then click "next" button) <br /></message>
 1113:     <files variable="FILES" multichoice='1'>
 1114:       <nextstate>PAGESIZE</nextstate>
 1115:       <filechoice>return '$subdir';</filechoice>
 1116: CHOOSE_FROM_SUBDIR
 1117:         
 1118:         $xmlfrag .= <<'CHOOSE_FROM_SUBDIR';
 1119:       <filefilter>return $filename =~ 
 1120:            m/^[^\.]+\.(problem|exam|quiz|assess|survey|form|library)$/;
 1121:       </filefilter>
 1122:       </files>
 1123:     </state>
 1124: CHOOSE_FROM_SUBDIR
 1125:         &Apache::lonxml::xmlparse($r, 'helper', $xmlfrag);
 1126:     }
 1127: 
 1128:     # Generate the first state, to select which resources get printed.
 1129:     Apache::lonhelper::state->new("START", "What do you want to print? Make a choice.");
 1130:     $paramHash = Apache::lonhelper::getParamHash();
 1131:     $paramHash->{MESSAGE_TEXT} = "";
 1132:     Apache::lonhelper::message->new();
 1133:     $paramHash = Apache::lonhelper::getParamHash();
 1134:     $paramHash->{'variable'} = 'PRINT_TYPE';
 1135:     $helper->declareVar('PRINT_TYPE');
 1136:     $paramHash->{CHOICES} = $printChoices;
 1137:     Apache::lonhelper::choices->new();
 1138: 
 1139:     if (($ENV{'request.role.adv'} and &Apache::lonnet::allowed('vgr',$ENV{'request.course.id'})) or 
 1140: 	($helper->{VARS}->{'construction'} eq '1')) {
 1141:         $paramHash = Apache::lonhelper::getParamHash();
 1142:         $paramHash->{MESSAGE_TEXT} = "<br /><big><b><i>Next option is available only for advanced users:</i></b></big><br />";
 1143:         Apache::lonhelper::message->new();
 1144:         $paramHash = Apache::lonhelper::getParamHash();
 1145: 	$paramHash->{'variable'} = 'ANSWER_TYPE';   
 1146: 	$helper->declareVar('ANSWER_TYPE');         
 1147:         $paramHash->{CHOICES} = [
 1148:                                    ['Print without answer', 'yes'],
 1149:                                    ['Print with answers', 'no'] ];
 1150:         Apache::lonhelper::choices->new();
 1151:     }
 1152: 
 1153:     Apache::lonprintout::page_format_state->new("FORMAT");
 1154: 
 1155:     # Generate the PAGESIZE state which will offer the user the margin
 1156:     # choices if they select one column
 1157:     Apache::lonhelper::state->new("PAGESIZE", "Set Margins");
 1158:     Apache::lonprintout::page_size_state->new('pagesize', 'FORMAT', 'FINAL');
 1159: 
 1160: 
 1161:     $helper->process();
 1162: 
 1163:     # MANUAL BAILOUT CONDITION:
 1164:     # If we're in the "final" state, bailout and return to handler
 1165:     if ($helper->{STATE} eq 'FINAL') {
 1166:         return $helper;
 1167:     }    
 1168: 
 1169:     $r->print($helper->display());
 1170: 
 1171:     Apache::lonhelper::unregisterHelperTags();
 1172: 
 1173:     untie %hash;
 1174: 
 1175:     return OK;
 1176: }
 1177: 
 1178: 
 1179: 1;
 1180: 
 1181: package Apache::lonprintout::page_format_state;
 1182: 
 1183: =pod
 1184: 
 1185: =head1 Helper element: page_format_state
 1186: 
 1187: See lonhelper.pm documentation for discussion of the helper framework.
 1188: 
 1189: Apache::lonprintout::page_format_state is an element that gives the 
 1190: user an opportunity to select the page layout they wish to print 
 1191: with: Number of columns, portrait/landscape, and paper size. If you 
 1192: want to change the paper size choices, change the @paperSize array 
 1193: contents in this package.
 1194: 
 1195: page_format_state is always directly invoked in lonprintout.pm, so there
 1196: is no tag interface. You actually pass parameters to the constructor.
 1197: 
 1198: =over 4
 1199: 
 1200: =item * B<new>(varName): varName is where the print information will be stored in the format FIXME.
 1201: 
 1202: =back
 1203: 
 1204: =cut
 1205: 
 1206: use Apache::lonhelper;
 1207: 
 1208: no strict;
 1209: @ISA = ("Apache::lonhelper::element");
 1210: use strict;
 1211: 
 1212: my $maxColumns = 2;
 1213: my @paperSize = ("Letter [8 1/2x11 in]", "Legal [8 1/2x14 in]", 
 1214:                  "Ledger/Tabloid [11x17 in]", "Executive [7 1/2x10 in]",
 1215:                  "A2 [420x594 mm]", "A3 [297x420 mm]", "A4 [210x297 mm]", 
 1216:                  "A5 [148x210 mm]", "A6 [105x148 mm]" );
 1217: 
 1218: # Tentative format: Orientation (L = Landscape, P = portrait) | Colnum |
 1219: #                   Paper type
 1220: 
 1221: sub new { 
 1222:     my $self = Apache::lonhelper::element->new();
 1223: 
 1224:     shift;
 1225: 
 1226:     $self->{'variable'} = shift;
 1227:     my $helper = Apache::lonhelper::getHelper();
 1228:     $helper->declareVar($self->{'variable'});
 1229:     bless($self);
 1230:     return $self;
 1231: }
 1232: 
 1233: sub render {
 1234:     my $self = shift;
 1235:     my $helper = Apache::lonhelper::getHelper();
 1236:     my $result = '';
 1237:     my $var = $self->{'variable'};
 1238: 
 1239:     $result .= <<STATEHTML;
 1240: 
 1241: <p><big><i><b>What page format do you prefer?</b></i></big></p>
 1242: <table cellpadding="3">
 1243:   <tr>
 1244:     <td align="center"><b>Page layout</b></td>
 1245:     <td align="center"><b>Number of columns</b></td>
 1246:     <td align="center"><b>Paper type</b></td>
 1247:   </tr>
 1248:   <tr>
 1249:     <td>
 1250:       <input type="radio" name="${var}.layout" value="L" /> Landscape<br />
 1251:       <input type="radio" name="${var}.layout" value="P" checked='1'  /> Portrait
 1252:     </td>
 1253:     <td align="center">
 1254:       <select name="${var}.cols">
 1255: STATEHTML
 1256: 
 1257:     my $i;
 1258:     for ($i = 1; $i <= $maxColumns; $i++) {
 1259:         if ($i == 2) {
 1260:             $result .= "<option value='$i' selected>$i</option>\n";
 1261:         } else {
 1262:             $result .= "<option value='$i'>$i</option>\n";
 1263:         }
 1264:     }
 1265: 
 1266:     $result .= "</select></td><td>\n";
 1267:     $result .= "<select name='${var}.paper'>\n";
 1268: 
 1269:     $i = 0;
 1270:     foreach (@paperSize) {
 1271:         if ($i == 0) {
 1272:             $result .= "<option selected value='$i'>" . $paperSize[$i] . "</option>\n";
 1273:         } else {
 1274:             $result .= "<option value='$i'>" . $paperSize[$i] . "</option>\n";
 1275:         }
 1276:         $i++;
 1277:     }
 1278: 
 1279:     $result .= "</select></td></tr></table>";
 1280:     return $result;
 1281: }
 1282: 
 1283: sub postprocess {
 1284:     my $self = shift;
 1285: 
 1286:     my $var = $self->{'variable'};
 1287:     my $helper = Apache::lonhelper->getHelper();
 1288:     $helper->{VARS}->{$var} = 
 1289:         $ENV{"form.$var.layout"} . '|' . $ENV{"form.$var.cols"} . '|' .
 1290:         $ENV{"form.$var.paper"};
 1291:     return 1;
 1292: }
 1293: 
 1294: 1;
 1295: 
 1296: package Apache::lonprintout::page_size_state;
 1297: 
 1298: =pod
 1299: 
 1300: =head1 Helper element: page_size_state
 1301: 
 1302: See lonhelper.pm documentation for discussion of the helper framework.
 1303: 
 1304: Apache::lonprintout::page_size_state is an element that gives the 
 1305: user the opportunity to further refine the page settings if they
 1306: select a single-column page.
 1307: 
 1308: page_size_state is always directly invoked in lonprintout.pm, so there
 1309: is no tag interface. You actually pass parameters to the constructor.
 1310: 
 1311: =over 4
 1312: 
 1313: =item * B<new>(varName): varName is where the print information will be stored in the format FIXME.
 1314: 
 1315: =back
 1316: 
 1317: =cut
 1318: 
 1319: use Apache::lonhelper;
 1320: 
 1321: no strict;
 1322: @ISA = ("Apache::lonhelper::element");
 1323: use strict;
 1324: 
 1325: 
 1326: 
 1327: sub new { 
 1328:     my $self = Apache::lonhelper::element->new();
 1329: 
 1330:     shift; # disturbs me (probably prevents subclassing) but works (drops
 1331:            # package descriptor)... - Jeremy
 1332: 
 1333:     $self->{'variable'} = shift;
 1334:     my $helper = Apache::lonhelper::getHelper();
 1335:     $helper->declareVar($self->{'variable'});
 1336: 
 1337:     # The variable name of the format element, so we can look into 
 1338:     # $helper->{VARS} to figure out whether the columns are one or two
 1339:     $self->{'formatvar'} = shift;
 1340: 
 1341:     # The state to transition to after selection, or after discovering
 1342:     # the cols are not set to 1
 1343:     $self->{NEXTSTATE} = shift;
 1344:     bless($self);
 1345:     return $self;
 1346: }
 1347: 
 1348: sub render {
 1349:     my $self = shift;
 1350:     my $helper = Apache::lonhelper::getHelper();
 1351:     my $result = '';
 1352:     my $var = $self->{'variable'};
 1353: 
 1354:     if (defined $self->{ERROR_MSG}) {
 1355:         $result .= '<br /><font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br />';
 1356:     }
 1357: 
 1358:     $result .= <<ELEMENTHTML;
 1359: 
 1360: <p>How should the column be formatted?</p>
 1361: 
 1362: <table cellpadding='3'>
 1363:   <tr>
 1364:     <td align='right'><b>Width</b>:</td>
 1365:     <td align='left'><input type='text' name='$var.width' value='18' size='4'></td>
 1366:     <td align='left'>
 1367:       <select name='$var.widthunit'>
 1368:         <option>cm</option><option>in</option>
 1369:       </select>
 1370:     </td>
 1371:   </tr>
 1372:   <tr>
 1373:     <td align='right'><b>Height</b>:</td>
 1374:     <td align='left'><input type='text' name="$var.height" value="25.9" size='4'></td>
 1375:     <td align='left'>
 1376:       <select name='$var.heightunit'>
 1377:         <option>cm</option><option>in</option>
 1378:       </select>
 1379:     </td>
 1380:   </tr>
 1381:   <tr>
 1382:     <td align='right'><b>Left margin</b>:</td>
 1383:     <td align='left'><input type='text' name='$var.lmargin' value='-1.5' size='4'></td>
 1384:     <td align='left'>
 1385:       <select name='$var.lmarginunit'>
 1386:         <option>cm</option><option>in</option>
 1387:       </select>
 1388:     </td>
 1389:   </tr>
 1390: </table>
 1391: 
 1392: <p>Hint: Some instructors like to leave scratch space for the student by
 1393: making the width much smaller then the width of the page.</p>
 1394: 
 1395: ELEMENTHTML
 1396: 
 1397:     return $result;
 1398: }
 1399: 
 1400: # If the user didn't select 1 column, skip this state.
 1401: sub preprocess {
 1402:     my $self = shift;
 1403:     my $helper = Apache::lonhelper::getHelper();
 1404: 
 1405:     my $format = $helper->{VARS}->{$self->{'formatvar'}};
 1406:     if (substr($format, 2, 1) ne '1') {
 1407:         $helper->changeState($self->{NEXTSTATE});
 1408:     }
 1409:    
 1410:     return 1;
 1411: }
 1412: 
 1413: sub postprocess {
 1414:     my $self = shift;
 1415: 
 1416:     my $var = $self->{'variable'};
 1417:     my $helper = Apache::lonhelper->getHelper();
 1418:     my $width = $helper->{VARS}->{$var .'.width'} = $ENV{"form.${var}.width"}; 
 1419:     my $height = $helper->{VARS}->{$var .'.height'} = $ENV{"form.${var}.height"}; 
 1420:     my $lmargin = $helper->{VARS}->{$var .'.lmargin'} = $ENV{"form.${var}.lmargin"}; 
 1421:     $helper->{VARS}->{$var .'.widthunit'} = $ENV{"form.${var}.widthunit"}; 
 1422:     $helper->{VARS}->{$var .'.heightunit'} = $ENV{"form.${var}.heightunit"}; 
 1423:     $helper->{VARS}->{$var .'.lmarginunit'} = $ENV{"form.${var}.lmarginunit"}; 
 1424: 
 1425:     my $error = '';
 1426: 
 1427:     # /^-?[0-9]+(\.[0-9]*)?$/ -> optional minus, at least on digit, followed 
 1428:     # by an optional period, followed by digits, ending the string
 1429: 
 1430:     if ($width !~  /^-?[0-9]+(\.[0-9]*)?$/) {
 1431:         $error .= "Invalid width; please type only a number.<br />\n";
 1432:     }
 1433:     if ($height !~  /^-?[0-9]+(\.[0-9]*)?$/) {
 1434:         $error .= "Invalid height; please type only a number.<br />\n";
 1435:     }
 1436:     if ($lmargin !~  /^-?[0-9]+(\.[0-9]*)?$/) {
 1437:         $error .= "Invalid left margin; please type only a number.<br />\n";
 1438:     }
 1439: 
 1440:     if (!$error) {
 1441:         Apache::lonhelper::getHelper()->changeState($self->{NEXTSTATE});
 1442:         return 1;
 1443:     } else {
 1444:         $self->{ERROR_MSG} = $error;
 1445:         return 0;
 1446:     }
 1447: }
 1448: 
 1449: 
 1450: 
 1451: __END__
 1452: 

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