Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/repository/filesystem/lib.php
Go to the documentation of this file.
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 
00031 class repository_filesystem extends repository {
00032     public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
00033         global $CFG;
00034         parent::__construct($repositoryid, $context, $options);
00035         $root = $CFG->dataroot.'/repository/';
00036         $subdir = $this->get_option('fs_path');
00037         $this->root_path = $root . $subdir . '/';
00038         if (!empty($options['ajax'])) {
00039             if (!is_dir($this->root_path)) {
00040                 $created = mkdir($this->root_path, $CFG->directorypermissions, true);
00041                 $ret = array();
00042                 $ret['msg'] = get_string('invalidpath', 'repository_filesystem');
00043                 $ret['nosearch'] = true;
00044                 if ($options['ajax'] && !$created) {
00045                     echo json_encode($ret);
00046                     exit;
00047                 }
00048             }
00049         }
00050     }
00051     public function get_listing($path = '', $page = '') {
00052         global $CFG, $OUTPUT;
00053         $list = array();
00054         $list['list'] = array();
00055         // process breacrumb trail
00056         $list['path'] = array(
00057             array('name'=>'Root', 'path'=>'')
00058         );
00059         $trail = '';
00060         if (!empty($path)) {
00061             $parts = explode('/', $path);
00062             if (count($parts) > 1) {
00063                 foreach ($parts as $part) {
00064                     if (!empty($part)) {
00065                         $trail .= ('/'.$part);
00066                         $list['path'][] = array('name'=>$part, 'path'=>$trail);
00067                     }
00068                 }
00069             } else {
00070                 $list['path'][] = array('name'=>$path, 'path'=>$path);
00071             }
00072             $this->root_path .= ($path.'/');
00073         }
00074         $list['manage'] = false;
00075         $list['dynload'] = true;
00076         $list['nologin'] = true;
00077         $list['nosearch'] = true;
00078         if ($dh = opendir($this->root_path)) {
00079             while (($file = readdir($dh)) != false) {
00080                 if ( $file != '.' and $file !='..') {
00081                     if (filetype($this->root_path.$file) == 'file') {
00082                         $list['list'][] = array(
00083                             'title' => $file,
00084                             'source' => $path.'/'.$file,
00085                             'size' => filesize($this->root_path.$file),
00086                             'date' => time(),
00087                             'thumbnail' => $OUTPUT->pix_url(file_extension_icon($this->root_path.$file, 32))->out(false)
00088                         );
00089                     } else {
00090                         if (!empty($path)) {
00091                             $current_path = $path . '/'. $file;
00092                         } else {
00093                             $current_path = $file;
00094                         }
00095                         $list['list'][] = array(
00096                             'title' => $file,
00097                             'children' => array(),
00098                             'thumbnail' => $OUTPUT->pix_url('f/folder-32')->out(false),
00099                             'path' => $current_path
00100                             );
00101                     }
00102                 }
00103             }
00104         }
00105         $list['list'] = array_filter($list['list'], array($this, 'filter'));
00106         return $list;
00107     }
00108     public function check_login() {
00109         return true;
00110     }
00111     public function print_login() {
00112         return true;
00113     }
00114     public function global_search() {
00115         return false;
00116     }
00121     public function get_file($file, $title = '') {
00122         global $CFG;
00123         if ($file{0} == '/') {
00124             $file = $this->root_path.substr($file, 1, strlen($file)-1);
00125         } else {
00126             $file = $this->root_path.$file;
00127         }
00128         // this is a hack to prevent move_to_file deleteing files
00129         // in local repository
00130         $CFG->repository_no_delete = true;
00131         return array('path'=>$file, 'url'=>'');
00132     }
00133 
00134     public function logout() {
00135         return true;
00136     }
00137 
00138     public static function get_instance_option_names() {
00139         return array('fs_path');
00140     }
00141 
00142     public function set_option($options = array()) {
00143         $options['fs_path'] = clean_param($options['fs_path'], PARAM_PATH);
00144         $ret = parent::set_option($options);
00145         return $ret;
00146     }
00147 
00148     public function instance_config_form($mform) {
00149         global $CFG, $PAGE;
00150         if (has_capability('moodle/site:config', get_system_context())) {
00151             $path = $CFG->dataroot . '/repository/';
00152             if (!is_dir($path)) {
00153                 mkdir($path, $CFG->directorypermissions, true);
00154             }
00155             if ($handle = opendir($path)) {
00156                 $fieldname = get_string('path', 'repository_filesystem');
00157                 $choices = array();
00158                 while (false !== ($file = readdir($handle))) {
00159                     if (is_dir($path.$file) && $file != '.' && $file!= '..') {
00160                         $choices[$file] = $file;
00161                         $fieldname = '';
00162                     }
00163                 }
00164                 if (empty($choices)) {
00165                     $mform->addElement('static', '', '', get_string('nosubdir', 'repository_filesystem', $path));
00166                     $mform->addElement('hidden', 'fs_path', '');
00167                 } else {
00168                     $mform->addElement('select', 'fs_path', $fieldname, $choices);
00169                     $mform->addElement('static', null, '',  get_string('information','repository_filesystem', $path));
00170                 }
00171                 closedir($handle);
00172             }
00173         } else {
00174             $mform->addElement('static', null, '',  get_string('nopermissions', 'error', get_string('configplugin', 'repository_filesystem')));
00175             return false;
00176         }
00177     }
00178 
00179     public function supported_returntypes() {
00180         return FILE_INTERNAL;
00181     }
00182 
00183     public static function create($type, $userid, $context, $params, $readonly=0) {
00184         global $PAGE;
00185         if (has_capability('moodle/site:config', get_system_context())) {
00186             return parent::create($type, $userid, $context, $params, $readonly);
00187         } else {
00188             require_capability('moodle/site:config', get_system_context());
00189             return false;
00190         }
00191     }
00192     public static function instance_form_validation($mform, $data, $errors) {
00193         if (empty($data['fs_path'])) {
00194             $errors['fs_path'] = get_string('invalidadminsettingname', 'error', 'fs_path');
00195         }
00196         return $errors;
00197     }
00198 }
 All Data Structures Namespaces Files Functions Variables Enumerations