Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * Configurable Reports
19
 * A Moodle block for creating Configurable Reports
20
 * @package blocks
21
 * @author: Juan leyva <http://www.twitter.com/jleyvadelgado>
22
 * @date: 2009
23
 */
24
 
25
require_once($CFG->dirroot.'/lib/evalmath/evalmath.class.php');
26
 
27
class report_base {
28
 
29
    public $id = 0;
30
    public $components = array();
31
    public $finalreport;
32
    public $totalrecords = 0;
33
    public $currentuser = 0;
34
    public $currentcourse = 0;
35
    public $starttime = 0;
36
    public $endtime = 0;
37
    public $sql = '';
38
    public $filterform = null;
39
 
40
    public function reports_base($report) {
41
        global $DB, $CFG, $USER, $remotedb;
42
 
43
        if (is_numeric($report)) {
44
            $this->config = $DB->get_record('block_configurable_reports', array('id' => $report));
45
        } else {
46
            $this->config = $report;
47
        }
48
 
49
        $this->currentuser = $USER;
50
        $this->currentcourseid = $this->config->courseid;
51
        $this->init();
52
 
53
        // Use a custom $DB (and not current system's $DB)
54
        // TODO: major security issue.
55
        $remotedbhost = get_config('block_configurable_reports', 'dbhost');
56
        $remotedbname = get_config('block_configurable_reports', 'dbname');
57
        $remotedbuser = get_config('block_configurable_reports', 'dbuser');
58
        $remotedbpass = get_config('block_configurable_reports', 'dbpass');
59
 
60
        if (!empty($remotedbhost) && !empty($remotedbname) && !empty($remotedbuser) && !empty($remotedbpass) && $this->config->remote) {
61
            $dbclass = get_class($DB);
62
            $remotedb = new $dbclass();
63
            $remotedb->connect($remotedbhost, $remotedbuser, $remotedbpass, $remotedbname, $CFG->prefix);
64
        } else {
65
            $remotedb = $DB;
66
        }
67
 
68
    }
69
 
70
    public function __construct($report) {
71
        $this->reports_base($report);
72
    }
73
 
74
    public function check_permissions($userid, $context) {
75
        global $DB, $CFG, $USER;
76
 
77
        if (has_capability('block/configurable_reports:manageownreports', $context, $userid) && $this->config->ownerid == $userid) {
78
            return true;
79
        }
80
 
81
        if (has_capability('block/configurable_reports:managereports', $context, $userid)) {
82
            return true;
83
        }
84
 
85
        if (empty($this->config->visible)) {
86
            return false;
87
        }
88
 
89
        $components = cr_unserialize($this->config->components);
90
        $permissions = (isset($components['permissions'])) ? $components['permissions'] : [];
91
 
92
        if (empty($permissions['elements'])) {
93
            return has_capability('block/configurable_reports:viewreports', $context);
94
        } else {
95
            $i = 1;
96
            $cond = array();
97
            foreach ($permissions['elements'] as $p) {
98
                require_once($CFG->dirroot.'/blocks/configurable_reports/plugin.class.php');
99
                require_once($CFG->dirroot.'/blocks/configurable_reports/components/permissions/'.$p['pluginname'].'/plugin.class.php');
100
                $classname = 'plugin_'.$p['pluginname'];
101
                $class = new $classname($this->config);
102
                $cond[$i] = $class->execute($userid, $context, $p['formdata']);
103
                $i++;
104
            }
105
            if (count($cond) == 1) {
106
                return $cond[1];
107
            } else {
108
                $m = new EvalMath;
109
                $orig = $dest = array();
110
 
111
                if (isset($permissions['config']) && isset($permissions['config']->conditionexpr)) {
112
                    $logic = trim($permissions['config']->conditionexpr);
113
                    // Security
114
                    // No more than: conditions * 10 chars.
115
                    $logic = substr($logic, 0, count($permissions['elements']) * 10);
116
                    $logic = str_replace(array('and', 'or'), array('&&', '||'), strtolower($logic));
117
                    // More Security Only allowed chars.
118
                    $logic = preg_replace('/[^&c\d\s|()]/i', '', $logic);
119
                    $logic = str_replace(array('&&', '||'), array('*', '+'), $logic);
120
 
121
                    for ($j = $i - 1; $j > 0; $j--) {
122
                        $orig[] = 'c'.$j;
123
                        $dest[] = ($cond[$j]) ? 1 : 0;
124
                    }
125
 
126
                    return $m->evaluate(str_replace($orig, $dest, $logic));
127
                } else {
128
                    return false;
129
                }
130
            }
131
        }
132
    }
133
 
134
    public function add_filter_elements(&$mform) {
135
        global $DB, $CFG;
136
 
137
        $components = cr_unserialize($this->config->components);
138
        $filters = (isset($components['filters']['elements'])) ? $components['filters']['elements'] : array();
139
 
140
        require_once($CFG->dirroot.'/blocks/configurable_reports/plugin.class.php');
141
        foreach ($filters as $f) {
142
            require_once($CFG->dirroot.'/blocks/configurable_reports/components/filters/'.$f['pluginname'].'/plugin.class.php');
143
            $classname = 'plugin_'.$f['pluginname'];
144
            $class = new $classname($this->config);
145
 
146
            $finalelements = $class->print_filter($mform, $f['formdata']);
147
 
148
        }
149
    }
150
 
151
 
152
    public function check_filters_request() {
153
        global $DB, $CFG;
154
 
155
        $components = cr_unserialize($this->config->components);
156
        $filters = (isset($components['filters']['elements'])) ? $components['filters']['elements'] : array();
157
 
158
        if (!empty($filters)) {
159
 
160
            $formdata = new stdclass;
161
            $request = array_merge($_POST, $_GET);
162
            if ($request) {
163
                foreach ($request as $key => $val) {
164
                    if (strpos($key, 'filter_') !== false) {
165
                        $key = clean_param($key, PARAM_CLEANHTML);
166
                        if (is_array($val)) {
167
                            $val = clean_param_array($val, PARAM_CLEANHTML);
168
                        } else {
169
                            $val = clean_param($val, PARAM_CLEANHTML);
170
                        }
171
                        $formdata->{$key} = $val;
172
                    }
173
                }
174
            }
175
 
176
            require_once('filter_form.php');
177
            $filterform = new report_edit_form(null, $this);
178
 
179
            $filterform->set_data($formdata);
180
 
181
            if ($filterform->is_cancelled()) {
182
                $params = ['id' => $this->config->id, 'courseid' => $this->config->courseid];
183
                redirect(new \moodle_url('/blocks/configurable_reports/viewreport.php', $params));
184
                die;
185
            }
186
            $this->filterform = $filterform;
187
        }
188
    }
189
 
190
    public function print_filters() {
191
        if (!is_null($this->filterform)) {
192
            $this->filterform->display();
193
        }
194
    }
195
 
196
    public function print_graphs($return = false) {
197
        $output = '';
198
        $graphs = $this->get_graphs($this->finalreport->table->data);
199
 
200
        if ($graphs) {
201
            foreach ($graphs as $g) {
202
                $output .= '<div class="centerpara">';
203
                $output .= ' <img src="'.$g.'" alt="'.$this->config->name.'"><br />';
204
                $output .= '</div>';
205
            }
206
        }
207
        if ($return) {
208
            return $output;
209
        }
210
 
211
        echo $output;
212
        return true;
213
    }
214
 
215
 
216
    public function print_export_options($return = false) {
217
        global $CFG;
218
 
219
        $wwwpath = $CFG->wwwroot;
220
        $request = array_merge($_POST, $_GET);
221
        if ($request) {
222
            $id = clean_param($request['id'], PARAM_INT);
223
            $wwwpath = 'viewreport.php?id='.$id;
224
            unset($request['id']);
225
            foreach ($request as $key => $val) {
226
                $key = clean_param($key, PARAM_CLEANHTML);
227
                if (is_array($val)) {
228
                    foreach ($val as $k => $v) {
229
                        $k = clean_param($k, PARAM_CLEANHTML);
230
                        $v = clean_param($v, PARAM_CLEANHTML);
231
                        $wwwpath .= "&amp;{$key}[$k]=".$v;
232
                    }
233
                } else {
234
                    $val = clean_param($val, PARAM_CLEANHTML);
235
                    $wwwpath .= "&amp;$key=".$val;
236
                }
237
            }
238
        }
239
 
240
        $output = '';
241
        $export = explode(',', $this->config->export);
242
        if (!empty($this->config->export)) {
243
            $output .= '<br /><div class="centerpara">';
244
            $output .= get_string('downloadreport', 'block_configurable_reports').': ';
245
            foreach ($export as $e) {
246
                if ($e) {
247
                    $output .= '<a href="'.$wwwpath.'&amp;download=1&amp;format='.$e.'"><img src="'.$CFG->wwwroot.'/blocks/configurable_reports/export/'.$e.'/pix.gif" alt="'.$e.'">&nbsp;'.(strtoupper($e)).'</a>&nbsp;';
248
                }
249
            }
250
            $output .= '</div>';
251
        }
252
 
253
        if ($return) {
254
            return $output;
255
        }
256
 
257
        echo $output;
258
        return true;
259
    }
260
 
261
    public function evaluate_conditions($data, $logic) {
262
        global $DB, $CFG;
263
 
264
        require_once($CFG->dirroot.'/blocks/configurable_reports/reports/evalwise.class.php');
265
 
266
        $logic = trim(strtolower($logic));
267
        $logic = substr($logic, 0, count($data) * 10);
268
        $logic = str_replace(array('or', 'and', 'not'), array('+', '*', '-'), $logic);
269
        $logic = preg_replace('/[^\*c\d\s\+\-()]/i', '', $logic);
270
 
271
        $orig = $dest = array();
272
        for ($j = count($data); $j > 0; $j--) {
273
            $orig[] = 'c'.$j;
274
            $dest[] = $j;
275
        }
276
        $logic = str_replace($orig, $dest, $logic);
277
 
278
        $m = new \EvalWise();
279
 
280
        $m->set_data($data);
281
        $result = $m->evaluate($logic);
282
        return $result;
283
    }
284
 
285
    public function get_graphs($finalreport) {
286
        global $DB, $CFG;
287
 
288
        $components = cr_unserialize($this->config->components);
289
        $graphs = (isset($components['plot']['elements'])) ? $components['plot']['elements'] : array();
290
 
291
        $reportgraphs = array();
292
 
293
        if (!empty($graphs)) {
294
            $series = array();
295
            foreach ($graphs as $g) {
296
                require_once($CFG->dirroot.'/blocks/configurable_reports/components/plot/'.$g['pluginname'].'/plugin.class.php');
297
                $classname = 'plugin_'.$g['pluginname'];
298
                $class = new $classname($this->config);
299
                $reportgraphs[] = $class->execute($g['id'], $g['formdata'], $finalreport);
300
            }
301
        }
302
        return $reportgraphs;
303
    }
304
 
305
    public function get_calcs($finaltable, $tablehead) {
306
        global $DB, $CFG;
307
 
308
        $components = cr_unserialize($this->config->components);
309
        $calcs = (isset($components['calcs']['elements'])) ? $components['calcs']['elements'] : array();
310
 
311
        // Calcs doesn't work with multi-rows so far.
312
        $columnscalcs = array();
313
        $finalcalcs = array();
314
        if (!empty($calcs)) {
315
            foreach ($calcs as $calc) {
316
                $columnscalcs[$calc['formdata']->column] = array();
317
            }
318
 
319
            $columnstostore = array_keys($columnscalcs);
320
 
321
            foreach ($finaltable as $r) {
322
                foreach ($columnstostore as $c) {
323
                    if (isset($r[$c])) {
324
                        $columnscalcs[$c][] = $r[$c];
325
                    }
326
                }
327
            }
328
 
329
            foreach ($calcs as $calc) {
330
                require_once($CFG->dirroot.'/blocks/configurable_reports/components/calcs/'.$calc['pluginname'].'/plugin.class.php');
331
                $classname = 'plugin_'.$calc['pluginname'];
332
                $class = new $classname($this->config);
333
                $result = $class->execute($columnscalcs[$calc['formdata']->column]);
334
                $finalcalcs[$calc['formdata']->column] = $result;
335
            }
336
 
337
            for ($i = 0; $i < count($tablehead); $i++) {
338
                if (!isset($finalcalcs[$i])) {
339
                    $finalcalcs[$i] = '';
340
                }
341
            }
342
 
343
            ksort($finalcalcs);
344
 
345
        }
346
        return $finalcalcs;
347
    }
348
 
349
    public function elements_by_conditions($conditions) {
350
        global $DB, $CFG;
351
 
352
        if (empty($conditions['elements'])) {
353
            $finalelements = $this->get_all_elements();
354
            return $finalelements;
355
        }
356
 
357
        $finalelements = array();
358
        $i = 1;
359
        foreach ($conditions['elements'] as $c) {
360
            require_once($CFG->dirroot.'/blocks/configurable_reports/components/conditions/'.$c['pluginname'].'/plugin.class.php');
361
            $classname = 'plugin_'.$c['pluginname'];
362
            $class = new $classname($this->config);
363
            $elements[$i] = $class->execute($c['formdata'], $this->currentuser, $this->currentcourseid);
364
            $i++;
365
        }
366
 
367
        if (count($conditions['elements']) == 1) {
368
            $finalelements = $elements[1];
369
        } else {
370
            $logic = $conditions['config']->conditionexpr;
371
            $finalelements = $this->evaluate_conditions($elements, $logic);
372
            if ($finalelements === false) {
373
                return false;
374
            }
375
        }
376
 
377
        return $finalelements;
378
    }
379
 
380
    /**
381
     * Returns a report object
382
     */
383
    public function create_report() {
384
        global $DB, $CFG;
385
 
386
        // Conditions.
387
        $components = cr_unserialize($this->config->components);
388
 
389
        $conditions = (isset($components['conditions']['elements'])) ? $components['conditions']['elements'] : array();
390
        $filters = (isset($components['filters']['elements'])) ? $components['filters']['elements'] : array();
391
        $columns = (isset($components['columns']['elements'])) ? $components['columns']['elements'] : array();
392
        $ordering = (isset($components['ordering']['elements'])) ? $components['ordering']['elements'] : array();
393
 
394
        $finalelements = array();
395
 
396
        if (!empty($conditions)) {
397
            $finalelements = $this->elements_by_conditions($components['conditions']);
398
        } else {
399
            // All elements.
400
            $finalelements = $this->get_all_elements();
401
        }
402
 
403
        // Filters.
404
 
405
        if (!empty($filters)) {
406
            foreach ($filters as $f) {
407
                require_once($CFG->dirroot.'/blocks/configurable_reports/components/filters/'.$f['pluginname'].'/plugin.class.php');
408
                $classname = 'plugin_'.$f['pluginname'];
409
                $class = new $classname($this->config);
410
                $finalelements = $class->execute($finalelements, $f['formdata']);
411
            }
412
        }
413
 
414
        // Ordering.
415
 
416
        $sqlorder = '';
417
 
418
        $orderingdata = array();
419
        if (!empty($ordering)) {
420
            foreach ($ordering as $o) {
421
                require_once($CFG->dirroot.'/blocks/configurable_reports/components/ordering/'.$o['pluginname'].'/plugin.class.php');
422
                $classname = 'plugin_'.$o['pluginname'];
423
                $classorder = new $classname($this->config);
424
                $orderingdata = $o['formdata'];
425
                if ($classorder->sql) {
426
                    $sqlorder = $classorder->execute($orderingdata);
427
                }
428
            }
429
        }
430
 
431
        // COLUMNS - FIELDS.
432
 
433
        $rows = $this->get_rows($finalelements, $sqlorder);
434
 
435
        if (!$sqlorder && isset($classorder)) {
436
            $rows = $classorder->execute($rows, $orderingdata);
437
        }
438
 
439
        $reporttable = array();
440
        $tablehead = array();
441
        $tablealign = array();
442
        $tablesize = array();
443
        $tablewrap = array();
444
        $firstrow = true;
445
 
446
        $pluginscache = array();
447
 
448
        if ($rows) {
449
            foreach ($rows as $r) {
450
                $tempcols = array();
451
                foreach ($columns as $c) {
452
                    if (empty($c)) {
453
                        continue;
454
                    }
455
                    require_once($CFG->dirroot.'/blocks/configurable_reports/components/columns/'.$c['pluginname'].'/plugin.class.php');
456
                    $classname = 'plugin_'.$c['pluginname'];
457
                    if (!isset($pluginscache[$classname])) {
458
                        $class = new $classname($this->config, $c);
459
                        $pluginscache[$classname] = $class;
460
                    } else {
461
                        $class = $pluginscache[$classname];
462
                    }
463
 
464
                    $tempcols[] = $class->execute($c['formdata'], $r, $this->currentuser, $this->currentcourseid, $this->starttime, $this->endtime);
465
                    if ($firstrow) {
466
                        $tablehead[] = $class->summary($c['formdata']);
467
                        list($align, $size, $wrap) = $class->colformat($c['formdata']);
468
                        $tablealign[] = $align;
469
                        $tablesize[] = $size;
470
                        $tablewrap[] = $wrap;
471
                    }
472
 
473
                }
474
                $firstrow = false;
475
                $reporttable[] = $tempcols;
476
            }
477
        }
478
 
479
        // EXPAND ROWS.
480
        $finaltable = array();
481
        $newcols = array();
482
 
483
        foreach ($reporttable as $row) {
484
            $col = array();
485
            $multiple = false;
486
            $nrows = 0;
487
            $mrowsi = array();
488
 
489
            foreach ($row as $key => $cell) {
490
                if (!is_array($cell)) {
491
                    $col[] = $cell;
492
                } else {
493
                    $multiple = true;
494
                    $nrows = count($cell);
495
                    $mrowsi[] = $key;
496
                }
497
            }
498
            if ($multiple) {
499
                $newrows = array();
500
                for ($i = 0; $i < $nrows; $i++) {
501
                    $newrows[$i] = $row;
502
                    foreach ($mrowsi as $index) {
503
                        $newrows[$i][$index] = $row[$index][$i];
504
                    }
505
                }
506
                foreach ($newrows as $r) {
507
                    $finaltable[] = $r;
508
                }
509
            } else {
510
                $finaltable[] = $col;
511
            }
512
        }
513
 
514
        // CALCS.
515
        $finalcalcs = $this->get_calcs($finaltable, $tablehead);
516
 
517
        // Make the table, head, columns, etc...
518
 
519
        $table = new \stdClass;
520
        $table->id = 'reporttable';
521
        $table->data = $finaltable;
522
        $table->head = $tablehead;
523
        $table->size = $tablesize;
524
        $table->align = $tablealign;
525
        $table->wrap = $tablewrap;
526
        $table->width = (isset($components['columns']['config'])) ? $components['columns']['config']->tablewidth : '';
527
        $table->summary = $this->config->summary;
528
        $table->tablealign = (isset($components['columns']['config'])) ? $components['columns']['config']->tablealign : 'center';
529
        $table->cellpadding = (isset($components['columns']['config'])) ? $components['columns']['config']->cellpadding : '5';
530
        $table->cellspacing = (isset($components['columns']['config'])) ? $components['columns']['config']->cellspacing : '1';
531
        $table->class = (isset($components['columns']['config'])) ? $components['columns']['config']->class : 'generaltable';
532
 
533
        $calcs = new \html_table();
534
        $calcs->data = array($finalcalcs);
535
        $calcs->head = $tablehead;
536
        $calcs->size = $tablesize;
537
        $calcs->align = $tablealign;
538
        $calcs->wrap = $tablewrap;
539
        $calcs->summary = $this->config->summary;
540
        $calcs->attributes['class'] = (isset($components['columns']['config'])) ? $components['columns']['config']->class : 'generaltable';
541
 
542
        if (!$this->finalreport) {
543
            $this->finalreport = new \stdClass;
544
        }
545
        $this->finalreport->table = $table;
546
        $this->finalreport->calcs = $calcs;
547
 
548
        return true;
549
 
550
    }
551
 
552
    public function add_jsordering(\moodle_page $moodle_page) {
553
        switch (get_config('block_configurable_reports', 'reporttableui')) {
554
            case 'datatables':
555
                cr_add_jsdatatables('#reporttable', $moodle_page);
556
                break;
557
            case 'jquery':
558
                cr_add_jsordering('#reporttable', $moodle_page);
559
                echo html_writer::tag('style',
560
                    '#page-blocks-configurable_reports-viewreport .generaltable {
561
                    overflow: auto;
562
                    width: 100%;
563
                    display: block;}');
564
                break;
565
            case 'html':
566
                echo html_writer::tag('style',
567
                    '#page-blocks-configurable_reports-viewreport .generaltable {
568
                    overflow: auto;
569
                    width: 100%;
570
                    display: block;}');
571
                break;
572
            default:
573
                break;
574
        }
575
 
576
    }
577
 
578
    public function print_template($config, \moodle_page $moodle_page) {
579
        global $DB, $CFG, $OUTPUT;
580
 
581
        $pagecontents = array();
582
        $pagecontents['header'] = (isset($config->header) && $config->header) ? $config->header : '';
583
        $pagecontents['footer'] = (isset($config->footer) && $config->footer) ? $config->footer : '';
584
 
585
        $recordtpl = (isset($config->record) && $config->record) ? $config->record : '';;
586
 
587
        $calculations = '';
588
 
589
        if (!empty($this->finalreport->calcs->data[0])) {
590
            $calculations = html_writer::table($this->finalreport->calcs);
591
        }
592
 
593
        $pagination = '';
594
        if ($this->config->pagination) {
595
            $page = optional_param('page', 0, PARAM_INT);
596
            $postfiltervars = '';
597
            $request = array_merge($_POST, $_GET);
598
            if ($request) {
599
                foreach ($request as $key => $val) {
600
                    if (strpos($key, 'filter_') !== false) {
601
                        $key = clean_param($key, PARAM_CLEANHTML);
602
                        if (is_array($val)) {
603
                            foreach ($val as $k => $v) {
604
                                $k = clean_param($k, PARAM_CLEANHTML);
605
                                $v = clean_param($v, PARAM_CLEANHTML);
606
                                $postfiltervars .= "&amp;{$key}[$k]=".$v;
607
                            }
608
                        } else {
609
                            $val = clean_param($val, PARAM_CLEANHTML);
610
                            $postfiltervars .= "&amp;$key=".$val;
611
                        }
612
                    }
613
                }
614
            }
615
 
616
            $this->totalrecords = count($this->finalreport->table->data);
617
            $pagingbar = new \paging_bar($this->totalrecords, $page, $this->config->pagination, "viewreport.php?id=".$this->config->id."&courseid=".$this->config->courseid."$postfiltervars&amp;");
618
            $pagingbar->pagevar = 'page';
619
            $pagination = $OUTPUT->render($pagingbar);
620
        }
621
 
622
        $search = [
623
            '##reportname##',
624
            '##reportsummary##',
625
            '##graphs##',
626
            '##exportoptions##',
627
            '##calculationstable##',
628
            '##pagination##'
629
        ];
630
        $replace = [
631
            format_string($this->config->name),
632
            format_text($this->config->summary),
633
            $this->print_graphs(true),
634
            $this->print_export_options(true),
635
            $calculations,
636
            $pagination
637
        ];
638
 
639
        foreach ($pagecontents as $key => $p) {
640
            if ($p) {
641
                $pagecontents[$key] = str_ireplace($search, $replace, $p);
642
            }
643
        }
644
 
645
        if ($this->config->jsordering) {
646
            $this->add_jsordering($moodle_page);
647
        }
648
        $this->print_filters();
649
 
650
        echo "<div id=\"printablediv\">\n";
651
        // Print the header.
652
        if (is_array($pagecontents['header'])) {
653
            echo format_text($pagecontents['header']['text'], $pagecontents['header']['format']);
654
        } else {
655
            echo format_text($pagecontents['header'], FORMAT_HTML);
656
        }
657
 
658
        $a = new \stdClass();
659
        $a->totalrecords = $this->totalrecords;
660
        echo \html_writer::tag('div', get_string('totalrecords', 'block_configurable_reports', $a), array('id' => 'totalrecords'));
661
 
662
        if ($recordtpl) {
663
            if ($this->config->pagination) {
664
                $page = optional_param('page', 0, PARAM_INT);
665
                $this->totalrecords = count($this->finalreport->table->data);
666
                $this->finalreport->table->data = array_slice($this->finalreport->table->data, $page * $this->config->pagination, $this->config->pagination);
667
            }
668
 
669
            foreach ($this->finalreport->table->data as $r) {
670
                if (is_array($recordtpl)) {
671
                    $recordtext = $recordtpl['text'];
672
                } else {
673
                    $recordtext = $recordtpl;
674
                }
675
 
676
                foreach ($this->finalreport->table->head as $key => $c) {
677
                    $recordtext = str_ireplace("[[$c]]", $r[$key], $recordtext);
678
                }
679
                echo format_text($recordtext, FORMAT_HTML);
680
            }
681
        }
682
 
683
        // Print the footer.
684
        if (is_array($pagecontents['footer'])) {
685
            echo format_text($pagecontents['footer']['text'], $pagecontents['footer']['format']);
686
        } else {
687
            echo format_text($pagecontents['footer'], FORMAT_HTML);
688
        }
689
 
690
        echo "</div>\n";
691
        echo '<div class="centerpara"><br />';
692
        echo $OUTPUT->pix_icon('print', get_string('printreport', 'block_configurable_reports'), 'block_configurable_reports');
693
        echo "&nbsp;<a href=\"javascript: printDiv('printablediv')\">".get_string('printreport', 'block_configurable_reports')."</a>";
694
        echo "</div>\n";
695
    }
696
 
697
    public function print_report_page(\moodle_page $moodlepage) {
698
        global $DB, $CFG, $OUTPUT, $USER;
699
 
700
        cr_print_js_function();
701
        $components = cr_unserialize($this->config->components);
702
 
703
        $template = (isset($components['template']['config']) && $components['template']['config']->enabled && $components['template']['config']->record) ? $components['template']['config'] : false;
704
 
705
        if ($template) {
706
            $this->print_template($template, $moodlepage);
707
            return true;
708
        }
709
 
710
        // Debug.
711
        $debug = optional_param('debug', false, PARAM_BOOL);
712
        if ($debug or !empty($this->config->debug)) {
713
            echo \html_writer::empty_tag('hr');
714
            echo \html_writer::tag('div', $this->sql, ['id' => 'debug', 'style' => 'direction:ltr;text-align:left;']);
715
            echo \html_writer::empty_tag('hr');
716
        }
717
 
718
        echo '<div class="centerpara">';
719
        echo format_text($this->config->summary);
720
        echo '</div>';
721
 
722
        $this->print_filters();
723
        if ($this->finalreport->table && !empty($this->finalreport->table->data[0])) {
724
 
725
            echo "<div id=\"printablediv\">\n";
726
            $this->print_graphs();
727
 
728
            if ($this->config->jsordering) {
729
                $this->add_jsordering($moodlepage);
730
            }
731
 
732
            $this->totalrecords = count($this->finalreport->table->data);
733
            if ($this->config->pagination) {
734
                $page = optional_param('page', 0, PARAM_INT);
735
                $this->totalrecords = count($this->finalreport->table->data);
736
                $this->finalreport->table->data = array_slice($this->finalreport->table->data, $page * $this->config->pagination, $this->config->pagination);
737
            }
738
 
739
            cr_print_table($this->finalreport->table);
740
 
741
            if ($this->config->pagination) {
742
                $postfiltervars = '';
743
                $request = array_merge($_POST, $_GET);
744
                if ($request) {
745
                    foreach ($request as $key => $val) {
746
                        if (strpos($key, 'filter_') !== false) {
747
                            $key = clean_param($key, PARAM_CLEANHTML);
748
                            if (is_array($val)) {
749
                                foreach ($val as $k => $v) {
750
                                    $k = clean_param($k, PARAM_CLEANHTML);
751
                                    $v = clean_param($v, PARAM_CLEANHTML);
752
                                    $postfiltervars .= "&amp;{$key}[$k]=".$v;
753
                                }
754
                            } else {
755
                                $val = clean_param($val, PARAM_CLEANHTML);
756
                                $postfiltervars .= "&amp;$key=".$val;
757
                            }
758
                        }
759
                    }
760
                }
761
 
762
                $pagingbar = new paging_bar($this->totalrecords, $page, $this->config->pagination, "viewreport.php?id=".$this->config->id."&courseid=".$this->config->courseid."$postfiltervars&amp;");
763
                $pagingbar->pagevar = 'page';
764
                echo $OUTPUT->render($pagingbar);
765
            }
766
 
767
            // Report statistics.
768
            $a = new \stdClass();
769
            $a->totalrecords = $this->totalrecords;
770
            echo \html_writer::tag('div', get_string('totalrecords', 'block_configurable_reports', $a), ['id' => 'totalrecords']);
771
 
772
            echo \html_writer::tag('div', get_string('lastexecutiontime', 'block_configurable_reports', $this->config->lastexecutiontime / 1000), array('id' => 'lastexecutiontime'));
773
 
774
            if (!empty($this->finalreport->calcs->data[0])) {
775
                echo '<br /><br /><br /><div class="centerpara"><b>'.get_string('columncalculations', 'block_configurable_reports').'</b></div><br />';
776
                echo html_writer::table($this->finalreport->calcs);
777
            }
778
            echo "</div>";
779
 
780
            $this->print_export_options();
781
        } else {
782
            echo '<div class="centerpara">'.get_string('norecordsfound', 'block_configurable_reports').'</div>';
783
        }
784
 
785
        echo '<div class="centerpara"><br />';
786
        echo $OUTPUT->pix_icon('print', get_string('printreport', 'block_configurable_reports'), 'block_configurable_reports');
787
        echo "&nbsp;<a href=\"javascript: printDiv('printablediv')\">".get_string('printreport', 'block_configurable_reports')."</a>";
788
        echo "</div>\n";
789
    }
790
 
791
    public function utf8_strrev($str) {
792
        preg_match_all('/./us', $str, $ar);
793
        return join('', array_reverse($ar[0]));
794
    }
795
}
796
 
797