Skip to content

Commit

Permalink
"MDL-23308, bring course files back"
Browse files Browse the repository at this point in the history
  • Loading branch information
Dongsheng Cai committed Aug 30, 2010
1 parent b3dbdce commit e921afa
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 40 deletions.
64 changes: 64 additions & 0 deletions files/coursefilesedit.php
@@ -0,0 +1,64 @@
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

require_once('../config.php');
require_once(dirname(__FILE__) . '/coursefilesedit_form.php');
require_once($CFG->dirroot . '/repository/lib.php');

// current context
$contextid = required_param('contextid', PARAM_INT);
$component = 'course';
$filearea = 'legacy';
$itemid = 0;

list($context, $course, $cm) = get_context_info_array($contextid);

$url = new moodle_url('/files/coursefilesedit.php', array('contextid'=>$contextid));

require_login($course);
require_capability('moodle/course:managefiles', $context);

$heading = get_string('coursefiles') . ': ' . $course->fullname;
$PAGE->set_url($url);
$PAGE->set_context($context);
$PAGE->set_title($heading);
$PAGE->set_heading($heading);
$PAGE->set_pagelayout('admin');

$data = new object();
$options = array('subdirs'=>1, 'maxfiles'=>-1, 'accepted_types'=>'*', 'return_types'=>FILE_INTERNAL);
file_prepare_standard_filemanager($data, 'files', $options, $context, $component, $filearea, $itemid);
$form = new coursefiles_edit_form(null, array('data'=>$data, 'contextid'=>$contextid));

$returnurl = new moodle_url('/files/index.php', array('contextid'=>$contextid));

if ($form->is_cancelled()) {
redirect($returnurl);
}

if ($data = $form->get_data()) {
$data = file_postupdate_standard_filemanager($data, 'files', $options, $context, $component, $filearea, $itemid);
redirect($returnurl);
}

echo $OUTPUT->header();

echo $OUTPUT->container_start();
$form->display();
echo $OUTPUT->container_end();

echo $OUTPUT->footer();
30 changes: 30 additions & 0 deletions files/coursefilesedit_form.php
@@ -0,0 +1,30 @@
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

require_once($CFG->libdir.'/formslib.php');

class coursefiles_edit_form extends moodleform {
function definition() {
$mform =& $this->_form;
$contextid = $this->_customdata['contextid'];
$options = array('subdirs'=>1, 'maxfiles'=>-1, 'accepted_types'=>'*', 'return_types'=>FILE_INTERNAL);
$mform->addElement('filemanager', 'files_filemanager', get_string('files'), null, $options);
$mform->addElement('hidden', 'contextid', $this->_customdata['contextid']);
$this->set_data($this->_customdata['data']);
$this->add_action_buttons(true);
}
}
37 changes: 9 additions & 28 deletions files/index.php
Expand Up @@ -27,25 +27,14 @@
require('../config.php');

$contextid = optional_param('contextid', SYSCONTEXTID, PARAM_INT);
$component = optional_param('component', '', PARAM_ALPHAEXT);
$filearea = optional_param('filearea', '', PARAM_ALPHAEXT);
$itemid = optional_param('itemid', -1, PARAM_INT);
$filepath = optional_param('filepath', '', PARAM_PATH);
$filename = optional_param('filename', '', PARAM_FILE);
// hard-coded to course legacy area
$component = 'course';
$filearea = 'legacy';
$itemid = 0;

$PAGE->set_url('/files/index.php', array('contextid'=>$contextid, 'component'=>$component, 'filearea'=>$filearea, 'itemid'=>$itemid, 'filepath'=>$filepath, 'filename'=>$filename));

if ($component === '') {
$component = null;
}

if ($filearea === '') {
$filearea = null;
}

if ($itemid < 0) {
$itemid = null;
}
$PAGE->set_url('/files/index.php', array('contextid'=>$contextid, 'filepath'=>$filepath, 'filename'=>$filename));

if ($filepath === '') {
$filepath = null;
Expand All @@ -72,16 +61,9 @@
$PAGE->navbar->add($strfiles);
}

$PAGE->set_title("$SITE->shortname: $strfiles");
$PAGE->set_heading($SITE->fullname);

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->set_title("$course->shortname: $strfiles");
$PAGE->set_heading($course->fullname);
$PAGE->set_pagelayout('course');

$output = $PAGE->get_renderer('core', 'files');

Expand All @@ -90,13 +72,12 @@

if ($file_info) {
$options = array();
$options['context'] = $context;
//$options['visible_areas'] = array('backup'=>array('section', 'course'), 'course'=>array('legacy'), 'user'=>array('backup'));
echo $output->files_tree_viewer($file_info, $options);
} else {
notify(get_string('nofilesavailable', 'repository'));
}

echo $output->box_end();

echo $output->footer();

22 changes: 14 additions & 8 deletions files/renderer.php
Expand Up @@ -31,7 +31,7 @@
*/

/**
* File manager render
* File browser render
*
* @copyright 2010 Dongsheng Cai
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
Expand All @@ -46,7 +46,8 @@ public function files_tree_viewer(file_info $file_info, array $options = null) {

public function render_files_tree_viewer(files_tree_viewer $tree) {

$html = '<div>';
$html = $this->output->notification(get_string('coursefileswarning'), 'notifyproblem');
$html .= '<div class="file-tree-breadcrumb">';
foreach($tree->path as $path) {
$html .= $path;
$html .= ' / ';
Expand Down Expand Up @@ -74,6 +75,7 @@ public function render_files_tree_viewer(files_tree_viewer $tree) {
$html .= '</ul>';
}
$html .= '</div>';
$html .= $this->output->single_button(new moodle_url('/files/coursefilesedit.php', array('contextid'=>$tree->context->id)), get_string('coursefilesedit'), 'get');
return $html;
}
}
Expand All @@ -89,6 +91,7 @@ public function render_files_tree_viewer(files_tree_viewer $tree) {
class files_tree_viewer implements renderable {
public $tree;
public $path;
public $context;

/**
* Constructor of moodle_file_tree_viewer class
Expand All @@ -99,8 +102,9 @@ public function __construct(file_info $file_info, array $options = null) {
global $CFG;

//note: this MUST NOT use get_file_storage() !!!!!!!!!!!!!!!!!!!!!!!!!!!!

$this->options = (array)$options;
$this->context = $options['context'];

if (isset($this->options['visible_areas'])) {
$visible_areas = (array)$this->options['visible_areas'];
} else {
Expand All @@ -110,18 +114,20 @@ public function __construct(file_info $file_info, array $options = null) {
$this->tree = array();
$children = $file_info->get_children();
$parent_info = $file_info->get_parent();

$level = $parent_info;
$this->path = array();
while ($level) {
$params = $level->get_params();
$context = get_context_instance_by_id($params['contextid']);
// lock user in course level
if ($context->contextlevel == CONTEXT_COURSECAT or $context->contextlevel == CONTEXT_SYSTEM) {
if ($context->id != $this->context->id) {
break;
}
// unset unused parameters
unset($params['component']);
unset($params['filearea']);
unset($params['itemid']);
$url = new moodle_url('/files/index.php', $params);
$this->path[] = html_writer::link($url->out(false), $level->get_visible_name());
$this->path[] = html_writer::link($url, $level->get_visible_name());
$level = $level->get_parent();
}
$this->path = array_reverse($this->path);
Expand All @@ -132,13 +138,13 @@ public function __construct(file_info $file_info, array $options = null) {
$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) : ''
);
$url = new moodle_url('/files/index.php', $params);
if ($child->is_directory()) {
$fileitem['isdir'] = true;
$fileitem['url'] = $url->out(false);
Expand Down
2 changes: 2 additions & 0 deletions lang/en/moodle.php
Expand Up @@ -293,6 +293,8 @@
$string['coursecreatorsdescription'] = 'Course creators can create new courses.';
$string['coursedeleted'] = 'Deleted course {$a}';
$string['coursefiles'] = 'Course files';
$string['coursefilesedit'] = 'Edit course files';
$string['coursefileswarning'] = 'Course files are deprecated since Moodle 2.0, please use external repositories instead as much as possible.';
$string['courseformatdata'] = 'Course format data';
$string['courseformats'] = 'Course formats';
$string['coursegrades'] = 'Course grades';
Expand Down
8 changes: 4 additions & 4 deletions lib/navigationlib.php
Expand Up @@ -2905,9 +2905,9 @@ protected function load_course_settings($forceopen = false) {
}

// Manage files
if ($course->legacyfiles == 2 and has_capability('moodle/course:managefiles', $coursecontext)) {
if (has_capability('moodle/course:managefiles', $coursecontext)) {
// hidden in new courses and courses where legacy files were turned off
$url = new moodle_url('/files/index.php', array('contextid'=>$coursecontext->id, 'itemid'=>0, 'component' => 'course', 'filearea'=>'legacy'));
$url = new moodle_url('/files/index.php', array('contextid'=>$coursecontext->id));
$coursenode->add(get_string('courselegacyfiles'), $url, self::TYPE_SETTING, null, 'coursefiles', new pix_icon('i/files', ''));
}

Expand Down Expand Up @@ -3582,9 +3582,9 @@ protected function load_front_page_settings($forceopen = false) {
}

// Manage files
if ($course->legacyfiles == 2 and has_capability('moodle/course:managefiles', $this->context)) {
if (has_capability('moodle/course:managefiles', $this->context)) {
//hiden in new installs
$url = new moodle_url('/files/index.php', array('contextid'=>$coursecontext->id, 'itemid'=>0, 'component' => 'course', 'filearea'=>'legacy'));
$url = new moodle_url('/files/index.php', array('contextid'=>$coursecontext->id));
$frontpage->add(get_string('sitelegacyfiles'), $url, self::TYPE_SETTING, null, null, new pix_icon('i/files', ''));
}
return $frontpage;
Expand Down

0 comments on commit e921afa

Please sign in to comment.