MDL-68525 behat: chrome/switches caps not allowed in Chrome > 81
[moodle.git] / repository / recent / lib.php
blob35a7a0ad3c04336f94f1379d9f54d921c028c89e
1 <?php
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.
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/>.
18 /**
19 * This plugin is used to access recent used files
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
26 require_once($CFG->dirroot . '/repository/lib.php');
28 /**
29 * repository_recent class is used to browse recent used files
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
36 define('DEFAULT_RECENT_FILES_NUM', 50);
37 class repository_recent extends repository {
39 /**
40 * Initialize recent plugin
41 * @param int $repositoryid
42 * @param int $context
43 * @param array $options
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;
56 /**
57 * recent plugin doesn't require login, so list all files
58 * @return mixed
60 public function print_login() {
61 return $this->get_listing();
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;
101 $rs->close();
102 return $result;
106 * Get file listing
108 * @param string $encodedpath
109 * @param string $path not used by this plugin
110 * @return mixed
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);
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->image_url(file_file_icon($fileinfo, 24))->out(false),
139 'thumbnail' => $OUTPUT->image_url(file_file_icon($fileinfo, 90))->out(false),
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'];
148 $list[] = $node;
151 } catch (Exception $e) {
152 throw new repository_exception('emptyfilelist', 'repository_recent');
154 $ret['list'] = array_filter($list, array($this, 'filter'));
155 return $ret;
158 public static function get_type_option_names() {
159 return array('recentfilesnumber', 'pluginname');
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;
168 $mform->addElement('text', 'recentfilesnumber', get_string('recentfilesnumber', 'repository_recent'));
169 $mform->setType('recentfilesnumber', PARAM_INT);
170 $mform->setDefault('recentfilesnumber', $number);
174 * This plugin doesn't support to link to external links
176 * @return int
178 public function supported_returntypes() {
179 return FILE_INTERNAL;
183 * Repository method to make sure that user can access particular file.
185 * This is checked when user tries to pick the file from repository to deal with
186 * potential parameter substitutions is request
188 * @todo MDL-33805 remove this function when recent files are managed correctly
190 * @param string $source
191 * @return bool whether the file is accessible by current user
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);
201 * Does this repository used to browse moodle files?
203 * @return boolean
205 public function has_moodle_files() {
206 return true;
210 * Is this repository accessing private data?
212 * @return bool
214 public function contains_private_data() {
215 return false;