]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-16374 support for file browsing in modules
authorskodak <skodak>
Sat, 6 Sep 2008 17:00:43 +0000 (17:00 +0000)
committerskodak <skodak>
Sat, 6 Sep 2008 17:00:43 +0000 (17:00 +0000)
lib/file/file_browser.php
lib/file/file_info_course.php
lib/file/file_info_module.php
mod/forum/lib.php

index 1e29acc4e9e07dc6e201bd09c03d7103b6ba6c2c..9c3cffa88c829aa52d78a28fb485337c5b08fc5d 100644 (file)
@@ -1,6 +1,7 @@
 <?php  //$Id$
 
 require_once("$CFG->libdir/file/file_info.php");
+require_once("$CFG->libdir/file/file_info_module.php");
 require_once("$CFG->libdir/file/file_info_stored.php");
 require_once("$CFG->libdir/file/file_info_system.php");
 require_once("$CFG->libdir/file/file_info_user.php");
@@ -23,7 +24,7 @@ class file_browser {
      * @return object file_info object or null if not found or access not allowed
      */
     public function get_file_info($context, $filearea=null, $itemid=null, $filepath=null, $filename=null) {
-        global $USER, $CFG, $DB;
+        global $USER, $CFG, $DB, $COURSE;
 
         $fs = get_file_storage();
 
@@ -117,12 +118,13 @@ class file_browser {
                     }
                     // TODO: localise
                     return new file_info_stored($this, $context, $storedfile, $urlbase, 'Category introduction files', false, true, true);
-
                 }
             }
 
         } else if ($context->contextlevel == CONTEXT_COURSE) {
-            if (!$course = $DB->get_record('course', array('id'=>$context->instanceid))) {
+            if ($context->instanceid == $COURSE->id) {
+                $course = $COURSE;
+            } else if (!$course = $DB->get_record('course', array('id'=>$context->instanceid))) {
                 return null;
             }
 
@@ -198,7 +200,67 @@ class file_browser {
             }
 
         } else if ($context->contextlevel == CONTEXT_MODULE) {
-            //TODO
+            if (!$cm = get_coursemodule_from_id('', $context->instanceid)) {
+                return null;
+            }
+
+            if ($cm->course == $COURSE->id) {
+                $course = $COURSE;
+            } else if (!$course = $DB->get_record('course', array('id'=>$cm->course))) {
+                return null;
+            }
+
+            $modinfo = get_fast_modinfo($course);
+
+            if (empty($modinfo->cms[$cm->id]->uservisible)) {
+                return null;
+            }
+
+            $modname = $modinfo->cms[$cm->id]->modname;
+
+            $libfile = "$CFG->dirroot/mod/$modname/lib.php";
+            if (!file_exists($libfile)) {
+                return null;
+            }
+            require_once($libfile);
+
+            $fileinfofunction = $modname.'_get_file_areas';
+            if (function_exists($fileinfofunction)) {
+                $areas = $fileinfofunction($course, $cm, $context);
+            } else {
+                return null;
+            }
+            if (is_null($filearea) or is_null($itemid)) {
+                return new file_info_module($this, $course, $cm, $context, $areas);
+                
+            } else if (!isset($areas[$filearea])) {
+                return null; 
+
+            } else if ($filearea === $modname.'_intro') {
+                if (!has_capability('moodle/course:managefiles', $context)) {
+                    return null;
+                }
+
+                $filepath = is_null($filepath) ? '/' : $filepath;
+                $filename = is_null($filename) ? '.' : $filename;
+
+                $urlbase = $CFG->wwwroot.'/pluginfile.php';
+                if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) {
+                    if ($filepath === '/' and $filename === '.') {
+                        $storedfile = $fs->create_directory($context->id, $filearea, 0, $filepath);
+                    } else {
+                        // not found
+                        return null;
+                    }
+                }
+                return new file_info_stored($this, $context, $storedfile, $urlbase, $areas[$filearea], false, true, true);
+
+            } else {
+                $fileinfofunction = $modname.'_get_file_info';
+                if (function_exists($fileinfofunction)) {
+                    return $fileinfofunction($course, $cm, $context, $filearea);
+                }
+            }
         }
 
         return null;
index dd8bf799189143a735be9aa67ba70f2ce285f53d..f723b805bd9e21cc3feb795bd38d4ebed7a361ce 100644 (file)
@@ -50,6 +50,17 @@ class file_info_course extends file_info {
             }
         }
 
+        $modinfo = get_fast_modinfo($this->course);
+        foreach ($modinfo->cms as $cminfo) {
+            if (empty($cminfo->uservisible)) {
+                continue;
+            }
+            $modcontext = get_context_instance(CONTEXT_MODULE, $cminfo->id);
+            if ($child = $this->browser->get_file_info($modcontext)) {
+                $children[] = $child;
+            }
+        }
+
         return $children;
     }
 
index c4b1992db4d0b722319c23b675a5e4d21f43b240..7b1951eaf1d135371ec689bee080b119c739cc6c 100644 (file)
@@ -22,7 +22,7 @@ class file_info_module extends file_info {
     }
 
     public function get_visible_name() {
-        return $this->cm->name.' ('.$this->cm->modname.')';
+        return $this->cm->name.' ('.get_string('modulename', $this->cm->modname).')';
     }
 
     public function is_writable() {
index 2500ee54759fe3bd9dae9ce995c54e90622ef984..451e5808cea84ceacedb88328d5ca61c7f6f4852 100644 (file)
@@ -3976,6 +3976,17 @@ function forum_print_attachments($post, $cm, $type) {
     }
 }
 
+/**
+ * Lists all browsable file areas
+ */
+function forum_get_file_areas($course, $cm, $context) {
+    $areas = array();
+    if (has_capability('moodle/course:managefiles', $context)) {
+        $areas['forum_intro'] = 'Forum introduction files'; // TODO: localise
+    }
+    return $areas;
+}
+
 /**
  * Serves the forum attachments. Implements needed access control ;-)
  */