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    /**
45     * Write the start of the output
46     */
47    public function start_output() {
48        echo "<!DOCTYPE html><html><head>";
49        echo \html_writer::empty_tag('meta', ['charset' => 'UTF-8']);
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>
77</head>
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>";
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     * Method to define whether the dataformat supports export of HTML
97     *
98     * @return bool
99     */
100    public function supports_html(): bool {
101        return true;
102    }
103
104    /**
105     * Write a single record
106     *
107     * @param array $record
108     * @param int $rownum
109     */
110    public function write_record($record, $rownum) {
111        $record = $this->format_record($record);
112
113        echo \html_writer::start_tag('tr');
114        foreach ($record as $cell) {
115            echo \html_writer::tag('td', $cell);
116        }
117        echo \html_writer::end_tag('tr');
118    }
119
120    /**
121     * Write the end of the sheet containing the data.
122     *
123     * @param array $columns
124     */
125    public function close_sheet($columns) {
126        echo "</table>";
127    }
128
129    /**
130     * Write the end of the sheet containing the data.
131     */
132    public function close_output() {
133        echo "</body></html>";
134    }
135}
136