Skip to content

Commit

Permalink
MDL-18111 improving file api comments and docs, fixing license header…
Browse files Browse the repository at this point in the history
… + minor refactoring
  • Loading branch information
skodak committed May 20, 2009
1 parent 16a95e8 commit c05e975
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 39 deletions.
63 changes: 31 additions & 32 deletions lib/file/file_browser.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
<?php //$Id$
<?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/>.


/**
* Utility class for browsing of files.
*
* @package moodle-core
* @copyright 2008 Petr Skoda (http://skodak.org)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require_once("$CFG->libdir/file/file_info.php");
require_once("$CFG->libdir/file/file_info_module.php");
Expand All @@ -21,11 +45,16 @@
* file areas, and a file area contains folders and files. The various file_info
* subclasses return info about the things in this tree. They should be obtained
* from an instance of this class.
*
* This virtual tree is different for each user depending of his/her current permissions.
* Some branches such as draft areas are hidden, but accessible.
*
* Always use this abstraction when you need to access module files from core code.
*/
class file_browser {

/**
* Looks up file_info object
* Looks up file_info instance
* @param object $context
* @param string $filearea
* @param int $itemid
Expand All @@ -50,36 +79,6 @@ public function get_file_info($context, $filearea=null, $itemid=null, $filepath=
return null;
}

/**
* Returns content of local directory
*/
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, $topvisiblename, $itemidused, $readaccess, $writeaccess, false);
}

return $result;
}

/**
* Returns content of coursefiles directory
*/
public function build_coursefile_children($context, $filepath) {
$result = array();
$fs = get_file_storage();

$storedfiles = $fs->get_directory_files($context->id, 'course_content', 0, $filepath, false, true, "filepath, filename");
foreach ($storedfiles as $file) {
$result[] = new file_info_coursefile($this, $context, $file);
}

return $result;
}

public function encodepath($urlbase, $path, $forcedownload=false, $https=false) {
global $CFG;

Expand Down
37 changes: 34 additions & 3 deletions lib/file/file_info_coursefile.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
<?php //$Id$
<?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/>.


/**
* Class for browsing of legacy course files.
*
* @package moodle-core
* @copyright 2008 Petr Skoda (http://skodak.org)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

/**
* Subclass of file_info_stored for files in the course files area.
Expand Down Expand Up @@ -34,8 +58,15 @@ public function get_children() {
if (!$this->lf->is_directory()) {
return array();
}
return $this->browser->build_coursefile_children($this->context, $this->lf->get_filepath());
}

$result = array();
$fs = get_file_storage();

$storedfiles = $fs->get_directory_files($this->context->id, 'course_content', 0, $this->lf->get_filepath(), false, true, "filepath, filename");
foreach ($storedfiles as $file) {
$result[] = new file_info_coursefile($this->browser, $this->context, $file);
}

return $result;
}
}
41 changes: 37 additions & 4 deletions lib/file/file_info_stored.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
<?php //$Id$
<?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/>.


/**
* Utility class for browsing of stored files.
*
* @package moodle-core
* @copyright 2008 Petr Skoda (http://skodak.org)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

/**
* Represents an actual file or folder - a row in the file table -
Expand Down Expand Up @@ -111,9 +135,18 @@ public function get_children() {
if (!$this->lf->is_directory()) {
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->topvisiblename, $this->itemidused, $this->readaccess, $this->writeaccess,
$this->areaonly);

$result = array();
$fs = get_file_storage();

$storedfiles = $fs->get_directory_files($this->context->id, $this->lf->get_filearea(), $this->lf->get_itemid(),
$this->lf->get_filepath(), false, true, "filepath, filename");
foreach ($storedfiles as $file) {
$result[] = new file_info_stored($this->browser, $this->context, $file, $this->urlbase, $this->topvisiblename,
$this->itemidused, $this->readaccess, $this->writeaccess, false);
}

return $result;
}

public function get_parent() {
Expand Down

0 comments on commit c05e975

Please sign in to comment.