Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [Versions 401 and 403]

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