MDL-56629 dataformat_html: Adding UTF-8 charset
[moodle.git] / dataformat / html / classes / writer.php
CommitLineData
bff1edbe
BH
1<?php
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 * html data format writer
19 *
20 * @package dataformat_html
21 * @copyright 2016 Brendan Heywood (brendan@catalyst-au.net)
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace dataformat_html;
26
27defined('MOODLE_INTERNAL') || die();
28
29/**
30 * html data format writer
31 *
32 * @package dataformat_html
33 * @copyright 2016 Brendan Heywood (brendan@catalyst-au.net)
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36class writer extends \core\dataformat\base {
37
38 /** @var $mimetype */
39 public $mimetype = "text/html";
40
41 /** @var $extension */
42 public $extension = ".html";
43
44 /**
eb26be5b 45 * Write the start of the output
bff1edbe 46 */
eb26be5b 47 public function start_output() {
bdf31b09
EL
48 echo "<!DOCTYPE html><html><head>";
49 echo \html_writer::empty_tag('meta', ['charset' => 'UTF-8']);
bff1edbe
BH
50 echo \html_writer::tag('title', $this->filename);
51 echo "<style>
52html, body {
53 margin: 0;
54 padding: 0;
55 font-family: sans-serif;
56 font-size: 13px;
57 background: #eee;
58}
59th {
60 border: solid 1px #999;
61 background: #eee;
62}
63td {
64 border: solid 1px #999;
65 background: #fff;
66}
67tr:hover td {
68 background: #eef;
69}
70table {
71 border-collapse: collapse;
72 border-spacing: 0pt;
73 width: 80%;
74 margin: auto;
75}
76</style>
bdf31b09 77</head>
eb26be5b
MN
78<body>";
79 }
80
81 /**
82 * Write the start of the sheet we will be adding data to.
83 *
84 * @param array $columns
85 */
86 public function start_sheet($columns) {
87 echo "<table border=1 cellspacing=0 cellpadding=3>";
bff1edbe
BH
88 echo \html_writer::start_tag('tr');
89 foreach ($columns as $k => $v) {
90 echo \html_writer::tag('th', $v);
91 }
92 echo \html_writer::end_tag('tr');
93 }
94
95 /**
96 * Write a single record
97 *
98 * @param array $record
99 * @param int $rownum
100 */
101 public function write_record($record, $rownum) {
102 echo \html_writer::start_tag('tr');
103 foreach ($record as $cell) {
104 echo \html_writer::tag('td', $cell);
105 }
106 echo \html_writer::end_tag('tr');
107 }
108
109 /**
eb26be5b 110 * Write the end of the sheet containing the data.
bff1edbe
BH
111 *
112 * @param array $columns
113 */
eb26be5b
MN
114 public function close_sheet($columns) {
115 echo "</table>";
bff1edbe
BH
116 }
117
eb26be5b
MN
118 /**
119 * Write the end of the sheet containing the data.
120 */
121 public function close_output() {
122 echo "</body></html>";
123 }
bff1edbe 124}