[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/repository/recent/ -> lib.php (source)

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * This plugin is used to access recent used files
  20   *
  21   * @since Moodle 2.0
  22   * @package    repository_recent
  23   * @copyright  2010 Dongsheng Cai {@link http://dongsheng.org}
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  require_once($CFG->dirroot . '/repository/lib.php');
  27  
  28  /**
  29   * repository_recent class is used to browse recent used files
  30   *
  31   * @since Moodle 2.0
  32   * @package    repository_recent
  33   * @copyright  2010 Dongsheng Cai {@link http://dongsheng.org}
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  define('DEFAULT_RECENT_FILES_NUM', 50);
  37  class repository_recent extends repository {
  38  
  39      /**
  40       * Initialize recent plugin
  41       * @param int $repositoryid
  42       * @param int $context
  43       * @param array $options
  44       */
  45      public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
  46          parent::__construct($repositoryid, $context, $options);
  47          $number = get_config('recent', 'recentfilesnumber');
  48          $number = (int)$number;
  49          if (empty($number)) {
  50              $this->number = DEFAULT_RECENT_FILES_NUM;
  51          } else {
  52              $this->number = $number;
  53          }
  54      }
  55  
  56      /**
  57       * recent plugin doesn't require login, so list all files
  58       * @return mixed
  59       */
  60      public function print_login() {
  61          return $this->get_listing();
  62      }
  63  
  64      private function get_recent_files($limitfrom = 0, $limit = DEFAULT_RECENT_FILES_NUM) {
  65          // XXX: get current itemid
  66          global $USER, $DB, $itemid;
  67          // This SQL will ignore draft files if not owned by current user.
  68          // Ignore all file references.
  69          $sql = 'SELECT files1.*
  70                    FROM {files} files1
  71               LEFT JOIN {files_reference} r
  72                         ON files1.referencefileid = r.id
  73                    JOIN (
  74                        SELECT contenthash, filename, MAX(id) AS id
  75                          FROM {files}
  76                         WHERE userid = :userid
  77                           AND filename != :filename
  78                           AND ((filearea = :filearea1 AND itemid = :itemid) OR filearea != :filearea2)
  79                      GROUP BY contenthash, filename
  80                    ) files2 ON files1.id = files2.id
  81                   WHERE r.repositoryid is NULL
  82                ORDER BY files1.timemodified DESC';
  83          $params = array(
  84              'userid' => $USER->id,
  85              'filename' => '.',
  86              'filearea1' => 'draft',
  87              'itemid' => $itemid,
  88              'filearea2' => 'draft');
  89          $rs = $DB->get_recordset_sql($sql, $params, $limitfrom, $limit);
  90          $result = array();
  91          foreach ($rs as $file_record) {
  92              $info = array();
  93              $info['contextid'] = $file_record->contextid;
  94              $info['itemid'] = $file_record->itemid;
  95              $info['filearea'] = $file_record->filearea;
  96              $info['component'] = $file_record->component;
  97              $info['filepath'] = $file_record->filepath;
  98              $info['filename'] = $file_record->filename;
  99              $result[$file_record->pathnamehash] = $info;
 100          }
 101          $rs->close();
 102          return $result;
 103      }
 104  
 105      /**
 106       * Get file listing
 107       *
 108       * @param string $encodedpath
 109       * @param string $path not used by this plugin
 110       * @return mixed
 111       */
 112      public function get_listing($encodedpath = '', $page = '') {
 113          global $OUTPUT;
 114          $ret = array();
 115          $ret['dynload'] = true;
 116          $ret['nosearch'] = true;
 117          $ret['nologin'] = true;
 118          $list = array();
 119          $files = $this->get_recent_files(0, $this->number);
 120  
 121          try {
 122              foreach ($files as $file) {
 123                  // Check that file exists and accessible, retrieve size/date info
 124                  $browser = get_file_browser();
 125                  $context = context::instance_by_id($file['contextid']);
 126                  $fileinfo = $browser->get_file_info($context, $file['component'],
 127                          $file['filearea'], $file['itemid'], $file['filepath'], $file['filename']);
 128                  if ($fileinfo) {
 129                      $params = base64_encode(json_encode($file));
 130                      $node = array(
 131                          'title' => $fileinfo->get_visible_name(),
 132                          'size' => $fileinfo->get_filesize(),
 133                          'datemodified' => $fileinfo->get_timemodified(),
 134                          'datecreated' => $fileinfo->get_timecreated(),
 135                          'author' => $fileinfo->get_author(),
 136                          'license' => $fileinfo->get_license(),
 137                          'source'=> $params,
 138                          'icon' => $OUTPUT->pix_url(file_file_icon($fileinfo, 24))->out(false),
 139                          'thumbnail' => $OUTPUT->pix_url(file_file_icon($fileinfo, 90))->out(false),
 140                      );
 141                      if ($imageinfo = $fileinfo->get_imageinfo()) {
 142                          $fileurl = new moodle_url($fileinfo->get_url());
 143                          $node['realthumbnail'] = $fileurl->out(false, array('preview' => 'thumb', 'oid' => $fileinfo->get_timemodified()));
 144                          $node['realicon'] = $fileurl->out(false, array('preview' => 'tinyicon', 'oid' => $fileinfo->get_timemodified()));
 145                          $node['image_width'] = $imageinfo['width'];
 146                          $node['image_height'] = $imageinfo['height'];
 147                      }
 148                      $list[] = $node;
 149                  }
 150              }
 151          } catch (Exception $e) {
 152              throw new repository_exception('emptyfilelist', 'repository_recent');
 153          }
 154          $ret['list'] = array_filter($list, array($this, 'filter'));
 155          return $ret;
 156      }
 157  
 158      public static function get_type_option_names() {
 159          return array('recentfilesnumber', 'pluginname');
 160      }
 161  
 162      public static function type_config_form($mform, $classname = 'repository') {
 163          parent::type_config_form($mform, $classname);
 164          $number = get_config('repository_recent', 'recentfilesnumber');
 165          if (empty($number)) {
 166              $number = DEFAULT_RECENT_FILES_NUM;
 167          }
 168          $mform->addElement('text', 'recentfilesnumber', get_string('recentfilesnumber', 'repository_recent'));
 169          $mform->setType('recentfilesnumber', PARAM_INT);
 170          $mform->setDefault('recentfilesnumber', $number);
 171      }
 172  
 173      /**
 174       * This plugin doesn't support to link to external links
 175       *
 176       * @return int
 177       */
 178      public function supported_returntypes() {
 179          return FILE_INTERNAL;
 180      }
 181  
 182      /**
 183       * Repository method to make sure that user can access particular file.
 184       *
 185       * This is checked when user tries to pick the file from repository to deal with
 186       * potential parameter substitutions is request
 187       *
 188       * @todo MDL-33805 remove this function when recent files are managed correctly
 189       *
 190       * @param string $source
 191       * @return bool whether the file is accessible by current user
 192       */
 193      public function file_is_accessible($source) {
 194          global $USER;
 195          $reference = $this->get_file_reference($source);
 196          $file = self::get_moodle_file($reference);
 197          return (!empty($file) && $file->get_userid() == $USER->id);
 198      }
 199  
 200      /**
 201       * Does this repository used to browse moodle files?
 202       *
 203       * @return boolean
 204       */
 205      public function has_moodle_files() {
 206          return true;
 207      }
 208  
 209      /**
 210       * Is this repository accessing private data?
 211       *
 212       * @return bool
 213       */
 214      public function contains_private_data() {
 215          return false;
 216      }
 217  }


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