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
 * Infected file report
19
 *
20
 * @package    report_infectedfiles
21
 * @author     Nathan Nguyen <nathannguyen@catalyst-au.net>
22
 * @copyright  Catalyst IT
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
namespace report_infectedfiles\table;
26
 
27
defined('MOODLE_INTERNAL') || die();
28
 
29
require_once($CFG->libdir . '/tablelib.php');
30
 
31
/**
32
 * Infected file report
33
 *
34
 * @package    report_infectedfiles
35
 * @author     Nathan Nguyen <nathannguyen@catalyst-au.net>
36
 * @copyright  Catalyst IT
37
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 */
39
class infectedfiles_table extends \table_sql implements \renderable {
40
 
41
    /** @var int current page. */
42
    protected $page;
43
 
44
    /**
45
     * Table constructor
46
     *
47
     * @param int $uniqueid table id
48
     * @param \moodle_url $url page url
49
     * @param int $page current page
50
     * @param int $perpage number or record per page
51
     * @throws \coding_exception
52
     */
53
    public function __construct($uniqueid, \moodle_url $url, $page = 0, $perpage = 30) {
54
        parent::__construct($uniqueid);
55
 
56
        $this->set_attribute('class', 'report_infectedfiles');
57
 
58
        // Set protected properties.
59
        $this->pagesize = $perpage;
60
        $this->page = $page;
61
 
62
        // Define columns in the table.
63
        $this->define_table_columns();
64
 
65
        // Define configs.
66
        $this->define_table_configs($url);
67
    }
68
 
69
    /**
70
     * Table columns and corresponding headers
71
     *
72
     * @throws \coding_exception
73
     */
74
    protected function define_table_columns() {
75
        $cols = array(
76
            'filename' => get_string('filename', 'report_infectedfiles'),
77
            'author' => get_string('author', 'report_infectedfiles'),
78
            'reason' => get_string('reason', 'report_infectedfiles'),
79
            'timecreated' => get_string('timecreated', 'report_infectedfiles'),
80
            'actions' => get_string('actions'),
81
        );
82
 
83
        $this->define_columns(array_keys($cols));
84
        $this->define_headers(array_values($cols));
85
    }
86
 
87
    /**
88
     * Define table configuration
89
     *
90
     * @param \moodle_url $url
91
     */
92
    protected function define_table_configs(\moodle_url $url) {
93
        // Set table url.
94
        $this->define_baseurl($url);
95
 
96
        // Set table configs.
97
        $this->collapsible(false);
98
        $this->sortable(false);
99
        $this->pageable(true);
100
    }
101
 
102
    /**
103
     * Builds the SQL query.
104
     *
105
     * @param bool $count When true, return the count SQL.
106
     * @return array containing sql to use and an array of params.
107
     */
108
    protected function get_sql_and_params($count = false): array {
109
        if ($count) {
110
            $select = "COUNT(1)";
111
        } else {
112
            $select = "*";
113
        }
114
 
115
        $sql = "SELECT $select
116
                  FROM {infected_files}";
117
 
118
        $params = array();
119
 
120
        if (!$count) {
121
            $sql .= " ORDER BY timecreated DESC";
122
        }
123
 
124
        return array($sql, $params);
125
    }
126
 
127
    /**
128
     * Get data.
129
     *
130
     * @param int $pagesize number of records to fetch
131
     * @param bool $useinitialsbar initial bar
132
     * @throws \dml_exception
133
     */
134
    public function query_db($pagesize, $useinitialsbar = true) {
135
        global $DB;
136
 
137
        list($countsql, $countparams) = $this->get_sql_and_params(true);
138
        list($sql, $params) = $this->get_sql_and_params();
139
        $total = $DB->count_records_sql($countsql, $countparams);
140
        $this->pagesize($pagesize, $total);
141
        $this->rawdata = $DB->get_records_sql($sql, $params, $this->get_page_start(), $this->get_page_size());
142
 
143
        // Set initial bars.
144
        if ($useinitialsbar) {
145
            $this->initialbars($total > $pagesize);
146
        }
147
    }
148
 
149
    /**
150
     * Column to display the authors fullname from userid.
151
     *
152
     * @param \stdClass $row the row from sql.
153
     * @return string the authors name.
154
     */
155
    protected function col_author($row): string {
156
        // Get user fullname from ID.
157
        $user = \core_user::get_user($row->userid);
158
        $url = new \moodle_url('/user/profile.php', ['id' => $row->userid]);
159
        return \html_writer::link($url, fullname($user));
160
    }
161
 
162
    /**
163
     * Column to display the failure reason.
164
     *
165
     * @param \stdClass $row the row from sql.
166
     * @return string the formatted reason.
167
     */
168
    protected function col_reason($row) {
169
        return format_text($row->reason);
170
    }
171
 
172
    /**
173
     * Custom actions column
174
     *
175
     * @param \stdClass $row an incident record.
176
     * @return string content of action column.
177
     * @throws \coding_exception
178
     * @throws \moodle_exception
179
     */
180
    protected function col_actions($row): string {
181
        global $OUTPUT;
182
        $filename = $row->quarantinedfile;
183
        $fileid = $row->id;
184
        // If the file isn't found, we can do nothing in this column.
185
        // This shouldn't happen, unless the file is manually deleted from the server externally.
186
        if (!\core\antivirus\quarantine::quarantined_file_exists($filename)) {
187
            return '';
188
        }
189
        $links = '';
190
        $managefilepage = new \moodle_url('/report/infectedfiles/index.php');
191
 
192
        // Download.
193
        $downloadparams = ['file' => $fileid, 'action' => 'download', 'sesskey' => sesskey()];
194
        $downloadurl = new \moodle_url($managefilepage, $downloadparams);
195
 
196
        $downloadconfirm = new \confirm_action(get_string('confirmdownload', 'report_infectedfiles'));
197
        $links .= $OUTPUT->action_icon(
198
            $downloadurl,
199
            new \pix_icon('t/download', get_string('download')),
200
            $downloadconfirm
201
        );
202
 
203
        // Delete.
204
        $deleteparams = ['file' => $fileid, 'action' => 'delete', 'sesskey' => sesskey()];
205
        $deleteurl = new \moodle_url($managefilepage, $deleteparams);
206
        $deleteconfirm = new \confirm_action(get_string('confirmdelete', 'report_infectedfiles'));
207
        $links .= $OUTPUT->action_icon(
208
            $deleteurl,
209
            new \pix_icon('t/delete', get_string('delete')),
210
            $deleteconfirm
211
        );
212
 
213
        return $links;
214
    }
215
 
216
    /**
217
     * Custom time column.
218
     *
219
     * @param \stdClass $row an incident record.
220
     * @return string time created in user-friendly format.
221
     */
222
    protected function col_timecreated($row): string {
223
        return userdate($row->timecreated);
224
    }
225
 
226
    /**
227
     * Display table with download all and delete all buttons
228
     *
229
     * @param int $pagesize number or records perpage
230
     * @param bool $useinitialsbar use the bar or not
231
     * @param string $downloadhelpbutton help button
232
     * @return void
233
     * @throws \coding_exception
234
     * @throws \moodle_exception
235
     */
236
    public function display($pagesize, $useinitialsbar, $downloadhelpbutton='') {
237
        global $OUTPUT;
238
        // Output the table, and then display buttons.
239
        $this->out($pagesize, $useinitialsbar, $downloadhelpbutton);
240
        $managefilepage = new \moodle_url('/report/infectedfiles/index.php');
241
 
242
        // If there are no rows, dont bother rendering extra buttons.
243
        if (empty($this->rawdata)) {
244
            return;
245
        }
246
 
247
        // Delete All.
248
        $deleteallparams = ['action' => 'deleteall', 'sesskey' => sesskey()];
249
        $deleteallurl = new \moodle_url($managefilepage, $deleteallparams);
250
        $deletebutton = new \single_button($deleteallurl, get_string('deleteall'), 'post', \single_button::BUTTON_PRIMARY);
251
        $deletebutton->add_confirm_action(get_string('confirmdeleteall', 'report_infectedfiles'));
252
        echo $OUTPUT->render($deletebutton);
253
 
254
        echo "&nbsp";
255
 
256
        // Download All.
257
        $downloadallparams = ['action' => 'downloadall', 'sesskey' => sesskey()];
258
        $downloadallurl = new \moodle_url($managefilepage, $downloadallparams);
259
        $downloadbutton = new \single_button($downloadallurl, get_string('downloadall'), 'post', \single_button::BUTTON_PRIMARY);
260
        $downloadbutton->add_confirm_action(get_string('confirmdownloadall', 'report_infectedfiles'));
261
        echo $OUTPUT->render($downloadbutton);
262
    }
263
 
264
}