return true;
}
+ /**
+ * Is this info area and is it "empty"? Are there any files in subfolders?
+ *
+ * This is used mostly in repositories to reduce the
+ * number of empty folders. This method may be very slow,
+ * use with care.
+ *
+ * @return bool
+ */
+ public function is_empty_area() {
+ return false;
+ }
+
/**
* Returns file size in bytes, null for directories
* @return int bytes or null if not known
* @return string url
*/
public function get_url($forcedownload=false, $https=false) {
- global $CFG;
-
if (!$this->is_readable()) {
return null;
}
return false;
}
+ /**
+ * Is this empty area?
+ *
+ * @return bool
+ */
+ public function is_empty_area() {
+ $fs = get_file_storage();
+ return $fs->is_area_empty($this->context->id, 'course', 'section');
+ }
+
/**
* Is directory?
* @return bool
return false;
}
+ /**
+ * Is this empty area?
+ *
+ * @return bool
+ */
+ public function is_empty_area() {
+ $fs = get_file_storage();
+ return $fs->is_area_empty($this->context->id, 'backup', 'section');
+ }
+
/**
* Is directory?
* @return bool
return false;
}
+ /**
+ * Is this empty area?
+ *
+ * @return bool
+ */
+ public function is_empty_area() {
+ if ($child = $this->get_area_backup(0, '/', '.')) {
+ if (!$child->is_empty_area()) {
+ return false;
+ }
+ }
+ if ($child = $this->get_area_intro(0, '/', '.')) {
+ if (!$child->is_empty_area()) {
+ return false;
+ }
+ }
+
+ foreach ($this->areas as $area=>$desctiption) {
+ if ($child = $this->get_file_info('mod_'.$this->modname, $area, null, null, null)) {
+ if (!$child->is_empty_area()) {
+ return false;
+ }
+ }
+ }
+
+ return true;
+ }
+
/**
* Is directory?
* @return bool
*/
public function get_params() {
return array('contextid'=>$this->context->id,
- 'component' =>$this->lf->get_component(),
+ 'component'=>$this->lf->get_component(),
'filearea' =>$this->lf->get_filearea(),
'itemid' =>$this->lf->get_itemid(),
'filepath' =>$this->lf->get_filepath(),
$contextid = $this->lf->get_contextid();
$component = $this->lf->get_component();
$filearea = $this->lf->get_filearea();
+ $itemid = $this->lf->get_itemid();
$filepath = $this->lf->get_filepath();
$filename = $this->lf->get_filename();
- $itemid = $this->lf->get_itemid();
if ($this->itemidused) {
$path = '/'.$contextid.'/'.$component.'/'.$filearea.'/'.$itemid.$filepath.$filename;
return $this->writeaccess;
}
+ /**
+ * Is this top of empty area?
+ *
+ * @return bool
+ */
+ public function is_empty_area() {
+ if ($this->lf->get_filepath() === '/' and $this->lf->get_filename() === '.') {
+ // test the emptiness only in the top most level, it does not make sense at lower levels
+ $fs = get_file_storage();
+ return $fs->is_area_empty($this->lf->get_contextid(), $this->lf->get_component(), $this->lf->get_filearea(), $this->lf->get_itemid());
+ } else {
+ return false;
+ }
+ }
+
/**
* Returns file size in bytes, null for directories
* @return int bytes or null if not known
return $this->get_file_by_hash($pathnamehash);
}
+ /**
+ * Are there any files (or directories)
+ * @param int $contextid
+ * @param string $component
+ * @param string $filearea
+ * @param bool|int $itemid tem id or false if all items
+ * @param bool $ignoredirs
+ * @return bool empty
+ */
+ public function is_area_empty($contextid, $component, $filearea, $itemid = false, $ignoredirs = true) {
+ global $DB;
+
+ $params = array('contextid'=>$contextid, 'component'=>$component, 'filearea'=>$filearea);
+ $where = "contextid = :contextid AND component = :component AND filearea = :filearea";
+
+ if ($itemid !== false) {
+ $params['itemid'] = $itemid;
+ $where .= " AND itemid = :itemid";
+ }
+
+ if ($ignoredirs) {
+ $sql = "SELECT 'x'
+ FROM {files}
+ WHERE $where AND filename <> '.'";
+ } else {
+ $sql = "SELECT 'x'
+ FROM {files}
+ WHERE $where AND (filename <> '.' OR filepath <> '/')";
+ }
+
+ return !$DB->record_exists_sql($sql, $params);
+ }
+
/**
* Returns all area files (optionally limited by itemid)
*
* @param bool $includedirs
* @return array of stored_files indexed by pathanmehash
*/
- public function get_area_files($contextid, $component, $filearea, $itemid=false, $sort="sortorder, itemid, filepath, filename", $includedirs = true) {
+ public function get_area_files($contextid, $component, $filearea, $itemid = false, $sort="sortorder, itemid, filepath, filename", $includedirs = true) {
global $DB;
$conditions = array('contextid'=>$contextid, 'component'=>$component, 'filearea'=>$filearea);
$children = $fileinfo->get_children();
foreach ($children as $child) {
if ($child->is_directory()) {
+ if ($child->is_empty_area()) {
+ continue;
+ }
$params = $child->get_params();
$subdir_children = $child->get_children();
//if (empty($subdir_children)) {