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
 * Field definitions.
19
 *
20
 * @package    block_dash
21
 * @copyright  2019 bdecent gmbh <https://bdecent.de>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
require(__DIR__.'/../../config.php');
26
 
27
if (!defined('AJAX_SCRIPT')) {
28
    define('AJAX_SCRIPT', true);
29
}
30
 
31
require_login();
32
 
33
use block_dash\local\block_builder;
34
 
35
$download = optional_param('download', 'csv', PARAM_TEXT);
36
$instanceid = required_param('block_instance_id', PARAM_INT);
37
$filterformdata = optional_param('filter_form_data', '', PARAM_TEXT);
38
$currentpage = optional_param('page', 0, PARAM_INT);
39
$sortfield = optional_param('sort_field', '', PARAM_TEXT);
40
$sortdir = optional_param('sort_direction', '', PARAM_TEXT);
41
 
42
$PAGE->set_context(context_system::instance());
43
 
44
$renderer = $PAGE->get_renderer('block_dash');
45
$binstance = $DB->get_record('block_instances', ['id' => $instanceid]);
46
$block = block_instance($binstance->blockname, $binstance);
47
if ($sortfield) {
48
    $block->set_sort($sortfield, $sortdir);
49
}
50
$bbdownload = block_builder::create($block);
51
if (!$bbdownload->get_configuration()->get_data_source()->get_preferences('exportdata') ) {
52
    return false;
53
}
54
foreach (json_decode($filterformdata, true) as $filter) {
55
    $bbdownload->get_configuration()
56
        ->get_data_source()
57
        ->get_filter_collection()
58
        ->apply_filter($filter['name'], $filter['value']);
59
}
60
$bbdownload->get_configuration()->get_data_source()->get_paginator()->set_current_page($currentpage);
61
$bbdownloadsource = $bbdownload->get_configuration()->get_data_source();
62
$file = $bbdownload->get_configuration()->get_data_source()->get_name();
63
$filename = $file . "_" . get_string('strdatasource', 'block_dash');
64
if ($download == "xls") {
65
    require_once("$CFG->libdir/excellib.class.php");
66
    // Calculate file name.
67
    // Creating a workbook.
68
    $workbook = new \MoodleExcelWorkbook("-");
69
    // Send HTTP headers.
70
    $filename .= "_" . time();
71
    $workbook->send($filename);
72
    // Creating the first worksheet.
73
    $myxls = $workbook->add_worksheet('dash');
74
    // Print names of all the fields.
75
    $i = 0;
76
    foreach ($bbdownloadsource->export_for_template($renderer)['data']->first_child()['data'] as $col) {
77
        if ($col->is_visible()) {
78
            $myxls->write_string(0, $i++, $col->get_label());
79
        }
80
    }
81
    $rowdata = $bbdownloadsource->export_for_template($renderer)['data']['rows'];
82
    if ($rowdata) {
83
        // Generate the data for the body of the spreadsheet.
84
        $j = 1;
85
        foreach ($rowdata as $row) {
86
            $fields = [];
87
            $k = 0;
88
            foreach ($row['data'] as $data) {
89
                if ($data->is_visible()) {
90
                    $myxls->write_string($j, $k++, trim(strip_tags(format_text($data->get_value(), true))));
91
                }
92
            }
93
            $j++;
94
        }
95
    }
96
    // Close the workbook.
97
    $workbook->close();
98
} else if ($download == 'csv') {
99
    require_once("$CFG->libdir/csvlib.class.php");
100
    $csvexport = new \csv_export_writer("-");
101
    $csvexport->set_filename($filename);
102
    $headers = [];
103
    foreach ($bbdownloadsource->export_for_template($renderer)['data']->first_child()['data'] as $col) {
104
        if ($col->is_visible()) {
105
            $headers[] = $col->get_label();
106
        }
107
    }
108
    $csvexport->add_data($headers);
109
    $rowdata = $bbdownloadsource->export_for_template($renderer)['data']['rows'];
110
    if ($rowdata) {
111
        foreach ($rowdata as $row) {
112
            $cols = [];
113
            foreach ($row['data'] as $data) {
114
                if ($data->is_visible()) {
115
                    $cols[] = trim(strip_tags(format_text($data->get_value(), true)));
116
                }
117
            }
118
            $csvexport->add_data($cols);
119
        }
120
    }
121
    $csvexport->download_file();
122
}
123
 
124