Skip to content

Instantly share code, notes, and snippets.

@marinaglancy
Created June 27, 2012 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marinaglancy/3001965 to your computer and use it in GitHub Desktop.
Save marinaglancy/3001965 to your computer and use it in GitHub Desktop.
MDL-33857
diff --git a/lib/filebrowser/file_info_context_module.php b/lib/filebrowser/file_info_context_module.php
index 0357ee3..0189e7c 100644
--- a/lib/filebrowser/file_info_context_module.php
+++ b/lib/filebrowser/file_info_context_module.php
@@ -244,6 +244,63 @@ class file_info_context_module extends file_info {
}
/**
+ * Checks if the module has any files inside that match specified extensions
+ *
+ * This function is called by repository_local_file::is_empty() to make sure this module needs
+ * to be displayed in 'Server files' repository. This is faster than retrieving all
+ * children and checking if they are empty or not.
+ *
+ * @param type $extensions
+ * @return type
+ */
+ public function has_files($extensions) {
+ global $DB;
+ if (has_capability('moodle/backup:backupactivity', $this->context)) {
+ $areas['backup'] = array('activity');
+ }
+
+ $areas = array_keys($this->areas);
+ if (plugin_supports('mod', $this->modname, FEATURE_MOD_INTRO, true) && has_capability('moodle/course:managefiles', $this->context)) {
+ $areas[] = 'intro';
+ }
+ $component = 'mod_'.$this->modname;
+
+ $sql = '';
+ $params = array();
+ if (count($areas)) {
+ list($sqlareas, $paramsareas) = $DB->get_in_or_equal($areas, SQL_PARAMS_NAMED, 'area');
+ $sql = "component = :component and filearea ".$sqlareas;
+ $params['component'] = 'mod_'.$this->modname;
+ $params = array_merge($params, $paramsareas);
+ }
+ if (!empty($sql) && has_capability('moodle/backup:backupactivity', $this->context)) {
+ $sql = "( (".$sql.") OR (component=:componentbackup and filearea = :areabackup))";
+ $params['componentbackup'] = 'backup';
+ $params['areabackup'] = 'activity';
+ }
+ if (empty($sql)) {
+ return false;
+ }
+ $sql .= ' AND contextid = :contextid AND filename <> :emptyfilename';
+ $params['contextid'] = $this->context->id;
+ $params['emptyfilename'] = '.';
+ if (!empty($extensions) && !is_array($extensions)) {
+ $extensions = array($extensions);
+ }
+ if (!empty($extensions) && !in_array('*', $extensions)) {
+ $likes = array();
+ $cnt = 0;
+ foreach ($extensions as $ext) {
+ $cnt++;
+ $likes[] = $DB->sql_like('filename', ':filename'.$cnt, false);
+ $params['filename'.$cnt] = '%'.$ext;
+ }
+ $sql .= ' AND ('.join(' OR ', $likes).')';
+ }
+ return $DB->record_exists_sql("select 1 from {files} WHERE ".$sql, $params);
+ }
+
+ /**
* Whether or not this is a directory
*
* @return bool
diff --git a/lib/filebrowser/file_info_stored.php b/lib/filebrowser/file_info_stored.php
index 44fa566..86daf9a 100644
--- a/lib/filebrowser/file_info_stored.php
+++ b/lib/filebrowser/file_info_stored.php
@@ -219,6 +219,42 @@ class file_info_stored extends file_info {
}
/**
+ * Checks if the folder has any files inside that match specified extensions
+ *
+ * This function is called by repository_local_file::is_empty() to make sure this folder needs
+ * to be displayed in 'Server files' repository. This is faster than retrieving all
+ * children and checking if they are empty or not with the standard functions.
+ *
+ * @param type $extensions
+ * @return type
+ */
+ public function has_files($extensions) {
+ global $DB;
+ $sql = 'contextid = :contextid AND component = :component AND filearea = :filearea AND '.
+ $DB->sql_like('filepath', ':filepath'). ' AND filename <> :emptyfilename';
+ $params = array();
+ $params['contextid'] = $this->lf->get_contextid();
+ $params['component'] = $this->lf->get_component();
+ $params['filearea'] = $this->lf->get_filearea();
+ $params['filepath'] = $this->lf->get_filepath().'%';
+ $params['emptyfilename'] = '.';
+ if (!empty($extensions) && !is_array($extensions)) {
+ $extensions = array($extensions);
+ }
+ if (!empty($extensions) && !in_array('*', $extensions)) {
+ $likes = array();
+ $cnt = 0;
+ foreach ($extensions as $ext) {
+ $cnt++;
+ $likes[] = $DB->sql_like('filename', ':filename'.$cnt, false);
+ $params['filename'.$cnt] = '%'.$ext;
+ }
+ $sql .= ' AND ('.join(' OR ', $likes).')';
+ }
+ return $DB->record_exists_sql("select 1 from {files} WHERE ".$sql, $params);
+ }
+
+ /**
* Returns file size in bytes, null for directories
*
* @return int bytes or null if not known
diff --git a/repository/local/lib.php b/repository/local/lib.php
index d8898bf..f43347f 100644
--- a/repository/local/lib.php
+++ b/repository/local/lib.php
@@ -298,7 +298,10 @@ class repository_local_file {
public function is_empty() {
if ($this->isempty === null) {
$this->isempty = true;
- if (!$this->fileinfo->is_empty_area()) {
+ if (method_exists($this->fileinfo, 'has_files')) {
+ $accepted_types = optional_param_array('accepted_types', '', PARAM_RAW);
+ $this->isempty = !$this->fileinfo->has_files($accepted_types);
+ } else if (!$this->fileinfo->is_empty_area()) {
// even if is_empty_area() returns false, element still may be empty
$children = $this->get_children();
if (!empty($children)) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment