weekly release 2.2dev
[moodle.git] / admin / tool / unittest / other / pdflibtestpage.php
CommitLineData
0a39261b 1<?php
0a39261b 2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Pruduces a sample PDF using lib/pdflib.php
19 *
b412f9c4
PS
20 * @package tool
21 * @subpackage unittest
0a39261b 22 * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
b412f9c4
PS
23 * @author N.D.Freear@open.ac.uk, T.J.Hunt@open.ac.uk
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
0a39261b 25 */
26
b412f9c4 27require(dirname(__FILE__) . '/../../../../config.php');
0a39261b 28require_once($CFG->libdir . '/pdflib.php');
29
30require_login();
31$context = get_context_instance(CONTEXT_SYSTEM);
b412f9c4 32require_capability('tool/unittest:execute', $context);
0a39261b 33
34$getpdf = optional_param('getpdf', 0, PARAM_INT);
65b6adb1 35$fontfamily = optional_param('fontfamily', PDF_DEFAULT_FONT, PARAM_ALPHA); // to be configurable
0a39261b 36
37/**
38 * Extend the standard PDF class to get access to some protected values we want to display
39 * at the test page.
40 *
41 * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 */
44class testable_pdf extends pdf {
45 public function returnFontsList() {
46 return $this->fontlist;
47 }
48 public function _getfontpath() {
49 return parent::_getfontpath();
50 }
51}
52
53if ($getpdf) {
54 $doc = new testable_pdf();
55
56 $doc->SetTitle('Moodle PDF library test');
57 $doc->SetAuthor('Moodle ' . $CFG->release);
b412f9c4 58 $doc->SetCreator('admin/tool/unittest/pdflibtestpage.php');
0a39261b 59 $doc->SetKeywords('Moodle, PDF');
60 $doc->SetSubject('This has been generated by Moodle as its PDF library test page');
61 $doc->SetMargins(15, 30);
62
63 $doc->setPrintHeader(true);
64 $doc->setHeaderMargin(10);
65 $doc->setHeaderFont(array($fontfamily, 'b', 10));
66 $doc->setHeaderData('pix/moodlelogo-med-white.gif', 40, $SITE->fullname, $CFG->wwwroot);
67
68 $doc->setPrintFooter(true);
69 $doc->setFooterMargin(10);
70 $doc->setFooterFont(array($fontfamily, '', 8));
71
72 $doc->AddPage();
73
74 $doc->SetTextColor(255,255,255);
75 $doc->SetFillColor(255,203,68);
76 $doc->SetFont($fontfamily, 'B', 24);
77 $doc->Cell(0, 0, 'Moodle PDF library test', 0, 1, 'C', 1);
78
79 $doc->SetFont($fontfamily, '', 12);
80 $doc->Ln(6);
81 $doc->SetTextColor(0,0,0);
82
83 $c = '<h3>General information</h3>';
84 $c .= 'Moodle release: ' . $CFG->release . '<br />';
85 $c .= 'PDF producer: ' . PDF_PRODUCER . '<br />';
86 $c .= 'Font of this test page: ' . $fontfamily . '<br />';
87
88 $c .= '<h3>Current settings</h3>';
89 $c .= '<table border="1" cellspacing="0" cellpadding="1">';
90 foreach (array('K_PATH_MAIN', 'K_PATH_URL', 'K_PATH_FONTS', 'K_PATH_CACHE', 'K_PATH_IMAGES', 'K_BLANK_IMAGE',
91 'K_CELL_HEIGHT_RATIO', 'K_SMALL_RATIO', 'PDF_CUSTOM_FONT_PATH', 'PDF_DEFAULT_FONT') as $setting) {
92 if (defined($setting)) {
93 $c .= '<tr style="font-size: x-small;"><td>' . $setting . '</td><td>' . constant($setting) . '</td></tr>';
94 }
95 }
96 $c .= '<tr style="font-size: x-small;"><td>Effective font path</td><td>' . $doc->_getfontpath() . '</td></tr>';
97 $c .= '</table><br />';
98
99 $c .= '<h3>Available font files</h3>';
100 $fontfiles = $doc->returnFontsList();
101 sort($fontfiles);
102 $c .= implode(', ', $fontfiles);
103 $c .= '<br />';
104
105 $c .= '<h3>Installed languages and their alphabets</h3>';
106 $languages = array();
107 $langdirs = get_list_of_plugins('lang', '', $CFG->dataroot);
108 array_unshift($langdirs, 'Moodle core English');
109 foreach ($langdirs as $langdir) {
110 if ('Moodle core English' == $langdir) {
111 $langconfig = $CFG->dirroot . '/lang/en_utf8/langconfig.php';
112 } else {
113 $langconfig = $CFG->dataroot . '/lang/' . $langdir . '/langconfig.php';
114 }
115 if (is_readable($langconfig)) {
116 include($langconfig);
117 if (is_array($string)) {
118 $languages[$langdir] = new stdClass();
119 $languages[$langdir]->langname = isset($string['thislanguage']) ? $string['thislanguage'] : '(unknown)';
120 $languages[$langdir]->alphabet = isset($string['alphabet']) ? $string['alphabet'] : '(no alphabet defined)';
121 }
122 }
123 }
124 $c .= '<dl>';
125 foreach ($languages as $langcode => $language) {
126 $c .= '<dt>' . $language->langname . ' (' . $langcode . ')</dt>';
127 $c .= '<dd>' . $language->alphabet . '</dd>';
128 }
129 $c .= '</dl>';
130
131 $doc->writeHTML($c);
132
133 $doc->Output('pdflibtestpage.pdf');
134 exit();
135}
136
b412f9c4 137$PAGE->set_url('/admin/tool/unittest/other/pdflibtestpage.php');
0a39261b 138$PAGE->set_context($context);
139$PAGE->set_title('PDF library test');
140$PAGE->set_heading('PDF library test');
141
142echo $OUTPUT->header();
143echo $OUTPUT->heading('Press the button to generate test PDF', 2);
eb788065 144echo $OUTPUT->continue_button(new moodle_url($PAGE->url, array('getpdf' => 1)));
0a39261b 145echo $OUTPUT->footer();