forked from open-lms-open-source/moodle-local_mr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.php
642 lines (580 loc) · 22.6 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
<?php
/**
* Open LMS framework
*
* This program 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.
*
* This program 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 this program. If not, see http://opensource.org/licenses/gpl-3.0.html.
*
* @copyright Copyright (c) 2009 Open LMS (https://www.openlms.net)
* @license http://opensource.org/licenses/gpl-3.0.html GNU Public License
* @package mr
* @author Mark Nielsen
*/
defined('MOODLE_INTERNAL') or die('Direct access to this script is forbidden.');
/**
* @see mr_html_tag
*/
require_once($CFG->dirroot.'/local/mr/framework/html/tag.php');
/**
* MR Renderer
*
* Default renderer for the framework.
*
* @package mr
* @author Mark Nielsen
*/
class local_mr_renderer extends plugin_renderer_base {
/**
* Returns rendered widget.
*
* Add another error catching layer for
* rendering reports.
*
* @param renderable $widget instance with renderable interface
* @return string
*/
public function render(renderable $widget) {
try {
return parent::render($widget);
} catch (coding_exception $e) {
if ($widget instanceof mr_report_abstract) {
return $this->render_mr_report_abstract($widget);
}
// Re-throw original error
throw $e;
}
}
/**
* Renders mr_html_notify
*
* @param mr_html_notify $notify mr_html_notify instance
* @return string
*/
protected function render_mr_html_notify(mr_html_notify $notify) {
$output = '';
foreach($notify->get_messages() as $message) {
$output .= $this->output->notification($message[0], $message[1]);
}
return $output;
}
/**
* Renders mr_html_tabs
*
* @param mr_html_tabs $tabs mr_html_tabs instance
* @return string
*/
protected function render_mr_html_tabs(mr_html_tabs $tabs) {
$rows = $tabs->get_rows();
$output = '';
if (!empty($rows)) {
$inactive = $active = array();
if (count($rows) == 2 and !empty($tabs->subtab) and !empty($rows[1][$tabs->subtab])) {
$active[] = $tabs->toptab;
$currenttab = $tabs->subtab;
} else {
$currenttab = $tabs->toptab;
}
$output = html_writer::tag('div', print_tabs($rows, $currenttab, $inactive, $active, true), array('class' => 'mr_html_tabs'));
}
return $output;
}
/**
* Render mr_html_heading
*
* @param mr_html_heading $heading mr_html_heading instance
* @return string
*/
protected function render_mr_html_heading(mr_html_heading $heading) {
// Do we have anything to render?
if (empty($heading->text)) {
return '';
}
$icon = '';
if (!empty($heading->icon)) {
$icon = $this->output->pix_icon($heading->icon, $heading->iconalt, $heading->component, array('class'=>'icon'));
}
$help = '';
if (!empty($heading->helpidentifier)) {
$help = $this->output->help_icon($heading->helpidentifier, $heading->component);
}
return html_writer::tag('div', $this->output->heading($icon.$heading->text.$help, $heading->level, $heading->classes, $heading->id), array('class' => 'mr_html_heading'));
}
/**
* Render mr_html_filter
*
* @param mr_html_filter $filter mr_html_filter instance
* @return string
*/
protected function render_mr_html_filter(mr_html_filter $filter) {
// Only render the filter form if one of the filters is not hidden
foreach ($filter->get_filters() as $mrfilter) {
if (!$mrfilter instanceof mr_html_filter_hidden) {
return html_writer::tag('div', $filter->init()->get_helper()->buffer(array($filter->get_mform(), 'display')), array('class' => 'mr_html_filter'));
}
}
return '';
}
/**
* Render mr_html_paging
*
* @param mr_html_paging $paging mr_html_paging instance
* @return string
*/
public function render_mr_html_paging(mr_html_paging $paging) {
$output = '';
if ($paging->get_perpage()) {
$output = $this->output->paging_bar($paging->get_total(), $paging->get_page(), $paging->get_perpage(), $paging->get_url(), $paging->REQUEST_PAGE);
}
if ($paging->get_perpageopts()) {
$options = array();
foreach ($paging->get_perpageopts() as $opt) {
if ($opt == 'all') {
$options[10000] = get_string('all');
} else {
$options[$opt] = $opt;
}
}
$singleselect = new single_select($paging->get_url(), $paging->REQUEST_PERPAGE, $options, $paging->get_perpage(), array());
$singleselect->set_label(get_string('rowsperpage', 'local_mr'), array('class' => 'accesshide'));
$select = $this->output->render($singleselect);
// Attempt to place it within the paging bar's div
if (substr($output, strlen($output)-6) == '</div>') {
$output = substr($output, 0, -6)."$select</div>";
} else {
$output .= $this->output->box($select, 'paging');
}
$output = html_writer::tag('div', $output, array('class' => 'mr_html_paging'));
}
return $output;
}
/**
* Render mr_html_table
*
* @param mr_html_table $table mr_html_table instance
* @return string
*/
protected function render_mr_html_table(mr_html_table $table) {
$tag = new mr_html_tag();
$rows = $table->get_rows();
$columns = $table->get_columns(true);
// Table setup
$htmltable = new html_table();
$htmltable->data = array();
foreach ($table->get_attributes() as $name => $value) {
if (property_exists($htmltable, $name)) {
$htmltable->$name = $value;
} else {
$htmltable->attributes[$name] = $value;
}
}
// Check if we have any column headings
$haveheadings = false;
foreach ($columns as $column) {
if ($column->has_heading()) {
$haveheadings = true;
break;
}
}
if ($haveheadings) {
$htmltable->head = array();
foreach ($columns as $column) {
// Must set sortable to false if table is not sort enabled or if empty $rows
if (!$table->get_sortenabled() or empty($rows)) {
$column->set_config('sortable', false);
}
$config = $column->get_config();
// Figure out column sort controls
if ($config->sortable) {
$icon = '';
$sortstr = get_string('asc');
$sortord = SORT_ASC;
if ($table->get_sort() == $column->get_name()) {
if ($table->get_order() == SORT_ASC) {
$icon = $tag->img()->src($this->output->image_url('t/down'))->alt(get_string('asc'));
$sortstr = get_string('asc');
$sortord = SORT_DESC;
} else {
$icon = $tag->img()->src($this->output->image_url('t/up'))->alt(get_string('desc'));
}
}
$url = $table->get_url()->out(false, array('tsort' => $config->name, 'torder' => $sortord));
$heading = get_string('sortby').' '.$config->heading.' '.$sortstr;
$heading = $config->heading.get_accesshide($heading);
$heading = $tag->a($heading)->href($url).$icon;
} else {
$heading = $config->heading;
}
$cell = new html_table_cell($heading);
$cell->attributes = array_merge($cell->attributes, $config->attributes);
$htmltable->head[] = $cell;
}
}
if (empty($rows)) {
$cell = new html_table_cell($table->get_emptymessage());
$cell->colspan = count($htmltable->head ?? array());
$htmltable->data[] = new html_table_row(array($cell));
} else {
$htmltable->data = $this->convert_to_htmlrows($table);
}
return html_writer::tag('div', html_writer::table($htmltable), array('class' => 'mr_html_table'));
}
/**
* Render mr_file_export
*
* @param mr_file_export $export mr_file_export instance
* @return string
*/
protected function render_mr_file_export(mr_file_export $export) {
$select = new url_select($export->get_url_select_options(), '');
$select->set_label(get_string('export', 'local_mr'));
return html_writer::tag('div', $this->output->render($select), array('class' => 'mr_file_export'));
}
/**
* Render mr_report_abstract
*
* @param mr_report_abstract $report mr_report_abstract instance
* @return string
* @todo Render in heading with help button?
*/
public function render_mr_report_abstract(mr_report_abstract $report) {
// Send JSON if necessary
$this->mr_html_table_json(
$report->get_table(),
$report->get_paging()
);
// Wrapper DIV
$output = $this->output->box_start('boxwidthwide boxaligncenter mr_report');
// Heading
// $output .= $this->output->heading($report->name());
// Render report SQL
$output .= $this->help_render_mr_report_sql($report);
// Render description
if ($description = $report->get_description()) {
$output .= $this->output->box($description, 'generalbox boxwidthnormal boxaligncenter mr_report_description');
}
// Render Hook
$output .= $this->help_render_navigation_display($report);
// Render filter
if ($report->get_filter() instanceof mr_html_filter) {
$output .= $this->render($report->get_filter());
}
// Render hook
$output .= $this->help_render_chart_select($report);
// Render as AJAX or our default rendering
if ($report->get_config()->ajax and $report->get_preferences()->get('forceajax', $report->get_config()->ajaxdefault)) {
$output .= $report->output_wrapper(
$this->mr_html_table_ajax(
$report->get_table(),
$report->get_paging()
)
);
} else {
// Render paging top
$output .= $this->render($report->get_paging());
// Render table and allow report to wrap it with w/e
$output .= $report->output_wrapper(
$this->render($report->get_table())
);
// Render paging bottom
$output .= $this->render($report->get_paging());
}
// Render export
if ($report->get_export() instanceof mr_file_export) {
$output .= $this->render($report->get_export());
}
// Render AJAX toggle link
if ($report->get_config()->ajax) {
if ($report->get_preferences()->get('forceajax', $report->get_config()->ajaxdefault)) {
$newajax = 0;
$label = get_string('basichtml', 'local_mr');
} else {
$newajax = 1;
$label = get_string('standard', 'local_mr');
}
$url = clone($report->get_url());
$url->param('forceajax', $newajax);
$link = html_writer::link($url, $label, array('title' => $label));
$output .= html_writer::tag('div', $link, array('class' => 'mr_ajax_table_forceajax'));
}
// Close wrapper DIV
$output .= $this->output->box_end();
return $output;
}
/**
* Generate YUI view of table and paging
*
* @param mr_html_table $table Table instance
* @param mr_html_paging $paging Paging instance
* @param bool $autoload
* @param null $id
* @return string
*/
public function mr_html_table_ajax(mr_html_table $table, mr_html_paging $paging, $autoload = true, $id = null) {
global $PAGE;
// Columns
$columns = array();
foreach ($table->get_columns(true) as $column) {
// Must set sortable to false if table is not sort enabled or if empty $rows
if (!$table->get_sortenabled()) {
$column->set_config('sortable', false);
}
$col = (object) array(
'key' => $column->get_name(),
'label' => $column->get_config()->heading,
'sortable' => $column->get_config()->sortable,
);
if ($column->get_config()->editor) {
$col->editor = $column->get_config()->editor;
}
$columns[] = $col;
}
// Page size
$opts = array();
if ($paging->get_perpageopts()) {
foreach ($paging->get_perpageopts() as $opt) {
if ($opt == 'all') {
$opts[] = (object) array('value' => 10000, 'text' => get_string('all'));
} else {
$opts[] = (object) array('value' => $opt, 'text' => $opt);
}
}
}
// Place holder div's ID
if (empty($id)) {
$id = html_writer::random_id();
}
$loadingmsg = $this->output->pix_icon('i/ajaxloader', get_string('loadingdotdotdot', 'local_mr')).
' '.get_string('loadingdotdotdot', 'local_mr');
$module = array(
'name' => 'local_mr',
'fullpath' => '/local/mr/renderer.js',
'requires' => array(
'yui2-yahoo',
'yui2-dom',
'yui2-event',
'yui2-element',
'yui2-paginator',
'yui2-datasource',
'yui2-json',
'yui2-connection',
'yui2-get',
'yui2-dragdrop',
'yui2-datatable',
'moodle-local_mr-livelog',
),
'strings' => array(
array('tablesortedbydesc', 'local_mr'),
array('tablesortedbyasc', 'local_mr'),
)
);
// Change the URL to send PLD AJAX requests with `tjson: 1`.
$url = $table->get_url();
if ($url->get_path() === '/local/pld/view.php') {
$url = new moodle_url('/local/pld/ajax.php', array('controller' => $url->get_param('controller'), 'courseid' => $url->get_param('courseid')));
}
$url = $url->out(false, array('tjson' => 1));
$arguments = (object) array(
'id' => $id,
'url' => $url,
'sort' => $table->get_sort(),
'order' => $table->get_order(),
'page' => $paging->get_page(),
'perpage' => $paging->get_perpage(),
'loadingmsg' => $loadingmsg,
'perpageopts' => $opts,
'columns' => $columns,
'asc' => SORT_ASC,
'desc' => SORT_DESC,
'autoload' => $autoload,
);
if (!is_null($table->get_summary())) {
$arguments->summary = $table->get_summary();
}
if (!empty($table->caption)) {
$arguments->caption = $table->caption;
}
$PAGE->requires->js_init_call('M.local_mr.init_mr_html_table', array($arguments), false, $module);
$PAGE->requires->strings_for_js(
array(
'paginatorfirstlabel',
'paginatorfirsttitle',
'paginatorlastlabel',
'paginatorlasttitle',
'paginatorprevlabel',
'paginatorprevtitle',
'paginatornextlabel',
'paginatornexttitle',
), 'local_mr');
return html_writer::tag('div', '', array('id' => $id, 'class' => 'mr_html_table mr_ajax_table'));
}
/**
* Generate table rows as JSON
*
* @param mr_html_table $table Table instance
* @param mr_html_paging $paging Paging instance
* @return void
* @todo Make this not a hack?
*/
public function mr_html_table_json(mr_html_table $table, mr_html_paging $paging) {
if (optional_param('tjson', 0, PARAM_BOOL)) {
$json = new stdClass;
$json->recordsReturned = (int) count($table->get_rows());
$json->totalRecords = (int) $paging->get_total();
$json->startIndex = (int) $paging->get_page();
$json->sort = $table->get_sort();
$json->pageSize = (int) $paging->get_perpage();
$json->records = array();
$json->emptyMessage = $table->get_emptymessage();
if ($table->get_order() == SORT_ASC) {
$json->dir = 'asc';
} else {
$json->dir = 'desc';
}
// If we are returning 0 records, we probably don't have any at all
if ($json->recordsReturned == 0) {
$json->totalRecords = 0;
}
$columns = $table->get_columns(true);
$htmlrows = $this->convert_to_htmlrows($table);
foreach ($htmlrows as $htmlrow) {
$position = 0;
foreach ($columns as $column) {
if (array_key_exists($position, $htmlrow->cells)) {
$value = $htmlrow->cells[$position]->text;
} else {
$value = '';
}
$record[$column->get_name()] = $value;
$position++;
}
$json->records[] = (object) $record;
}
echo json_encode($json);
die;
}
}
/**
* Hook for blocks/reports
*
* @param mr_report_abstract
* @return string
*/
protected function help_render_navigation_display(mr_report_abstract $report) {
return '';
}
/**
* Hook for blocks/reports
*
* @param mr_report_abstract
* @return string
*/
protected function help_render_chart_select(mr_report_abstract $report) {
return '';
}
/**
* Help render mr_report_abstract SQL
*
* @param mr_report_abstract $report mr_report_abstract instance
* @return string
*/
public function help_render_mr_report_sql(mr_report_abstract $report) {
global $CFG, $USER;
$output = '';
$executedsql = $report->get_executedsql();
$usernames = array('mrsupport', 'mrdev');
if (!empty($CFG->reportviewsql) and is_array($CFG->reportviewsql)) {
$usernames = array_merge($usernames, $CFG->reportviewsql);
}
if (in_array($USER->username, $usernames) and !empty($executedsql)) {
$sql = '';
foreach ($executedsql as $values) {
list($rawsql, $params) = $values;
$rawsql = trim($rawsql);
$sql .= s($rawsql)."\n\n";
if (!is_null($params)) {
$sql .= s(var_export($params, true))."\n\n\n";
}
}
$output = print_collapsible_region(
$this->output->box('<pre>'.trim($sql).'</pre>', ''),
'generalbox mr_report_sql',
'mr_report_sql_id',
get_string('reportsql', 'local_mr'),
'mr_report_sql_toggle',
false,
true
);
}
return $output;
}
/**
* Convert a mr_html_table into an array of html_table_row instances
*
* @param mr_html_table $table Instance
* @return array
*/
protected function convert_to_htmlrows(mr_html_table $table) {
$rows = $table->get_rows();
$columns = $table->get_columns(true);
$suppress = array();
$htmlrows = array();
foreach ($rows as $row) {
// Generate a html_table_row
if ($row instanceof html_table_row) {
$htmlrow = $row;
} else {
$htmlrow = new html_table_row();
foreach ($columns as $column) {
$cell = $column->get_cell($row);
if ($cell instanceof html_table_cell) {
$htmlrow->cells[] = $cell;
} else {
$cell = new html_table_cell($cell);
foreach ($column->get_config()->attributes as $name => $value) {
if (property_exists($cell, $name)) {
$cell->$name = $value;
} else {
$cell->attributes[$name] = $value;
}
}
$htmlrow->cells[] = $cell;
}
}
}
// Apply column suppression to the row
$position = -1;
foreach ($columns as $column) {
$position++;
if (!$column->get_config()->suppress or !array_key_exists($position, $htmlrow->cells)) {
continue;
}
$cell = $htmlrow->cells[$position];
if (isset($suppress[$position]) and $suppress[$position] == $cell->text) {
$htmlrow->cells[$position]->text = ''; // Suppressed
} else {
// If a cell changes, reset suppression for all cells after it (left to right)
if (isset($suppress[$position]) and $suppress[$position] != $cell->text) {
foreach ($suppress as $key => $value) {
if ($key > $position) {
unset($suppress[$key]);
}
}
}
$suppress[$position] = $cell->text;
}
}
$htmlrows[] = $htmlrow;
}
return $htmlrows;
}
}