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
namespace mod_data\local\exporter;
18
 
19
use context;
20
use context_system;
21
 
22
/**
23
 * Utility class for exporting data from a mod_data instance.
24
 *
25
 * @package    mod_data
26
 * @copyright  2023 ISB Bayern
27
 * @author     Philipp Memmel
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 */
30
class utils {
31
 
32
    /**
33
     * Exports the data of the mod_data instance to an entries_exporter object which then can export it to a file format.
34
     *
35
     * @param int $dataid
36
     * @param array $fields
37
     * @param array $selectedfields
38
     * @param entries_exporter $exporter the entries_exporter object used
39
     * @param int $currentgroup group ID of the current group. This is used for
40
     *  exporting data while maintaining group divisions.
41
     * @param context|null $context the context in which the operation is performed (for capability checks)
42
     * @param bool $userdetails whether to include the details of the record author
43
     * @param bool $time whether to include time created/modified
44
     * @param bool $approval whether to include approval status
45
     * @param bool $tags whether to include tags
46
     * @param bool $includefiles whether files should be exported as well
47
     * @return void
48
     */
49
    public static function data_exportdata(int $dataid, array $fields, array $selectedfields, entries_exporter $exporter,
50
        int $currentgroup = 0, context $context = null, bool $userdetails = false, bool $time = false, bool $approval = false,
51
        bool $tags = false, bool $includefiles = true): void {
52
        global $DB;
53
 
54
        if (is_null($context)) {
55
            $context = context_system::instance();
56
        }
57
        // Exporting user data needs special permission.
58
        $userdetails = $userdetails && has_capability('mod/data:exportuserinfo', $context);
59
 
60
        // Populate the header in first row of export.
61
        $header = [];
62
        foreach ($fields as $key => $field) {
63
            if (!in_array($field->field->id, $selectedfields)) {
64
                // Ignore values we aren't exporting.
65
                unset($fields[$key]);
66
            } else {
67
                $header[] = $field->field->name;
68
            }
69
        }
70
        if ($tags) {
71
            $header[] = get_string('tags', 'data');
72
        }
73
        if ($userdetails) {
74
            $header[] = get_string('user');
75
            $header[] = get_string('username');
76
            $header[] = get_string('email');
77
        }
78
        if ($time) {
79
            $header[] = get_string('timeadded', 'data');
80
            $header[] = get_string('timemodified', 'data');
81
        }
82
        if ($approval) {
83
            $header[] = get_string('approved', 'data');
84
        }
85
        $exporter->add_row($header);
86
 
87
        $datarecords = $DB->get_records('data_records', array('dataid' => $dataid));
88
        ksort($datarecords);
89
        $line = 1;
90
        foreach ($datarecords as $record) {
91
            // Get content indexed by fieldid.
92
            if ($currentgroup) {
93
                $select = 'SELECT c.fieldid, c.content, c.content1, c.content2, c.content3, c.content4 FROM {data_content} c, '
94
                    . '{data_records} r WHERE c.recordid = ? AND r.id = c.recordid AND r.groupid = ?';
95
                $where = array($record->id, $currentgroup);
96
            } else {
97
                $select = 'SELECT fieldid, content, content1, content2, content3, content4 FROM {data_content} WHERE recordid = ?';
98
                $where = array($record->id);
99
            }
100
 
101
            if ($content = $DB->get_records_sql($select, $where)) {
102
                foreach ($fields as $field) {
103
                    $contents = '';
104
                    if (isset($content[$field->field->id])) {
105
                        $contents = $field->export_text_value($content[$field->field->id]);
106
                        if (!empty($contents) && $field->file_export_supported() && $includefiles
107
                            && !is_null($field->export_file_value($record))) {
108
                            // For exporting overwrite the content of the column with a unique
109
                            // filename, even it is not exactly the name of the file in the
110
                            // mod_data instance content. But it's more important to match the name
111
                            // of the exported file.
112
                            $contents = $exporter->create_unique_filename($contents);
113
                            $exporter->add_file_from_string($contents, $field->export_file_value($record));
114
                        }
115
                    }
116
                    // Just be double sure.
117
                    $contents = !empty($contents) ? $contents : '';
118
                    $exporter->add_to_current_row($contents);
119
                }
120
                if ($tags) {
121
                    $itemtags = \core_tag_tag::get_item_tags_array('mod_data', 'data_records', $record->id);
122
                    $exporter->add_to_current_row(implode(', ', $itemtags));
123
                }
124
                if ($userdetails) { // Add user details to the export data.
125
                    $userdata = get_complete_user_data('id', $record->userid);
126
                    $exporter->add_to_current_row(fullname($userdata));
127
                    $exporter->add_to_current_row($userdata->username);
128
                    $exporter->add_to_current_row($userdata->email);
129
                }
130
                if ($time) { // Add time added / modified.
131
                    $exporter->add_to_current_row(userdate($record->timecreated));
132
                    $exporter->add_to_current_row(userdate($record->timemodified));
133
                }
134
                if ($approval) { // Add approval status.
135
                    $exporter->add_to_current_row((int) $record->approved);
136
                }
137
            }
138
            $exporter->next_row();
139
        }
140
    }
141
}