MDL-32569 improve SimpleTest emulation
[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
685dc443 22 * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
b412f9c4 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
0a39261b 24 */
25
b412f9c4 26require(dirname(__FILE__) . '/../../../../config.php');
0a39261b 27require_once($CFG->libdir . '/pdflib.php');
28
29require_login();
30$context = get_context_instance(CONTEXT_SYSTEM);
b412f9c4 31require_capability('tool/unittest:execute', $context);
0a39261b 32
33$getpdf = optional_param('getpdf', 0, PARAM_INT);
65b6adb1 34$fontfamily = optional_param('fontfamily', PDF_DEFAULT_FONT, PARAM_ALPHA); // to be configurable
0a39261b 35
36/**
37 * Extend the standard PDF class to get access to some protected values we want to display
38 * at the test page.
39 *
40 * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 */
43class testable_pdf extends pdf {
44 public function returnFontsList() {
45 return $this->fontlist;
46 }
47 public function _getfontpath() {
48 return parent::_getfontpath();
49 }
50}
51
52if ($getpdf) {
53 $doc = new testable_pdf();
54
55 $doc->SetTitle('Moodle PDF library test');
56 $doc->SetAuthor('Moodle ' . $CFG->release);
b412f9c4 57 $doc->SetCreator('admin/tool/unittest/pdflibtestpage.php');
0a39261b 58 $doc->SetKeywords('Moodle, PDF');
59 $doc->SetSubject('This has been generated by Moodle as its PDF library test page');
60 $doc->SetMargins(15, 30);
61
62 $doc->setPrintHeader(true);
63 $doc->setHeaderMargin(10);
64 $doc->setHeaderFont(array($fontfamily, 'b', 10));
65 $doc->setHeaderData('pix/moodlelogo-med-white.gif', 40, $SITE->fullname, $CFG->wwwroot);
66
67 $doc->setPrintFooter(true);
68 $doc->setFooterMargin(10);
69 $doc->setFooterFont(array($fontfamily, '', 8));
70
71 $doc->AddPage();
72
73 $doc->SetTextColor(255,255,255);
74 $doc->SetFillColor(255,203,68);
75 $doc->SetFont($fontfamily, 'B', 24);
76 $doc->Cell(0, 0, 'Moodle PDF library test', 0, 1, 'C', 1);
77
78 $doc->SetFont($fontfamily, '', 12);
79 $doc->Ln(6);
80 $doc->SetTextColor(0,0,0);
81
82 $c = '<h3>General information</h3>';
83 $c .= 'Moodle release: ' . $CFG->release . '<br />';
d0a0db42 84 $c .= 'PDF producer: TCPDF ' . $doc->getTCPDFVersion() . ' (http://www.tcpdf.org) <br />';
0a39261b 85 $c .= 'Font of this test page: ' . $fontfamily . '<br />';
86
87 $c .= '<h3>Current settings</h3>';
88 $c .= '<table border="1" cellspacing="0" cellpadding="1">';
89 foreach (array('K_PATH_MAIN', 'K_PATH_URL', 'K_PATH_FONTS', 'K_PATH_CACHE', 'K_PATH_IMAGES', 'K_BLANK_IMAGE',
90 'K_CELL_HEIGHT_RATIO', 'K_SMALL_RATIO', 'PDF_CUSTOM_FONT_PATH', 'PDF_DEFAULT_FONT') as $setting) {
91 if (defined($setting)) {
92 $c .= '<tr style="font-size: x-small;"><td>' . $setting . '</td><td>' . constant($setting) . '</td></tr>';
93 }
94 }
95 $c .= '<tr style="font-size: x-small;"><td>Effective font path</td><td>' . $doc->_getfontpath() . '</td></tr>';
96 $c .= '</table><br />';
97
98 $c .= '<h3>Available font files</h3>';
99 $fontfiles = $doc->returnFontsList();
100 sort($fontfiles);
101 $c .= implode(', ', $fontfiles);
102 $c .= '<br />';
103
104 $c .= '<h3>Installed languages and their alphabets</h3>';
105 $languages = array();
106 $langdirs = get_list_of_plugins('lang', '', $CFG->dataroot);
d0a0db42 107 array_unshift($langdirs, 'en');
0a39261b 108 foreach ($langdirs as $langdir) {
d0a0db42
PS
109 if ('en' == $langdir) {
110 $langconfig = $CFG->dirroot . '/lang/en/langconfig.php';
0a39261b 111 } else {
112 $langconfig = $CFG->dataroot . '/lang/' . $langdir . '/langconfig.php';
113 }
114 if (is_readable($langconfig)) {
115 include($langconfig);
116 if (is_array($string)) {
117 $languages[$langdir] = new stdClass();
118 $languages[$langdir]->langname = isset($string['thislanguage']) ? $string['thislanguage'] : '(unknown)';
119 $languages[$langdir]->alphabet = isset($string['alphabet']) ? $string['alphabet'] : '(no alphabet defined)';
120 }
121 }
122 }
123 $c .= '<dl>';
124 foreach ($languages as $langcode => $language) {
125 $c .= '<dt>' . $language->langname . ' (' . $langcode . ')</dt>';
d0a0db42 126 $c .= '<dd>"' . $language->alphabet . '"</dd>';
0a39261b 127 }
128 $c .= '</dl>';
129
130 $doc->writeHTML($c);
131
132 $doc->Output('pdflibtestpage.pdf');
133 exit();
134}
135
b412f9c4 136$PAGE->set_url('/admin/tool/unittest/other/pdflibtestpage.php');
0a39261b 137$PAGE->set_context($context);
138$PAGE->set_title('PDF library test');
139$PAGE->set_heading('PDF library test');
140
141echo $OUTPUT->header();
142echo $OUTPUT->heading('Press the button to generate test PDF', 2);
eb788065 143echo $OUTPUT->continue_button(new moodle_url($PAGE->url, array('getpdf' => 1)));
0a39261b 144echo $OUTPUT->footer();