1: #
2: # The LearningOnline Network
3: # Printout
4: #
5: # $Id: lonprintout.pm,v 1.487 2006/10/17 09:56:16 foxr Exp $
6: #
7: # Copyright Michigan State University Board of Trustees
8: #
9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
10: #
11: # LON-CAPA is free software; you can redistribute it and/or modify
12: # it under the terms of the GNU General Public License as published by
13: # the Free Software Foundation; either version 2 of the License, or
14: # (at your option) any later version.
15: #
16: # LON-CAPA is distributed in the hope that it will be useful,
17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19: # GNU General Public License for more details.
20: #
21: # You should have received a copy of the GNU General Public License
22: # along with LON-CAPA; if not, write to the Free Software
23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24: #
25: # /home/httpd/html/adm/gpl.txt
26: # http://www.lon-capa.org/
27: #
28: #
29: package Apache::lonprintout;
30:
31: use strict;
32: use Apache::Constants qw(:common :http);
33: use Apache::lonxml;
34: use Apache::lonnet;
35: use Apache::loncommon;
36: use Apache::inputtags;
37: use Apache::grades;
38: use Apache::edit;
39: use Apache::File();
40: use Apache::lonnavmaps;
41: use Apache::lonratedt;
42: use POSIX qw(strftime);
43: use Apache::lonlocal;
44: use Carp;
45: use lib '/home/httpd/lib/perl/';
46: use LONCAPA;
47:
48: my %perm;
49: my %parmhash;
50: my $resources_printed;
51:
52: #
53: # printf_style_subst item format_string repl
54: #
55: # Does printf style substitution for a format string that
56: # can have %[n]item in it.. wherever, %[n]item occurs,
57: # rep is substituted in format_string. Note that
58: # [n] is an optional integer length. If provided,
59: # repl is truncated to at most [n] characters prior to
60: # substitution.
61: #
62: sub printf_style_subst {
63: my ($item, $format_string, $repl) = @_;
64:
65: while ($format_string =~ /%\d*\Q$item\E/) {
66: my $start = $-[0];
67: my $end = $+[0];
68: my $len = $end - $start;
69:
70: # see if we need to truncate:
71:
72: my $subst = $repl;
73: my $fmt = substr($format_string, $start, $len);
74: my $size = $fmt;
75: $size =~ s/%(\d*)\Q$item\E/$1/;
76: if ($size ne "") {
77: $subst = substr($subst, 0, $size);
78: }
79: $format_string =~ s/%(\d*)\Q$item\E/$subst/;
80:
81: }
82:
83: return $format_string;
84: }
85:
86:
87: # Format a header according to a format.
88: #
89:
90: # Substitutions:
91: # %a - Assignment name.
92: # %c - Course name.
93: # %n - Student name.
94: #
95: sub format_page_header {
96: my ($width, $format, $assignment, $course, $student) = @_;
97:
98: $width = &recalcto_mm($width); # Get width in mm.
99: # Default format?
100:
101: if ($format eq '') {
102: # For the default format, we may need to truncate
103: # elements.. To do this we need to get the page width.
104: # we assume that each character is about 2mm in width.
105: # (correct for the header text size??). We ignore
106: # any formatting (e.g. boldfacing in this).
107: #
108: # - Allow the student/course to be one line.
109: # but only truncate the course.
110: # - Allow the assignment to be 2 lines (wrapped).
111: #
112: my $chars_per_line = $width/2; # Character/textline.
113:
114:
115: my $firstline = "$student $course";
116: if (length($firstline) > $chars_per_line) {
117: my $lastchar = $chars_per_line - length($student) - 1;
118: if ($lastchar > 0) {
119: $course = substr($course, 0, $lastchar);
120: } else { # Nothing left of course:
121: $course = '';
122: }
123: }
124: if (length($assignment) > $chars_per_line) {
125: $assignment = substr($assignment, 0, $chars_per_line);
126: }
127:
128: $format = "\\textbf{$student} $course \\hfill \\thepage \\\\ \\textit{$assignment}";
129:
130: } else {
131: # An open question is how to handle long user formatted page headers...
132: # A possible future is to support e.g. %na so that the user can control
133: # the truncation of the elements that can appear in the header.
134: #
135: $format = &printf_style_subst("a", $format, $assignment);
136: $format = &printf_style_subst("c", $format, $course);
137: $format = &printf_style_subst("n", $format, $student);
138: }
139:
140:
141: return $format;
142:
143: }
144:
145: #
146: # Convert a numeric code to letters
147: #
148: sub num_to_letters {
149: my ($num) = @_;
150: my @nums= split('',$num);
151: my @num_to_let=('A'..'Z');
152: my $word;
153: foreach my $digit (@nums) { $word.=$num_to_let[$digit]; }
154: return $word;
155: }
156: # Convert a letter code to numeric.
157: #
158: sub letters_to_num {
159: my ($letters) = @_;
160: my @letters = split('', uc($letters));
161: my %substitution;
162: my $digit = 0;
163: foreach my $letter ('A'..'J') {
164: $substitution{$letter} = $digit;
165: $digit++;
166: }
167: # The substitution is done as below to preserve leading
168: # zeroes which are needed to keep the code size exact
169: #
170: my $result ="";
171: foreach my $letter (@letters) {
172: $result.=$substitution{$letter};
173: }
174: return $result;
175: }
176:
177: # Determine if a code is a valid numeric code. Valid
178: # numeric codes must be comprised entirely of digits and
179: # have a correct number of digits.
180: #
181: # Parameters:
182: # value - proposed code value.
183: # num_digits - Number of digits required.
184: #
185: sub is_valid_numeric_code {
186: my ($value, $num_digits) = @_;
187: # Remove leading/trailing whitespace;
188: $value =~ s/^\s*//g;
189: $value =~ s/\s*$//g;
190:
191: # All digits?
192: if ($value !~ /^[0-9]+$/) {
193: return "Numeric code $value has invalid characters - must only be digits";
194: }
195: if (length($value) != $num_digits) {
196: return "Numeric code $value incorrect number of digits (correct = $num_digits)";
197: }
198: return undef;
199: }
200: # Determines if a code is a valid alhpa code. Alpha codes
201: # are ciphers that map [A-J,a-j] -> 0..9 0..9.
202: # They also have a correct digit count.
203: # Parameters:
204: # value - Proposed code value.
205: # num_letters - correct number of letters.
206: # Note:
207: # leading and trailing whitespace are ignored.
208: #
209: sub is_valid_alpha_code {
210: my ($value, $num_letters) = @_;
211:
212: # strip leading and trailing spaces.
213:
214: $value =~ s/^\s*//g;
215: $value =~ s/\s*$//g;
216:
217: # All alphas in the right range?
218: if ($value !~ /^[A-J,a-j]+$/) {
219: return "Invalid letter code $value must only contain A-J";
220: }
221: if (length($value) != $num_letters) {
222: return "Letter code $value has incorrect number of letters (correct = $num_letters)";
223: }
224: return undef;
225: }
226:
227: # Determine if a code entered by the user in a helper is valid.
228: # valid depends on the code type and the type of code selected.
229: # The type of code selected can either be numeric or
230: # Alphabetic. If alphabetic, the code, in fact is a simple
231: # substitution cipher for the actual numeric code: 0->A, 1->B ...
232: # We'll be nice and be case insensitive for alpha codes.
233: # Parameters:
234: # code_value - the value of the code the user typed in.
235: # code_option - The code type selected from the set in the scantron format
236: # table.
237: # Returns:
238: # undef - The code is valid.
239: # other - An error message indicating what's wrong.
240: #
241: sub is_code_valid {
242: my ($code_value, $code_option) = @_;
243: my ($code_type, $code_length) = ('letter', 6); # defaults.
244: open(FG, $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab');
245: foreach my $line (<FG>) {
246: my ($name, $type, $length) = (split(/:/, $line))[0,2,4];
247: if($name eq $code_option) {
248: $code_length = $length;
249: if($type eq 'number') {
250: $code_type = 'number';
251: }
252: }
253: }
254: my $valid;
255: if ($code_type eq 'number') {
256: return &is_valid_numeric_code($code_value, $code_length);
257: } else {
258: return &is_valid_alpha_code($code_value, $code_length);
259: }
260:
261: }
262:
263: # Compare two students by name. The students are in the form
264: # returned by the helper:
265: # user:domain:section:last, first:status
266: # This is a helper function for the perl sort built-in therefore:
267: # Implicit Inputs:
268: # $a - The first element to compare (global)
269: # $b - The second element to compare (global)
270: # Returns:
271: # -1 - $a < $b
272: # 0 - $a == $b
273: # +1 - $a > $b
274: # Note that the initial comparison is done on the last names with the
275: # first names only used to break the tie.
276: #
277: #
278: sub compare_names {
279: # First split the names up into the primary fields.
280:
281: my ($u1, $d1, $s1, $n1, $stat1) = split(/:/, $a);
282: my ($u2, $d2, $s2, $n2, $stat2) = split(/:/, $b);
283:
284: # Now split the last name and first name of each n:
285: #
286:
287: my ($l1,$f1) = split(/,/, $n1);
288: my ($l2,$f2) = split(/,/, $n2);
289:
290: # We don't bother to remove the leading/trailing whitespace from the
291: # firstname, unless the last names compare identical.
292:
293: if($l1 lt $l2) {
294: return -1;
295: }
296: if($l1 gt $l2) {
297: return 1;
298: }
299:
300: # Break the tie on the first name, but there are leading (possibly trailing
301: # whitespaces to get rid of first
302: #
303: $f1 =~ s/^\s+//; # Remove leading...
304: $f1 =~ s/\s+$//; # Trailing spaces from first 1...
305:
306: $f2 =~ s/^\s+//;
307: $f2 =~ s/\s+$//; # And the same for first 2...
308:
309: if($f1 lt $f2) {
310: return -1;
311: }
312: if($f1 gt $f2) {
313: return 1;
314: }
315:
316: # Must be the same name.
317:
318: return 0;
319: }
320:
321: sub latex_header_footer_remove {
322: my $text = shift;
323: $text =~ s/\\end{document}//;
324: $text =~ s/\\documentclass([^&]*)\\begin{document}//;
325: return $text;
326: }
327: #
328: # If necessary, encapsulate text inside
329: # a minipage env.
330: # necessity is determined by the problem_split param.
331: #
332: sub encapsulate_minipage {
333: my ($text) = @_;
334: if (!($env{'form.problem.split'} =~ /yes/i)) {
335: $text = '\begin{minipage}{\textwidth}'.$text.'\end{minipage}';
336: }
337: return $text;
338: }
339: #
340: # The NUMBER_TO_PRINT and SPLIT_PDFS
341: # variables interact, this sub looks at these two parameters
342: # and comes up with a final value for NUMBER_TO_PRINT which can be:
343: # all - if SPLIT_PDFS eq 'all'.
344: # 1 - if SPLIT_PDFS eq 'oneper'
345: # section - if SPLIT_PDFS eq 'sections'
346: # <unchanged> - if SPLIT_PDFS eq 'usenumber'
347: #
348: sub adjust_number_to_print {
349: my $helper = shift;
350:
351: my $split_pdf = $helper->{'VARS'}->{'SPLIT_PDFS'};
352:
353: if ($split_pdf eq 'all') {
354: $helper->{'VARS'}->{'NUMBER_TO_PRINT'} = 'all';
355: } elsif ($split_pdf eq 'oneper') {
356: $helper->{'VARS'}->{'NUMBER_TO_PRINT'} = 1;
357: } elsif ($split_pdf eq 'sections') {
358: $helper->{'VARS'}->{'NUMBER_TO_PRINT'} = 'section';
359: } elsif ($split_pdf eq 'usenumber') {
360: # Unmodified.
361: } else {
362: # Error!!!!
363:
364: croak "bad SPLIT_PDFS: $split_pdf in lonprintout::adjust_number_to_print";
365: }
366: }
367:
368: sub character_chart {
369: my $result = shift;
370: $result =~ s/&\#0?0?(7|9);//g;
371: $result =~ s/&\#0?(10|13);//g;
372: $result =~ s/&\#0?32;/ /g;
373: $result =~ s/&\#0?33;/!/g;
374: $result =~ s/&(\#0?34|quot);/\"/g;
375: $result =~ s/&\#0?35;/\\\#/g;
376: $result =~ s/&\#0?36;/\\\$/g;
377: $result =~ s/&\#0?37;/\\%/g;
378: $result =~ s/&(\#0?38|amp);/\\&/g;
379: $result =~ s/&\#(0?39|146);/\'/g;
380: $result =~ s/&\#0?40;/(/g;
381: $result =~ s/&\#0?41;/)/g;
382: $result =~ s/&\#0?42;/\*/g;
383: $result =~ s/&\#0?43;/\+/g;
384: $result =~ s/&\#(0?44|130);/,/g;
385: $result =~ s/&\#0?45;/-/g;
386: $result =~ s/&\#0?46;/\./g;
387: $result =~ s/&\#0?47;/\//g;
388: $result =~ s/&\#0?48;/0/g;
389: $result =~ s/&\#0?49;/1/g;
390: $result =~ s/&\#0?50;/2/g;
391: $result =~ s/&\#0?51;/3/g;
392: $result =~ s/&\#0?52;/4/g;
393: $result =~ s/&\#0?53;/5/g;
394: $result =~ s/&\#0?54;/6/g;
395: $result =~ s/&\#0?55;/7/g;
396: $result =~ s/&\#0?56;/8/g;
397: $result =~ s/&\#0?57;/9/g;
398: $result =~ s/&\#0?58;/:/g;
399: $result =~ s/&\#0?59;/;/g;
400: $result =~ s/&(\#0?60|lt|\#139);/\$<\$/g;
401: $result =~ s/&\#0?61;/\\ensuremath\{=\}/g;
402: $result =~ s/&(\#0?62|gt|\#155);/\\ensuremath\{>\}/g;
403: $result =~ s/&\#0?63;/\?/g;
404: $result =~ s/&\#0?65;/A/g;
405: $result =~ s/&\#0?66;/B/g;
406: $result =~ s/&\#0?67;/C/g;
407: $result =~ s/&\#0?68;/D/g;
408: $result =~ s/&\#0?69;/E/g;
409: $result =~ s/&\#0?70;/F/g;
410: $result =~ s/&\#0?71;/G/g;
411: $result =~ s/&\#0?72;/H/g;
412: $result =~ s/&\#0?73;/I/g;
413: $result =~ s/&\#0?74;/J/g;
414: $result =~ s/&\#0?75;/K/g;
415: $result =~ s/&\#0?76;/L/g;
416: $result =~ s/&\#0?77;/M/g;
417: $result =~ s/&\#0?78;/N/g;
418: $result =~ s/&\#0?79;/O/g;
419: $result =~ s/&\#0?80;/P/g;
420: $result =~ s/&\#0?81;/Q/g;
421: $result =~ s/&\#0?82;/R/g;
422: $result =~ s/&\#0?83;/S/g;
423: $result =~ s/&\#0?84;/T/g;
424: $result =~ s/&\#0?85;/U/g;
425: $result =~ s/&\#0?86;/V/g;
426: $result =~ s/&\#0?87;/W/g;
427: $result =~ s/&\#0?88;/X/g;
428: $result =~ s/&\#0?89;/Y/g;
429: $result =~ s/&\#0?90;/Z/g;
430: $result =~ s/&\#0?91;/[/g;
431: $result =~ s/&\#0?92;/\\ensuremath\{\\setminus\}/g;
432: $result =~ s/&\#0?93;/]/g;
433: $result =~ s/&\#(0?94|136);/\\ensuremath\{\\wedge\}/g;
434: $result =~ s/&\#(0?95|138|154);/\\underline{\\makebox[2mm]{\\strut}}/g;
435: $result =~ s/&\#(0?96|145);/\`/g;
436: $result =~ s/&\#0?97;/a/g;
437: $result =~ s/&\#0?98;/b/g;
438: $result =~ s/&\#0?99;/c/g;
439: $result =~ s/&\#100;/d/g;
440: $result =~ s/&\#101;/e/g;
441: $result =~ s/&\#102;/f/g;
442: $result =~ s/&\#103;/g/g;
443: $result =~ s/&\#104;/h/g;
444: $result =~ s/&\#105;/i/g;
445: $result =~ s/&\#106;/j/g;
446: $result =~ s/&\#107;/k/g;
447: $result =~ s/&\#108;/l/g;
448: $result =~ s/&\#109;/m/g;
449: $result =~ s/&\#110;/n/g;
450: $result =~ s/&\#111;/o/g;
451: $result =~ s/&\#112;/p/g;
452: $result =~ s/&\#113;/q/g;
453: $result =~ s/&\#114;/r/g;
454: $result =~ s/&\#115;/s/g;
455: $result =~ s/&\#116;/t/g;
456: $result =~ s/&\#117;/u/g;
457: $result =~ s/&\#118;/v/g;
458: $result =~ s/&\#119;/w/g;
459: $result =~ s/&\#120;/x/g;
460: $result =~ s/&\#121;/y/g;
461: $result =~ s/&\#122;/z/g;
462: $result =~ s/&\#123;/\\{/g;
463: $result =~ s/&\#124;/\|/g;
464: $result =~ s/&\#125;/\\}/g;
465: $result =~ s/&\#126;/\~/g;
466: $result =~ s/&\#131;/\\textflorin /g;
467: $result =~ s/&\#132;/\"/g;
468: $result =~ s/&\#133;/\\ensuremath\{\\ldots\}/g;
469: $result =~ s/&\#134;/\\ensuremath\{\\dagger\}/g;
470: $result =~ s/&\#135;/\\ensuremath\{\\ddagger\}/g;
471: $result =~ s/&\#137;/\\textperthousand /g;
472: $result =~ s/&\#140;/{\\OE}/g;
473: $result =~ s/&\#147;/\`\`/g;
474: $result =~ s/&\#148;/\'\'/g;
475: $result =~ s/&\#149;/\\ensuremath\{\\bullet\}/g;
476: $result =~ s/&\#150;/--/g;
477: $result =~ s/&\#151;/---/g;
478: $result =~ s/&\#152;/\\ensuremath\{\\sim\}/g;
479: $result =~ s/&\#153;/\\texttrademark /g;
480: $result =~ s/&\#156;/\\oe/g;
481: $result =~ s/&\#159;/\\\"Y/g;
482: $result =~ s/&(\#160|nbsp);/~/g;
483: $result =~ s/&(\#161|iexcl);/!\`/g;
484: $result =~ s/&(\#162|cent);/\\textcent /g;
485: $result =~ s/&(\#163|pound);/\\pounds /g;
486: $result =~ s/&(\#164|curren);/\\textcurrency /g;
487: $result =~ s/&(\#165|yen);/\\textyen /g;
488: $result =~ s/&(\#166|brvbar);/\\textbrokenbar /g;
489: $result =~ s/&(\#167|sect);/\\textsection /g;
490: $result =~ s/&(\#168|uml);/\\texthighdieresis /g;
491: $result =~ s/&(\#169|copy);/\\copyright /g;
492: $result =~ s/&(\#170|ordf);/\\textordfeminine /g;
493: $result =~ s/&(\#172|not);/\\ensuremath\{\\neg\}/g;
494: $result =~ s/&(\#173|shy);/ - /g;
495: $result =~ s/&(\#174|reg);/\\textregistered /g;
496: $result =~ s/&(\#175|macr);/\\ensuremath\{^{-}\}/g;
497: $result =~ s/&(\#176|deg);/\\ensuremath\{^{\\circ}\}/g;
498: $result =~ s/&(\#177|plusmn);/\\ensuremath\{\\pm\}/g;
499: $result =~ s/&(\#178|sup2);/\\ensuremath\{^2\}/g;
500: $result =~ s/&(\#179|sup3);/\\ensuremath\{^3\}/g;
501: $result =~ s/&(\#180|acute);/\\textacute /g;
502: $result =~ s/&(\#181|micro);/\\ensuremath\{\\mu\}/g;
503: $result =~ s/&(\#182|para);/\\P/g;
504: $result =~ s/&(\#183|middot);/\\ensuremath\{\\cdot\}/g;
505: $result =~ s/&(\#184|cedil);/\\c{\\strut}/g;
506: $result =~ s/&(\#185|sup1);/\\ensuremath\{^1\}/g;
507: $result =~ s/&(\#186|ordm);/\\textordmasculine /g;
508: $result =~ s/&(\#188|frac14);/\\textonequarter /g;
509: $result =~ s/&(\#189|frac12);/\\textonehalf /g;
510: $result =~ s/&(\#190|frac34);/\\textthreequarters /g;
511: $result =~ s/&(\#191|iquest);/?\`/g;
512: $result =~ s/&(\#192|Agrave);/\\\`{A}/g;
513: $result =~ s/&(\#193|Aacute);/\\\'{A}/g;
514: $result =~ s/&(\#194|Acirc);/\\^{A}/g;
515: $result =~ s/&(\#195|Atilde);/\\~{A}/g;
516: $result =~ s/&(\#196|Auml);/\\\"{A}/g;
517: $result =~ s/&(\#197|Aring);/{\\AA}/g;
518: $result =~ s/&(\#198|AElig);/{\\AE}/g;
519: $result =~ s/&(\#199|Ccedil);/\\c{c}/g;
520: $result =~ s/&(\#200|Egrave);/\\\`{E}/g;
521: $result =~ s/&(\#201|Eacute);/\\\'{E}/g;
522: $result =~ s/&(\#202|Ecirc);/\\^{E}/g;
523: $result =~ s/&(\#203|Euml);/\\\"{E}/g;
524: $result =~ s/&(\#204|Igrave);/\\\`{I}/g;
525: $result =~ s/&(\#205|Iacute);/\\\'{I}/g;
526: $result =~ s/&(\#206|Icirc);/\\^{I}/g;
527: $result =~ s/&(\#207|Iuml);/\\\"{I}/g;
528: $result =~ s/&(\#209|Ntilde);/\\~{N}/g;
529: $result =~ s/&(\#210|Ograve);/\\\`{O}/g;
530: $result =~ s/&(\#211|Oacute);/\\\'{O}/g;
531: $result =~ s/&(\#212|Ocirc);/\\^{O}/g;
532: $result =~ s/&(\#213|Otilde);/\\~{O}/g;
533: $result =~ s/&(\#214|Ouml);/\\\"{O}/g;
534: $result =~ s/&(\#215|times);/\\ensuremath\{\\times\}/g;
535: $result =~ s/&(\#216|Oslash);/{\\O}/g;
536: $result =~ s/&(\#217|Ugrave);/\\\`{U}/g;
537: $result =~ s/&(\#218|Uacute);/\\\'{U}/g;
538: $result =~ s/&(\#219|Ucirc);/\\^{U}/g;
539: $result =~ s/&(\#220|Uuml);/\\\"{U}/g;
540: $result =~ s/&(\#221|Yacute);/\\\'{Y}/g;
541: $result =~ s/&(\#223|szlig);/{\\ss}/g;
542: $result =~ s/&(\#224|agrave);/\\\`{a}/g;
543: $result =~ s/&(\#225|aacute);/\\\'{a}/g;
544: $result =~ s/&(\#226|acirc);/\\^{a}/g;
545: $result =~ s/&(\#227|atilde);/\\~{a}/g;
546: $result =~ s/&(\#228|auml);/\\\"{a}/g;
547: $result =~ s/&(\#229|aring);/{\\aa}/g;
548: $result =~ s/&(\#230|aelig);/{\\ae}/g;
549: $result =~ s/&(\#231|ccedil);/\\c{c}/g;
550: $result =~ s/&(\#232|egrave);/\\\`{e}/g;
551: $result =~ s/&(\#233|eacute);/\\\'{e}/g;
552: $result =~ s/&(\#234|ecirc);/\\^{e}/g;
553: $result =~ s/&(\#235|euml);/\\\"{e}/g;
554: $result =~ s/&(\#236|igrave);/\\\`{i}/g;
555: $result =~ s/&(\#237|iacute);/\\\'{i}/g;
556: $result =~ s/&(\#238|icirc);/\\^{i}/g;
557: $result =~ s/&(\#239|iuml);/\\\"{i}/g;
558: $result =~ s/&(\#240|eth);/\\ensuremath\{\\partial\}/g;
559: $result =~ s/&(\#241|ntilde);/\\~{n}/g;
560: $result =~ s/&(\#242|ograve);/\\\`{o}/g;
561: $result =~ s/&(\#243|oacute);/\\\'{o}/g;
562: $result =~ s/&(\#244|ocirc);/\\^{o}/g;
563: $result =~ s/&(\#245|otilde);/\\~{o}/g;
564: $result =~ s/&(\#246|ouml);/\\\"{o}/g;
565: $result =~ s/&(\#247|divide);/\\ensuremath\{\\div\}/g;
566: $result =~ s/&(\#248|oslash);/{\\o}/g;
567: $result =~ s/&(\#249|ugrave);/\\\`{u}/g;
568: $result =~ s/&(\#250|uacute);/\\\'{u}/g;
569: $result =~ s/&(\#251|ucirc);/\\^{u}/g;
570: $result =~ s/&(\#252|uuml);/\\\"{u}/g;
571: $result =~ s/&(\#253|yacute);/\\\'{y}/g;
572: $result =~ s/&(\#255|yuml);/\\\"{y}/g;
573: $result =~ s/&\#295;/\\ensuremath\{\\hbar\}/g;
574: $result =~ s/&\#952;/\\ensuremath\{\\theta\}/g;
575: #Greek Alphabet
576: $result =~ s/&(alpha|\#945);/\\ensuremath\{\\alpha\}/g;
577: $result =~ s/&(beta|\#946);/\\ensuremath\{\\beta\}/g;
578: $result =~ s/&(gamma|\#947);/\\ensuremath\{\\gamma\}/g;
579: $result =~ s/&(delta|\#948);/\\ensuremath\{\\delta\}/g;
580: $result =~ s/&(epsilon|\#949);/\\ensuremath\{\\epsilon\}/g;
581: $result =~ s/&(zeta|\#950);/\\ensuremath\{\\zeta\}/g;
582: $result =~ s/&(eta|\#951);/\\ensuremath\{\\eta\}/g;
583: $result =~ s/&(theta|\#952);/\\ensuremath\{\\theta\}/g;
584: $result =~ s/&(iota|\#953);/\\ensuremath\{\\iota\}/g;
585: $result =~ s/&(kappa|\#954);/\\ensuremath\{\\kappa\}/g;
586: $result =~ s/&(lambda|\#955);/\\ensuremath\{\\lambda\}/g;
587: $result =~ s/&(mu|\#956);/\\ensuremath\{\\mu\}/g;
588: $result =~ s/&(nu|\#957);/\\ensuremath\{\\nu\}/g;
589: $result =~ s/&(xi|\#958);/\\ensuremath\{\\xi\}/g;
590: $result =~ s/&(omicron|\#959);/o/g;
591: $result =~ s/&(pi|\#960);/\\ensuremath\{\\pi\}/g;
592: $result =~ s/&(rho|\#961);/\\ensuremath\{\\rho\}/g;
593: $result =~ s/&(sigma|\#963);/\\ensuremath\{\\sigma\}/g;
594: $result =~ s/&(tau|\#964);/\\ensuremath\{\\tau\}/g;
595: $result =~ s/&(upsilon|\#965);/\\ensuremath\{\\upsilon\}/g;
596: $result =~ s/&(phi|\#966);/\\ensuremath\{\\phi\}/g;
597: $result =~ s/&(chi|\#967);/\\ensuremath\{\\chi\}/g;
598: $result =~ s/&(psi|\#968);/\\ensuremath\{\\psi\}/g;
599: $result =~ s/&(omega|\#969);/\\ensuremath\{\\omega\}/g;
600: $result =~ s/&(thetasym|\#977);/\\ensuremath\{\\vartheta\}/g;
601: $result =~ s/&(piv|\#982);/\\ensuremath\{\\varpi\}/g;
602: $result =~ s/&(Alpha|\#913);/A/g;
603: $result =~ s/&(Beta|\#914);/B/g;
604: $result =~ s/&(Gamma|\#915);/\\ensuremath\{\\Gamma\}/g;
605: $result =~ s/&(Delta|\#916);/\\ensuremath\{\\Delta\}/g;
606: $result =~ s/&(Epsilon|\#917);/E/g;
607: $result =~ s/&(Zeta|\#918);/Z/g;
608: $result =~ s/&(Eta|\#919);/H/g;
609: $result =~ s/&(Theta|\#920);/\\ensuremath\{\\Theta\}/g;
610: $result =~ s/&(Iota|\#921);/I/g;
611: $result =~ s/&(Kappa|\#922);/K/g;
612: $result =~ s/&(Lambda|\#923);/\\ensuremath\{\\Lambda\}/g;
613: $result =~ s/&(Mu|\#924);/M/g;
614: $result =~ s/&(Nu|\#925);/N/g;
615: $result =~ s/&(Xi|\#926);/\\ensuremath\{\\Xi\}/g;
616: $result =~ s/&(Omicron|\#927);/O/g;
617: $result =~ s/&(Pi|\#928);/\\ensuremath\{\\Pi\}/g;
618: $result =~ s/&(Rho|\#929);/P/g;
619: $result =~ s/&(Sigma|\#931);/\\ensuremath\{\\Sigma\}/g;
620: $result =~ s/&(Tau|\#932);/T/g;
621: $result =~ s/&(Upsilon|\#933);/\\ensuremath\{\\Upsilon\}/g;
622: $result =~ s/&(Phi|\#934);/\\ensuremath\{\\Phi\}/g;
623: $result =~ s/&(Chi|\#935);/X/g;
624: $result =~ s/&(Psi|\#936);/\\ensuremath\{\\Psi\}/g;
625: $result =~ s/&(Omega|\#937);/\\ensuremath\{\\Omega\}/g;
626: #Arrows (extended HTML 4.01)
627: $result =~ s/&(larr|\#8592);/\\ensuremath\{\\leftarrow\}/g;
628: $result =~ s/&(uarr|\#8593);/\\ensuremath\{\\uparrow\}/g;
629: $result =~ s/&(rarr|\#8594);/\\ensuremath\{\\rightarrow\}/g;
630: $result =~ s/&(darr|\#8595);/\\ensuremath\{\\downarrow\}/g;
631: $result =~ s/&(harr|\#8596);/\\ensuremath\{\\leftrightarrow\}/g;
632: $result =~ s/&(lArr|\#8656);/\\ensuremath\{\\Leftarrow\}/g;
633: $result =~ s/&(uArr|\#8657);/\\ensuremath\{\\Uparrow\}/g;
634: $result =~ s/&(rArr|\#8658);/\\ensuremath\{\\Rightarrow\}/g;
635: $result =~ s/&(dArr|\#8659);/\\ensuremath\{\\Downarrow\}/g;
636: $result =~ s/&(hArr|\#8660);/\\ensuremath\{\\Leftrightarrow\}/g;
637: #Mathematical Operators (extended HTML 4.01)
638: $result =~ s/&(forall|\#8704);/\\ensuremath\{\\forall\}/g;
639: $result =~ s/&(part|\#8706);/\\ensuremath\{\\partial\}/g;
640: $result =~ s/&(exist|\#8707);/\\ensuremath\{\\exists\}/g;
641: $result =~ s/&(empty|\#8709);/\\ensuremath\{\\emptyset\}/g;
642: $result =~ s/&(nabla|\#8711);/\\ensuremath\{\\nabla\}/g;
643: $result =~ s/&(isin|\#8712);/\\ensuremath\{\\in\}/g;
644: $result =~ s/&(notin|\#8713);/\\ensuremath\{\\notin\}/g;
645: $result =~ s/&(ni|\#8715);/\\ensuremath\{\\ni\}/g;
646: $result =~ s/&(prod|\#8719);/\\ensuremath\{\\prod\}/g;
647: $result =~ s/&(sum|\#8721);/\\ensuremath\{\\sum\}/g;
648: $result =~ s/&(minus|\#8722);/\\ensuremath\{-\}/g;
649: $result =~ s/–/\\ensuremath\{-\}/g;
650: $result =~ s/&(lowast|\#8727);/\\ensuremath\{*\}/g;
651: $result =~ s/&(radic|\#8730);/\\ensuremath\{\\surd\}/g;
652: $result =~ s/&(prop|\#8733);/\\ensuremath\{\\propto\}/g;
653: $result =~ s/&(infin|\#8734);/\\ensuremath\{\\infty\}/g;
654: $result =~ s/&(ang|\#8736);/\\ensuremath\{\\angle\}/g;
655: $result =~ s/&(and|\#8743);/\\ensuremath\{\\wedge\}/g;
656: $result =~ s/&(or|\#8744);/\\ensuremath\{\\vee\}/g;
657: $result =~ s/&(cap|\#8745);/\\ensuremath\{\\cap\}/g;
658: $result =~ s/&(cup|\#8746);/\\ensuremath\{\\cup\}/g;
659: $result =~ s/&(int|\#8747);/\\ensuremath\{\\int\}/g;
660: $result =~ s/&(sim|\#8764);/\\ensuremath\{\\sim\}/g;
661: $result =~ s/&(cong|\#8773);/\\ensuremath\{\\cong\}/g;
662: $result =~ s/&(asymp|\#8776);/\\ensuremath\{\\approx\}/g;
663: $result =~ s/&(ne|\#8800);/\\ensuremath\{\\not=\}/g;
664: $result =~ s/&(equiv|\#8801);/\\ensuremath\{\\equiv\}/g;
665: $result =~ s/&(le|\#8804);/\\ensuremath\{\\leq\}/g;
666: $result =~ s/&(ge|\#8805);/\\ensuremath\{\\geq\}/g;
667: $result =~ s/&(sub|\#8834);/\\ensuremath\{\\subset\}/g;
668: $result =~ s/&(sup|\#8835);/\\ensuremath\{\\supset\}/g;
669: $result =~ s/&(nsub|\#8836);/\\ensuremath\{\\not\\subset\}/g;
670: $result =~ s/&(sube|\#8838);/\\ensuremath\{\\subseteq\}/g;
671: $result =~ s/&(supe|\#8839);/\\ensuremath\{\\supseteq\}/g;
672: $result =~ s/&(oplus|\#8853);/\\ensuremath\{\\oplus\}/g;
673: $result =~ s/&(otimes|\#8855);/\\ensuremath\{\\otimes\}/g;
674: $result =~ s/&(perp|\#8869);/\\ensuremath\{\\perp\}/g;
675: $result =~ s/&(sdot|\#8901);/\\ensuremath\{\\cdot\}/g;
676: #Geometric Shapes (extended HTML 4.01)
677: $result =~ s/&(loz|\#9674);/\\ensuremath\{\\Diamond\}/g;
678: #Miscellaneous Symbols (extended HTML 4.01)
679: $result =~ s/&(spades|\#9824);/\\ensuremath\{\\spadesuit\}/g;
680: $result =~ s/&(clubs|\#9827);/\\ensuremath\{\\clubsuit\}/g;
681: $result =~ s/&(hearts|\#9829);/\\ensuremath\{\\heartsuit\}/g;
682: $result =~ s/&(diams|\#9830);/\\ensuremath\{\\diamondsuit\}/g;
683: return $result;
684: }
685:
686:
687: #width, height, oddsidemargin, evensidemargin, topmargin
688: my %page_formats=
689: ('letter' => {
690: 'book' => {
691: '1' => [ '7.1 in','9.8 in', '-0.57 in','-0.57 in','0.7 cm'],
692: '2' => ['3.66 in','9.8 in', '-0.57 in','-0.57 in','0.7 cm']
693: },
694: 'album' => {
695: '1' => [ '8.8 in', '6.8 in','-0.55 in', '-0.55 in','1 cm'],
696: '2' => [ '4.8 in', '6.8 in','-0.5 in', '-1.0 in','3.5 in']
697: },
698: },
699: 'legal' => {
700: 'book' => {
701: '1' => ['7.1 in','13 in',,'-0.57 in','-0.57 in','-0.5 in'],
702: '2' => ['3.16 in','13 in','-0.57 in','-0.57 in','-0.5 in']
703: },
704: 'album' => {
705: '1' => ['12 in','7.1 in',,'-0.57 in','-0.57 in','-0.5 in'],
706: '2' => ['6.0 in','7.1 in','-1 in','-1 in','5 in']
707: },
708: },
709: 'tabloid' => {
710: 'book' => {
711: '1' => ['9.8 in','16 in','-0.57 in','-0.57 in','-0.5 in'],
712: '2' => ['4.9 in','16 in','-0.57 in','-0.57 in','-0.5 in']
713: },
714: 'album' => {
715: '1' => ['16 in','9.8 in','-0.57 in','-0.57 in','-0.5 in'],
716: '2' => ['16 in','4.9 in','-0.57 in','-0.57 in','-0.5 in']
717: },
718: },
719: 'executive' => {
720: 'book' => {
721: '1' => ['6.8 in','9 in','-0.57 in','-0.57 in','1.2 in'],
722: '2' => ['3.1 in','9 in','-0.57 in','-0.57 in','1.2 in']
723: },
724: 'album' => {
725: '1' => [],
726: '2' => []
727: },
728: },
729: 'a2' => {
730: 'book' => {
731: '1' => [],
732: '2' => []
733: },
734: 'album' => {
735: '1' => [],
736: '2' => []
737: },
738: },
739: 'a3' => {
740: 'book' => {
741: '1' => [],
742: '2' => []
743: },
744: 'album' => {
745: '1' => [],
746: '2' => []
747: },
748: },
749: 'a4' => {
750: 'book' => {
751: '1' => ['17.6 cm','27.2 cm','-0.55 in','-0.83 in','-0.5 in'],
752: '2' => [ '9.1 cm','27.2 cm','-0.55 in','-0.83 in','-0.5 in']
753: },
754: 'album' => {
755: '1' => ['8.5 in','7.7 in','-0.55 in','-0.83 in','0 in'],
756: '2' => ['3.9 in','7.7 in','-0.55 in','-0.83 in','0 in']
757: },
758: },
759: 'a5' => {
760: 'book' => {
761: '1' => [],
762: '2' => []
763: },
764: 'album' => {
765: '1' => [],
766: '2' => []
767: },
768: },
769: 'a6' => {
770: 'book' => {
771: '1' => [],
772: '2' => []
773: },
774: 'album' => {
775: '1' => [],
776: '2' => []
777: },
778: },
779: );
780:
781: sub page_format {
782: #
783: #Supported paper format: "Letter [8 1/2x11 in]", "Legal [8 1/2x14 in]",
784: # "Ledger/Tabloid [11x17 in]", "Executive [7 1/2x10 in]",
785: # "A2 [420x594 mm]", "A3 [297x420 mm]",
786: # "A4 [210x297 mm]", "A5 [148x210 mm]",
787: # "A6 [105x148 mm]"
788: #
789: my ($papersize,$layout,$numberofcolumns) = @_;
790: return @{$page_formats{$papersize}->{$layout}->{$numberofcolumns}};
791: }
792:
793:
794: sub get_name {
795: my ($uname,$udom)=@_;
796: if (!defined($uname)) { $uname=$env{'user.name'}; }
797: if (!defined($udom)) { $udom=$env{'user.domain'}; }
798: my $plainname=&Apache::loncommon::plainname($uname,$udom);
799: if ($plainname=~/^\s*$/) { $plainname=$uname.'@'.$udom; }
800: $plainname=&Apache::lonxml::latex_special_symbols($plainname,'header');
801: return $plainname;
802: }
803:
804: sub get_course {
805: my $courseidinfo;
806: if (defined($env{'request.course.id'})) {
807: $courseidinfo = &Apache::lonxml::latex_special_symbols(&unescape($env{'course.'.$env{'request.course.id'}.'.description'}),'header');
808: }
809: return $courseidinfo;
810: }
811:
812: sub page_format_transformation {
813: my ($papersize,$layout,$numberofcolumns,$choice,$text,$assignment,$tableofcontents,$indexlist,$selectionmade) = @_;
814: my ($textwidth,$textheight,$oddoffset,$evenoffset,$topmargin);
815:
816: if ($selectionmade eq '4') {
817: $assignment='Problems from the Whole Course';
818: } else {
819: $assignment=&Apache::lonxml::latex_special_symbols($assignment,'header');
820: }
821: ($textwidth,$textheight,$oddoffset,$evenoffset,$topmargin) = &page_format($papersize,$layout,$numberofcolumns,$topmargin);
822:
823:
824: my $name = &get_name();
825: my $courseidinfo = &get_course();
826: if (defined($courseidinfo)) { $courseidinfo=' - '.$courseidinfo }
827: my $header_text = $parmhash{'print_header_format'};
828: $header_text = &format_page_header($textwidth, $header_text, $assignment,
829: $courseidinfo, $name);
830: my $topmargintoinsert = '';
831: if ($topmargin ne '0') {$topmargintoinsert='\setlength{\topmargin}{'.$topmargin.'}';}
832: my $fancypagestatement='';
833: if ($numberofcolumns eq '2') {
834: $fancypagestatement="\\fancyhead{}\\fancyhead[LO]{$header_text}";
835: } else {
836: $fancypagestatement="\\rhead{}\\chead{}\\lhead{$header_text}";
837: }
838: if ($layout eq 'album') {
839: $text =~ s/\\begin{document}/\\setlength{\\oddsidemargin}{$oddoffset}\\setlength{\\evensidemargin}{$evenoffset}$topmargintoinsert\n\\setlength{\\textwidth}{$textwidth}\\setlength{\\textheight}{$textheight}\\setlength{\\textfloatsep}{8pt plus 2\.0pt minus 4\.0pt}\n\\newlength{\\minipagewidth}\\setlength{\\minipagewidth}{\\textwidth\/\$number_of_columns-0\.2cm}\\usepackage{fancyhdr}\\addtolength{\\headheight}{\\baselineskip}\n\\pagestyle{fancy}$fancypagestatement\\begin{document}\\voffset=-0\.8 cm\\setcounter{page}{1}\n /;
840: } elsif ($layout eq 'book') {
841: if ($choice ne 'All class print') {
842: $text =~ s/\\begin{document}/\\textheight $textheight\\oddsidemargin = $evenoffset\\evensidemargin = $evenoffset $topmargintoinsert\n\\textwidth= $textwidth\\newlength{\\minipagewidth}\\setlength{\\minipagewidth}{\\textwidth\/\$number_of_columns-0\.2cm}\n\\renewcommand{\\ref}{\\keephidden\}\\usepackage{fancyhdr}\\addtolength{\\headheight}{\\baselineskip}\\pagestyle{fancy}$fancypagestatement\\begin{document}\n\\voffset=-0\.8 cm\\setcounter{page}{1}\n/;
843: } else {
844: $text =~ s/\\pagestyle{fancy}\\rhead{}\\chead{}\s*\\begin{document}/\\textheight = $textheight\\oddsidemargin = $evenoffset\n\\evensidemargin = $evenoffset $topmargintoinsert\\textwidth= $textwidth\\newlength{\\minipagewidth}\n\\setlength{\\minipagewidth}{\\textwidth\/\$number_of_columns-0\.2cm}\\renewcommand{\\ref}{\\keephidden\}\\pagestyle{fancy}\\rhead{}\\chead{}\\begin{document}\\voffset=-0\.8cm\n\\setcounter{page}{1} \\vskip 5 mm\n /;
845: }
846: if ($papersize eq 'a4') {
847: $text =~ s/(\\begin{document})/$1\\special{papersize=210mm,297mm}/;
848: }
849: }
850: if ($tableofcontents eq 'yes') {$text=~s/(\\setcounter\{page\}\{1\})/$1 \\tableofcontents\\newpage /;}
851: if ($indexlist eq 'yes') {
852: $text=~s/(\\begin{document})/\\makeindex $1/;
853: $text=~s/(\\end{document})/\\strut\\\\\\strut\\printindex $1/;
854: }
855: return $text;
856: }
857:
858:
859: sub page_cleanup {
860: my $result = shift;
861:
862: $result =~ m/\\end{document}(\d*)$/;
863: my $number_of_columns = $1;
864: my $insert = '{';
865: for (my $id=1;$id<=$number_of_columns;$id++) { $insert .='l'; }
866: $insert .= '}';
867: $result =~ s/(\\begin{longtable})INSERTTHEHEADOFLONGTABLE\\endfirsthead\\endhead/$1$insert/g;
868: $result =~ s/&\s*REMOVETHEHEADOFLONGTABLE\\\\/\\\\/g;
869: return $result,$number_of_columns;
870: }
871:
872:
873: sub details_for_menu {
874: my ($helper)=@_;
875: my $postdata=$env{'form.postdata'};
876: if (!$postdata) { $postdata=$helper->{VARS}{'postdata'}; }
877: my $name_of_resource = &Apache::lonnet::gettitle($postdata);
878: my $symbolic = &Apache::lonnet::symbread($postdata);
879: return if ( $symbolic eq '');
880:
881: my ($map,$id,$resource)=&Apache::lonnet::decode_symb($symbolic);
882: $map=&Apache::lonnet::clutter($map);
883: my $name_of_sequence = &Apache::lonnet::gettitle($map);
884: if ($name_of_sequence =~ /^\s*$/) {
885: $map =~ m|([^/]+)$|;
886: $name_of_sequence = $1;
887: }
888: my $name_of_map = &Apache::lonnet::gettitle($env{'request.course.uri'});
889: if ($name_of_map =~ /^\s*$/) {
890: $env{'request.course.uri'} =~ m|([^/]+)$|;
891: $name_of_map = $1;
892: }
893: return ($name_of_resource,$name_of_sequence,$name_of_map);
894: }
895:
896: sub copyright_line {
897: return '\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}\vspace*{-2 mm}\newline\noindent{\tiny Printed from LON-CAPA\copyright MSU{\hfill} Licensed under GNU General Public License } ';
898: }
899: my $end_of_student = "\n".'\special{ps:ENDOFSTUDENTSTAMP}'."\n";
900:
901: sub latex_corrections {
902: my ($number_of_columns,$result,$selectionmade,$answer_mode) = @_;
903: # $result =~ s/\\includegraphics{/\\includegraphics\[width=\\minipagewidth\]{/g;
904: my $copyright = ©right_line();
905: if ($selectionmade eq '1' || $answer_mode eq 'only') {
906: $result =~ s/(\\end{document})/\\strut\\vskip 0 mm $copyright $end_of_student $1/;
907: } else {
908: $result =~ s/(\\end{document})/\\strut\\vspace\*{-4 mm}\\newline $copyright $end_of_student $1/;
909: }
910: $result =~ s/\$number_of_columns/$number_of_columns/g;
911: $result =~ s/(\\end{longtable}\s*)(\\strut\\newline\\noindent\\makebox\[\\textwidth\/$number_of_columns\]\[b\]{\\hrulefill})/$2$1/g;
912: $result =~ s/(\\end{longtable}\s*)\\strut\\newline/$1/g;
913: #-- LaTeX corrections
914: my $first_comment = index($result,'<!--',0);
915: while ($first_comment != -1) {
916: my $end_comment = index($result,'-->',$first_comment);
917: substr($result,$first_comment,$end_comment-$first_comment+3) = '';
918: $first_comment = index($result,'<!--',$first_comment);
919: }
920: $result =~ s/^\s+$//gm; #remove empty lines
921: #removes more than one empty space
922: $result =~ s|(\s\s+)|($1=~/[\n\r]/)?"\n":" "|ge;
923: $result =~ s/\\\\\s*\\vskip/\\vskip/gm;
924: $result =~ s/\\\\\s*\\noindent\s*(\\\\)+/\\\\\\noindent /g;
925: $result =~ s/{\\par }\s*\\\\/\\\\/gm;
926: $result =~ s/\\\\\s+\[/ \[/g;
927: #conversion of html characters to LaTeX equivalents
928: if ($result =~ m/&(\w+|#\d+);/) {
929: $result = &character_chart($result);
930: }
931: $result =~ s/(\\end{tabular})\s*\\vskip 0 mm/$1/g;
932: $result =~ s/(\\begin{enumerate})\s*\\noindent/$1/g;
933: return $result;
934: }
935:
936:
937: sub index_table {
938: my $currentURL = shift;
939: my $insex_string='';
940: $currentURL=~s/\.([^\/+])$/\.$1\.meta/;
941: $insex_string=&Apache::lonnet::metadata($currentURL,'keywords');
942: return $insex_string;
943: }
944:
945:
946: sub IndexCreation {
947: my ($texversion,$currentURL)=@_;
948: my @key_words=split(/,/,&index_table($currentURL));
949: my $chunk='';
950: my $st=index $texversion,'\addcontentsline{toc}{subsection}{';
951: if ($st>0) {
952: for (my $i=0;$i<3;$i++) {$st=(index $texversion,'}',$st+1);}
953: $chunk=substr($texversion,0,$st+1);
954: substr($texversion,0,$st+1)=' ';
955: }
956: foreach my $key_word (@key_words) {
957: if ($key_word=~/\S+/) {
958: $texversion=~s/\b($key_word)\b/$1 \\index{$key_word} /i;
959: }
960: }
961: if ($st>0) {substr($texversion,0,1)=$chunk;}
962: return $texversion;
963: }
964:
965: sub print_latex_header {
966: my $mode=shift;
967: my $output='\documentclass[letterpaper,twoside]{article}\raggedbottom';
968: if (($mode eq 'batchmode') || (!$perm{'pav'})) {
969: $output.='\batchmode';
970: }
971: $output.='\newcommand{\keephidden}[1]{}\renewcommand{\deg}{$^{\circ}$}'."\n".
972: '\usepackage{multirow}'."\n".
973: '\usepackage{longtable}\usepackage{textcomp}\usepackage{makeidx}'."\n".
974: '\usepackage[dvips]{graphicx}\usepackage{epsfig}'."\n".
975: '\usepackage{wrapfig}'.
976: '\usepackage{picins}\usepackage{calc}'."\n".
977: '\newenvironment{choicelist}{\begin{list}{}{\setlength{\rightmargin}{0in}'."\n".
978: '\setlength{\leftmargin}{0.13in}\setlength{\topsep}{0.05in}'."\n".
979: '\setlength{\itemsep}{0.022in}\setlength{\parsep}{0in}'."\n".
980: '\setlength{\belowdisplayskip}{0.04in}\setlength{\abovedisplayskip}{0.05in}'."\n".
981: '\setlength{\abovedisplayshortskip}{-0.04in}'."\n".
982: '\setlength{\belowdisplayshortskip}{0.04in}}}{\end{list}}'."\n".
983: '\renewenvironment{theindex}{\begin{list}{}{{\vskip 1mm \noindent \large'."\n".
984: '\textbf{Index}} \newline \setlength{\rightmargin}{0in}'."\n".
985: '\setlength{\leftmargin}{0.13in}\setlength{\topsep}{0.01in}'."\n".
986: '\setlength{\itemsep}{0.1in}\setlength{\parsep}{-0.02in}'."\n".
987: '\setlength{\belowdisplayskip}{0.01in}\setlength{\abovedisplayskip}{0.01in}'."\n".
988: '\setlength{\abovedisplayshortskip}{-0.04in}'."\n".
989: '\setlength{\belowdisplayshortskip}{0.01in}}}{\end{list}}\begin{document}'."\n";
990: return $output;
991: }
992:
993: sub path_to_problem {
994: my ($urlp,$colwidth)=@_;
995: $urlp=&Apache::lonnet::clutter($urlp);
996:
997: my $newurlp = '';
998: $colwidth=~s/\s*mm\s*$//;
999: #characters average about 2 mm in width
1000: if (length($urlp)*2 > $colwidth) {
1001: my @elements = split('/',$urlp);
1002: my $curlength=0;
1003: foreach my $element (@elements) {
1004: if ($element eq '') { next; }
1005: if ($curlength+(length($element)*2) > $colwidth) {
1006: $newurlp .= '|\vskip -1 mm \verb|';
1007: $curlength=length($element)*2;
1008: } else {
1009: $curlength+=length($element)*2;
1010: }
1011: $newurlp.='/'.$element;
1012: }
1013: } else {
1014: $newurlp=$urlp;
1015: }
1016: return '{\small\noindent\verb|'.$newurlp.'|\vskip 0 mm}';
1017: }
1018:
1019: sub recalcto_mm {
1020: my $textwidth=shift;
1021: my $LaTeXwidth;
1022: if ($textwidth=~/(-?\d+\.?\d*)\s*cm/) {
1023: $LaTeXwidth = $1*10;
1024: } elsif ($textwidth=~/(-?\d+\.?\d*)\s*mm/) {
1025: $LaTeXwidth = $1;
1026: } elsif ($textwidth=~/(-?\d+\.?\d*)\s*in/) {
1027: $LaTeXwidth = $1*25.4;
1028: }
1029: $LaTeXwidth.=' mm';
1030: return $LaTeXwidth;
1031: }
1032:
1033: sub get_textwidth {
1034: my ($helper,$LaTeXwidth)=@_;
1035: my $textwidth=$LaTeXwidth;
1036: if ($helper->{'VARS'}->{'pagesize.width'}=~/\d+/ &&
1037: $helper->{'VARS'}->{'pagesize.widthunit'}=~/\w+/) {
1038: $textwidth=&recalcto_mm($helper->{'VARS'}->{'pagesize.width'}.' '.
1039: $helper->{'VARS'}->{'pagesize.widthunit'});
1040: }
1041: return $textwidth;
1042: }
1043:
1044:
1045: sub unsupported {
1046: my ($currentURL,$mode,$symb)=@_;
1047: if ($mode ne '') {$mode='\\'.$mode}
1048: my $result.= &print_latex_header($mode);
1049: if ($currentURL=~m|^(/adm/wrapper/)?ext/|) {
1050: $currentURL=~s|^(/adm/wrapper/)?ext/|http://|;
1051: my $title=&Apache::lonnet::gettitle($symb);
1052: $title = &Apache::lonxml::latex_special_symbols($title);
1053: $result.=' \strut \\\\ '.$title.' \strut \\\\ '.$currentURL.' ';
1054: } else {
1055: $result.=$currentURL;
1056: }
1057: $result.= '\vskip 0.5mm\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill} \end{document}';
1058: return $result;
1059: }
1060:
1061:
1062: #
1063: # List of recently generated print files
1064: #
1065: sub recently_generated {
1066: my $r=shift;
1067: my $prtspool=$r->dir_config('lonPrtDir');
1068: my $zip_result;
1069: my $pdf_result;
1070: opendir(DIR,$prtspool);
1071:
1072: my @files =
1073: grep(/^$env{'user.name'}_$env{'user.domain'}_printout_(\d+)_.*\.(pdf|zip)$/,readdir(DIR));
1074: closedir(DIR);
1075:
1076: @files = sort {
1077: my ($actime) = (stat($prtspool.'/'.$a))[10];
1078: my ($bctime) = (stat($prtspool.'/'.$b))[10];
1079: return $bctime <=> $actime;
1080: } (@files);
1081:
1082: foreach my $filename (@files) {
1083: my ($ext) = ($filename =~ m/(pdf|zip)$/);
1084: my ($cdev,$cino,$cmode,$cnlink,
1085: $cuid,$cgid,$crdev,$csize,
1086: $catime,$cmtime,$cctime,
1087: $cblksize,$cblocks)=stat($prtspool.'/'.$filename);
1088: my $result="<a href='/prtspool/$filename'>".
1089: &mt('Generated [_1] ([_2] bytes)',
1090: &Apache::lonlocal::locallocaltime($cctime),$csize).
1091: '</a><br />';
1092: if ($ext eq 'pdf') { $pdf_result .= $result; }
1093: if ($ext eq 'zip') { $zip_result .= $result; }
1094: }
1095: if ($zip_result) {
1096: $r->print('<h4>'.&mt('Recently generated printout zip files')."</h4>\n"
1097: .$zip_result);
1098: }
1099: if ($pdf_result) {
1100: $r->print('<h4>'.&mt('Recently generated printouts')."</h4>\n"
1101: .$pdf_result);
1102: }
1103: }
1104:
1105: #
1106: # Retrieve the hash of page breaks.
1107: #
1108: # Inputs:
1109: # helper - reference to helper object.
1110: # Outputs
1111: # A reference to a page break hash.
1112: #
1113: #
1114: #use Data::Dumper;
1115: #sub dump_helper_vars {
1116: # my ($helper) = @_;
1117: # my $helpervars = Dumper($helper->{'VARS'});
1118: # &Apache::lonnet::logthis("Dump of helper vars:\n $helpervars");
1119: #}
1120:
1121: sub get_page_breaks {
1122: my ($helper) = @_;
1123: my %page_breaks;
1124:
1125: foreach my $break (split /\|\|\|/, $helper->{'VARS'}->{'FINISHPAGE'}) {
1126: $page_breaks{$break} = 1;
1127: }
1128: return %page_breaks;
1129: }
1130:
1131: # Output a sequence (recursively if neeed)
1132: # from construction space.
1133: # Parameters:
1134: # url = URL of the sequence to print.
1135: # helper - Reference to the helper hash.
1136: # form - Copy of the format hash.
1137: # LaTeXWidth
1138: # Returns:
1139: # Text to add to the printout.
1140: # NOTE if the first element of the outermost sequence
1141: # is itself a sequence, the outermost caller may need to
1142: # prefix the latex with the page headers stuff.
1143: #
1144: sub print_construction_sequence {
1145: my ($currentURL, $helper, %form, $LaTeXwidth) = @_;
1146: my $result;
1147: my $rndseed=time;
1148: if ($helper->{'VARS'}->{'curseed'}) {
1149: $rndseed=$helper->{'VARS'}->{'curseed'};
1150: }
1151: my $errtext=&Apache::lonratedt::mapread($currentURL);
1152: #
1153: # These make this all support recursing for subsequences.
1154: #
1155: my @order = @Apache::lonratedt::order;
1156: my @resources = @Apache::lonratedt::resources;
1157: for (my $member=0;$member<=$#order;$member++) {
1158: $resources[$order[$member]]=~/^([^:]*):([^:]*):/;
1159: my $urlp=$2;
1160: if ($urlp=~/\.(problem|exam|quiz|assess|survey|form|library|xml|html|htm|xhtml|xhtm)$/) {
1161: my $texversion='';
1162: if ($helper->{'VARS'}->{'ANSWER_TYPE'} ne 'only') {
1163: $form{'problem_split'}=$parmhash{'problem_stream_switch'};
1164: $form{'suppress_tries'}=$parmhash{'suppress_tries'};
1165: $form{'latex_type'}=$helper->{'VARS'}->{'LATEX_TYPE'};
1166: $form{'rndseed'}=$rndseed;
1167: $resources_printed .=$urlp.':';
1168: $texversion=&Apache::lonnet::ssi($urlp,%form);
1169: }
1170: if((($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') ||
1171: ($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'only')) &&
1172: ($urlp=~/\.(problem|exam|quiz|assess|survey|form|library|page)$/)) {
1173: # Don't permanently modify %$form...
1174: my %answerform = %form;
1175: $answerform{'grade_target'}='answer';
1176: $answerform{'answer_output_mode'}='tex';
1177: $answerform{'rndseed'}=$rndseed;
1178: $answerform{'problem_split'}=$parmhash{'problem_stream_switch'};
1179: if ($urlp=~/\/res\//) {$env{'request.state'}='published';}
1180: $resources_printed .= $urlp.':';
1181: my $answer=&Apache::lonnet::ssi($urlp,%answerform);
1182: if ($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') {
1183: $texversion=~s/(\\keephidden{ENDOFPROBLEM})/$answer$1/;
1184: } else {
1185: # If necessary, encapsulate answer in minipage:
1186:
1187: $texversion=&print_latex_header($helper->{'VARS'}->{'LATEX_TYPE'});
1188: my $title = &Apache::lonnet::gettitle($helper->{'VARS'}->{'symb'});
1189: $title = &Apache::lonxml::latex_special_symbols($title);
1190: my $body ='\vskip 0 mm \noindent\textbf{'.$title.'}\vskip 0 mm ';
1191: $body.=&path_to_problem($urlp,$LaTeXwidth);
1192: $body.='\vskip 1 mm '.$answer.'\end{document}';
1193: $body = &encapsulate_minipage($body);
1194: $texversion.=$body;
1195: }
1196: }
1197: $texversion = &latex_header_footer_remove($texversion);
1198:
1199: if ($helper->{'VARS'}->{'TABLE_INDEX'} eq 'yes') {
1200: $texversion=&IndexCreation($texversion,$urlp);
1201: }
1202: if ($helper->{'VARS'}->{'CONSTR_RESOURSE_URL'} eq 'yes') {
1203: $texversion=~s/(\\addcontentsline\{toc\}\{subsection\}\{[^\}]*\})/$1 URL: \\verb|$urlp| \\strut\\\\\\strut /;
1204: }
1205: $result.=$texversion;
1206:
1207: } elsif ($urlp=~/\.(sequence|page)$/) {
1208:
1209: # header:
1210:
1211: $result.='\strut\newline\noindent Sequence/page '.$urlp.'\strut\newline\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}\newline\noindent ';
1212:
1213: # IF sequence, recurse:
1214:
1215: if ($urlp =~ /\.sequence$/) {
1216: my $sequence_url = $urlp;
1217: my $domain = $env{'user.domain'}; # Constr. space only on local
1218: my $user = $env{'user.name'};
1219:
1220: $sequence_url =~ s/^\/res\/$domain/\/home/;
1221: $sequence_url =~ s/^(\/home\/$user)/$1\/public_html/;
1222: # $sequence_url =~ s|\/~([^\/]+)\/|\/home\/$1\/public_html\/|;
1223: $result .= &print_construction_sequence($sequence_url,
1224: $helper, %form,
1225: $LaTeXwidth);
1226: }
1227: }
1228: }
1229: if ($helper->{VARS}->{'construction'} eq '1') {$result=~s/(\\begin{document})/$1 \\fbox\{RANDOM SEED IS $rndseed\} /;}
1230: return $result;
1231: }
1232:
1233: sub output_data {
1234: my ($r,$helper,$rparmhash) = @_;
1235: my %parmhash = %$rparmhash;
1236: $resources_printed = '';
1237: my $js = <<ENDPART;
1238: <script type="text/javascript">
1239: var editbrowser;
1240: function openbrowser(formname,elementname,only,omit) {
1241: var url = '/res/?';
1242: if (editbrowser == null) {
1243: url += 'launch=1&';
1244: }
1245: url += 'catalogmode=interactive&';
1246: url += 'mode=parmset&';
1247: url += 'form=' + formname + '&';
1248: if (only != null) {
1249: url += 'only=' + only + '&';
1250: }
1251: if (omit != null) {
1252: url += 'omit=' + omit + '&';
1253: }
1254: url += 'element=' + elementname + '';
1255: var title = 'Browser';
1256: var options = 'scrollbars=1,resizable=1,menubar=0';
1257: options += ',width=700,height=600';
1258: editbrowser = open(url,title,options,'1');
1259: editbrowser.focus();
1260: }
1261: </script>
1262: ENDPART
1263:
1264: my $start_page = &Apache::loncommon::start_page('Preparing Printout',$js);
1265: my $msg = &mt('Please stand by while processing your print request, this may take some time ...');
1266:
1267: $r->print($start_page."\n<p>\n$msg\n</p>\n");
1268:
1269: # fetch the pagebreaks and store them in the course environment
1270: # The page breaks will be pulled into the hash %page_breaks which is
1271: # indexed by symb and contains 1's for each break.
1272:
1273: $env{'form.pagebreaks'} = $helper->{'VARS'}->{'FINISHPAGE'};
1274: $env{'form.lastprinttype'} = $helper->{'VARS'}->{'PRINT_TYPE'};
1275: &Apache::loncommon::store_course_settings('print',
1276: {'pagebreaks' => 'scalar',
1277: 'lastprinttype' => 'scalar'});
1278:
1279: my %page_breaks = &get_page_breaks($helper);
1280:
1281: my $format_from_helper = $helper->{'VARS'}->{'FORMAT'};
1282: my ($result,$selectionmade) = ('','');
1283: my $number_of_columns = 1; #used only for pages to determine the width of the cell
1284: my @temporary_array=split /\|/,$format_from_helper;
1285: my ($laystyle,$numberofcolumns,$papersize)=@temporary_array;
1286: if ($laystyle eq 'L') {
1287: $laystyle='album';
1288: } else {
1289: $laystyle='book';
1290: }
1291: my ($textwidth,$textheight,$oddoffset,$evenoffset) = &page_format($papersize,$laystyle,$numberofcolumns);
1292: my $assignment = $env{'form.assignment'};
1293: my $LaTeXwidth=&recalcto_mm($textwidth);
1294: my @print_array=();
1295: my @student_names=();
1296:
1297: # Common settings for the %form has:
1298: # In some cases these settings get overriddent by specific cases, but the
1299: # settings are common enough to make it worthwhile factoring them out
1300: # here.
1301: #
1302: my %form;
1303: $form{'grade_target'} = 'tex';
1304: $form{'textwidth'} = &get_textwidth($helper, $LaTeXwidth);
1305:
1306: # If form.showallfoils is set, then request all foils be shown:
1307: # privilege will be enforced both by not allowing the
1308: # check box selecting this option to be presnt unless it's ok,
1309: # and by lonresponse's priv. check.
1310: # The if is here because lonresponse.pm only cares that
1311: # showallfoils is defined, not what the value is.
1312:
1313: if ($helper->{'VARS'}->{'showallfoils'} eq "1") {
1314: $form{'showallfoils'} = $helper->{'VARS'}->{'showallfoils'};
1315: }
1316:
1317: if ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'current_document') {
1318: #-- single document - problem, page, html, xml, ...
1319: my ($currentURL,$cleanURL);
1320:
1321: if ($helper->{'VARS'}->{'construction'} ne '1') {
1322: #prints published resource
1323: $currentURL=$helper->{'VARS'}->{'postdata'};
1324: $cleanURL=&Apache::lonenc::check_decrypt($currentURL);
1325: } else {
1326: #prints resource from the construction space
1327: $currentURL='/'.$helper->{'VARS'}->{'filename'};
1328: if ($currentURL=~/([^?]+)/) {$currentURL=$1;}
1329: $cleanURL=$currentURL;
1330: }
1331: $selectionmade = 1;
1332: if ($cleanURL!~m|^/adm/|
1333: && $cleanURL=~/\.(problem|exam|quiz|assess|survey|form|library|page|xml|html|htm|xhtml|xhtm)$/) {
1334: my $rndseed=time;
1335: my $texversion='';
1336: if ($helper->{'VARS'}->{'ANSWER_TYPE'} ne 'only') {
1337: my %moreenv;
1338: $moreenv{'request.filename'}=$cleanURL;
1339: if ($helper->{'VARS'}->{'style_file'}=~/\w/) {
1340: $moreenv{'construct.style'}=$helper->{'VARS'}->{'style_file'};
1341: my $dom = $env{'user.domain'};
1342: my $user = $env{'user.name'};
1343: my $put_result = &Apache::lonnet::put('environment',{'construct.style'=>$helper->{'VARS'}->{'style_file'}},$dom,$user);
1344: }
1345: if ($helper->{'VARS'}->{'probstatus'} eq 'exam') {$form{'problemtype'}='exam';}
1346: $form{'problem_split'}=$parmhash{'problem_stream_switch'};
1347: $form{'suppress_tries'}=$parmhash{'suppress_tries'};
1348: $form{'latex_type'}=$helper->{'VARS'}->{'LATEX_TYPE'};
1349: $form{'print_discussions'}=$helper->{'VARS'}->{'PRINT_DISCUSSIONS'};
1350: if ($helper->{'VARS'}->{'PRINT_DISCUSSIONS'} eq 'yes') {$form{'problem_split'}='yes';}
1351: if ($helper->{'VARS'}->{'curseed'}) {
1352: $rndseed=$helper->{'VARS'}->{'curseed'};
1353: }
1354: $form{'rndseed'}=$rndseed;
1355: &Apache::lonnet::appenv(%moreenv);
1356:
1357: &Apache::lonxml::clear_problem_counter();
1358:
1359: $resources_printed .= $currentURL.':';
1360: $texversion.=&Apache::lonnet::ssi($currentURL,%form);
1361:
1362: &Apache::lonxml::clear_problem_counter();
1363:
1364: &Apache::lonnet::delenv('request.filename');
1365: }
1366: # current document with answers.. no need to encap in minipage
1367: # since there's only one answer.
1368:
1369: if(($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') ||
1370: ($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'only')) {
1371: $form{'problem_split'}=$parmhash{'problem_stream_switch'};
1372: $form{'grade_target'}='answer';
1373: $form{'answer_output_mode'}='tex';
1374: $form{'rndseed'}=$rndseed;
1375: if ($helper->{'VARS'}->{'probstatus'} eq 'exam') {
1376: $form{'problemtype'}='exam';
1377: }
1378: $resources_printed .= $currentURL.':';
1379: my $answer=&Apache::lonnet::ssi($currentURL,%form);
1380: if ($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') {
1381: $texversion=~s/(\\keephidden{ENDOFPROBLEM})/$answer$1/;
1382: } else {
1383: $texversion=&print_latex_header($helper->{'VARS'}->{'LATEX_TYPE'});
1384: if ($helper->{'VARS'}->{'construction'} ne '1') {
1385: my $title = &Apache::lonnet::gettitle($helper->{'VARS'}->{'symb'});
1386: $title = &Apache::lonxml::latex_special_symbols($title);
1387: $texversion.='\vskip 0 mm \noindent\textbf{'.$title.'}\vskip 0 mm ';
1388: $texversion.=&path_to_problem($cleanURL,$LaTeXwidth);
1389: } else {
1390: $texversion.='\vskip 0 mm \noindent\textbf{Prints from construction space - there is no title.}\vskip 0 mm ';
1391: my $URLpath=$cleanURL;
1392: $URLpath=~s/~([^\/]+)/public_html\/$1\/$1/;
1393: $texversion.=&path_to_problem ($URLpath,$LaTeXwidth);
1394: }
1395: $texversion.='\vskip 1 mm '.$answer.'\end{document}';
1396: }
1397: }
1398: if ($helper->{'VARS'}->{'TABLE_INDEX'} eq 'yes') {
1399: $texversion=&IndexCreation($texversion,$currentURL);
1400: }
1401: if ($helper->{'VARS'}->{'CONSTR_RESOURSE_URL'} eq 'yes') {
1402: $texversion=~s/(\\addcontentsline\{toc\}\{subsection\}\{[^\}]*\})/$1 URL: \\verb|$currentURL| \\strut\\\\\\strut /;
1403:
1404: }
1405: $result .= $texversion;
1406: if ($currentURL=~m/\.page\s*$/) {
1407: ($result,$number_of_columns) = &page_cleanup($result);
1408: }
1409: } elsif ($cleanURL!~m|^/adm/|
1410: && $currentURL=~/\.sequence$/ && $helper->{'VARS'}->{'construction'} eq '1') {
1411: #printing content of sequence from the construction space
1412: $currentURL=~s|\/~([^\/]+)\/|\/home\/$1\/public_html\/|;
1413: $result .= &print_latex_header($helper->{'VARS'}->{'LATEX_TYPE'});
1414: $result .= &print_construction_sequence($currentURL, $helper, %form,
1415: $LaTeXwidth);
1416: $result .= '\end{document}';
1417: if (!($result =~ /\\begin\{document\}/)) {
1418: $result = &print_latex_header() . $result;
1419: }
1420: # End construction space sequence.
1421: } elsif ($cleanURL=~/\/(smppg|syllabus|aboutme|bulletinboard)$/) {
1422: $form{'latex_type'}=$helper->{'VARS'}->{'LATEX_TYPE'};
1423: if ($currentURL=~/\/syllabus$/) {$currentURL=~s/\/res//;}
1424: $resources_printed .= $currentURL.':';
1425: my $texversion=&Apache::lonnet::ssi($currentURL,%form);
1426: $result .= $texversion;
1427: } else {
1428: $result.=&unsupported($currentURL,$helper->{'VARS'}->{'LATEX_TYPE'},
1429: $helper->{'VARS'}->{'symb'});
1430: }
1431: } elsif (($helper->{'VARS'}->{'PRINT_TYPE'} eq 'map_problems') or
1432: ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'map_problems_pages') or
1433: ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'all_problems') or
1434: ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'all_resources') or # BUGBUG
1435: ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'select_sequences')) {
1436: #-- produce an output string
1437: if ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'map_problems') {
1438: $selectionmade = 2;
1439: } elsif ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'map_problems_pages') {
1440: $selectionmade = 3;
1441: } elsif ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'all_problems') {
1442: $selectionmade = 4;
1443: } elsif ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'all_resources') { #BUGBUG
1444: $selectionmade = 4;
1445: } elsif ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'select_sequences') {
1446: $selectionmade = 7;
1447: }
1448: $form{'problem_split'}=$parmhash{'problem_stream_switch'};
1449: $form{'suppress_tries'}=$parmhash{'suppress_tries'};
1450: $form{'latex_type'}=$helper->{'VARS'}->{'LATEX_TYPE'};
1451: $form{'print_discussions'}=$helper->{'VARS'}->{'PRINT_DISCUSSIONS'};
1452: if ($helper->{'VARS'}->{'PRINT_DISCUSSIONS'} eq 'yes') {$form{'problem_split'}='yes';}
1453: my $flag_latex_header_remove = 'NO';
1454: my $flag_page_in_sequence = 'NO';
1455: my @master_seq=split /\|\|\|/, $helper->{'VARS'}->{'RESOURCES'};
1456: my $prevassignment='';
1457:
1458: &Apache::lonxml::clear_problem_counter();
1459:
1460: my $pbreakresources = keys %page_breaks;
1461: for (my $i=0;$i<=$#master_seq;$i++) {
1462:
1463: # Note due to document structure, not allowed to put \newpage
1464: # prior to the first resource
1465:
1466: if (defined $page_breaks{$master_seq[$i]}) {
1467: if($i != 0) {
1468: $result.="\\newpage\n";
1469: }
1470: }
1471: my ($sequence,undef,$urlp)=&Apache::lonnet::decode_symb($master_seq[$i]);
1472: $urlp=&Apache::lonnet::clutter($urlp);
1473: $form{'symb'}=$master_seq[$i];
1474:
1475: my $assignment=&Apache::lonxml::latex_special_symbols(&Apache::lonnet::gettitle($sequence),'header'); #title of the assignment which contains this problem
1476: if ($selectionmade==7) {$helper->{VARS}->{'assignment'}=$assignment;}
1477: if ($i==0) {$prevassignment=$assignment;}
1478: my $texversion='';
1479: if ($urlp!~m|^/adm/|
1480: && $urlp=~/\.(problem|exam|quiz|assess|survey|form|library|page|xml|html|htm|xhtml|xhtm)$/) {
1481: $resources_printed .= $urlp.':';
1482:
1483: &Apache::lonxml::remember_problem_counter();
1484: $texversion.=&Apache::lonnet::ssi($urlp,%form);
1485: if ($urlp=~/\.page$/) {
1486: ($texversion,my $number_of_columns_page) = &page_cleanup($texversion);
1487: if ($number_of_columns_page > $number_of_columns) {$number_of_columns=$number_of_columns_page;}
1488: $texversion =~ s/\\end{document}\d*/\\end{document}/;
1489: $flag_page_in_sequence = 'YES';
1490: }
1491:
1492: if(($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') ||
1493: ($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'only')) {
1494: # Don't permanently pervert the %form hash
1495: my %answerform = %form;
1496: $answerform{'grade_target'}='answer';
1497: $answerform{'answer_output_mode'}='tex';
1498: $resources_printed .= $urlp.':';
1499:
1500: &Apache::lonxml::restore_problem_counter();
1501: my $answer=&Apache::lonnet::ssi($urlp,%answerform);
1502:
1503: if ($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') {
1504: $texversion=~s/(\\keephidden{ENDOFPROBLEM})/$answer$1/;
1505: } else {
1506: if ($urlp=~/\.(problem|exam|quiz|assess|survey|form|library)$/) {
1507: $texversion=&print_latex_header($helper->{'VARS'}->{'LATEX_TYPE'});
1508: my $title = &Apache::lonnet::gettitle($master_seq[$i]);
1509: $title = &Apache::lonxml::latex_special_symbols($title);
1510: my $body ='\vskip 0 mm \noindent\textbf{'.$title.'}\vskip 0 mm ';
1511: $body .= &path_to_problem ($urlp,$LaTeXwidth);
1512: $body .='\vskip 1 mm '.$answer;
1513: $body = &encapsulate_minipage($body);
1514: $texversion .= $body;
1515: } else {
1516: $texversion='';
1517: }
1518: }
1519: }
1520: if ($flag_latex_header_remove ne 'NO') {
1521: $texversion = &latex_header_footer_remove($texversion);
1522: } else {
1523: $texversion =~ s/\\end{document}//;
1524: }
1525: if ($helper->{'VARS'}->{'TABLE_INDEX'} eq 'yes') {
1526: $texversion=&IndexCreation($texversion,$urlp);
1527: }
1528: if (($selectionmade == 4) and ($assignment ne $prevassignment)) {
1529: my $name = &get_name();
1530: my $courseidinfo = &get_course();
1531: if (defined($courseidinfo)) { $courseidinfo=' - '.$courseidinfo }
1532: $prevassignment=$assignment;
1533: my $header_text = $parmhash{'print_header_format'};
1534: $header_text = &format_page_header($textwidth, $header_text,
1535: $assignment,
1536: $courseidinfo,
1537: $name);
1538: if ($numberofcolumns eq '1') {
1539: $result .='\newpage \noindent\parbox{\minipagewidth}{\noindent\\lhead{'.$header_text.'}} \vskip 5 mm ';
1540: } else {
1541: $result .='\newpage \noindent\parbox{\minipagewidth}{\noindent\\fancyhead[LO]{'.$header_text.'}} \vskip 5 mm ';
1542: }
1543: }
1544: $result .= $texversion;
1545: $flag_latex_header_remove = 'YES';
1546: } elsif ($urlp=~/\/(smppg|syllabus|aboutme|bulletinboard)$/) {
1547: $form{'latex_type'}=$helper->{'VARS'}->{'LATEX_TYPE'};
1548: if ($urlp=~/\/syllabus$/) {$urlp=~s/\/res//;}
1549: $resources_printed .= $urlp.':';
1550: my $texversion=&Apache::lonnet::ssi($urlp,%form);
1551: if ($flag_latex_header_remove ne 'NO') {
1552: $texversion = &latex_header_footer_remove($texversion);
1553: } else {
1554: $texversion =~ s/\\end{document}/\\vskip 0\.5mm\\noindent\\makebox\[\\textwidth\/\$number_of_columns\]\[b\]\{\\hrulefill\}/;
1555: }
1556: $result .= $texversion;
1557: $flag_latex_header_remove = 'YES';
1558: } else {
1559: $texversion=&unsupported($urlp,$helper->{'VARS'}->{'LATEX_TYPE'},
1560: $master_seq[$i]);
1561: if ($flag_latex_header_remove ne 'NO') {
1562: $texversion = &latex_header_footer_remove($texversion);
1563: } else {
1564: $texversion =~ s/\\end{document}//;
1565: }
1566: $result .= $texversion;
1567: $flag_latex_header_remove = 'YES';
1568: }
1569: if (&Apache::loncommon::connection_aborted($r)) { last; }
1570: }
1571: &Apache::lonxml::clear_problem_counter();
1572: if ($flag_page_in_sequence eq 'YES') {
1573: $result =~ s/\\usepackage{calc}/\\usepackage{calc}\\usepackage{longtable}/;
1574: }
1575: $result .= '\end{document}';
1576: } elsif (($helper->{'VARS'}->{'PRINT_TYPE'} eq 'problems_for_students') ||
1577: ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'resources_for_students')){
1578:
1579:
1580: #-- prints assignments for whole class or for selected students
1581: my $type;
1582: if ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'problems_for_students') {
1583: $selectionmade=5;
1584: $type='problems';
1585: } elsif ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'resources_for_students') {
1586: $selectionmade=8;
1587: $type='resources';
1588: }
1589: my @students=split /\|\|\|/, $helper->{'VARS'}->{'STUDENTS'};
1590: # The normal sort order is by section then by students within the
1591: # section. If the helper var student_sort is 1, then the user has elected
1592: # to override this and output the students by name.
1593: # Each element of the students array is of the form:
1594: # username:domain:section:last, first:status
1595: #
1596: # Note that student sort is not compatible with printing
1597: # 1 section per pdf...so that setting overrides.
1598: #
1599: if (($helper->{'VARS'}->{'student_sort'} eq 1) &&
1600: ($helper->{'VARS'}->{'SPLIT_PDFS'} ne "sections")) {
1601: @students = sort compare_names @students;
1602: }
1603: &adjust_number_to_print($helper);
1604:
1605: if ($helper->{'VARS'}->{'NUMBER_TO_PRINT'} eq '0' ||
1606: $helper->{'VARS'}->{'NUMBER_TO_PRINT'} eq 'all' ) {
1607: $helper->{'VARS'}->{'NUMBER_TO_PRINT'}=$#students+1;
1608: }
1609: # If we are splitting on section boundaries, we need
1610: # to remember that in split_on_sections and
1611: # print all of the students in the list.
1612: #
1613: my $split_on_sections = 0;
1614: if ($helper->{'VARS'}->{'NUMBER_TO_PRINT'} eq 'section') {
1615: $split_on_sections = 1;
1616: $helper->{'VARS'}->{'NUMBER_TO_PRINT'} = $#students+1;
1617: }
1618: my @master_seq=split /\|\|\|/, $helper->{'VARS'}->{'RESOURCES'};
1619:
1620: #loop over students
1621: my $flag_latex_header_remove = 'NO';
1622: my %moreenv;
1623: $moreenv{'instructor_comments'}='hide';
1624: $moreenv{'textwidth'}=&get_textwidth($helper,$LaTeXwidth);
1625: $moreenv{'print_discussions'}=$helper->{'VARS'}->{'PRINT_DISCUSSIONS'};
1626: $moreenv{'problem_split'} = $parmhash{'problem_stream_switch'};
1627: $moreenv{'suppress_tries'} = $parmhash{'suppress_tries'};
1628: if ($helper->{'VARS'}->{'PRINT_DISCUSSIONS'} eq 'yes') {$moreenv{'problem_split'}='yes';}
1629: my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin($r,'Print Status','Class Print Status',$#students+1,'inline','75');
1630: my $student_counter=-1;
1631: my $i = 0;
1632: my $last_section = (split(/:/,$students[0]))[2];
1633: foreach my $person (@students) {
1634:
1635: my $duefile="/home/httpd/prtspool/$env{'user.name'}_$env{'user.domain'}_printout.due";
1636: if (-e $duefile) {
1637: my $temp_file = Apache::File->new('>>'.$duefile);
1638: print $temp_file "1969\n";
1639: }
1640: $student_counter++;
1641: if ($split_on_sections) {
1642: my $this_section = (split(/:/,$person))[2];
1643: if ($this_section ne $last_section) {
1644: $i++;
1645: $last_section = $this_section;
1646: }
1647: } else {
1648: $i=int($student_counter/$helper->{'VARS'}{'NUMBER_TO_PRINT'});
1649: }
1650: my ($output,$fullname, $printed)=&print_resources($r,$helper,
1651: $person,$type,
1652: \%moreenv,\@master_seq,
1653: $flag_latex_header_remove,
1654: $LaTeXwidth);
1655: $resources_printed .= ":";
1656: $print_array[$i].=$output;
1657: $student_names[$i].=$person.':'.$fullname.'_END_';
1658: &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,&mt('last student').' '.$fullname);
1659: $flag_latex_header_remove = 'YES';
1660: if (&Apache::loncommon::connection_aborted($r)) { last; }
1661: }
1662: &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
1663: $result .= $print_array[0].' \end{document}';
1664: } elsif (($helper->{'VARS'}->{'PRINT_TYPE'} eq 'problems_for_anon') ||
1665: ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'resources_for_anon') ) {
1666: my $cdom =$env{'course.'.$env{'request.course.id'}.'.domain'};
1667: my $cnum =$env{'course.'.$env{'request.course.id'}.'.num'};
1668: my $num_todo=$helper->{'VARS'}->{'NUMBER_TO_PRINT_TOTAL'};
1669: my $code_name=$helper->{'VARS'}->{'ANON_CODE_STORAGE_NAME'};
1670: my $old_name=$helper->{'VARS'}->{'REUSE_OLD_CODES'};
1671: my $single_code = $helper->{'VARS'}->{'SINGLE_CODE'};
1672: my $selected_code = $helper->{'VARS'}->{'CODE_SELECTED_FROM_LIST'};
1673:
1674: my $code_option=$helper->{'VARS'}->{'CODE_OPTION'};
1675: open(FH,$Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab');
1676: my ($code_type,$code_length)=('letter',6);
1677: foreach my $line (<FH>) {
1678: my ($name,$type,$length) = (split(/:/,$line))[0,2,4];
1679: if ($name eq $code_option) {
1680: $code_length=$length;
1681: if ($type eq 'number') { $code_type = 'number'; }
1682: }
1683: }
1684: my %moreenv = ('textwidth' => &get_textwidth($helper,$LaTeXwidth));
1685: $moreenv{'problem_split'} = $parmhash{'problem_stream_switch'};
1686: $moreenv{'instructor_comments'}='hide';
1687: my $seed=time+($$<<16)+($$);
1688: my @allcodes;
1689: if ($old_name) {
1690: my %result=&Apache::lonnet::get('CODEs',
1691: [$old_name,"type\0$old_name"],
1692: $cdom,$cnum);
1693: $code_type=$result{"type\0$old_name"};
1694: @allcodes=split(',',$result{$old_name});
1695: $num_todo=scalar(@allcodes);
1696: } elsif ($selected_code) { # Selection value is always numeric.
1697: $num_todo = 1;
1698: @allcodes = ($selected_code);
1699: } elsif ($single_code) {
1700:
1701: $num_todo = 1; # Unconditionally one code to do.
1702: # If an alpha code have to convert to numbers so it can be
1703: # converted back to letters again :-)
1704: #
1705: if ($code_type ne 'number') {
1706: $single_code = &letters_to_num($single_code);
1707: }
1708: @allcodes = ($single_code);
1709: } else {
1710: my %allcodes;
1711: srand($seed);
1712: for (my $i=0;$i<$num_todo;$i++) {
1713: $moreenv{'CODE'}=&get_CODE(\%allcodes,$i,$seed,$code_length,
1714: $code_type);
1715: }
1716: if ($code_name) {
1717: &Apache::lonnet::put('CODEs',
1718: {
1719: $code_name =>join(',',keys(%allcodes)),
1720: "type\0$code_name" => $code_type
1721: },
1722: $cdom,$cnum);
1723: }
1724: @allcodes=keys(%allcodes);
1725: }
1726: my @master_seq=split /\|\|\|/, $helper->{'VARS'}->{'RESOURCES'};
1727: my ($type) = split(/_/,$helper->{'VARS'}->{'PRINT_TYPE'});
1728: &adjust_number_to_print($helper);
1729: my $number_per_page=$helper->{'VARS'}->{'NUMBER_TO_PRINT'};
1730: if ($number_per_page eq '0' || $number_per_page eq 'all') {
1731: $number_per_page=$num_todo;
1732: }
1733: my $flag_latex_header_remove = 'NO';
1734: my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin($r,'Print Status','Class Print Status',$num_todo,'inline','75');
1735: my $count=0;
1736: foreach my $code (sort(@allcodes)) {
1737: my $file_num=int($count/$number_per_page);
1738: if ($code_type eq 'number') {
1739: $moreenv{'CODE'}=$code;
1740: } else {
1741: $moreenv{'CODE'}=&num_to_letters($code);
1742: }
1743: my ($output,$fullname, $printed)=
1744: &print_resources($r,$helper,'anonymous',$type,\%moreenv,
1745: \@master_seq,$flag_latex_header_remove,
1746: $LaTeXwidth);
1747: $resources_printed .= ":";
1748: $print_array[$file_num].=$output;
1749: &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
1750: &mt('last assignment').' '.$fullname);
1751: $flag_latex_header_remove = 'YES';
1752: $count++;
1753: if (&Apache::loncommon::connection_aborted($r)) { last; }
1754: }
1755: &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
1756: $result .= $print_array[0].' \end{document}';
1757: } elsif ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'problems_from_directory') {
1758: #prints selected problems from the subdirectory
1759: $selectionmade = 6;
1760: my @list_of_files=split /\|\|\|/, $helper->{'VARS'}->{'FILES'};
1761: @list_of_files=sort @list_of_files;
1762: my $flag_latex_header_remove = 'NO';
1763: my $rndseed=time;
1764: if ($helper->{'VARS'}->{'curseed'}) {
1765: $rndseed=$helper->{'VARS'}->{'curseed'};
1766: }
1767: for (my $i=0;$i<=$#list_of_files;$i++) {
1768: my $urlp = $list_of_files[$i];
1769: $urlp=~s|//|/|;
1770: if ($urlp=~/\//) {
1771: $form{'problem_split'}=$parmhash{'problem_stream_switch'};
1772: $form{'rndseed'}=$rndseed;
1773: if ($urlp =~ m|/home/([^/]+)/public_html|) {
1774: $urlp =~ s|/home/([^/]*)/public_html|/~$1|;
1775: } else {
1776: $urlp =~ s|^$Apache::lonnet::perlvar{'lonDocRoot'}||;
1777: }
1778: $resources_printed .= $urlp.':';
1779: my $texversion=&Apache::lonnet::ssi($urlp,%form);
1780: if(($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') ||
1781: ($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'only')) {
1782: # Don't permanently pervert %form:
1783: my %answerform = %form;
1784: $answerform{'grade_target'}='answer';
1785: $answerform{'answer_output_mode'}='tex';
1786: $answerform{'latex_type'}=$helper->{'VARS'}->{'LATEX_TYPE'};
1787: $answerform{'rndseed'}=$rndseed;
1788: $resources_printed .= $urlp.':';
1789: my $answer=&Apache::lonnet::ssi($urlp,%answerform);
1790: if ($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') {
1791: $texversion=~s/(\\keephidden{ENDOFPROBLEM})/$answer$1/;
1792: } else {
1793: $texversion=&print_latex_header($helper->{'VARS'}->{'LATEX_TYPE'});
1794: if ($helper->{'VARS'}->{'construction'} ne '1') {
1795: $texversion.='\vskip 0 mm \noindent ';
1796: $texversion.=&path_to_problem ($urlp,$LaTeXwidth);
1797: } else {
1798: $texversion.='\vskip 0 mm \noindent\textbf{Prints from construction space - there is no title.}\vskip 0 mm ';
1799: my $URLpath=$urlp;
1800: $URLpath=~s/~([^\/]+)/public_html\/$1\/$1/;
1801: $texversion.=&path_to_problem ($URLpath,$LaTeXwidth);
1802: }
1803: $texversion.='\vskip 1 mm '.$answer.'\end{document}';
1804: }
1805: }
1806: #this chunck is responsible for printing the path to problem
1807: my $newurlp=$urlp;
1808: if ($newurlp=~/~/) {$newurlp=~s|\/~([^\/]+)\/|\/home\/$1\/public_html\/|;}
1809: $newurlp=&path_to_problem($newurlp,$LaTeXwidth);
1810: $texversion =~ s/(\\begin{minipage}{\\textwidth})/$1 $newurlp/;
1811: if ($flag_latex_header_remove ne 'NO') {
1812: $texversion = &latex_header_footer_remove($texversion);
1813: } else {
1814: $texversion =~ s/\\end{document}//;
1815: }
1816: if ($helper->{'VARS'}->{'TABLE_INDEX'} eq 'yes') {
1817: $texversion=&IndexCreation($texversion,$urlp);
1818: }
1819: if ($helper->{'VARS'}->{'CONSTR_RESOURSE_URL'} eq 'yes') {
1820: $texversion=~s/(\\addcontentsline\{toc\}\{subsection\}\{[^\}]*\})/$1 URL: \\verb|$urlp| \\strut\\\\\\strut /;
1821:
1822: }
1823: $result .= $texversion;
1824: }
1825: $flag_latex_header_remove = 'YES';
1826: }
1827: if ($helper->{VARS}->{'construction'} eq '1') {$result=~s/(\\typeout)/ RANDOM SEED IS $rndseed $1/;}
1828: $result .= '\end{document}';
1829: }
1830: #-------------------------------------------------------- corrections for the different page formats
1831: $result = &page_format_transformation($papersize,$laystyle,$numberofcolumns,$helper->{'VARS'}->{'PRINT_TYPE'},$result,$helper->{VARS}->{'assignment'},$helper->{'VARS'}->{'TABLE_CONTENTS'},$helper->{'VARS'}->{'TABLE_INDEX'},$selectionmade);
1832: $result = &latex_corrections($number_of_columns,$result,$selectionmade,
1833: $helper->{'VARS'}->{'ANSWER_TYPE'});
1834: #if ($numberofcolumns == 1) {
1835: $result =~ s/\\textwidth\s*=\s*-?\d*\.?\d*\s*(cm|mm|in)/\\textwidth= $helper->{'VARS'}->{'pagesize.width'} $helper->{'VARS'}->{'pagesize.widthunit'} /;
1836: $result =~ s/\\textheight\s*=?\s*-?\d*\.?\d*\s*(cm|mm|in)/\\textheight $helper->{'VARS'}->{'pagesize.height'} $helper->{'VARS'}->{'pagesize.heightunit'} /;
1837: $result =~ s/\\evensidemargin\s*=\s*-?\d*\.?\d*\s*(cm|mm|in)/\\evensidemargin= $helper->{'VARS'}->{'pagesize.lmargin'} $helper->{'VARS'}->{'pagesize.lmarginunit'} /;
1838: $result =~ s/\\oddsidemargin\s*=\s*-?\d*\.?\d*\s*(cm|mm|in)/\\oddsidemargin= $helper->{'VARS'}->{'pagesize.lmargin'} $helper->{'VARS'}->{'pagesize.lmarginunit'} /;
1839: #}
1840:
1841: #-- writing .tex file in prtspool
1842: my $temp_file;
1843: my $identifier = &Apache::loncommon::get_cgi_id();
1844: my $filename = "/home/httpd/prtspool/$env{'user.name'}_$env{'user.domain'}_printout_$identifier.tex";
1845: if (!($#print_array>0)) {
1846: unless ($temp_file = Apache::File->new('>'.$filename)) {
1847: $r->log_error("Couldn't open $filename for output $!");
1848: return SERVER_ERROR;
1849: }
1850: print $temp_file $result;
1851: my $begin=index($result,'\begin{document}',0);
1852: my $inc=substr($result,0,$begin+16);
1853: } else {
1854: my $begin=index($result,'\begin{document}',0);
1855: my $inc=substr($result,0,$begin+16);
1856: for (my $i=0;$i<=$#print_array;$i++) {
1857: if ($i==0) {
1858: $print_array[$i]=$result;
1859: } else {
1860: $print_array[$i].='\end{document}';
1861: $print_array[$i] =
1862: &latex_corrections($number_of_columns,$print_array[$i],
1863: $selectionmade,
1864: $helper->{'VARS'}->{'ANSWER_TYPE'});
1865:
1866: my $anobegin=index($print_array[$i],'\setcounter{page}',0);
1867: substr($print_array[$i],0,$anobegin)='';
1868: $print_array[$i]=$inc.$print_array[$i];
1869: }
1870: my $temp_file;
1871: my $newfilename=$filename;
1872: my $num=$i+1;
1873: $newfilename =~s/\.tex$//;
1874: $newfilename=sprintf("%s_%03d.tex",$newfilename, $num);
1875: unless ($temp_file = Apache::File->new('>'.$newfilename)) {
1876: $r->log_error("Couldn't open $newfilename for output $!");
1877: return SERVER_ERROR;
1878: }
1879: print $temp_file $print_array[$i];
1880: }
1881: }
1882: my $student_names='';
1883: if ($#print_array>0) {
1884: for (my $i=0;$i<=$#print_array;$i++) {
1885: $student_names.=$student_names[$i].'_ENDPERSON_';
1886: }
1887: } else {
1888: if ($#student_names>-1) {
1889: $student_names=$student_names[0].'_ENDPERSON_';
1890: } else {
1891: my $fullname = &get_name($env{'user.name'},$env{'user.domain'});
1892: $student_names=join(':',$env{'user.name'},$env{'user.domain'},
1893: $env{'request.course.sec'},$fullname).
1894: '_ENDPERSON_'.'_END_';
1895: }
1896: }
1897:
1898: my $URLback=''; #link to original document
1899: if ($helper->{'VARS'}->{'construction'} ne '1') {
1900: #prints published resource
1901: $URLback=&escape('/adm/flip?postdata=return:');
1902: } else {
1903: #prints resource from the construction space
1904: $URLback='/'.$helper->{'VARS'}->{'filename'};
1905: if ($URLback=~/([^?]+)/) {
1906: $URLback=$1;
1907: $URLback=~s|^/~|/priv/|;
1908: }
1909: }
1910: # logic for now is too complex to trace if this has been defined
1911: # yet.
1912: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
1913: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
1914: &Apache::lonnet::appenv('cgi.'.$identifier.'.file' => $filename,
1915: 'cgi.'.$identifier.'.layout' => $laystyle,
1916: 'cgi.'.$identifier.'.numcol' => $numberofcolumns,
1917: 'cgi.'.$identifier.'.paper' => $papersize,
1918: 'cgi.'.$identifier.'.selection' => $selectionmade,
1919: 'cgi.'.$identifier.'.tableofcontents' => $helper->{'VARS'}->{'TABLE_CONTENTS'},
1920: 'cgi.'.$identifier.'.tableofindex' => $helper->{'VARS'}->{'TABLE_INDEX'},
1921: 'cgi.'.$identifier.'.role' => $perm{'pav'},
1922: 'cgi.'.$identifier.'.numberoffiles' => $#print_array,
1923: 'cgi.'.$identifier.'.studentnames' => $student_names,
1924: 'cgi.'.$identifier.'.backref' => $URLback,);
1925: &Apache::lonnet::appenv("cgi.$identifier.user" => $env{'user.name'},
1926: "cgi.$identifier.domain" => $env{'user.domain'},
1927: "cgi.$identifier.courseid" => $cnum,
1928: "cgi.$identifier.coursedom" => $cdom,
1929: "cgi.$identifier.resources" => $resources_printed);
1930:
1931: my $end_page = &Apache::loncommon::end_page();
1932: $r->print(<<FINALEND);
1933: <br />
1934: <meta http-equiv="Refresh" content="0; url=/cgi-bin/printout.pl?$identifier" />
1935: <a href="/cgi-bin/printout.pl?$identifier">Continue</a>
1936: $end_page
1937: FINALEND
1938: }
1939:
1940:
1941: sub get_CODE {
1942: my ($all_codes,$num,$seed,$size,$type)=@_;
1943: my $max='1'.'0'x$size;
1944: my $newcode;
1945: while(1) {
1946: $newcode=sprintf("%0".$size."d",int(rand($max)));
1947: if (!exists($$all_codes{$newcode})) {
1948: $$all_codes{$newcode}=1;
1949: if ($type eq 'number' ) {
1950: return $newcode;
1951: } else {
1952: return &num_to_letters($newcode);
1953: }
1954: }
1955: }
1956: }
1957:
1958: sub print_resources {
1959: my ($r,$helper,$person,$type,$moreenv,$master_seq,$remove_latex_header,
1960: $LaTeXwidth)=@_;
1961: my $current_output = '';
1962: my $printed = '';
1963: my ($username,$userdomain,$usersection) = split /:/,$person;
1964: my $fullname = &get_name($username,$userdomain);
1965: my $namepostfix;
1966: if ($person =~ 'anon') {
1967: $namepostfix="\\\\Name: ";
1968: $fullname = "CODE - ".$moreenv->{'CODE'};
1969: }
1970: # Fullname may have special latex characters that need \ prefixing:
1971: #
1972:
1973: my $i = 0;
1974: #goes through all resources, checks if they are available for
1975: #current student, and produces output
1976:
1977: &Apache::lonxml::clear_problem_counter();
1978: my %page_breaks = &get_page_breaks($helper);
1979: my $columns_in_format = (split(/\|/,$helper->{'VARS'}->{'FORMAT'}))[1];
1980: #
1981: # end each student with a
1982: # Special that allows the post processor to even out the page
1983: # counts later. Nasty problem this... it would be really
1984: # nice to put the special in as a postscript comment
1985: # e.g. \special{ps:\ENDOFSTUDENTSTAMP} unfortunately,
1986: # The special gets passed the \ and dvips puts it in the output file
1987: # so we will just rely on prntout.pl to strip ENDOFSTUDENTSTAMP from the
1988: # postscript. Each ENDOFSTUDENTSTAMP will go on a line by itself.
1989: #
1990:
1991: foreach my $curresline (@{$master_seq}) {
1992: if (defined $page_breaks{$curresline}) {
1993: if($i != 0) {
1994: $current_output.= "\\newpage\n";
1995: }
1996: }
1997: $i++;
1998: if ( !($type eq 'problems' &&
1999: ($curresline!~ m/\.(problem|exam|quiz|assess|survey|form|library)$/)) ) {
2000: my ($map,$id,$res_url) = &Apache::lonnet::decode_symb($curresline);
2001: if (&Apache::lonnet::allowed('bre',$res_url)) {
2002: if ($res_url!~m|^ext/|
2003: && $res_url=~/\.(problem|exam|quiz|assess|survey|form|library|page|xml|html|htm|xhtml|xhtm)$/) {
2004: $printed .= $curresline.':';
2005:
2006: &Apache::lonxml::remember_problem_counter();
2007:
2008: my $rendered = &Apache::loncommon::get_student_view($curresline,$username,$userdomain,$env{'request.course.id'},'tex',$moreenv);
2009:
2010: if(($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') ||
2011: ($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'only')) {
2012: # Use a copy of the hash so we don't pervert it on future loop passes.
2013: my %answerenv = %{$moreenv};
2014: $answerenv{'answer_output_mode'}='tex';
2015: $answerenv{'latex_type'}=$helper->{'VARS'}->{'LATEX_TYPE'};
2016:
2017: &Apache::lonxml::restore_problem_counter();
2018:
2019: my $ansrendered = &Apache::loncommon::get_student_answers($curresline,$username,$userdomain,$env{'request.course.id'},%answerenv);
2020:
2021: if ($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') {
2022: $rendered=~s/(\\keephidden{ENDOFPROBLEM})/$ansrendered$1/;
2023: } else {
2024:
2025:
2026: my $header =&print_latex_header($helper->{'VARS'}->{'LATEX_TYPE'});
2027: my $title = &Apache::lonnet::gettitle($curresline);
2028: $title = &Apache::lonxml::latex_special_symbols($title);
2029: my $body ='\vskip 0 mm \noindent\textbf{'.$title.'}\vskip 0 mm ';
2030: $body .=&path_to_problem($res_url,$LaTeXwidth);
2031: $body .='\vskip 1 mm '.$ansrendered;
2032: $body = &encapsulate_minipage($body);
2033: $rendered = $header.$body;
2034: }
2035: }
2036: if ($remove_latex_header eq 'YES') {
2037: $rendered = &latex_header_footer_remove($rendered);
2038: } else {
2039: $rendered =~ s/\\end{document}//;
2040: }
2041: $current_output .= $rendered;
2042: } elsif ($res_url=~/\/(smppg|syllabus|aboutme|bulletinboard)$/) {
2043: $printed .= $curresline.':';
2044: my $rendered = &Apache::loncommon::get_student_view($curresline,$username,$userdomain,$env{'request.course.id'},'tex',$moreenv);
2045:
2046: if ($remove_latex_header eq 'YES') {
2047: $rendered = &latex_header_footer_remove($rendered);
2048: } else {
2049: $rendered =~ s/\\end{document}//;
2050: }
2051: $current_output .= $rendered.'\vskip 0.5mm\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}\strut \vskip 0 mm \strut ';
2052:
2053: } else {
2054: my $rendered = &unsupported($res_url,$helper->{'VARS'}->{'LATEX_TYPE'},$curresline);
2055: if ($remove_latex_header ne 'NO') {
2056: $rendered = &latex_header_footer_remove($rendered);
2057: } else {
2058: $rendered =~ s/\\end{document}//;
2059: }
2060: $current_output .= $rendered;
2061: }
2062: }
2063: $remove_latex_header = 'YES';
2064: }
2065: if (&Apache::loncommon::connection_aborted($r)) { last; }
2066: }
2067: my $courseidinfo = &get_course();
2068: if (defined($courseidinfo)) { $courseidinfo=' - '.$courseidinfo }
2069: if ($usersection ne '') {$courseidinfo.=' - Sec. '.$usersection}
2070: my $currentassignment=&Apache::lonxml::latex_special_symbols($helper->{VARS}->{'assignment'},'header');
2071: my $header_line =
2072: &format_page_header($LaTeXwidth, $parmhash{'print_header_format'},
2073: $currentassignment, $courseidinfo, $fullname);
2074: my $header_start = ($columns_in_format == 1) ? '\lhead'
2075: : '\fancyhead[LO]';
2076: $header_line = $header_start.'{'.$header_line.'}';
2077:
2078: if ($current_output=~/\\documentclass/) {
2079: $current_output =~ s/\\begin{document}/\\setlength{\\topmargin}{1cm} \\begin{document}\\noindent\\parbox{\\minipagewidth}{\\noindent$header_line$namepostfix}\\vskip 5 mm /;
2080: } else {
2081: my $blankpages =
2082: '\clearpage\strut\clearpage'x$helper->{'VARS'}->{'EMPTY_PAGES'};
2083:
2084: $current_output = '\strut\vspace*{-6 mm}\\newline'.
2085: ©right_line().' \newpage '.$blankpages.$end_of_student.
2086: '\setcounter{page}{1}\noindent\parbox{\minipagewidth}{\noindent'.
2087: $header_line.$namepostfix.'} \vskip 5 mm '.$current_output;
2088: }
2089: #
2090: # Close the student bracketing.
2091: #
2092: return ($current_output,$fullname, $printed);
2093:
2094: }
2095:
2096: sub handler {
2097:
2098: my $r = shift;
2099:
2100: &init_perm();
2101:
2102:
2103:
2104: my $helper = printHelper($r);
2105: if (!ref($helper)) {
2106: return $helper;
2107: }
2108:
2109:
2110: %parmhash=&Apache::lonnet::coursedescription($env{'request.course.id'});
2111:
2112:
2113:
2114:
2115: # If a figure conversion queue file exists for this user.domain
2116: # we delete it since it can only be bad (if it were good, printout.pl
2117: # would have deleted it the last time around.
2118:
2119: my $conversion_queuefile = "/home/httpd/prtspool/$env{'user.name'}_$env{'user.domain'}_printout.dat";
2120: if(-e $conversion_queuefile) {
2121: unlink $conversion_queuefile;
2122: }
2123: &output_data($r,$helper,\%parmhash);
2124: return OK;
2125: }
2126:
2127: use Apache::lonhelper;
2128:
2129: sub addMessage {
2130: my $text = shift;
2131: my $paramHash = Apache::lonhelper::getParamHash();
2132: $paramHash->{MESSAGE_TEXT} = $text;
2133: Apache::lonhelper::message->new();
2134: }
2135:
2136:
2137:
2138: sub init_perm {
2139: undef(%perm);
2140: $perm{'pav'}=&Apache::lonnet::allowed('pav',$env{'request.course.id'});
2141: if (!$perm{'pav'}) {
2142: $perm{'pav'}=&Apache::lonnet::allowed('pav',
2143: $env{'request.course.id'}.'/'.$env{'request.course.sec'});
2144: }
2145: $perm{'pfo'}=&Apache::lonnet::allowed('pfo',$env{'request.course.id'});
2146: if (!$perm{'pfo'}) {
2147: $perm{'pfo'}=&Apache::lonnet::allowed('pfo',
2148: $env{'request.course.id'}.'/'.$env{'request.course.sec'});
2149: }
2150: }
2151:
2152: sub printHelper {
2153: my $r = shift;
2154:
2155: if ($r->header_only) {
2156: if ($env{'browser.mathml'}) {
2157: &Apache::loncommon::content_type($r,'text/xml');
2158: } else {
2159: &Apache::loncommon::content_type($r,'text/html');
2160: }
2161: $r->send_http_header;
2162: return OK;
2163: }
2164:
2165: # Send header, nocache
2166: if ($env{'browser.mathml'}) {
2167: &Apache::loncommon::content_type($r,'text/xml');
2168: } else {
2169: &Apache::loncommon::content_type($r,'text/html');
2170: }
2171: &Apache::loncommon::no_cache($r);
2172: $r->send_http_header;
2173: $r->rflush();
2174:
2175: # Unfortunately, this helper is so complicated we have to
2176: # write it by hand
2177:
2178: Apache::loncommon::get_unprocessed_cgi($ENV{QUERY_STRING});
2179:
2180: my $helper = Apache::lonhelper::helper->new("Printing Helper");
2181: $helper->declareVar('symb');
2182: $helper->declareVar('postdata');
2183: $helper->declareVar('curseed');
2184: $helper->declareVar('probstatus');
2185: $helper->declareVar('filename');
2186: $helper->declareVar('construction');
2187: $helper->declareVar('assignment');
2188: $helper->declareVar('style_file');
2189: $helper->declareVar('student_sort');
2190: $helper->declareVar('FINISHPAGE');
2191: $helper->declareVar('PRINT_TYPE');
2192: $helper->declareVar("showallfoils");
2193: $helper->declareVar("STUDENTS");
2194:
2195: # The page breaks can get loaded initially from the course environment:
2196: # But we only do this in the initial state so that they are allowed to change.
2197: #
2198:
2199: # $helper->{VARS}->{FINISHPAGE} = '';
2200:
2201: &Apache::loncommon::restore_course_settings('print',
2202: {'pagebreaks' => 'scalar',
2203: 'lastprinttype' => 'scalar'});
2204:
2205: # This will persistently load in the data we want from the
2206: # very first screen.
2207:
2208: if($helper->{VARS}->{PRINT_TYPE} eq $env{'form.lastprinttype'}) {
2209: if (!defined ($env{"form.CURRENT_STATE"})) {
2210:
2211: $helper->{VARS}->{FINISHPAGE} = $env{'form.pagebreaks'};
2212: } else {
2213: my $state = $env{"form.CURRENT_STATE"};
2214: if ($state eq "START") {
2215: $helper->{VARS}->{FINISHPAGE} = $env{'form.pagebreaks'};
2216: }
2217: }
2218:
2219: }
2220:
2221:
2222: # Detect whether we're coming from construction space
2223: if ($env{'form.postdata'}=~/^(?:http:\/\/[^\/]+\/|\/|)\~([^\/]+)\/(.*)$/) {
2224: $helper->{VARS}->{'filename'} = "~$1/$2";
2225: $helper->{VARS}->{'construction'} = 1;
2226: } else {
2227: if ($env{'form.postdata'}) {
2228: $helper->{VARS}->{'symb'} = &Apache::lonnet::symbread($env{'form.postdata'});
2229: if ( $helper->{VARS}->{'symb'} eq '') {
2230: $helper->{VARS}->{'postdata'} = $env{'form.postdata'};
2231: }
2232: }
2233: if ($env{'form.symb'}) {
2234: $helper->{VARS}->{'symb'} = $env{'form.symb'};
2235: }
2236: if ($env{'form.url'}) {
2237: $helper->{VARS}->{'symb'} = &Apache::lonnet::symbread($helper->{VARS}->{'postdata'});
2238: }
2239:
2240: }
2241:
2242: if ($env{'form.symb'}) {
2243: $helper->{VARS}->{'symb'} = $env{'form.symb'};
2244: }
2245: if ($env{'form.url'}) {
2246: $helper->{VARS}->{'symb'} = &Apache::lonnet::symbread($helper->{VARS}->{'postdata'});
2247:
2248: }
2249: $helper->{VARS}->{'symb'}=
2250: &Apache::lonenc::check_encrypt($helper->{VARS}->{'symb'});
2251: my ($resourceTitle,$sequenceTitle,$mapTitle) = &details_for_menu($helper);
2252: if ($sequenceTitle ne '') {$helper->{VARS}->{'assignment'}=$sequenceTitle;}
2253:
2254:
2255: # Extract map
2256: my $symb = $helper->{VARS}->{'symb'};
2257: my ($map, $id, $url);
2258: my $subdir;
2259: my $is_published=0; # True when printing from resource space.
2260:
2261: # Get the resource name from construction space
2262: if ($helper->{VARS}->{'construction'}) {
2263: $resourceTitle = substr($helper->{VARS}->{'filename'},
2264: rindex($helper->{VARS}->{'filename'}, '/')+1);
2265: $subdir = substr($helper->{VARS}->{'filename'},
2266: 0, rindex($helper->{VARS}->{'filename'}, '/') + 1);
2267: } else {
2268: if ($symb ne '') {
2269: ($map, $id, $url) = &Apache::lonnet::decode_symb($symb);
2270: $helper->{VARS}->{'postdata'} =
2271: &Apache::lonenc::check_encrypt(&Apache::lonnet::clutter($url));
2272: } else {
2273: $url = $helper->{VARS}->{'postdata'};
2274: $is_published=1; # From resource space.
2275: }
2276: $url = &Apache::lonnet::clutter($url);
2277:
2278: if (!$resourceTitle) { # if the resource doesn't have a title, use the filename
2279: my $postdata = $helper->{VARS}->{'postdata'};
2280: $resourceTitle = substr($postdata, rindex($postdata, '/') + 1);
2281: }
2282: $subdir = &Apache::lonnet::filelocation("", $url);
2283: }
2284: if (!$helper->{VARS}->{'curseed'} && $env{'form.curseed'}) {
2285: $helper->{VARS}->{'curseed'}=$env{'form.curseed'};
2286: }
2287: if (!$helper->{VARS}->{'probstatus'} && $env{'form.problemtype'}) {
2288: $helper->{VARS}->{'probstatus'}=$env{'form.problemtype'};
2289: }
2290:
2291: my $userCanSeeHidden = Apache::lonnavmaps::advancedUser();
2292:
2293: Apache::lonhelper::registerHelperTags();
2294:
2295: # "Delete everything after the last slash."
2296: $subdir =~ s|/[^/]+$||;
2297:
2298: # What can be printed is a very dynamic decision based on
2299: # lots of factors. So we need to dynamically build this list.
2300: # To prevent security leaks, states are only added to the wizard
2301: # if they can be reached, which ensures manipulating the form input
2302: # won't allow anyone to reach states they shouldn't have permission
2303: # to reach.
2304:
2305: # printChoices is tracking the kind of printing the user can
2306: # do, and will be used in a choices construction later.
2307: # In the meantime we will be adding states and elements to
2308: # the helper by hand.
2309: my $printChoices = [];
2310: my $paramHash;
2311:
2312: if ($resourceTitle) {
2313: push @{$printChoices}, ["<b><i>$resourceTitle</i></b> (".&mt('the resource you just saw on the screen').")", 'current_document', 'PAGESIZE'];
2314: }
2315:
2316: # Useful filter strings
2317: my $isProblem = '($res->is_problem()||$res->contains_problem) ';
2318: $isProblem .= ' && !$res->randomout()' if !$userCanSeeHidden;
2319: my $isProblemOrMap = '$res->is_problem() || $res->contains_problem() || $res->is_sequence()';
2320: my $isNotMap = '!$res->is_sequence()';
2321: $isNotMap .= ' && !$res->randomout()' if !$userCanSeeHidden;
2322: my $isMap = '$res->is_map()';
2323: my $symbFilter = '$res->shown_symb()';
2324: my $urlValue = '$res->link()';
2325:
2326: $helper->declareVar('SEQUENCE');
2327:
2328: # If we're in a sequence...
2329:
2330: my $start_new_option;
2331: if ($perm{'pav'}) {
2332: $start_new_option =
2333: "<option text='".&mt('Start new page<br />before selected').
2334: "' variable='FINISHPAGE' />";
2335: }
2336:
2337: if (($helper->{'VARS'}->{'construction'} ne '1' ) &&
2338:
2339: $helper->{VARS}->{'postdata'} &&
2340: $helper->{VARS}->{'assignment'}) {
2341: # Allow problems from sequence
2342: push @{$printChoices}, [&mt('Selected <b>Problems</b> in folder <b><i>[_1]</i></b>',$sequenceTitle), 'map_problems', 'CHOOSE_PROBLEMS'];
2343: # Allow all resources from sequence
2344: push @{$printChoices}, [&mt('Selected <b>Resources</b> in folder <b><i>[_1]</i></b>',$sequenceTitle), 'map_problems_pages', 'CHOOSE_PROBLEMS_HTML'];
2345:
2346: my $helperFragment = <<HELPERFRAGMENT;
2347: <state name="CHOOSE_PROBLEMS" title="Select Problem(s) to print">
2348: <resource variable="RESOURCES" multichoice="1" toponly='1' addstatus="1"
2349: closeallpages="1">
2350: <nextstate>PAGESIZE</nextstate>
2351: <filterfunc>return $isProblem;</filterfunc>
2352: <mapurl>$map</mapurl>
2353: <valuefunc>return $symbFilter;</valuefunc>
2354: $start_new_option
2355: </resource>
2356: </state>
2357:
2358: <state name="CHOOSE_PROBLEMS_HTML" title="Select Resource(s) to print">
2359: <resource variable="RESOURCES" multichoice="1" toponly='1' addstatus="1"
2360: closeallpages="1">
2361: <nextstate>PAGESIZE</nextstate>
2362: <filterfunc>return $isNotMap;</filterfunc>
2363: <mapurl>$map</mapurl>
2364: <valuefunc>return $symbFilter;</valuefunc>
2365: $start_new_option
2366: </resource>
2367: </state>
2368: HELPERFRAGMENT
2369:
2370: &Apache::lonxml::xmlparse($r, 'helper', $helperFragment);
2371: }
2372:
2373: # If the user has pfo (print for otheres) allow them to print all
2374: # problems and resources in the entier course, optionally for selected students
2375: if ($perm{'pfo'} && !$is_published &&
2376: ($helper->{VARS}->{'postdata'}=~/\/res\// || $helper->{VARS}->{'postdata'}=~/\/(syllabus|smppg|aboutme|bulletinboard)$/)) {
2377:
2378: push @{$printChoices}, ['Selected <b>Problems</b> from <b>entire course</b>', 'all_problems', 'ALL_PROBLEMS'];
2379: push @{$printChoices}, ['Selected <b>Resources</b> from <b>entire course</b>', 'all_resources', 'ALL_RESOURCES'];
2380: &Apache::lonxml::xmlparse($r, 'helper', <<ALL_PROBLEMS);
2381: <state name="ALL_PROBLEMS" title="Select Problem(s) to print">
2382: <resource variable="RESOURCES" toponly='0' multichoice="1"
2383: suppressEmptySequences='0' addstatus="1" closeallpages="1">
2384: <nextstate>PAGESIZE</nextstate>
2385: <filterfunc>return $isProblemOrMap;</filterfunc>
2386: <choicefunc>return $isNotMap;</choicefunc>
2387: <valuefunc>return $symbFilter;</valuefunc>
2388: $start_new_option
2389: </resource>
2390: </state>
2391: <state name="ALL_RESOURCES" title="Select Resource(s) to print">
2392: <resource variable="RESOURCES" toponly='0' multichoice='1'
2393: suppressEmptySequences='0' addstatus='1' closeallpages='1'>
2394: <nextstate>PAGESIZE</nextstate>
2395: <filterfunc>return $isNotMap; </filterfunc>
2396: <valuefunc>return $symbFilter;</valuefunc>
2397: $start_new_option
2398: </resource>
2399: </state>
2400: ALL_PROBLEMS
2401:
2402: if ($helper->{VARS}->{'assignment'}) {
2403: push @{$printChoices}, [&mt("Selected <b>Problems</b> from folder <b><i>[_1]</i></b> for <b>selected people</b>",$sequenceTitle), 'problems_for_students', 'CHOOSE_STUDENTS'];
2404: push @{$printChoices}, [&mt("Selected <b>Problems</b> from folder <b><i>[_1]</i></b> for <b>CODEd assignments</b>",$sequenceTitle), 'problems_for_anon', 'CHOOSE_ANON1'];
2405: }
2406:
2407: # resource_selector will hold a few states that:
2408: # - Allow resources to be selected for printing.
2409: # - Determine pagination between assignments.
2410: # - Determine how many assignments should be bundled into a single PDF.
2411: # TODO:
2412: # Probably good to do things like separate this up into several vars, each
2413: # with one state, and use REGEXPs at inclusion time to set state names
2414: # and next states for better mix and match capability
2415: #
2416: my $resource_selector=<<RESOURCE_SELECTOR;
2417: <state name="SELECT_PROBLEMS" title="Select resources to print">
2418: <nextstate>PRINT_FORMATTING</nextstate>
2419: <message><br /><big><i><b>Select resources for the assignment</b></i></big><br /></message>
2420: <resource variable="RESOURCES" multichoice="1" addstatus="1"
2421: closeallpages="1">
2422: <filterfunc>return $isProblem;</filterfunc>
2423: <mapurl>$map</mapurl>
2424: <valuefunc>return $symbFilter;</valuefunc>
2425: $start_new_option
2426: </resource>
2427: </state>
2428: <state name="PRINT_FORMATTING" title="How should results be printed?">
2429: <message><br /><big><i><b>How should the results be printed?</b></i></big><br /></message>
2430: <choices variable="EMPTY_PAGES">
2431: <choice computer='0'>Start each student\'s assignment on a new page/column (add a pagefeed after each assignment)</choice>
2432: <choice computer='1'>Add one empty page/column after each student\'s assignment</choice>
2433: <choice computer='2'>Add two empty pages/column after each student\'s assignment</choice>
2434: <choice computer='3'>Add three empty pages/column after each student\'s assignment</choice>
2435: </choices>
2436: <nextstate>PAGESIZE</nextstate>
2437: <message><hr width='33%' /><b>How do you want assignments split into PDF files? </b></message>
2438: <choices variable="SPLIT_PDFS">
2439: <choice computer="all">All assignments in a single PDF file</choice>
2440: <choice computer="sections">Each PDF contains exactly one section</choice>
2441: <choice computer="oneper">Each PDF contains exactly one assignment</choice>
2442: <choice computer="usenumber" relatedvalue="NUMBER_TO_PRINT">
2443: Specify the number of assignments per PDF:</choice>
2444: </choices>
2445: </state>
2446: RESOURCE_SELECTOR
2447:
2448: &Apache::lonxml::xmlparse($r, 'helper', <<CHOOSE_STUDENTS);
2449: <state name="CHOOSE_STUDENTS" title="Select Students and Resources">
2450: <message><b>Select sorting order of printout</b> </message>
2451: <choices variable='student_sort'>
2452: <choice computer='0'>Sort by section then student</choice>
2453: <choice computer='1'>Sort by students across sections.</choice>
2454: </choices>
2455: <message><br /><hr /><br /> </message>
2456: <student multichoice='1' variable="STUDENTS" nextstate="SELECT_PROBLEMS" coursepersonnel="1"/>
2457: </state>
2458: $resource_selector
2459: CHOOSE_STUDENTS
2460:
2461: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
2462: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
2463: my @names=&Apache::lonnet::getkeys('CODEs',$cdom,$cnum);
2464: my $namechoice='<choice></choice>';
2465: foreach my $name (sort {uc($a) cmp uc($b)} @names) {
2466: if ($name =~ /^error: 2 /) { next; }
2467: if ($name =~ /^type\0/) { next; }
2468: $namechoice.='<choice computer="'.$name.'">'.$name.'</choice>';
2469: }
2470:
2471:
2472: my %code_values;
2473: my %codes_to_print;
2474: foreach my $key (@names) {
2475: %code_values = &Apache::grades::get_codes($key, $cdom, $cnum);
2476: foreach my $key (keys(%code_values)) {
2477: $codes_to_print{$key} = 1;
2478: }
2479: }
2480:
2481: my $code_selection;
2482: foreach my $code (sort {uc($a) cmp uc($b)} (keys(%codes_to_print))) {
2483: my $choice = $code;
2484: if ($code =~ /^[A-Z]+$/) { # Alpha code
2485: $choice = &letters_to_num($code);
2486: }
2487: push(@{$helper->{DATA}{ALL_CODE_CHOICES}},[$code,$choice]);
2488: }
2489: if (%codes_to_print) {
2490: $code_selection .='
2491: <message><b>Choose single CODE from list:</b></message>
2492: <message></td><td></message>
2493: <dropdown variable="CODE_SELECTED_FROM_LIST" multichoice="0" allowempty="0">
2494: <choice></choice>
2495: <exec>
2496: push(@{$state->{CHOICES}},@{$helper->{DATA}{ALL_CODE_CHOICES}});
2497: </exec>
2498: </dropdown>
2499: <message></td></tr><tr><td></message>
2500: '.$/;
2501:
2502: }
2503:
2504:
2505: open(FH,$Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab');
2506: my $codechoice='';
2507: foreach my $line (<FH>) {
2508: my ($name,$description,$code_type,$code_length)=
2509: (split(/:/,$line))[0,1,2,4];
2510: if ($code_length > 0 &&
2511: $code_type =~/^(letter|number|-1)/) {
2512: $codechoice.='<choice computer="'.$name.'">'.$description.'</choice>';
2513: }
2514: }
2515: if ($codechoice eq '') {
2516: $codechoice='<choice computer="default">Default</choice>';
2517: }
2518: &Apache::lonxml::xmlparse($r, 'helper', <<CHOOSE_ANON1);
2519: <state name="CHOOSE_ANON1" title="Specify CODEd Assignments">
2520: <nextstate>SELECT_PROBLEMS</nextstate>
2521: <message><h4>Fill out one of the forms below</h4></message>
2522: <message><br /><hr /> <br /></message>
2523: <message><h3>Generate new CODEd Assignments</h3></message>
2524: <message><table><tr><td><b>Number of CODEd assignments to print:</b></td><td></message>
2525: <string variable="NUMBER_TO_PRINT_TOTAL" maxlength="5" size="5">
2526: <validator>
2527: if (((\$helper->{'VARS'}{'NUMBER_TO_PRINT_TOTAL'}+0) < 1) &&
2528: !\$helper->{'VARS'}{'REUSE_OLD_CODES'} &&
2529: !\$helper->{'VARS'}{'SINGLE_CODE'} &&
2530: !\$helper->{'VARS'}{'CODE_SELECTED_FROM_LIST'}) {
2531: return "You need to specify the number of assignments to print";
2532: }
2533: return undef;
2534: </validator>
2535: </string>
2536: <message></td></tr><tr><td></message>
2537: <message><b>Names to store the CODEs under for later:</b></message>
2538: <message></td><td></message>
2539: <string variable="ANON_CODE_STORAGE_NAME" maxlength="50" size="20" />
2540: <message></td></tr><tr><td></message>
2541: <message><b>Bubble sheet type:</b></message>
2542: <message></td><td></message>
2543: <dropdown variable="CODE_OPTION" multichoice="0" allowempty="0">
2544: $codechoice
2545: </dropdown>
2546: <message></td></tr><tr><td colspan="2"></td></tr><tr><td></message>
2547: <message></td></tr><tr><td></table></message>
2548: <message><br /><hr /><h3>Print a Specific CODE </h3><br /><table></message>
2549: <message><tr><td><b>Enter a CODE to print:</b></td><td></message>
2550: <string variable="SINGLE_CODE" size="10">
2551: <validator>
2552: if(!\$helper->{'VARS'}{'NUMBER_TO_PRINT_TOTAL'} &&
2553: !\$helper->{'VARS'}{'REUSE_OLD_CODES'} &&
2554: !\$helper->{'VARS'}{'CODE_SELECTED_FROM_LIST'}) {
2555: return &Apache::lonprintout::is_code_valid(\$helper->{'VARS'}{'SINGLE_CODE'},
2556: \$helper->{'VARS'}{'CODE_OPTION'});
2557: } else {
2558: return undef; # Other forces control us.
2559: }
2560: </validator>
2561: </string>
2562: <message></td></tr><tr><td></message>
2563: $code_selection
2564: <message></td></tr></table></message>
2565: <message><hr /><h3>Reprint a Set of Saved CODEs</h3><table><tr><td></message>
2566: <message><b>Select saved CODEs:</b></message>
2567: <message></td><td></message>
2568: <dropdown variable="REUSE_OLD_CODES">
2569: $namechoice
2570: </dropdown>
2571: <message></td></tr></table></message>
2572: </state>
2573: $resource_selector
2574: CHOOSE_ANON1
2575:
2576:
2577: if ($helper->{VARS}->{'assignment'}) {
2578: push @{$printChoices}, [&mt("Selected <b>Resources</b> from folder <b><i>[_1]</i></b> for <b>selected people</b>",$sequenceTitle), 'resources_for_students', 'CHOOSE_STUDENTS1'];
2579: push @{$printChoices}, [&mt("Selected <b>Resources</b> from folder <b><i>[_1]</i></b> for <b>CODEd assignments</b>",$sequenceTitle), 'resources_for_anon', 'CHOOSE_ANON2'];
2580: }
2581:
2582:
2583: $resource_selector=<<RESOURCE_SELECTOR;
2584: <state name="SELECT_RESOURCES" title="Select Resources">
2585: <nextstate>PRINT_FORMATTING</nextstate>
2586: <message><br /><big><i><b>Select resources for the assignment</b></i></big><br /></message>
2587: <resource variable="RESOURCES" multichoice="1" addstatus="1"
2588: closeallpages="1">
2589: <filterfunc>return $isNotMap;</filterfunc>
2590: <mapurl>$map</mapurl>
2591: <valuefunc>return $symbFilter;</valuefunc>
2592: $start_new_option
2593: </resource>
2594: </state>
2595: <state name="PRINT_FORMATTING" title="Format of the print job">
2596: <nextstate>NUMBER_PER_PDF</nextstate>
2597: <message><br /><big><i><b>How should the results be printed?</b></i></big><br /></message>
2598: <choices variable="EMPTY_PAGES">
2599: <choice computer='0'>Start each student\'s assignment on a new page/column (add a pagefeed after each assignment)</choice>
2600: <choice computer='1'>Add one empty page/column after each student\'s assignment</choice>
2601: <choice computer='2'>Add two empty pages/column after each student\'s assignment</choice>
2602: <choice computer='3'>Add three empty pages/column after each student\'s assignment</choice>
2603: </choices>
2604: <nextstate>PAGESIZE</nextstate>
2605: <message><hr width='33%' /><b>How do you want assignments split into PDF files? </b></message>
2606: <choices variable="SPLIT_PDFS">
2607: <choice computer="all">All assignments in a single PDF file</choice>
2608: <choice computer="sections">Each PDF contains exactly one section</choice>
2609: <choice computer="oneper">Each PDF contains exactly one assignment</choice>
2610: <choice computer="usenumber" relatedvalue="NUMBER_TO_PRINT">
2611: Specify the number of assignments per PDF:</choice>
2612: </choices>
2613: </state>
2614: RESOURCE_SELECTOR
2615:
2616: &Apache::lonxml::xmlparse($r, 'helper', <<CHOOSE_STUDENTS1);
2617: <state name="CHOOSE_STUDENTS1" title="Select Students and Resources">
2618: <choices variable='student_sort'>
2619: <choice computer='0'>Sort by section then student</choice>
2620: <choice computer='1'>Sort by students across sections.</choice>
2621: </choices>
2622: <message><br /><hr /><br /></message>
2623: <student multichoice='1' variable="STUDENTS" nextstate="SELECT_RESOURCES" coursepersonnel="1" />
2624:
2625: </state>
2626: $resource_selector
2627: CHOOSE_STUDENTS1
2628:
2629: &Apache::lonxml::xmlparse($r, 'helper', <<CHOOSE_ANON2);
2630: <state name="CHOOSE_ANON2" title="Select CODEd Assignments">
2631: <nextstate>SELECT_RESOURCES</nextstate>
2632: <message><h4>Fill out one of the forms below</h4></message>
2633: <message><br /><hr /> <br /></message>
2634: <message><h3>Generate new CODEd Assignments</h3></message>
2635: <message><table><tr><td><b>Number of CODEd assignments to print:</b></td><td></message>
2636: <string variable="NUMBER_TO_PRINT_TOTAL" maxlength="5" size="5">
2637: <validator>
2638: if (((\$helper->{'VARS'}{'NUMBER_TO_PRINT_TOTAL'}+0) < 1) &&
2639: !\$helper->{'VARS'}{'REUSE_OLD_CODES'} &&
2640: !\$helper->{'VARS'}{'SINGLE_CODE'} &&
2641: !\$helper->{'VARS'}{'CODE_SELECTED_FROM_LIST'}) {
2642: return "You need to specify the number of assignments to print";
2643: }
2644: return undef;
2645: </validator>
2646: </string>
2647: <message></td></tr><tr><td></message>
2648: <message><b>Names to store the CODEs under for later:</b></message>
2649: <message></td><td></message>
2650: <string variable="ANON_CODE_STORAGE_NAME" maxlength="50" size="20" />
2651: <message></td></tr><tr><td></message>
2652: <message><b>Bubble sheet type:</b></message>
2653: <message></td><td></message>
2654: <dropdown variable="CODE_OPTION" multichoice="0" allowempty="0">
2655: $codechoice
2656: </dropdown>
2657: <message></td></tr><tr><td></table></message>
2658: <message><br /><hr /><h3>Print a Specific CODE </h3><br /><table></message>
2659: <message><tr><td><b>Enter a CODE to print:</b></td><td></message>
2660: <string variable="SINGLE_CODE" size="10">
2661: <validator>
2662: if(!\$helper->{'VARS'}{'NUMBER_TO_PRINT_TOTAL'} &&
2663: !\$helper->{'VARS'}{'REUSE_OLD_CODES'} &&
2664: !\$helper->{'VARS'}{'CODE_SELECTED_FROM_LIST'}) {
2665: return &Apache::lonprintout::is_code_valid(\$helper->{'VARS'}{'SINGLE_CODE'},
2666: \$helper->{'VARS'}{'CODE_OPTION'});
2667: } else {
2668: return undef; # Other forces control us.
2669: }
2670: </validator>
2671: </string>
2672: <message></td></tr><tr><td></message>
2673: $code_selection
2674: <message></td></tr></table></message>
2675: <message><hr /><h3>Reprint a Set of Saved CODEs</h3><table><tr><td></message>
2676: <message><b>Select saved CODEs:</b></message>
2677: <message></td><td></message>
2678: <dropdown variable="REUSE_OLD_CODES">
2679: $namechoice
2680: </dropdown>
2681: <message></td></tr></table></message>
2682: </state>
2683: $resource_selector
2684: CHOOSE_ANON2
2685: }
2686:
2687: # FIXME: That RE should come from a library somewhere.
2688: if (($perm{'pav'}
2689: && $subdir ne $Apache::lonnet::perlvar{'lonDocRoot'}.'/res/'
2690: && (defined($helper->{'VARS'}->{'construction'})
2691: ||
2692: (&Apache::lonnet::allowed('bre',$subdir) eq 'F'
2693: &&
2694: $helper->{VARS}->{'postdata'}=~/\.(problem|exam|quiz|assess|survey|form|library|page|xml|html|htm|xhtml|xhtm)/)
2695: ))
2696: && $helper->{VARS}->{'assignment'} eq ""
2697: ) {
2698:
2699: my $pretty_dir = &Apache::lonnet::hreflocation($subdir);
2700: push @{$printChoices}, [&mt("Selected <b>Problems</b> from current subdirectory <b><i>[_1]</i></b>",$pretty_dir), 'problems_from_directory', 'CHOOSE_FROM_SUBDIR'];
2701: my $xmlfrag = <<CHOOSE_FROM_SUBDIR;
2702: <state name="CHOOSE_FROM_SUBDIR" title="Select File(s) from <b><small>$pretty_dir</small></b> to print">
2703:
2704: <files variable="FILES" multichoice='1'>
2705: <nextstate>PAGESIZE</nextstate>
2706: <filechoice>return '$subdir';</filechoice>
2707: CHOOSE_FROM_SUBDIR
2708:
2709: # this is broken up because I really want interpolation above,
2710: # and I really DON'T want it below
2711: $xmlfrag .= <<'CHOOSE_FROM_SUBDIR';
2712: <filefilter>return Apache::lonhelper::files::not_old_version($filename) &&
2713: $filename =~ m/\.(problem|exam|quiz|assess|survey|form|library)$/;
2714: </filefilter>
2715: </files>
2716: </state>
2717: CHOOSE_FROM_SUBDIR
2718: &Apache::lonxml::xmlparse($r, 'helper', $xmlfrag);
2719: }
2720:
2721: # Allow the user to select any sequence in the course, feed it to
2722: # another resource selector for that sequence
2723: if (!$helper->{VARS}->{'construction'} && !$is_published) {
2724: push @$printChoices, ["Selected <b>Resources</b> from <b>selected folder</b> in course",
2725: 'select_sequences', 'CHOOSE_SEQUENCE'];
2726: my $escapedSequenceName = $helper->{VARS}->{'SEQUENCE'};
2727: #Escape apostrophes and backslashes for Perl
2728: $escapedSequenceName =~ s/\\/\\\\/g;
2729: $escapedSequenceName =~ s/'/\\'/g;
2730: &Apache::lonxml::xmlparse($r, 'helper', <<CHOOSE_FROM_ANY_SEQUENCE);
2731: <state name="CHOOSE_SEQUENCE" title="Select Sequence To Print From">
2732: <message>Select the sequence to print resources from:</message>
2733: <resource variable="SEQUENCE">
2734: <nextstate>CHOOSE_FROM_ANY_SEQUENCE</nextstate>
2735: <filterfunc>return \$res->is_sequence;</filterfunc>
2736: <valuefunc>return $urlValue;</valuefunc>
2737: <choicefunc>return \$res->hasResource(\$res,sub { return !\$_[0]->is_sequence() },0,0);
2738: </choicefunc>
2739: </resource>
2740: </state>
2741: <state name="CHOOSE_FROM_ANY_SEQUENCE" title="Select Resources To Print">
2742: <message>(mark desired resources then click "next" button) <br /></message>
2743: <resource variable="RESOURCES" multichoice="1" toponly='1' addstatus="1"
2744: closeallpages="1">
2745: <nextstate>PAGESIZE</nextstate>
2746: <filterfunc>return $isNotMap</filterfunc>
2747: <mapurl evaluate='1'>return '$escapedSequenceName';</mapurl>
2748: <valuefunc>return $symbFilter;</valuefunc>
2749: $start_new_option
2750: </resource>
2751: </state>
2752: CHOOSE_FROM_ANY_SEQUENCE
2753: }
2754:
2755: # Generate the first state, to select which resources get printed.
2756: Apache::lonhelper::state->new("START", "Select Printing Options:");
2757: $paramHash = Apache::lonhelper::getParamHash();
2758: $paramHash->{MESSAGE_TEXT} = "";
2759: Apache::lonhelper::message->new();
2760: $paramHash = Apache::lonhelper::getParamHash();
2761: $paramHash->{'variable'} = 'PRINT_TYPE';
2762: $paramHash->{CHOICES} = $printChoices;
2763: Apache::lonhelper::choices->new();
2764:
2765: my $startedTable = 0; # have we started an HTML table yet? (need
2766: # to close it later)
2767:
2768: if (($perm{'pav'} and &Apache::lonnet::allowed('vgr',$env{'request.course.id'})) or
2769: ($helper->{VARS}->{'construction'} eq '1')) {
2770: addMessage("<hr width='33%' /><table><tr><td align='right'>Print: </td><td>");
2771: $paramHash = Apache::lonhelper::getParamHash();
2772: $paramHash->{'variable'} = 'ANSWER_TYPE';
2773: $helper->declareVar('ANSWER_TYPE');
2774: $paramHash->{CHOICES} = [
2775: ['Without Answers', 'yes'],
2776: ['With Answers', 'no'],
2777: ['Only Answers', 'only']
2778: ];
2779: Apache::lonhelper::dropdown->new();
2780: addMessage("</td></tr>");
2781: $startedTable = 1;
2782: }
2783:
2784: if ($perm{'pav'}) {
2785: if (!$startedTable) {
2786: addMessage("<hr width='33%' /><table><tr><td align='right'>LaTeX mode: </td><td>");
2787: $startedTable = 1;
2788: } else {
2789: addMessage("<tr><td align='right'>LaTeX mode: </td><td>");
2790: }
2791: $paramHash = Apache::lonhelper::getParamHash();
2792: $paramHash->{'variable'} = 'LATEX_TYPE';
2793: $helper->declareVar('LATEX_TYPE');
2794: if ($helper->{VARS}->{'construction'} eq '1') {
2795: $paramHash->{CHOICES} = [
2796: ['standard LaTeX mode', 'standard'],
2797: ['LaTeX batchmode', 'batchmode'], ];
2798: } else {
2799: $paramHash->{CHOICES} = [
2800: ['LaTeX batchmode', 'batchmode'],
2801: ['standard LaTeX mode', 'standard'] ];
2802: }
2803: Apache::lonhelper::dropdown->new();
2804:
2805: addMessage("</td></tr><tr><td align='right'>Print Table of Contents: </td><td>");
2806: $paramHash = Apache::lonhelper::getParamHash();
2807: $paramHash->{'variable'} = 'TABLE_CONTENTS';
2808: $helper->declareVar('TABLE_CONTENTS');
2809: $paramHash->{CHOICES} = [
2810: ['No', 'no'],
2811: ['Yes', 'yes'] ];
2812: Apache::lonhelper::dropdown->new();
2813: addMessage("</td></tr>");
2814:
2815: if (not $helper->{VARS}->{'construction'}) {
2816: addMessage("<tr><td align='right'>Print Index: </td><td>");
2817: $paramHash = Apache::lonhelper::getParamHash();
2818: $paramHash->{'variable'} = 'TABLE_INDEX';
2819: $helper->declareVar('TABLE_INDEX');
2820: $paramHash->{CHOICES} = [
2821: ['No', 'no'],
2822: ['Yes', 'yes'] ];
2823: Apache::lonhelper::dropdown->new();
2824: addMessage("</td></tr>");
2825: addMessage("<tr><td align='right'>Print Discussions: </td><td>");
2826: $paramHash = Apache::lonhelper::getParamHash();
2827: $paramHash->{'variable'} = 'PRINT_DISCUSSIONS';
2828: $helper->declareVar('PRINT_DISCUSSIONS');
2829: $paramHash->{CHOICES} = [
2830: ['No', 'no'],
2831: ['Yes', 'yes'] ];
2832: Apache::lonhelper::dropdown->new();
2833: addMessage("</td></tr>");
2834:
2835: addMessage("<tr><td align = 'right'> </td><td>");
2836: $paramHash = Apache::lonhelper::getParamHash();
2837: $paramHash->{'multichoice'} = "true";
2838: $paramHash->{'allowempty'} = "true";
2839: $paramHash->{'variable'} = "showallfoils";
2840: $paramHash->{'CHOICES'} = [ ["Show all foils", "1"] ];
2841: Apache::lonhelper::choices->new();
2842: addMessage("</td></tr>");
2843: }
2844:
2845: if ($helper->{'VARS'}->{'construction'}) {
2846: my $stylevalue=$env{'construct.style'};
2847: my $xmlfrag .= <<"RNDSEED";
2848: <message><tr><td align='right'>Use random seed: </td><td></message>
2849: <string variable="curseed" size="15" maxlength="15">
2850: <defaultvalue>
2851: return $helper->{VARS}->{'curseed'};
2852: </defaultvalue>
2853: </string>
2854: <message></td></tr><tr><td align="right">Use style file:</td><td></message>
2855: <message><input type="text" size="40" name="style_file_value" value="$stylevalue" /> <a href="javascript:openbrowser('helpform','style_file_value','sty')">Select style file</a> </td><tr><td></message>
2856: <choices allowempty="1" multichoice="true" variable="showallfoils">
2857: <choice computer="1">Show all foils?</choice>
2858: </choices>
2859: <message></td></tr></message>
2860: RNDSEED
2861: &Apache::lonxml::xmlparse($r, 'helper', $xmlfrag);
2862: $helper->{'VARS'}->{'style_file'}=$env{'form.style_file_value'};
2863:
2864: }
2865: }
2866:
2867:
2868:
2869:
2870: if ($startedTable) {
2871: addMessage("</table>");
2872: }
2873:
2874: Apache::lonprintout::page_format_state->new("FORMAT");
2875:
2876: # Generate the PAGESIZE state which will offer the user the margin
2877: # choices if they select one column
2878: Apache::lonhelper::state->new("PAGESIZE", "Set Margins");
2879: Apache::lonprintout::page_size_state->new('pagesize', 'FORMAT', 'FINAL');
2880:
2881:
2882: $helper->process();
2883:
2884:
2885: # MANUAL BAILOUT CONDITION:
2886: # If we're in the "final" state, bailout and return to handler
2887: if ($helper->{STATE} eq 'FINAL') {
2888: return $helper;
2889: }
2890:
2891: $r->print($helper->display());
2892: if ($helper->{STATE} eq 'START') {
2893: &recently_generated($r);
2894: }
2895: &Apache::lonhelper::unregisterHelperTags();
2896:
2897: return OK;
2898: }
2899:
2900:
2901: 1;
2902:
2903: package Apache::lonprintout::page_format_state;
2904:
2905: =pod
2906:
2907: =head1 Helper element: page_format_state
2908:
2909: See lonhelper.pm documentation for discussion of the helper framework.
2910:
2911: Apache::lonprintout::page_format_state is an element that gives the
2912: user an opportunity to select the page layout they wish to print
2913: with: Number of columns, portrait/landscape, and paper size. If you
2914: want to change the paper size choices, change the @paperSize array
2915: contents in this package.
2916:
2917: page_format_state is always directly invoked in lonprintout.pm, so there
2918: is no tag interface. You actually pass parameters to the constructor.
2919:
2920: =over 4
2921:
2922: =item * B<new>(varName): varName is where the print information will be stored in the format FIXME.
2923:
2924: =back
2925:
2926: =cut
2927:
2928: use Apache::lonhelper;
2929:
2930: no strict;
2931: @ISA = ("Apache::lonhelper::element");
2932: use strict;
2933: use Apache::lonlocal;
2934: use Apache::lonnet;
2935:
2936: my $maxColumns = 2;
2937: # it'd be nice if these all worked
2938: #my @paperSize = ("letter [8 1/2x11 in]", "legal [8 1/2x14 in]",
2939: # "tabloid (ledger) [11x17 in]", "executive [7 1/2x10 in]",
2940: # "a2 [420x594 mm]", "a3 [297x420 mm]", "a4 [210x297 mm]",
2941: # "a5 [148x210 mm]", "a6 [105x148 mm]" );
2942: my @paperSize = ("letter [8 1/2x11 in]", "legal [8 1/2x14 in]",
2943: "a4 [210x297 mm]");
2944:
2945: # Tentative format: Orientation (L = Landscape, P = portrait) | Colnum |
2946: # Paper type
2947:
2948: sub new {
2949: my $self = Apache::lonhelper::element->new();
2950:
2951: shift;
2952:
2953: $self->{'variable'} = shift;
2954: my $helper = Apache::lonhelper::getHelper();
2955: $helper->declareVar($self->{'variable'});
2956: bless($self);
2957: return $self;
2958: }
2959:
2960: sub render {
2961: my $self = shift;
2962: my $helper = Apache::lonhelper::getHelper();
2963: my $result = '';
2964: my $var = $self->{'variable'};
2965: my $PageLayout=&mt('Page layout');
2966: my $NumberOfColumns=&mt('Number of columns');
2967: my $PaperType=&mt('Paper type');
2968: $result .= <<STATEHTML;
2969:
2970: <hr width="33%" />
2971: <table cellpadding="3">
2972: <tr>
2973: <td align="center"><b>$PageLayout</b></td>
2974: <td align="center"><b>$NumberOfColumns</b></td>
2975: <td align="center"><b>$PaperType</b></td>
2976: </tr>
2977: <tr>
2978: <td>
2979: <label><input type="radio" name="${var}.layout" value="L" /> Landscape </label><br />
2980: <label><input type="radio" name="${var}.layout" value="P" checked='1' /> Portrait </label>
2981: </td>
2982: <td align="center">
2983: <select name="${var}.cols">
2984: STATEHTML
2985:
2986: my $i;
2987: for ($i = 1; $i <= $maxColumns; $i++) {
2988: if ($i == 2) {
2989: $result .= "<option value='$i' selected>$i</option>\n";
2990: } else {
2991: $result .= "<option value='$i'>$i</option>\n";
2992: }
2993: }
2994:
2995: $result .= "</select></td><td>\n";
2996: $result .= "<select name='${var}.paper'>\n";
2997:
2998: my %parmhash=&Apache::lonnet::coursedescription($env{'request.course.id'});
2999: my $DefaultPaperSize=lc($parmhash{'default_paper_size'});
3000: $DefaultPaperSize=~s/\s//g;
3001: if ($DefaultPaperSize eq '') {$DefaultPaperSize='letter';}
3002: $i = 0;
3003: foreach (@paperSize) {
3004: $_=~/(\w+)/;
3005: my $papersize=$1;
3006: if ($paperSize[$i]=~/$DefaultPaperSize/) {
3007: $result .= "<option selected value='$papersize'>" . $paperSize[$i] . "</option>\n";
3008: } else {
3009: $result .= "<option value='$papersize'>" . $paperSize[$i] . "</option>\n";
3010: }
3011: $i++;
3012: }
3013: $result .= "</select></td></tr></table>";
3014: return $result;
3015: }
3016:
3017: sub postprocess {
3018: my $self = shift;
3019:
3020: my $var = $self->{'variable'};
3021: my $helper = Apache::lonhelper->getHelper();
3022: $helper->{VARS}->{$var} =
3023: $env{"form.$var.layout"} . '|' . $env{"form.$var.cols"} . '|' .
3024: $env{"form.$var.paper"};
3025: return 1;
3026: }
3027:
3028: 1;
3029:
3030: package Apache::lonprintout::page_size_state;
3031:
3032: =pod
3033:
3034: =head1 Helper element: page_size_state
3035:
3036: See lonhelper.pm documentation for discussion of the helper framework.
3037:
3038: Apache::lonprintout::page_size_state is an element that gives the
3039: user the opportunity to further refine the page settings if they
3040: select a single-column page.
3041:
3042: page_size_state is always directly invoked in lonprintout.pm, so there
3043: is no tag interface. You actually pass parameters to the constructor.
3044:
3045: =over 4
3046:
3047: =item * B<new>(varName): varName is where the print information will be stored in the format FIXME.
3048:
3049: =back
3050:
3051: =cut
3052:
3053: use Apache::lonhelper;
3054: use Apache::lonnet;
3055: no strict;
3056: @ISA = ("Apache::lonhelper::element");
3057: use strict;
3058:
3059:
3060:
3061: sub new {
3062: my $self = Apache::lonhelper::element->new();
3063:
3064: shift; # disturbs me (probably prevents subclassing) but works (drops
3065: # package descriptor)... - Jeremy
3066:
3067: $self->{'variable'} = shift;
3068: my $helper = Apache::lonhelper::getHelper();
3069: $helper->declareVar($self->{'variable'});
3070:
3071: # The variable name of the format element, so we can look into
3072: # $helper->{VARS} to figure out whether the columns are one or two
3073: $self->{'formatvar'} = shift;
3074:
3075:
3076: $self->{NEXTSTATE} = shift;
3077: bless($self);
3078:
3079: return $self;
3080: }
3081:
3082: sub render {
3083: my $self = shift;
3084: my $helper = Apache::lonhelper::getHelper();
3085: my $result = '';
3086: my $var = $self->{'variable'};
3087:
3088:
3089:
3090: if (defined $self->{ERROR_MSG}) {
3091: $result .= '<br /><span class="LC_error">' . $self->{ERROR_MSG} . '</span><br />';
3092: }
3093:
3094: my $format = $helper->{VARS}->{$self->{'formatvar'}};
3095:
3096: # Use format to get sensible defaults for the margins:
3097:
3098:
3099: my ($laystyle, $cols, $papersize) = split(/\|/, $format);
3100: ($papersize) = split(/ /, $papersize);
3101:
3102:
3103: if ($laystyle eq 'L') {
3104: $laystyle = 'album';
3105: } else {
3106: $laystyle = 'book';
3107: }
3108:
3109:
3110: my %size;
3111: ($size{'width_and_units'},
3112: $size{'height_and_units'},
3113: $size{'margin_and_units'})=
3114: &Apache::lonprintout::page_format($papersize, $laystyle, $cols);
3115:
3116: foreach my $dimension ('width','height','margin') {
3117: ($size{$dimension},$size{$dimension.'_unit'}) =
3118: split(/ +/, $size{$dimension.'_and_units'},2);
3119:
3120: foreach my $unit ('cm','in') {
3121: $size{$dimension.'_options'} .= '<option ';
3122: if ($size{$dimension.'_unit'} eq $unit) {
3123: $size{$dimension.'_options'} .= 'selected="selected" ';
3124: }
3125: $size{$dimension.'_options'} .= '>'.$unit.'</option>';
3126: }
3127: }
3128:
3129: # Adjust margin for LaTeX margin: .. requires units == cm or in.
3130:
3131: if ($size{'margin_unit'} eq 'in') {
3132: $size{'margin'} += 1;
3133: } else {
3134: $size{'margin'} += 2.54;
3135: }
3136: $result .= <<ELEMENTHTML;
3137:
3138:
3139:
3140: <p>How should each column be formatted?</p>
3141:
3142: <table cellpadding='3'>
3143: <tr>
3144: <td align='right'><b>Width</b>:</td>
3145: <td align='left'><input type='text' name='$var.width' value="$size{'width'}" size='4' /></td>
3146: <td align='left'>
3147: <select name='$var.widthunit'>
3148: $size{'width_options'}
3149: </select>
3150: </td>
3151: </tr>
3152: <tr>
3153: <td align='right'><b>Height</b>:</td>
3154: <td align='left'><input type='text' name="$var.height" value="$size{'height'}" size='4' /></td>
3155: <td align='left'>
3156: <select name='$var.heightunit'>
3157: $size{'height_options'}
3158: </select>
3159: </td>
3160: </tr>
3161: <tr>
3162: <td align='right'><b>Left margin</b>:</td>
3163: <td align='left'><input type='text' name='$var.lmargin' value="$size{'margin'}" size='4' /></td>
3164: <td align='left'>
3165: <select name='$var.lmarginunit'>
3166: $size{'margin_options'}
3167: </select>
3168: </td>
3169: </tr>
3170: </table>
3171:
3172: <!--<p>Hint: Some instructors like to leave scratch space for the student by
3173: making the width much smaller than the width of the page.</p>-->
3174:
3175: ELEMENTHTML
3176:
3177: return $result;
3178: }
3179:
3180:
3181: sub preprocess {
3182: my $self = shift;
3183: my $helper = Apache::lonhelper::getHelper();
3184:
3185: my $format = $helper->{VARS}->{$self->{'formatvar'}};
3186:
3187: # If the user does not have 'pav' privilege, set default widths and
3188: # on to the next state right away.
3189: #
3190: if (!$perm{'pav'}) {
3191: my $var = $self->{'variable'};
3192: my $format = $helper->{VARS}->{$self->{'formatvar'}};
3193:
3194: my ($laystyle, $cols, $papersize) = split(/\|/, $format);
3195: ($papersize) = split(/ /, $papersize);
3196:
3197:
3198: if ($laystyle eq 'L') {
3199: $laystyle = 'album';
3200: } else {
3201: $laystyle = 'book';
3202: }
3203: # Figure out some good defaults for the print out and set them:
3204:
3205: my %size;
3206: ($size{'width'},
3207: $size{'height'},
3208: $size{'lmargin'})=
3209: &Apache::lonprintout::page_format($papersize, $laystyle, $cols);
3210:
3211: foreach my $dim ('width', 'height', 'lmargin') {
3212: my ($value, $units) = split(/ /, $size{$dim});
3213:
3214: $helper->{VARS}->{"$var.".$dim} = $value;
3215: $helper->{VARS}->{"$var.".$dim.'unit'} = $units;
3216:
3217: }
3218:
3219:
3220: # Transition to the next state
3221:
3222: $helper->changeState($self->{NEXTSTATE});
3223: }
3224:
3225: return 1;
3226: }
3227:
3228: sub postprocess {
3229: my $self = shift;
3230:
3231: my $var = $self->{'variable'};
3232: my $helper = Apache::lonhelper->getHelper();
3233: my $width = $helper->{VARS}->{$var .'.width'} = $env{"form.${var}.width"};
3234: my $height = $helper->{VARS}->{$var .'.height'} = $env{"form.${var}.height"};
3235: my $lmargin = $helper->{VARS}->{$var .'.lmargin'} = $env{"form.${var}.lmargin"};
3236: $helper->{VARS}->{$var .'.widthunit'} = $env{"form.${var}.widthunit"};
3237: $helper->{VARS}->{$var .'.heightunit'} = $env{"form.${var}.heightunit"};
3238: $helper->{VARS}->{$var .'.lmarginunit'} = $env{"form.${var}.lmarginunit"};
3239:
3240: my $error = '';
3241:
3242: # /^-?[0-9]+(\.[0-9]*)?$/ -> optional minus, at least on digit, followed
3243: # by an optional period, followed by digits, ending the string
3244:
3245: if ($width !~ /^-?[0-9]*(\.[0-9]*)?$/) {
3246: $error .= "Invalid width; please type only a number.<br />\n";
3247: }
3248: if ($height !~ /^-?[0-9]*(\.[0-9]*)?$/) {
3249: $error .= "Invalid height; please type only a number.<br />\n";
3250: }
3251: if ($lmargin !~ /^-?[0-9]*(\.[0-9]*)?$/) {
3252: $error .= "Invalid left margin; please type only a number.<br />\n";
3253: } else {
3254: # Adjust for LaTeX 1.0 inch margin:
3255:
3256: if ($env{"form.${var}.lmarginunit"} eq "in") {
3257: $helper->{VARS}->{$var.'.lmargin'} = $lmargin - 1;
3258: } else {
3259: $helper->{VARS}->{$var.'.lmargin'} = $lmargin - 2.54;
3260: }
3261: }
3262:
3263: if (!$error) {
3264: Apache::lonhelper::getHelper()->changeState($self->{NEXTSTATE});
3265: return 1;
3266: } else {
3267: $self->{ERROR_MSG} = $error;
3268: return 0;
3269: }
3270: }
3271:
3272:
3273:
3274: __END__
3275:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>