]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-14589 implemented browsing of course section files + fixed bug when getting paren...
authorskodak <skodak>
Mon, 16 Feb 2009 20:47:22 +0000 (20:47 +0000)
committerskodak <skodak>
Mon, 16 Feb 2009 20:47:22 +0000 (20:47 +0000)
lib/file/file_browser.php
lib/file/file_info_course.php
lib/file/file_info_coursesection.php [new file with mode: 0644]
lib/file/file_info_stored.php
lib/filelib.php
mod/scorm/lib.php
pluginfile.php

index ff2a0afd1b534ffb3dc69e119e53bd9d4cce4122..d6c196cab86bab372952e1246ad7e0b5cb7a3bbf 100644 (file)
@@ -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;
index 939555c0c97106a358587f9e0275dc4a45da74e2..707e31dd4452427e58d16b85ab40ed933c247739 100644 (file)
@@ -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 (file)
index 0000000..cbe1fb9
--- /dev/null
@@ -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);
+    }
+}
index 2bf82bf03c510dcacf9e8a205172f245d79c4087..0436af80689e8fc252e1fc1de69031f8e1e6e19f 100644 (file)
@@ -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);
             }
         }
 
index 294fca77c6798ec58ba90570b1756744707d3b7a..5e703c98bbb990390c7764c59857cf79dc3eb6cc 100644 (file)
@@ -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
index 3c0bd534b08fefe474e271b0158990ff8b2d3fb3..02587b07ecbd283b43bd06a558738231b981e9ec 100755 (executable)
@@ -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();
             }
index 7e95039629051c1d792eccdf7fbb69a506b1b086..4a7f327d09c337fa36a222578421d88167dccbf3 100644 (file)
             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);