[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/editor/atto/plugins/managefiles/ -> manage.php (source)

   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   * Manage files in user draft area attached to texteditor.
  19   *
  20   * @package   atto_managefiles
  21   * @copyright 2014 Frédéric Massart
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require(__DIR__ . '/../../../../../config.php');
  26  require_once (__DIR__ . '/manage_form.php');
  27  require_once($CFG->libdir . '/filestorage/file_storage.php');
  28  require_once($CFG->dirroot . '/repository/lib.php');
  29  
  30  $itemid = required_param('itemid', PARAM_INT);
  31  $maxbytes = optional_param('maxbytes', 0, PARAM_INT);
  32  $subdirs = optional_param('subdirs', 0, PARAM_INT);
  33  $accepted_types = optional_param('accepted_types', '*', PARAM_RAW); // TODO Not yet passed to this script.
  34  $return_types = optional_param('return_types', null, PARAM_INT);
  35  $areamaxbytes = optional_param('areamaxbytes', FILE_AREA_MAX_BYTES_UNLIMITED, PARAM_INT);
  36  $contextid = optional_param('context', SYSCONTEXTID, PARAM_INT);
  37  $elementid = optional_param('elementid', '', PARAM_TEXT);
  38  
  39  $context = context::instance_by_id($contextid);
  40  if ($context->contextlevel == CONTEXT_MODULE) {
  41      // Module context.
  42      $cm = $DB->get_record('course_modules', array('id' => $context->instanceid));
  43      require_login($cm->course, true, $cm);
  44  } else if (($coursecontext = $context->get_course_context(false)) && $coursecontext->id != SITEID) {
  45      // Course context or block inside the course.
  46      require_login($coursecontext->instanceid);
  47      $PAGE->set_context($context);
  48  } else {
  49      // Block that is not inside the course, user or system context.
  50      require_login();
  51      $PAGE->set_context($context);
  52  }
  53  
  54  // Guests can never manage files.
  55  if (isguestuser()) {
  56      print_error('noguest');
  57  }
  58  
  59  $title = get_string('managefiles', 'atto_managefiles');
  60  
  61  $PAGE->set_url('/lib/editor/atto/plugins/managefiles/manage.php');
  62  $PAGE->set_title($title);
  63  $PAGE->set_heading($title);
  64  $PAGE->set_pagelayout('popup');
  65  
  66  if ($return_types !== null) {
  67      // Links are allowed in textarea but never allowed in filemanager.
  68      $return_types = $return_types & ~FILE_EXTERNAL;
  69  }
  70  
  71  $options = array(
  72      'subdirs' => $subdirs,
  73      'maxbytes' => $maxbytes,
  74      'maxfiles' => -1,
  75      'accepted_types' => $accepted_types,
  76      'areamaxbytes' => $areamaxbytes,
  77      'return_types' => $return_types,
  78      'context' => $context,
  79  );
  80  
  81  $usercontext = context_user::instance($USER->id);
  82  $fs = get_file_storage();
  83  $files = $fs->get_directory_files($usercontext->id, 'user', 'draft', $itemid, '/', !empty($subdirs), false);
  84  $filenames = array();
  85  foreach ($files as $file) {
  86      $filenames[$file->get_pathnamehash()] = ltrim($file->get_filepath(), '/') . $file->get_filename();
  87  }
  88  
  89  $mform = new atto_managefiles_manage_form(null,
  90      array('options' => $options, 'draftitemid' => $itemid, 'files' => $filenames, 'elementid' => $elementid),
  91      'post', '', array('id' => 'atto_managefiles_manageform'));
  92  
  93  if ($data = $mform->get_data()) {
  94      if (!empty($data->deletefile)) {
  95          foreach (array_keys($data->deletefile) as $filehash) {
  96              if ($file = $fs->get_file_by_hash($filehash)) {
  97                  // Make sure the user didn't modify the filehash to delete another file.
  98                  if ($file->get_component() == 'user' && $file->get_filearea() == 'draft'
  99                          && $file->get_itemid() == $itemid && $file->get_contextid() == $usercontext->id) {
 100                      $file->delete();
 101                  }
 102              }
 103          }
 104          $filenames = array_diff_key($filenames, $data->deletefile);
 105          $mform = new atto_managefiles_manage_form(null,
 106              array('options' => $options, 'draftitemid' => $itemid, 'files' => $filenames, 'elementid' => $data->elementid),
 107              'post', '', array('id' => 'atto_managefiles_manageform'));
 108      }
 109  }
 110  
 111  echo $OUTPUT->header();
 112  $mform->display();
 113  echo $OUTPUT->footer();


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1