public function get_file_info($context, $filearea=null, $itemid=null, $filepath=null, $filename=null) {
global $USER, $CFG, $DB, $COURSE;
- $fs = get_file_storage();
+ if (empty($context->contextlevel)) {
+ throw new exception('incompletecontextobject');
+ } elseif ($contextname = $this->get_context_name($context->contextlevel)) {
+ $function_name = "get_file_info_$contextname";
- if ($context->contextlevel == CONTEXT_SYSTEM) {
- if (is_null($filearea)) {
- return new file_info_system($this);
+ if (method_exists($this, $function_name)) {
+ return $this->$function_name($context, $filearea, $itemid, $filepath, $filename);
+ } else {
+ throw new exception('undefinedmethod');
}
- //TODO: question files browsing
+ } else {
+ throw new exception('invalidcontext');
+ }
+ }
- } else if ($context->contextlevel == CONTEXT_USER) {
+ /**
+ * Returns info about the files at System context
+ * @param object $context
+ * @param string $filearea
+ * @return file_info_system
+ */
+ public function get_file_info_system($context, $filearea=null) {
+ if (is_null($filearea)) {
+ return new file_info_system($this);
+ }
+ //TODO: question files browsing
- if ($context->instanceid == $USER->id) {
- $user = $USER;
- } else {
- $user = $DB->get_record('user', array('id'=>$context->instanceid));
- }
+ return null;
+ }
- if (isguestuser($user) or empty($user->id)) {
- // no guests or not logged in users here
- return null;
- }
+ /**
+ * Returns info about the files at User context
+ * @param object $context
+ * @param string $filearea
+ * @return file_info_system
+ */
+ public function get_file_info_user($context, $filearea=null, $itemid=null, $filepath=null, $filename=null) {
+ global $USER, $DB;
+ if ($context->instanceid == $USER->id) {
+ $user = $USER;
+ } else {
+ $user = $DB->get_record('user', array('id'=>$context->instanceid));
+ }
+
+ if (isguestuser($user) or empty($user->id)) {
+ // no guests or not logged in users here
+ return null;
+ }
- if ($user->deleted) {
+ if ($user->deleted) {
+ return null;
+ }
+
+ if (is_null($filearea)) {
+ // access control: list areas only for myself
+ if ($context->instanceid != $USER->id) {
return null;
}
- if (is_null($filearea)) {
- // access control: list areas only for myself
- if ($context->instanceid != $USER->id) {
- return null;
- }
+ return new file_info_user($this, $context);
- return new file_info_user($this, $context);
+ } else {
+ $methodname = "get_file_info_$filearea";
+ if (method_exists($this, $methodname)) {
+ return $this->$methodname($user, $context, $filearea, $itemid, $filepath, $filename);
+ }
+ }
- } else {
- if ($filearea == 'user_private') {
- // access control: only my files for now, nobody else
- if ($context->instanceid != $USER->id) {
- return null;
- }
+ return null;
+ }
- if (is_null($itemid)) {
- return new file_info_user($this, $context);
- }
- $filepath = is_null($filepath) ? '/' : $filepath;
- $filename = is_null($filename) ? '.' : $filename;
-
- if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) {
- if ($filepath === '/' and $filename === '.') {
- $storedfile = new virtual_root_file($context->id, $filearea, 0);
- } else {
- // not found
- return null;
- }
- }
- $urlbase = $CFG->wwwroot.'/userfile.php';
- return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areauserpersonal', 'repository'), false, true, true, false);
+ private function get_file_info_user_private($user, $context, $filearea=null, $itemid=null, $filepath=null, $filename=null) {
+ global $USER, $CFG;
- } else if ($filearea == 'user_profile') {
- if (is_null($itemid)) {
- return new file_info_user($this, $context);
- }
+ $fs = get_file_storage();
- // access controll here must match user edit forms
- if ($user->id == $USER->id) {
- if (!has_capability('moodle/user:editownprofile', get_context_instance(CONTEXT_SYSTEM))) {
- return null;
- }
- } else {
- if (!has_capability('moodle/user:editprofile', $context) and !has_capability('moodle/user:update', $context)) {
- return null;
- }
- }
+ // access control: only my files for now, nobody else
+ if ($context->instanceid != $USER->id) {
+ return null;
+ }
- $filepath = is_null($filepath) ? '/' : $filepath;
- $filename = is_null($filename) ? '.' : $filename;
+ if (is_null($itemid)) {
+ return new file_info_user($this, $context);
+ }
+ $filepath = is_null($filepath) ? '/' : $filepath;
+ $filename = is_null($filename) ? '.' : $filename;
- if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) {
- if ($filepath === '/' and $filename === '.') {
- $storedfile = new virtual_root_file($context->id, $filearea, 0);
- } else {
- // not found
- return null;
- }
- }
- $urlbase = $CFG->wwwroot.'/userfile.php';
- return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areauserprofile', 'repository'), false, true, true, false);
+ if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) {
+ if ($filepath === '/' and $filename === '.') {
+ $storedfile = new virtual_root_file($context->id, $filearea, 0);
+ } else {
+ // not found
+ return null;
+ }
+ }
+ $urlbase = $CFG->wwwroot.'/userfile.php';
+ return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areauserpersonal', 'repository'), false, true, true, false);
+ }
+ public function get_file_info_user_profile($user, $context, $filearea=null, $itemid=null, $filepath=null, $filename=null) {
+ global $USER, $CFG;
- } else if ($filearea == 'user_draft') {
- // access control: only my files
- if ($context->instanceid != $USER->id) {
- return null;
- }
+ $fs = get_file_storage();
- if (empty($itemid)) {
- // do not browse itemids - you most know the draftid to see what is there
- return null;
- }
- $urlbase = $CFG->wwwroot.'/draftfile.php';
-
- $filepath = is_null($filepath) ? '/' : $filepath;
- $filename = is_null($filename) ? '.' : $filename;
-
- 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, get_string('areauserdraft', 'repository'), true, true, true, true);
- }
- }
+ if (is_null($itemid)) {
+ return new file_info_user($this, $context);
+ }
- } else if ($context->contextlevel == CONTEXT_COURSECAT) {
- if (!$category = $DB->get_record('course_categories', array('id'=>$context->instanceid))) {
+ // access controll here must match user edit forms
+ if ($user->id == $USER->id) {
+ if (!has_capability('moodle/user:editownprofile', get_context_instance(CONTEXT_SYSTEM))) {
+ return null;
+ }
+ } else {
+ if (!has_capability('moodle/user:editprofile', $context) and !has_capability('moodle/user:update', $context)) {
return null;
}
+ }
- if (!$category->visible and !has_capability('moodle/course:viewhiddencourses', $context)) {
+ $filepath = is_null($filepath) ? '/' : $filepath;
+ $filename = is_null($filename) ? '.' : $filename;
+
+ if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) {
+ if ($filepath === '/' and $filename === '.') {
+ $storedfile = new virtual_root_file($context->id, $filearea, 0);
+ } else {
+ // not found
return null;
}
+ }
+ $urlbase = $CFG->wwwroot.'/userfile.php';
+ return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areauserprofile', 'repository'), false, true, true, false);
+ }
- if (!is_null($filearea) and !in_array($filearea, array('coursecat_intro'))) {
- // file area does not exist, sorry
- $filearea = null;
- }
+ public function get_file_info_user_draft($user, $context, $filearea=null, $itemid=null, $filepath=null, $filename=null) {
+ global $USER, $CFG;
+ $fs = get_file_storage();
- if (is_null($filearea) or is_null($itemid)) {
- return new file_info_coursecat($this, $context, $category);
+ // access control: only my files
+ if ($context->instanceid != $USER->id) {
+ return null;
+ }
- } else {
- if ($filearea == 'coursecat_intro') {
- if (!has_capability('moodle/course:update', $context)) {
- return null;
- }
+ if (empty($itemid)) {
+ // do not browse itemids - you most know the draftid to see what is there
+ return null;
+ }
+ $urlbase = $CFG->wwwroot.'/draftfile.php';
- $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 = new virtual_root_file($context->id, $filearea, 0);
- } else {
- // not found
- return null;
- }
- }
- return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areacategoryintro', 'repository'), false, true, true, false);
- }
- }
+ $filepath = is_null($filepath) ? '/' : $filepath;
+ $filename = is_null($filename) ? '.' : $filename;
- } else if ($context->contextlevel == CONTEXT_COURSE) {
- if ($context->instanceid == $COURSE->id) {
- $course = $COURSE;
- } else if (!$course = $DB->get_record('course', array('id'=>$context->instanceid))) {
+ 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, get_string('areauserdraft', 'repository'), true, true, true, true);
+ }
- if (!$course->visible and !has_capability('moodle/course:viewhiddencourses', $context)) {
- return null;
- }
+ public function get_file_info_coursecat($context, $filearea=null, $itemid=null, $filepath=null, $filename=null) {
+ global $DB, $CFG;
- 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;
- }
+ $fs = get_file_storage();
- $filepath = is_null($filepath) ? '/' : $filepath;
- $filename = is_null($filename) ? '.' : $filename;
+ if (!$category = $DB->get_record('course_categories', array('id'=>$context->instanceid))) {
+ return null;
+ }
- if (is_null($filearea)) {
- return new file_info_course($this, $context, $course);
+ if (!$category->visible and !has_capability('moodle/course:viewhiddencourses', $context)) {
+ return null;
+ }
- } 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);
- }
+ if (!is_null($filearea) and !in_array($filearea, array('coursecat_intro'))) {
+ // file area does not exist, sorry
+ $filearea = null;
+ }
- $urlbase = $CFG->wwwroot.'/pluginfile.php';
- if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) {
- if ($filepath === '/' and $filename === '.') {
- $storedfile = new virtual_root_file($context->id, $filearea, 0);
- } else {
- // not found
- return null;
- }
- }
- return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areacourseintro', 'repository'), false, true, true, false);
+ if (is_null($filearea) or is_null($itemid)) {
+ return new file_info_coursecat($this, $context, $category);
- } else if ($filearea === 'course_section') {
- if (!has_capability('moodle/course:update', $context)) {
- return null;
- }
- $urlbase = $CFG->wwwroot.'/pluginfile.php';
+ } else {
+ if ($filearea == 'coursecat_intro') {
+ if (!has_capability('moodle/course:update', $context)) {
+ return null;
+ }
- if (empty($itemid)) {
- // list all sections
- return new file_info_coursesection($this, $context, $course);
- }
+ $filepath = is_null($filepath) ? '/' : $filepath;
+ $filename = is_null($filename) ? '.' : $filename;
- if (!$section = $DB->get_record('course_sections', array('course'=>$course->id, 'id'=>$itemid))) {
- return null; // does not exist
+ $urlbase = $CFG->wwwroot.'/pluginfile.php';
+ if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) {
+ if ($filepath === '/' and $filename === '.') {
+ $storedfile = new virtual_root_file($context->id, $filearea, 0);
+ } else {
+ // not found
+ return null;
}
+ }
+ return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areacategoryintro', 'repository'), false, true, true, false);
+ }
+ }
- 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);
+ return null;
+ }
- } 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);
- }
+ public function get_file_info_course($context, $filearea=null, $itemid=null, $filepath=null, $filename=null) {
+ global $DB, $COURSE;
- $urlbase = $CFG->wwwroot.'/pluginfile.php';
- if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) {
- if ($filepath === '/' and $filename === '.') {
- $storedfile = new virtual_root_file($context->id, $filearea, 0);
- } else {
- // not found
- return null;
- }
- }
+ if ($context->instanceid == $COURSE->id) {
+ $course = $COURSE;
+ } else if (!$course = $DB->get_record('course', array('id'=>$context->instanceid))) {
+ return null;
+ }
- $downloadable = has_capability('moodle/site:backupdownload', $context);
- $uploadable = has_capability('moodle/site:backupupload', $context);
- return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areabackup', 'repository'), false, $downloadable, $uploadable, false);
+ if (!$course->visible and !has_capability('moodle/course:viewhiddencourses', $context)) {
+ return null;
+ }
- } else if ($filearea == 'course_content') {
- if (!has_capability('moodle/course:managefiles', $context)) {
- return null;
- }
- if (is_null($itemid)) {
- return new file_info_course($this, $context, $course);
- }
+ 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;
+ }
- if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) {
- if ($filepath === '/' and $filename === '.') {
- $storedfile = new virtual_root_file($context->id, $filearea, 0);
- } else {
- // not found
- return null;
- }
- }
+ $filepath = is_null($filepath) ? '/' : $filepath;
+ $filename = is_null($filename) ? '.' : $filename;
- return new file_info_coursefile($this, $context, $storedfile);
- }
- }
+ if (is_null($filearea)) {
+ return new file_info_course($this, $context, $course);
- } else if ($context->contextlevel == CONTEXT_MODULE) {
- if (!$cm = get_coursemodule_from_id('', $context->instanceid)) {
- return null;
+ } else {
+ $methodname = "get_file_info_$filearea";
+ if (method_exists($this, $methodname)) {
+ return $this->$methodname($course, $context, $filearea, $itemid, $filepath, $filename);
}
+ }
+
+ return null;
+ }
+
+ public function get_file_info_course_intro($course, $context, $filearea=null, $itemid=null, $filepath=null, $filename=null) {
+ global $CFG;
+
+ $fs = get_file_storage();
- if ($cm->course == $COURSE->id) {
- $course = $COURSE;
- } else if (!$course = $DB->get_record('course', array('id'=>$cm->course))) {
+ 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)) {
+ if ($filepath === '/' and $filename === '.') {
+ $storedfile = new virtual_root_file($context->id, $filearea, 0);
+ } else {
+ // not found
return null;
}
+ }
+ return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areacourseintro', 'repository'), false, true, true, false);
+
+ }
- $modinfo = get_fast_modinfo($course);
+ public function get_file_info_course_section($course, $context, $filearea=null, $itemid=null, $filepath=null, $filename=null) {
+ global $CFG, $DB;
- if (empty($modinfo->cms[$cm->id]->uservisible)) {
+ $fs = get_file_storage();
+
+ 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);
- $modname = $modinfo->cms[$cm->id]->modname;
+ }
- $libfile = "$CFG->dirroot/mod/$modname/lib.php";
- if (!file_exists($libfile)) {
+ public function get_file_info_course_backup($course, $context, $filearea=null, $itemid=null, $filepath=null, $filename=null) {
+ global $CFG;
+
+ $fs = get_file_storage();
+
+ 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)) {
+ if ($filepath === '/' and $filename === '.') {
+ $storedfile = new virtual_root_file($context->id, $filearea, 0);
+ } else {
+ // not found
return null;
}
- require_once($libfile);
+ }
- $fileinfofunction = $modname.'_get_file_areas';
- if (function_exists($fileinfofunction)) {
- $areas = $fileinfofunction($course, $cm, $context);
+ $downloadable = has_capability('moodle/site:backupdownload', $context);
+ $uploadable = has_capability('moodle/site:backupupload', $context);
+ return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areabackup', 'repository'), false, $downloadable, $uploadable, false);
+
+ }
+
+ public function get_file_info_course_content($course, $context, $filearea=null, $itemid=null, $filepath=null, $filename=null) {
+ $fs = get_file_storage();
+
+ 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 === '.') {
+ $storedfile = new virtual_root_file($context->id, $filearea, 0);
} else {
+ // not found
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;
+ return new file_info_coursefile($this, $context, $storedfile);
+ }
- } else if ($filearea === $modname.'_intro') {
- if (!has_capability('moodle/course:managefiles', $context)) {
- return null;
- }
+ public function get_file_info_module($context, $filearea=null, $itemid=null, $filepath=null, $filename=null) {
+ global $COURSE, $DB, $CFG;
- $filepath = is_null($filepath) ? '/' : $filepath;
- $filename = is_null($filename) ? '.' : $filename;
+ $fs = get_file_storage();
- $urlbase = $CFG->wwwroot.'/pluginfile.php';
- if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) {
- if ($filepath === '/' and $filename === '.') {
- $storedfile = new virtual_root_file($context->id, $filearea, 0);
- } else {
- // not found
- return null;
- }
- }
- return new file_info_stored($this, $context, $storedfile, $urlbase, $areas[$filearea], false, true, true, false);
+ if (!$cm = get_coursemodule_from_id('', $context->instanceid)) {
+ return null;
+ }
- } else {
- $fileinfofunction = $modname.'_get_file_info';
- if (function_exists($fileinfofunction)) {
- return $fileinfofunction($this, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename);
+ 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 = new virtual_root_file($context->id, $filearea, 0);
+ } else {
+ // not found
+ return null;
}
}
+ return new file_info_stored($this, $context, $storedfile, $urlbase, $areas[$filearea], false, true, true, false);
+
+ } else {
+ $fileinfofunction = $modname.'_get_file_info';
+ if (function_exists($fileinfofunction)) {
+ return $fileinfofunction($this, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename);
+ }
}
return null;
return $return;
}
+ private function get_context_name($contextlevel) {
+ switch ($contextlevel) {
+ case CONTEXT_SYSTEM:
+ return 'system';
+ case CONTEXT_USER:
+ return 'user';
+ case CONTEXT_COURSECAT:
+ return 'coursecat';
+ case CONTEXT_COURSE:
+ return 'course';
+ case CONTEXT_MODULE:
+ return 'module';
+ }
+
+ return false;
+ }
}