PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/moodle/lib/form/filemanager.php

#
PHP | 342 lines | 232 code | 52 blank | 58 comment | 16 complexity | 999622a2c17244c8a811c2a6276f9b1f MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, BSD-3-Clause, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, Apache-2.0
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * File manager
  18. *
  19. * @package moodlecore
  20. * @subpackage file
  21. * @copyright 1999 onwards Dongsheng Cai <dongsheng@moodle.com>
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. global $CFG;
  25. require_once('HTML/QuickForm/element.php');
  26. require_once($CFG->dirroot.'/lib/filelib.php');
  27. require_once($CFG->dirroot.'/repository/lib.php');
  28. class MoodleQuickForm_filemanager extends HTML_QuickForm_element {
  29. public $_helpbutton = '';
  30. protected $_options = array('mainfile'=>'', 'subdirs'=>1, 'maxbytes'=>-1, 'maxfiles'=>-1, 'accepted_types'=>'*', 'return_types'=>FILE_INTERNAL);
  31. function MoodleQuickForm_filemanager($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
  32. global $CFG, $PAGE;
  33. $options = (array)$options;
  34. foreach ($options as $name=>$value) {
  35. if (array_key_exists($name, $this->_options)) {
  36. $this->_options[$name] = $value;
  37. }
  38. }
  39. if (!empty($options['maxbytes'])) {
  40. $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $options['maxbytes']);
  41. }
  42. $this->_type = 'filemanager';
  43. parent::HTML_QuickForm_element($elementName, $elementLabel, $attributes);
  44. }
  45. function setName($name) {
  46. $this->updateAttributes(array('name'=>$name));
  47. }
  48. function getName() {
  49. return $this->getAttribute('name');
  50. }
  51. function setValue($value) {
  52. $this->updateAttributes(array('value'=>$value));
  53. }
  54. function getValue() {
  55. return $this->getAttribute('value');
  56. }
  57. function getMaxbytes() {
  58. return $this->_options['maxbytes'];
  59. }
  60. function setMaxbytes($maxbytes) {
  61. global $CFG;
  62. $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $maxbytes);
  63. }
  64. function getSubdirs() {
  65. return $this->_options['subdirs'];
  66. }
  67. function setSubdirs($allow) {
  68. $this->_options['subdirs'] = $allow;
  69. }
  70. function getMaxfiles() {
  71. return $this->_options['maxfiles'];
  72. }
  73. function setMaxfiles($num) {
  74. $this->_options['maxfiles'] = $num;
  75. }
  76. function setHelpButton($helpbuttonargs, $function='helpbutton'){
  77. debugging('component setHelpButton() is not used any more, please use $mform->setHelpButton() instead');
  78. }
  79. function getHelpButton() {
  80. return $this->_helpbutton;
  81. }
  82. function getElementTemplateType() {
  83. if ($this->_flagFrozen){
  84. return 'nodisplay';
  85. } else {
  86. return 'default';
  87. }
  88. }
  89. function toHtml() {
  90. global $CFG, $USER, $COURSE, $PAGE, $OUTPUT;
  91. require_once("$CFG->dirroot/repository/lib.php");
  92. // security - never ever allow guest/not logged in user to upload anything or use this element!
  93. if (isguestuser() or !isloggedin()) {
  94. print_error('noguest');
  95. }
  96. if ($this->_flagFrozen) {
  97. return $this->getFrozenHtml();
  98. }
  99. $id = $this->_attributes['id'];
  100. $elname = $this->_attributes['name'];
  101. $subdirs = $this->_options['subdirs'];
  102. $maxbytes = $this->_options['maxbytes'];
  103. $draftitemid = $this->getValue();
  104. $accepted_types = $this->_options['accepted_types'];
  105. if (empty($draftitemid)) {
  106. // no existing area info provided - let's use fresh new draft area
  107. require_once("$CFG->libdir/filelib.php");
  108. $this->setValue(file_get_unused_draft_itemid());
  109. $draftitemid = $this->getValue();
  110. }
  111. $client_id = uniqid();
  112. // filemanager options
  113. $options = new stdClass();
  114. $options->mainfile = $this->_options['mainfile'];
  115. $options->maxbytes = $this->_options['maxbytes'];
  116. $options->maxfiles = $this->getMaxfiles();
  117. $options->client_id = $client_id;
  118. $options->itemid = $draftitemid;
  119. $options->subdirs = $this->_options['subdirs'];
  120. $options->target = $id;
  121. $options->accepted_types = $accepted_types;
  122. $options->return_types = FILE_INTERNAL;
  123. $options->context = $PAGE->context;
  124. $html = $this->_getTabs();
  125. $html .= form_filemanager_render($options);
  126. $html .= '<input value="'.$draftitemid.'" name="'.$elname.'" type="hidden" />';
  127. // label element needs 'for' attribute work
  128. $html .= '<input value="" id="id_'.$elname.'" type="hidden" />';
  129. return $html;
  130. }
  131. }
  132. /**
  133. * Data structure representing a file manager.
  134. *
  135. * @copyright 2010 Dongsheng Cai
  136. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  137. * @since Moodle 2.0
  138. */
  139. class form_filemanaer_x {
  140. //TODO: do not use this abstraction (skodak)
  141. public $options;
  142. public function __construct(stdClass $options) {
  143. global $CFG, $USER, $PAGE;
  144. require_once($CFG->dirroot. '/repository/lib.php');
  145. $defaults = array(
  146. 'maxbytes'=>-1,
  147. 'maxfiles'=>-1,
  148. 'itemid'=>0,
  149. 'subdirs'=>0,
  150. 'client_id'=>uniqid(),
  151. 'accepted_types'=>'*',
  152. 'return_types'=>FILE_INTERNAL,
  153. 'context'=>$PAGE->context
  154. );
  155. foreach ($defaults as $key=>$value) {
  156. if (empty($options->$key)) {
  157. $options->$key = $value;
  158. }
  159. }
  160. $fs = get_file_storage();
  161. // initilise options, getting files in root path
  162. $this->options = file_get_drafarea_files($options->itemid, '/');
  163. // calculate file count
  164. $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
  165. $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $options->itemid, 'id', false);
  166. $filecount = count($files);
  167. $this->options->filecount = $filecount;
  168. // copying other options
  169. foreach ($options as $name=>$value) {
  170. $this->options->$name = $value;
  171. }
  172. // building file picker options
  173. $params = new stdClass();
  174. $params->accepted_types = $options->accepted_types;
  175. $params->return_types = $options->return_types;
  176. $params->context = $options->context;
  177. $params->env = 'filemanager';
  178. $params->disable_types = !empty($options->disable_types)?$options->disable_types:array();
  179. $filepicker_options = initialise_filepicker($params);
  180. $this->options->filepicker = $filepicker_options;
  181. }
  182. }
  183. /**
  184. * Print the file manager
  185. *
  186. * <pre>
  187. * $OUTPUT->file_manager($options);
  188. * </pre>
  189. *
  190. * @param array $options associative array with file manager options
  191. * options are:
  192. * maxbytes=>-1,
  193. * maxfiles=>-1,
  194. * itemid=>0,
  195. * subdirs=>false,
  196. * client_id=>uniqid(),
  197. * acepted_types=>'*',
  198. * return_types=>FILE_INTERNAL,
  199. * context=>$PAGE->context
  200. * @return string HTML fragment
  201. */
  202. function form_filemanager_render($options) {
  203. global $CFG, $OUTPUT, $PAGE;
  204. $fm = new form_filemanaer_x($options); //TODO: this is unnecessary here, the nested options are getting too complex
  205. static $filemanagertemplateloaded;
  206. $html = '';
  207. $options = $fm->options;
  208. $straddfile = get_string('addfile', 'repository');
  209. $strmakedir = get_string('makeafolder', 'moodle');
  210. $strdownload = get_string('downloadfolder', 'repository');
  211. $strloading = get_string('loading', 'repository');
  212. $icon_progress = $OUTPUT->pix_icon('i/loading_small', $strloading).'';
  213. $client_id = $options->client_id;
  214. $itemid = $options->itemid;
  215. list($context, $course, $cm) = get_context_info_array($options->context->id);
  216. if (is_object($course)) {
  217. $course_maxbytes = $course->maxbytes;
  218. } else {
  219. $course_maxbytes = $CFG->maxbytes;
  220. }
  221. if ($options->maxbytes == -1 || empty($options->maxbytes)) {
  222. $options->maxbytes = $CFG->maxbytes;
  223. }
  224. if (empty($options->filecount)) {
  225. $extra = ' style="display:none"';
  226. } else {
  227. $extra = '';
  228. }
  229. $maxsize = get_string('maxfilesize', 'moodle', display_size(get_max_upload_file_size($CFG->maxbytes, $course_maxbytes, $options->maxbytes)));
  230. $loading = get_string('loading', 'repository');
  231. $html .= <<<FMHTML
  232. <div class="filemanager-loading mdl-align" id='filemanager-loading-{$client_id}'>
  233. $icon_progress
  234. </div>
  235. <div id="filemanager-wrapper-{$client_id}" style="display:none">
  236. <div class="fm-breadcrumb" id="fm-path-{$client_id}"></div>
  237. <div class="filemanager-toolbar">
  238. <input type="button" class="fm-btn-add" id="btnadd-{$client_id}" onclick="return false" value="{$straddfile}" />
  239. <input type="button" class="fm-btn-mkdir" id="btncrt-{$client_id}" onclick="return false" value="{$strmakedir}" />
  240. <input type="button" class="fm-btn-download" id="btndwn-{$client_id}" onclick="return false" {$extra} value="{$strdownload}" />
  241. <span> $maxsize </span>
  242. </div>
  243. <div class="filemanager-container" id="filemanager-{$client_id}">
  244. <ul id="draftfiles-{$client_id}" class="fm-filelist">
  245. <li>{$loading}</li>
  246. </ul>
  247. </div>
  248. </div>
  249. <div class='clearer'></div>
  250. FMHTML;
  251. if (empty($filemanagertemplateloaded)) {
  252. $filemanagertemplateloaded = true;
  253. $html .= <<<FMHTML
  254. <div id="fm-template" style="display:none">___fullname___ ___action___</div>
  255. FMHTML;
  256. }
  257. $module = array(
  258. 'name'=>'form_filemanager',
  259. 'fullpath'=>'/lib/form/filemanager.js',
  260. 'requires' => array('core_filepicker', 'base', 'io-base', 'node', 'json', 'yui2-button', 'yui2-container', 'yui2-layout', 'yui2-menu', 'yui2-treeview'),
  261. 'strings' => array(array('loading', 'repository'), array('nomorefiles', 'repository'), array('confirmdeletefile', 'repository'),
  262. array('add', 'repository'), array('accessiblefilepicker', 'repository'), array('move', 'moodle'),
  263. array('cancel', 'moodle'), array('download', 'moodle'), array('ok', 'moodle'),
  264. array('emptylist', 'repository'), array('nofilesattached', 'repository'), array('entername', 'repository'), array('enternewname', 'repository'),
  265. array('zip', 'editor'), array('unzip', 'moodle'), array('rename', 'moodle'), array('delete', 'moodle'),
  266. array('cannotdeletefile', 'error'), array('confirmdeletefile', 'repository'),
  267. array('nopathselected', 'repository'), array('popupblockeddownload', 'repository'),
  268. array('draftareanofiles', 'repository'), array('path', 'moodle'), array('setmainfile', 'repository'),
  269. array('moving', 'repository'), array('files', 'moodle')
  270. )
  271. );
  272. $PAGE->requires->js_module($module);
  273. $PAGE->requires->js_init_call('M.form_filemanager.init', array($options), true, $module);
  274. // non javascript file manager
  275. $filemanagerurl = new moodle_url('/repository/draftfiles_manager.php', array(
  276. 'env'=>'filemanager',
  277. 'action'=>'browse',
  278. 'itemid'=>$itemid,
  279. 'subdirs'=>$options->subdirs,
  280. 'maxbytes'=>$options->maxbytes,
  281. 'maxfiles'=>$options->maxfiles,
  282. 'ctx_id'=>$PAGE->context->id,
  283. 'course'=>$PAGE->course->id,
  284. 'sesskey'=>sesskey(),
  285. ));
  286. $html .= '<noscript>';
  287. $html .= "<div><object type='text/html' data='$filemanagerurl' height='160' width='600' style='border:1px solid #000'></object></div>";
  288. $html .= '</noscript>';
  289. return $html;
  290. }