|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00028 define('DEFAULT_RECENT_FILES_NUM', 50); 00029 class repository_recent extends repository { 00030 00037 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) { 00038 parent::__construct($repositoryid, $context, $options); 00039 $number = get_config('recent', 'recentfilesnumber'); 00040 $number = (int)$number; 00041 if (empty($number)) { 00042 $this->number = DEFAULT_RECENT_FILES_NUM; 00043 } else { 00044 $this->number = $number; 00045 } 00046 } 00047 00052 public function print_login() { 00053 return $this->get_listing(); 00054 } 00055 00056 private function get_recent_files($limitfrom = 0, $limit = DEFAULT_RECENT_FILES_NUM) { 00057 // XXX: get current itemid 00058 global $USER, $DB, $itemid; 00059 $sql = 'SELECT files1.* 00060 FROM {files} files1 00061 JOIN ( 00062 SELECT contenthash, filename, MAX(id) AS id 00063 FROM {files} 00064 WHERE userid = :userid 00065 AND filename != :filename 00066 AND ((filearea = :filearea1 AND itemid = :itemid) OR filearea != :filearea2) 00067 GROUP BY contenthash, filename 00068 ) files2 ON files1.id = files2.id 00069 ORDER BY files1.timemodified DESC'; 00070 $params = array( 00071 'userid' => $USER->id, 00072 'filename' => '.', 00073 'filearea1' => 'draft', 00074 'itemid' => $itemid, 00075 'filearea2' => 'draft'); 00076 $rs = $DB->get_recordset_sql($sql, $params, $limitfrom, $limit); 00077 $result = array(); 00078 foreach ($rs as $file_record) { 00079 $info = array(); 00080 $info['contextid'] = $file_record->contextid; 00081 $info['itemid'] = $file_record->itemid; 00082 $info['filearea'] = $file_record->filearea; 00083 $info['component'] = $file_record->component; 00084 $info['filepath'] = $file_record->filepath; 00085 $info['filename'] = $file_record->filename; 00086 $result[$file_record->pathnamehash] = $info; 00087 } 00088 $rs->close(); 00089 return $result; 00090 } 00091 00099 public function get_listing($encodedpath = '', $page = '') { 00100 global $OUTPUT; 00101 $ret = array(); 00102 $ret['dynload'] = true; 00103 $ret['nosearch'] = true; 00104 $ret['nologin'] = true; 00105 $list = array(); 00106 $files = $this->get_recent_files(0, $this->number); 00107 00108 try { 00109 foreach ($files as $file) { 00110 $params = base64_encode(serialize($file)); 00111 // Check that file exists and accessible 00112 $filesize = $this->get_file_size($params); 00113 if (!empty($filesize)) { 00114 $node = array( 00115 'title' => $file['filename'], 00116 'size' => $filesize, 00117 'date' => '', 00118 'source'=> $params, 00119 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($file['filename'], 32))->out(false), 00120 ); 00121 $list[] = $node; 00122 } 00123 } 00124 } catch (Exception $e) { 00125 throw new repository_exception('emptyfilelist', 'repository_recent'); 00126 } 00127 $ret['list'] = array_filter($list, array($this, 'filter')); 00128 return $ret; 00129 } 00130 00131 public static function get_type_option_names() { 00132 return array('recentfilesnumber', 'pluginname'); 00133 } 00134 00135 public function type_config_form($mform) { 00136 parent::type_config_form($mform); 00137 $number = get_config('repository_recent', 'recentfilesnumber'); 00138 if (empty($number)) { 00139 $number = DEFAULT_RECENT_FILES_NUM; 00140 } 00141 $mform->addElement('text', 'recentfilesnumber', get_string('recentfilesnumber', 'repository_recent')); 00142 $mform->setDefault('recentfilesnumber', $number); 00143 } 00144 00150 public function supported_returntypes() { 00151 return FILE_INTERNAL; 00152 } 00164 public function copy_to_area($encoded, $draftitemid, $new_filepath, $new_filename) { 00165 global $USER, $DB; 00166 00167 $user_context = get_context_instance(CONTEXT_USER, $USER->id); 00168 00169 $fs = get_file_storage(); 00170 00171 $params = unserialize(base64_decode($encoded)); 00172 00173 $contextid = clean_param($params['contextid'], PARAM_INT); 00174 $fileitemid = clean_param($params['itemid'], PARAM_INT); 00175 $filename = clean_param($params['filename'], PARAM_FILE); 00176 $filepath = clean_param($params['filepath'], PARAM_PATH);; 00177 $filearea = clean_param($params['filearea'], PARAM_AREA); 00178 $component = clean_param($params['component'], PARAM_COMPONENT); 00179 00180 // XXX: 00181 // When user try to pick a file from other filearea, normally file api will use file browse to 00182 // operate the files with capability check, but in some areas, users don't have permission to 00183 // browse the files (for example, forum_attachment area). 00184 // 00185 // To get 'recent' plugin working, we need to use lower level file_stoarge class to bypass the 00186 // capability check, we will use a better workaround to improve it. 00187 if ($stored_file = $fs->get_file($contextid, $component, $filearea, $fileitemid, $filepath, $filename)) { 00188 // verify user id 00189 if ($USER->id != $stored_file->get_userid()) { 00190 throw new moodle_exception('errornotyourfile', 'repository'); 00191 } 00192 $file_record = array('contextid'=>$user_context->id, 'component'=>'user', 'filearea'=>'draft', 00193 'itemid'=>$draftitemid, 'filepath'=>$new_filepath, 'filename'=>$new_filename, 'sortorder'=>0); 00194 00195 // test if file already exists 00196 if (repository::draftfile_exists($draftitemid, $new_filepath, $new_filename)) { 00197 // create new file 00198 $unused_filename = repository::get_unused_filename($draftitemid, $new_filepath, $new_filename); 00199 $file_record['filename'] = $unused_filename; 00200 // create a tmp file 00201 $fs->create_file_from_storedfile($file_record, $stored_file); 00202 $event = array(); 00203 $event['event'] = 'fileexists'; 00204 $event['newfile'] = new stdClass; 00205 $event['newfile']->filepath = $new_filepath; 00206 $event['newfile']->filename = $unused_filename; 00207 $event['newfile']->url = moodle_url::make_draftfile_url($draftitemid, $new_filepath, $unused_filename)->out(); 00208 $event['existingfile'] = new stdClass; 00209 $event['existingfile']->filepath = $new_filepath; 00210 $event['existingfile']->filename = $new_filename; 00211 $event['existingfile']->url = moodle_url::make_draftfile_url($draftitemid, $new_filepath, $new_filename)->out();; 00212 return $event; 00213 } else { 00214 $fs->create_file_from_storedfile($file_record, $stored_file); 00215 $info = array(); 00216 $info['title'] = $new_filename; 00217 $info['itemid'] = $draftitemid; 00218 $info['filesize'] = $stored_file->get_filesize(); 00219 $info['url'] = moodle_url::make_draftfile_url($draftitemid, $new_filepath, $new_filename)->out();; 00220 $info['contextid'] = $user_context->id; 00221 return $info; 00222 } 00223 } 00224 return false; 00225 00226 } 00227 00233 public function has_moodle_files() { 00234 return true; 00235 } 00236 }