Skip to content

Commit

Permalink
MDL-14589 implemented browsing of course section files + fixed bug wh…
Browse files Browse the repository at this point in the history
…en getting parent dirs of stored files
  • Loading branch information
skodak committed Feb 16, 2009
1 parent 23a6c9d commit 3156b8c
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 36 deletions.
43 changes: 39 additions & 4 deletions lib/file/file_browser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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");

Expand Down Expand Up @@ -200,22 +201,25 @@ public function get_file_info($context, $filearea=null, $itemid=null, $filepath=
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;
}

$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 {
if ($filearea === 'course_intro') {
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)) {
Expand All @@ -228,10 +232,38 @@ public function get_file_info($context, $filearea=null, $itemid=null, $filepath=
}
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)) {
Expand All @@ -251,6 +283,9 @@ public function get_file_info($context, $filearea=null, $itemid=null, $filepath=
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 === '.') {
Expand Down Expand Up @@ -335,13 +370,13 @@ public function get_file_info($context, $filearea=null, $itemid=null, $filepath=
/**
* 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;
Expand Down
21 changes: 9 additions & 12 deletions lib/file/file_info_course.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,19 @@ public function is_directory() {
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);
Expand Down
58 changes: 58 additions & 0 deletions lib/file/file_info_coursesection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php //$Id$

/**
* Represents a course category context in the tree navigated by @see{file_browser}.
*/
class file_info_coursesection extends file_info {
protected $course;

public function __construct($browser, $context, $course) {
parent::__construct($browser, $context);
$this->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);
}
}
32 changes: 13 additions & 19 deletions lib/file/file_info_stored.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -45,13 +45,7 @@ public function get_visible_name() {
$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;
}
Expand Down Expand Up @@ -118,7 +112,7 @@ public function get_children() {
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);
}

Expand All @@ -127,9 +121,9 @@ public function get_parent() {
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);
}
}

Expand Down
12 changes: 12 additions & 0 deletions lib/filelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion mod/scorm/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ public function get_parent() {
}
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();
}
Expand Down
27 changes: 27 additions & 0 deletions pluginfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 3156b8c

Please sign in to comment.