-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.php
251 lines (222 loc) · 10.7 KB
/
renderer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* exacomp block rendrer
*
* @package block_exastud
* @copyright 2014 gtn gmbh
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
class block_exastud_renderer extends plugin_renderer_base {
public function print_esr_table (html_table $table) {
// prepare table data and populate missing properties with reasonable defaults
if (!empty($table->align)) {
foreach ($table->align as $key => $aa) {
if ($aa) {
$table->align[$key] = 'text-align:'. fix_align_rtl($aa) .';'; // Fix for RTL languages
} else {
$table->align[$key] = null;
}
}
}
if (!empty($table->size)) {
foreach ($table->size as $key => $ss) {
if ($ss) {
$table->size[$key] = 'width:'. $ss .';';
} else {
$table->size[$key] = null;
}
}
}
if (!empty($table->wrap)) {
foreach ($table->wrap as $key => $ww) {
if ($ww) {
$table->wrap[$key] = 'white-space:nowrap;';
} else {
$table->wrap[$key] = '';
}
}
}
if (!empty($table->head)) {
foreach ($table->head as $key => $val) {
if (!isset($table->align[$key])) {
$table->align[$key] = null;
}
if (!isset($table->size[$key])) {
$table->size[$key] = null;
}
if (!isset($table->wrap[$key])) {
$table->wrap[$key] = null;
}
}
}
if (empty($table->attributes['class'])) {
$table->attributes['class'] = 'esr_table';
}
if (!empty($table->tablealign)) {
$table->attributes['class'] .= ' boxalign' . $table->tablealign;
}
// explicitly assigned properties override those defined via $table->attributes
$table->attributes['class'] = trim($table->attributes['class']);
$attributes = array_merge($table->attributes, array(
'id' => $table->id,
'width' => $table->width,
'summary' => $table->summary,
'cellpadding' => $table->cellpadding,
'cellspacing' => $table->cellspacing,
));
$output = html_writer::start_tag('table', $attributes) . "\n";
$countcols = 0;
if (!empty($table->head)) {
$countcols = count($table->head);
$output .= html_writer::start_tag('thead', array()) . "\n";
$output .= html_writer::start_tag('tr', array()) . "\n";
$keys = array_keys($table->head);
$lastkey = end($keys);
foreach ($table->head as $key => $heading) {
// Convert plain string headings into html_table_cell objects
if (!($heading instanceof html_table_cell)) {
$headingtext = $heading;
$heading = new html_table_cell();
$heading->text = $headingtext;
$heading->header = true;
}
if ($heading->header !== false) {
$heading->header = true;
}
if ($heading->header && empty($heading->scope)) {
$heading->scope = 'col';
}
if (isset($table->headspan[$key]) && $table->headspan[$key] > 1) {
$heading->colspan = $table->headspan[$key];
$countcols += $table->headspan[$key] - 1;
}
if ($key == $lastkey) {
$heading->attributes['class'] .= ' lastcol';
}
if (isset($table->colclasses[$key])) {
$heading->attributes['class'] .= ' ' . $table->colclasses[$key];
}
$heading->attributes['class'] = trim($heading->attributes['class']);
$attributes = array_merge($heading->attributes, array(
'style' => $table->align[$key] . $table->size[$key] . $heading->style,
'scope' => $heading->scope,
'colspan' => $heading->colspan,
));
$tagtype = 'td';
if ($heading->header === true) {
$tagtype = 'th';
}
$output .= html_writer::tag($tagtype, $heading->text, $attributes) . "\n";
}
$output .= html_writer::end_tag('tr') . "\n";
$output .= html_writer::end_tag('thead') . "\n";
if (empty($table->data)) {
// For valid XHTML strict every table must contain either a valid tr
// or a valid tbody... both of which must contain a valid td
$output .= html_writer::start_tag('tbody', array('class' => 'empty'));
$output .= html_writer::tag('tr', html_writer::tag('td', '', array('colspan'=>count($table->head))));
$output .= html_writer::end_tag('tbody');
}
}
if (!empty($table->data)) {
$oddeven = 1;
$keys = array_keys($table->data);
$lastrowkey = end($keys);
$output .= html_writer::start_tag('tbody', array());
foreach ($table->data as $key => $row) {
if (($row === 'hr') && ($countcols)) {
$output .= html_writer::tag('td', html_writer::tag('div', '', array('class' => 'tabledivider')), array('colspan' => $countcols));
} else {
// Convert array rows to html_table_rows and cell strings to html_table_cell objects
if (!($row instanceof html_table_row)) {
$newrow = new html_table_row();
foreach ($row as $cell) {
if (!($cell instanceof html_table_cell)) {
$cell = new html_table_cell($cell);
}
$newrow->cells[] = $cell;
}
$row = $newrow;
}
$oddeven = $oddeven ? 0 : 1;
if (isset($table->rowclasses[$key])) {
$row->attributes['class'] .= ' ' . $table->rowclasses[$key];
}
$row->attributes['class'] .= ' r' . $oddeven;
if ($key == $lastrowkey) {
$row->attributes['class'] .= ' lastrow';
}
$output .= html_writer::start_tag('tr', array('class' => trim($row->attributes['class']), 'style' => $row->style, 'id' => $row->id)) . "\n";
$keys2 = array_keys($row->cells);
$lastkey = end($keys2);
$gotlastkey = false; //flag for sanity checking
foreach ($row->cells as $key => $cell) {
if ($gotlastkey) {
//This should never happen. Why do we have a cell after the last cell?
mtrace("A cell with key ($key) was found after the last key ($lastkey)");
}
if (!($cell instanceof html_table_cell)) {
$mycell = new html_table_cell();
$mycell->text = $cell;
$cell = $mycell;
}
if (($cell->header === true) && empty($cell->scope)) {
$cell->scope = 'row';
}
if (isset($table->colclasses[$key])) {
$cell->attributes['class'] .= ' ' . $table->colclasses[$key];
}
$cell->attributes['class'] .= ' cell c' . $key;
if ($key == $lastkey) {
$cell->attributes['class'] .= ' lastcol';
$gotlastkey = true;
}
$tdstyle = '';
$tdstyle .= isset($table->align[$key]) ? $table->align[$key] : '';
$tdstyle .= isset($table->size[$key]) ? $table->size[$key] : '';
$tdstyle .= isset($table->wrap[$key]) ? $table->wrap[$key] : '';
$cell->attributes['class'] = trim($cell->attributes['class']);
$tdattributes = array_merge($cell->attributes, array(
'style' => $tdstyle . $cell->style,
'colspan' => $cell->colspan,
'rowspan' => $cell->rowspan,
'id' => $cell->id,
'abbr' => $cell->abbr,
'scope' => $cell->scope,
));
$tagtype = 'td';
if ($cell->header === true) {
$tagtype = 'th';
}
$output .= html_writer::tag($tagtype, $cell->text, $tdattributes) . "\n";
}
}
$output .= html_writer::end_tag('tr') . "\n";
}
$output .= html_writer::end_tag('tbody') . "\n";
}
$output .= html_writer::end_tag('table') . "\n";
return $output;
}
function print_subtitle($subtitle,$editlink = null) {
return html_writer::tag("p", $subtitle . (($editlink == null) ? "" : " " . html_writer::tag("a", html_writer::tag("img", '',array('src'=>'pix/edit.png')),array('href'=>$editlink,'class'=>'ers_inlineicon'))), array('class'=>'esr_subtitle'));
}
function print_edit_link($link) {
return html_writer::tag("a", html_writer::tag("img", '',array('src'=>'pix/edit.png')),array('href'=>$link,'class'=>'ers_inlineicon'));
}
}