Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 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  namespace core_user\form;
  18  
  19  use html_writer;
  20  use moodle_url;
  21  
  22  /**
  23   * Manage user private area files form
  24   *
  25   * @package    core_user
  26   * @copyright  2010 Petr Skoda (http://skodak.org)
  27   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   */
  29  class private_files extends \core_form\dynamic_form {
  30  
  31      /**
  32       * Add elements to this form.
  33       */
  34      public function definition() {
  35          global $OUTPUT;
  36          $mform = $this->_form;
  37          $options = $this->get_options();
  38  
  39          // Show file area space usage.
  40          $maxareabytes = $options['areamaxbytes'];
  41          if ($maxareabytes != FILE_AREA_MAX_BYTES_UNLIMITED) {
  42              $fileareainfo = file_get_file_area_info($this->get_context_for_dynamic_submission()->id, 'user', 'private');
  43              // Display message only if we have files.
  44              if ($fileareainfo['filecount']) {
  45                  $a = (object) [
  46                      'used' => display_size($fileareainfo['filesize_without_references']),
  47                      'total' => display_size($maxareabytes)
  48                  ];
  49                  $quotamsg = get_string('quotausage', 'moodle', $a);
  50                  $notification = new \core\output\notification($quotamsg, \core\output\notification::NOTIFY_INFO);
  51                  $mform->addElement('static', 'areabytes', '', $OUTPUT->render($notification));
  52              }
  53          }
  54  
  55          $mform->addElement('filemanager', 'files_filemanager', get_string('files'), null, $options);
  56          if ($link = $this->get_emaillink()) {
  57              $emaillink = html_writer::link(new moodle_url('mailto:' . $link), $link);
  58              $mform->addElement('static', 'emailaddress', '',
  59                  get_string('emailtoprivatefiles', 'moodle', $emaillink));
  60          }
  61          $mform->setType('returnurl', PARAM_LOCALURL);
  62  
  63          // The 'nosubmit' param (default false) determines whether we should show the standard form action buttons (save/cancel).
  64          // This value is set when the form is displayed within a modal, which adds the action buttons itself.
  65          if (!$this->optional_param('nosubmit', false, PARAM_BOOL)) {
  66              $this->add_action_buttons();
  67          }
  68      }
  69  
  70      /**
  71       * Validate incoming data.
  72       *
  73       * @param array $data
  74       * @param array $files
  75       * @return array
  76       */
  77      public function validation($data, $files) {
  78          $errors = array();
  79          $draftitemid = $data['files_filemanager'];
  80          $options = $this->get_options();
  81          if (file_is_draft_area_limit_reached($draftitemid, $options['areamaxbytes'])) {
  82              $errors['files_filemanager'] = get_string('userquotalimit', 'error');
  83          }
  84  
  85          return $errors;
  86      }
  87  
  88      /**
  89       * Link to email private files
  90       *
  91       * @return string|null
  92       * @throws \coding_exception
  93       */
  94      protected function get_emaillink() {
  95          global $USER;
  96  
  97          // Attempt to generate an inbound message address to support e-mail to private files.
  98          $generator = new \core\message\inbound\address_manager();
  99          $generator->set_handler('\core\message\inbound\private_files_handler');
 100          $generator->set_data(-1);
 101          return $generator->generate($USER->id);
 102      }
 103  
 104      /**
 105       * Check if current user has access to this form, otherwise throw exception
 106       *
 107       * Sometimes permission check may depend on the action and/or id of the entity.
 108       * If necessary, form data is available in $this->_ajaxformdata or
 109       * by calling $this->optional_param()
 110       */
 111      protected function check_access_for_dynamic_submission(): void {
 112          require_capability('moodle/user:manageownfiles', $this->get_context_for_dynamic_submission());
 113      }
 114  
 115      /**
 116       * Returns form context
 117       *
 118       * If context depends on the form data, it is available in $this->_ajaxformdata or
 119       * by calling $this->optional_param()
 120       *
 121       * @return \context
 122       */
 123      protected function get_context_for_dynamic_submission(): \context {
 124          global $USER;
 125          return \context_user::instance($USER->id);
 126      }
 127  
 128      /**
 129       * File upload options
 130       *
 131       * @return array
 132       * @throws \coding_exception
 133       */
 134      protected function get_options(): array {
 135          global $CFG;
 136  
 137          $maxbytes = $CFG->userquota;
 138          $maxareabytes = $CFG->userquota;
 139          if (has_capability('moodle/user:ignoreuserquota', $this->get_context_for_dynamic_submission())) {
 140              $maxbytes = USER_CAN_IGNORE_FILE_SIZE_LIMITS;
 141              $maxareabytes = FILE_AREA_MAX_BYTES_UNLIMITED;
 142          }
 143  
 144          return ['subdirs' => 1, 'maxbytes' => $maxbytes, 'maxfiles' => -1, 'accepted_types' => '*',
 145              'areamaxbytes' => $maxareabytes];
 146      }
 147  
 148      /**
 149       * Process the form submission, used if form was submitted via AJAX
 150       *
 151       * This method can return scalar values or arrays that can be json-encoded, they will be passed to the caller JS.
 152       *
 153       * Submission data can be accessed as: $this->get_data()
 154       *
 155       * @return mixed
 156       */
 157      public function process_dynamic_submission() {
 158          file_postupdate_standard_filemanager($this->get_data(), 'files',
 159              $this->get_options(), $this->get_context_for_dynamic_submission(), 'user', 'private', 0);
 160          return null;
 161      }
 162  
 163      /**
 164       * Load in existing data as form defaults
 165       *
 166       * Can be overridden to retrieve existing values from db by entity id and also
 167       * to preprocess editor and filemanager elements
 168       *
 169       * Example:
 170       *     $this->set_data(get_entity($this->_ajaxformdata['id']));
 171       */
 172      public function set_data_for_dynamic_submission(): void {
 173          $data = new \stdClass();
 174          file_prepare_standard_filemanager($data, 'files', $this->get_options(),
 175              $this->get_context_for_dynamic_submission(), 'user', 'private', 0);
 176          $this->set_data($data);
 177      }
 178  
 179      /**
 180       * Returns url to set in $PAGE->set_url() when form is being rendered or submitted via AJAX
 181       *
 182       * This is used in the form elements sensitive to the page url, such as Atto autosave in 'editor'
 183       *
 184       * If the form has arguments (such as 'id' of the element being edited), the URL should
 185       * also have respective argument.
 186       *
 187       * @return \moodle_url
 188       */
 189      protected function get_page_url_for_dynamic_submission(): \moodle_url {
 190          return new moodle_url('/user/files.php');
 191      }
 192  }