From 3156b8ca481190b29a6757fb317e902ba1d04418 Mon Sep 17 00:00:00 2001 From: skodak Date: Mon, 16 Feb 2009 20:47:22 +0000 Subject: [PATCH] MDL-14589 implemented browsing of course section files + fixed bug when getting parent dirs of stored files --- lib/file/file_browser.php | 43 +++++++++++++++++++-- lib/file/file_info_course.php | 21 +++++----- lib/file/file_info_coursesection.php | 58 ++++++++++++++++++++++++++++ lib/file/file_info_stored.php | 32 +++++++-------- lib/filelib.php | 12 ++++++ mod/scorm/lib.php | 2 +- pluginfile.php | 27 +++++++++++++ 7 files changed, 159 insertions(+), 36 deletions(-) create mode 100644 lib/file/file_info_coursesection.php diff --git a/lib/file/file_browser.php b/lib/file/file_browser.php index ff2a0afd1b..d6c196cab8 100644 --- a/lib/file/file_browser.php +++ b/lib/file/file_browser.php @@ -7,6 +7,7 @@ require_once("$CFG->libdir/file/file_info_system.php"); require_once("$CFG->libdir/file/file_info_user.php"); require_once("$CFG->libdir/file/file_info_coursecat.php"); require_once("$CFG->libdir/file/file_info_course.php"); +require_once("$CFG->libdir/file/file_info_coursesection.php"); require_once("$CFG->libdir/file/file_info_coursefile.php"); require_once("$CFG->libdir/file/virtual_root_file.php"); @@ -200,7 +201,7 @@ class file_browser { return null; } - if (!is_null($filearea) and !in_array($filearea, array('course_intro', 'course_content', 'course_backup'))) { + if (!is_null($filearea) and !in_array($filearea, array('course_intro', 'course_content', 'course_section', 'course_backup'))) { // file area does not exist, sorry $filearea = null; } @@ -208,7 +209,7 @@ class file_browser { $filepath = is_null($filepath) ? '/' : $filepath; $filename = is_null($filename) ? '.' : $filename; - if (is_null($filearea) or is_null($itemid)) { + if (is_null($filearea)) { return new file_info_course($this, $context, $course); } else { @@ -216,6 +217,9 @@ class file_browser { if (!has_capability('moodle/course:update', $context)) { return null; } + if (is_null($itemid)) { + return new file_info_course($this, $context, $course); + } $urlbase = $CFG->wwwroot.'/pluginfile.php'; if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) { @@ -228,10 +232,38 @@ class file_browser { } return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areacourseintro', 'repository'), false, true, true, false); + } else if ($filearea === 'course_section') { + if (!has_capability('moodle/course:update', $context)) { + return null; + } + $urlbase = $CFG->wwwroot.'/pluginfile.php'; + + if (empty($itemid)) { + // list all sections + return new file_info_coursesection($this, $context, $course); + } + + if (!$section = $DB->get_record('course_sections', array('course'=>$course->id, 'id'=>$itemid))) { + return null; // does not exist + } + + if (!$storedfile = $fs->get_file($context->id, $filearea, $itemid, $filepath, $filename)) { + if ($filepath === '/' and $filename === '.') { + $storedfile = new virtual_root_file($context->id, $filearea, $itemid); + } else { + // not found + return null; + } + } + return new file_info_stored($this, $context, $storedfile, $urlbase, $section->section, true, true, true, false); + } else if ($filearea == 'course_backup') { if (!has_capability('moodle/site:backup', $context) and !has_capability('moodle/site:restore', $context)) { return null; } + if (is_null($itemid)) { + return new file_info_course($this, $context, $course); + } $urlbase = $CFG->wwwroot.'/pluginfile.php'; if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) { @@ -251,6 +283,9 @@ class file_browser { if (!has_capability('moodle/course:managefiles', $context)) { return null; } + if (is_null($itemid)) { + return new file_info_course($this, $context, $course); + } if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) { if ($filepath === '/' and $filename === '.') { @@ -335,13 +370,13 @@ class file_browser { /** * Returns content of local directory */ - public function build_stored_file_children($context, $filearea, $itemid, $filepath, $urlbase, $areavisiblename, $itemidused, $readaccess, $writeaccess) { + public function build_stored_file_children($context, $filearea, $itemid, $filepath, $urlbase, $topvisiblename, $itemidused, $readaccess, $writeaccess) { $result = array(); $fs = get_file_storage(); $storedfiles = $fs->get_directory_files($context->id, $filearea, $itemid, $filepath, false, true, "filepath, filename"); foreach ($storedfiles as $file) { - $result[] = new file_info_stored($this, $context, $file, $urlbase, $areavisiblename, $itemidused, $readaccess, $writeaccess, false); + $result[] = new file_info_stored($this, $context, $file, $urlbase, $topvisiblename, $itemidused, $readaccess, $writeaccess, false); } return $result; diff --git a/lib/file/file_info_course.php b/lib/file/file_info_course.php index 939555c0c9..707e31dd44 100644 --- a/lib/file/file_info_course.php +++ b/lib/file/file_info_course.php @@ -35,22 +35,19 @@ class file_info_course extends file_info { public function get_children() { $children = array(); - if (has_capability('moodle/course:update', $this->context)) { - if ($child = $this->browser->get_file_info($this->context, 'course_intro', 0)) { - $children[] = $child; - } + if ($child = $this->browser->get_file_info($this->context, 'course_intro', 0)) { + $children[] = $child; + } + if ($child = $this->browser->get_file_info($this->context, 'course_section')) { + $children[] = $child; } - if (has_capability('moodle/site:backup', $this->context) or has_capability('moodle/site:restorep', $this->context)) { - if ($child = $this->browser->get_file_info($this->context, 'course_backup', 0)) { - $children[] = $child; - } + if ($child = $this->browser->get_file_info($this->context, 'course_backup', 0)) { + $children[] = $child; } - if (has_capability('moodle/course:managefiles', $this->context)) { - if ($child = $this->browser->get_file_info($this->context, 'course_content', 0)) { - $children[] = $child; - } + if ($child = $this->browser->get_file_info($this->context, 'course_content', 0)) { + $children[] = $child; } $modinfo = get_fast_modinfo($this->course); diff --git a/lib/file/file_info_coursesection.php b/lib/file/file_info_coursesection.php new file mode 100644 index 0000000000..cbe1fb95d6 --- /dev/null +++ b/lib/file/file_info_coursesection.php @@ -0,0 +1,58 @@ +course = $course; + } + + public function get_params() { + return array('contextid'=>$this->context->id, + 'filearea' =>'course_section', + 'itemid' =>null, + 'filepath' =>null, + 'filename' =>null); + } + + public function get_visible_name() { + $format = $this->course->format; + $sectionsname = get_string("coursesections$format","format_$format"); // TODO: localise + if ($sectionsname === "[[coursesections$format]]") { + $sectionsname = get_string("coursesections$format", 'repository'); // TODO: localise + } + + return $sectionsname; + } + + public function is_writable() { + return false; + } + + public function is_directory() { + return true; + } + + public function get_children() { + global $DB; + + $children = array(); + + $course_sections = $DB->get_records('course_sections', array('course'=>$this->course->id), 'section'); + foreach ($course_sections as $section) { + if ($child = $this->browser->get_file_info($this->context, 'course_section', $section->id)) { + $children[] = $child; + } + } + + return $children; + } + + public function get_parent() { + return $this->browser->get_file_info($this->context); + } +} diff --git a/lib/file/file_info_stored.php b/lib/file/file_info_stored.php index 2bf82bf03c..0436af8068 100644 --- a/lib/file/file_info_stored.php +++ b/lib/file/file_info_stored.php @@ -7,22 +7,22 @@ class file_info_stored extends file_info { protected $lf; protected $urlbase; - protected $areavisiblename; + protected $topvisiblename; protected $itemidused; protected $readaccess; protected $writeaccess; protected $areaonly; - public function __construct($browser, $context, $storedfile, $urlbase, $areavisiblename, $itemidused, $readaccess, $writeaccess, $areaonly) { + public function __construct($browser, $context, $storedfile, $urlbase, $topvisiblename, $itemidused, $readaccess, $writeaccess, $areaonly) { parent::__construct($browser, $context); - $this->lf = $storedfile; - $this->urlbase = $urlbase; - $this->areavisiblename = $areavisiblename; - $this->itemidused = $itemidused; - $this->readaccess = $readaccess; - $this->writeaccess = $writeaccess; - $this->areaonly = $areaonly; + $this->lf = $storedfile; + $this->urlbase = $urlbase; + $this->topvisiblename = $topvisiblename; + $this->itemidused = $itemidused; + $this->readaccess = $readaccess; + $this->writeaccess = $writeaccess; + $this->areaonly = $areaonly; } public function get_params() { @@ -45,13 +45,7 @@ class file_info_stored extends file_info { $dir = explode('/', $dir); $dir = array_pop($dir); if ($dir === '') { - if ($this->areaonly) { - return $this->areavisiblename; - } else if ($this->itemidused) { - return $this->lf->get_itemid(); - } else { - return $this->areavisiblename; - } + return $this->topvisiblename; } else { return $dir; } @@ -118,7 +112,7 @@ class file_info_stored extends file_info { return array(); } return $this->browser->build_stored_file_children($this->context, $this->lf->get_filearea(), $this->lf->get_itemid(), $this->lf->get_filepath(), - $this->urlbase, $this->areavisiblename, $this->itemidused, $this->readaccess, $this->writeaccess, + $this->urlbase, $this->topvisiblename, $this->itemidused, $this->readaccess, $this->writeaccess, $this->areaonly); } @@ -127,9 +121,9 @@ class file_info_stored extends file_info { if ($this->areaonly) { return null; } else if ($this->itemidused) { - return $this->browser->get_file_info($this->context, $this->lf->get_filearea(), $this->lf->get_itemid()); - } else { return $this->browser->get_file_info($this->context, $this->lf->get_filearea()); + } else { + return $this->browser->get_file_info($this->context); } } diff --git a/lib/filelib.php b/lib/filelib.php index 294fca77c6..5e703c98bb 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -165,6 +165,18 @@ function get_draftarea_info($draftitemid) { return array('filecount'=>count($draftfiles)); } +/** + * Returns draftitemid of given editor element. + * @param string $elname name of formlib editor element + * @return int 0 if not submitted yet + */ +function file_get_submitted_draftitemid($elname) { + if (!empty($_REQUEST[$elname]['itemid']) and confirm_sesskey()) { + return (int)$_REQUEST[$elname]['itemid']; + } + return 0; +} + /** * Converts absolute links in text and merges draft files to target area. * @param int $draftitemid diff --git a/mod/scorm/lib.php b/mod/scorm/lib.php index 3c0bd534b0..02587b07ec 100755 --- a/mod/scorm/lib.php +++ b/mod/scorm/lib.php @@ -734,7 +734,7 @@ function scorm_get_file_info($browser, $areas, $course, $cm, $context, $filearea } public function get_visible_name() { if ($this->lf->get_filepath() === '/' and $this->lf->get_filename() === '.') { - return $this->areavisiblename; + return $this->topvisiblename; } return parent::get_visible_name(); } diff --git a/pluginfile.php b/pluginfile.php index 7e95039629..4a7f327d09 100644 --- a/pluginfile.php +++ b/pluginfile.php @@ -141,6 +141,33 @@ session_get_instance()->write_close(); // unlock session during fileserving send_stored_file($file, 60*60, 0, false); // TODO: change timeout? + } else if ($filearea === 'course_section') { + if ($CFG->forcelogin) { + require_login($course); + } else if ($course->id !== SITEID) { + require_login($course); + } + + $sectionid = (int)array_shift($args); + + if ($course->numsections < $sectionid) { + if (!has_capability('moodle/course:update', $context)) { + // disable access to invisible sections if can not edit course + // this is going to break some ugly hacks, but is necessary + send_file_not_found(); + } + } + + $relativepath = '/'.implode('/', $args); + $fullpath = $context->id.'course_section'.$sectionid.$relativepath; + + if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) { + send_file_not_found(); + } + + session_get_instance()->write_close(); // unlock session during fileserving + send_stored_file($file, 60*60, 0, false); // TODO: change timeout? + } else if ($filearea === 'user_profile') { $userid = (int)array_shift($args); $usercontext = get_context_instance(CONTEXT_USER, $userid); -- 2.39.5