// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
- * Temporary file manager for all moodle files. To be replaced by something much better.
+ * Moodle file tree viewer based on YUI2 Treeview
*
* @package moodlecore
* @subpackage file
- * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
+ * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require('../config.php');
-require_once($CFG->libdir.'/filelib.php');
-require_once($CFG->libdir.'/adminlib.php');
$courseid = optional_param('id', 0, PARAM_INT);
$filepath = optional_param('filepath', '', PARAM_PATH);
$filename = optional_param('filename', '', PARAM_FILE);
-$newdirname = optional_param('newdirname', '', PARAM_FILE);
-$delete = optional_param('delete', 0, PARAM_BOOL);
-
if ($courseid) {
$course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
$context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
$filename = null;
}
-$error = '';
-
$browser = get_file_browser();
$file_info = $browser->get_file_info($context, $filearea, $itemid, $filepath, $filename);
-/// process actions
-if ($file_info and $file_info->is_directory() and $file_info->is_writable() and $newdirname !== '' and data_submitted() and confirm_sesskey()) {
- if ($newdir_info = $file_info->create_directory($newdirname, $USER->id)) {
- $params = $newdir_info->get_params();
- redirect(new moodle_url('index.php', $params));
- } else {
- $error = "Could not create new dir"; // TODO: localise
- }
-}
-
-if ($file_info and $file_info->is_directory() and $file_info->is_writable() and isset($_FILES['newfile']) and data_submitted() and confirm_sesskey()) {
- $file = $_FILES['newfile'];
- $newfilename = clean_param($file['name'], PARAM_FILE);
- if (is_uploaded_file($_FILES['newfile']['tmp_name'])) {
- try {
- if ($newfile = $file_info->create_file_from_pathname($newfilename, $_FILES['newfile']['tmp_name'], $USER->id)) {
- $params = $file_info->get_params();
- redirect(new moodle_url('index.php', $params));
-
- } else {
- $error = "Could not create upload file"; // TODO: localise
- }
- } catch (file_exception $e) {
- $error = "Exception: Could not create upload file"; // TODO: localise
- }
- }
-}
-
-if ($file_info and $delete) {
- if (!data_submitted() or !confirm_sesskey()) {
- $params = $file_info->get_params();
- $params = array_merge($params, array('delete'=>1));
- $PAGE->set_url("/files/index.php", $params);
- $PAGE->set_title("$SITE->shortname: " .get_string('files'));
- $PAGE->set_heading($SITE->fullname);
- echo $OUTPUT->header();
- echo $OUTPUT->notification(get_string('deletecheckwarning').': '.$file_info->get_visible_name());
- $parent_info = $file_info->get_parent();
-
- $optionsno = $parent_info->get_params();
- $optionsyes = $file_info->get_params();
- $optionsyes['delete'] = 1;
- $optionsyes['sesskey'] = sesskey();
-
- echo $OUTPUT->confirm(get_string('deletecheckfiles'), new moodle_url( 'index.php', $optionsyes), new moodle_url('index.php', $optionsno));
- echo $OUTPUT->footer();
- die;
- }
-
- if ($parent_info = $file_info->get_parent() and $parent_info->is_writable()) {
- if (!$file_info->delete()) {
- $error = "Could not delete file!"; // TODO: localise
- }
- $params = $parent_info->get_params();
- redirect(new moodle_url('index.php', $params));
- }
-}
-
-
-/// print dir listing
-html_header($context, $file_info);
-
-if ($error !== '') {
- echo $OUTPUT->notification($error);
-}
-
-displaydir($file_info);
-
-if ($file_info and $file_info->is_directory() and $file_info->is_writable()) {
- echo '<br />';
-
- echo '<form action="index.php" method="post"><div>';
- echo '<input type="hidden" name="contextid" value="'.$contextid.'" />';
- echo '<input type="hidden" name="filearea" value="'.$filearea.'" />';
- echo '<input type="hidden" name="itemid" value="'.$itemid.'" />';
- echo '<input type="hidden" name="filepath" value="'.s($filepath).'" />';
- echo '<input type="hidden" name="filename" value="'.s($filename).'" />';
- echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
- echo '<input type="text" name="newdirname" value="" />';
- echo '<input type="submit" value="'.get_string('makeafolder').'" />';
- echo '</div></form>';
-
- echo '<br />';
-
- echo '<form enctype="multipart/form-data" method="post" action="index.php"><div>';
- echo '<input type="hidden" name="contextid" value="'.$contextid.'" />';
- echo '<input type="hidden" name="filearea" value="'.$filearea.'" />';
- echo '<input type="hidden" name="itemid" value="'.$itemid.'" />';
- echo '<input type="hidden" name="filepath" value="'.s($filepath).'" />';
- echo '<input type="hidden" name="filename" value="'.s($filename).'" />';
- echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
- echo '<input name="newfile" type="file" />';
- echo '<input type="submit" value="'.get_string('uploadafile').'" />';
- echo '</div></form>';
-}
-
-html_footer();
-
-/// UI functions /////////////////////////
-
-function html_footer() {
- global $OUTPUT;
- echo '</td></tr></table>';
- echo $OUTPUT->footer();
-}
-
-function html_header($context, $file_info){
- global $CFG, $SITE, $PAGE, $OUTPUT;
-
- $strfiles = get_string("files");
- if ($context->contextlevel == CONTEXT_MODULE) {
- $PAGE->set_pagelayout('incourse');
- } else if ($context->contextlevel == CONTEXT_COURSE) {
- $PAGE->set_pagelayout('course');
- } else {
- $PAGE->set_pagelayout('admin');
- }
- $PAGE->navbar->add($strfiles);
- $PAGE->set_url("/files/index.php", $file_info->get_params());
- $PAGE->set_title("$SITE->shortname: $strfiles");
- $PAGE->set_heading($SITE->fullname);
- echo $OUTPUT->header();
-
- echo "<table border=\"0\" style=\"margin-left:auto;margin-right:auto\" cellspacing=\"3\" cellpadding=\"3\" width=\"740\">";
- echo "<tr>";
- echo "<td colspan=\"2\">";
-}
-
-/// FILE FUNCTIONS ///////////////////////////////////////////////////////////
-
-function print_cell($alignment='center', $text=' ', $class='') {
- if ($class) {
- $class = ' class="'.$class.'"';
- }
- echo '<td align="'.$alignment.'" style="white-space:nowrap "'.$class.'>'.$text.'</td>';
+$strfiles = get_string("files");
+if ($context->contextlevel == CONTEXT_MODULE) {
+ $PAGE->set_pagelayout('incourse');
+} else if ($context->contextlevel == CONTEXT_COURSE) {
+ $PAGE->set_pagelayout('course');
+} else {
+ $PAGE->set_pagelayout('admin');
}
-function displaydir($file_info) {
- global $CFG, $OUTPUT;
-
- $children = $file_info->get_children();
- $parent_info = $file_info->get_parent();
-
- $strname = get_string('name');
- $strsize = get_string('size');
- $strmodified = get_string('modified');
- $strfolder = get_string('folder');
- $strfile = get_string('file');
- $strdownload = get_string('download');
- $strdelete = get_string('delete');
- $straction = get_string('action');
-
- $path = array();
- $params = $file_info->get_params_rawencoded();
- $params = implode('&', $params);
- $path[] = $file_info->get_visible_name();
-
- $level = $parent_info;
- while ($level) {
- $params = $level->get_params_rawencoded();
- $params = implode('&', $params);
- $path[] = '<a href="index.php?'.$params.'">'.$level->get_visible_name().'</a>';
- $level = $level->get_parent();
- }
-
- $path = array_reverse($path);
-
- $path = implode (' / ', $path);
- echo $path. ' /';
-
- echo "<div>";
- echo "<hr/>";
- echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"2\" width=\"740\" class=\"files\">";
- echo "<tr>";
- echo "<th class=\"header\" scope=\"col\"></th>";
- echo "<th class=\"header name\" scope=\"col\">$strname</th>";
- echo "<th class=\"header size\" scope=\"col\">$strsize</th>";
- echo "<th class=\"header date\" scope=\"col\">$strmodified</th>";
- echo "<th class=\"header commands\" scope=\"col\">$straction</th>";
- echo "</tr>\n";
-
- $parentwritable = $file_info->is_writable();
-
- if ($parent_info) {
- $params = $parent_info->get_params_rawencoded();
- $params = implode('&', $params);
+$PAGE->navbar->add($strfiles);
+$PAGE->set_url("/files/index.php", $file_info->get_params());
+$PAGE->set_title("$SITE->shortname: $strfiles");
+$PAGE->set_heading($SITE->fullname);
+echo $OUTPUT->header();
- echo "<tr class=\"folder\">";
- print_cell();
- print_cell('left', '<a href="index.php?'.$params.'"><img src="'.$OUTPUT->pix_url('f/parent') . '" class="icon" alt="" /> '.get_string('parentfolder').'</a>', 'name');
- print_cell();
- print_cell();
- print_cell();
+echo $OUTPUT->box_start();
+echo $OUTPUT->moodle_file_tree_viewer($context->id, $filearea, $itemid, $filepath);
+echo $OUTPUT->box_end();
- echo "</tr>";
- }
-
- if ($children) {
- foreach ($children as $child_info) {
- $filename = $child_info->get_visible_name();
- $filesize = $child_info->get_filesize();
- $filesize = $filesize ? display_size($filesize) : '';
- $filedate = $child_info->get_timemodified();
- $filedate = $filedate ? userdate($filedate) : '';
-
- $mimetype = $child_info->get_mimetype();
-
- $params = $child_info->get_params_rawencoded();
- $params = implode('&', $params);
-
- if ($child_info->is_directory()) {
-
- echo "<tr class=\"folder\">";
- print_cell();
- print_cell("left", "<a href=\"index.php?$params\"><img src=\"" . $OUTPUT->pix_url('f/folder') . "\" class=\"icon\" alt=\"$strfolder\" /> ".s($filename)."</a>", 'name');
- print_cell("right", $filesize, 'size');
- print_cell("right", $filedate, 'date');
- if ($parentwritable) {
- print_cell("right", "<a href=\"index.php?$params&sesskey=".sesskey()."&delete=1\"><img src=\"" . $OUTPUT->pix_url('t/delete') . "\" class=\"iconsmall\" alt=\"$strdelete\" /></a>", 'command');
- } else {
- print_cell();
- }
- echo "</tr>";
-
- } else {
-
- if ($downloadurl = $child_info->get_url(true)) {
- $downloadurl = " <a href=\"$downloadurl\" title=\"" . get_string('downloadfile') . "\"><img src=\"" . $OUTPUT->pix_url('t/down') . "\" class=\"iconsmall\" alt=\"$strdownload\" /></a>";
- } else {
- $downloadurl = '';
- }
-
- if ($viewurl = $child_info->get_url()) {
- $viewurl = " ".$OUTPUT->action_link($viewurl, "<img src=\"" . $OUTPUT->pix_url('t/preview') . "\" class=\"iconsmall\" alt=\"$strfile\" /> ",
- new popup_action('click', $viewurl, 'display', array('height' => 480, 'width' => 640)));
- } else {
- $viewurl = '';
- }
-
-
-
- echo "<tr class=\"file\">";
- print_cell();
- print_cell("left", "<img src=\"" . $OUTPUT->pix_url(file_mimetype_icon($mimetype)) . "\" class=\"icon\" alt=\"$strfile\" /> ".s($filename).$downloadurl.$viewurl, 'name');
- print_cell("right", $filesize, 'size');
- print_cell("right", $filedate, 'date');
- if ($parentwritable) {
- print_cell("right", "<a href=\"index.php?$params&sesskey=".sesskey()."&delete=1\"><img src=\"" . $OUTPUT->pix_url('t/delete') . "\" class=\"iconsmall\" alt=\"$strdelete\" /></a>", 'command');
- } else {
- print_cell();
- }
- echo "</tr>";
- }
- }
- }
-
- echo "</table>";
- echo "</div>";
- echo "<hr/>";
-
-}
+echo $OUTPUT->footer();
}
/**
- * Data structure representing a file tree viewer
+ * Data structure representing a area file tree viewer
*
* @copyright 2010 Dongsheng Cai
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.0
*/
-class file_tree_viewer implements renderable {
+class area_file_tree_viewer implements renderable {
public $dir;
public $result;
public $filearea;
/**
- * Constructor of file_tree_viewer class
+ * Constructor of area_file_tree_viewer class
* @param int $contextid
* @param string $area, file area
* @param int $itemid
}
}
+/**
+ * Data structure representing a general moodle file tree viewer
+ *
+ * @copyright 2010 Dongsheng Cai
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @since Moodle 2.0
+ */
+class moodle_file_tree_viewer implements renderable {
+ public $tree;
+ public $path;
+ /**
+ * Constructor of moodle_file_tree_viewer class
+ * @param int $contextid
+ * @param string $area, file area
+ * @param int $itemid
+ * @param string $urlbase, file serving url base
+ */
+ public function __construct($contextid, $filearea, $itemid, $filepath) {
+ global $CFG, $OUTPUT;
+ $this->tree = array();
+ $browser = get_file_browser();
+ $fileinfo = $browser->get_file_info(get_context_instance_by_id($contextid), $filearea, $itemid, $filepath);
+ $children = $fileinfo->get_children();
+ $parent_info = $fileinfo->get_parent();
+
+ $level = $parent_info;
+ $this->path = array();
+ while ($level) {
+ $params = $level->get_params();
+ $url = new moodle_url('/files/index.php', $params);
+ $this->path[] = html_writer::link($url->out(false), $level->get_visible_name());
+ $level = $level->get_parent();
+ }
+ $this->path = array_reverse($this->path);
+ $this->path[] = $fileinfo->get_visible_name();
+
+ foreach ($children as $child) {
+ $filedate = $child->get_timemodified();
+ $filesize = $child->get_filesize();
+ $mimetype = $child->get_mimetype();
+ $params = $child->get_params();
+ $url = new moodle_url('/files/index.php', $params);
+ $fileitem = array(
+ 'params'=>$params,
+ 'filename'=>$child->get_visible_name(),
+ 'filedate'=>$filedate ? userdate($filedate) : '',
+ 'filesize'=>$filesize ? display_size($filesize) : ''
+ );
+ if ($child->is_directory()) {
+ $fileitem['isdir'] = true;
+ $fileitem['url'] = $url->out(false);
+ } else {
+ $fileitem['url'] = $child->get_url();
+ }
+ $this->tree[] = $fileitem;
+ }
+
+ }
+}
+
/**
* Data structure representing a file picker.
*
if ($this->grouping !== null) {
$image->link['rel'] .= "[{$this->grouping}]";
}
-
+
$image->thumb['src'] = new moodle_url($thumburl);
$image->thumb['alt'] = $alt;
-
+
$this->images[] = $image;
}
}
return html_writer::tag('a', $output, $attributes);
}
+
+ /**
+ * General moodle file tree viwer
+ *
+ * <pre>
+ * $OUTPUT->moodle_file_tree_viewer($contextid, $filearea, $itemid, $filepath);
+ * </pre>
+ *
+ * @param int $contextid
+ * @param string $area
+ * @param int $itemid
+ * @param string $filepath
+ * @return string HTML fragment
+ */
+ public function moodle_file_tree_viewer($contextid, $filearea, $itemid, $filepath) {
+ $tree = new moodle_file_tree_viewer($contextid, $filearea, $itemid, $filepath);
+ return $this->render($tree);
+ }
+ public function render_moodle_file_tree_viewer(moodle_file_tree_viewer $tree) {
+ $html = '<div>';
+ foreach($tree->path as $path) {
+ $html .= $path;
+ $html .= ' / ';
+ }
+ $html .= '</div>';
+
+ $html .= '<div id="course-file-tree-view" class="filemanager-container">';
+ if (empty($tree->tree)) {
+ $html .= get_string('nofilesavailable', 'repository');
+ } else {
+ $this->page->requires->js_init_call('M.core_filetree.init');
+ $html .= '<ul>';
+ foreach($tree->tree as $node) {
+ $link_attributes = array();
+ if (!empty($node['isdir'])) {
+ $class = ' class="file-tree-folder"';
+ } else {
+ $class = ' class="file-tree-file"';
+ $link_attributes['target'] = '_blank';
+ }
+ $html .= '<li '.$class.'>';
+ $html .= html_writer::link($node['url'], $node['filename'], $link_attributes);
+ $html .= '</li>';
+ }
+ $html .= '</ul>';
+ }
+ $html .= '</div>';
+ return $html;
+ }
+
/**
- * Print the file tree viewer
+ * Print the area file tree viewer
*
* <pre>
- * $OUTPUT->file_tree_viewer($contextid, $filearea, $itemid, $urlbase);
+ * $OUTPUT->area_file_tree_viewer($contextid, $filearea, $itemid, $urlbase);
* </pre>
*
* @param int $contextid
* @param string $urlbase
* @return string HTML fragment
*/
- public function file_tree_viewer($contextid, $area, $itemid, $urlbase='') {
- $tree = new file_tree_viewer($contextid, $area, $itemid, $urlbase);
+ public function area_file_tree_viewer($contextid, $area, $itemid, $urlbase='') {
+ $tree = new area_file_tree_viewer($contextid, $area, $itemid, $urlbase);
return $this->render($tree);
}
/**
- * Internal implementation of file tree viewer rendering.
- * @param file_tree_viewer $tree
+ * Internal implementation of area file tree viewer rendering.
+ * @param area_file_tree_viewer $tree
* @return string
*/
- public function render_file_tree_viewer(file_tree_viewer $tree) {
+ public function render_area_file_tree_viewer(area_file_tree_viewer $tree) {
$this->page->requires->js_init_call('M.mod_folder.init_tree', array(true));
$html = '';
$html .= '<div id="folder_tree">';