From e921afa874a07bc33e6cb37a65560f2c4f4f07a3 Mon Sep 17 00:00:00 2001 From: Dongsheng Cai Date: Mon, 30 Aug 2010 08:22:14 +0000 Subject: [PATCH] "MDL-23308, bring course files back" --- files/coursefilesedit.php | 64 ++++++++++++++++++++++++++++++++++ files/coursefilesedit_form.php | 30 ++++++++++++++++ files/index.php | 37 +++++--------------- files/renderer.php | 22 +++++++----- lang/en/moodle.php | 2 ++ lib/navigationlib.php | 8 ++--- 6 files changed, 123 insertions(+), 40 deletions(-) create mode 100644 files/coursefilesedit.php create mode 100644 files/coursefilesedit_form.php diff --git a/files/coursefilesedit.php b/files/coursefilesedit.php new file mode 100644 index 00000000000..b7fc5bc52a5 --- /dev/null +++ b/files/coursefilesedit.php @@ -0,0 +1,64 @@ +. + +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(); diff --git a/files/coursefilesedit_form.php b/files/coursefilesedit_form.php new file mode 100644 index 00000000000..4ef7ae79401 --- /dev/null +++ b/files/coursefilesedit_form.php @@ -0,0 +1,30 @@ +. + +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); + } +} diff --git a/files/index.php b/files/index.php index dae4d8ea5dc..c1d7e1668f4 100644 --- a/files/index.php +++ b/files/index.php @@ -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; @@ -72,16 +61,9 @@ if ($node = $PAGE->settingsnav->find('coursefiles', navigation_node::TYPE_SETTIN $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'); @@ -90,6 +72,7 @@ echo $output->box_start(); 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 { @@ -97,6 +80,4 @@ if ($file_info) { } echo $output->box_end(); - echo $output->footer(); - diff --git a/files/renderer.php b/files/renderer.php index 141b3bc432c..1616a921a6d 100644 --- a/files/renderer.php +++ b/files/renderer.php @@ -31,7 +31,7 @@ defined('MOODLE_INTERNAL') || die(); */ /** - * File manager render + * File browser render * * @copyright 2010 Dongsheng Cai * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later @@ -46,7 +46,8 @@ class core_files_renderer extends plugin_renderer_base { public function render_files_tree_viewer(files_tree_viewer $tree) { - $html = '
'; + $html = $this->output->notification(get_string('coursefileswarning'), 'notifyproblem'); + $html .= '
'; foreach($tree->path as $path) { $html .= $path; $html .= ' / '; @@ -74,6 +75,7 @@ class core_files_renderer extends plugin_renderer_base { $html .= ''; } $html .= '
'; + $html .= $this->output->single_button(new moodle_url('/files/coursefilesedit.php', array('contextid'=>$tree->context->id)), get_string('coursefilesedit'), 'get'); return $html; } } @@ -89,6 +91,7 @@ class core_files_renderer extends plugin_renderer_base { class files_tree_viewer implements renderable { public $tree; public $path; + public $context; /** * Constructor of moodle_file_tree_viewer class @@ -99,8 +102,9 @@ class files_tree_viewer implements renderable { 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 { @@ -110,18 +114,20 @@ class files_tree_viewer implements renderable { $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); @@ -132,13 +138,13 @@ class files_tree_viewer implements renderable { $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); diff --git a/lang/en/moodle.php b/lang/en/moodle.php index 30a7769edce..5dc6a1ff95b 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -293,6 +293,8 @@ $string['coursecreators'] = 'Course creator'; $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'; diff --git a/lib/navigationlib.php b/lib/navigationlib.php index 6914e506b8e..8914a641739 100644 --- a/lib/navigationlib.php +++ b/lib/navigationlib.php @@ -2905,9 +2905,9 @@ class settings_navigation extends navigation_node { } // 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', '')); } @@ -3582,9 +3582,9 @@ class settings_navigation extends navigation_node { } // 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;