"REPOSITORY/MDL-18520, file system plugin added"
[moodle.git] / repository / filesystem / repository.class.php
CommitLineData
520de343 1<?php // $Id$
2
3class repository_filesystem extends repository {
4 public function __construct($repositoryid, $context = SITEID, $options = array()) {
5 parent::__construct($repositoryid, $context, $options);
6 $this->root_path = trim($this->root_path);
7 if ($options['ajax']) {
8 // if created from filepicker
9 if (empty($this->root_path)) {
10 $ret = array();
11 $ret['msg'] = get_string('invalidpath', 'repository_filesystem');
12 $ret['nosearch'] = true;
13 echo json_encode($ret);
14 exit;
15 } else {
16 if (!is_dir($this->root_path)) {
17 $ret = array();
18 $ret['msg'] = get_string('invalidpath', 'repository_filesystem');
19 $ret['nosearch'] = true;
20 if ($options['ajax']) {
21 echo json_encode($ret);
22 exit;
23 }
24 }
25 }
26 if ($this->root_path{strlen($this->root_path)-1} !== '/') {
27 $this->root_path .= '/';
28 }
29 }
30 }
31 public function get_listing($path = '', $page = '') {
32 global $CFG;
33 $list = array();
34 $list['list'] = array();
35 // process breacrumb trail
36 $list['path'] = array(
37 array('name'=>'Root','path'=>'')
38 );
39 $trail = '';
40 if (!empty($path)) {
41 $parts = explode('/', $path);
42 if (count($parts) > 1) {
43 foreach ($parts as $part) {
44 $trail .= ('/'.$part);
45 $list['path'][] = array('name'=>$part, 'path'=>$trail);
46 }
47 } else {
48 $list['path'][] = array('name'=>$path, 'path'=>$path);
49 }
50 $this->root_path .= ($path.'/');
51 }
52 // set options
53 $list['manage'] = false;
54 // dynamically loading
55 $list['dynload'] = true;
56 // the current path of this list.
57 // set to true, the login link will be removed
58 $list['nologin'] = true;
59 // set to true, the search button will be removed
60 $list['nosearch'] = true;
61 if ($dh = opendir($this->root_path)) {
62 while (($file = readdir($dh)) != false) {
63 if ( $file != '.' and $file !='..') {
64 if (filetype($this->root_path.$file) == 'file') {
65 $list['list'][] = array(
66 'title' => $file,
67 'source' => $path.'/'.$file,
68 'size' => filesize($this->root_path.$file),
69 'date' => time(),
70 'thumbnail' => $CFG->pixpath .'/f/'. mimeinfo('icon32', $this->root_path.$file)
71 );
72 } else {
73 if (!empty($path)) {
74 $current_path = $path . '/'. $file;
75 } else {
76 $current_path = $file;
77 }
78 $list['list'][] = array(
79 'title' => $file,
80 'children' => array(),
81 'thumbnail' => $CFG->pixpath .'/f/folder-32.png',
82 'path' => $current_path
83 );
84 }
85 }
86 }
87 }
88 return $list;
89 }
90 // login
91 public function check_login() {
92 return true;
93 }
94 // if check_login returns false,
95 // this function will be called to print a login form.
96 public function print_login() {
97 return true;
98 }
99 //search
100 // if this plugin support global search, if this function return
101 // true, search function will be called when global searching working
102 public function global_search() {
103 return false;
104 }
105 public function search($text) {
106 $search_result = array();
107 $search_result['list'] = array();
108 return $search_result;
109 }
110 // move file to local moodle
111 public function get_file($file, $title = '') {
112 global $CFG;
113 if ($file{0} == '/') {
114 $file = $this->root_path.substr($file, 1, strlen($file)-1);
115 }
116 // this is a hack to prevent move_to_file deleteing files
117 // in local repository
118 $CFG->repository_no_delete = true;
119 return $file;
120 }
121
122 public function logout() {
123 return true;
124 }
125
126 public static function get_instance_option_names() {
127 return array('root_path');
128 }
129
130 public function instance_config_form(&$mform) {
131 $mform->addElement('text', 'root_path', get_string('path', 'repository_filesystem'), array('value'=>'','size' => '40'));
132 }
133
134 public static function get_type_option_names() {
135 return array();
136 }
137 public function type_config_form(&$mform) {
138 }
139}