2010-04-28 05:49:20 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
/**
|
2012-05-12 04:14:53 +08:00
|
|
|
* This plugin is used to access recent used files
|
2010-04-28 05:49:20 +00:00
|
|
|
*
|
2014-05-19 17:03:04 +01:00
|
|
|
* @since Moodle 2.0
|
2012-05-12 04:14:53 +08:00
|
|
|
* @package repository_recent
|
|
|
|
* @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
|
2010-09-06 11:29:21 +00:00
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2010-04-28 05:49:20 +00:00
|
|
|
*/
|
2012-05-12 04:14:53 +08:00
|
|
|
require_once($CFG->dirroot . '/repository/lib.php');
|
2010-04-28 05:49:20 +00:00
|
|
|
|
2012-05-12 04:14:53 +08:00
|
|
|
/**
|
|
|
|
* repository_recent class is used to browse recent used files
|
|
|
|
*
|
2014-05-19 17:03:04 +01:00
|
|
|
* @since Moodle 2.0
|
2012-05-12 04:14:53 +08:00
|
|
|
* @package repository_recent
|
|
|
|
* @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
2010-04-28 05:49:20 +00:00
|
|
|
define('DEFAULT_RECENT_FILES_NUM', 50);
|
|
|
|
class repository_recent extends repository {
|
|
|
|
|
|
|
|
/**
|
2010-07-05 07:27:49 +00:00
|
|
|
* Initialize recent plugin
|
2010-04-28 05:49:20 +00:00
|
|
|
* @param int $repositoryid
|
|
|
|
* @param int $context
|
|
|
|
* @param array $options
|
|
|
|
*/
|
|
|
|
public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
|
|
|
|
parent::__construct($repositoryid, $context, $options);
|
|
|
|
$number = get_config('recent', 'recentfilesnumber');
|
|
|
|
$number = (int)$number;
|
|
|
|
if (empty($number)) {
|
|
|
|
$this->number = DEFAULT_RECENT_FILES_NUM;
|
|
|
|
} else {
|
|
|
|
$this->number = $number;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* recent plugin doesn't require login, so list all files
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function print_login() {
|
|
|
|
return $this->get_listing();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function get_recent_files($limitfrom = 0, $limit = DEFAULT_RECENT_FILES_NUM) {
|
2010-08-24 06:29:24 +00:00
|
|
|
// XXX: get current itemid
|
|
|
|
global $USER, $DB, $itemid;
|
2012-05-16 14:08:37 +08:00
|
|
|
// This SQL will ignore draft files if not owned by current user.
|
|
|
|
// Ignore all file references.
|
2011-06-13 18:52:39 +02:00
|
|
|
$sql = 'SELECT files1.*
|
|
|
|
FROM {files} files1
|
2012-05-16 14:08:37 +08:00
|
|
|
LEFT JOIN {files_reference} r
|
|
|
|
ON files1.referencefileid = r.id
|
2011-06-13 18:52:39 +02:00
|
|
|
JOIN (
|
|
|
|
SELECT contenthash, filename, MAX(id) AS id
|
|
|
|
FROM {files}
|
|
|
|
WHERE userid = :userid
|
|
|
|
AND filename != :filename
|
|
|
|
AND ((filearea = :filearea1 AND itemid = :itemid) OR filearea != :filearea2)
|
|
|
|
GROUP BY contenthash, filename
|
|
|
|
) files2 ON files1.id = files2.id
|
2012-05-16 14:08:37 +08:00
|
|
|
WHERE r.repositoryid is NULL
|
|
|
|
ORDER BY files1.timemodified DESC';
|
2011-06-13 18:52:39 +02:00
|
|
|
$params = array(
|
|
|
|
'userid' => $USER->id,
|
|
|
|
'filename' => '.',
|
|
|
|
'filearea1' => 'draft',
|
|
|
|
'itemid' => $itemid,
|
|
|
|
'filearea2' => 'draft');
|
2010-05-03 12:40:51 +00:00
|
|
|
$rs = $DB->get_recordset_sql($sql, $params, $limitfrom, $limit);
|
2010-04-28 05:49:20 +00:00
|
|
|
$result = array();
|
|
|
|
foreach ($rs as $file_record) {
|
|
|
|
$info = array();
|
|
|
|
$info['contextid'] = $file_record->contextid;
|
|
|
|
$info['itemid'] = $file_record->itemid;
|
|
|
|
$info['filearea'] = $file_record->filearea;
|
2010-07-06 07:15:08 +00:00
|
|
|
$info['component'] = $file_record->component;
|
2010-04-28 05:49:20 +00:00
|
|
|
$info['filepath'] = $file_record->filepath;
|
|
|
|
$info['filename'] = $file_record->filename;
|
|
|
|
$result[$file_record->pathnamehash] = $info;
|
|
|
|
}
|
|
|
|
$rs->close();
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get file listing
|
|
|
|
*
|
|
|
|
* @param string $encodedpath
|
|
|
|
* @param string $path not used by this plugin
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function get_listing($encodedpath = '', $page = '') {
|
2012-02-09 13:55:41 +08:00
|
|
|
global $OUTPUT;
|
2010-04-28 05:49:20 +00:00
|
|
|
$ret = array();
|
|
|
|
$ret['dynload'] = true;
|
|
|
|
$ret['nosearch'] = true;
|
2010-05-19 02:49:18 +00:00
|
|
|
$ret['nologin'] = true;
|
2010-04-28 05:49:20 +00:00
|
|
|
$list = array();
|
|
|
|
$files = $this->get_recent_files(0, $this->number);
|
|
|
|
|
|
|
|
try {
|
|
|
|
foreach ($files as $file) {
|
MDL-14636: Support table view in Filepicker, provide more info about files
- Added table view in Filepicker where for each file we show Name, Last modified, Size and Type. Sortable by column
- Preprocess list of files returned from repositories to include formatted size, dates, type and also add filetype icon
- Make sure that local repositories (coursefiles, filesystem, local, recent, user) return information about file size, datemodified, datecreated, author, license
2012-04-17 14:20:20 +08:00
|
|
|
// Check that file exists and accessible, retrieve size/date info
|
|
|
|
$browser = get_file_browser();
|
2012-08-21 14:20:30 +08:00
|
|
|
$context = context::instance_by_id($file['contextid']);
|
MDL-14636: Support table view in Filepicker, provide more info about files
- Added table view in Filepicker where for each file we show Name, Last modified, Size and Type. Sortable by column
- Preprocess list of files returned from repositories to include formatted size, dates, type and also add filetype icon
- Make sure that local repositories (coursefiles, filesystem, local, recent, user) return information about file size, datemodified, datecreated, author, license
2012-04-17 14:20:20 +08:00
|
|
|
$fileinfo = $browser->get_file_info($context, $file['component'],
|
|
|
|
$file['filearea'], $file['itemid'], $file['filepath'], $file['filename']);
|
|
|
|
if ($fileinfo) {
|
2014-06-26 08:53:25 +08:00
|
|
|
$params = base64_encode(json_encode($file));
|
2012-02-09 13:55:41 +08:00
|
|
|
$node = array(
|
2012-05-21 15:17:53 +08:00
|
|
|
'title' => $fileinfo->get_visible_name(),
|
MDL-14636: Support table view in Filepicker, provide more info about files
- Added table view in Filepicker where for each file we show Name, Last modified, Size and Type. Sortable by column
- Preprocess list of files returned from repositories to include formatted size, dates, type and also add filetype icon
- Make sure that local repositories (coursefiles, filesystem, local, recent, user) return information about file size, datemodified, datecreated, author, license
2012-04-17 14:20:20 +08:00
|
|
|
'size' => $fileinfo->get_filesize(),
|
|
|
|
'datemodified' => $fileinfo->get_timemodified(),
|
|
|
|
'datecreated' => $fileinfo->get_timecreated(),
|
|
|
|
'author' => $fileinfo->get_author(),
|
|
|
|
'license' => $fileinfo->get_license(),
|
2012-02-09 13:55:41 +08:00
|
|
|
'source'=> $params,
|
2017-01-19 16:20:27 +08:00
|
|
|
'icon' => $OUTPUT->image_url(file_file_icon($fileinfo, 24))->out(false),
|
|
|
|
'thumbnail' => $OUTPUT->image_url(file_file_icon($fileinfo, 90))->out(false),
|
2012-02-09 13:55:41 +08:00
|
|
|
);
|
2012-05-10 06:12:24 +02:00
|
|
|
if ($imageinfo = $fileinfo->get_imageinfo()) {
|
|
|
|
$fileurl = new moodle_url($fileinfo->get_url());
|
2012-05-18 22:17:41 +02:00
|
|
|
$node['realthumbnail'] = $fileurl->out(false, array('preview' => 'thumb', 'oid' => $fileinfo->get_timemodified()));
|
|
|
|
$node['realicon'] = $fileurl->out(false, array('preview' => 'tinyicon', 'oid' => $fileinfo->get_timemodified()));
|
2012-05-10 06:12:24 +02:00
|
|
|
$node['image_width'] = $imageinfo['width'];
|
|
|
|
$node['image_height'] = $imageinfo['height'];
|
|
|
|
}
|
2012-02-09 13:55:41 +08:00
|
|
|
$list[] = $node;
|
|
|
|
}
|
2010-04-28 05:49:20 +00:00
|
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
|
|
throw new repository_exception('emptyfilelist', 'repository_recent');
|
|
|
|
}
|
2010-05-31 03:45:41 +00:00
|
|
|
$ret['list'] = array_filter($list, array($this, 'filter'));
|
2010-04-28 05:49:20 +00:00
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function get_type_option_names() {
|
2010-07-27 09:03:54 +00:00
|
|
|
return array('recentfilesnumber', 'pluginname');
|
2010-04-28 05:49:20 +00:00
|
|
|
}
|
|
|
|
|
2012-03-26 11:47:15 +02:00
|
|
|
public static function type_config_form($mform, $classname = 'repository') {
|
2012-03-18 18:31:32 +01:00
|
|
|
parent::type_config_form($mform, $classname);
|
2010-04-28 05:49:20 +00:00
|
|
|
$number = get_config('repository_recent', 'recentfilesnumber');
|
|
|
|
if (empty($number)) {
|
|
|
|
$number = DEFAULT_RECENT_FILES_NUM;
|
|
|
|
}
|
|
|
|
$mform->addElement('text', 'recentfilesnumber', get_string('recentfilesnumber', 'repository_recent'));
|
2013-04-05 03:06:15 +02:00
|
|
|
$mform->setType('recentfilesnumber', PARAM_INT);
|
2010-04-29 03:08:41 +00:00
|
|
|
$mform->setDefault('recentfilesnumber', $number);
|
2010-04-28 05:49:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This plugin doesn't support to link to external links
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function supported_returntypes() {
|
|
|
|
return FILE_INTERNAL;
|
|
|
|
}
|
2012-06-28 11:50:17 +08:00
|
|
|
|
2010-04-28 16:49:51 +00:00
|
|
|
/**
|
2012-06-28 11:50:17 +08:00
|
|
|
* Repository method to make sure that user can access particular file.
|
|
|
|
*
|
|
|
|
* This is checked when user tries to pick the file from repository to deal with
|
|
|
|
* potential parameter substitutions is request
|
2010-04-28 16:49:51 +00:00
|
|
|
*
|
2012-06-28 11:50:17 +08:00
|
|
|
* @todo MDL-33805 remove this function when recent files are managed correctly
|
|
|
|
*
|
|
|
|
* @param string $source
|
|
|
|
* @return bool whether the file is accessible by current user
|
2010-04-28 16:49:51 +00:00
|
|
|
*/
|
2012-06-28 11:50:17 +08:00
|
|
|
public function file_is_accessible($source) {
|
2012-05-31 12:58:34 +08:00
|
|
|
global $USER;
|
2014-06-26 08:53:25 +08:00
|
|
|
$reference = $this->get_file_reference($source);
|
|
|
|
$file = self::get_moodle_file($reference);
|
2012-06-28 11:50:17 +08:00
|
|
|
return (!empty($file) && $file->get_userid() == $USER->id);
|
2011-05-02 10:11:19 +08:00
|
|
|
}
|
2010-04-28 16:49:51 +00:00
|
|
|
|
2011-05-02 10:11:19 +08:00
|
|
|
/**
|
|
|
|
* Does this repository used to browse moodle files?
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function has_moodle_files() {
|
|
|
|
return true;
|
2010-04-28 16:49:51 +00:00
|
|
|
}
|
2013-02-04 14:58:46 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Is this repository accessing private data?
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function contains_private_data() {
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-28 05:49:20 +00:00
|
|
|
}
|