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
 * savefileformat.php - Replaces dataformatlib.php to capture output into files.
19
 *
20
 * @package    mod_questionnaire
21
 * @copyright  2019 onward Mike Churchward (mike.churchward@poetopensource.org)
22
 * @author     Mike Churchward
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
/**
27
 * Sends a formated data file to the browser and optionally a file. This is needed until the main data format API provides a way
28
 * to ouput a file as well as stream to the browser. This file relies on capturing output buffers (ugly hack).
29
 *
30
 * @param string $filename The base filename without an extension
31
 * @param string $dataformat A dataformat name
32
 * @param array $columns An ordered map of column keys and labels
33
 * @param Iterator $iterator An iterator over the records, usually a RecordSet
34
 * @param array $users
35
 * @param array $emails
36
 * @param string $redirect
37
 */
38
function save_as_dataformat($filename, $dataformat, $columns, $iterator, $users = [], $emails = [], $redirect = '') {
39
    global $CFG, $OUTPUT;
40
 
41
    $classname = 'dataformat_' . $dataformat . '\writer';
42
    if (!class_exists($classname)) {
43
        throw new coding_exception("Unable to locate dataformat/$dataformat/classes/writer.php");
44
    }
45
    $format = new $classname;
46
 
47
    // The data format export could take a while to generate...
48
    set_time_limit(0);
49
 
50
    // Close the session so that the users other tabs in the same session are not blocked.
51
    \core\session\manager::write_close();
52
 
53
    $format->set_filename($filename);
54
    // File creation for any data format is initiated by "send_http_headers()". This is required. But, this also will cause the
55
    // browser to respond with a "save / open" dialogue. To get rid of the dialogue, immediately retract the headers with
56
    // "header_remove()".
57
    $format->send_http_headers();
58
    header_remove();
59
 
60
    // Start capturing output to write to a file.
61
    ob_start();
62
    // This exists to support all dataformats - see MDL-56046.
63
    if (method_exists($format, 'write_header')) {
64
        debugging('The function write_header() does not support multiple sheets. In order to support multiple sheets you ' .
65
            'must implement start_output() and start_sheet() and remove write_header() in your dataformat.', DEBUG_DEVELOPER);
66
        $format->write_header($columns);
67
    } else {
68
        $format->start_output();
69
        $format->start_sheet($columns);
70
    }
71
    $c = 0;
72
    foreach ($iterator as $row) {
73
        if ($row === null) {
74
            continue;
75
        }
76
        $format->write_record($row, $c++);
77
    }
78
    // This exists to support all dataformats - see MDL-56046.
79
    if (method_exists($format, 'write_footer')) {
80
        debugging('The function write_footer() does not support multiple sheets. In order to support multiple sheets you ' .
81
            'must implement close_sheet() and close_output() and remove write_footer() in your dataformat.', DEBUG_DEVELOPER);
82
        $format->write_footer($columns);
83
    } else {
84
        $format->close_sheet($columns);
85
        $format->close_output();
86
        $output = ob_get_contents();
87
        $ext = $format->get_extension();
88
        $filepath = make_temp_directory('mod_questionnaire') . '/' . $filename . $ext;
89
        $fp = fopen($filepath, 'wb');
90
        fwrite($fp, $output);
91
        fclose($fp);
92
        $subjecttext = get_string('summaryreportattached', 'questionnaire');
93
        foreach ($users as $user) {
94
            email_to_user($user, $CFG->noreplyaddress, $subjecttext, $subjecttext, '', $filepath, $filename.$ext);
95
        }
96
        foreach ($emails as $email) {
97
            $email = trim($email);
98
            $user = (object)['id' => -10, 'email' => $email, 'firstname' => $email, 'lastname' => $email, 'mailformat' => 1];
99
            email_to_user($user, $CFG->noreplyaddress, $subjecttext, $subjecttext, '', $filepath, $filename.$ext);
100
        }
101
        unlink($filepath);
102
    }
103
    ob_end_clean();
104
    echo $OUTPUT->redirect_message($redirect, get_string('emailssent', 'questionnaire'), 3, false);
105
}